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.launcher3.tapl;
18 
19 import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
20 
21 import android.graphics.Point;
22 
23 import androidx.test.uiautomator.By;
24 import androidx.test.uiautomator.BySelector;
25 import androidx.test.uiautomator.UiObject2;
26 import androidx.test.uiautomator.Until;
27 
28 /**
29  * Ancestor for AppIcon and AppMenuItem.
30  */
31 abstract class Launchable {
32     protected final LauncherInstrumentation mLauncher;
33 
34     protected final UiObject2 mObject;
35 
Launchable(LauncherInstrumentation launcher, UiObject2 object)36     Launchable(LauncherInstrumentation launcher, UiObject2 object) {
37         mObject = object;
38         mLauncher = launcher;
39     }
40 
getObject()41     UiObject2 getObject() {
42         return mObject;
43     }
44 
45     /**
46      * Clicks the object to launch its app.
47      */
launch(String expectedPackageName)48     public Background launch(String expectedPackageName) {
49         return launch(By.pkg(expectedPackageName));
50     }
51 
launch(BySelector selector)52     private Background launch(BySelector selector) {
53         LauncherInstrumentation.log("Launchable.launch before click " +
54                 mObject.getVisibleCenter() + " in " + mObject.getVisibleBounds());
55 
56         mLauncher.executeAndWaitForEvent(
57                 () -> mObject.click(),
58                 event -> event.getEventType() == TYPE_WINDOW_STATE_CHANGED,
59                 "Launching an app didn't open a new window: " + mObject.getText());
60 
61         mLauncher.assertTrue(
62                 "App didn't start: " + selector,
63                 mLauncher.getDevice().wait(Until.hasObject(selector),
64                         LauncherInstrumentation.WAIT_TIME_MS));
65         return new Background(mLauncher);
66     }
67 
68     /**
69      * Drags an object to the center of homescreen.
70      */
dragToWorkspace()71     public void dragToWorkspace() {
72         final Point launchableCenter = getObject().getVisibleCenter();
73         final Point displaySize = mLauncher.getRealDisplaySize();
74         final int width = displaySize.x / 2;
75         Workspace.dragIconToWorkspace(
76                 mLauncher,
77                 this,
78                 new Point(
79                         launchableCenter.x >= width ?
80                                 launchableCenter.x - width / 2 : launchableCenter.x + width / 2,
81                         displaySize.y / 2),
82                 getLongPressIndicator());
83     }
84 
getLongPressIndicator()85     protected abstract String getLongPressIndicator();
86 }
87