1 /*
2  * Copyright (C) 2015 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 android.leanbackjank.cts;
18 
19 import android.os.Bundle;
20 import android.support.test.uiautomator.UiDevice;
21 
22 import androidx.test.jank.GfxMonitor;
23 import androidx.test.jank.JankTestBase;
24 import androidx.test.jank.WindowContentFrameStatsMonitor;
25 
26 import com.android.compatibility.common.util.DeviceReportLog;
27 import com.android.compatibility.common.util.ResultType;
28 import com.android.compatibility.common.util.ResultUnit;
29 
30 public abstract class CtsJankTestBase extends JankTestBase {
31 
32     private final String REPORT_LOG_NAME = "CtsLeanbackJankTestCases";
33 
34     private UiDevice mDevice;
35     private DeviceReportLog mLog;
36 
printIntValueWithKey(String source, Bundle metrics, String key, ResultType resultType, ResultUnit resultUnit)37     private void printIntValueWithKey(String source, Bundle metrics, String key,
38             ResultType resultType, ResultUnit resultUnit) {
39         if (!metrics.containsKey(key)) {
40             return;
41         }
42         mLog.addValue(source, formatKeyForTestMetrics(key), metrics.getInt(key), resultType, resultUnit);
43     }
44 
printDoubleValueWithKey(String source, Bundle metrics, String key, ResultType resultType, ResultUnit resultUnit)45     private void printDoubleValueWithKey(String source, Bundle metrics, String key,
46             ResultType resultType, ResultUnit resultUnit) {
47         if (!metrics.containsKey(key)) {
48             return;
49         }
50         mLog.addValue(source, formatKeyForTestMetrics(key), metrics.getDouble(key), resultType,
51                 resultUnit);
52     }
53 
formatKeyForTestMetrics(String key)54     private String formatKeyForTestMetrics(String key) {
55         return key.toLowerCase().replace('-', '_');
56     }
57 
58     @Override
afterTest(Bundle metrics)59     public void afterTest(Bundle metrics) {
60         String source = String.format("%s#%s", getClass().getCanonicalName(), getName());
61         printDoubleValueWithKey(source, metrics, WindowContentFrameStatsMonitor.KEY_AVG_FPS,
62                 ResultType.HIGHER_BETTER, ResultUnit.FPS);
63         printDoubleValueWithKey(source, metrics,
64                 WindowContentFrameStatsMonitor.KEY_AVG_LONGEST_FRAME,
65                 ResultType.LOWER_BETTER, ResultUnit.MS);
66         printIntValueWithKey(source, metrics, WindowContentFrameStatsMonitor.KEY_MAX_NUM_JANKY,
67                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
68         mLog.setSummary(formatKeyForTestMetrics(WindowContentFrameStatsMonitor.KEY_AVG_NUM_JANKY),
69                 metrics.getDouble(WindowContentFrameStatsMonitor.KEY_AVG_NUM_JANKY),
70                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
71 
72         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_NUM_JANKY,
73                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
74         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_FRAME_TIME_90TH_PERCENTILE,
75                 ResultType.LOWER_BETTER, ResultUnit.MS);
76         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_FRAME_TIME_95TH_PERCENTILE,
77                 ResultType.LOWER_BETTER, ResultUnit.MS);
78         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_FRAME_TIME_99TH_PERCENTILE,
79                 ResultType.LOWER_BETTER, ResultUnit.MS);
80         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_MISSED_VSYNC,
81                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
82         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_SLOW_UI_THREAD,
83                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
84         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_SLOW_BITMAP_UPLOADS,
85                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
86         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_SLOW_DRAW,
87                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
88         printDoubleValueWithKey(source, metrics, GfxMonitor.KEY_AVG_HIGH_INPUT_LATENCY,
89                 ResultType.LOWER_BETTER, ResultUnit.COUNT);
90     }
91 
92     @Override
setUp()93     protected void setUp() throws Exception {
94         super.setUp();
95         String streamName = "cts_leanback_jank";
96         mLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
97         // fix device orientation
98         mDevice = UiDevice.getInstance(getInstrumentation());
99         mDevice.setOrientationNatural();
100     }
101 
102     @Override
tearDown()103     protected void tearDown() throws Exception {
104         mLog.submit(getInstrumentation());
105         // restore device orientation
106         mDevice.unfreezeRotation();
107         super.tearDown();
108     }
109 
getUiDevice()110     protected UiDevice getUiDevice() {
111         return mDevice;
112     }
113 }
114