Skip to content

Commit 5236a03

Browse files
leticiarossiafohrman
authored andcommitted
Refactoring text field unit tests to test icons separately. Also adding tests for the custom end icon.
PiperOrigin-RevId: 235002414
1 parent 7b79678 commit 5236a03

File tree

8 files changed

+592
-328
lines changed

8 files changed

+592
-328
lines changed

testing/java/com/google/android/material/testapp/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@
100100
android:name=".TextInputLayoutActivity"
101101
android:theme="@style/Theme.MaterialComponents.NoActionBar"/>
102102

103+
<activity
104+
android:name=".TextInputLayoutWithIconsActivity"
105+
android:theme="@style/Theme.MaterialComponents.NoActionBar"
106+
android:exported="false"/>
107+
103108

104109
</application>
105110
</manifest>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2019 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.android.material.testapp;
17+
18+
import com.google.android.material.testapp.base.BaseTestActivity;
19+
20+
/** Test activity that has TextInputLayouts with different supported icons. */
21+
public class TextInputLayoutWithIconsActivity extends BaseTestActivity {
22+
@Override
23+
protected int getContentViewLayoutResId() {
24+
return R.layout.design_text_input_icons;
25+
}
26+
}

testing/java/com/google/android/material/testapp/res/layout/design_text_input.xml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,6 @@
3737

3838
</com.google.android.material.textfield.TextInputLayout>
3939

40-
<com.google.android.material.textfield.TextInputLayout
41-
android:id="@+id/textinput_password"
42-
android:layout_width="match_parent"
43-
android:layout_height="wrap_content"
44-
app:endIconMode="password_toggle">
45-
46-
<com.google.android.material.textfield.TextInputEditText
47-
android:id="@+id/textinput_edittext_pwd"
48-
android:layout_width="match_parent"
49-
android:layout_height="wrap_content"
50-
android:hint="@string/textinput_hint"
51-
android:imeOptions="flagNoExtractUi"
52-
android:inputType="textPassword"/>
53-
54-
</com.google.android.material.textfield.TextInputLayout>
55-
56-
<com.google.android.material.textfield.TextInputLayout
57-
android:id="@+id/textinput_clear"
58-
android:layout_width="match_parent"
59-
android:layout_height="wrap_content"
60-
app:endIconMode="clear_text">
61-
62-
<com.google.android.material.textfield.TextInputEditText
63-
android:id="@+id/textinput_edittext_clear"
64-
android:layout_width="match_parent"
65-
android:layout_height="wrap_content"
66-
android:hint="@string/textinput_hint"/>
67-
68-
</com.google.android.material.textfield.TextInputLayout>
69-
7040
<com.google.android.material.textfield.TextInputLayout
7141
android:id="@+id/textinput_noedittext"
7242
android:layout_width="match_parent"
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2019 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:orientation="vertical"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent">
23+
24+
<com.google.android.material.textfield.TextInputLayout
25+
android:id="@+id/textinput_no_icon"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
app:errorEnabled="true">
29+
30+
<com.google.android.material.textfield.TextInputEditText
31+
android:id="@+id/textinput_edittext_no_icon"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
android:hint="@string/textinput_hint"
35+
android:imeOptions="flagNoExtractUi"/>
36+
37+
</com.google.android.material.textfield.TextInputLayout>
38+
39+
<com.google.android.material.textfield.TextInputLayout
40+
android:id="@+id/textinput_password"
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content"
43+
app:endIconMode="password_toggle">
44+
45+
<com.google.android.material.textfield.TextInputEditText
46+
android:id="@+id/textinput_edittext_pwd"
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:hint="@string/textinput_hint"
50+
android:imeOptions="flagNoExtractUi"
51+
android:inputType="textPassword"/>
52+
53+
</com.google.android.material.textfield.TextInputLayout>
54+
55+
<com.google.android.material.textfield.TextInputLayout
56+
android:id="@+id/textinput_clear"
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
app:endIconMode="clear_text">
60+
61+
<com.google.android.material.textfield.TextInputEditText
62+
android:id="@+id/textinput_edittext_clear"
63+
android:layout_width="match_parent"
64+
android:layout_height="wrap_content"
65+
android:hint="@string/textinput_hint"/>
66+
67+
</com.google.android.material.textfield.TextInputLayout>
68+
69+
<com.google.android.material.textfield.TextInputLayout
70+
android:id="@+id/textinput_custom"
71+
android:layout_width="match_parent"
72+
android:layout_height="wrap_content"
73+
app:endIconMode="custom"
74+
app:endIconDrawable="@drawable/test_background_green"
75+
app:endIconContentDescription="@string/textinput_custom_end_icon">
76+
77+
<com.google.android.material.textfield.TextInputEditText
78+
android:id="@+id/textinput_edittext_custom"
79+
android:layout_width="match_parent"
80+
android:layout_height="wrap_content"
81+
android:hint="@string/textinput_hint"/>
82+
83+
</com.google.android.material.textfield.TextInputLayout>
84+
</LinearLayout>

