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
MaryamShaghaghi and Boki91 committed Dec 7, 2023
1 parent 6b8cf0a commit a7fe0fa
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.mockk.verify
import net.mullvad.mullvadvpn.applist.AppData
import net.mullvad.mullvadvpn.applist.ApplicationsIconManager
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.AppListState
import net.mullvad.mullvadvpn.compose.state.SplitTunnelingUiState
import org.junit.After
import org.junit.Before
Expand Down Expand Up @@ -49,7 +50,7 @@ class SplitTunnelingScreenTest {
fun testLoadingState() {
// Arrange
composeTestRule.setContentWithTheme {
SplitTunnelingScreen(uiState = SplitTunnelingUiState.Loading)
SplitTunnelingScreen(uiState = SplitTunnelingUiState())
}

// Assert
Expand All @@ -72,10 +73,13 @@ class SplitTunnelingScreenTest {
composeTestRule.setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
appListState =
AppListState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
)
)
)
}
Expand All @@ -100,10 +104,13 @@ class SplitTunnelingScreenTest {
composeTestRule.setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(includedApp),
showSystemApps = false
)
)
)
}
Expand Down Expand Up @@ -131,10 +138,13 @@ class SplitTunnelingScreenTest {
composeTestRule.setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
appListState =
AppListState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
)
),
onExcludeAppClick = mockedClickHandler
)
Expand All @@ -158,10 +168,13 @@ class SplitTunnelingScreenTest {
composeTestRule.setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
appListState =
AppListState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
)
),
onIncludeAppClick = mockedClickHandler
)
Expand All @@ -185,10 +198,13 @@ class SplitTunnelingScreenTest {
composeTestRule.setContentWithTheme {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
SplitTunnelingUiState(
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 @@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
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 @@ -71,12 +72,21 @@ 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(
checked = true,
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = emptyList(),
showSystemApps = false
)
)
testSubject.uiState.test { assertEquals(expectedState, awaitItem()) }
}
Expand All @@ -90,21 +100,31 @@ class SplitTunnelingViewModelTest {
{
lambda<(Set<String>) -> Unit>().invoke(setOf(appExcluded.packageName))
}
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
{
lambda<(Boolean) -> Unit>().invoke(true)
}

every { mockedSplitTunneling.enableSplitTunneling(any()) } just runs

initTestSubject(listOf(appExcluded, appNotExcluded))

val expectedState =
SplitTunnelingUiState.ShowAppList(
excludedApps = listOf(appExcluded),
includedApps = listOf(appNotExcluded),
showSystemApps = false
SplitTunnelingUiState(
checked = 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 @@ -121,20 +141,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(
checked = true,
appListState =
AppListState.ShowAppList(
excludedApps = listOf(app),
includedApps = emptyList(),
showSystemApps = false
)
)
val expectedStateAfterAction =
SplitTunnelingUiState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
SplitTunnelingUiState(
checked = true,
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
)
)

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

verifyAll {
mockedSplitTunneling.enabled
mockedSplitTunneling.enabledChange = any()
mockedSplitTunneling.excludedAppsChange = any()
mockedSplitTunneling.includeApp(app.packageName)
}
Expand All @@ -162,21 +194,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(
checked = true,
appListState =
AppListState.ShowAppList(
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
)
)

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

testSubject.uiState.test {
Expand All @@ -186,13 +230,36 @@ class SplitTunnelingViewModelTest {
assertEquals(expectedStateAfterAction, awaitItem())

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

@Test
fun test_disabled_state() =
runTest(testCoroutineRule.testDispatcher) {
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(checked = 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 a7fe0fa

Please sign in to comment.