Skip to content

Commit

Permalink
Add test for split tunneling view model and update the other tests
Browse files Browse the repository at this point in the history
Co-Authored-By: Boban Sijuk <[email protected]>
  • Loading branch information
2 people authored and Pururun committed Feb 7, 2024
1 parent cdb377a commit 8fd089a
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.mockk.unmockkAll
import io.mockk.verify
import net.mullvad.mullvadvpn.applist.AppData
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.AppListState
import net.mullvad.mullvadvpn.compose.state.SplitTunnelingUiState
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
Expand All @@ -34,7 +35,12 @@ class SplitTunnelingScreenTest {
fun testLoadingState() =
composeExtension.use {
// Arrange
setContentWithTheme { SplitTunnelingScreen(uiState = SplitTunnelingUiState.Loading) }
setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState(enabled = true, appListState = AppListState.Loading)
)
}

// Assert
onNodeWithText(TITLE).assertExists()
Expand Down Expand Up @@ -63,10 +69,14 @@ class SplitTunnelingScreenTest {
setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
)
)
)
}
Expand Down Expand Up @@ -94,10 +104,14 @@ class SplitTunnelingScreenTest {
setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(includedApp),
showSystemApps = false
)
)
)
}
Expand Down Expand Up @@ -132,10 +146,14 @@ class SplitTunnelingScreenTest {
setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
)
),
onExcludeAppClick = mockedClickHandler
)
Expand Down Expand Up @@ -168,10 +186,14 @@ class SplitTunnelingScreenTest {
setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
)
),
onIncludeAppClick = mockedClickHandler
)
Expand Down Expand Up @@ -204,10 +226,14 @@ class SplitTunnelingScreenTest {
setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
)
),
onShowSystemAppsClick = mockedClickHandler
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.applist.AppData
import net.mullvad.mullvadvpn.applist.ApplicationsProvider
import net.mullvad.mullvadvpn.compose.state.AppListState
import net.mullvad.mullvadvpn.compose.state.SplitTunnelingUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionContainer
Expand Down Expand Up @@ -57,7 +58,7 @@ class SplitTunnelingViewModelTest {
initTestSubject(emptyList())
val actualState: SplitTunnelingUiState = testSubject.uiState.value

val initialExpectedState = SplitTunnelingUiState.Loading
val initialExpectedState = SplitTunnelingUiState()

assertEquals(initialExpectedState, actualState)

Expand All @@ -70,12 +71,20 @@ class SplitTunnelingViewModelTest {
{
lambda<(Set<String>) -> Unit>().invoke(emptySet())
}
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
{
lambda<(Boolean) -> Unit>().invoke(true)
}
initTestSubject(emptyList())
val expectedState =
SplitTunnelingUiState.ShowAppList(
excludedApps = emptyList(),
includedApps = emptyList(),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = emptyList(),
showSystemApps = false
)
)
testSubject.uiState.test { assertEquals(expectedState, awaitItem()) }
}
Expand All @@ -88,21 +97,29 @@ class SplitTunnelingViewModelTest {
{
lambda<(Set<String>) -> Unit>().invoke(setOf(appExcluded.packageName))
}
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
{
lambda<(Boolean) -> Unit>().invoke(true)
}

initTestSubject(listOf(appExcluded, appNotExcluded))

val expectedState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(appExcluded),
includedApps = listOf(appNotExcluded),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(appExcluded),
includedApps = listOf(appNotExcluded),
showSystemApps = false
)
)

testSubject.uiState.test {
val actualState = awaitItem()
assertEquals(expectedState, actualState)
verifyAll {
mockedSplitTunneling.enabled
mockedSplitTunneling.enabledChange = any()
mockedSplitTunneling.excludedAppsChange = any()
}
}
Expand All @@ -118,20 +135,32 @@ class SplitTunnelingViewModelTest {
excludedAppsCallback = lambda()
excludedAppsCallback.invoke(setOf(app.packageName))
}
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
{
lambda<(Boolean) -> Unit>().invoke(true)
}

initTestSubject(listOf(app))

val expectedStateBeforeAction =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(app),
includedApps = emptyList(),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(app),
includedApps = emptyList(),
showSystemApps = false
)
)
val expectedStateAfterAction =
SplitTunnelingUiState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
)
)

testSubject.uiState.test {
Expand All @@ -141,7 +170,7 @@ class SplitTunnelingViewModelTest {
assertEquals(expectedStateAfterAction, awaitItem())

verifyAll {
mockedSplitTunneling.enabled
mockedSplitTunneling.enabledChange = any()
mockedSplitTunneling.excludedAppsChange = any()
mockedSplitTunneling.includeApp(app.packageName)
}
Expand All @@ -158,21 +187,33 @@ class SplitTunnelingViewModelTest {
excludedAppsCallback = lambda()
excludedAppsCallback.invoke(emptySet())
}
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
{
lambda<(Boolean) -> Unit>().invoke(true)
}

initTestSubject(listOf(app))

val expectedStateBeforeAction =
SplitTunnelingUiState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
)
)

val expectedStateAfterAction =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(app),
includedApps = emptyList(),
showSystemApps = false
SplitTunnelingUiState(
enabled = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(app),
includedApps = emptyList(),
showSystemApps = false
)
)

testSubject.uiState.test {
Expand All @@ -182,13 +223,35 @@ class SplitTunnelingViewModelTest {
assertEquals(expectedStateAfterAction, awaitItem())

verifyAll {
mockedSplitTunneling.enabled
mockedSplitTunneling.enabledChange = any()
mockedSplitTunneling.excludedAppsChange = any()
mockedSplitTunneling.excludeApp(app.packageName)
}
}
}

@Test
fun test_disabled_state() = runTest {
every { mockedSplitTunneling.excludedAppsChange = captureLambda() } answers
{
lambda<(Set<String>) -> Unit>().invoke(emptySet())
}
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
{
lambda<(Boolean) -> Unit>().invoke(false)
}

initTestSubject(emptyList())

val expectedState =
SplitTunnelingUiState(enabled = false, appListState = AppListState.Disabled)

testSubject.uiState.test {
val actualState = awaitItem()
assertEquals(expectedState, actualState)
}
}

private fun initTestSubject(appList: List<AppData>) {
every { mockedApplicationsProvider.getAppsList() } returns appList
every { mockedServiceConnectionManager.connectionState } returns
Expand Down

0 comments on commit 8fd089a

Please sign in to comment.