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 com.android.androidtv.janktests; 18 19 import android.os.Bundle; 20 import android.os.SystemClock; 21 import android.support.test.jank.GfxMonitor; 22 import android.support.test.jank.JankTest; 23 import android.support.test.jank.JankTestBase; 24 import android.support.test.uiautomator.By; 25 import android.support.test.uiautomator.Direction; 26 import android.support.test.uiautomator.UiDevice; 27 import android.support.test.uiautomator.UiObject2; 28 import android.support.test.uiautomator.UiObjectNotFoundException; 29 import android.support.test.uiautomator.Until; 30 import android.util.Log; 31 import java.io.IOException; 32 33 /* 34 * This class contains the tests for Android TV jank. 35 */ 36 public class SystemUiJankTests extends JankTestBase { 37 38 private static final int SHORT_TIMEOUT = 1000; 39 private static final int LONG_TIMEOUT = 3000; 40 private static final int INNER_LOOP = 4; 41 private static final int FLING_SPEED = 12000; 42 private static final String LEANBACK_LAUNCHER = "com.google.android.leanbacklauncher"; 43 private static final String SETTINGS_PACKAGE = "com.android.tv.settings"; 44 private UiDevice mDevice; 45 46 @Override 47 public void setUp() { 48 mDevice = UiDevice.getInstance(getInstrumentation()); 49 } 50 51 @Override 52 protected void tearDown() throws Exception { 53 super.tearDown(); 54 } 55 56 public void goHome() { 57 mDevice.pressHome(); 58 // Ensure that Home screen is being displayed 59 UiObject2 homeScreen = mDevice.wait( 60 Until.findObject(By.scrollable(true).res(LEANBACK_LAUNCHER, "main_list_view")), 61 SHORT_TIMEOUT); 62 } 63 64 public void afterTestHomeScreenNavigation(Bundle metrics) throws IOException { 65 super.afterTest(metrics); 66 } 67 68 // Measures jank while scrolling down the Home screen 69 @JankTest(expectedFrames=100, beforeTest = "goHome", 70 afterTest="afterTestHomeScreenNavigation") 71 @GfxMonitor(processName=LEANBACK_LAUNCHER) 72 public void testHomeScreenNavigation() throws UiObjectNotFoundException { 73 // We've already verified that Home screen is being displayed. 74 // Scroll up and down the home screen. 75 navigateDownAndUpCurrentScreen(); 76 } 77 78 // Navigates to the Settings row on the Home screen 79 public void goToSettingsRow() { 80 // Navigate to Home screen and verify that it is being displayed. 81 goHome(); 82 mDevice.wait(Until.findObject(By.scrollable(true).res(LEANBACK_LAUNCHER, "main_list_view")), 83 SHORT_TIMEOUT); 84 // Look for the row with 'Settings' text. 85 // This will ensure that the DPad focus is on the Settings icon. 86 int count = 0; 87 while (count <= 5 && !(mDevice.hasObject(By.res(LEANBACK_LAUNCHER, "label") 88 .text("Settings")))) { 89 mDevice.pressDPadDown(); 90 count++; 91 } 92 if (!mDevice.hasObject(By.res(LEANBACK_LAUNCHER, "label").text("Settings"))) { 93 Log.d(LEANBACK_LAUNCHER, "Couldn't navigate to settings"); 94 } 95 } 96 97 public void afterTestSettings(Bundle metrics) throws IOException { 98 // Navigate back home 99 goHome(); 100 super.afterTest(metrics); 101 } 102 103 // Measures jank while navigating to Settings from Home and back 104 @JankTest(expectedFrames=100, beforeTest="goToSettingsRow", 105 afterTest="afterTestSettings") 106 @GfxMonitor(processName=SETTINGS_PACKAGE) 107 public void testNavigateToSettings() throws UiObjectNotFoundException { 108 for (int i = 0; i < INNER_LOOP * 10; i++) { 109 // Press DPad center button to navigate to settings. 110 mDevice.pressDPadCenter(); 111 // Press Back button to go back to the Home screen with focus on Settings 112 mDevice.pressBack(); 113 } 114 } 115 116 // Navigates to the Settings Screen 117 public void goToSettings() { 118 goToSettingsRow(); 119 mDevice.pressDPadCenter(); 120 } 121 122 // Measures jank while scrolling on the Settings screen 123 @JankTest(expectedFrames=100, beforeTest="goToSettings", 124 afterTest="afterTestSettings") 125 @GfxMonitor(processName=SETTINGS_PACKAGE) 126 public void testSettingsScreenNavigation() throws UiObjectNotFoundException { 127 // Ensure that Settings screen is being displayed 128 mDevice.wait(Until.findObject(By.scrollable(true).res(SETTINGS_PACKAGE, "container_list")), 129 SHORT_TIMEOUT); 130 navigateDownAndUpCurrentScreen(); 131 } 132 133 public void navigateDownAndUpCurrentScreen() { 134 for (int i = 0; i < INNER_LOOP; i++) { 135 // Press DPad button down eight times in succession 136 mDevice.pressDPadDown(); 137 } 138 for (int i = 0; i < INNER_LOOP; i++) { 139 // Press DPad button up eight times in succession. 140 mDevice.pressDPadUp(); 141 } 142 } 143 } 144