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 android.graphics.Point; 20 import android.graphics.Rect; 21 import android.widget.TextView; 22 23 import androidx.annotation.NonNull; 24 import androidx.test.uiautomator.By; 25 import androidx.test.uiautomator.BySelector; 26 import androidx.test.uiautomator.UiObject2; 27 28 import com.android.launcher3.testing.TestProtocol; 29 30 import java.util.regex.Pattern; 31 32 /** 33 * App icon, whether in all apps or in workspace/ 34 */ 35 public final class AppIcon extends Launchable implements FolderDragTarget { 36 37 private static final Pattern LONG_CLICK_EVENT = Pattern.compile("onAllAppsItemLongClick"); 38 AppIcon(LauncherInstrumentation launcher, UiObject2 icon)39 AppIcon(LauncherInstrumentation launcher, UiObject2 icon) { 40 super(launcher, icon); 41 } 42 getAppIconSelector(String appName, LauncherInstrumentation launcher)43 static BySelector getAppIconSelector(String appName, LauncherInstrumentation launcher) { 44 return By.clazz(TextView.class).text(appName).pkg(launcher.getLauncherPackageName()); 45 } 46 47 /** 48 * Long-clicks the icon to open its menu. 49 */ openMenu()50 public AppIconMenu openMenu() { 51 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 52 return new AppIconMenu(mLauncher, mLauncher.clickAndGet( 53 mObject, "popup_container", LONG_CLICK_EVENT)); 54 } 55 } 56 57 /** 58 * Long-clicks the icon to open its menu, and looks at the deep shortcuts container only. 59 */ openDeepShortcutMenu()60 public AppIconMenu openDeepShortcutMenu() { 61 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 62 return new AppIconMenu(mLauncher, mLauncher.clickAndGet( 63 mObject, "deep_shortcuts_container", LONG_CLICK_EVENT)); 64 } 65 } 66 67 /** 68 * Drag the AppIcon to the given position of other icon. The drag must result in a folder. 69 * 70 * @param target the destination icon. 71 */ 72 @NonNull dragToIcon(FolderDragTarget target)73 public FolderIcon dragToIcon(FolderDragTarget target) { 74 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck(); 75 LauncherInstrumentation.Closable c = mLauncher.addContextLayer("want to drag icon")) { 76 final Rect dropBounds = target.getDropLocationBounds(); 77 Workspace.dragIconToWorkspace( 78 mLauncher, this, 79 () -> { 80 final Rect bounds = target.getDropLocationBounds(); 81 return new Point(bounds.centerX(), bounds.centerY()); 82 }, 83 getLongPressIndicator()); 84 FolderIcon result = target.getTargetFolder(dropBounds); 85 mLauncher.assertTrue("Can't find the target folder.", result != null); 86 return result; 87 } 88 } 89 90 @Override addExpectedEventsForLongClick()91 protected void addExpectedEventsForLongClick() { 92 mLauncher.expectEvent(TestProtocol.SEQUENCE_MAIN, LONG_CLICK_EVENT); 93 } 94 95 @Override getLongPressIndicator()96 protected String getLongPressIndicator() { 97 return "popup_container"; 98 } 99 100 @Override expectActivityStartEvents()101 protected void expectActivityStartEvents() { 102 mLauncher.expectEvent(TestProtocol.SEQUENCE_MAIN, LauncherInstrumentation.EVENT_START); 103 } 104 105 @Override launchableType()106 protected String launchableType() { 107 return "app icon"; 108 } 109 110 @Override getDropLocationBounds()111 public Rect getDropLocationBounds() { 112 return mLauncher.getVisibleBounds(mObject); 113 } 114 115 @Override getTargetFolder(Rect bounds)116 public FolderIcon getTargetFolder(Rect bounds) { 117 for (FolderIcon folderIcon : mLauncher.getWorkspace().getFolderIcons()) { 118 final Rect folderIconBounds = folderIcon.getDropLocationBounds(); 119 if (bounds.contains(folderIconBounds.centerX(), folderIconBounds.centerY())) { 120 return folderIcon; 121 } 122 } 123 return null; 124 } 125 } 126