Get rid of custom negaitve save button label - CTS

Test: CTS tests pass including the two new tests

bug:37649790

Change-Id: I6f38264981182f7874856367a7d9a6febf9c5bea
diff --git a/tests/autofillservice/src/android/autofillservice/cts/CannedFillResponse.java b/tests/autofillservice/src/android/autofillservice/cts/CannedFillResponse.java
index 2155af0..9f5368f 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/CannedFillResponse.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/CannedFillResponse.java
@@ -67,7 +67,7 @@
     private final IntentSender mAuthentication;
     private final String[] mAuthenticationIds;
     private final String[] mIgnoredIds;
-    private final CharSequence mNegativeActionLabel;
+    private final int mNegativeActionStyle;
     private final IntentSender mNegativeActionListener;
     private final int mFlags;
 
@@ -82,7 +82,7 @@
         mAuthentication = builder.mAuthentication;
         mAuthenticationIds = builder.mAuthenticationIds;
         mIgnoredIds = builder.mIgnoredIds;
-        mNegativeActionLabel = builder.mNegativeActionLabel;
+        mNegativeActionStyle = builder.mNegativeActionStyle;
         mNegativeActionListener = builder.mNegativeActionListener;
         mFlags = builder.mFlags;
     }
@@ -118,9 +118,7 @@
             if (mSaveDescription != null) {
                 saveInfo.setDescription(mSaveDescription);
             }
-            if (mNegativeActionLabel != null) {
-                saveInfo.setNegativeAction(mNegativeActionLabel, mNegativeActionListener);
-            }
+            saveInfo.setNegativeAction(mNegativeActionStyle, mNegativeActionListener);
             builder.setSaveInfo(saveInfo.build());
         }
         if (mIgnoredIds != null) {
@@ -158,7 +156,7 @@
         private IntentSender mAuthentication;
         private String[] mAuthenticationIds;
         private String[] mIgnoredIds;
-        private CharSequence mNegativeActionLabel;
+        private int mNegativeActionStyle;
         private IntentSender mNegativeActionListener;
         private int mFlags;
 
@@ -241,9 +239,9 @@
         /**
          * Sets the negative action spec.
          */
-        public Builder setNegativeAction(CharSequence label,
+        public Builder setNegativeAction(int style,
                 IntentSender listener) {
-            mNegativeActionLabel = label;
+            mNegativeActionStyle = style;
             mNegativeActionListener = listener;
             return this;
         }
diff --git a/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java b/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
index 30cfde7..37beb59 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/LoginActivityTest.java
@@ -62,6 +62,7 @@
 import android.content.IntentSender;
 import android.os.Bundle;
 import android.service.autofill.FillEventHistory;
+import android.service.autofill.SaveInfo;
 import android.support.test.rule.ActivityTestRule;
 import android.support.test.uiautomator.UiObject2;
 import android.view.View;
@@ -1146,7 +1147,7 @@
     }
 
     @Test
-    public void testCustomNegativeSaveButton() throws Exception {
+    public void testRejectStyleNegativeSaveButton() throws Exception {
         enableService();
 
         // Set service behavior.
@@ -1159,7 +1160,7 @@
 
         sReplier.addResponse(new CannedFillResponse.Builder()
                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_USERNAME, ID_PASSWORD)
-                .setNegativeAction("Foo", listener)
+                .setNegativeAction(SaveInfo.NEGATIVE_BUTTON_STYLE_REJECT, listener)
                 .build());
 
         // Trigger auto-fill.
@@ -1185,7 +1186,8 @@
         }, intentFilter);
 
         // Trigger the negative button.
-        sUiBot.saveForAutofill(false, SAVE_DATA_TYPE_PASSWORD);
+        sUiBot.saveForAutofill(SaveInfo.NEGATIVE_BUTTON_STYLE_REJECT,
+                false, SAVE_DATA_TYPE_PASSWORD);
 
         // Wait for the custom action.
         assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
@@ -1194,6 +1196,55 @@
     }
 
     @Test
