Skip to content

Commit 8ba4383

Browse files
leticiarossildjcmu
authored andcommitted
Adding tests for the Exposed Dropdown Menu.
PiperOrigin-RevId: 246509842
1 parent f1255d9 commit 8ba4383

File tree

7 files changed

+327
-0
lines changed

7 files changed

+327
-0
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
@@ -72,6 +72,11 @@
7272
android:name=".ExpandableTransformationActivity"
7373
android:theme="@style/Theme.AppCompat.NoActionBar"/>
7474

75+
<activity
76+
android:name=".ExposedDropdownMenuActivity"
77+
android:theme="@style/Theme.MaterialComponents.NoActionBar"
78+
android:exported="false"/>
79+
7580
<activity
7681
android:name=".NavigationViewActivity"
7782
android:theme="@style/Theme.AppCompat.NoActionBar"/>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 android.os.Bundle;
19+
import com.google.android.material.testapp.base.BaseTestActivity;
20+
import android.widget.ArrayAdapter;
21+
import android.widget.AutoCompleteTextView;
22+
23+
/** Test activity that has the different variations of the Exposed Dropdown Menu. */
24+
public class ExposedDropdownMenuActivity extends BaseTestActivity {
25+
@Override
26+
protected int getContentViewLayoutResId() {
27+
return R.layout.exposed_dropdown_menu;
28+
}
29+
30+
@Override
31+
protected void onCreate(Bundle savedInstanceState) {
32+
super.onCreate(savedInstanceState);
33+
34+
ArrayAdapter<String> adapter =
35+
new ArrayAdapter<>(
36+
this,
37+
R.layout.exposed_dropdown_popup_item,
38+
getResources().getStringArray(R.array.exposed_dropdown_content));
39+
40+
AutoCompleteTextView editTextFilledExposedDropdown = findViewById(R.id.edittext_filled);
41+
editTextFilledExposedDropdown.setAdapter(adapter);
42+
43+
AutoCompleteTextView editTextFilledEditableExposedDropdown =
44+
findViewById(R.id.edittext_filled_editable);
45+
editTextFilledEditableExposedDropdown.setAdapter(adapter);
46+
}
47+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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/filled_dropdown"
26+
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:hint="@string/textinput_hint"
30+
app:errorEnabled="true">
31+
32+
<AutoCompleteTextView
33+
android:id="@+id/edittext_filled"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:editable="false"
37+
android:imeOptions="flagNoExtractUi"/>
38+
39+
</com.google.android.material.textfield.TextInputLayout>
40+
41+
<com.google.android.material.textfield.TextInputLayout
42+
android:id="@+id/filled_editable_dropdown"
43+
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:hint="@string/textinput_hint"
47+
app:errorEnabled="true">
48+
49+
<AutoCompleteTextView
50+
android:id="@+id/edittext_filled_editable"
51+
android:layout_width="match_parent"
52+
android:layout_height="wrap_content"
53+
android:imeOptions="flagNoExtractUi"/>
54+
55+
</com.google.android.material.textfield.TextInputLayout>
56+
57+
</LinearLayout>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 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+
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:id="@+id/dropdown_item"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:padding="16dp"
22+
android:ellipsize="end"
23+
android:singleLine="true"
24+
android:textAppearance="?attr/textAppearanceSubtitle1"/>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@
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>
5151
<string name="textinput_custom_end_icon">Custom end icon</string>
52+
53+
<string-array name="exposed_dropdown_content">
54+
<item>Item 1</item>
55+
<item>Item 2</item>
56+
<item>Item 3</item>
57+
<item>Item 4</item>
58+
</string-array>
5259
</resources>

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.text.method.PasswordTransformationMethod;
3232
import android.view.View;
3333
import android.view.View.OnClickListener;
34+
import android.widget.AutoCompleteTextView;
3435
import android.widget.EditText;
3536
import androidx.test.espresso.UiController;
3637
import androidx.test.espresso.ViewAction;
@@ -540,4 +541,50 @@ public void perform(UiController uiController, View view) {
540541
}
541542
};
542543
}
544+
545+
/** Skips any animations on the layout. */
546+
public static ViewAction skipAnimations() {
547+
return new ViewAction() {
548+
549+
@Override
550+
public Matcher<View> getConstraints() {
551+
return ViewMatchers.isAssignableFrom(TextInputLayout.class);
552+
}
553+
554+
@Override
555+
public String getDescription() {
556+
return "Skips any animations.";
557+
}
558+
559+
@Override
560+
public void perform(UiController uiController, View view) {
561+
view.jumpDrawablesToCurrentState();
562+
}
563+
};
564+
}
565+
566+
/** Shows the dropdown popup. */
567+
public static ViewAction showDropdown() {
568+
return new ViewAction() {
569+
570+
@Override
571+
public Matcher<View> getConstraints() {
572+
return ViewMatchers.isAssignableFrom(TextInputLayout.class);
573+
}
574+
575+
@Override
576+
public String getDescription() {
577+
return "Shows the dropdown popup.";
578+
}
579+
580+
@Override
581+
public void perform(UiController uiController, View view) {
582+
TextInputLayout item = (TextInputLayout) view;
583+
((AutoCompleteTextView) item.getEditText()).showDropDown();
584+
view.jumpDrawablesToCurrentState();
585+
CheckableImageButton iconView = item.findViewById(R.id.text_input_end_icon);
586+
iconView.setChecked(true);
587+
}
588+
};
589+
}
543590
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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.textfield;
17+
18+
import static com.google.android.material.testutils.TestUtilsActions.waitFor;
19+
import static com.google.android.material.testutils.TextInputLayoutActions.clickIcon;
20+
import static com.google.android.material.testutils.TextInputLayoutActions.showDropdown;
21+
import static com.google.android.material.testutils.TextInputLayoutActions.skipAnimations;
22+
import static com.google.android.material.testutils.TextInputLayoutMatchers.endIconHasContentDescription;
23+
import static androidx.test.espresso.Espresso.onView;
24+
import static androidx.test.espresso.action.ViewActions.clearText;
25+
import static androidx.test.espresso.action.ViewActions.click;
26+
import static androidx.test.espresso.action.ViewActions.typeText;
27+
import static androidx.test.espresso.action.ViewActions.typeTextIntoFocusedView;
28+
import static androidx.test.espresso.assertion.ViewAssertions.matches;
29+
import static androidx.test.espresso.contrib.AccessibilityChecks.accessibilityAssertion;
30+
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
31+
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
32+
import static androidx.test.espresso.matcher.ViewMatchers.withId;
33+
import static org.hamcrest.MatcherAssert.assertThat;
34+
import static org.hamcrest.Matchers.allOf;
35+
import static org.hamcrest.core.Is.is;
36+
37+
import android.app.Activity;
38+
import com.google.android.material.testapp.ExposedDropdownMenuActivity;
39+
import com.google.android.material.testapp.R;
40+
import android.widget.AutoCompleteTextView;
41+
import androidx.test.filters.MediumTest;
42+
import androidx.test.rule.ActivityTestRule;
43+
import androidx.test.runner.AndroidJUnit4;
44+
import org.junit.Rule;
45+
import org.junit.Test;
46+
import org.junit.runner.RunWith;
47+
48+
@MediumTest
49+
@RunWith(AndroidJUnit4.class)
50+
public class ExposedDropdownMenuTest {
51+
@Rule
52+
public final ActivityTestRule<ExposedDropdownMenuActivity> activityTestRule =
53+
new ActivityTestRule<>(ExposedDropdownMenuActivity.class);
54+
55+
private static final String INPUT_TEXT = "I";
56+
57+
@Test
58+
public void testEndIconClickShowsDropdownPopup() {
59+
final Activity activity = activityTestRule.getActivity();
60+
final AutoCompleteTextView editText = activity.findViewById(R.id.edittext_filled);
61+
62+
onView(withId(R.id.filled_dropdown)).perform(clickIcon(true));
63+
onView(withId(R.id.filled_dropdown)).perform(skipAnimations());
64+
65+
assertThat(editText.isPopupShowing(), is(true));
66+
}
67+
68+
@Test
69+
public void testEndIconClickHidesDropdownPopup() {
70+
final Activity activity = activityTestRule.getActivity();
71+
final AutoCompleteTextView editText = activity.findViewById(R.id.edittext_filled);
72+
onView(withId(R.id.filled_dropdown)).perform(showDropdown());
73+
74+
onView(withId(R.id.filled_dropdown)).perform(clickIcon(true));
75+
onView(withId(R.id.filled_dropdown)).perform(skipAnimations());
76+
77+
assertThat(editText.isPopupShowing(), is(false));
78+
}
79+
80+
@Test
81+
public void testLayoutClickShowsDropdownPopup() {
82+
final Activity activity = activityTestRule.getActivity();
83+
final AutoCompleteTextView editText = activity.findViewById(R.id.edittext_filled);
84+
85+
onView(withId(R.id.filled_dropdown)).perform(click());
86+
onView(withId(R.id.filled_dropdown)).perform(skipAnimations());
87+
88+
assertThat(editText.isPopupShowing(), is(true));
89+
}
90+
91+
@Test
92+
public void testLayoutClickHidesDropdownPopup() {
93+
final Activity activity = activityTestRule.getActivity();
94+
final AutoCompleteTextView editText = activity.findViewById(R.id.edittext_filled);
95+
onView(withId(R.id.filled_dropdown)).perform(showDropdown());
96+
97+
onView(withId(R.id.filled_dropdown)).perform(click());
98+
onView(withId(R.id.filled_dropdown)).perform(skipAnimations());
99+
100+
assertThat(editText.isPopupShowing(), is(false));
101+
}
102+
103+
@Test
104+
public void testTextInputShowsDropdownPopup() {
105+
final Activity activity = activityTestRule.getActivity();
106+
final AutoCompleteTextView editText = activity.findViewById(R.id.edittext_filled_editable);
107+
// Makes sure dropdown is not showing before entering input.
108+
onView(withId(R.id.filled_editable_dropdown)).perform(clickIcon(true));
109+
onView(withId(R.id.filled_editable_dropdown)).perform(clickIcon(true));
110+
onView(withId(R.id.filled_editable_dropdown)).perform(skipAnimations());
111+
112+
onView(isRoot()).perform(waitFor(2000));
113+
onView(withId(R.id.edittext_filled_editable)).perform(typeTextIntoFocusedView(INPUT_TEXT));
114+
onView(isRoot()).perform(waitFor(2000));
115+
116+
assertThat(editText.isPopupShowing(), is(true));
117+
}
118+
119+
@Test
120+
public void testClearingTextInputHidesDropdownPopup() {
121+
final Activity activity = activityTestRule.getActivity();
122+
final AutoCompleteTextView editText = activity.findViewById(R.id.edittext_filled_editable);
123+
onView(withId(R.id.edittext_filled_editable)).perform(typeText(INPUT_TEXT));
124+
125+
onView(withId(R.id.edittext_filled_editable)).perform(clearText());
126+
127+
assertThat(editText.isPopupShowing(), is(false));
128+
}
129+
130+
@Test
131+
public void testEndIconHasDefaultContentDescription() {
132+
onView(withId(R.id.filled_dropdown)).check(matches(endIconHasContentDescription()));
133+
}
134+
135+
@Test
136+
public void testEndIconIsAccessible() {
137+
onView(allOf(withId(R.id.text_input_end_icon), isDescendantOfA(withId(R.id.filled_dropdown))))
138+
.check(accessibilityAssertion());
139+
}
140+
}

0 commit comments

Comments
 (0)