1 /* 2 * Copyright (C) 2016 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.wearable.sysapp.janktests; 18 19 import android.app.Instrumentation; 20 import android.content.ComponentName; 21 import android.content.Intent; 22 import android.os.SystemClock; 23 import android.support.test.uiautomator.By; 24 import android.support.test.uiautomator.UiDevice; 25 import android.support.test.uiautomator.UiObject2; 26 import android.support.test.uiautomator.Until; 27 import android.util.Log; 28 import android.view.KeyEvent; 29 import junit.framework.Assert; 30 31 /** 32 * Helper for all the system apps jank tests 33 */ 34 public class SysAppTestHelper { 35 36 public static final int EXPECTED_FRAMES_CARDS_TEST = 20; 37 public static final int EXPECTED_FRAMES_DISMISS_EXPANDED_CARDS_TEST = 15; 38 public static final int EXPECTED_FRAMES_WATCHFACE_PICKER_TEST = 20; 39 public static final int EXPECTED_FRAMES_SWIPERIGHT_TO_DISMISS_TEST = 20; 40 public static final int EXPECTED_FRAMES_WATCHFACE_PICKER_TEST_ADD_FAVORITE = 5; 41 public static final int EXPECTED_FRAMES = 100; 42 public static final int LONG_TIMEOUT = 5000; 43 public static final int SHORT_TIMEOUT = 500; 44 public static final int FLING_SPEED = 5000; 45 private static final String LOG_TAG = SysAppTestHelper.class.getSimpleName(); 46 private static final String RELOAD_NOTIFICATION_CARD_INTENT = "com.google.android.wearable." 47 + "support.wearnotificationgenerator.SHOW_NOTIFICATION"; 48 private static final String HOME_INDICATOR = "charging_icon"; 49 private static final String NO_NOTIFICATION_ID = "no_notifications"; 50 private static final String STREAM_CARD_ID = "stream_card"; 51 52 private static SysAppTestHelper sysAppTestHelperInstance; 53 private UiDevice mDevice = null; 54 private Instrumentation instrumentation = null; 55 private Intent mIntent = null; 56 57 /** 58 * @param mDevice Instance to represent the current device. 59 * @param instrumentation Instance for instrumentation. 60 */ 61 private SysAppTestHelper(UiDevice mDevice, Instrumentation instrumentation) { 62 super(); 63 this.mDevice = mDevice; 64 this.instrumentation = instrumentation; 65 mIntent = new Intent(); 66 } 67 68 public static SysAppTestHelper getInstance(UiDevice device, Instrumentation instrumentation) { 69 if (sysAppTestHelperInstance == null) { 70 sysAppTestHelperInstance = new SysAppTestHelper(device, instrumentation); 71 } 72 return sysAppTestHelperInstance; 73 } 74 75 // TODO: Cleanup confusion between swipe and fling. 76 public void swipeRight() { 77 mDevice.swipe(50, 78 mDevice.getDisplayHeight() / 2, mDevice.getDisplayWidth() - 25, 79 mDevice.getDisplayHeight() / 2, 30); // slow speed 80 SystemClock.sleep(SHORT_TIMEOUT); 81 } 82 83 public void swipeLeft() { 84 mDevice.swipe(mDevice.getDisplayWidth() - 50, mDevice.getDisplayHeight() / 2, 50, 85 mDevice.getDisplayHeight() / 2, 30); // slow speed 86 SystemClock.sleep(SHORT_TIMEOUT); 87 } 88 89 public void swipeUp() { 90 mDevice.swipe(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight() / 2 + 50, 91 mDevice.getDisplayWidth() / 2, 0, 30); // slow speed 92 SystemClock.sleep(SHORT_TIMEOUT); 93 } 94 95 public void swipeDown() { 96 mDevice.swipe(mDevice.getDisplayWidth() / 2, 0, mDevice.getDisplayWidth() / 2, 97 mDevice.getDisplayHeight() / 2 + 50, 30); // slow speed 98 SystemClock.sleep(SHORT_TIMEOUT); 99 } 100 101 // TODO: Cleanup confusion between swipe and fling. 102 public void flingLeft() { 103 mDevice.swipe(mDevice.getDisplayWidth() - 50, mDevice.getDisplayHeight() / 2, 104 50, mDevice.getDisplayHeight() / 2, 5); // fast speed 105 SystemClock.sleep(SHORT_TIMEOUT); 106 } 107 108 public void flingRight() { 109 mDevice.swipe(50, mDevice.getDisplayHeight() / 2, 110 mDevice.getDisplayWidth() - 50, mDevice.getDisplayHeight() / 2, 5); // fast speed 111 SystemClock.sleep(SHORT_TIMEOUT); 112 } 113 114 public void flingUp() { 115 mDevice.swipe(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight() / 2 + 50, 116 mDevice.getDisplayWidth() / 2, 0, 5); // fast speed 117 SystemClock.sleep(SHORT_TIMEOUT); 118 } 119 120 public void flingDown() { 121 mDevice.swipe(mDevice.getDisplayWidth() / 2, 0, mDevice.getDisplayWidth() / 2, 122 mDevice.getDisplayHeight() / 2 + 50, 5); // fast speed 123 SystemClock.sleep(SHORT_TIMEOUT); 124 } 125 126 public void clickScreenCenter() { 127 mDevice.click(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight() / 2); 128 SystemClock.sleep(SHORT_TIMEOUT); 129 } 130 131 // Helper method to go back to home screen 132 public void goBackHome() { 133 int count = 0; 134 do { 135 UiObject2 homeScreen = waitForSysAppUiObject2(HOME_INDICATOR); 136 if (homeScreen != null) { 137 break; 138 } 139 mDevice.pressBack(); 140 count++; 141 } while (count < 5); 142 143 SystemClock.sleep(LONG_TIMEOUT); 144 } 145 146 // Helper method to verify if there are any Demo cards. 147 148 // TODO: Allow user to pass in how many cards are expected to find cause some tests may require 149 // more than one card. 150 public void hasDemoCards() { 151 // Device should be pre-loaded with demo cards. 152 153 goBackHome(); 154 155 // Swipe up to go to notification tray. 156 swipeUp(); 157 158 if (waitForSysAppUiObject2(NO_NOTIFICATION_ID) != null) { 159 Log.d(LOG_TAG, "No cards, going to reload the cards"); 160 // If there are no Demo cards, reload them. 161 goBackHome(); 162 reloadDemoCards(); 163 } 164 else if (waitForSysAppUiObject2(STREAM_CARD_ID) != null){ 165 goBackHome(); 166 } 167 else { 168 Assert.fail("Swipe up failed to go to notification tray."); 169 } 170 } 171 172 // This will ensure to reload notification cards by launching NotificationsGeneratorWear app 173 // when there are insufficient cards. 174 private void reloadDemoCards() { 175 mIntent.setAction(RELOAD_NOTIFICATION_CARD_INTENT); 176 instrumentation.getContext().sendBroadcast(mIntent); 177 SystemClock.sleep(LONG_TIMEOUT); 178 } 179 180 public void launchActivity(String appPackage, String activityToLaunch) { 181 mIntent.setAction("android.intent.action.MAIN"); 182 mIntent.setComponent(new ComponentName(appPackage, activityToLaunch)); 183 mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 184 instrumentation.getContext().startActivity(mIntent); 185 } 186 187 // Helper method to goto app launcher and verifies you are there. 188 public void gotoAppLauncher() { 189 goBackHome(); 190 mDevice.pressKeyCode(KeyEvent.KEYCODE_BACK); 191 UiObject2 appLauncher = mDevice.wait(Until.findObject(By.text("Agenda")), 192 SysAppTestHelper.LONG_TIMEOUT); 193 Assert.assertNotNull("App launcher not launched", appLauncher); 194 } 195 196 public UiObject2 waitForSysAppUiObject2(String resourceId) { 197 String launcherPackageName = mDevice.getLauncherPackageName(); 198 return mDevice.wait( 199 Until.findObject(By.res(launcherPackageName, resourceId)), 200 SHORT_TIMEOUT); 201 } 202 } 203