+    public void testCancelStyleNegativeSaveButton() throws Exception {
+        enableService();
+
+        // Set service behavior.
+
+        final String intentAction = "android.autofillservice.cts.CUSTOM_ACTION";
+
+        // Configure the save UI.
+        final IntentSender listener = PendingIntent.getBroadcast(
+                getContext(), 0, new Intent(intentAction), 0).getIntentSender();
+
+        sReplier.addResponse(new CannedFillResponse.Builder()
+                .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_USERNAME, ID_PASSWORD)
+                .setNegativeAction(SaveInfo.NEGATIVE_BUTTON_STYLE_CANCEL, listener)
+                .build());
+
+        // Trigger auto-fill.
+        mActivity.onUsername(View::requestFocus);
+
+        // Wait for onFill() before proceeding.
+        sReplier.getNextFillRequest();
+
+        // Trigger save.
+        mActivity.onUsername((v) -> v.setText("foo"));
+        mActivity.onPassword((v) -> v.setText("foo"));
+        mActivity.tapLogin();
+
+        // Start watching for the negative intent
+        final CountDownLatch latch = new CountDownLatch(1);
+        final IntentFilter intentFilter = new IntentFilter(intentAction);
+        getContext().registerReceiver(new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                getContext().unregisterReceiver(this);
+                latch.countDown();
+            }
+        }, intentFilter);
+
+        // Trigger the negative button.
+        sUiBot.saveForAutofill(SaveInfo.NEGATIVE_BUTTON_STYLE_CANCEL,
+                false, SAVE_DATA_TYPE_PASSWORD);
+
+        // Wait for the custom action.
+        assertThat(latch.await(500, TimeUnit.SECONDS)).isTrue();
+
+        assertNoDanglingSessions();
+    }
+
+    @Test
     public void testGetTextInputType() throws Exception {
         // Set service.
         enableService();
diff --git a/tests/autofillservice/src/android/autofillservice/cts/UiBot.java b/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
index 3b8c12a..fa7780a 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
@@ -31,6 +31,7 @@
 import android.app.UiAutomation;
 import android.content.res.Resources;
 import android.os.SystemClock;
+import android.service.autofill.SaveInfo;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.uiautomator.By;
 import android.support.test.uiautomator.BySelector;
@@ -249,6 +250,10 @@
     }
 
     UiObject2 assertSaveShowing(String description, int... types) {
+        return assertSaveShowing(SaveInfo.NEGATIVE_BUTTON_STYLE_CANCEL, description, types);
+    }
+
+    UiObject2 assertSaveShowing(int negativeButtonStyle, String description, int... types) {
         final UiObject2 snackbar = waitForObject(By.res("android", RESOURCE_ID_SAVE_SNACKBAR),
                 SAVE_TIMEOUT_MS);
 
@@ -288,6 +293,12 @@
             assertWithMessage("save subtitle(%s)", description).that(saveSubTitle).isNotNull();
         }
 
+        final String negativeButtonText = (negativeButtonStyle
+                == SaveInfo.NEGATIVE_BUTTON_STYLE_REJECT) ? "NOT NOW" : "NO THANKS";
+        UiObject2 negativeButton = snackbar.findObject(By.text(negativeButtonText));
+        assertWithMessage("negative button (%s)", negativeButtonText)
+                .that(negativeButton).isNotNull();
+
         final String expectedAccessibilityTitle =
                 getString(RESOURCE_STRING_SAVE_SNACKBAR_ACCESSIBILITY_TITLE);
         assertAccessibilityTitle(snackbar, expectedAccessibilityTitle);
@@ -302,7 +313,19 @@
      * @param types expected types of save info.
      */
     void saveForAutofill(boolean yesDoIt, int... types) {
-        final UiObject2 saveSnackBar = assertSaveShowing(null, types);
+        final UiObject2 saveSnackBar = assertSaveShowing(
+                SaveInfo.NEGATIVE_BUTTON_STYLE_CANCEL,null, types);
+        saveForAutofill(saveSnackBar, yesDoIt);
+    }
+
+    /**
+     * Taps an option in the save snackbar.
+     *
+     * @param yesDoIt {@code true} for 'YES', {@code false} for 'NO THANKS'.
+     * @param types expected types of save info.
+     */
+    void saveForAutofill(int negativeButtonStyle, boolean yesDoIt, int... types) {
+        final UiObject2 saveSnackBar = assertSaveShowing(negativeButtonStyle,null, types);
         saveForAutofill(saveSnackBar, yesDoIt);
     }