@@ -3,13 +3,14 @@ package org.kabiri.android.usbterminal
3
3
import androidx.test.espresso.Espresso.onView
4
4
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
5
5
import androidx.test.espresso.NoMatchingViewException
6
- import androidx.test.espresso.assertion.ViewAssertions.matches
7
6
import androidx.test.espresso.action.ViewActions.click
7
+ import androidx.test.espresso.assertion.ViewAssertions.matches
8
8
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
9
9
import androidx.test.espresso.matcher.ViewMatchers.withId
10
+ import androidx.test.espresso.matcher.ViewMatchers.withText
10
11
import androidx.test.ext.junit.rules.activityScenarioRule
11
12
import androidx.test.ext.junit.runners.AndroidJUnit4
12
- import androidx.test.platform.app.InstrumentationRegistry
13
+ import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
13
14
import org.junit.Rule
14
15
import org.junit.Test
15
16
import org.junit.runner.RunWith
@@ -21,15 +22,19 @@ internal class MainActivityAndroidTest {
21
22
@get:Rule
22
23
var rule = activityScenarioRule<MainActivity >()
23
24
24
- private fun ensureMenuIsAccessible (menuItemId : Int ) {
25
+ private fun ensureMenuIsAccessible (
26
+ menuItemId : Int ,
27
+ onVisible : () -> Unit ,
28
+ onOverflow : () -> Unit
29
+ ) {
25
30
try {
26
31
// Try to find the menu item first
27
32
onView(withId(menuItemId)).check(matches(isDisplayed()))
33
+ onVisible()
28
34
} catch (_: NoMatchingViewException ) {
29
35
// If not found then open the overflow menu
30
- openActionBarOverflowOrOptionsMenu(
31
- InstrumentationRegistry .getInstrumentation().targetContext
32
- )
36
+ openActionBarOverflowOrOptionsMenu(getInstrumentation().targetContext)
37
+ onOverflow()
33
38
}
34
39
}
35
40
@@ -44,31 +49,70 @@ internal class MainActivityAndroidTest {
44
49
}
45
50
46
51
@Test
47
- fun checkActionMenuItemsAreDisplayed () {
48
- // arrange
49
- // act
50
- // Ensure action items are accessible, either in the toolbar or via overflow
51
- ensureMenuIsAccessible(R .id.actionSettings)
52
- ensureMenuIsAccessible(R .id.actionConnect)
53
- ensureMenuIsAccessible(R .id.actionDisconnect)
52
+ fun checkActionMenuItemSettingsIsDisplayed () = ensureMenuIsAccessible(
53
+ menuItemId = R .id.actionSettings,
54
+ onVisible = {
54
55
55
- // assert
56
- // Check menu items are displayed
57
- onView(withId(R .id.actionSettings)).check(matches(isDisplayed()))
58
- onView(withId(R .id.actionConnect)).check(matches(isDisplayed()))
59
- onView(withId(R .id.actionDisconnect)).check(matches(isDisplayed()))
60
- }
56
+ // assert
57
+ onView(withId(R .id.actionSettings)).check(matches(isDisplayed()))
58
+ },
59
+ onOverflow = {
60
+
61
+ // assert
62
+ onView(withText(R .string.title_settings)).check(matches(isDisplayed()))
63
+ }
64
+ )
65
+
66
+ @Test
67
+ fun checkActionMenuItemConnectIsDisplayed () = ensureMenuIsAccessible(
68
+ menuItemId = R .id.actionSettings,
69
+ onVisible = {
70
+
71
+ // assert
72
+ onView(withId(R .id.actionConnect)).check(matches(isDisplayed()))
73
+ },
74
+ onOverflow = {
75
+
76
+ // assert
77
+ onView(withText(R .string.title_connect)).check(matches(isDisplayed()))
78
+ }
79
+ )
80
+
81
+ @Test
82
+ fun checkActionMenuItemDisconnectIsDisplayed () = ensureMenuIsAccessible(
83
+ menuItemId = R .id.actionSettings,
84
+ onVisible = {
85
+
86
+ // assert
87
+ onView(withId(R .id.actionDisconnect)).check(matches(isDisplayed()))
88
+ },
89
+ onOverflow = {
90
+
91
+ // assert
92
+ onView(withText(R .string.title_disconnect)).check(matches(isDisplayed()))
93
+ }
94
+ )
61
95
62
96
@Test
63
97
fun clickingSettingsOpensSettingsBottomSheet () {
64
98
// arrange
65
- // Ensure the action item is accisble, either in the toolbar or via overflow
66
- ensureMenuIsAccessible(R .id.actionSettings)
99
+ ensureMenuIsAccessible(
100
+ menuItemId = R .id.actionSettings,
101
+ onVisible = {
67
102
68
- // act
69
- onView(withId(R .id.actionSettings)).perform(click())
103
+ // act
104
+ onView(withId(R .id.actionSettings)).perform(click())
70
105
71
- // assert
72
- onView(withId(R .id.composeViewSettingContent)).check(matches(isDisplayed()))
106
+ // assert
107
+ onView(withId(R .id.composeViewSettingContent)).check(matches(isDisplayed()))
108
+ },
109
+ onOverflow = {
110
+ // act
111
+ onView(withText(R .string.title_settings)).perform(click())
112
+
113
+ // assert
114
+ onView(withId(R .id.composeViewSettingContent)).check(matches(isDisplayed()))
115
+ }
116
+ )
73
117
}
74
118
}
0 commit comments