blob: 4a5e96cbbc504d2d9f2eeeb36f3c60e738ae1ca2 [file] [log] [blame]
Md Haqueaec7abc2015-06-10 18:31:28 -07001/*
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
17package com.android.sysapp.janktests;
18
Gopinathfe6f8132015-09-13 17:23:36 -070019import java.io.File;
20import java.io.IOException;
21
Md Haquee3080cb2015-06-23 14:41:15 -070022import android.content.Intent;
23import android.content.pm.PackageManager;
Gopinathfe6f8132015-09-13 17:23:36 -070024import android.os.Bundle;
25import android.os.Environment;
Md Haqueaec7abc2015-06-10 18:31:28 -070026import android.os.RemoteException;
27import android.os.SystemClock;
28import android.support.test.jank.GfxMonitor;
29import android.support.test.jank.JankTest;
30import android.support.test.jank.JankTestBase;
Md Haqueaec7abc2015-06-10 18:31:28 -070031import android.support.test.uiautomator.By;
32import android.support.test.uiautomator.BySelector;
33import android.support.test.uiautomator.Direction;
Md Haque54fa3ea2015-06-28 19:31:07 -070034import android.support.test.uiautomator.StaleObjectException;
Md Haqueaec7abc2015-06-10 18:31:28 -070035import android.support.test.uiautomator.UiDevice;
36import android.support.test.uiautomator.UiObject2;
37import android.support.test.uiautomator.UiObjectNotFoundException;
38import android.support.test.uiautomator.Until;
Md Haque54fa3ea2015-06-28 19:31:07 -070039import android.widget.ImageButton;
Md Haqueaec7abc2015-06-10 18:31:28 -070040import junit.framework.Assert;
Gopinathfe6f8132015-09-13 17:23:36 -070041import android.support.test.timeresulthelper.TimeResultLogger;
Md Haqueaec7abc2015-06-10 18:31:28 -070042
43/**
44 * Jank test for scrolling gmail inbox mails
45 */
46
47public class GMailJankTests extends JankTestBase {
48 private static final int SHORT_TIMEOUT = 1000;
Md Haque54fa3ea2015-06-28 19:31:07 -070049 private static final int LONG_TIMEOUT = 5000;
Md Haqueaec7abc2015-06-10 18:31:28 -070050 private static final int INNER_LOOP = 5;
51 private static final int EXPECTED_FRAMES = 100;
Md Haque54fa3ea2015-06-28 19:31:07 -070052 private static final int TAB_MIN_WIDTH = 600;
Md Haqueaec7abc2015-06-10 18:31:28 -070053 private static final String PACKAGE_NAME = "com.google.android.gm";
Md Haque54fa3ea2015-06-28 19:31:07 -070054 private static final String RES_PACKAGE_NAME = "android";
Md Haqueaec7abc2015-06-10 18:31:28 -070055 private UiDevice mDevice;
Gopinathfe6f8132015-09-13 17:23:36 -070056 private static final File TIMESTAMP_FILE = new File(Environment.getExternalStorageDirectory()
57 .getAbsolutePath(), "autotester.log");
58 private static final File RESULTS_FILE = new File(Environment.getExternalStorageDirectory()
59 .getAbsolutePath(), "results.log");
Md Haqueaec7abc2015-06-10 18:31:28 -070060
61 @Override
62 public void setUp() throws Exception {
63 super.setUp();
64 mDevice = UiDevice.getInstance(getInstrumentation());
Md Haque54fa3ea2015-06-28 19:31:07 -070065 mDevice.setOrientationNatural();
Md Haqueaec7abc2015-06-10 18:31:28 -070066 }
67
68 @Override
69 protected void tearDown() throws Exception {
70 mDevice.unfreezeRotation();
71 super.tearDown();
72 }
73
Md Haquee3080cb2015-06-23 14:41:15 -070074 public void launchApp(String packageName) throws UiObjectNotFoundException{
75 PackageManager pm = getInstrumentation().getContext().getPackageManager();
76 Intent appIntent = pm.getLaunchIntentForPackage(packageName);
77 appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
78 getInstrumentation().getContext().startActivity(appIntent);
79 SystemClock.sleep(SHORT_TIMEOUT);
80 }
81
Md Haqueaec7abc2015-06-10 18:31:28 -070082 public void launchGMail () throws UiObjectNotFoundException {
Md Haquee3080cb2015-06-23 14:41:15 -070083 launchApp(PACKAGE_NAME);
Md Haqueaec7abc2015-06-10 18:31:28 -070084 dismissClings();
85 // Need any check for account-name??
86 waitForEmailSync();
87 }
88
Gopinathfe6f8132015-09-13 17:23:36 -070089 public void prepGMailInboxFling() throws UiObjectNotFoundException, IOException {
Md Haque54fa3ea2015-06-28 19:31:07 -070090 launchGMail();
91 // Ensure test is ready to be executed
92 UiObject2 list = mDevice.wait(
93 Until.findObject(By.res(PACKAGE_NAME, "conversation_list_view")), SHORT_TIMEOUT);
94 Assert.assertNotNull("Failed to locate 'conversation_list_view'", list);
Gopinathfe6f8132015-09-13 17:23:36 -070095 TimeResultLogger.writeTimeStampLogStart(String.format("%s-%s",
96 getClass().getSimpleName(), getName()), TIMESTAMP_FILE);
97 }
98
99 public void afterTestGMailInboxFling(Bundle metrics) throws IOException {
100 TimeResultLogger.writeTimeStampLogEnd(String.format("%s-%s",
101 getClass().getSimpleName(), getName()), TIMESTAMP_FILE);
102 TimeResultLogger.writeResultToFile(String.format("%s-%s",
103 getClass().getSimpleName(), getName()), RESULTS_FILE, metrics);
104 super.afterTest(metrics);
Md Haque54fa3ea2015-06-28 19:31:07 -0700105 }
106
Md Haqueaec7abc2015-06-10 18:31:28 -0700107 // Measures jank while scrolling gmail inbox
Gopinathfe6f8132015-09-13 17:23:36 -0700108 @JankTest(beforeTest="prepGMailInboxFling", expectedFrames=EXPECTED_FRAMES,
109 afterTest="afterTestGMailInboxFling")
Md Haqueaec7abc2015-06-10 18:31:28 -0700110 @GfxMonitor(processName=PACKAGE_NAME)
111 public void testGMailInboxFling() {
112 UiObject2 list = mDevice.wait(
Md Haque54fa3ea2015-06-28 19:31:07 -0700113 Until.findObject(By.res(PACKAGE_NAME, "conversation_list_view")), LONG_TIMEOUT);
114 Assert.assertNotNull("Failed to locate 'conversation_list_view'", list);
Md Haqueaec7abc2015-06-10 18:31:28 -0700115 for (int i = 0; i < INNER_LOOP; i++) {
Md Haque54fa3ea2015-06-28 19:31:07 -0700116 list.scroll(Direction.DOWN, 1.0f);
117 SystemClock.sleep(SHORT_TIMEOUT);
118 list.scroll(Direction.UP, 1.0f);
119 SystemClock.sleep(SHORT_TIMEOUT);
120 }
121 }
122
Gopinathfe6f8132015-09-13 17:23:36 -0700123 public void prepOpenNavDrawer() throws UiObjectNotFoundException, IOException {
Md Haque54fa3ea2015-06-28 19:31:07 -0700124 launchGMail();
125 // Ensure test is ready to be executed
126 Assert.assertNotNull("Failed to locate Nav Drawer Openner", openNavigationDrawer());
Gopinathfe6f8132015-09-13 17:23:36 -0700127 TimeResultLogger.writeTimeStampLogStart(String.format("%s-%s",
128 getClass().getSimpleName(), getName()), TIMESTAMP_FILE);
129 }
130
131 public void afterTestOpenNavDrawer(Bundle metrics) throws IOException {
132 TimeResultLogger.writeTimeStampLogEnd(String.format("%s-%s",
133 getClass().getSimpleName(), getName()), TIMESTAMP_FILE);
134 TimeResultLogger.writeResultToFile(String.format("%s-%s",
135 getClass().getSimpleName(), getName()), RESULTS_FILE, metrics);
136 super.afterTest(metrics);
Md Haque54fa3ea2015-06-28 19:31:07 -0700137 }
138
139 // Measures jank while opening Navigation Drawer
Gopinathfe6f8132015-09-13 17:23:36 -0700140 @JankTest(beforeTest="prepOpenNavDrawer", expectedFrames=EXPECTED_FRAMES,
141 afterTest="afterTestOpenNavDrawer")
Md Haque54fa3ea2015-06-28 19:31:07 -0700142 @GfxMonitor(processName=PACKAGE_NAME)
143 public void testOpenNavDrawer() {
144 UiObject2 navDrawer = openNavigationDrawer();
145 for (int i = 0; i < INNER_LOOP; i++) {
146 navDrawer.click();
147 SystemClock.sleep(SHORT_TIMEOUT);
148 mDevice.pressBack();
149 SystemClock.sleep(SHORT_TIMEOUT);
150 }
151 }
152
Gopinathfe6f8132015-09-13 17:23:36 -0700153 public void prepFlingNavDrawer() throws UiObjectNotFoundException, IOException{
Md Haque54fa3ea2015-06-28 19:31:07 -0700154 launchGMail();
155 UiObject2 navDrawer = openNavigationDrawer();
156 Assert.assertNotNull("Failed to locate Nav Drawer Openner", navDrawer);
157 navDrawer.click();
158 // Ensure test is ready to be executed
159 UiObject2 container = getNavigationDrawerContainer();
160 Assert.assertNotNull("Failed to locate Nav drawer container", container);
Gopinathfe6f8132015-09-13 17:23:36 -0700161 TimeResultLogger.writeTimeStampLogStart(String.format("%s-%s",
162 getClass().getSimpleName(), getName()), TIMESTAMP_FILE);
163 }
164
165 public void afterTestFlingNavDrawer(Bundle metrics) throws IOException {
166 TimeResultLogger.writeTimeStampLogEnd(String.format("%s-%s",
167 getClass().getSimpleName(), getName()), TIMESTAMP_FILE);
168 TimeResultLogger.writeResultToFile(String.format("%s-%s",
169 getClass().getSimpleName(), getName()), RESULTS_FILE, metrics);
170 super.afterTest(metrics);
Md Haque54fa3ea2015-06-28 19:31:07 -0700171 }
172
173 // Measures jank while flinging Navigation Drawer
Gopinathfe6f8132015-09-13 17:23:36 -0700174 @JankTest(beforeTest="prepFlingNavDrawer", expectedFrames=EXPECTED_FRAMES,
175 afterTest="afterTestFlingNavDrawer")
Md Haque54fa3ea2015-06-28 19:31:07 -0700176 @GfxMonitor(processName=PACKAGE_NAME)
177 public void testFlingNavDrawer() {
178 UiObject2 container = getNavigationDrawerContainer();
179 for (int i = 0; i < INNER_LOOP; i++) {
180 container.fling(Direction.DOWN);
181 SystemClock.sleep(SHORT_TIMEOUT);
182 container.fling(Direction.UP);
183 SystemClock.sleep(SHORT_TIMEOUT);
Md Haqueaec7abc2015-06-10 18:31:28 -0700184 }
185 }
186
187 private void dismissClings() {
188 UiObject2 welcomeScreenGotIt = mDevice.wait(
Md Haque54fa3ea2015-06-28 19:31:07 -0700189 Until.findObject(By.res(PACKAGE_NAME, "welcome_tour_got_it")), SHORT_TIMEOUT);
Md Haqueaec7abc2015-06-10 18:31:28 -0700190 if (welcomeScreenGotIt != null) {
191 welcomeScreenGotIt.clickAndWait(Until.newWindow(), SHORT_TIMEOUT);
192 }
193 UiObject2 welcomeScreenSkip = mDevice.wait(
Md Haque54fa3ea2015-06-28 19:31:07 -0700194 Until.findObject(By.res(PACKAGE_NAME, "welcome_tour_skip")), SHORT_TIMEOUT);
Md Haqueaec7abc2015-06-10 18:31:28 -0700195 if (welcomeScreenSkip != null) {
196 welcomeScreenSkip.clickAndWait(Until.newWindow(), SHORT_TIMEOUT);
197 }
198 UiObject2 tutorialDone = mDevice.wait(
199 Until.findObject(By.res(PACKAGE_NAME, "action_done")), 2 * SHORT_TIMEOUT);
200 if (tutorialDone != null) {
201 tutorialDone.clickAndWait(Until.newWindow(), SHORT_TIMEOUT);
202 }
203 mDevice.wait(Until.findObject(By.text("CONFIDENTIAL")), 2 * SHORT_TIMEOUT);
204 UiObject2 splash = mDevice.findObject(By.text("Ok, got it"));
205 if (splash != null) {
206 splash.clickAndWait(Until.newWindow(), SHORT_TIMEOUT);
207 }
208 }
209
210 public void waitForEmailSync() {
211 // Wait up to 2 seconds for a "waiting" message to appear
212 mDevice.wait(Until.hasObject(By.text("Waiting for sync")), 2 * SHORT_TIMEOUT);
213 // Wait until any "waiting" messages are gone
214 Assert.assertTrue("'Waiting for sync' timed out",
Md Haque54fa3ea2015-06-28 19:31:07 -0700215 mDevice.wait(Until.gone(By.text("Waiting for sync")), LONG_TIMEOUT * 6));
Md Haqueaec7abc2015-06-10 18:31:28 -0700216 Assert.assertTrue("'Loading' timed out",
Md Haque54fa3ea2015-06-28 19:31:07 -0700217 mDevice.wait(Until.gone(By.text("Loading")), LONG_TIMEOUT * 6));
Md Haqueaec7abc2015-06-10 18:31:28 -0700218 }
Md Haque54fa3ea2015-06-28 19:31:07 -0700219
220 public UiObject2 openNavigationDrawer() {
221 UiObject2 navDrawer = null;
222 if (mDevice.getDisplaySizeDp().x < TAB_MIN_WIDTH) {
223 navDrawer = mDevice.wait(Until.findObject(
224 By.clazz(ImageButton.class).desc("Navigate up")), SHORT_TIMEOUT);
225 } else {
226 navDrawer = mDevice.wait(Until.findObject(
227 By.clazz(ImageButton.class).desc("Open navigation drawer")), SHORT_TIMEOUT);
228 }
229 return navDrawer;
230 }
231
232 public UiObject2 getNavigationDrawerContainer() {
233 UiObject2 container = null;
234 if (mDevice.getDisplaySizeDp().x < TAB_MIN_WIDTH) {
235 container = mDevice.wait(
Md Haque4ad1dd92015-07-15 18:49:34 -0700236 Until.findObject(By.res(PACKAGE_NAME, "content_pane")), SHORT_TIMEOUT);
Md Haque54fa3ea2015-06-28 19:31:07 -0700237 } else {
238 container = mDevice.wait(
239 Until.findObject(By.res(RES_PACKAGE_NAME, "list")), SHORT_TIMEOUT);
240 }
241 return container;
242 }
Gopinathfe6f8132015-09-13 17:23:36 -0700243}