|
| 1 | +/* |
| 2 | + * Copyright 2016, The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.android.architecture.blueprints.todoapp.data |
| 18 | + |
| 19 | +import android.support.test.InstrumentationRegistry |
| 20 | +import android.support.test.filters.LargeTest |
| 21 | +import android.support.test.runner.AndroidJUnit4 |
| 22 | +import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource |
| 23 | +import com.example.android.architecture.blueprints.todoapp.data.source.local.TasksDbHelper |
| 24 | +import com.example.android.architecture.blueprints.todoapp.data.source.local.TasksLocalDataSource |
| 25 | +import com.example.android.architecture.blueprints.todoapp.util.schedulers.BaseSchedulerProvider |
| 26 | +import com.example.android.architecture.blueprints.todoapp.util.schedulers.ImmediateSchedulerProvider |
| 27 | +import io.reactivex.observers.TestObserver |
| 28 | +import org.hamcrest.core.Is.`is` |
| 29 | +import org.hamcrest.core.IsCollectionContaining.hasItems |
| 30 | +import org.hamcrest.core.IsNot.not |
| 31 | +import org.junit.After |
| 32 | +import org.junit.Assert.assertNotNull |
| 33 | +import org.junit.Assert.assertThat |
| 34 | +import org.junit.Before |
| 35 | +import org.junit.Test |
| 36 | +import org.junit.runner.RunWith |
| 37 | + |
| 38 | +/** |
| 39 | + * Integration test for the [TasksDataSource], which uses the [TasksDbHelper]. |
| 40 | + */ |
| 41 | +@RunWith(AndroidJUnit4::class) |
| 42 | +@LargeTest |
| 43 | +class TasksLocalDataSourceTest { |
| 44 | + private lateinit var schedulerProvider: BaseSchedulerProvider |
| 45 | + private lateinit var localDataSource: TasksLocalDataSource |
| 46 | + |
| 47 | + @Before |
| 48 | + fun setup() { |
| 49 | + TasksLocalDataSource.clearInstance() |
| 50 | + schedulerProvider = ImmediateSchedulerProvider() |
| 51 | + |
| 52 | + localDataSource = TasksLocalDataSource |
| 53 | + .getInstance(InstrumentationRegistry.getTargetContext(), schedulerProvider) |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + fun cleanUp() { |
| 58 | + localDataSource.deleteAllTasks() |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun testPreConditions() { |
| 63 | + assertNotNull(localDataSource) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun saveTask_retrievesTask() { |
| 68 | + // Given a new task |
| 69 | + val newTask = Task(title = TITLE, description = "") |
| 70 | + |
| 71 | + // When saved into the persistent repository |
| 72 | + localDataSource.saveTask(newTask) |
| 73 | + |
| 74 | + // Then the task can be retrieved from the persistent repository |
| 75 | + val testObserver = TestObserver<Task>() |
| 76 | + localDataSource.getTask(newTask.id).subscribe(testObserver) |
| 77 | + testObserver.assertValue(newTask) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun completeTask_retrievedTaskIsComplete() { |
| 82 | + // Given a new task in the persistent repository |
| 83 | + val newTask = Task(title = TITLE, description = "") |
| 84 | + localDataSource.saveTask(newTask) |
| 85 | + |
| 86 | + // When completed in the persistent repository |
| 87 | + localDataSource.completeTask(newTask) |
| 88 | + |
| 89 | + // Then the task can be retrieved from the persistent repository and is complete |
| 90 | + val testObserver = TestObserver<Task>() |
| 91 | + localDataSource.getTask(newTask.id).subscribe(testObserver) |
| 92 | + testObserver.assertValueCount(1) |
| 93 | + val (_, _, _, completed) = testObserver.values()[0] |
| 94 | + assertThat(completed, `is`(true)) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun activateTask_retrievedTaskIsActive() { |
| 99 | + // Given a new completed task in the persistent repository |
| 100 | + val newTask = Task(title = TITLE, description = "") |
| 101 | + localDataSource.saveTask(newTask) |
| 102 | + localDataSource.completeTask(newTask) |
| 103 | + |
| 104 | + // When activated in the persistent repository |
| 105 | + localDataSource.activateTask(newTask) |
| 106 | + |
| 107 | + // Then the task can be retrieved from the persistent repository and is active |
| 108 | + val testObserver = TestObserver<Task>() |
| 109 | + localDataSource.getTask(newTask.id).subscribe(testObserver) |
| 110 | + testObserver.assertValueCount(1) |
| 111 | + val result = testObserver.values()[0] |
| 112 | + assertThat(result.active, `is`(true)) |
| 113 | + assertThat(result.completed, `is`(false)) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun clearCompletedTask_taskNotRetrievable() { |
| 118 | + // Given 2 new completed tasks and 1 active task in the persistent repository |
| 119 | + val newTask1 = Task(title = TITLE, description = "") |
| 120 | + localDataSource.saveTask(newTask1) |
| 121 | + localDataSource.completeTask(newTask1) |
| 122 | + val newTask2 = Task(title = TITLE2, description = "") |
| 123 | + localDataSource.saveTask(newTask2) |
| 124 | + localDataSource.completeTask(newTask2) |
| 125 | + val newTask3 = Task(title = TITLE3, description = "") |
| 126 | + localDataSource.saveTask(newTask3) |
| 127 | + |
| 128 | + // When completed tasks are cleared in the repository |
| 129 | + localDataSource.clearCompletedTasks() |
| 130 | + |
| 131 | + // Then the completed tasks cannot be retrieved and the active one can |
| 132 | + val testObserver = TestObserver<List<Task>>() |
| 133 | + localDataSource.getTasks().subscribe(testObserver) |
| 134 | + val result = testObserver.values()[0] |
| 135 | + assertThat(result, not(hasItems(newTask1, newTask2))) |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun deleteAllTasks_emptyListOfRetrievedTask() { |
| 140 | + // Given a new task in the persistent repository and a mocked callback |
| 141 | + val newTask = Task(title = TITLE, description = "") |
| 142 | + localDataSource.saveTask(newTask) |
| 143 | + |
| 144 | + // When all tasks are deleted |
| 145 | + localDataSource.deleteAllTasks() |
| 146 | + |
| 147 | + // Then the retrieved tasks is an empty list |
| 148 | + val testObserver = TestObserver<List<Task>>() |
| 149 | + localDataSource.getTasks().subscribe(testObserver) |
| 150 | + val result = testObserver.values()[0] |
| 151 | + assertThat(result.isEmpty(), `is`(true)) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + fun getTasks_retrieveSavedTasks() { |
| 156 | + // Given 2 new tasks in the persistent repository |
| 157 | + val newTask1 = Task(title = TITLE, description = "a") |
| 158 | + localDataSource.saveTask(newTask1) |
| 159 | + val newTask2 = Task(title = TITLE, description = "b") |
| 160 | + localDataSource.saveTask(newTask2) |
| 161 | + |
| 162 | + // Then the tasks can be retrieved from the persistent repository |
| 163 | + val testObserver = TestObserver<List<Task>>() |
| 164 | + localDataSource.getTasks().subscribe(testObserver) |
| 165 | + val result = testObserver.values()[0] |
| 166 | + assertThat(result, hasItems(newTask1, newTask2)) |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + fun getTask_whenTaskNotSaved() { |
| 171 | + //Given that no task has been saved |
| 172 | + //When querying for a task, null is returned. |
| 173 | + val testObserver = TestObserver<Task>() |
| 174 | + localDataSource.getTask("1").subscribe(testObserver) |
| 175 | + testObserver.assertEmpty() |
| 176 | + } |
| 177 | + |
| 178 | + companion object { |
| 179 | + private const val TITLE = "title" |
| 180 | + private const val TITLE2 = "title2" |
| 181 | + private const val TITLE3 = "title3" |
| 182 | + } |
| 183 | +} |
0 commit comments