Merge "Use BySelector to search for text in search results." into main
diff --git a/tests/tapl/com/android/launcher3/tapl/SearchResultFromQsb.java b/tests/tapl/com/android/launcher3/tapl/SearchResultFromQsb.java
index 4be46ab..c58d16e 100644
--- a/tests/tapl/com/android/launcher3/tapl/SearchResultFromQsb.java
+++ b/tests/tapl/com/android/launcher3/tapl/SearchResultFromQsb.java
@@ -17,10 +17,12 @@
 
 import static com.android.launcher3.testing.shared.TestProtocol.NORMAL_STATE_ORDINAL;
 
-import android.text.TextUtils;
+import android.graphics.Rect;
 import android.widget.TextView;
 
 import androidx.test.uiautomator.By;
+import androidx.test.uiautomator.BySelector;
+import androidx.test.uiautomator.Direction;
 import androidx.test.uiautomator.UiObject2;
 
 import java.util.ArrayList;
@@ -34,10 +36,12 @@
     // This particular ID change should happen with caution
     private static final String SEARCH_CONTAINER_RES_ID = "search_results_list_view";
     protected final LauncherInstrumentation mLauncher;
+    private final UiObject2 mSearchContainer;
 
     SearchResultFromQsb(LauncherInstrumentation launcher) {
         mLauncher = launcher;
         mLauncher.waitForLauncherObject("search_container_all_apps");
+        mSearchContainer = mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID);
     }
 
     /** Find the app from search results with app name. */
@@ -52,18 +56,9 @@
 
     /** Find the web suggestion from search suggestion's title text */
     public SearchWebSuggestion findWebSuggestion(String text) {
-        ArrayList<UiObject2> webSuggestions =
-                new ArrayList<>(mLauncher.waitForObjectsInContainer(
-                        mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID),
-                        By.clazz(TextView.class)));
-        for (UiObject2 uiObject: webSuggestions) {
-            String currentString = uiObject.getText();
-            if (currentString.equals(text)) {
-                return createWebSuggestion(uiObject);
-            }
-        }
-        mLauncher.fail("Web suggestion title: " + text + " not found");
-        return null;
+        UiObject2 webSuggestion = mLauncher.waitForObjectInContainer(mSearchContainer,
+                getSearchResultSelector(text));
+        return createWebSuggestion(webSuggestion);
     }
 
     protected SearchWebSuggestion createWebSuggestion(UiObject2 webSuggestion) {
@@ -73,13 +68,25 @@
     /** Find the total amount of views being displayed and return the size */
     public int getSearchResultItemSize() {
         ArrayList<UiObject2> searchResultItems =
-                new ArrayList<>(mLauncher.waitForObjectsInContainer(
-                        mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID),
+                new ArrayList<>(mLauncher.waitForObjectsInContainer(mSearchContainer,
                         By.clazz(TextView.class)));
         return searchResultItems.size();
     }
 
     /**
+     * Scroll down to make next page search results rendered.
+     */
+    public void getNextPageSearchResults() {
+        final int searchContainerHeight = mLauncher.getVisibleBounds(mSearchContainer).height();
+        // Start scrolling from center of the screen to top of the screen.
+        mLauncher.scroll(mSearchContainer,
+                Direction.DOWN,
+                new Rect(0, 0, 0, searchContainerHeight / 2),
+                /* steps= */ 10,
+                /*slowDown= */ false);
+    }
+
+    /**
      * Taps outside bottom sheet to dismiss and return to workspace. Available on tablets only.
      * @param tapRight Tap on the right of bottom sheet if true, or left otherwise.
      */
@@ -121,25 +128,13 @@
 
     /** Verify a tile is present by checking its title and subtitle. */
     public void verifyTileIsPresent(String title, String subtitle) {
-        ArrayList<UiObject2> searchResults =
-                new ArrayList<>(mLauncher.waitForObjectsInContainer(
-                        mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID),
-                        By.clazz(TextView.class)));
-        boolean foundTitle = false;
-        boolean foundSubtitle = false;
-        for (UiObject2 uiObject: searchResults) {
-            String currentString = uiObject.getText();
-            if (TextUtils.equals(currentString, title)) {
-                foundTitle = true;
-            } else if (TextUtils.equals(currentString, subtitle)) {
-                foundSubtitle = true;
-            }
-        }
-        if (!foundTitle) {
-            mLauncher.fail("Tile not found for title: " + title);
-        }
-        if (!foundSubtitle) {
-            mLauncher.fail("Tile not found for subtitle: " + subtitle);
-        }
+        mLauncher.waitForObjectInContainer(mSearchContainer,
+                getSearchResultSelector(title));
+        mLauncher.waitForObjectInContainer(mSearchContainer,
+                getSearchResultSelector(subtitle));
+    }
+
+    private BySelector getSearchResultSelector(String text) {
+        return By.clazz(TextView.class).text(text);
     }
 }