summaryrefslogtreecommitdiff
path: root/libraries/launcher-helper/src/android/support/test/launcherhelper/AutoLauncherStrategy.java
blob: 3a97a4b8d497f8b89d04582b3f2d38a4bcb945ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.support.test.launcherhelper;

import android.app.Instrumentation;
import android.os.SystemClock;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.Direction;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.system.helpers.CommandsHelper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AutoLauncherStrategy implements IAutoLauncherStrategy {
    private static final String LOG_TAG = AutoLauncherStrategy.class.getSimpleName();
    private static final String CAR_LENSPICKER = "com.android.car.carlauncher";
    private static final String SYSTEM_UI_PACKAGE = "com.android.systemui";
    private static final String MAPS_PACKAGE = "com.google.android.apps.maps";
    private static final String MEDIA_PACKAGE = "com.android.car.media";
    private static final String RADIO_PACKAGE = "com.android.car.radio";
    private static final String DIAL_PACKAGE = "com.android.car.dialer";
    private static final String ASSISTANT_PACKAGE = "com.google.android.googlequicksearchbox";
    private static final String SETTINGS_PACKAGE = "com.android.car.settings";
    private static final String APP_SWITCH_ID = "app_switch_container";
    private static final String APP_LIST_ID = "apps_grid";

    private static final long APP_LAUNCH_TIMEOUT = 30000;
    private static final long UI_WAIT_TIMEOUT = 5000;
    private static final long POLL_INTERVAL = 100;

    private static final BySelector UP_BTN =
            By.res(Pattern.compile(".*:id/car_ui_scrollbar_page_up"));
    private static final BySelector DOWN_BTN =
            By.res(Pattern.compile(".*:id/car_ui_scrollbar_page_down"));
    private static final BySelector APP_SWITCH = By.res(Pattern.compile(".*:id/" + APP_SWITCH_ID));
    private static final BySelector APP_LIST = By.res(Pattern.compile(".*:id/" + APP_LIST_ID));
    private static final BySelector SCROLLABLE_APP_LIST =
            By.res(Pattern.compile(".*:id/" + APP_LIST_ID)).scrollable(true);
    private static final BySelector QUICK_SETTINGS = By.res(SYSTEM_UI_PACKAGE, "qs");
    private static final BySelector LEFT_HVAC = By.res(SYSTEM_UI_PACKAGE, "hvacleft");
    private static final BySelector RIGHT_HVAC = By.res(SYSTEM_UI_PACKAGE, "hvacright");

    private static final Map<String, BySelector> FACET_MAP =
            Stream.of(new Object[][] {
                { "Home", By.res(SYSTEM_UI_PACKAGE, "home").clickable(true) },
                { "Maps", By.res(SYSTEM_UI_PACKAGE, "maps_nav").clickable(true) },
                { "Media", By.res(SYSTEM_UI_PACKAGE, "music_nav").clickable(true) },
                { "Dial", By.res(SYSTEM_UI_PACKAGE, "phone_nav").clickable(true) },
                { "App Grid", By.res(SYSTEM_UI_PACKAGE, "grid_nav").clickable(true) },
                { "Notification", By.res(SYSTEM_UI_PACKAGE, "notifications").clickable(true) },
                { "Google Assistant", By.res(SYSTEM_UI_PACKAGE, "assist").clickable(true) },
            }).collect(Collectors.toMap(data -> (String) data[0], data -> (BySelector) data[1]));

    private static final Map<String, BySelector> APP_OPEN_VERIFIERS =
            Stream.of(new Object[][] {
                { "Home", By.hasDescendant(By.res(CAR_LENSPICKER, "maps"))
                            .hasDescendant(By.res(CAR_LENSPICKER, "contextual"))
                            .hasDescendant(By.res(CAR_LENSPICKER, "playback"))
                },
                { "Maps", By.pkg(MAPS_PACKAGE).depth(0) },
                { "Media", By.pkg(MEDIA_PACKAGE).depth(0) },
                { "Radio", By.pkg(RADIO_PACKAGE).depth(0) },
                { "Dial",  By.pkg(DIAL_PACKAGE).depth(0) },
                { "App Grid", By.res(CAR_LENSPICKER, "apps_grid") },
                { "Notification", By.res(SYSTEM_UI_PACKAGE, "notifications") },
                { "Google Assistant", By.pkg(ASSISTANT_PACKAGE) },
                { "Settings", By.pkg(SETTINGS_PACKAGE).depth(0) },
            }).collect(Collectors.toMap(data -> (String) data[0], data -> (BySelector) data[1]));

    protected UiDevice mDevice;
    private Instrumentation mInstrumentation;
    protected CommandsHelper mCommandsHelper;

    /**
     * {@inheritDoc}
     */
    @Override
    public String getSupportedLauncherPackage() {
        return CAR_LENSPICKER;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setUiDevice(UiDevice uiDevice) {
        mDevice = uiDevice;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setInstrumentation(Instrumentation instrumentation) {
        mInstrumentation = instrumentation;
        mCommandsHelper = CommandsHelper.getInstance(mInstrumentation);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void open() {
        openHomeFacet();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openHomeFacet() {
        openFacet("Home");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openMapsFacet() {
        openFacet("Maps");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openMediaFacet() {
        openFacet("Media");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openMediaFacet(String appName) {
        openMediaFacet();

        // Click on app switch to open app list.
        UiObject2 appSwitch = mDevice.wait(Until.findObject(APP_SWITCH), APP_LAUNCH_TIMEOUT);
        if (appSwitch == null) {
            throw new RuntimeException("Failed to find app switch.");
        }
        appSwitch.clickAndWait(Until.newWindow(), UI_WAIT_TIMEOUT);
        mDevice.waitForIdle();

        // Click the targeted app in app list.
        UiObject2 app = findApplication(appName);
        if (app == null) {
            throw new RuntimeException(String.format("Failed to find %s app in media.", appName));
        }
        app.click();
        mDevice.wait(Until.gone(APP_LIST), UI_WAIT_TIMEOUT);

        // Verify either a Radio or Media app is in foreground for success.
        if (appName.equals("Radio")) {
            waitUntilAppOpen("Radio", APP_LAUNCH_TIMEOUT);
        } else {
            waitUntilAppOpen("Media", APP_LAUNCH_TIMEOUT);
        }

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openDialFacet() {
        openFacet("Dial");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openAppGridFacet() {
        openFacet("App Grid");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openNotificationFacet() {
        openFacet("Notification");
    }

    /** {@inheritDoc} */
    @Override
    public void openNotifications() {
        openNotificationFacet();
    }

    /** {@inheritDoc} */
    @Override
    public void pressHome() {
        openHomeFacet();
    }

    /** {@inheritDoc} */
    @Override
    public void openAssistantFacet() {
        openFacet("Google Assistant");
    }

    private void openFacet(String facetName) {
        BySelector facetSelector = FACET_MAP.get(facetName);
        UiObject2 facet = mDevice.findObject(facetSelector);
        if (facet != null) {
            facet.click();
            if (!facetName.equals("Media")) {
                // Verify the corresponding app has been open in the foregorund for success.
                waitUntilAppOpen(facetName, APP_LAUNCH_TIMEOUT);
            } else {
                // For Media facet, it could open either radio app or some media app.
                waitUntilOneOfTheAppOpen(Arrays.asList("Radio", "Media"), APP_LAUNCH_TIMEOUT);
            }
        } else {
            throw new RuntimeException(String.format("Failed to find %s facet.", facetName));
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void openQuickSettings() {
        UiObject2 quickSettings = mDevice.findObject(QUICK_SETTINGS);
        if (quickSettings != null) {
            quickSettings.click();
            waitUntilAppOpen("Settings", APP_LAUNCH_TIMEOUT);
        } else {
            throw new RuntimeException("Failed to find quick settings.");
        }
    }

    /**
     * Wait for <code>timeout</code> milliseconds until the BySelector which can verify that the app
     * corresponding to <code>appName</code> is in foreground has been found. If the BySelector
     * isn't found after timeout, throw an error.
     */
    private void waitUntilAppOpen(String appName, long timeout) {
        waitUntilOneOfTheAppOpen(Arrays.asList(appName), timeout);
    }

    /**
     * Wait for <code>time</code> milliseconds until one of the app in <code>appNames</code> has
     * been found in the foreground.
     */
    private void waitUntilOneOfTheAppOpen(List<String> appNames, long timeout) {
        long startTime = SystemClock.uptimeMillis();

        boolean isAnyOpen = false;
        while (SystemClock.uptimeMillis() - startTime < timeout) {
            isAnyOpen =
                    appNames.stream()
                            .map(appName -> mDevice.hasObject(APP_OPEN_VERIFIERS.get(appName)))
                            .anyMatch(isOpen -> isOpen == true);
            if (isAnyOpen) {
                break;
            }

            SystemClock.sleep(POLL_INTERVAL);
        }

        if (!isAnyOpen) {
            throw new IllegalStateException(
                    String.format(
                            "Did not find any app of %s in foreground after %d ms.",
                            String.join(", ", appNames), timeout));
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clickLeftHvac() {
        clickHvac(LEFT_HVAC);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clickRightHvac() {
        clickHvac(RIGHT_HVAC);
    }

    private void clickHvac(BySelector hvacSelector) {
        UiObject2 hvac = mDevice.findObject(hvacSelector);
        if (hvac != null) {
            // Hvac is not verifiable from uiautomator. It does not starts a new window, nor does
            // it spawn any specific identifiable ui components from uiautomator's perspective.
            // Therefore, this wait does not verify a new window, but rather it always timeout
            // after <code>APP_LAUNCH_TIMEOUT</code> amount of time so that hvac has sufficient
            // time to be opened/closed.
            hvac.clickAndWait(Until.newWindow(), APP_LAUNCH_TIMEOUT);
            mDevice.waitForIdle();
        } else {
            throw new RuntimeException("Failed to find hvac.");
        }
    }

    @Override
    public boolean checkApplicationExists(String appName) {
        openAppGridFacet();
        UiObject2 app = findApplication(appName);
        return app != null;
    }

    @Override
    public void openApp(String appName) {
        if (checkApplicationExists(appName)) {
            UiObject2 app = mDevice.findObject(By.clickable(true).hasDescendant(By.text(appName)));
            app.clickAndWait(Until.newWindow(), APP_LAUNCH_TIMEOUT);
            mDevice.waitForIdle();
        } else {
            throw new RuntimeException(String.format("Application %s not found", appName));
        }
    }

    @Override
    public void openBluetoothAudioApp() {
        String appName = "Bluetooth Audio";
        if (checkApplicationExists(appName)) {
            UiObject2 app = mDevice.findObject(By.clickable(true).hasDescendant(By.text(appName)));
            app.clickAndWait(Until.newWindow(), APP_LAUNCH_TIMEOUT);
            mDevice.waitForIdle();
        } else {
            throw new RuntimeException(String.format("Application %s not found", appName));
        }
    }

    @Override
    public void openGooglePlayStore() {
        openApp("Play Store");
        SystemClock.sleep(APP_LAUNCH_TIMEOUT);
    }

    private UiObject2 findApplication(String appName) {
        BySelector appSelector = By.clickable(true).hasDescendant(By.text(appName));
        if (mDevice.hasObject(SCROLLABLE_APP_LIST)) {
            // App list has more than 1 page. Scroll if necessary when searching for app.
            UiObject2 down = mDevice.findObject(DOWN_BTN);
            UiObject2 up = mDevice.findObject(UP_BTN);

            while (up.isEnabled()) {
                up.click();
                mDevice.waitForIdle();
            }

            UiObject2 app = mDevice.wait(Until.findObject(appSelector), UI_WAIT_TIMEOUT);
            while (app == null && down.isEnabled()) {
                down.click();
                mDevice.waitForIdle();
                app = mDevice.wait(Until.findObject(appSelector), UI_WAIT_TIMEOUT);
            }
            return app;
        } else {
            // App list only has 1 page.
            return mDevice.findObject(appSelector);
        }
    }

    @SuppressWarnings("unused")
    @Override
    public UiObject2 openAllApps(boolean reset) {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public BySelector getAllAppsButtonSelector() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public BySelector getAllAppsSelector() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public Direction getAllAppsScrollDirection() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public UiObject2 openAllWidgets(boolean reset) {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public BySelector getAllWidgetsSelector() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public Direction getAllWidgetsScrollDirection() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public BySelector getWorkspaceSelector() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public BySelector getHotSeatSelector() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public Direction getWorkspaceScrollDirection() {
        throw new UnsupportedOperationException(
                "The feature not supported on Auto");
    }

    @SuppressWarnings("unused")
    @Override
    public long launch(String appName, String packageName) {
        openApp(appName);
        return 0;
    }
}