Skip to content

Commit 4587b1a

Browse files
committed
Adds ActivityScenario.close() calls to UI tests
Change-Id: I386fe61133cdb91ad273132e768fa896989c101b
1 parent dd26bd6 commit 4587b1a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

app/src/androidTestMock/java/com/example/android/architecture/blueprints/todoapp/tasks/AppNavigationTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ import org.junit.runner.RunWith
5151
/**
5252
* Tests for the [DrawerLayout] layout component in [TasksActivity] which manages
5353
* 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()`.
5457
*/
5558
@RunWith(AndroidJUnit4::class)
5659
@LargeTest
@@ -117,6 +120,8 @@ class AppNavigationTest {
117120

118121
// Check that tasks screen was opened.
119122
onView(withId(R.id.tasks_container_layout)).check(matches(isDisplayed()))
123+
// When using ActivityScenario.launch, always call close()
124+
activityScenario.close()
120125
}
121126

122127
@Test
@@ -140,6 +145,8 @@ class AppNavigationTest {
140145
// Check if drawer is open
141146
onView(withId(R.id.drawer_layout))
142147
.check(matches(isOpen(Gravity.START))) // Left drawer is open open.
148+
// When using ActivityScenario.launch, always call close()
149+
activityScenario.close()
143150
}
144151

145152
@Test
@@ -168,6 +175,8 @@ class AppNavigationTest {
168175
// Then check that the drawer is open
169176
onView(withId(R.id.drawer_layout))
170177
.check(matches(isOpen(Gravity.START))) // Left drawer is open open.
178+
// When using ActivityScenario.launch, always call close()
179+
activityScenario.close()
171180
}
172181

173182
@Test
@@ -201,6 +210,8 @@ class AppNavigationTest {
201210
)
202211
).perform(click())
203212
onView(withId(R.id.tasks_container_layout)).check(matches(isDisplayed()))
213+
// When using ActivityScenario.launch, always call close()
214+
activityScenario.close()
204215
}
205216

206217
@Test
@@ -224,5 +235,7 @@ class AppNavigationTest {
224235
// Confirm that if we click back a second time, we end up back at the home screen
225236
pressBack()
226237
onView(withId(R.id.tasks_container_layout)).check(matches(isDisplayed()))
238+
// When using ActivityScenario.launch, always call close()
239+
activityScenario.close()
227240
}
228241
}

app/src/androidTestMock/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksActivityTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ import org.junit.runner.RunWith
5252

5353
/**
5454
* Large End-to-End test for the tasks module.
55+
*
56+
* UI tests usually use [ActivityTestRule] but there's no API to perform an action before
57+
* each test. The workaround is to use `ActivityScenario.launch()` and `ActivityScenario.close()`.
5558
*/
5659
@RunWith(AndroidJUnit4::class)
5760
@LargeTest
@@ -116,6 +119,8 @@ class TasksActivityTest {
116119
onView(withText("NEW TITLE")).check(matches(isDisplayed()))
117120
// Verify previous task is not displayed
118121
onView(withText("TITLE1")).check(doesNotExist())
122+
// Make sure the activity is closed before resetting the db:
123+
activityScenario.close()
119124
}
120125

121126
@Test
@@ -141,6 +146,8 @@ class TasksActivityTest {
141146
onView(withId(R.id.menu_filter)).perform(click())
142147
onView(withText(string.nav_all)).perform(click())
143148
onView(withText("TITLE1")).check(doesNotExist())
149+
// Make sure the activity is closed before resetting the db:
150+
activityScenario.close()
144151
}
145152

146153
@Test
@@ -162,6 +169,8 @@ class TasksActivityTest {
162169
onView(withText(string.nav_all)).perform(click())
163170
onView(withText("TITLE1")).check(matches(isDisplayed()))
164171
onView(withText("TITLE2")).check(doesNotExist())
172+
// Make sure the activity is closed before resetting the db:
173+
activityScenario.close()
165174
}
166175

167176
@Test
@@ -190,6 +199,8 @@ class TasksActivityTest {
190199
// Check that the task is marked as completed
191200
onView(allOf(withId(R.id.complete_checkbox), hasSibling(withText(taskTitle))))
192201
.check(matches(isChecked()))
202+
// Make sure the activity is closed before resetting the db:
203+
activityScenario.close()
193204
}
194205

195206
@Test
@@ -217,6 +228,8 @@ class TasksActivityTest {
217228
// Check that the task is marked as active
218229
onView(allOf(withId(R.id.complete_checkbox), hasSibling(withText(taskTitle))))
219230
.check(matches(not(isChecked())))
231+
// Make sure the activity is closed before resetting the db:
232+
activityScenario.close()
220233
}
221234

222235
@Test
@@ -275,6 +288,8 @@ class TasksActivityTest {
275288
// Check that the task is marked as active
276289
onView(allOf(withId(R.id.complete_checkbox), hasSibling(withText(taskTitle))))
277290
.check(matches(isChecked()))
291+
// Make sure the activity is closed before resetting the db:
292+
activityScenario.close()
278293
}
279294

280295
@Test
@@ -292,5 +307,7 @@ class TasksActivityTest {
292307

293308
// Then verify task is displayed on screen
294309
onView(withText("title")).check(matches(isDisplayed()))
310+
// Make sure the activity is closed before resetting the db:
311+
activityScenario.close()
295312
}
296313
}

0 commit comments

Comments
 (0)