Disable the services before each test

We set the AutofillService in some tests but we don’t disable
before the next test, the test execute time is very quick, sometimes
it may affect the next test. To reduce the problem, we disable the
AutofillService and AugmentedAutofillService before the test to make
sure the test state is clean.

Bug: 165822715
Test: atest AutofillPerfTests
Change-Id: I77c9cc0749c38823a4305a7a0a23f940295db828
diff --git a/apct-tests/perftests/autofill/src/android/view/autofill/AbstractAutofillPerfTestCase.java b/apct-tests/perftests/autofill/src/android/view/autofill/AbstractAutofillPerfTestCase.java
index d97b500..54e1860 100644
--- a/apct-tests/perftests/autofill/src/android/view/autofill/AbstractAutofillPerfTestCase.java
+++ b/apct-tests/perftests/autofill/src/android/view/autofill/AbstractAutofillPerfTestCase.java
@@ -16,19 +16,23 @@
 
 package android.view.autofill;
 
+import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
+
 import static org.junit.Assert.assertTrue;
 
 import android.os.Looper;
 import android.perftests.utils.PerfStatusReporter;
 import android.perftests.utils.PerfTestActivity;
-import android.perftests.utils.SettingsHelper;
 import android.perftests.utils.SettingsStateKeeperRule;
 import android.provider.Settings;
+import android.util.Log;
 
 import androidx.test.InstrumentationRegistry;
 import androidx.test.rule.ActivityTestRule;
 
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.rules.RuleChain;
@@ -38,6 +42,8 @@
  */
 public abstract class AbstractAutofillPerfTestCase {
 
+    private static final String TAG = "AbstractAutofillPerfTestCase";
+
     @ClassRule
     public static final SettingsStateKeeperRule mServiceSettingsKeeper =
             new SettingsStateKeeperRule(InstrumentationRegistry.getTargetContext(),
@@ -60,6 +66,27 @@
         mLayoutId = layoutId;
     }
 
+    @BeforeClass
+    public static void disableDefaultAugmentedService() {
+        Log.v(TAG, "@BeforeClass: disableDefaultAugmentedService()");
+        setDefaultAugmentedAutofillServiceEnabled(false);
+    }
+
+    @AfterClass
+    public static void enableDefaultAugmentedService() {
+        Log.v(TAG, "@AfterClass: enableDefaultAugmentedService()");
+        setDefaultAugmentedAutofillServiceEnabled(true);
+    }
+
+    /**
+     * Enables / disables the default augmented autofill service.
+     */
+    private static void setDefaultAugmentedAutofillServiceEnabled(boolean enabled) {
+        Log.d(TAG, "setDefaultAugmentedAutofillServiceEnabled(): " + enabled);
+        runShellCommand("cmd autofill set default-augmented-service-enabled 0 %s",
+                Boolean.toString(enabled));
+    }
+
     /**
      * Prepares the activity so that by the time the test is run it has reference to its fields.
      */
@@ -80,23 +107,4 @@
      * Initializes the {@link PerfTestActivity} after it was launched.
      */
     protected abstract void onCreate(PerfTestActivity activity);
-
-    /**
-     * Uses the {@code settings} binary to set the autofill service.
-     */
-    protected void setService() {
-        SettingsHelper.syncSet(InstrumentationRegistry.getTargetContext(),
-                SettingsHelper.NAMESPACE_SECURE,
-                Settings.Secure.AUTOFILL_SERVICE,
-                MyAutofillService.COMPONENT_NAME);
-    }
-
-    /**
-     * Uses the {@code settings} binary to reset the autofill service.
-     */
-    protected void resetService() {
-        SettingsHelper.syncDelete(InstrumentationRegistry.getTargetContext(),
-                SettingsHelper.NAMESPACE_SECURE,
-                Settings.Secure.AUTOFILL_SERVICE);
-    }
 }
diff --git a/apct-tests/perftests/autofill/src/android/view/autofill/AutofillTestWatcher.java b/apct-tests/perftests/autofill/src/android/view/autofill/AutofillTestWatcher.java
index f1f812d..2475d98 100644
--- a/apct-tests/perftests/autofill/src/android/view/autofill/AutofillTestWatcher.java
+++ b/apct-tests/perftests/autofill/src/android/view/autofill/AutofillTestWatcher.java
@@ -18,10 +18,13 @@
 
 import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
 
+import android.perftests.utils.SettingsHelper;
+import android.provider.Settings;
 import android.util.Log;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.test.InstrumentationRegistry;
 
 import org.junit.rules.TestWatcher;
 import org.junit.runner.Description;
@@ -44,19 +47,26 @@
     @Override
     protected void starting(Description description) {
         super.starting(description);
+        final String testName = description.getDisplayName();
+        Log.i(TAG, "Starting " + testName);
 
         enableVerboseLog();
-        MyAutofillService.resetStaticState();
-        MyAutofillService.setEnabled(true);
+        // Prepare the service before each test.
+        // Disable the current AutofillService.
+        resetAutofillService();
+        // Set MyAutofillService status enable, it can start to accept the calls.
+        enableMyAutofillService();
         setServiceWatcher();
     }
 
     @Override
     protected void finished(Description description) {
         super.finished(description);
-
+        final String testName = description.getDisplayName();
+        Log.i(TAG, "Finished " + testName);
         restoreLogLevel();
-        disableService();
+        // Set MyAutofillService status disable, so the calls are ignored.
+        disableMyAutofillService();
         clearServiceWatcher();
     }
 
