1919import static androidx .test .espresso .accessibility .AccessibilityChecks .accessibilityAssertion ;
2020import static androidx .test .espresso .action .ViewActions .clearText ;
2121import static androidx .test .espresso .action .ViewActions .click ;
22+ import static androidx .test .espresso .action .ViewActions .pressKey ;
2223import static androidx .test .espresso .action .ViewActions .typeText ;
2324import static androidx .test .espresso .action .ViewActions .typeTextIntoFocusedView ;
2425import static androidx .test .espresso .assertion .ViewAssertions .matches ;
2526import static androidx .test .espresso .matcher .ViewMatchers .isDescendantOfA ;
2627import static androidx .test .espresso .matcher .ViewMatchers .isRoot ;
2728import static androidx .test .espresso .matcher .ViewMatchers .withContentDescription ;
2829import static androidx .test .espresso .matcher .ViewMatchers .withId ;
30+ import static com .google .android .material .testutils .EditTextActions .setSingleLine ;
31+ import static com .google .android .material .testutils .TestUtilsActions .requestFocus ;
2932import static com .google .android .material .testutils .TestUtilsActions .waitFor ;
3033import static com .google .android .material .testutils .TextInputLayoutActions .clickIcon ;
3134import static com .google .android .material .testutils .TextInputLayoutActions .setInputType ;
4548import android .graphics .drawable .Drawable ;
4649import android .graphics .drawable .LayerDrawable ;
4750import android .text .InputType ;
51+ import android .view .KeyEvent ;
4852import android .widget .AutoCompleteTextView ;
4953import androidx .test .filters .MediumTest ;
5054import androidx .test .rule .ActivityTestRule ;
@@ -65,6 +69,7 @@ public class ExposedDropdownMenuTest {
6569 public final ActivityTestRule <ExposedDropdownMenuActivity > activityTestRule =
6670 new ActivityTestRule <>(ExposedDropdownMenuActivity .class );
6771
72+
6873 @ Test
6974 public void testMenuIsNonEditableWithInputTypeNone () {
7075 final Activity activity = activityTestRule .getActivity ();
@@ -238,4 +243,150 @@ public void testEndIconIsAccessible() {
238243 isDescendantOfA (withId (R .id .filled_dropdown ))))
239244 .check (accessibilityAssertion ());
240245 }
246+
247+ @ Test
248+ public void testOnKeyDown_enterOnNonEditableField_showsDropDown () {
249+ final Activity activity = activityTestRule .getActivity ();
250+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled );
251+
252+ onView (withId (R .id .edittext_filled )).perform (requestFocus ());
253+ onView (withId (R .id .edittext_filled )).perform (pressKey (KeyEvent .KEYCODE_ENTER ));
254+ onView (withId (R .id .filled_dropdown )).perform (skipAnimations ());
255+
256+ assertThat (editText .isPopupShowing (), is (true ));
257+ }
258+
259+ @ Test
260+ public void testOnKeyDown_spaceOnNonEditableField_showsDropDown () {
261+ final Activity activity = activityTestRule .getActivity ();
262+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled );
263+
264+ onView (withId (R .id .edittext_filled )).perform (requestFocus ());
265+ onView (withId (R .id .edittext_filled )).perform (pressKey (KeyEvent .KEYCODE_SPACE ));
266+ onView (withId (R .id .filled_dropdown )).perform (skipAnimations ());
267+
268+ assertThat (editText .isPopupShowing (), is (true ));
269+ }
270+
271+ @ Test
272+ public void testOnKeyDown_enterOnNonEditableField_hidesDropDown () {
273+ final Activity activity = activityTestRule .getActivity ();
274+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled );
275+
276+ onView (withId (R .id .filled_dropdown )).perform (click ());
277+ onView (withId (R .id .filled_dropdown )).perform (skipAnimations ());
278+
279+ onView (withId (R .id .edittext_filled )).perform (requestFocus ());
280+ onView (withId (R .id .edittext_filled )).perform (pressKey (KeyEvent .KEYCODE_ENTER ));
281+ onView (withId (R .id .filled_dropdown )).perform (skipAnimations ());
282+
283+ assertThat (editText .isPopupShowing (), is (false ));
284+ }
285+
286+ @ Test
287+ public void testOnKeyDown_spaceOnNonEditableField_hidesDropDown () {
288+ final Activity activity = activityTestRule .getActivity ();
289+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled );
290+
291+ onView (withId (R .id .filled_dropdown )).perform (click ());
292+ onView (withId (R .id .filled_dropdown )).perform (skipAnimations ());
293+
294+ onView (withId (R .id .edittext_filled )).perform (requestFocus ());
295+ onView (withId (R .id .edittext_filled )).perform (pressKey (KeyEvent .KEYCODE_SPACE ));
296+ onView (withId (R .id .filled_dropdown )).perform (skipAnimations ());
297+
298+ assertThat (editText .isPopupShowing (), is (false ));
299+ }
300+
301+ @ Test
302+ public void testOnKeyDown_enterOnEditableMultiLineField_doesNotShowDropDown () {
303+ final Activity activity = activityTestRule .getActivity ();
304+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled_editable );
305+
306+ onView (withId (R .id .edittext_filled_editable )).perform (requestFocus ());
307+ onView (withId (R .id .edittext_filled_editable )).perform (pressKey (KeyEvent .KEYCODE_ENTER ));
308+ onView (withId (R .id .filled_editable_dropdown )).perform (skipAnimations ());
309+
310+ assertThat (editText .isPopupShowing (), is (false ));
311+ }
312+
313+ @ Test
314+ public void testOnKeyDown_enterOnEditableSingleLineField_showsDropDown () {
315+ final Activity activity = activityTestRule .getActivity ();
316+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled_editable );
317+
318+ onView (withId (R .id .edittext_filled_editable )).perform (setSingleLine (true ));
319+
320+ onView (withId (R .id .edittext_filled_editable )).perform (requestFocus ());
321+ onView (withId (R .id .edittext_filled_editable )).perform (pressKey (KeyEvent .KEYCODE_ENTER ));
322+ onView (withId (R .id .filled_editable_dropdown )).perform (skipAnimations ());
323+
324+ assertThat (editText .isPopupShowing (), is (true ));
325+ }
326+
327+ @ Test
328+ public void testOnKeyDown_enterOnEditableSingleLineField_hidesDropDown () {
329+ final Activity activity = activityTestRule .getActivity ();
330+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled_editable );
331+
332+ onView (withId (R .id .edittext_filled_editable )).perform (setSingleLine (true ));
333+
334+ onView (withId (R .id .filled_editable_dropdown )).perform (click ());
335+ onView (withId (R .id .filled_editable_dropdown )).perform (skipAnimations ());
336+
337+ onView (withId (R .id .edittext_filled_editable )).perform (requestFocus ());
338+ onView (withId (R .id .edittext_filled_editable )).perform (pressKey (KeyEvent .KEYCODE_ENTER ));
339+ onView (withId (R .id .filled_editable_dropdown )).perform (skipAnimations ());
340+
341+ assertThat (editText .isPopupShowing (), is (false ));
342+ }
343+
344+ @ Test
345+ public void testOnKeyDown_spaceOnEditableField_doesNotShowDropDown () {
346+ final Activity activity = activityTestRule .getActivity ();
347+ final AutoCompleteTextView editText = activity .findViewById (R .id .edittext_filled_editable );
348+
349+ onView (withId (R .id .edittext_filled_editable )).perform (requestFocus ());
350+ onView (withId (R .id .edittext_filled_editable )).perform (pressKey (KeyEvent .KEYCODE_SPACE ));
351+ onView (withId (R .id .filled_editable_dropdown )).perform (skipAnimations ());
352+
353+ assertThat (editText .isPopupShowing (), is (false ));
354+ }
355+
356+ @ Test
357+ public void shouldShowPopup_nonEditable_isTrueForEnterOrSpace () {
358+ final Activity activity = activityTestRule .getActivity ();
359+ final MaterialAutoCompleteTextView nonEditable = activity .findViewById (R .id .edittext_filled );
360+
361+ assertThat (nonEditable .shouldShowPopup (KeyEvent .KEYCODE_ENTER ), is (true ));
362+ assertThat (nonEditable .shouldShowPopup (KeyEvent .KEYCODE_DPAD_CENTER ), is (true ));
363+ assertThat (nonEditable .shouldShowPopup (KeyEvent .KEYCODE_SPACE ), is (true ));
364+ assertThat (nonEditable .shouldShowPopup (KeyEvent .KEYCODE_A ), is (false ));
365+ }
366+
367+ @ Test
368+ public void shouldShowPopup_editableMultiLine_isFalse () {
369+ final Activity activity = activityTestRule .getActivity ();
370+ final MaterialAutoCompleteTextView editable =
371+ activity .findViewById (R .id .edittext_filled_editable );
372+
373+ onView (withId (R .id .edittext_filled_editable )).perform (setSingleLine (false ));
374+
375+ assertThat (editable .shouldShowPopup (KeyEvent .KEYCODE_ENTER ), is (false ));
376+ assertThat (editable .shouldShowPopup (KeyEvent .KEYCODE_DPAD_CENTER ), is (false ));
377+ assertThat (editable .shouldShowPopup (KeyEvent .KEYCODE_SPACE ), is (false ));
378+ }
379+
380+ @ Test
381+ public void shouldShowPopup_editableSingleLine_isTrueForEnter () {
382+ final Activity activity = activityTestRule .getActivity ();
383+ final MaterialAutoCompleteTextView editable =
384+ activity .findViewById (R .id .edittext_filled_editable );
385+
386+ onView (withId (R .id .edittext_filled_editable )).perform (setSingleLine (true ));
387+
388+ assertThat (editable .shouldShowPopup (KeyEvent .KEYCODE_ENTER ), is (true ));
389+ assertThat (editable .shouldShowPopup (KeyEvent .KEYCODE_DPAD_CENTER ), is (true ));
390+ assertThat (editable .shouldShowPopup (KeyEvent .KEYCODE_SPACE ), is (false ));
391+ }
241392}
0 commit comments