Check entire context menu for AUTOFILL.

Bug: 138982829
Test: atest android.autofillservice.cts.LoginActivityTest#testAutofillManuallyOneDatasetWhenClipboardFull
Test: atest CtsAutoFillServiceTestCases
Change-Id: I463adeb711050347cae53c58a1734f9c22db8a91
(cherry picked from commit 9704bbabd10968e44e40a58d0b7d14453835c4c0)
(cherry picked from commit 6fbf288992f11c62ca85b595edda4b5e67bb6e3b)
diff --git a/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java b/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
index 2c154eb..0dbfc73 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
@@ -2087,7 +2087,7 @@
         mActivity.expectAutoFill("dude", "sweet");
 
         // Explicitly uses the contextual menu to test that functionality.
-        mUiBot.getAutofillMenuOption(ID_USERNAME, false).click();
+        mUiBot.getAutofillMenuOption(ID_USERNAME).click();
 
         final FillRequest fillRequest = sReplier.getNextFillRequest();
         assertHasFlags(fillRequest.flags, FLAG_MANUAL_REQUEST);
@@ -2121,7 +2121,7 @@
         mActivity.expectAutoFill("dude", "sweet");
 
         // Explicitly uses the contextual menu to test that functionality.
-        mUiBot.getAutofillMenuOption(ID_USERNAME, true).click();
+        mUiBot.getAutofillMenuOption(ID_USERNAME).click();
 
         final FillRequest fillRequest = sReplier.getNextFillRequest();
         assertHasFlags(fillRequest.flags, FLAG_MANUAL_REQUEST);
diff --git a/tests/autofillservice/src/android/autofillservice/cts/UiBot.java b/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
index bcc1bc0..ff884ce 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
@@ -760,9 +760,8 @@
      * faster.
      *
      * @param id resource id of the field.
-     * @param expectOverflow whether overflow menu should be shown (when clipboard contains text)
      */
-    public UiObject2 getAutofillMenuOption(String id, boolean expectOverflow) throws Exception {
+    public UiObject2 getAutofillMenuOption(String id) throws Exception {
         final UiObject2 field = waitForObject(By.res(mPackageName, id));
         // TODO: figure out why obj.longClick() doesn't always work
         field.click(3000);
@@ -771,31 +770,36 @@
                 By.res("android", RESOURCE_ID_CONTEXT_MENUITEM), mDefaultTimeout);
         final String expectedText = getAutofillContextualMenuTitle();
 
-        if (expectOverflow) {
-            // Check first menu does not have AUTOFILL
-            for (UiObject2 menuItem : menuItems) {
-                final String menuName = menuItem.getText();
-                if (menuName.equalsIgnoreCase(expectedText)) {
-                    throw new IllegalStateException(expectedText + " in context menu");
-                }
-            }
-
-            final BySelector overflowSelector = By.res("android", RESOURCE_ID_OVERFLOW);
-
-            // Click overflow menu button.
-            final UiObject2 overflowMenu = waitForObject(overflowSelector, mDefaultTimeout);
-            overflowMenu.click();
-
-            // Wait for overflow menu to show.
-            mDevice.wait(Until.gone(overflowSelector), 1000);
-        }
-
-        menuItems = waitForObjects(
-                By.res("android", RESOURCE_ID_CONTEXT_MENUITEM), mDefaultTimeout);
         final StringBuffer menuNames = new StringBuffer();
+
+        // Check first menu for AUTOFILL
         for (UiObject2 menuItem : menuItems) {
             final String menuName = menuItem.getText();
             if (menuName.equalsIgnoreCase(expectedText)) {
+                Log.v(TAG, "AUTOFILL found in first menu");
+                return menuItem;
+            }
+            menuNames.append("'").append(menuName).append("' ");
+        }
+
+        menuNames.append(";");
+
+        // First menu does not have AUTOFILL, check overflow
+        final BySelector overflowSelector = By.res("android", RESOURCE_ID_OVERFLOW);
+
+        // Click overflow menu button.
+        final UiObject2 overflowMenu = waitForObject(overflowSelector, mDefaultTimeout);
+        overflowMenu.click();
+
+        // Wait for overflow menu to show.
+        mDevice.wait(Until.gone(overflowSelector), 1000);
+
+        menuItems = waitForObjects(
+                By.res("android", RESOURCE_ID_CONTEXT_MENUITEM), mDefaultTimeout);
+        for (UiObject2 menuItem : menuItems) {
+            final String menuName = menuItem.getText();
+            if (menuName.equalsIgnoreCase(expectedText)) {
+                Log.v(TAG, "AUTOFILL found in overflow menu");
                 return menuItem;
             }
             menuNames.append("'").append(menuName).append("' ");