|
45 | 45 | import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconOnClickListener; |
46 | 46 | import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconOnLongClickListener; |
47 | 47 | import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconTintList; |
| 48 | +import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconTintMode; |
48 | 49 | import static com.google.android.material.testutils.TextInputLayoutActions.setSuffixText; |
49 | 50 | import static com.google.android.material.testutils.TextInputLayoutActions.setTransformationMethod; |
50 | 51 | import static com.google.android.material.testutils.TextInputLayoutMatchers.doesNotShowEndIcon; |
|
53 | 54 | import static com.google.android.material.testutils.TextInputLayoutMatchers.endIconIsChecked; |
54 | 55 | import static com.google.android.material.testutils.TextInputLayoutMatchers.endIconIsNotChecked; |
55 | 56 | import static com.google.android.material.testutils.TextInputLayoutMatchers.showsEndIcon; |
| 57 | +import static com.google.common.truth.Truth.assertThat; |
56 | 58 | import static org.hamcrest.Matchers.allOf; |
57 | 59 | import static org.hamcrest.Matchers.not; |
58 | 60 | import static org.junit.Assert.assertEquals; |
|
62 | 64 | import static org.junit.Assert.assertTrue; |
63 | 65 |
|
64 | 66 | import android.app.Activity; |
| 67 | +import android.content.res.ColorStateList; |
65 | 68 | import android.graphics.Color; |
| 69 | +import android.graphics.PorterDuff; |
66 | 70 | import android.graphics.drawable.ColorDrawable; |
67 | 71 | import android.graphics.drawable.Drawable; |
| 72 | +import androidx.core.graphics.drawable.TintAwareDrawable; |
68 | 73 | import android.text.method.PasswordTransformationMethod; |
69 | 74 | import android.text.method.TransformationMethod; |
70 | 75 | import android.view.KeyEvent; |
71 | 76 | import android.widget.EditText; |
| 77 | +import androidx.annotation.Nullable; |
72 | 78 | import androidx.test.espresso.ViewAssertion; |
73 | 79 | import androidx.test.espresso.matcher.ViewMatchers.Visibility; |
74 | 80 | import androidx.test.filters.LargeTest; |
@@ -525,6 +531,30 @@ public void testSetStartIconProgrammatically() { |
525 | 531 | assertEquals(contentDesc, textInputLayout.getStartIconContentDescription().toString()); |
526 | 532 | } |
527 | 533 |
|
| 534 | + @Test |
| 535 | + public void testSetStartIconTint() { |
| 536 | + final Activity activity = activityTestRule.getActivity(); |
| 537 | + final TextInputLayout textInputLayout = activity.findViewById(R.id.textinput_no_icon); |
| 538 | + Drawable drawable = new TintCapturedDrawable(); |
| 539 | + |
| 540 | + // Set start icon |
| 541 | + onView(withId(R.id.textinput_no_icon)).perform( |
| 542 | + setStartIconTintList(ColorStateList.valueOf(Color.RED))); |
| 543 | + onView(withId(R.id.textinput_no_icon)).perform(setStartIconTintMode(PorterDuff.Mode.MULTIPLY)); |
| 544 | + onView(withId(R.id.textinput_no_icon)).perform(setStartIcon(drawable)); |
| 545 | + |
| 546 | + // Assert the start icon's tint is set |
| 547 | + assertNotNull(textInputLayout.getStartIconDrawable()); |
| 548 | + assertThat(textInputLayout.getStartIconDrawable()).isInstanceOf(TintCapturedDrawable.class); |
| 549 | + assertEquals( |
| 550 | + Color.RED, |
| 551 | + ((TintCapturedDrawable) textInputLayout.getStartIconDrawable()) |
| 552 | + .capturedTint.getDefaultColor()); |
| 553 | + assertEquals( |
| 554 | + PorterDuff.Mode.MULTIPLY, |
| 555 | + ((TintCapturedDrawable) textInputLayout.getStartIconDrawable()).capturedTintMode); |
| 556 | + } |
| 557 | + |
528 | 558 | @Test |
529 | 559 | public void testStartIconDisables() { |
530 | 560 | // Disable the start icon |
@@ -876,4 +906,23 @@ private static ViewAssertion isPasswordToggledVisible(final boolean isToggledVis |
876 | 906 | } |
877 | 907 | }; |
878 | 908 | } |
| 909 | + |
| 910 | + private static class TintCapturedDrawable extends ColorDrawable implements TintAwareDrawable { |
| 911 | + ColorStateList capturedTint; |
| 912 | + PorterDuff.Mode capturedTintMode; |
| 913 | + |
| 914 | + TintCapturedDrawable() { |
| 915 | + super(Color.WHITE); |
| 916 | + } |
| 917 | + |
| 918 | + @Override |
| 919 | + public void setTintList(@Nullable ColorStateList tint) { |
| 920 | + capturedTint = tint; |
| 921 | + } |
| 922 | + |
| 923 | + @Override |
| 924 | + public void setTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 925 | + capturedTintMode = tintMode; |
| 926 | + } |
| 927 | + } |
879 | 928 | } |
0 commit comments