1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package com.android.cts.jank;
15 
16 import com.android.cts.tradefed.build.CtsBuildHelper;
17 import com.android.cts.tradefed.util.HostReportLog;
18 import com.android.cts.util.ResultType;
19 import com.android.cts.util.ResultUnit;
20 import com.android.ddmlib.IShellOutputReceiver;
21 import com.android.ddmlib.Log;
22 import com.android.ddmlib.Log.LogLevel;
23 import com.android.tradefed.build.IBuildInfo;
24 import com.android.tradefed.device.ITestDevice;
25 import com.android.tradefed.testtype.DeviceTestCase;
26 import com.android.tradefed.testtype.IAbi;
27 import com.android.tradefed.testtype.IAbiReceiver;
28 import com.android.tradefed.testtype.IBuildReceiver;
29 
30 import java.io.File;
31 import java.util.HashMap;
32 import java.util.Scanner;
33 
34 public class CtsHostJankTest extends DeviceTestCase implements IAbiReceiver, IBuildReceiver {
35 
36     private static final String TAG = CtsHostJankTest.class.getSimpleName();
37     private static final String DEVICE_LOCATION = "/data/local/tmp/";
38     // FIXME uiautomator is deprecated and does not support --abi flag
39     private static final String RUN_UI_AUTOMATOR_CMD = "uiautomator runtest %s -c %s";
40     private final String mHostTestClass;
41     private final String mDeviceTestClass;
42     private final String mJarName;
43     private final String mJarPath;
44     protected ITestDevice mDevice;
45     protected CtsBuildHelper mBuild;
46     protected IAbi mAbi;
47 
CtsHostJankTest(String jarName, String deviceTestClass, String hostTestClass)48     public CtsHostJankTest(String jarName, String deviceTestClass, String hostTestClass) {
49         this.mHostTestClass = hostTestClass;
50         this.mDeviceTestClass = deviceTestClass;
51         this.mJarName = jarName;
52         this.mJarPath = DEVICE_LOCATION + jarName;
53     }
54 
55     @Override
setAbi(IAbi abi)56     public void setAbi(IAbi abi) {
57         mAbi = abi;
58     }
59 
60     @Override
setBuild(IBuildInfo buildInfo)61     public void setBuild(IBuildInfo buildInfo) {
62         mBuild = CtsBuildHelper.createBuildHelper(buildInfo);
63     }
64 
65     @Override
setUp()66     protected void setUp() throws Exception {
67         super.setUp();
68         mDevice = getDevice();
69         // Push jar to device.
70         File jarFile = mBuild.getTestApp(mJarName);
71         boolean result = mDevice.pushFile(jarFile, mJarPath);
72         assertTrue("Failed to push file to " + mJarPath, result);
73     }
74 
75     @Override
tearDown()76     protected void tearDown() throws Exception {
77         // Delete jar from device.
78         mDevice.executeShellCommand("rm " + mJarPath);
79         super.tearDown();
80     }
81 
runUiAutomatorTest(String testName)82     public void runUiAutomatorTest(String testName) throws Exception {
83         // Delete any existing result files
84         mDevice.executeShellCommand("rm -r " + DEVICE_LOCATION + "*.txt");
85 
86         // Run ui automator test.
87         mDevice.executeShellCommand(
88                 String.format(RUN_UI_AUTOMATOR_CMD, mJarName, mDeviceTestClass + "#" + testName),
89                 new IShellOutputReceiver() {
90                     private StringBuilder sb = new StringBuilder();
91 
92                     @Override
93                     public void addOutput(byte[] data, int offset, int length) {
94                         byte[] raw = new byte[length];
95                         for (int i = 0; i < length; i++) {
96                             raw[i] = data[i + offset];
97                         }
98                         sb.append(new String(raw));
99                     }
100 
101                     @Override
102                     public void flush() {
103                         Log.logAndDisplay(LogLevel.INFO, TAG, sb.toString());
104                     }
105 
106                     @Override
107                     public boolean isCancelled() {
108                         return false;
109                     }
110                 });
111 
112         // Pull result file across
113         File result = mDevice.pullFile(DEVICE_LOCATION + "UiJankinessTestsOutput.txt");
114         assertNotNull("Couldn't get result file", result);
115         // Parse result file
116         Scanner in = new Scanner(result);
117         HashMap<String, Double> results = new HashMap<String, Double>(4);
118         while (in.hasNextLine()) {
119             String[] parts = in.nextLine().split(":");
120             if (parts.length == 2) {
121                 results.put(parts[0], Double.parseDouble(parts[1]));
122             }
123         }
124         in.close();
125         Log.logAndDisplay(LogLevel.INFO, TAG, "Results: " + results);
126         assertEquals("Could not parse the results file: ", 4, results.size());
127 
128         double avgNumJanks = results.get("average number of jankiness");
129         double maxNumJanks = results.get("max number of jankiness");
130         double avgFrameRate = results.get("average frame rate");
131         double avgMaxAccFrames = results.get("average of max accumulated frames");
132 
133         // Create and deliver the report.
134         HostReportLog report = new HostReportLog(mDevice.getSerialNumber(), mAbi.getName(),
135                 mHostTestClass + "#" + testName);
136         report.printValue(
137                 "Average Frame Rate", avgFrameRate, ResultType.HIGHER_BETTER, ResultUnit.COUNT);
138         report.printValue("Average of Maximum Accumulated Frames", avgMaxAccFrames,
139                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
140         report.printValue(
141                 "Maximum Number of Janks", maxNumJanks, ResultType.LOWER_BETTER, ResultUnit.COUNT);
142         report.printSummary(
143                 "Average Number of Janks", avgNumJanks, ResultType.LOWER_BETTER, ResultUnit.SCORE);
144         report.deliverReportToHost();
145     }
146 
147 }
148