Skip to content

Commit 3fe9d28

Browse files
committed
Addresses Lyla's comments
1 parent 7803918 commit 3fe9d28

File tree

6 files changed

+47
-40
lines changed

6 files changed

+47
-40
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class TasksActivityTest {
259259
// Check that the task is marked as active
260260
onView(allOf(withId(R.id.complete_checkbox), hasSibling(withText(taskTitle))))
261261
.check(matches(not(isChecked())))
262+
// Make sure the activity is closed before resetting the db:
263+
activityScenario.close()
262264
}
263265

264266
@Test

app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class DefaultTasksRepository(
6161
val remoteTasks = tasksRemoteDataSource.getTasks()
6262

6363
if (remoteTasks is Success) {
64-
// Real apps might want to do a proper sync.
64+
// Real apps might want to do a proper sync, deleting, modifying or adding each task.
6565
tasksLocalDataSource.deleteAllTasks()
6666
remoteTasks.data.forEach { task ->
6767
tasksLocalDataSource.saveTask(task)

app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksViewModel.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,20 @@ class TasksViewModel(
214214
}
215215

216216
private fun filterItems(tasks: List<Task>, filteringType: TasksFilterType): List<Task> {
217-
val tasksToShow = ArrayList<Task>()
218-
// We filter the tasks based on the requestType
219-
for (task in tasks) {
220-
when (filteringType) {
221-
TasksFilterType.ALL_TASKS -> tasksToShow.add(task)
222-
TasksFilterType.ACTIVE_TASKS -> if (task.isActive) {
223-
tasksToShow.add(task)
224-
}
225-
TasksFilterType.COMPLETED_TASKS -> if (task.isCompleted) {
226-
tasksToShow.add(task)
227-
}
217+
val tasksToShow = ArrayList<Task>()
218+
// We filter the tasks based on the requestType
219+
for (task in tasks) {
220+
when (filteringType) {
221+
TasksFilterType.ALL_TASKS -> tasksToShow.add(task)
222+
TasksFilterType.ACTIVE_TASKS -> if (task.isActive) {
223+
tasksToShow.add(task)
224+
}
225+
TasksFilterType.COMPLETED_TASKS -> if (task.isCompleted) {
226+
tasksToShow.add(task)
228227
}
229228
}
230-
return tasksToShow
229+
}
230+
return tasksToShow
231231
}
232232

233233
fun refresh() {

app/src/test/java/com/example/android/architecture/blueprints/todoapp/LiveDataTestUtil.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ fun <T> LiveData<T>.getOrAwaitValue(
4545
}
4646
this.observeForever(observer)
4747

48-
afterObserve.invoke()
48+
try {
49+
afterObserve.invoke()
50+
51+
// Don't wait indefinitely if the LiveData is not set.
52+
if (!latch.await(time, timeUnit)) {
53+
this.removeObserver(observer)
54+
throw TimeoutException("LiveData value was never set.")
55+
}
4956

50-
// Don't wait indefinitely if the LiveData is not set.
51-
if (!latch.await(time, timeUnit)) {
57+
} finally {
5258
this.removeObserver(observer)
53-
throw TimeoutException("LiveData value was never set.")
5459
}
55-
5660
@Suppress("UNCHECKED_CAST")
5761
return data as T
5862
}

app/src/test/java/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepositoryTest.kt

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class DefaultTasksRepositoryTest {
6464

6565
@ExperimentalCoroutinesApi
6666
@Test
67-
fun getTasks_emptyRepositoryAndUninitializedCache() = runBlockingTest {
67+
fun getTasks_emptyRepositoryAndUninitializedCache() = mainCoroutineRule.runBlockingTest {
6868
val emptySource = FakeDataSource()
6969
val tasksRepository = DefaultTasksRepository(
7070
emptySource, emptySource, Dispatchers.Main
@@ -74,7 +74,7 @@ class DefaultTasksRepositoryTest {
7474
}
7575

7676
@Test
77-
fun getTasks_repositoryCachesAfterFirstApiCall() = runBlockingTest {
77+
fun getTasks_repositoryCachesAfterFirstApiCall() = mainCoroutineRule.runBlockingTest {
7878
// Trigger the repository to load data, which loads from remote and caches
7979
val initial = tasksRepository.getTasks()
8080

@@ -87,7 +87,7 @@ class DefaultTasksRepositoryTest {
8787
}
8888

8989
@Test
90-
fun getTasks_requestsAllTasksFromRemoteDataSource() = runBlockingTest {
90+
fun getTasks_requestsAllTasksFromRemoteDataSource() = mainCoroutineRule.runBlockingTest {
9191
// When tasks are requested from the tasks repository
9292
val tasks = tasksRepository.getTasks(true) as Success
9393

@@ -96,7 +96,7 @@ class DefaultTasksRepositoryTest {
9696
}
9797

9898
@Test
99-
fun saveTask_savesToLocalAndRemote() = runBlockingTest {
99+
fun saveTask_savesToLocalAndRemote() = mainCoroutineRule.runBlockingTest {
100100
// Make sure newTask is not in the remote or local datasources
101101
assertThat(tasksRemoteDataSource.tasks).doesNotContain(newTask)
102102
assertThat(tasksLocalDataSource.tasks).doesNotContain(newTask)
@@ -110,7 +110,7 @@ class DefaultTasksRepositoryTest {
110110
}
111111

112112
@Test
113-
fun getTasks_WithDirtyCache_tasksAreRetrievedFromRemote() = runBlockingTest {
113+
fun getTasks_WithDirtyCache_tasksAreRetrievedFromRemote() = mainCoroutineRule.runBlockingTest {
114114
// First call returns from REMOTE
115115
val tasks = tasksRepository.getTasks()
116116

@@ -129,7 +129,7 @@ class DefaultTasksRepositoryTest {
129129
}
130130

131131
@Test
132-
fun getTasks_WithDirtyCache_remoteUnavailable_error() = runBlockingTest {
132+
fun getTasks_WithDirtyCache_remoteUnavailable_error() = mainCoroutineRule.runBlockingTest {
133133
// Make remote data source unavailable
134134
tasksRemoteDataSource.tasks = null
135135

@@ -141,16 +141,17 @@ class DefaultTasksRepositoryTest {
141141
}
142142

143143
@Test
144-
fun getTasks_WithRemoteDataSourceUnavailable_tasksAreRetrievedFromLocal() = runBlockingTest {
145-
// When the remote data source is unavailable
146-
tasksRemoteDataSource.tasks = null
144+
fun getTasks_WithRemoteDataSourceUnavailable_tasksAreRetrievedFromLocal() =
145+
mainCoroutineRule.runBlockingTest {
146+
// When the remote data source is unavailable
147+
tasksRemoteDataSource.tasks = null
147148

148-
// The repository fetches from the local source
149-
assertThat((tasksRepository.getTasks() as Success).data).isEqualTo(localTasks)
150-
}
149+
// The repository fetches from the local source
150+
assertThat((tasksRepository.getTasks() as Success).data).isEqualTo(localTasks)
151+
}
151152

152153
@Test
153-
fun getTasks_WithBothDataSourcesUnavailable_returnsError() = runBlockingTest {
154+
fun getTasks_WithBothDataSourcesUnavailable_returnsError() = mainCoroutineRule.runBlockingTest {
154155
// When both sources are unavailable
155156
tasksRemoteDataSource.tasks = null
156157
tasksLocalDataSource.tasks = null
@@ -160,7 +161,7 @@ class DefaultTasksRepositoryTest {
160161
}
161162

162163
@Test
163-
fun getTasks_refreshesLocalDataSource() = runBlockingTest {
164+
fun getTasks_refreshesLocalDataSource() = mainCoroutineRule.runBlockingTest {
164165
val initialLocal = tasksLocalDataSource.tasks
165166

166167
// First load will fetch from remote
@@ -172,7 +173,7 @@ class DefaultTasksRepositoryTest {
172173
}
173174

174175
@Test
175-
fun completeTask_completesTaskToServiceAPIUpdatesCache() = runBlockingTest {
176+
fun completeTask_completesTaskToServiceAPIUpdatesCache() = mainCoroutineRule.runBlockingTest {
176177
// Save a task
177178
tasksRepository.saveTask(newTask)
178179

@@ -187,7 +188,7 @@ class DefaultTasksRepositoryTest {
187188
}
188189

189190
@Test
190-
fun completeTask_activeTaskToServiceAPIUpdatesCache() = runBlockingTest {
191+
fun completeTask_activeTaskToServiceAPIUpdatesCache() = mainCoroutineRule.runBlockingTest {
191192
// Save a task
192193
tasksRepository.saveTask(newTask)
193194
tasksRepository.completeTask(newTask.id)
@@ -204,7 +205,7 @@ class DefaultTasksRepositoryTest {
204205
}
205206

206207
@Test
207-
fun getTask_repositoryCachesAfterFirstApiCall() = runBlockingTest {
208+
fun getTask_repositoryCachesAfterFirstApiCall() = mainCoroutineRule.runBlockingTest {
208209
// Trigger the repository to load data, which loads from remote
209210
tasksRemoteDataSource.tasks = mutableListOf(task1)
210211
tasksRepository.getTask(task1.id, true)
@@ -221,7 +222,7 @@ class DefaultTasksRepositoryTest {
221222
}
222223

223224
@Test
224-
fun getTask_forceRefresh() = runBlockingTest {
225+
fun getTask_forceRefresh() = mainCoroutineRule.runBlockingTest {
225226
// Trigger the repository to load data, which loads from remote and caches
226227
tasksRemoteDataSource.tasks = mutableListOf(task1)
227228
tasksRepository.getTask(task1.id)
@@ -239,7 +240,7 @@ class DefaultTasksRepositoryTest {
239240
}
240241

241242
@Test
242-
fun clearCompletedTasks() = runBlockingTest {
243+
fun clearCompletedTasks() = mainCoroutineRule.runBlockingTest {
243244
val completedTask = task1.copy().apply { isCompleted = true }
244245
tasksRemoteDataSource.tasks = mutableListOf(completedTask, task2)
245246
tasksRepository.clearCompletedTasks()
@@ -252,7 +253,7 @@ class DefaultTasksRepositoryTest {
252253
}
253254

254255
@Test
255-
fun deleteAllTasks() = runBlockingTest {
256+
fun deleteAllTasks() = mainCoroutineRule.runBlockingTest {
256257
val initialTasks = (tasksRepository.getTasks() as? Success)?.data
257258

258259
// Delete all tasks
@@ -267,7 +268,7 @@ class DefaultTasksRepositoryTest {
267268
}
268269

269270
@Test
270-
fun deleteSingleTask() = runBlockingTest {
271+
fun deleteSingleTask() = mainCoroutineRule.runBlockingTest {
271272
val initialTasks = (tasksRepository.getTasks(true) as? Success)?.data
272273

273274
// Delete first task

app/src/test/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailViewModelTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class TaskDetailViewModelTest {
106106
// When the ViewModel is asked to complete the task
107107
taskDetailViewModel.setCompleted(false)
108108

109-
runBlockingTest {
109+
mainCoroutineRule.runBlockingTest {
110110
// Then the task is not completed and the snackbar shows the correct message
111111
val newTask = (tasksRepository.getTask(task.id) as Success).data
112112
assertTrue(newTask.isActive)

0 commit comments

Comments
 (0)