From df4dfdba4dcbb2a1521dfa76d2a206918fca6b4d Mon Sep 17 00:00:00 2001 From: Brandon Dayauon Date: Tue, 13 Jun 2023 10:55:35 -0700 Subject: [PATCH] Update tests for ENABLE_TWOLINE_ALLAPPS and ENABLE_TWOLINE_DEVICESEARCH PM said we can enable ENABLE_TWOLINE_ALLAPPS and ENABLE_TWOLINE_DEVICESEARCH flags. Since I am enabling two-line text for all apps, it's possible that the text would have other texts within the app name itself. For example, if a title of an app is long for example "Amazon Shopping" the title can now be like "Amazon\nShopping". For tests to recognize its "Amazon shopping", I am resorting to identifying the app title based on the content description. Turn on the two feature flags in another CL bug: 287307252 test: manual Change-Id: I8bdc3db710514c9098ccb5d9781a100ac9b35eab --- .../launcher3/tapl/TaplUtilityTests.java | 38 +++++++++++++++++++ .../launcher3/ui/TaplTestsLauncher3.java | 4 +- .../ui/workspace/ThemeIconsTest.java | 5 +-- .../com/android/launcher3/tapl/AllApps.java | 3 ++ .../com/android/launcher3/tapl/AppIcon.java | 26 ++++++++++++- .../android/launcher3/tapl/AppIconMenu.java | 4 +- 6 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 tests/src/com/android/launcher3/tapl/TaplUtilityTests.java diff --git a/tests/src/com/android/launcher3/tapl/TaplUtilityTests.java b/tests/src/com/android/launcher3/tapl/TaplUtilityTests.java new file mode 100644 index 0000000000..15db1d887c --- /dev/null +++ b/tests/src/com/android/launcher3/tapl/TaplUtilityTests.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 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 com.android.launcher3.tapl; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class TaplUtilityTests { + + @Test + public void testNewStringWithRegex() { + assertTrue(AppIcon.makeMultilinePattern("Play Store") + .matcher("Play Store has 7 notifications").matches()); + assertTrue(AppIcon.makeMultilinePattern("Play Store") + .matcher("Play Store!").matches()); + assertFalse(AppIcon.makeMultilinePattern("Play Store") + .matcher("play store").matches()); + assertFalse(AppIcon.makeMultilinePattern("Play Store") + .matcher("").matches()); + assertTrue(AppIcon.makeMultilinePattern("Play Store") + .matcher("Play \n Store").matches()); + } +} diff --git a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java index 0d63a68781..b67478f1c9 100644 --- a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java +++ b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java @@ -683,8 +683,8 @@ public class TaplTestsLauncher3 extends AbstractLauncherUiTest { HomeAllApps allApps = mLauncher.getWorkspace().switchToAllApps(); allApps.freeze(); try { - HomeAppIcon icon = allApps.getAppIcon(APP_NAME); - assertEquals("Wrong app icon name.", icon.getIconName(), APP_NAME); + // getAppIcon() already verifies that the icon is not null and is the correct icon name. + allApps.getAppIcon(APP_NAME); } finally { allApps.unfreeze(); } diff --git a/tests/src/com/android/launcher3/ui/workspace/ThemeIconsTest.java b/tests/src/com/android/launcher3/ui/workspace/ThemeIconsTest.java index 7ba0b53b22..8e5e9cc2a1 100644 --- a/tests/src/com/android/launcher3/ui/workspace/ThemeIconsTest.java +++ b/tests/src/com/android/launcher3/ui/workspace/ThemeIconsTest.java @@ -147,9 +147,8 @@ public class ThemeIconsTest extends AbstractLauncherUiTest { for (int i = parent.getChildCount() - 1; i >= 0; i--) { viewQueue.add(parent.getChildAt(i)); } - } else if (view instanceof BubbleTextView) { - BubbleTextView btv = (BubbleTextView) view; - if (title.equals(btv.getText())) { + } else if (view instanceof BubbleTextView btv) { + if (title.equals(btv.getContentDescription().toString())) { icon = btv; break; } diff --git a/tests/tapl/com/android/launcher3/tapl/AllApps.java b/tests/tapl/com/android/launcher3/tapl/AllApps.java index 399abc7ad2..23d09d4928 100644 --- a/tests/tapl/com/android/launcher3/tapl/AllApps.java +++ b/tests/tapl/com/android/launcher3/tapl/AllApps.java @@ -210,6 +210,9 @@ public abstract class AllApps extends LauncherInstrumentation.VisibleContainer { public AppIcon getAppIcon(String appName) { AppIcon appIcon = tryGetAppIcon(appName); mLauncher.assertNotNull("Unable to scroll to a clickable icon: " + appName, appIcon); + // appIcon.getAppName() checks for content description, so it is possible that it can have + // trailing words. So check if the content description contains the appName. + mLauncher.assertTrue("Wrong app icon name.", appIcon.getAppName().contains(appName)); return appIcon; } diff --git a/tests/tapl/com/android/launcher3/tapl/AppIcon.java b/tests/tapl/com/android/launcher3/tapl/AppIcon.java index 0a0cf07482..85098c899c 100644 --- a/tests/tapl/com/android/launcher3/tapl/AppIcon.java +++ b/tests/tapl/com/android/launcher3/tapl/AppIcon.java @@ -37,10 +37,14 @@ public abstract class AppIcon extends Launchable { } static BySelector getAppIconSelector(String appName, LauncherInstrumentation launcher) { - return By.clazz(TextView.class).textContains(appName) + return By.clazz(TextView.class).desc(makeMultilinePattern(appName)) .pkg(launcher.getLauncherPackageName()); } + static BySelector getMenuItemSelector(String text, LauncherInstrumentation launcher) { + return By.clazz(TextView.class).text(text).pkg(launcher.getLauncherPackageName()); + } + static BySelector getAnyAppIconSelector() { return By.clazz(TextView.class); } @@ -94,4 +98,24 @@ public abstract class AppIcon extends Launchable { public String getIconName() { return getObject().getText(); } + + /** + * Return the app name of a icon by the content description. This should be used when trying to + * get the name of an app where the text of it is multiline. + */ + @NonNull + String getAppName() { + return getObject().getContentDescription(); + } + + /** + * Create a regular expression pattern that matches strings starting with the app name, where + * spaces in the app name are replaced with zero or more occurrences of the "\s" character + * (which represents a whitespace character in regular expressions), followed by any characters + * after the app name. + */ + static Pattern makeMultilinePattern(String appName) { + return Pattern.compile(appName.replaceAll("\\s+", "\\\\s*") + ".*", + Pattern.DOTALL); + } } diff --git a/tests/tapl/com/android/launcher3/tapl/AppIconMenu.java b/tests/tapl/com/android/launcher3/tapl/AppIconMenu.java index 667290f0a2..bbcc6a85ba 100644 --- a/tests/tapl/com/android/launcher3/tapl/AppIconMenu.java +++ b/tests/tapl/com/android/launcher3/tapl/AppIconMenu.java @@ -50,7 +50,7 @@ public abstract class AppIconMenu { */ public AppIconMenuItem getMenuItem(String shortcutText) { final UiObject2 menuItem = mLauncher.waitForObjectInContainer(mDeepShortcutsContainer, - AppIcon.getAppIconSelector(shortcutText, mLauncher)); + AppIcon.getMenuItemSelector(shortcutText, mLauncher)); return createMenuItem(menuItem); } @@ -59,7 +59,7 @@ public abstract class AppIconMenu { */ public SplitScreenMenuItem getSplitScreenMenuItem() { final UiObject2 menuItem = mLauncher.waitForObjectInContainer(mDeepShortcutsContainer, - AppIcon.getAppIconSelector("Split screen", mLauncher)); + AppIcon.getMenuItemSelector("Split screen", mLauncher)); return new SplitScreenMenuItem(mLauncher, menuItem); }