tm1701
tm1701

Reputation: 7591

Android - Espresso - long options menu - clicking on a option-menu-item that is not visible

How can Espresso click on a (option)menu-item that is not yet visible in a long options menu?

Opening the options menu is easy:

openActionBarOverflowOrOptionsMenu( getInstrumentation().getTargetContext());

I tried e.g. scrollTo, but it didn't work out:

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?

Upvotes: 3

Views: 1921

Answers (1)

yogurtearl
yogurtearl

Reputation: 3065

The ActionBar overflow menu is a PopUpWindow containing a ListView.

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

Because the view you want is inside an AdapterView, you will need to use onData.

The data objects is the AdapterView are of type MenuItem and you want to match the title of the Menu item. Something like this:

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) { }
}

Upvotes: 8

Related Questions