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 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 50 return launch(By.pkg(expectedPackageName)); 51 } 52 } 53 expectActivityStartEvents()54 protected abstract void expectActivityStartEvents(); 55 launchableType()56 protected abstract String launchableType(); 57 launch(BySelector selector)58 private Background launch(BySelector selector) { 59 try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer( 60 "want to launch an app from " + launchableType())) { 61 LauncherInstrumentation.log("Launchable.launch before click " 62 + mObject.getVisibleCenter() + " in " + mLauncher.getVisibleBounds(mObject)); 63 final String label = mObject.getText(); 64 65 mLauncher.executeAndWaitForEvent( 66 () -> { 67 mLauncher.clickLauncherObject(mObject); 68 expectActivityStartEvents(); 69 }, 70 event -> event.getEventType() == TYPE_WINDOW_STATE_CHANGED, 71 () -> "Launching an app didn't open a new window: " + label, 72 "clicking " + launchableType()); 73 74 try (LauncherInstrumentation.Closable c1 = mLauncher.addContextLayer("clicked")) { 75 mLauncher.assertTrue( 76 "App didn't start: " + label + " (" + selector + ")", 77 TestHelpers.wait(Until.hasObject(selector), 78 LauncherInstrumentation.WAIT_TIME_MS)); 79 return new Background(mLauncher); 80 } 81 } 82 } 83 84 /** 85 * Drags an object to the center of homescreen. 86 * 87 * @param startsActivity whether it's expected to start an activity. 88 * @param isWidgetShortcut whether we drag a widget shortcut 89 */ dragToWorkspace(boolean startsActivity, boolean isWidgetShortcut)90 public void dragToWorkspace(boolean startsActivity, boolean isWidgetShortcut) { 91 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 92 final Point launchableCenter = getObject().getVisibleCenter(); 93 final Point displaySize = mLauncher.getRealDisplaySize(); 94 final int width = displaySize.x / 2; 95 Workspace.dragIconToWorkspace( 96 mLauncher, 97 this, 98 new Point( 99 launchableCenter.x >= width 100 ? launchableCenter.x - width / 2 101 : launchableCenter.x + width / 2, 102 displaySize.y / 2), 103 getLongPressIndicator(), 104 startsActivity, 105 isWidgetShortcut, 106 () -> addExpectedEventsForLongClick()); 107 } 108 } 109 addExpectedEventsForLongClick()110 protected abstract void addExpectedEventsForLongClick(); 111 getLongPressIndicator()112 protected abstract String getLongPressIndicator(); 113 } 114