Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.server.wm.flicker; |
| 18 | |
| 19 | import static android.os.SystemClock.sleep; |
| 20 | import static android.view.Surface.rotationToString; |
| 21 | |
| 22 | import static com.android.server.wm.flicker.AutomationUtils.clearRecents; |
| 23 | import static com.android.server.wm.flicker.AutomationUtils.closePipWindow; |
| 24 | import static com.android.server.wm.flicker.AutomationUtils.exitSplitScreen; |
| 25 | import static com.android.server.wm.flicker.AutomationUtils.expandPipWindow; |
| 26 | import static com.android.server.wm.flicker.AutomationUtils.launchSplitScreen; |
| 27 | import static com.android.server.wm.flicker.AutomationUtils.stopPackage; |
| 28 | |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.os.RemoteException; |
| 32 | import android.platform.helpers.IAppHelper; |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 33 | import android.support.test.uiautomator.By; |
| 34 | import android.support.test.uiautomator.UiDevice; |
| 35 | import android.support.test.uiautomator.UiObject2; |
| 36 | import android.support.test.uiautomator.Until; |
| 37 | import android.util.Rational; |
| 38 | import android.view.Surface; |
| 39 | |
Brett Chabot | 502ec7a | 2019-03-01 14:43:20 -0800 | [diff] [blame] | 40 | import androidx.test.InstrumentationRegistry; |
| 41 | |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 42 | import com.android.server.wm.flicker.TransitionRunner.TransitionBuilder; |
| 43 | |
| 44 | /** |
| 45 | * Collection of common transitions which can be used to test different apps or scenarios. |
| 46 | */ |
| 47 | class CommonTransitions { |
| 48 | |
| 49 | public static final int ITERATIONS = 1; |
| 50 | private static final String TAG = "FLICKER"; |
| 51 | private static final long APP_LAUNCH_TIMEOUT = 10000; |
| 52 | |
| 53 | private static void setRotation(UiDevice device, int rotation) { |
| 54 | try { |
| 55 | switch (rotation) { |
| 56 | case Surface.ROTATION_270: |
| 57 | device.setOrientationLeft(); |
| 58 | break; |
| 59 | |
| 60 | case Surface.ROTATION_90: |
| 61 | device.setOrientationRight(); |
| 62 | break; |
| 63 | |
| 64 | case Surface.ROTATION_0: |
| 65 | default: |
| 66 | device.setOrientationNatural(); |
| 67 | } |
| 68 | // Wait for animation to complete |
| 69 | sleep(3000); |
| 70 | } catch (RemoteException e) { |
| 71 | throw new RuntimeException(e); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private static void clickEditTextWidget(UiDevice device, IAppHelper testApp) { |
| 76 | UiObject2 editText = device.findObject(By.res(testApp.getPackage(), "plain_text_input")); |
| 77 | editText.click(); |
| 78 | sleep(500); |
| 79 | } |
| 80 | |
| 81 | private static void clickEnterPipButton(UiDevice device, IAppHelper testApp) { |
| 82 | UiObject2 enterPipButton = device.findObject(By.res(testApp.getPackage(), "enter_pip")); |
| 83 | enterPipButton.click(); |
| 84 | sleep(500); |
| 85 | } |
| 86 | |
| 87 | static TransitionBuilder openAppWarm(IAppHelper testApp, UiDevice |
| 88 | device) { |
| 89 | return TransitionRunner.newBuilder() |
| 90 | .withTag("OpenAppWarm_" + testApp.getLauncherName()) |
| 91 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 92 | .runBeforeAll(testApp::open) |
| 93 | .runBefore(device::pressHome) |
| 94 | .runBefore(device::waitForIdle) |
| 95 | .run(testApp::open) |
| 96 | .runAfterAll(testApp::exit) |
| 97 | .runAfterAll(AutomationUtils::setDefaultWait) |
| 98 | .repeat(ITERATIONS); |
| 99 | } |
| 100 | |
| 101 | static TransitionBuilder closeAppWithBackKey(IAppHelper testApp, UiDevice |
| 102 | device) { |
| 103 | return TransitionRunner.newBuilder() |
| 104 | .withTag("closeAppWithBackKey_" + testApp.getLauncherName()) |
| 105 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 106 | .runBefore(testApp::open) |
| 107 | .runBefore(device::waitForIdle) |
| 108 | .run(device::pressBack) |
| 109 | .run(device::waitForIdle) |
| 110 | .runAfterAll(testApp::exit) |
| 111 | .runAfterAll(AutomationUtils::setDefaultWait) |
| 112 | .repeat(ITERATIONS); |
| 113 | } |
| 114 | |
| 115 | static TransitionBuilder closeAppWithHomeKey(IAppHelper testApp, UiDevice |
| 116 | device) { |
| 117 | return TransitionRunner.newBuilder() |
| 118 | .withTag("closeAppWithHomeKey_" + testApp.getLauncherName()) |
| 119 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 120 | .runBefore(testApp::open) |
| 121 | .runBefore(device::waitForIdle) |
| 122 | .run(device::pressHome) |
| 123 | .run(device::waitForIdle) |
| 124 | .runAfterAll(testApp::exit) |
| 125 | .runAfterAll(AutomationUtils::setDefaultWait) |
| 126 | .repeat(ITERATIONS); |
| 127 | } |
| 128 | |
| 129 | static TransitionBuilder getOpenAppCold(IAppHelper testApp, |
| 130 | UiDevice device) { |
| 131 | return TransitionRunner.newBuilder() |
| 132 | .withTag("OpenAppCold_" + testApp.getLauncherName()) |
| 133 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 134 | .runBefore(device::pressHome) |
| 135 | .runBefore(testApp::exit) |
| 136 | .runBefore(device::waitForIdle) |
| 137 | .run(testApp::open) |
| 138 | .runAfterAll(testApp::exit) |
| 139 | .repeat(ITERATIONS); |
| 140 | } |
| 141 | |
| 142 | static TransitionBuilder changeAppRotation(IAppHelper testApp, UiDevice |
| 143 | device, int beginRotation, int endRotation) { |
| 144 | return TransitionRunner.newBuilder() |
| 145 | .withTag("changeAppRotation_" + testApp.getLauncherName() |
| 146 | + rotationToString(beginRotation) + "_" + |
| 147 | rotationToString(endRotation)) |
| 148 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 149 | .runBeforeAll(testApp::open) |
| 150 | .runBefore(() -> setRotation(device, beginRotation)) |
| 151 | .run(() -> setRotation(device, endRotation)) |
| 152 | .runAfterAll(testApp::exit) |
| 153 | .runAfterAll(() -> setRotation(device, Surface.ROTATION_0)) |
| 154 | .repeat(ITERATIONS); |
| 155 | } |
| 156 | |
| 157 | static TransitionBuilder changeAppRotation(Intent intent, String intentId, Context context, |
| 158 | UiDevice |
| 159 | device, int beginRotation, int endRotation) { |
| 160 | final String testTag = "changeAppRotation_" + intentId + "_" + |
| 161 | rotationToString(beginRotation) + "_" + rotationToString(endRotation); |
| 162 | return TransitionRunner.newBuilder() |
| 163 | .withTag(testTag) |
| 164 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 165 | .runBeforeAll(() -> { |
| 166 | context.startActivity(intent); |
| 167 | device.wait(Until.hasObject(By.pkg(intent.getComponent() |
| 168 | .getPackageName()).depth(0)), APP_LAUNCH_TIMEOUT); |
| 169 | } |
| 170 | ) |
| 171 | .runBefore(() -> setRotation(device, beginRotation)) |
| 172 | .run(() -> setRotation(device, endRotation)) |
| 173 | .runAfterAll(() -> stopPackage(context, intent.getComponent().getPackageName())) |
| 174 | .runAfterAll(() -> setRotation(device, Surface.ROTATION_0)) |
| 175 | .repeat(ITERATIONS); |
| 176 | } |
| 177 | |
| 178 | static TransitionBuilder appToSplitScreen(IAppHelper testApp, UiDevice device) { |
| 179 | return TransitionRunner.newBuilder() |
| 180 | .withTag("appToSplitScreen_" + testApp.getLauncherName()) |
| 181 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 182 | .runBefore(testApp::open) |
| 183 | .runBefore(device::waitForIdle) |
| 184 | .runBefore(() -> sleep(500)) |
| 185 | .run(() -> launchSplitScreen(device)) |
| 186 | .runAfter(() -> exitSplitScreen(device)) |
| 187 | .runAfterAll(testApp::exit) |
| 188 | .repeat(ITERATIONS); |
| 189 | } |
| 190 | |
| 191 | static TransitionBuilder splitScreenToLauncher(IAppHelper testApp, UiDevice device) { |
| 192 | return TransitionRunner.newBuilder() |
| 193 | .withTag("splitScreenToLauncher_" + testApp.getLauncherName()) |
| 194 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 195 | .runBefore(testApp::open) |
| 196 | .runBefore(device::waitForIdle) |
| 197 | .runBefore(() -> launchSplitScreen(device)) |
| 198 | .run(() -> exitSplitScreen(device)) |
| 199 | .runAfterAll(testApp::exit) |
| 200 | .repeat(ITERATIONS); |
| 201 | } |
| 202 | |
| 203 | static TransitionBuilder editTextSetFocus(UiDevice device) { |
| 204 | IAppHelper testApp = new StandardAppHelper(InstrumentationRegistry.getInstrumentation(), |
| 205 | "com.android.server.wm.flicker.testapp", "ImeApp"); |
| 206 | return TransitionRunner.newBuilder() |
| 207 | .withTag("editTextSetFocus_" + testApp.getLauncherName()) |
| 208 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 209 | .runBefore(device::pressHome) |
| 210 | .runBefore(testApp::open) |
| 211 | .run(() -> clickEditTextWidget(device, testApp)) |
| 212 | .runAfterAll(testApp::exit) |
| 213 | .repeat(ITERATIONS); |
| 214 | } |
| 215 | |
| 216 | static TransitionBuilder resizeSplitScreen(IAppHelper testAppTop, IAppHelper testAppBottom, |
| 217 | UiDevice device, Rational startRatio, Rational stopRatio) { |
| 218 | String testTag = "resizeSplitScreen_" + testAppTop.getLauncherName() + "_" + |
| 219 | testAppBottom.getLauncherName() + "_" + |
| 220 | startRatio.toString().replace("/", ":") + "_to_" + |
| 221 | stopRatio.toString().replace("/", ":"); |
| 222 | return TransitionRunner.newBuilder() |
| 223 | .withTag(testTag) |
| 224 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 225 | .runBeforeAll(() -> clearRecents(device)) |
| 226 | .runBefore(testAppBottom::open) |
| 227 | .runBefore(device::pressHome) |
| 228 | .runBefore(testAppTop::open) |
| 229 | .runBefore(device::waitForIdle) |
| 230 | .runBefore(() -> launchSplitScreen(device)) |
| 231 | .runBefore(() -> { |
| 232 | UiObject2 snapshot = device.findObject( |
| 233 | By.res("com.google.android.apps.nexuslauncher", "snapshot")); |
| 234 | snapshot.click(); |
| 235 | }) |
| 236 | .runBefore(() -> AutomationUtils.resizeSplitScreen(device, startRatio)) |
| 237 | .run(() -> AutomationUtils.resizeSplitScreen(device, stopRatio)) |
| 238 | .runAfter(() -> exitSplitScreen(device)) |
| 239 | .runAfter(device::pressHome) |
| 240 | .runAfterAll(testAppTop::exit) |
| 241 | .runAfterAll(testAppBottom::exit) |
| 242 | .repeat(ITERATIONS); |
| 243 | } |
| 244 | |
| 245 | static TransitionBuilder editTextLoseFocusToHome(UiDevice device) { |
| 246 | IAppHelper testApp = new StandardAppHelper(InstrumentationRegistry.getInstrumentation(), |
| 247 | "com.android.server.wm.flicker.testapp", "ImeApp"); |
| 248 | return TransitionRunner.newBuilder() |
| 249 | .withTag("editTextLoseFocusToHome_" + testApp.getLauncherName()) |
| 250 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 251 | .runBefore(device::pressHome) |
| 252 | .runBefore(testApp::open) |
| 253 | .runBefore(() -> clickEditTextWidget(device, testApp)) |
| 254 | .run(device::pressHome) |
| 255 | .run(device::waitForIdle) |
| 256 | .runAfterAll(testApp::exit) |
| 257 | .repeat(ITERATIONS); |
| 258 | } |
| 259 | |
| 260 | static TransitionBuilder editTextLoseFocusToApp(UiDevice device) { |
| 261 | IAppHelper testApp = new StandardAppHelper(InstrumentationRegistry.getInstrumentation(), |
| 262 | "com.android.server.wm.flicker.testapp", "ImeApp"); |
| 263 | return TransitionRunner.newBuilder() |
| 264 | .withTag("editTextLoseFocusToApp_" + testApp.getLauncherName()) |
| 265 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 266 | .runBefore(device::pressHome) |
| 267 | .runBefore(testApp::open) |
| 268 | .runBefore(() -> clickEditTextWidget(device, testApp)) |
| 269 | .run(device::pressBack) |
| 270 | .run(device::waitForIdle) |
| 271 | .runAfterAll(testApp::exit) |
| 272 | .repeat(ITERATIONS); |
| 273 | } |
| 274 | |
| 275 | static TransitionBuilder enterPipMode(UiDevice device) { |
| 276 | IAppHelper testApp = new StandardAppHelper(InstrumentationRegistry.getInstrumentation(), |
| 277 | "com.android.server.wm.flicker.testapp", "PipApp"); |
| 278 | return TransitionRunner.newBuilder() |
| 279 | .withTag("enterPipMode_" + testApp.getLauncherName()) |
| 280 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 281 | .runBefore(device::pressHome) |
| 282 | .runBefore(testApp::open) |
| 283 | .run(() -> clickEnterPipButton(device, testApp)) |
| 284 | .runAfter(() -> closePipWindow(device)) |
| 285 | .runAfterAll(testApp::exit) |
| 286 | .repeat(ITERATIONS); |
| 287 | } |
| 288 | |
| 289 | static TransitionBuilder exitPipModeToHome(UiDevice device) { |
| 290 | IAppHelper testApp = new StandardAppHelper(InstrumentationRegistry.getInstrumentation(), |
| 291 | "com.android.server.wm.flicker.testapp", "PipApp"); |
| 292 | return TransitionRunner.newBuilder() |
| 293 | .withTag("exitPipModeToHome_" + testApp.getLauncherName()) |
| 294 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 295 | .runBefore(device::pressHome) |
| 296 | .runBefore(testApp::open) |
| 297 | .runBefore(() -> clickEnterPipButton(device, testApp)) |
| 298 | .run(() -> closePipWindow(device)) |
| 299 | .run(device::waitForIdle) |
| 300 | .runAfterAll(testApp::exit) |
| 301 | .repeat(ITERATIONS); |
| 302 | } |
| 303 | |
| 304 | static TransitionBuilder exitPipModeToApp(UiDevice device) { |
| 305 | IAppHelper testApp = new StandardAppHelper(InstrumentationRegistry.getInstrumentation(), |
| 306 | "com.android.server.wm.flicker.testapp", "PipApp"); |
| 307 | return TransitionRunner.newBuilder() |
| 308 | .withTag("exitPipModeToApp_" + testApp.getLauncherName()) |
| 309 | .runBeforeAll(AutomationUtils::wakeUpAndGoToHomeScreen) |
| 310 | .runBefore(device::pressHome) |
| 311 | .runBefore(testApp::open) |
| 312 | .runBefore(() -> clickEnterPipButton(device, testApp)) |
| 313 | .run(() -> expandPipWindow(device)) |
| 314 | .run(device::waitForIdle) |
| 315 | .runAfterAll(testApp::exit) |
| 316 | .repeat(ITERATIONS); |
| 317 | } |
| 318 | } |