Skip to content

Commit

Permalink
Use last known location instead of null when tunnel state is error
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Nov 20, 2024
1 parent 8e8c679 commit 08e8569
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ class ConnectViewModel(
location =
when (tunnelState) {
is TunnelState.Disconnected ->
tunnelState.location() ?: lastKnownDisconnectedLocation
tunnelState.location ?: lastKnownDisconnectedLocation
is TunnelState.Connecting -> tunnelState.location
is TunnelState.Connected -> tunnelState.location
is TunnelState.Disconnecting -> lastKnownDisconnectedLocation
is TunnelState.Error -> null
is TunnelState.Error -> lastKnownDisconnectedLocation
},
selectedRelayItemTitle = selectedRelayItemTitle,
tunnelState = tunnelState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.state.ConnectUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
Expand Down Expand Up @@ -79,6 +78,7 @@ class ConnectViewModelTest {
// Flows
private val tunnelState = MutableStateFlow<TunnelState>(TunnelState.Disconnected())
private val selectedRelayItemFlow = MutableStateFlow<String?>(null)
private val lastKnownLocationFlow = MutableStateFlow<GeoIpLocation?>(null)

// Out Of Time Use Case
private val outOfTimeUseCase: OutOfTimeUseCase = mockk()
Expand All @@ -104,7 +104,8 @@ class ConnectViewModelTest {

every { mockConnectionProxy.tunnelState } returns tunnelState

every { mockLastKnownLocationUseCase.lastKnownDisconnectedLocation } returns flowOf(null)
every { mockLastKnownLocationUseCase.lastKnownDisconnectedLocation } returns
lastKnownLocationFlow

every { mockLocation.country } returns "dummy country"

Expand Down Expand Up @@ -142,15 +143,14 @@ class ConnectViewModelTest {
}

@Test
fun `given change in tunnelRealState uiState should emit new tunnelRealState`() = runTest {
val tunnelRealStateTestItem =
TunnelState.Connected(mockk(relaxed = true), null, emptyList())
fun `given change in tunnel state uiState should emit new tunnel state`() = runTest {
val tunnelStateTestItem = TunnelState.Connected(mockk(relaxed = true), null, emptyList())

viewModel.uiState.test {
assertEquals(ConnectUiState.INITIAL, awaitItem())
tunnelState.emit(tunnelRealStateTestItem)
tunnelState.emit(tunnelStateTestItem)
val result = awaitItem()
assertEquals(tunnelRealStateTestItem, result.tunnelState)
assertEquals(tunnelStateTestItem, result.tunnelState)
}
}

Expand Down Expand Up @@ -326,4 +326,21 @@ class ConnectViewModelTest {
// Assert
assertIs<ConnectViewModel.UiSideEffect.OutOfTime>(deferred.await())
}

@Test
fun `given tunnel state error should emit last known disconnected location as location`() =
runTest {
// Arrange
val tunnel = TunnelState.Error(mockk(relaxed = true))
val lastKnownLocation: GeoIpLocation = mockk(relaxed = true)
lastKnownLocationFlow.emit(lastKnownLocation)
tunnelState.emit(tunnel)

// Act, Assert
viewModel.uiState.test {
assertEquals(ConnectUiState.INITIAL, awaitItem())
val result = awaitItem()
assertEquals(lastKnownLocation, result.location)
}
}
}

0 comments on commit 08e8569

Please sign in to comment.