Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 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.sysapp.janktests; |
| 18 | |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | |
Md Haque | e3080cb | 2015-06-23 14:41:15 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
| 25 | import android.os.Environment; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.os.SystemClock; |
| 28 | import android.support.test.jank.GfxMonitor; |
| 29 | import android.support.test.jank.JankTest; |
| 30 | import android.support.test.jank.JankTestBase; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 31 | import android.support.test.uiautomator.By; |
| 32 | import android.support.test.uiautomator.BySelector; |
| 33 | import android.support.test.uiautomator.Direction; |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 34 | import android.support.test.uiautomator.StaleObjectException; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 35 | import android.support.test.uiautomator.UiDevice; |
| 36 | import android.support.test.uiautomator.UiObject2; |
| 37 | import android.support.test.uiautomator.UiObjectNotFoundException; |
| 38 | import android.support.test.uiautomator.Until; |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 39 | import android.widget.ImageButton; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 40 | import junit.framework.Assert; |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 41 | import android.support.test.timeresulthelper.TimeResultLogger; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Jank test for scrolling gmail inbox mails |
| 45 | */ |
| 46 | |
| 47 | public class GMailJankTests extends JankTestBase { |
| 48 | private static final int SHORT_TIMEOUT = 1000; |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 49 | private static final int LONG_TIMEOUT = 5000; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 50 | private static final int INNER_LOOP = 5; |
| 51 | private static final int EXPECTED_FRAMES = 100; |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 52 | private static final int TAB_MIN_WIDTH = 600; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 53 | private static final String PACKAGE_NAME = "com.google.android.gm"; |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 54 | private static final String RES_PACKAGE_NAME = "android"; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 55 | private UiDevice mDevice; |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 56 | 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 Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 60 | |
| 61 | @Override |
| 62 | public void setUp() throws Exception { |
| 63 | super.setUp(); |
| 64 | mDevice = UiDevice.getInstance(getInstrumentation()); |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 65 | mDevice.setOrientationNatural(); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void tearDown() throws Exception { |
| 70 | mDevice.unfreezeRotation(); |
| 71 | super.tearDown(); |
| 72 | } |
| 73 | |
Md Haque | e3080cb | 2015-06-23 14:41:15 -0700 | [diff] [blame] | 74 | 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 Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 82 | public void launchGMail () throws UiObjectNotFoundException { |
Md Haque | e3080cb | 2015-06-23 14:41:15 -0700 | [diff] [blame] | 83 | launchApp(PACKAGE_NAME); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 84 | dismissClings(); |
| 85 | // Need any check for account-name?? |
| 86 | waitForEmailSync(); |
| 87 | } |
| 88 | |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 89 | public void prepGMailInboxFling() throws UiObjectNotFoundException, IOException { |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 90 | 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); |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 95 | 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 Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 107 | // Measures jank while scrolling gmail inbox |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 108 | @JankTest(beforeTest="prepGMailInboxFling", expectedFrames=EXPECTED_FRAMES, |
| 109 | afterTest="afterTestGMailInboxFling") |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 110 | @GfxMonitor(processName=PACKAGE_NAME) |
| 111 | public void testGMailInboxFling() { |
| 112 | UiObject2 list = mDevice.wait( |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 113 | Until.findObject(By.res(PACKAGE_NAME, "conversation_list_view")), LONG_TIMEOUT); |
| 114 | Assert.assertNotNull("Failed to locate 'conversation_list_view'", list); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 115 | for (int i = 0; i < INNER_LOOP; i++) { |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 116 | 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 | |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 123 | public void prepOpenNavDrawer() throws UiObjectNotFoundException, IOException { |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 124 | launchGMail(); |
| 125 | // Ensure test is ready to be executed |
| 126 | Assert.assertNotNull("Failed to locate Nav Drawer Openner", openNavigationDrawer()); |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 127 | 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 Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Measures jank while opening Navigation Drawer |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 140 | @JankTest(beforeTest="prepOpenNavDrawer", expectedFrames=EXPECTED_FRAMES, |
| 141 | afterTest="afterTestOpenNavDrawer") |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 142 | @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 | |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 153 | public void prepFlingNavDrawer() throws UiObjectNotFoundException, IOException{ |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 154 | 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); |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 161 | 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 Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Measures jank while flinging Navigation Drawer |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 174 | @JankTest(beforeTest="prepFlingNavDrawer", expectedFrames=EXPECTED_FRAMES, |
| 175 | afterTest="afterTestFlingNavDrawer") |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 176 | @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 Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | private void dismissClings() { |
| 188 | UiObject2 welcomeScreenGotIt = mDevice.wait( |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 189 | Until.findObject(By.res(PACKAGE_NAME, "welcome_tour_got_it")), SHORT_TIMEOUT); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 190 | if (welcomeScreenGotIt != null) { |
| 191 | welcomeScreenGotIt.clickAndWait(Until.newWindow(), SHORT_TIMEOUT); |
| 192 | } |
| 193 | UiObject2 welcomeScreenSkip = mDevice.wait( |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 194 | Until.findObject(By.res(PACKAGE_NAME, "welcome_tour_skip")), SHORT_TIMEOUT); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 195 | 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 Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 215 | mDevice.wait(Until.gone(By.text("Waiting for sync")), LONG_TIMEOUT * 6)); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 216 | Assert.assertTrue("'Loading' timed out", |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 217 | mDevice.wait(Until.gone(By.text("Loading")), LONG_TIMEOUT * 6)); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 218 | } |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 219 | |
| 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 Haque | 4ad1dd9 | 2015-07-15 18:49:34 -0700 | [diff] [blame] | 236 | Until.findObject(By.res(PACKAGE_NAME, "content_pane")), SHORT_TIMEOUT); |
Md Haque | 54fa3ea | 2015-06-28 19:31:07 -0700 | [diff] [blame] | 237 | } else { |
| 238 | container = mDevice.wait( |
| 239 | Until.findObject(By.res(RES_PACKAGE_NAME, "list")), SHORT_TIMEOUT); |
| 240 | } |
| 241 | return container; |
| 242 | } |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 243 | } |