|
| 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