I have the following menu item that I want to click using Espresso:
<item
android:id="@+id/action_save"
android:icon="@drawable/vector_image_save"
android:orderInCategory="4"
android:title="@string/menu_action_save"
app:showAsAction="ifRoom"/>
Due to ifRoom
, in some devices the menu is shown as an icon in action bar while in smaller devices it's shown under the "more options".
I could use the below code to tap on the Save icon in the action bar:
onView(withId(R.id.action_save)).perform(click());
And I could use the below code to tap on Save if it's present under the "more options":
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
onView(withText(R.string.menu_action_save)).perform(click());
I want a single test method that would work in both cases.