From 05b5dcfa6e57df10be08a4f1f968024ea7c32bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Wed, 6 Dec 2023 16:45:46 +0100 Subject: [PATCH] Fix tests --- .../mullvad/mullvadvpn/compose/screen/MullvadApp.kt | 5 ++--- .../mullvadvpn/viewmodel/OutOfTimeViewModelTest.kt | 11 ++++++++++- .../mullvadvpn/viewmodel/WelcomeViewModelTest.kt | 10 +++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/MullvadApp.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/MullvadApp.kt index 6020287c3cb5..39c3c512d42c 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/MullvadApp.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/MullvadApp.kt @@ -53,9 +53,8 @@ fun MullvadApp() { LaunchedEffect(Unit) { serviceVm.sideEffect.collect { when (it) { - DaemonScreenEvent.Show -> navController.navigate(NoDaemonScreenDestination) { - launchSingleTop = true - } + DaemonScreenEvent.Show -> + navController.navigate(NoDaemonScreenDestination) { launchSingleTop = true } DaemonScreenEvent.Remove -> navController.popBackStack(NoDaemonScreenDestination, true) } diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModelTest.kt index bbc047cfe612..57b390ea6f15 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModelTest.kt @@ -33,6 +33,7 @@ import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState import net.mullvad.mullvadvpn.ui.serviceconnection.authTokenCache import net.mullvad.mullvadvpn.ui.serviceconnection.connectionProxy +import net.mullvad.mullvadvpn.usecase.OutOfTimeUseCase import net.mullvad.mullvadvpn.usecase.PaymentUseCase import net.mullvad.talpid.util.EventNotifier import org.joda.time.DateTime @@ -51,6 +52,7 @@ class OutOfTimeViewModelTest { private val deviceState = MutableStateFlow(DeviceState.Initial) private val paymentAvailability = MutableStateFlow(null) private val purchaseResult = MutableStateFlow(null) + private val outOfTime = MutableStateFlow(true) // Service connections private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk() @@ -63,6 +65,7 @@ class OutOfTimeViewModelTest { private val mockDeviceRepository: DeviceRepository = mockk() private val mockServiceConnectionManager: ServiceConnectionManager = mockk() private val mockPaymentUseCase: PaymentUseCase = mockk(relaxed = true) + private val mockOutOfTimeUseCase: OutOfTimeUseCase = mockk(relaxed = true) private lateinit var viewModel: OutOfTimeViewModel @@ -85,18 +88,24 @@ class OutOfTimeViewModelTest { coEvery { mockPaymentUseCase.paymentAvailability } returns paymentAvailability + coEvery { mockOutOfTimeUseCase.isOutOfTime() } returns outOfTime + viewModel = OutOfTimeViewModel( accountRepository = mockAccountRepository, serviceConnectionManager = mockServiceConnectionManager, deviceRepository = mockDeviceRepository, paymentUseCase = mockPaymentUseCase, + outOfTimeUseCase = mockOutOfTimeUseCase, pollAccountExpiry = false ) + + viewModel.start() } @After fun tearDown() { + viewModel.stop() viewModel.viewModelScope.coroutineContext.cancel() unmockkAll() } @@ -145,7 +154,7 @@ class OutOfTimeViewModelTest { // Act, Assert viewModel.uiSideEffect.test { - accountExpiryState.value = AccountExpiry.Available(mockExpiryDate) + outOfTime.value = false val action = awaitItem() assertIs(action) } diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt index 9ec642dcc899..564366bbe42e 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModelTest.kt @@ -32,6 +32,7 @@ import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionContainer import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState import net.mullvad.mullvadvpn.ui.serviceconnection.authTokenCache +import net.mullvad.mullvadvpn.usecase.OutOfTimeUseCase import net.mullvad.mullvadvpn.usecase.PaymentUseCase import net.mullvad.talpid.util.EventNotifier import org.joda.time.DateTime @@ -50,6 +51,7 @@ class WelcomeViewModelTest { private val accountExpiryState = MutableStateFlow(AccountExpiry.Missing) private val purchaseResult = MutableStateFlow(null) private val paymentAvailability = MutableStateFlow(null) + private val outOfTime = MutableStateFlow(true) // Service connections private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk() @@ -62,6 +64,7 @@ class WelcomeViewModelTest { private val mockDeviceRepository: DeviceRepository = mockk() private val mockServiceConnectionManager: ServiceConnectionManager = mockk() private val mockPaymentUseCase: PaymentUseCase = mockk(relaxed = true) + private val mockOutOfTimeUseCase: OutOfTimeUseCase = mockk(relaxed = true) private lateinit var viewModel: WelcomeViewModel @@ -84,19 +87,24 @@ class WelcomeViewModelTest { coEvery { mockPaymentUseCase.paymentAvailability } returns paymentAvailability + coEvery { mockOutOfTimeUseCase.isOutOfTime() } returns outOfTime + viewModel = WelcomeViewModel( accountRepository = mockAccountRepository, deviceRepository = mockDeviceRepository, serviceConnectionManager = mockServiceConnectionManager, paymentUseCase = mockPaymentUseCase, + outOfTimeUseCase = mockOutOfTimeUseCase, pollAccountExpiry = false ) + viewModel.start() } @After fun tearDown() { viewModel.viewModelScope.coroutineContext.cancel() + viewModel.stop() unmockkAll() } @@ -166,7 +174,7 @@ class WelcomeViewModelTest { // Act, Assert viewModel.uiSideEffect.test { - accountExpiryState.value = AccountExpiry.Available(mockExpiryDate) + outOfTime.value = false val action = awaitItem() assertIs(action) }