Add 'clear' button to pin/password lock screen

bug: 62904466
Test: Manually tested, added functional tests
Change-Id: I5f5555d99b47550a8fef6995222d12155b4072a9
diff --git a/res/layout/choose_lock_password_footer.xml b/res/layout/choose_lock_password_footer.xml
index 323a884..41306e9 100644
--- a/res/layout/choose_lock_password_footer.xml
+++ b/res/layout/choose_lock_password_footer.xml
@@ -28,6 +28,15 @@
         android:layout_height="wrap_content"
         android:text="@string/lockpassword_cancel_label" />
 
+
+    <!-- left : clear -->
+    <Button android:id="@+id/clear_button"
+        style="@style/SuwGlifButton.Secondary"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:visibility="gone"
+        android:text="@string/lockpassword_clear_label" />
+
     <Space
         android:layout_width="0dp"
         android:layout_height="0dp"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 31a1872..ef7c00c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1375,6 +1375,9 @@
     <!-- Label for ChoosePassword/PIN OK button -->
     <string name="lockpassword_cancel_label">Cancel</string>
 
+    <!-- Label for ChoosePassword/PIN OK button -->
+    <string name="lockpassword_clear_label">Clear</string>
+
     <!-- Label for LockPatternTutorial Cancel button -->
     <string name="lockpattern_tutorial_cancel_label">Cancel</string>
 
diff --git a/src/com/android/settings/password/ChooseLockPassword.java b/src/com/android/settings/password/ChooseLockPassword.java
index 84e150a..e013b8b 100644
--- a/src/com/android/settings/password/ChooseLockPassword.java
+++ b/src/com/android/settings/password/ChooseLockPassword.java
@@ -211,6 +211,7 @@
         private RecyclerView mPasswordRestrictionView;
         protected boolean mIsAlphaMode;
         protected Button mCancelButton;
+        private Button mClearButton;
         private Button mNextButton;
 
         private TextChangedHandler mTextChangedHandler;
@@ -347,6 +348,8 @@
             mCancelButton.setOnClickListener(this);
             mNextButton = (Button) view.findViewById(R.id.next_button);
             mNextButton.setOnClickListener(this);
+            mClearButton = view.findViewById(R.id.clear_button);
+            mClearButton.setOnClickListener(this);
 
             if (mForFingerprint) {
                 TextView fingerprintBackupMessage =
@@ -735,6 +738,10 @@
                 case R.id.cancel_button:
                     getActivity().finish();
                     break;
+
+                case R.id.clear_button:
+                    mPasswordEntry.setText("");
+                    break;
             }
         }
 
@@ -839,11 +846,20 @@
                 mPasswordRestrictionView.setVisibility(View.GONE);
                 setHeaderText(getString(mUiStage.getHint(mIsAlphaMode, mForFingerprint)));
                 setNextEnabled(canInput && length > 0);
+                mClearButton.setEnabled(canInput && length > 0);
             }
+
+            mClearButton.setVisibility(toVisibility(mUiStage != Stage.Introduction));
+            mCancelButton.setVisibility(toVisibility(mUiStage == Stage.Introduction));
+
             setNextText(mUiStage.buttonText);
             mPasswordEntryInputDisabler.setInputEnabled(canInput);
         }
 
