Skip to content

Commit 895eeed

Browse files
committed
tests: make sure action items are accessible to allow testing on headless emulator e.g. on the CI
1 parent db1bf2a commit 895eeed

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

app/src/androidTest/java/org/kabiri/android/usbterminal/MainActivityAndroidTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.kabiri.android.usbterminal
22

33
import androidx.test.espresso.Espresso.onView
4+
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
5+
import androidx.test.espresso.NoMatchingViewException
46
import androidx.test.espresso.assertion.ViewAssertions.matches
57
import androidx.test.espresso.action.ViewActions.click
68
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
79
import androidx.test.espresso.matcher.ViewMatchers.withId
810
import androidx.test.ext.junit.rules.activityScenarioRule
911
import androidx.test.ext.junit.runners.AndroidJUnit4
12+
import androidx.test.platform.app.InstrumentationRegistry
1013
import org.junit.Rule
1114
import org.junit.Test
1215
import 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

Comments
 (0)