11package org.kabiri.android.usbterminal
22
33import androidx.test.espresso.Espresso.onView
4+ import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
5+ import androidx.test.espresso.NoMatchingViewException
46import androidx.test.espresso.assertion.ViewAssertions.matches
57import androidx.test.espresso.action.ViewActions.click
68import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
79import androidx.test.espresso.matcher.ViewMatchers.withId
810import androidx.test.ext.junit.rules.activityScenarioRule
911import androidx.test.ext.junit.runners.AndroidJUnit4
12+ import androidx.test.platform.app.InstrumentationRegistry
1013import org.junit.Rule
1114import org.junit.Test
1215import org.junit.runner.RunWith
@@ -18,12 +21,29 @@ internal class MainActivityAndroidTest {
1821 @get:Rule
1922 var rule = activityScenarioRule<MainActivity >()
2023
24+ private fun ensureMenuIsAccessible (menuItemId : Int ) {
25+ try {
26+ // Try to find the menu item first
27+ onView(withId(menuItemId)).check(matches(isDisplayed()))
28+ } catch (e: NoMatchingViewException ) {
29+ // If not found then open the overflow menu
30+ openActionBarOverflowOrOptionsMenu(
31+ InstrumentationRegistry .getInstrumentation().targetContext
32+ )
33+ }
34+ }
35+
2136 @Test
2237 fun checkUiViewsAreDisplayed () {
2338 onView(withId(R .id.tvOutput)).check(matches(isDisplayed()))
2439 onView(withId(R .id.btEnter)).check(matches(isDisplayed()))
2540 onView(withId(R .id.etInput)).check(matches(isDisplayed()))
2641
42+ // Ensure action items are accessible, either in the toolbar or via overflow
43+ ensureMenuIsAccessible(R .id.actionSettings)
44+ ensureMenuIsAccessible(R .id.actionConnect)
45+ ensureMenuIsAccessible(R .id.actionDisconnect)
46+
2747 // Check menu items are displayed
2848 onView(withId(R .id.actionSettings)).check(matches(isDisplayed()))
2949 onView(withId(R .id.actionConnect)).check(matches(isDisplayed()))
@@ -32,6 +52,8 @@ internal class MainActivityAndroidTest {
3252
3353 @Test
3454 fun clickingSettingsOpensSettingsBottomSheet () {
55+ // Ensure the action item is accisble, either in the toolbar or via overflow
56+ ensureMenuIsAccessible(R .id.actionSettings)
3557 // Click the Settings menu item
3658 onView(withId(R .id.actionSettings)).perform(click())
3759 // Assert the bottom sheet content is displayed
0 commit comments