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 6, 2023
1 parent 127ad3e commit 2531f89
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.asSharedFlow
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.SelectLocationUiState
import net.mullvad.mullvadvpn.compose.test.CIRCULAR_PROGRESS_INDICATOR
import net.mullvad.mullvadvpn.model.Constraint
import net.mullvad.mullvadvpn.model.PortRange
import net.mullvad.mullvadvpn.model.RelayEndpointData
import net.mullvad.mullvadvpn.model.RelayList
Expand Down Expand Up @@ -188,6 +189,6 @@ class SelectLocationScreenTest {
arrayListOf(DUMMY_RELAY_COUNTRY_1, DUMMY_RELAY_COUNTRY_2),
DUMMY_WIREGUARD_ENDPOINT_DATA
)
.toRelayCountries()
.toRelayCountries(ownership = Constraint.Any(), providers = Constraint.Any())
}
}
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,19 @@ 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 selectedRelayFlow = MutableStateFlow<RelayItem?>(null)

@Before
fun setup() {
mockkStatic(CACHE_EXTENSION_CLASS)
Expand All @@ -107,7 +111,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 +127,18 @@ class ConnectViewModelTest {

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

// Flows
every { mockRelayListUseCase.selectedRelayItem() } returns selectedRelayFlow

viewModel =
ConnectViewModel(
serviceConnectionManager = mockServiceConnectionManager,
accountRepository = mockAccountRepository,
deviceRepository = mockDeviceRepository,
inAppNotificationController = mockInAppNotificationController,
relayListUseCase = mockRelayListUseCase,
newDeviceNotificationUseCase = mockk()
)
}
Expand All @@ -156,7 +162,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 +178,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 +194,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 +205,13 @@ class ConnectViewModelTest {
runTest(testCoroutineRule.testDispatcher) {
val relayTestItem =
RelayCountry(name = "Name", code = "Code", expanded = false, cities = emptyList())
selectedRelayFlow.value = 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 +234,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 +251,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 +309,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 +347,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 @@ -19,14 +18,12 @@ import net.mullvad.mullvadvpn.lib.common.test.assertLists
import net.mullvad.mullvadvpn.model.GeographicLocationConstraint
import net.mullvad.mullvadvpn.relaylist.RelayCountry
import net.mullvad.mullvadvpn.relaylist.RelayItem
import net.mullvad.mullvadvpn.relaylist.RelayList
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 +35,18 @@ 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(RelayList(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 = RelayList(mockCountries, 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 = RelayList(mockCountries, 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 = RelayList(mockRelayList, 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 = RelayList(mockRelayList, 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 2531f89

Please sign in to comment.