1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.media.tests;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.Option.Importance;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.result.ITestInvocationListener;
24 import com.android.tradefed.result.TestDescription;
25 import com.android.tradefed.testtype.IDeviceTest;
26 import com.android.tradefed.testtype.IRemoteTest;
27 import com.android.tradefed.util.CommandResult;
28 import com.android.tradefed.util.proto.TfMetricProtoUtil;
29 
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.concurrent.Semaphore;
33 
34 /**
35  * A harness that test video playback with multiple devices and reports result.
36  */
37 public class VideoMultimeterRunner extends VideoMultimeterTest
38     implements IDeviceTest, IRemoteTest {
39 
40     @Option(name = "robot-util-path", description = "path for robot control util",
41             importance = Importance.ALWAYS, mandatory = true)
42     String mRobotUtilPath = "/tmp/robot_util.sh";
43 
44     @Option(name = "device-map", description =
45             "Device serials map to location and audio input. May be repeated",
46             importance = Importance.ALWAYS)
47     Map<String, String> mDeviceMap = new HashMap<String, String>();
48 
49     @Option(name = "calibration-map", description =
50             "Device serials map to calibration values. May be repeated",
51             importance = Importance.ALWAYS)
52     Map<String, String> mCalibrationMap = new HashMap<String, String>();
53 
54     static final long ROBOT_TIMEOUT_MS = 60 * 1000;
55 
56     static final Semaphore runToken = new Semaphore(1);
57 
58     /**
59      * {@inheritDoc}
60      */
61     @Override
run(ITestInvocationListener listener)62     public void run(ITestInvocationListener listener)
63             throws DeviceNotAvailableException {
64         long durationMs = 0;
65         TestDescription testId = new TestDescription(getClass()
66                 .getCanonicalName(), RUN_KEY);
67 
68         listener.testRunStarted(RUN_KEY, 0);
69         listener.testStarted(testId);
70 
71         long testStartTime = System.currentTimeMillis();
72         Map<String, String> metrics = new HashMap<String, String>();
73 
74         try {
75             CLog.v("Waiting to acquire run token");
76             runToken.acquire();
77 
78             String deviceSerial = getDevice().getSerialNumber();
79 
80             String calibrationValue = (mCalibrationMap.containsKey(deviceSerial) ?
81                     mCalibrationMap.get(deviceSerial) : null);
82             if (mDebugWithoutHardware
83                     || (moveArm(deviceSerial) && setupTestEnv(calibrationValue))) {
84                 runMultimeterTest(listener, metrics);
85             } else {
86                 listener.testFailed(testId, "Failed to set up environment");
87             }
88         } catch (InterruptedException e) {
89             CLog.d("Acquire run token interrupted");
90             listener.testFailed(testId, "Failed to acquire run token");
91         } finally {
92             runToken.release();
93             listener.testEnded(testId, TfMetricProtoUtil.upgradeConvert(metrics));
94             durationMs = System.currentTimeMillis() - testStartTime;
95             listener.testRunEnded(durationMs, TfMetricProtoUtil.upgradeConvert(metrics));
96         }
97     }
98 
moveArm(String deviceSerial)99     protected boolean moveArm(String deviceSerial) {
100         if (mDeviceMap.containsKey(deviceSerial)) {
101             CLog.v("Moving robot arm to device " + deviceSerial);
102             CommandResult cr = getRunUtil().runTimedCmd(
103                     ROBOT_TIMEOUT_MS, mRobotUtilPath, mDeviceMap.get(deviceSerial));
104             CLog.v(cr.getStdout());
105             return true;
106         } else {
107             CLog.e("Cannot find device in map, test failed");
108             return false;
109         }
110     }
111 }
112