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.content.Intent; 20 import android.content.pm.PackageManager; 21 import android.os.Bundle; 22 import android.os.SystemClock; 23 import android.support.test.jank.GfxMonitor; 24 import android.support.test.jank.JankTest; 25 import android.support.test.jank.JankTestBase; 26 import android.support.test.uiautomator.By; 27 import android.support.test.uiautomator.UiDevice; 28 import android.support.test.uiautomator.UiObject2; 29 import android.support.test.uiautomator.UiObjectNotFoundException; 30 import android.support.test.uiautomator.Until; 31 32 import java.io.IOException; 33 34 /* 35 * This class contains the tests for key system apps on Android TV jank. 36 */ 37 public class SystemAppJankTests extends JankTestBase { 38 39 private static final int LONG_TIMEOUT = 5000; 40 private static final int INNER_LOOP = 8; 41 private static final int FLING_SPEED = 12000; 42 private static final String YOUTUBE_PACKAGE = "com.google.android.youtube.tv"; 43 private UiDevice mDevice; 44 45 @Override 46 public void setUp() { 47 mDevice = UiDevice.getInstance(getInstrumentation()); 48 } 49 50 @Override 51 protected void tearDown() throws Exception { 52 super.tearDown(); 53 } 54 55 public void afterTestSystemApp(Bundle metrics) throws IOException { 56 mDevice.pressHome(); 57 super.afterTest(metrics); 58 } 59 60 public void launchYoutube() throws UiObjectNotFoundException { 61 mDevice.pressHome(); 62 launchApp(YOUTUBE_PACKAGE); 63 SystemClock.sleep(LONG_TIMEOUT); 64 // Ensure that Youtube has loaded on Android TV with nav bar in focus 65 UiObject2 youtubeScreen = mDevice.wait( 66 Until.findObject(By.scrollable(true).res(YOUTUBE_PACKAGE, "guide")), LONG_TIMEOUT); 67 } 68 69 // Measures jank while scrolling down the Youtube Navigation Bar 70 @JankTest(expectedFrames=100, beforeTest = "launchYoutube", 71 afterTest="afterTestSystemApp") 72 @GfxMonitor(processName=YOUTUBE_PACKAGE) 73 public void testYoutubeGuideNavigation() throws UiObjectNotFoundException { 74 // As of launching Youtube, we're already at the screen where 75 // the navigation bar is in focus, so we only need to scroll. 76 navigateDownAndUpCurrentScreen(); 77 } 78 79 public void goToYoutubeContainer() throws UiObjectNotFoundException { 80 launchYoutube(); 81 // Move focus from Youtube navigation bar to content 82 mDevice.pressDPadRight(); 83 SystemClock.sleep(LONG_TIMEOUT); 84 // Ensure that Youtube content is in focus 85 UiObject2 youtubeScreen = mDevice.wait( Until.findObject(By.scrollable(true) 86 .res(YOUTUBE_PACKAGE, "container_list")), LONG_TIMEOUT); 87 } 88 89 // Measures jank while scrolling down the Youtube Navigation Bar 90 @JankTest(expectedFrames=100, beforeTest = "goToYoutubeContainer", 91 afterTest="afterTestSystemApp") 92 @GfxMonitor(processName=YOUTUBE_PACKAGE) 93 public void testYoutubeContainerListNavigation() throws UiObjectNotFoundException { 94 // The gotoYouTubeContainer method confirms that the focus is 95 // on the content, so we only need to scroll. 96 navigateDownAndUpCurrentScreen(); 97 } 98 99 public void navigateDownAndUpCurrentScreen() { 100 for (int i = 0; i < INNER_LOOP; i++) { 101 // Press DPad button down eight times in succession to scroll down. 102 mDevice.pressDPadDown(); 103 } 104 for (int i = 0; i < INNER_LOOP; i++) { 105 // Press DPad button up eight times in succession to scroll up. 106 mDevice.pressDPadUp(); 107 } 108 } 109 110 public void launchApp(String packageName) throws UiObjectNotFoundException { 111 PackageManager pm = getInstrumentation().getContext().getPackageManager(); 112 Intent appIntent = pm.getLaunchIntentForPackage(packageName); 113 appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 114 getInstrumentation().getContext().startActivity(appIntent); 115 SystemClock.sleep(LONG_TIMEOUT); 116 } 117 }