diff options
| author | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2023-09-26 11:32:38 +0200 |
|---|---|---|
| committer | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2023-09-26 11:32:38 +0200 |
| commit | 29bb3f728840f3766728000da5122f07ff3f0fdb (patch) | |
| tree | f4b5f4ac857a7bdaef6f7e8ada9e7fabc98522cc /android/app/src/test | |
| parent | 77b74239c60356a23546a49726a7337d84b69d7b (diff) | |
| parent | f52aa1729fbabe7213c7c2f3c4d9824704a512ca (diff) | |
| download | mullvadvpn-29bb3f728840f3766728000da5122f07ff3f0fdb.tar.xz mullvadvpn-29bb3f728840f3766728000da5122f07ff3f0fdb.zip | |
Merge branch 'migrate-out-of-time-view-to-compose-droid-55'
Diffstat (limited to 'android/app/src/test')
| -rw-r--r-- | android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModelTest.kt | 149 |
1 files changed, 149 insertions, 0 deletions
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 new file mode 100644 index 0000000000..b12c0382a5 --- /dev/null +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModelTest.kt @@ -0,0 +1,149 @@ +package net.mullvad.mullvadvpn.viewmodel + +import androidx.lifecycle.viewModelScope +import app.cash.turbine.test +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import io.mockk.verify +import kotlin.test.assertEquals +import kotlin.test.assertIs +import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.runTest +import net.mullvad.mullvadvpn.compose.state.OutOfTimeUiState +import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule +import net.mullvad.mullvadvpn.model.AccountExpiry +import net.mullvad.mullvadvpn.model.TunnelState +import net.mullvad.mullvadvpn.repository.AccountRepository +import net.mullvad.mullvadvpn.ui.serviceconnection.AuthTokenCache +import net.mullvad.mullvadvpn.ui.serviceconnection.ConnectionProxy +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.ui.serviceconnection.connectionProxy +import net.mullvad.talpid.util.EventNotifier +import org.joda.time.DateTime +import org.joda.time.ReadableInstant +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +class OutOfTimeViewModelTest { + @get:Rule val testCoroutineRule = TestCoroutineRule() + + private val serviceConnectionState = + MutableStateFlow<ServiceConnectionState>(ServiceConnectionState.Disconnected) + private val accountExpiryState = MutableStateFlow<AccountExpiry>(AccountExpiry.Missing) + + // Service connections + private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk() + private val mockConnectionProxy: ConnectionProxy = mockk() + + // Event notifiers + private val eventNotifierTunnelRealState = EventNotifier<TunnelState>(TunnelState.Disconnected) + + private val mockAccountRepository: AccountRepository = mockk() + private val mockServiceConnectionManager: ServiceConnectionManager = mockk() + + private lateinit var viewModel: OutOfTimeViewModel + + @Before + fun setUp() { + mockkStatic(SERVICE_CONNECTION_MANAGER_EXTENSIONS) + + every { mockServiceConnectionManager.connectionState } returns serviceConnectionState + + every { mockServiceConnectionContainer.connectionProxy } returns mockConnectionProxy + + every { mockConnectionProxy.onStateChange } returns eventNotifierTunnelRealState + + every { mockAccountRepository.accountExpiryState } returns accountExpiryState + + viewModel = + OutOfTimeViewModel( + accountRepository = mockAccountRepository, + serviceConnectionManager = mockServiceConnectionManager, + pollAccountExpiry = false + ) + } + + @After + fun tearDown() { + viewModel.viewModelScope.coroutineContext.cancel() + unmockkAll() + } + + @Test + fun testSitePaymentClick() = + runTest(testCoroutineRule.testDispatcher) { + // Arrange + val mockToken = "4444 5555 6666 7777" + val mockAuthTokenCache: AuthTokenCache = mockk(relaxed = true) + every { mockServiceConnectionManager.authTokenCache() } returns mockAuthTokenCache + coEvery { mockAuthTokenCache.fetchAuthToken() } returns mockToken + + // Act, Assert + viewModel.viewActions.test { + viewModel.onSitePaymentClick() + val action = awaitItem() + assertIs<OutOfTimeViewModel.ViewAction.OpenAccountView>(action) + assertEquals(mockToken, action.token) + } + } + + @Test + fun testUpdateTunnelState() = + runTest(testCoroutineRule.testDispatcher) { + // Arrange + val tunnelRealStateTestItem = TunnelState.Connected(mockk(), mockk()) + + // Act, Assert + viewModel.uiState.test { + assertEquals(OutOfTimeUiState(), awaitItem()) + serviceConnectionState.value = + ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer) + eventNotifierTunnelRealState.notify(tunnelRealStateTestItem) + val result = awaitItem() + assertEquals(tunnelRealStateTestItem, result.tunnelState) + } + } + + @Test + fun testOpenConnectScreen() = + runTest(testCoroutineRule.testDispatcher) { + // Arrange + val mockExpiryDate: DateTime = mockk() + every { mockExpiryDate.isAfter(any<ReadableInstant>()) } returns true + + // Act, Assert + viewModel.viewActions.test { + accountExpiryState.value = AccountExpiry.Available(mockExpiryDate) + val action = awaitItem() + assertIs<OutOfTimeViewModel.ViewAction.OpenConnectScreen>(action) + } + } + + @Test + fun testOnDisconnectClick() = + runTest(testCoroutineRule.testDispatcher) { + // Arrange + val mockProxy: ConnectionProxy = mockk(relaxed = true) + every { mockServiceConnectionManager.connectionProxy() } returns mockProxy + + // Act + viewModel.onDisconnectClick() + + // Assert + verify { mockProxy.disconnect() } + } + + companion object { + private const val SERVICE_CONNECTION_MANAGER_EXTENSIONS = + "net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManagerExtensionsKt" + } +} |
