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