1 /*
2  * Copyright (C) 2015 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 
15 package android.leanbackjank.cts;
16 
17 import android.content.ComponentName;
18 import android.content.Intent;
19 import android.content.pm.PackageManager;
20 import android.leanbackjank.app.IntentKeys;
21 import android.os.SystemClock;
22 import android.support.test.uiautomator.By;
23 import android.support.test.uiautomator.Until;
24 
25 import androidx.test.jank.GfxMonitor;
26 import androidx.test.jank.JankTest;
27 import androidx.test.jank.WindowContentFrameStatsMonitor;
28 
29 public class CtsDeviceLeanback extends CtsJankTestBase {
30     private static final String TAG = "CtsDeviceLeanback";
31     private static final int MILLIS_PER_SECOND = 1000;
32     private static final long WAIT_TIMEOUT = 5 * MILLIS_PER_SECOND;
33     private static final int SCROLL_COUNT = 100;
34     private static final int SCROLL_INTERVAL_MILLIS = 200;
35     private static final int PRE_SCROLL_DELAY_MILLIS = 500;
36     private static final int PRE_SCROLL_IDLE_TIME = 2 * MILLIS_PER_SECOND;
37     private static final int SAMPLING_DURATION_SECONDS = 2;
38     private static final int SAMPLING_DURATION_MILLIS =
39             SAMPLING_DURATION_SECONDS * MILLIS_PER_SECOND;
40     private final static String APP_PACKAGE = "android.leanbackjank.app";
41     private final static String JAVA_PACKAGE = "android.leanbackjank.app.ui";
42     private final static String CLASS = JAVA_PACKAGE + ".MainActivity";
43 
shouldSkip()44     private boolean shouldSkip() {
45 	PackageManager packageManager =
46                 getInstrumentation().getTargetContext().getPackageManager();
47         if (!packageManager.hasSystemFeature(
48                 PackageManager.FEATURE_LEANBACK)) {
49             return true;
50         }
51         return false;
52     }
53 
54     @Override
runTest()55     protected void runTest() throws Throwable {
56         if (shouldSkip()) {
57             return;
58         }
59         super.runTest();
60     }
61 
62     @Override
setUp()63     protected void setUp() throws Exception {
64         super.setUp();
65         if (shouldSkip()) {
66             return;
67         }
68         Intent intent = new Intent(Intent.ACTION_MAIN);
69         intent.setComponent(new ComponentName(APP_PACKAGE, CLASS));
70 
71         // Trigger automated scroll of the helper app.
72         intent.putExtra(IntentKeys.SCROLL_DELAY, PRE_SCROLL_DELAY_MILLIS);
73         intent.putExtra(IntentKeys.SCROLL_COUNT, SCROLL_COUNT);
74         intent.putExtra(IntentKeys.SCROLL_INTERVAL, SCROLL_INTERVAL_MILLIS);
75         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
76         getInstrumentation().getTargetContext().startActivity(intent);
77         if (!getUiDevice().wait(Until.hasObject(By.pkg(APP_PACKAGE)), WAIT_TIMEOUT)) {
78             fail("Test helper app package not found on device");
79         }
80 
81         // Wait until scroll animation starts.
82         SystemClock.sleep(PRE_SCROLL_IDLE_TIME);
83     }
84 
85     @Override
tearDown()86     protected void tearDown() throws Exception {
87         getUiDevice().pressHome();
88         super.tearDown();
89     }
90 
91     // Requires at least 50 fps on average to pass the test.
92     @JankTest(expectedFrames = 50 * SAMPLING_DURATION_SECONDS, defaultIterationCount = 2)
93     @GfxMonitor(processName = APP_PACKAGE)
94     @WindowContentFrameStatsMonitor
testScrollingByTimer()95     public void testScrollingByTimer() {
96         SystemClock.sleep(SAMPLING_DURATION_MILLIS);
97     }
98 }
99