Skip to content

Commit

Permalink
Fix android tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Nov 2, 2023
1 parent f699885 commit 040115c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import net.mullvad.mullvadvpn.ui.serviceconnection.AppVersionInfoCache
import net.mullvad.mullvadvpn.ui.serviceconnection.AuthTokenCache
import net.mullvad.mullvadvpn.ui.serviceconnection.ConnectionProxy
import net.mullvad.mullvadvpn.ui.serviceconnection.LocationInfoCache
import net.mullvad.mullvadvpn.ui.serviceconnection.RelayListListener
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.mullvadvpn.usecase.RelayListUseCase
import net.mullvad.mullvadvpn.util.appVersionCallbackFlow
import net.mullvad.talpid.tunnel.ErrorState
import net.mullvad.talpid.tunnel.ErrorStateCause
Expand Down Expand Up @@ -73,7 +73,6 @@ class ConnectViewModelTest {
// Service connections
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
private val mockLocationInfoCache: LocationInfoCache = mockk(relaxUnitFun = true)
private val mockRelayListListener: RelayListListener = mockk(relaxUnitFun = true)
private lateinit var mockAppVersionInfoCache: AppVersionInfoCache
private val mockConnectionProxy: ConnectionProxy = mockk()
private val mockLocation: GeoIpLocation = mockk(relaxed = true)
Expand All @@ -87,14 +86,20 @@ class ConnectViewModelTest {
// In App Notifications
private val mockInAppNotificationController: InAppNotificationController = mockk()

// Relay list use case
private val mockRelayListUseCase: RelayListUseCase = mockk()

// Captures
private val locationSlot = slot<((GeoIpLocation?) -> Unit)>()
private val relaySlot = slot<(List<RelayCountry>, RelayItem?) -> Unit>()

// Event notifiers
private val eventNotifierTunnelUiState = EventNotifier<TunnelState>(TunnelState.Disconnected)
private val eventNotifierTunnelRealState = EventNotifier<TunnelState>(TunnelState.Disconnected)

// Flows
private val relayListWithSelectionFlow =
MutableStateFlow<Pair<List<RelayCountry>, RelayItem?>>(Pair(emptyList(), null))

@Before
fun setup() {
mockkStatic(CACHE_EXTENSION_CLASS)
Expand All @@ -107,7 +112,6 @@ class ConnectViewModelTest {

every { mockServiceConnectionManager.connectionState } returns serviceConnectionState
every { mockServiceConnectionContainer.locationInfoCache } returns mockLocationInfoCache
every { mockServiceConnectionContainer.relayListListener } returns mockRelayListListener
every { mockServiceConnectionContainer.appVersionInfoCache } returns mockAppVersionInfoCache
every { mockServiceConnectionContainer.connectionProxy } returns mockConnectionProxy

Expand All @@ -124,15 +128,18 @@ class ConnectViewModelTest {

// Listeners
every { mockLocationInfoCache.onNewLocation = capture(locationSlot) } answers {}
every { mockRelayListListener.onRelayCountriesChange = capture(relaySlot) } answers {}
every { mockAppVersionInfoCache.onUpdate = any() } answers {}

// Flows
every { mockRelayListUseCase.relayListWithSelection() } returns relayListWithSelectionFlow

viewModel =
ConnectViewModel(
serviceConnectionManager = mockServiceConnectionManager,
accountRepository = mockAccountRepository,
deviceRepository = mockDeviceRepository,
inAppNotificationController = mockInAppNotificationController,
relayListUseCase = mockRelayListUseCase,
newDeviceNotificationUseCase = mockk()
)
}
Expand All @@ -156,7 +163,6 @@ class ConnectViewModelTest {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(mockLocation)
relaySlot.captured.invoke(mockk(), mockk())
viewModel.toggleTunnelInfoExpansion()
val result = awaitItem()
assertTrue(result.isTunnelInfoExpanded)
Expand All @@ -173,7 +179,6 @@ class ConnectViewModelTest {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(mockLocation)
relaySlot.captured.invoke(mockk(), mockk())
eventNotifierTunnelRealState.notify(tunnelRealStateTestItem)
val result = awaitItem()
assertEquals(tunnelRealStateTestItem, result.tunnelRealState)
Expand All @@ -190,7 +195,6 @@ class ConnectViewModelTest {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(mockLocation)
relaySlot.captured.invoke(mockk(), mockk())
eventNotifierTunnelUiState.notify(tunnelUiStateTestItem)
val result = awaitItem()
assertEquals(tunnelUiStateTestItem, result.tunnelUiState)
Expand All @@ -202,13 +206,13 @@ class ConnectViewModelTest {
runTest(testCoroutineRule.testDispatcher) {
val relayTestItem =
RelayCountry(name = "Name", code = "Code", expanded = false, cities = emptyList())
relayListWithSelectionFlow.value = emptyList<RelayCountry>() to relayTestItem

viewModel.uiState.test {
assertEquals(ConnectUiState.INITIAL, awaitItem())
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(mockLocation)
relaySlot.captured.invoke(mockk(), relayTestItem)
val result = awaitItem()
assertEquals(relayTestItem, result.relayLocation)
}
Expand All @@ -231,7 +235,6 @@ class ConnectViewModelTest {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(locationTestItem)
relaySlot.captured.invoke(mockk(), mockk())
val result = awaitItem()
assertEquals(locationTestItem, result.location)
}
Expand All @@ -249,7 +252,6 @@ class ConnectViewModelTest {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(locationTestItem)
relaySlot.captured.invoke(mockk(), mockk())
expectNoEvents()
val result = awaitItem()
assertEquals(locationTestItem, result.location)
Expand Down Expand Up @@ -308,7 +310,6 @@ class ConnectViewModelTest {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(mockLocation)
relaySlot.captured.invoke(mockk(), mockk())
eventNotifierTunnelUiState.notify(tunnelUiState)
val result = awaitItem()
assertEquals(expectedConnectNotificationState, result.inAppNotification)
Expand Down Expand Up @@ -347,7 +348,6 @@ class ConnectViewModelTest {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
locationSlot.captured.invoke(mockLocation)
relaySlot.captured.invoke(mockk(), mockk())
eventNotifierTunnelRealState.notify(tunnelRealStateTestItem)
awaitItem()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import app.cash.turbine.test
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.slot
import io.mockk.unmockkAll
import io.mockk.verify
import kotlin.test.assertEquals
Expand All @@ -21,12 +20,9 @@ import net.mullvad.mullvadvpn.relaylist.RelayCountry
import net.mullvad.mullvadvpn.relaylist.RelayItem
import net.mullvad.mullvadvpn.relaylist.filterOnSearchTerm
import net.mullvad.mullvadvpn.ui.serviceconnection.ConnectionProxy
import net.mullvad.mullvadvpn.ui.serviceconnection.RelayListListener
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.connectionProxy
import net.mullvad.mullvadvpn.ui.serviceconnection.relayListListener
import net.mullvad.mullvadvpn.usecase.RelayListUseCase
import org.junit.After
import org.junit.Before
import org.junit.Rule
Expand All @@ -38,28 +34,19 @@ class SelectLocationViewModelTest {
private val mockServiceConnectionManager: ServiceConnectionManager = mockk()
private lateinit var viewModel: SelectLocationViewModel

// Service connections
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
private val mockRelayListListener: RelayListListener = mockk(relaxUnitFun = true)
private val relayListWithSelectionFlow =
MutableStateFlow<Pair<List<RelayCountry>, RelayItem?>>(Pair(emptyList(), null))

// Captures
private val relaySlot = slot<(List<RelayCountry>, RelayItem?) -> Unit>()

private val serviceConnectionState =
MutableStateFlow<ServiceConnectionState>(ServiceConnectionState.Disconnected)
private val mockRelayListUseCase: RelayListUseCase = mockk()

@Before
fun setup() {
every { mockServiceConnectionManager.connectionState } returns serviceConnectionState
every { mockServiceConnectionContainer.relayListListener } returns mockRelayListListener

every { mockRelayListListener.onRelayCountriesChange = capture(relaySlot) } answers {}
every { mockRelayListListener.onRelayCountriesChange = null } answers {}
every { mockRelayListUseCase.relayListWithSelection() } returns relayListWithSelectionFlow

mockkStatic(SERVICE_CONNECTION_MANAGER_EXTENSIONS)
mockkStatic(RELAY_LIST_EXTENSIONS)

viewModel = SelectLocationViewModel(mockServiceConnectionManager)
viewModel = SelectLocationViewModel(mockServiceConnectionManager, mockRelayListUseCase)
}

@After
Expand All @@ -70,7 +57,7 @@ class SelectLocationViewModelTest {

@Test
fun testInitialState() = runTest {
viewModel.uiState.test { assertEquals(SelectLocationUiState.Loading, awaitItem()) }
assertEquals(SelectLocationUiState.Loading, viewModel.uiState.value)
}

@Test
Expand All @@ -79,14 +66,10 @@ class SelectLocationViewModelTest {
val mockCountries = listOf<RelayCountry>(mockk(), mockk())
val selectedRelay: RelayItem = mockk()
every { mockCountries.filterOnSearchTerm(any(), selectedRelay) } returns mockCountries
relayListWithSelectionFlow.value = mockCountries to selectedRelay

// Act, Assert
viewModel.uiState.test {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
relaySlot.captured.invoke(mockCountries, selectedRelay)

assertEquals(SelectLocationUiState.Loading, awaitItem())
val actualState = awaitItem()
assertIs<SelectLocationUiState.ShowData>(actualState)
assertLists(mockCountries, actualState.countries)
Expand All @@ -100,14 +83,10 @@ class SelectLocationViewModelTest {
val mockCountries = listOf<RelayCountry>(mockk(), mockk())
val selectedRelay: RelayItem? = null
every { mockCountries.filterOnSearchTerm(any(), selectedRelay) } returns mockCountries
relayListWithSelectionFlow.value = mockCountries to selectedRelay

// Act, Assert
viewModel.uiState.test {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
relaySlot.captured.invoke(mockCountries, selectedRelay)

assertEquals(SelectLocationUiState.Loading, awaitItem())
val actualState = awaitItem()
assertIs<SelectLocationUiState.ShowData>(actualState)
assertLists(mockCountries, actualState.countries)
Expand All @@ -122,8 +101,8 @@ class SelectLocationViewModelTest {
val mockLocation: GeographicLocationConstraint.Country = mockk(relaxed = true)
val connectionProxyMock: ConnectionProxy = mockk(relaxUnitFun = true)
every { mockRelayItem.location } returns mockLocation
every { mockServiceConnectionManager.relayListListener() } returns mockRelayListListener
every { mockServiceConnectionManager.connectionProxy() } returns connectionProxyMock
every { mockRelayListUseCase.updateSelectedRelayLocation(mockLocation) } returns Unit

// Act, Assert
viewModel.uiCloseAction.test {
Expand All @@ -132,7 +111,7 @@ class SelectLocationViewModelTest {
assertEquals(Unit, awaitItem())
verify {
connectionProxyMock.connect()
mockRelayListListener.updateSelectedRelayLocation(mockLocation)
mockRelayListUseCase.updateSelectedRelayLocation(mockLocation)
}
}
}
Expand All @@ -146,15 +125,10 @@ class SelectLocationViewModelTest {
val mockSearchString = "SEARCH"
every { mockRelayList.filterOnSearchTerm(mockSearchString, selectedRelay) } returns
mockCountries
relayListWithSelectionFlow.value = mockRelayList to selectedRelay

// Act, Assert
viewModel.uiState.test {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
relaySlot.captured.invoke(mockRelayList, selectedRelay)

// Wait for loading
assertEquals(SelectLocationUiState.Loading, awaitItem())
// Wait for first data
assertIs<SelectLocationUiState.ShowData>(awaitItem())

Expand All @@ -178,15 +152,10 @@ class SelectLocationViewModelTest {
val mockSearchString = "SEARCH"
every { mockRelayList.filterOnSearchTerm(mockSearchString, selectedRelay) } returns
mockCountries
relayListWithSelectionFlow.value = mockRelayList to selectedRelay

// Act, Assert
viewModel.uiState.test {
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
relaySlot.captured.invoke(mockRelayList, selectedRelay)

// Wait for loading
assertEquals(SelectLocationUiState.Loading, awaitItem())
// Wait for first data
assertIs<SelectLocationUiState.ShowData>(awaitItem())

Expand Down
Loading

0 comments on commit 040115c

Please sign in to comment.