Preserve left+right compound drawables

Caused by the logic to remove the password dummy
compound drawable ran all the time, even when it
didn't need to. Due to the way that TextView returns
relative  drawables when absolutely set, this meant that
custom set compounds were wiped out.

Fixed making that logic only run when it needs to.
This still means that absolute set compounds are wiped
out when the password toggle is actually used. Added
documentation to explain.

Test: included
BUG: 32326780

Change-Id: I80e7c8f504c07bca437038167f6240dddf4bfe7f
diff --git a/design/src/android/support/design/widget/TextInputLayout.java b/design/src/android/support/design/widget/TextInputLayout.java
index 3355956..e645a34 100644
--- a/design/src/android/support/design/widget/TextInputLayout.java
+++ b/design/src/android/support/design/widget/TextInputLayout.java
@@ -77,6 +77,16 @@
  * {@link #setError(CharSequence)}, and a character counter via
  * {@link #setCounterEnabled(boolean)}.</p>
  *
+ * <p>Password visibility toggling is also supported via the
+ * {@link #setPasswordVisibilityToggleEnabled(boolean)} API and related attribute.
+ * If enabled, a button is displayed to toggle between the password being displayed as plain-text
+ * or disguised, when your EditText is set to display a password.</p>
+ *
+ * <p><strong>Note:</strong> When using the password toggle functionality, the 'end' compound
+ * drawable of the EditText will be overridden while the toggle is enabled. To ensure that any
+ * existing drawables are restored correctly, you should set those compound drawables relatively
+ * (start/end), opposed to absolutely (left/right).</p>
+ *
  * The {@link TextInputEditText} class is provided to be used as a child of this layout. Using
  * TextInputEditText allows TextInputLayout greater control over the visual aspects of any
  * text input. An example usage is as so:
@@ -219,7 +229,7 @@
                 R.styleable.TextInputLayout_counterOverflowTextAppearance, 0);
 
         mPasswordToggleEnabled = a.getBoolean(
-                R.styleable.TextInputLayout_passwordToggleEnabled, true);
+                R.styleable.TextInputLayout_passwordToggleEnabled, false);
         mPasswordToggleDrawable = a.getDrawable(R.styleable.TextInputLayout_passwordToggleDrawable);
         mPasswordToggleContentDesc = a.getText(
                 R.styleable.TextInputLayout_passwordToggleContentDescription);
@@ -1041,11 +1051,15 @@
                 mPasswordToggleView.setVisibility(View.GONE);
             }
 
-            // Make sure that we remove the dummy end compound drawable
-            final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
-            if (compounds[2] == mPasswordToggleDummyDrawable) {
-                TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1],
-                        mOriginalEditTextEndDrawable, compounds[3]);
+            if (mPasswordToggleDummyDrawable != null) {
+                // Make sure that we remove the dummy end compound drawable if it exists, and then
+                // clear it
+                final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
+                if (compounds[2] == mPasswordToggleDummyDrawable) {
+                    TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0],
+                            compounds[1], mOriginalEditTextEndDrawable, compounds[3]);
+                    mPasswordToggleDummyDrawable = null;
+                }
             }
         }
     }
diff --git a/design/tests/src/android/support/design/widget/TextInputLayoutTest.java b/design/tests/src/android/support/design/widget/TextInputLayoutTest.java
index 66ea00b..e5959eb 100755
--- a/design/tests/src/android/support/design/widget/TextInputLayoutTest.java
+++ b/design/tests/src/android/support/design/widget/TextInputLayoutTest.java
@@ -36,6 +36,7 @@
 import static org.hamcrest.Matchers.not;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
 import android.app.Activity;
@@ -46,6 +47,7 @@
 import android.support.test.annotation.UiThreadTest;
 import android.support.test.espresso.NoMatchingViewException;
 import android.support.test.espresso.ViewAssertion;
+import android.support.v4.widget.TextViewCompat;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.view.View;
 import android.widget.EditText;
@@ -230,6 +232,68 @@
         layout.drawableStateChanged();
     }
 
+    @Test
+    public void testMaintainsLeftRightCompoundDrawables() throws Throwable {
+        final Activity activity = mActivityTestRule.getActivity();
+
+        // Set a known set of test compound drawables on the EditText
+        final Drawable left = new ColorDrawable(Color.RED);
+        final Drawable top = new ColorDrawable(Color.GREEN);
+        final Drawable right = new ColorDrawable(Color.BLUE);
+        final Drawable bottom = new ColorDrawable(Color.BLACK);
+
+        final TextInputEditText editText = new TextInputEditText(activity);
+        editText.setCompoundDrawables(left, top, right, bottom);
+
+        // Now add the EditText to a TextInputLayout
+        mActivityTestRule.runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                TextInputLayout til = (TextInputLayout)
+                        activity.findViewById(R.id.textinput_noedittext);
+                til.addView(editText);
+            }
+        });
+
+        // Finally assert that all of the drawables are untouched
+        final Drawable[] compoundDrawables = editText.getCompoundDrawables();
+        assertSame(left, compoundDrawables[0]);
+        assertSame(top, compoundDrawables[1]);
+        assertSame(right, compoundDrawables[2]);
+        assertSame(bottom, compoundDrawables[3]);
+    }
+
+    @Test
+    public void testMaintainsStartEndCompoundDrawables() throws Throwable {
+        final Activity activity = mActivityTestRule.getActivity();
+
+        // Set a known set of test compound drawables on the EditText
+        final Drawable start = new ColorDrawable(Color.RED);
+        final Drawable top = new ColorDrawable(Color.GREEN);
+        final Drawable end = new ColorDrawable(Color.BLUE);
+        final Drawable bottom = new ColorDrawable(Color.BLACK);
+
+        final TextInputEditText editText = new TextInputEditText(activity);
+        TextViewCompat.setCompoundDrawablesRelative(editText, start, top, end, bottom);
+
+        // Now add the EditText to a TextInputLayout
+        mActivityTestRule.runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                TextInputLayout til = (TextInputLayout)
+                        activity.findViewById(R.id.textinput_noedittext);
+                til.addView(editText);
+            }
+        });
+
+        // Finally assert that all of the drawables are untouched
+        final Drawable[] compoundDrawables = TextViewCompat.getCompoundDrawablesRelative(editText);
+        assertSame(start, compoundDrawables[0]);
+        assertSame(top, compoundDrawables[1]);
+        assertSame(end, compoundDrawables[2]);
+        assertSame(bottom, compoundDrawables[3]);
+    }
+
     static ViewAssertion isHintExpanded(final boolean expanded) {
         return new ViewAssertion() {
             @Override