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 f73477d commit 0ff48ab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class SplitTunnelingScreenTest {
fun testLoadingState() =
composeExtension.use {
// Arrange
setContentWithTheme { SplitTunnelingScreen(uiState = SplitTunnelingUiState.Loading) }
setContentWithTheme {
SplitTunnelingScreen(uiState = SplitTunnelingUiState.Loading(enabled = true))
}

// Assert
onNodeWithText(TITLE).assertExists()
Expand Down Expand Up @@ -64,6 +66,7 @@ class SplitTunnelingScreenTest {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
Expand Down Expand Up @@ -95,6 +98,7 @@ class SplitTunnelingScreenTest {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = emptyList(),
includedApps = listOf(includedApp),
showSystemApps = false
Expand Down Expand Up @@ -133,6 +137,7 @@ class SplitTunnelingScreenTest {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
Expand Down Expand Up @@ -169,6 +174,7 @@ class SplitTunnelingScreenTest {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
Expand Down Expand Up @@ -205,6 +211,7 @@ class SplitTunnelingScreenTest {
SplitTunnelingScreen(
uiState =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = listOf(excludedApp),
includedApps = listOf(includedApp),
showSystemApps = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SplitTunnelingViewModelTest {
initTestSubject(emptyList())
val actualState: SplitTunnelingUiState = testSubject.uiState.value

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

assertEquals(initialExpectedState, actualState)

Expand All @@ -70,9 +70,14 @@ class SplitTunnelingViewModelTest {
{
lambda<(Set<String>) -> Unit>().invoke(emptySet())
}
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
{
lambda<(Boolean) -> Unit>().invoke(true)
}
initTestSubject(emptyList())
val expectedState =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = emptyList(),
includedApps = emptyList(),
showSystemApps = false
Expand All @@ -88,11 +93,16 @@ 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(
enabled = true,
excludedApps = listOf(appExcluded),
includedApps = listOf(appNotExcluded),
showSystemApps = false
Expand All @@ -102,7 +112,7 @@ class SplitTunnelingViewModelTest {
val actualState = awaitItem()
assertEquals(expectedState, actualState)
verifyAll {
mockedSplitTunneling.enabled
mockedSplitTunneling.enabledChange = any()
mockedSplitTunneling.excludedAppsChange = any()
}
}
Expand All @@ -118,17 +128,23 @@ 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(
enabled = true,
excludedApps = listOf(app),
includedApps = emptyList(),
showSystemApps = false
)
val expectedStateAfterAction =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = emptyList(),
includedApps = listOf(app),
showSystemApps = false
Expand All @@ -141,7 +157,7 @@ class SplitTunnelingViewModelTest {
assertEquals(expectedStateAfterAction, awaitItem())

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

initTestSubject(listOf(app))

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

val expectedStateAfterAction =
SplitTunnelingUiState.ShowAppList(
enabled = true,
excludedApps = listOf(app),
includedApps = emptyList(),
showSystemApps = false
Expand All @@ -182,13 +204,34 @@ 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.ShowAppList(enabled = false)

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 0ff48ab

Please sign in to comment.