+        private int toVisibility(boolean visibleOrGone) {
+            return visibleOrGone ? View.VISIBLE : View.GONE;
+        }
+
         private void setHeaderText(String text) {
             // Only set the text if it is different than the existing one to avoid announcing again.
             if (!TextUtils.isEmpty(mLayout.getHeaderText())
diff --git a/src/com/android/settings/password/SetupChooseLockPassword.java b/src/com/android/settings/password/SetupChooseLockPassword.java
index 2988f52..79cfef3 100644
--- a/src/com/android/settings/password/SetupChooseLockPassword.java
+++ b/src/com/android/settings/password/SetupChooseLockPassword.java
@@ -84,13 +84,6 @@
         @Override
         public void onViewCreated(View view, Bundle savedInstanceState) {
             super.onViewCreated(view, savedInstanceState);
-
-            if (mForFingerprint) {
-                mCancelButton.setVisibility(View.GONE);
-            } else {
-                mCancelButton.setText(R.string.skip_label);
-            }
-
             final Activity activity = getActivity();
             ChooseLockGenericController chooseLockGenericController =
                     new ChooseLockGenericController(activity, mUserId);
@@ -190,6 +183,12 @@
         @Override
         protected void updateUi() {
             super.updateUi();
+            if (mForFingerprint) {
+                mCancelButton.setVisibility(View.GONE);
+            } else {
+                mCancelButton.setText(R.string.skip_label);
+            }
+
             if (mOptionsButton != null) {
                 mOptionsButton.setVisibility(
                         mUiStage == Stage.Introduction ? View.VISIBLE : View.GONE);
diff --git a/tests/app/src/com/android/settings/password/ChooseLockPasswordTest.java b/tests/app/src/com/android/settings/password/ChooseLockPasswordTest.java
new file mode 100644
index 0000000..36611cf
--- /dev/null
+++ b/tests/app/src/com/android/settings/password/ChooseLockPasswordTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.password;
+
+import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.action.ViewActions.pressKey;
+import static android.support.test.espresso.assertion.ViewAssertions.matches;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
+import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
+import static android.support.test.espresso.matcher.ViewMatchers.withId;
+
+import static org.hamcrest.CoreMatchers.not;
+
+import android.app.Instrumentation;
+import android.content.Context;
+import android.content.Intent;
+import android.os.SystemClock;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.espresso.action.ViewActions;
+import android.support.test.espresso.matcher.ViewMatchers;
+import android.support.test.runner.AndroidJUnit4;
+import android.view.KeyEvent;
+
+import com.android.settings.R;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class ChooseLockPasswordTest {
+    private Instrumentation mInstrumentation;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        mInstrumentation = InstrumentationRegistry.getInstrumentation();
+        mContext = mInstrumentation.getTargetContext();
+    }
+
+    @Test
+    public void clearNotVisible_when_activityLaunchedInitially() {
+        mInstrumentation.startActivitySync(new Intent(mContext, ChooseLockPassword.class));
+        onView(withId(R.id.clear_button)).check(matches(
+                withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
+    }
+
+    @Test
+    public void clearNotEnabled_when_nothingEntered() {
+        mInstrumentation.startActivitySync(new Intent(mContext, ChooseLockPassword.class));
+        onView(withId(R.id.password_entry)).perform(ViewActions.typeText("1234"))
+                .perform(pressKey(KeyEvent.KEYCODE_ENTER));
+        onView(withId(R.id.clear_button)).check(matches(isDisplayed()))
+            .check(matches(not(isEnabled())));
+    }
+
+    @Test
+    public void clearEnabled_when_somethingEnteredToConfirm() {
+        mInstrumentation.startActivitySync(new Intent(mContext, ChooseLockPassword.class));
+        onView(withId(R.id.password_entry)).perform(ViewActions.typeText("1234"))
+                .perform(pressKey(KeyEvent.KEYCODE_ENTER))
+                .perform(ViewActions.typeText("1"));
+        // clear should be present if text field contains content
+        onView(withId(R.id.clear_button)).check(matches(isDisplayed()));
+    }
+}
diff --git a/tests/app/src/com/android/settings/password/SetupChooseLockPasswordAppTest.java b/tests/app/src/com/android/settings/password/SetupChooseLockPasswordAppTest.java
index 78acc3e..3a6c39c 100644
--- a/tests/app/src/com/android/settings/password/SetupChooseLockPasswordAppTest.java
+++ b/tests/app/src/com/android/settings/password/SetupChooseLockPasswordAppTest.java
@@ -18,16 +18,24 @@
 
 import static android.support.test.espresso.Espresso.onView;
 import static android.support.test.espresso.action.ViewActions.click;
+import static android.support.test.espresso.action.ViewActions.pressKey;
 import static android.support.test.espresso.assertion.ViewAssertions.matches;
 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
+import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
 import static android.support.test.espresso.matcher.ViewMatchers.withId;
 import static android.support.test.espresso.matcher.ViewMatchers.withText;
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.hamcrest.CoreMatchers.not;
+
+import android.support.test.espresso.action.ViewActions;
+import android.support.test.espresso.matcher.ViewMatchers;
 import android.support.test.filters.MediumTest;
 import android.support.test.rule.ActivityTestRule;
 import android.support.test.runner.AndroidJUnit4;
+import android.view.KeyEvent;
 
 import com.android.settings.R;
 
@@ -58,4 +66,30 @@
 
         assertThat(activity.isFinishing()).named("Is finishing").isTrue();
     }
+
+    @Test
+    public void clearNotVisible_when_activityLaunchedInitially() {
+        mActivityTestRule.launchActivity(null);
+        onView(withId(R.id.clear_button)).check(matches(
+                withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
+    }
+
+    @Test
+    public void clearNotEnabled_when_nothingEntered() throws Throwable {
+        mActivityTestRule.launchActivity(null);
+        onView(withId(R.id.password_entry)).perform(ViewActions.typeText("1234"))
+                .perform(pressKey(KeyEvent.KEYCODE_ENTER));
+        onView(withId(R.id.clear_button)).check(matches(isDisplayed()))
+                .check(matches(not(isEnabled())));
+    }
+
+    @Test
+    public void clearEnabled_when_somethingEnteredToConfirm() {
+        mActivityTestRule.launchActivity(null);
+        onView(withId(R.id.password_entry)).perform(ViewActions.typeText("1234"))
+                .perform(pressKey(KeyEvent.KEYCODE_ENTER))
+                .perform(ViewActions.typeText("1"));
+        // clear should be present if text field contains content
+        onView(withId(R.id.clear_button)).check(matches(isDisplayed()));
+    }
 }