Android - Espresso - long options menu - clicking on an option menu item that is not displayed

How can Espresso click on a menu item (option) that is not already shown in the long options menu?

Opening the options menu is easy:

openActionBarOverflowOrOptionsMenu( getInstrumentation().getTargetContext());

      

I tried for example. scrollTo, but it didn't work:

onView( withText("Option menu item text")).perform( scrollTo(), click());

onView( withText( R.id.optionMenuId)).perform( scrollTo(), click());

onView( withId( is( R.id.appOptionMenu))).perform( swipeDown()); // where SwipeDown is a simple utility method on GeneralSwipeAction.

onData( anything()).inAdapterView( withId(R.id.wpeOptionMenu)).atPosition( 12).perform(click()); // I guess because it is not an adapter

      

Do you have a good solution?

+3


source to share


1 answer


The Overflow Menu ActionBar is a PopUpWindow containing a ListView .

scrollTo () only works with descendants of the ScrollView , so won't work here.

Since the view you want is inside the adapter, you need to use onData.



The AdapterView data objects are of type MenuItem and you want to match the name of the menu item. Something like that:

onData(allOf(instanceOf(MenuItem.class), withTitle(title))).perform(click());

static MenuItemTitleMatcher withTitle(String title) {
    return new MenuItemTitleMatcher(title);
}

class MenuItemTitleMatcher extends BaseMatcher<Object> {
    private final String title;
    public MenuItemTitleMatcher(String title) { this.title = title; }

    @Override public boolean matches(Object o) {
        if (o instanceof MenuItem) {
            return ((MenuItem) o).getTitle().equals(title);
        }
        return false;
    }
    @Override public void describeTo(Description description) { }
}

      

+8


source







All Articles