@@ -67,12 +77,31 @@
         }
     }
 
-    private void enableService() {
+    /**
+     * Uses the {@code settings} binary to set the autofill service.
+     */
+    void setAutofillService() {
+        SettingsHelper.syncSet(InstrumentationRegistry.getTargetContext(),
+                SettingsHelper.NAMESPACE_SECURE,
+                Settings.Secure.AUTOFILL_SERVICE,
+                MyAutofillService.COMPONENT_NAME);
+    }
+
+    /**
+     * Uses the {@code settings} binary to reset the autofill service.
+     */
+    void resetAutofillService() {
+        SettingsHelper.syncDelete(InstrumentationRegistry.getTargetContext(),
+                SettingsHelper.NAMESPACE_SECURE,
+                Settings.Secure.AUTOFILL_SERVICE);
+    }
+
+    private void enableMyAutofillService() {
         MyAutofillService.resetStaticState();
         MyAutofillService.setEnabled(true);
     }
 
-    private void disableService() {
+    private void disableMyAutofillService() {
         // Must disable service so calls are ignored in case of errors during the test case;
         // otherwise, other tests will fail because these calls are made in the UI thread (as both
         // the service, the tests, and the app run in the same process).
@@ -88,7 +117,7 @@
     }
 
     private void restoreLogLevel() {
-        Log.w(TAG, "restoreLogLevel to " + mOriginalLogLevel);
+        Log.d(TAG, "restoreLogLevel to " + mOriginalLogLevel);
         if (!mOriginalLogLevel.equals("verbose")) {
             runShellCommand("cmd autofill set log_level %s", mOriginalLogLevel);
         }
diff --git a/apct-tests/perftests/autofill/src/android/view/autofill/LoginTest.java b/apct-tests/perftests/autofill/src/android/view/autofill/LoginTest.java
index 99b2590..37b4bde 100644
--- a/apct-tests/perftests/autofill/src/android/view/autofill/LoginTest.java
+++ b/apct-tests/perftests/autofill/src/android/view/autofill/LoginTest.java
@@ -55,7 +55,7 @@
      */
     @Test
     public void testFocus_noService() throws Throwable {
-        resetService();
+        mTestWatcher.resetAutofillService();
 
         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
         while (state.keepRunning()) {
@@ -73,7 +73,7 @@
     @Test
     public void testFocus_serviceDoesNotAutofill() throws Throwable {
         MyAutofillService.newCannedResponse().reply();
-        setService();
+        mTestWatcher.setAutofillService();
 
         // Must first focus in a field to trigger autofill and wait for service response
         // outside the loop
@@ -102,7 +102,7 @@
                 .setUsername(mUsername.getAutofillId(), "user")
                 .setPassword(mPassword.getAutofillId(), "pass")
                 .reply();
-        setService();
+        mTestWatcher.setAutofillService();
 
         // Callback is used to slow down the calls made to the autofill server so the
         // app is not crashed due to binder exhaustion. But the time spent waiting for the callbacks
@@ -157,7 +157,7 @@
                 .setUsername(mUsername.getAutofillId(), "user")
                 .setIgnored(mPassword.getAutofillId())
                 .reply();
-        setService();
+        mTestWatcher.setAutofillService();
 
         // Callback is used to slow down the calls made to the autofill server so the
         // app is not crashed due to binder exhaustion. But the time spent waiting for the callbacks
@@ -201,7 +201,7 @@
      */
     @Test
     public void testChange_noService() throws Throwable {
-        resetService();
+        mTestWatcher.resetAutofillService();
 
         changeTest(false);
     }
@@ -213,7 +213,7 @@
     @Test
     public void testChange_serviceDoesNotAutofill() throws Throwable {
         MyAutofillService.newCannedResponse().reply();
-        setService();
+        mTestWatcher.setAutofillService();
 
         changeTest(true);
     }
@@ -227,7 +227,7 @@
                 .setUsername(mUsername.getAutofillId(), "user")
                 .setPassword(mPassword.getAutofillId(), "pass")
                 .reply();
-        setService();
+        mTestWatcher.setAutofillService();
 
         changeTest(true);
     }
@@ -242,7 +242,7 @@
                 .setUsername(mUsername.getAutofillId(), "user")
                 .setIgnored(mPassword.getAutofillId())
                 .reply();
-        setService();
+        mTestWatcher.setAutofillService();
 
         changeTest(true);
     }
@@ -274,7 +274,7 @@
                 .setUsername(mUsername.getAutofillId(), "user")
                 .setPassword(mPassword.getAutofillId(), "pass")
                 .reply();
-        setService();
+        mTestWatcher.setAutofillService();
 
         MyAutofillCallback callback = new MyAutofillCallback();
         mAfm.registerCallback(callback);
diff --git a/apct-tests/perftests/autofill/src/android/view/autofill/MyAutofillService.java b/apct-tests/perftests/autofill/src/android/view/autofill/MyAutofillService.java
index 77c1b85..ddac68b 100644
--- a/apct-tests/perftests/autofill/src/android/view/autofill/MyAutofillService.java
+++ b/apct-tests/perftests/autofill/src/android/view/autofill/MyAutofillService.java
@@ -126,6 +126,7 @@
             onError("ignoring onFillRequest(): response not set", callback);
             return;
         }
+        // TODO(b/162216576): fix error FillResponse
         Dataset.Builder dataset = new Dataset.Builder(newDatasetPresentation("dataset"));
         boolean hasData = false;
         if (response.mUsername != null) {