|
| 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.tasks |
| 18 | + |
| 19 | +import com.example.android.architecture.blueprints.todoapp.data.Task |
| 20 | +import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository |
| 21 | +import com.example.android.architecture.blueprints.todoapp.util.schedulers.BaseSchedulerProvider |
| 22 | +import com.example.android.architecture.blueprints.todoapp.util.schedulers.ImmediateSchedulerProvider |
| 23 | +import com.google.common.collect.Lists |
| 24 | +import com.nhaarman.mockito_kotlin.any |
| 25 | +import io.reactivex.Completable |
| 26 | +import io.reactivex.Observable |
| 27 | +import io.reactivex.Single |
| 28 | +import io.reactivex.observers.TestObserver |
| 29 | +import org.junit.Before |
| 30 | +import org.junit.Test |
| 31 | +import org.mockito.Mock |
| 32 | +import org.mockito.Mockito.`when` |
| 33 | +import org.mockito.Mockito.verify |
| 34 | +import org.mockito.MockitoAnnotations |
| 35 | + |
| 36 | +/** |
| 37 | + * Unit tests for the implementation of [TasksViewModel] |
| 38 | + */ |
| 39 | +class TasksViewModelTest { |
| 40 | + @Mock |
| 41 | + private lateinit var tasksRepository: TasksRepository |
| 42 | + private lateinit var schedulerProvider: BaseSchedulerProvider |
| 43 | + private lateinit var tasksViewModel: TasksViewModel |
| 44 | + private lateinit var testObserver: TestObserver<TasksViewState> |
| 45 | + private lateinit var tasks: List<Task> |
| 46 | + |
| 47 | + @Before |
| 48 | + fun setupTasksViewModel() { |
| 49 | + // Mockito has a very convenient way to inject mocks by using the @Mock annotation. To |
| 50 | + // inject the mocks in the test the initMocks method needs to be called. |
| 51 | + MockitoAnnotations.initMocks(this) |
| 52 | + |
| 53 | + // Make the sure that all schedulers are immediate. |
| 54 | + schedulerProvider = ImmediateSchedulerProvider() |
| 55 | + |
| 56 | + // Get a reference to the class under test |
| 57 | + tasksViewModel = TasksViewModel(TasksActionProcessorHolder(tasksRepository, schedulerProvider)) |
| 58 | + |
| 59 | + // We subscribe the tasks to 3, with one active and two completed |
| 60 | + tasks = Lists.newArrayList( |
| 61 | + Task(title = "Title1", description = "Description1", completed = false), |
| 62 | + Task(title = "Title2", description = "Description2", completed = true), |
| 63 | + Task(title = "Title3", description = "Description3", completed = true)) |
| 64 | + |
| 65 | + testObserver = tasksViewModel.states().test() |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun loadAllTasksFromRepositoryAndLoadIntoView() { |
| 70 | + // Given an initialized TasksViewModel with initialized tasks |
| 71 | + `when`(tasksRepository.getTasks(any())).thenReturn(Single.just(tasks)) |
| 72 | + // When loading of Tasks is initiated |
| 73 | + tasksViewModel.processIntents(Observable.just(TasksIntent.InitialIntent.create())) |
| 74 | + |
| 75 | + // Then progress indicator state is emitted |
| 76 | + testObserver.assertValueAt(1, TasksViewState::isLoading) |
| 77 | + // Then progress indicator state is canceled and all tasks are emitted |
| 78 | + testObserver.assertValueAt(2) { tasksViewState -> !tasksViewState.isLoading } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun loadActiveTasksFromRepositoryAndLoadIntoView() { |
| 83 | + // Given an initialized TasksViewModel with initialized tasks |
| 84 | + `when`(tasksRepository.getTasks(any())).thenReturn(Single.just(tasks)) |
| 85 | + // When loading of Tasks is initiated |
| 86 | + tasksViewModel.processIntents( |
| 87 | + Observable.just(TasksIntent.ChangeFilterIntent.create(TasksFilterType.ACTIVE_TASKS))) |
| 88 | + |
| 89 | + // Then progress indicator state is emitted |
| 90 | + testObserver.assertValueAt(1, TasksViewState::isLoading) |
| 91 | + // Then progress indicator state is canceled and all tasks are emitted |
| 92 | + testObserver.assertValueAt(2) { tasksViewState -> !tasksViewState.isLoading } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun loadCompletedTasksFromRepositoryAndLoadIntoView() { |
| 97 | + // Given an initialized TasksViewModel with initialized tasks |
| 98 | + `when`(tasksRepository.getTasks(any())).thenReturn(Single.just(tasks)) |
| 99 | + // When loading of Tasks is requested |
| 100 | + tasksViewModel.processIntents( |
| 101 | + Observable.just(TasksIntent.ChangeFilterIntent.create(TasksFilterType.COMPLETED_TASKS))) |
| 102 | + |
| 103 | + // Then progress indicator state is emitted |
| 104 | + testObserver.assertValueAt(1, TasksViewState::isLoading) |
| 105 | + // Then progress indicator state is canceled and all tasks are emitted |
| 106 | + testObserver.assertValueAt(2) { tasksViewState -> !tasksViewState.isLoading } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun completeTask_ShowsTaskMarkedComplete() { |
| 111 | + // Given a stubbed task |
| 112 | + val task = Task(title = "Details Requested", description = "For this task") |
| 113 | + // And no tasks available in the repository |
| 114 | + `when`(tasksRepository.completeTask(task)).thenReturn(Completable.complete()) |
| 115 | + `when`(tasksRepository.getTasks()).thenReturn(Single.just(emptyList())) |
| 116 | + |
| 117 | + // When task is marked as complete |
| 118 | + tasksViewModel.processIntents(Observable.just(TasksIntent.CompleteTaskIntent.create(task))) |
| 119 | + |
| 120 | + // Then repository is called and task marked complete state is emitted |
| 121 | + verify(tasksRepository).completeTask(task) |
| 122 | + verify(tasksRepository).getTasks() |
| 123 | + testObserver.assertValueAt(2, TasksViewState::taskComplete) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun activateTask_ShowsTaskMarkedActive() { |
| 128 | + // Given a stubbed completed task |
| 129 | + val task = Task(title = "Details Requested", description = "For this task", completed = true) |
| 130 | + // And no tasks available in the repository |
| 131 | + `when`(tasksRepository.activateTask(task)).thenReturn(Completable.complete()) |
| 132 | + `when`(tasksRepository.getTasks()).thenReturn(Single.just(emptyList())) |
| 133 | + |
| 134 | + // When task is marked as activated |
| 135 | + tasksViewModel.processIntents(Observable.just(TasksIntent.ActivateTaskIntent.create(task))) |
| 136 | + |
| 137 | + // Then repository is called and task marked active state is emitted |
| 138 | + verify(tasksRepository).activateTask(task) |
| 139 | + verify(tasksRepository).getTasks() |
| 140 | + testObserver.assertValueAt(2, TasksViewState::taskActivated) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun errorLoadingTasks_ShowsError() { |
| 145 | + // Given that no tasks are available in the repository |
| 146 | + `when`(tasksRepository.getTasks(any())).thenReturn(Single.error(Exception())) |
| 147 | + |
| 148 | + // When tasks are loaded |
| 149 | + tasksViewModel.processIntents(Observable.just(TasksIntent.InitialIntent.create())) |
| 150 | + |
| 151 | + // Then an error containing state is emitted |
| 152 | + testObserver.assertValueAt(2) { state -> state.error() != null } |
| 153 | + } |
| 154 | +} |
0 commit comments