16
16
package com.example.android.architecture.blueprints.todoapp.tasks
17
17
18
18
import android.view.Gravity
19
+ import androidx.compose.ui.test.assertIsDisplayed
20
+ import androidx.compose.ui.test.junit4.createAndroidComposeRule
21
+ import androidx.compose.ui.test.onNodeWithText
19
22
import androidx.drawerlayout.widget.DrawerLayout
20
23
import androidx.navigation.findNavController
21
- import androidx.test.core.app.ActivityScenario
22
24
import androidx.test.core.app.ApplicationProvider.getApplicationContext
23
25
import androidx.test.espresso.Espresso.onView
24
26
import androidx.test.espresso.Espresso.pressBack
@@ -45,15 +47,13 @@ import com.example.android.architecture.blueprints.todoapp.util.monitorActivity
45
47
import com.example.android.architecture.blueprints.todoapp.util.saveTaskBlocking
46
48
import org.junit.After
47
49
import org.junit.Before
50
+ import org.junit.Rule
48
51
import org.junit.Test
49
52
import org.junit.runner.RunWith
50
53
51
54
/* *
52
55
* Tests for the [DrawerLayout] layout component in [TasksActivity] which manages
53
56
* navigation within the app.
54
- *
55
- * UI tests usually use [ActivityTestRule] but there's no API to perform an action before
56
- * each test. The workaround is to use `ActivityScenario.launch()` and `ActivityScenario.close()`.
57
57
*/
58
58
@RunWith(AndroidJUnit4 ::class )
59
59
@LargeTest
@@ -64,6 +64,9 @@ class AppNavigationTest {
64
64
// An Idling Resource that waits for Data Binding to have no pending bindings
65
65
private val dataBindingIdlingResource = DataBindingIdlingResource ()
66
66
67
+ @get:Rule
68
+ val composeTestRule = createAndroidComposeRule<TasksActivity >()
69
+
67
70
@Before
68
71
fun init () {
69
72
tasksRepository = ServiceLocator .provideTasksRepository(getApplicationContext())
@@ -96,8 +99,7 @@ class AppNavigationTest {
96
99
@Test
97
100
fun drawerNavigationFromTasksToStatistics () {
98
101
// start up Tasks screen
99
- val activityScenario = ActivityScenario .launch(TasksActivity ::class .java)
100
- dataBindingIdlingResource.monitorActivity(activityScenario)
102
+ dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario)
101
103
102
104
onView(withId(R .id.drawer_layout))
103
105
.check(matches(isClosed(Gravity .START ))) // Left Drawer should be closed.
@@ -108,7 +110,7 @@ class AppNavigationTest {
108
110
.perform(navigateTo(R .id.statistics_fragment_dest))
109
111
110
112
// Check that statistics screen was opened.
111
- onView(withId( R .id.statistics_layout)).check(matches(isDisplayed()) )
113
+ composeTestRule.onNodeWithText( " You have no tasks. " ).assertIsDisplayed( )
112
114
113
115
onView(withId(R .id.drawer_layout))
114
116
.check(matches(isClosed(Gravity .START ))) // Left Drawer should be closed.
@@ -120,15 +122,12 @@ class AppNavigationTest {
120
122
121
123
// Check that tasks screen was opened.
122
124
onView(withId(R .id.tasks_container_layout)).check(matches(isDisplayed()))
123
- // When using ActivityScenario.launch, always call close()
124
- activityScenario.close()
125
125
}
126
126
127
127
@Test
128
128
fun tasksScreen_clickOnAndroidHomeIcon_OpensNavigation () {
129
129
// start up Tasks screen
130
- val activityScenario = ActivityScenario .launch(TasksActivity ::class .java)
131
- dataBindingIdlingResource.monitorActivity(activityScenario)
130
+ dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario)
132
131
133
132
// Check that left drawer is closed at startup
134
133
onView(withId(R .id.drawer_layout))
@@ -137,26 +136,21 @@ class AppNavigationTest {
137
136
// Open Drawer
138
137
onView(
139
138
withContentDescription(
140
- activityScenario
141
- .getToolbarNavigationContentDescription()
139
+ composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription()
142
140
)
143
141
).perform(click())
144
142
145
143
// Check if drawer is open
146
144
onView(withId(R .id.drawer_layout))
147
145
.check(matches(isOpen(Gravity .START ))) // Left drawer is open open.
148
- // When using ActivityScenario.launch, always call close()
149
- activityScenario.close()
150
146
}
151
147
152
148
@Test
153
149
fun statsScreen_clickOnAndroidHomeIcon_OpensNavigation () {
154
- // start up Tasks screen
155
- val activityScenario = ActivityScenario .launch(TasksActivity ::class .java)
156
- dataBindingIdlingResource.monitorActivity(activityScenario)
150
+ dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario)
157
151
158
152
// When the user navigates to the stats screen
159
- activityScenario .onActivity {
153
+ composeTestRule.activityRule.scenario .onActivity {
160
154
it.findNavController(R .id.nav_host_fragment).navigate(R .id.statistics_fragment_dest)
161
155
}
162
156
@@ -167,26 +161,21 @@ class AppNavigationTest {
167
161
// When the drawer is opened
168
162
onView(
169
163
withContentDescription(
170
- activityScenario
171
- .getToolbarNavigationContentDescription()
164
+ composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription()
172
165
)
173
166
).perform(click())
174
167
175
168
// Then check that the drawer is open
176
169
onView(withId(R .id.drawer_layout))
177
170
.check(matches(isOpen(Gravity .START ))) // Left drawer is open open.
178
- // When using ActivityScenario.launch, always call close()
179
- activityScenario.close()
180
171
}
181
172
182
173
@Test
183
174
fun taskDetailScreen_doubleUIBackButton () {
184
175
val task = Task (" UI <- button" , " Description" )
185
176
tasksRepository.saveTaskBlocking(task)
186
177
187
- // start up Tasks screen
188
- val activityScenario = ActivityScenario .launch(TasksActivity ::class .java)
189
- dataBindingIdlingResource.monitorActivity(activityScenario)
178
+ dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario)
190
179
191
180
// Click on the task on the list
192
181
onView(withText(" UI <- button" )).perform(click())
@@ -196,32 +185,26 @@ class AppNavigationTest {
196
185
// Confirm that if we click "<-" once, we end up back at the task details page
197
186
onView(
198
187
withContentDescription(
199
- activityScenario
200
- .getToolbarNavigationContentDescription()
188
+ composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription()
201
189
)
202
190
).perform(click())
203
191
onView(withId(R .id.task_detail_title_text)).check(matches(isDisplayed()))
204
192
205
193
// Confirm that if we click "<-" a second time, we end up back at the home screen
206
194
onView(
207
195
withContentDescription(
208
- activityScenario
209
- .getToolbarNavigationContentDescription()
196
+ composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription()
210
197
)
211
198
).perform(click())
212
199
onView(withId(R .id.tasks_container_layout)).check(matches(isDisplayed()))
213
- // When using ActivityScenario.launch, always call close()
214
- activityScenario.close()
215
200
}
216
201
217
202
@Test
218
203
fun taskDetailScreen_doubleBackButton () {
219
204
val task = Task (" Back button" , " Description" )
220
205
tasksRepository.saveTaskBlocking(task)
221
206
222
- // start up Tasks screen
223
- val activityScenario = ActivityScenario .launch(TasksActivity ::class .java)
224
- dataBindingIdlingResource.monitorActivity(activityScenario)
207
+ dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario)
225
208
226
209
// Click on the task on the list
227
210
onView(withText(" Back button" )).perform(click())
@@ -235,7 +218,5 @@ class AppNavigationTest {
235
218
// Confirm that if we click back a second time, we end up back at the home screen
236
219
pressBack()
237
220
onView(withId(R .id.tasks_container_layout)).check(matches(isDisplayed()))
238
- // When using ActivityScenario.launch, always call close()
239
- activityScenario.close()
240
221
}
241
222
}
0 commit comments