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.widget.TextView; 20 21 import androidx.annotation.NonNull; 22 import androidx.test.uiautomator.By; 23 import androidx.test.uiautomator.BySelector; 24 import androidx.test.uiautomator.UiObject2; 25 26 import com.android.launcher3.testing.shared.TestProtocol; 27 28 import java.util.regex.Pattern; 29 30 /** 31 * App icon, whether in all apps, workspace or the taskbar. 32 */ 33 public abstract class AppIcon extends Launchable { 34 AppIcon(LauncherInstrumentation launcher, UiObject2 icon)35 AppIcon(LauncherInstrumentation launcher, UiObject2 icon) { 36 super(launcher, icon); 37 } 38 getAppIconSelector(String appName, LauncherInstrumentation launcher)39 static BySelector getAppIconSelector(String appName, LauncherInstrumentation launcher) { 40 return By.clazz(TextView.class).desc(makeMultilinePattern(appName)) 41 .pkg(launcher.getLauncherPackageName()); 42 } 43 getMenuItemSelector(String text, LauncherInstrumentation launcher)44 static BySelector getMenuItemSelector(String text, LauncherInstrumentation launcher) { 45 return By.clazz(TextView.class).text(text).pkg(launcher.getLauncherPackageName()); 46 } 47 getAnyAppIconSelector()48 static BySelector getAnyAppIconSelector() { 49 return By.clazz(TextView.class); 50 } 51 getLongClickEvent()52 protected abstract Pattern getLongClickEvent(); 53 54 /** 55 * Long-clicks the icon to open its menu. 56 */ openMenu()57 public AppIconMenu openMenu() { 58 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 59 return createMenu(mLauncher.clickAndGet( 60 mObject, /* resName= */ "popup_container", getLongClickEvent())); 61 } 62 } 63 64 /** 65 * Long-clicks the icon to open its menu, and looks at the deep shortcuts container only. 66 */ openDeepShortcutMenu()67 public AppIconMenu openDeepShortcutMenu() { 68 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 69 return createMenu(mLauncher.clickAndGet( 70 mObject, /* resName= */ "deep_shortcuts_container", getLongClickEvent())); 71 } 72 } 73 createMenu(UiObject2 menu)74 protected abstract AppIconMenu createMenu(UiObject2 menu); 75 76 @Override addExpectedEventsForLongClick()77 protected void addExpectedEventsForLongClick() { 78 mLauncher.expectEvent(TestProtocol.SEQUENCE_MAIN, getLongClickEvent()); 79 } 80 81 @Override waitForLongPressConfirmation()82 protected void waitForLongPressConfirmation() { 83 mLauncher.waitForLauncherObject("popup_container"); 84 } 85 86 @Override expectActivityStartEvents()87 protected void expectActivityStartEvents() { 88 mLauncher.expectEvent(TestProtocol.SEQUENCE_MAIN, LauncherInstrumentation.EVENT_START); 89 } 90 91 @Override launchableType()92 protected String launchableType() { 93 return "app icon"; 94 } 95 96 /** Return the app name of a icon */ 97 @NonNull getIconName()98 public String getIconName() { 99 return getObject().getText(); 100 } 101 102 /** 103 * Return the app name of a icon by the content description. This should be used when trying to 104 * get the name of an app where the text of it is multiline. 105 */ 106 @NonNull getAppName()107 String getAppName() { 108 return getObject().getContentDescription(); 109 } 110 111 /** 112 * Create a regular expression pattern that matches strings starting with the app name, where 113 * spaces in the app name are replaced with zero or more occurrences of the "\s" character 114 * (which represents a whitespace character in regular expressions), followed by any characters 115 * after the app name. 116 */ makeMultilinePattern(String appName)117 static Pattern makeMultilinePattern(String appName) { 118 return Pattern.compile(appName.replaceAll("\\s+", "\\\\s*") + ".*", 119 Pattern.DOTALL); 120 } 121 } 122