testing/java/com/google/android/material/testapp/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@
4848
<string name="textinput_hint">Hint to the user</string>
4949
<string name="textinput_helper">Helper text to the user</string>
5050
<string name="textinput_helper_not_enabled">Helper text to the user, helper not enabled</string>
51-
51+
<string name="textinput_custom_end_icon">Custom end icon</string>
5252
</resources>

tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818

1919
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
2020

21+
import android.graphics.Color;
2122
import android.graphics.Typeface;
23+
import android.graphics.drawable.ColorDrawable;
2224
import androidx.annotation.ColorInt;
2325
import androidx.annotation.DimenRes;
2426
import com.google.android.material.internal.CheckableImageButton;
2527
import com.google.android.material.testapp.R;
2628
import com.google.android.material.textfield.TextInputLayout;
2729
import android.text.method.PasswordTransformationMethod;
2830
import android.view.View;
31+
import android.view.View.OnClickListener;
2932
import android.widget.EditText;
3033
import androidx.test.espresso.UiController;
3134
import androidx.test.espresso.ViewAction;
@@ -194,6 +197,26 @@ public void perform(UiController uiController, View view) {
194197
};
195198
}
196199

200+
public static ViewAction setEndIconOnClickListener(final OnClickListener onClickListener) {
201+
return new ViewAction() {
202+
@Override
203+
public Matcher<View> getConstraints() {
204+
return isAssignableFrom(TextInputLayout.class);
205+
}
206+
207+
@Override
208+
public String getDescription() {
209+
return "Set end icon OnClickListener";
210+
}
211+
212+
@Override
213+
public void perform(UiController uiController, View view) {
214+
TextInputLayout layout = (TextInputLayout) view;
215+
layout.setEndIconOnClickListener(onClickListener);
216+
}
217+
};
218+
}
219+
197220
public static ViewAction setEndIconMode(final int endIconMode) {
198221
return new ViewAction() {
199222
@Override
@@ -214,6 +237,27 @@ public void perform(UiController uiController, View view) {
214237
};
215238
}
216239

240+
public static ViewAction setCustomEndIconContent() {
241+
return new ViewAction() {
242+
@Override
243+
public Matcher<View> getConstraints() {
244+
return isAssignableFrom(TextInputLayout.class);
245+
}
246+
247+
@Override
248+
public String getDescription() {
249+
return "Set custom end icon content";
250+
}
251+
252+
@Override
253+
public void perform(UiController uiController, View view) {
254+
TextInputLayout layout = (TextInputLayout) view;
255+
layout.setEndIconDrawable(new ColorDrawable(Color.BLUE));
256+
layout.setEndIconContentDescription(R.string.textinput_custom_end_icon);
257+
}
258+
};
259+
}
260+
217261
public static ViewAction setCounterEnabled(final boolean enabled) {
218262
return new ViewAction() {
219263
@Override

0 commit comments

Comments
 (0)