Retry calls to AutofillManager.isEnabled()...

...otherwise tests could fail due to race condition.

Fixes: 126316043

Test: atest CtsAutoFillServiceTestCases:android.autofillservice.cts.LoginActivityTest#testSetupComplete
Test: atest CtsAutoFillServiceTestCases # sanity check

Change-Id: I413d2e9b714cf0e17689835c7a27e30675d32d91
Merged-In: I413d2e9b714cf0e17689835c7a27e30675d32d91
(cherry picked from commit 7fb68cdce9c3f4f0f8dade03fd5ce9212b7b288e)
diff --git a/tests/autofillservice/src/android/autofillservice/cts/Helper.java b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
index 2037259..1f0f5ac 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/Helper.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
@@ -53,6 +53,7 @@
 import android.view.ViewGroup;
 import android.view.ViewStructure.HtmlInfo;
 import android.view.autofill.AutofillId;
+import android.view.autofill.AutofillManager;
 import android.view.autofill.AutofillManager.AutofillCallback;
 import android.view.autofill.AutofillValue;
 import android.webkit.WebView;
@@ -1231,7 +1232,27 @@
                 "bitmap comparison failed; check contents of " + dump1 + " and " + dump2);
     }
 
-    @Nullable
+    /**
+     * Asserts that autofill is enabled in the context, retrying if necessariy.
+     */
+    public static void assertAutofillEnabled(@NonNull Context context, boolean expected)
+        throws Exception {
+      assertAutofillEnabled(context.getSystemService(AutofillManager.class), expected);
+    }
+
+    /**
+     * Asserts that autofill is enabled in the manager, retrying if necessariy.
+     */
+    public static void assertAutofillEnabled(@NonNull AutofillManager afm, boolean expected)
+        throws Exception {
+      Timeouts.IDLE_UNBIND_TIMEOUT.run("assertEnabled(" + expected + ")", () -> {
+            final boolean actual = afm.isEnabled();
+            Log.v(TAG, "assertEnabled(): expected=" + expected + ", actual=" + actual);
+            return actual == expected ? "not_used" : null;
+          });
+    }
+
+  @Nullable
     private static File dumpBitmap(@NonNull Bitmap bitmap, @NonNull File dir,
             @NonNull String filename) throws IOException {
         final File file = new File(dir, filename);
diff --git a/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java b/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
index d52bcf6..c208917a 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
@@ -2332,12 +2332,12 @@
 
         // Sanity check.
         final AutofillManager afm = mActivity.getAutofillManager();
-        assertThat(afm.isEnabled()).isTrue();
+        Helper.assertAutofillEnabled(afm, true);
 
         // Now disable user_complete and try again.
         try {
             setUserComplete(mContext, false);
-            assertThat(afm.isEnabled()).isFalse();
+            Helper.assertAutofillEnabled(afm, false);
         } finally {
             setUserComplete(mContext, true);
         }
diff --git a/tests/autofillservice/src/android/autofillservice/cts/WebViewActivityTest.java b/tests/autofillservice/src/android/autofillservice/cts/WebViewActivityTest.java
index 7149899..1a9ca43 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/WebViewActivityTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/WebViewActivityTest.java
@@ -32,7 +32,6 @@
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.ViewStructure.HtmlInfo;
-import android.view.autofill.AutofillManager;
 
 import org.junit.AfterClass;
 import org.junit.Before;
@@ -122,8 +121,7 @@
         // Load WebView
         final MyWebView myWebView = mActivity.loadWebView(mUiBot, usesAppContext);
         // Sanity check to make sure autofill is enabled in the application context
-        assertThat(myWebView.getContext().getSystemService(AutofillManager.class).isEnabled())
-                .isTrue();
+        Helper.assertAutofillEnabled(myWebView.getContext(), true);
 
         // Set expectations.
         myWebView.expectAutofill("dude", "sweet");