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