|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 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 | + * https://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.google.samples.apps.iosched.ui.reservation |
| 18 | + |
| 19 | +import androidx.arch.core.executor.testing.InstantTaskExecutorRule |
| 20 | +import androidx.lifecycle.SavedStateHandle |
| 21 | +import com.google.samples.apps.iosched.model.SessionId |
| 22 | +import com.google.samples.apps.iosched.model.TestDataRepository |
| 23 | +import com.google.samples.apps.iosched.shared.data.session.DefaultSessionRepository |
| 24 | +import com.google.samples.apps.iosched.shared.data.userevent.DefaultSessionAndUserEventRepository |
| 25 | +import com.google.samples.apps.iosched.shared.data.userevent.UserEventDataSource |
| 26 | +import com.google.samples.apps.iosched.shared.domain.sessions.LoadUserSessionUseCase |
| 27 | +import com.google.samples.apps.iosched.shared.domain.sessions.StarReserveNotificationAlarmUpdater |
| 28 | +import com.google.samples.apps.iosched.shared.domain.users.ReservationActionUseCase |
| 29 | +import com.google.samples.apps.iosched.test.data.MainCoroutineRule |
| 30 | +import com.google.samples.apps.iosched.test.data.TestData |
| 31 | +import com.google.samples.apps.iosched.test.data.runBlockingTest |
| 32 | +import com.google.samples.apps.iosched.test.util.fakes.FakeSignInViewModelDelegate |
| 33 | +import com.google.samples.apps.iosched.ui.schedule.TestUserEventDataSource |
| 34 | +import com.google.samples.apps.iosched.ui.signin.SignInViewModelDelegate |
| 35 | +import com.nhaarman.mockito_kotlin.any |
| 36 | +import com.nhaarman.mockito_kotlin.mock |
| 37 | +import com.nhaarman.mockito_kotlin.verify |
| 38 | +import kotlinx.coroutines.flow.first |
| 39 | +import org.junit.Assert.assertEquals |
| 40 | +import org.junit.Before |
| 41 | +import org.junit.Rule |
| 42 | +import org.junit.Test |
| 43 | + |
| 44 | +/** |
| 45 | + * Unit tests for the [RemoveReservationViewModel]. |
| 46 | + */ |
| 47 | +class RemoveReservationViewModelTest { |
| 48 | + |
| 49 | + // Executes tasks in the Architecture Components in the same thread |
| 50 | + @get:Rule |
| 51 | + var instantTaskExecutorRule = InstantTaskExecutorRule() |
| 52 | + |
| 53 | + // Overrides Dispatchers.Main used in Coroutines |
| 54 | + @get:Rule |
| 55 | + var coroutineRule = MainCoroutineRule() |
| 56 | + |
| 57 | + private val signInViewModelDelegate = FakeSignInViewModelDelegate() |
| 58 | + private val alarmUpdater: StarReserveNotificationAlarmUpdater = mock() |
| 59 | + |
| 60 | + @Before |
| 61 | + fun setUp() { |
| 62 | + signInViewModelDelegate.loadUser("123") |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun dataIsLoaded() = coroutineRule.runBlockingTest { |
| 67 | + val removeReservationViewModel = createRemoveReservationViewModel(TestData.session0.id) |
| 68 | + |
| 69 | + val userSession = removeReservationViewModel.userSession.first() |
| 70 | + assertEquals("0", userSession?.session?.id) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun reservationIsPlaced() = coroutineRule.runBlockingTest { |
| 75 | + val removeReservationViewModel = createRemoveReservationViewModel(TestData.session0.id) |
| 76 | + |
| 77 | + val userSession = removeReservationViewModel.userSession.first() |
| 78 | + assertEquals("0", userSession?.session?.id) |
| 79 | + |
| 80 | + removeReservationViewModel.removeReservation() |
| 81 | + |
| 82 | + verify(alarmUpdater).updateSession(any(), any()) |
| 83 | + } |
| 84 | + |
| 85 | + private fun createRemoveReservationViewModel( |
| 86 | + sessionId: SessionId = TestData.session0.id, |
| 87 | + signInViewModelDelegate: SignInViewModelDelegate = this.signInViewModelDelegate, |
| 88 | + loadUserSessionUseCase: LoadUserSessionUseCase = createTestLoadUserSessionUseCase(), |
| 89 | + reservationActionUseCase: ReservationActionUseCase = createReservationActionUseCase() |
| 90 | + ) = RemoveReservationViewModel( |
| 91 | + savedStateHandle = SavedStateHandle(mapOf("session_id" to sessionId)), |
| 92 | + signInViewModelDelegate = signInViewModelDelegate, |
| 93 | + loadUserSessionUseCase = loadUserSessionUseCase, |
| 94 | + reservationActionUseCase = reservationActionUseCase |
| 95 | + ) |
| 96 | + |
| 97 | + private fun createTestLoadUserSessionUseCase( |
| 98 | + userEventDataSource: UserEventDataSource = TestUserEventDataSource() |
| 99 | + ): LoadUserSessionUseCase { |
| 100 | + val sessionRepository = DefaultSessionRepository(TestDataRepository) |
| 101 | + val userEventRepository = DefaultSessionAndUserEventRepository( |
| 102 | + userEventDataSource, |
| 103 | + sessionRepository |
| 104 | + ) |
| 105 | + return LoadUserSessionUseCase(userEventRepository, coroutineRule.testDispatcher) |
| 106 | + } |
| 107 | + |
| 108 | + private fun createReservationActionUseCase() = object : ReservationActionUseCase( |
| 109 | + DefaultSessionAndUserEventRepository( |
| 110 | + TestUserEventDataSource(), DefaultSessionRepository(TestDataRepository) |
| 111 | + ), |
| 112 | + alarmUpdater, |
| 113 | + coroutineRule.testDispatcher |
| 114 | + ) {} |
| 115 | +} |
0 commit comments