Skip to content

Commit 8fd089a

Browse files
MaryamShaghaghiBoki91
authored andcommitted
Add test for split tunneling view model and update the other tests
Co-Authored-By: Boban Sijuk <49131853+Boki91@users.noreply.github.com>
1 parent cdb377a commit 8fd089a

File tree

2 files changed

+138
-49
lines changed

2 files changed

+138
-49
lines changed

android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/SplitTunnelingScreenTest.kt

+47-21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.mockk.unmockkAll
1010
import io.mockk.verify
1111
import net.mullvad.mullvadvpn.applist.AppData
1212
import net.mullvad.mullvadvpn.compose.setContentWithTheme
13+
import net.mullvad.mullvadvpn.compose.state.AppListState
1314
import net.mullvad.mullvadvpn.compose.state.SplitTunnelingUiState
1415
import org.junit.jupiter.api.AfterEach
1516
import org.junit.jupiter.api.BeforeEach
@@ -34,7 +35,12 @@ class SplitTunnelingScreenTest {
3435
fun testLoadingState() =
3536
composeExtension.use {
3637
// Arrange
37-
setContentWithTheme { SplitTunnelingScreen(uiState = SplitTunnelingUiState.Loading) }
38+
setContentWithTheme {
39+
SplitTunnelingScreen(
40+
uiState =
41+
SplitTunnelingUiState(enabled = true, appListState = AppListState.Loading)
42+
)
43+
}
3844

3945
// Assert
4046
onNodeWithText(TITLE).assertExists()
@@ -63,10 +69,14 @@ class SplitTunnelingScreenTest {
6369
setContentWithTheme {
6470
SplitTunnelingScreen(
6571
uiState =
66-
SplitTunnelingUiState.ShowAppList(
67-
excludedApps = listOf(excludedApp),
68-
includedApps = listOf(includedApp),
69-
showSystemApps = false
72+
SplitTunnelingUiState(
73+
enabled = true,
74+
appListState =
75+
AppListState.ShowAppList(
76+
excludedApps = listOf(excludedApp),
77+
includedApps = listOf(includedApp),
78+
showSystemApps = false
79+
)
7080
)
7181
)
7282
}
@@ -94,10 +104,14 @@ class SplitTunnelingScreenTest {
94104
setContentWithTheme {
95105
SplitTunnelingScreen(
96106
uiState =
97-
SplitTunnelingUiState.ShowAppList(
98-
excludedApps = emptyList(),
99-
includedApps = listOf(includedApp),
100-
showSystemApps = false
107+
SplitTunnelingUiState(
108+
enabled = true,
109+
appListState =
110+
AppListState.ShowAppList(
111+
excludedApps = emptyList(),
112+
includedApps = listOf(includedApp),
113+
showSystemApps = false
114+
)
101115
)
102116
)
103117
}
@@ -132,10 +146,14 @@ class SplitTunnelingScreenTest {
132146
setContentWithTheme {
133147
SplitTunnelingScreen(
134148
uiState =
135-
SplitTunnelingUiState.ShowAppList(
136-
excludedApps = listOf(excludedApp),
137-
includedApps = listOf(includedApp),
138-
showSystemApps = false
149+
SplitTunnelingUiState(
150+
enabled = true,
151+
appListState =
152+
AppListState.ShowAppList(
153+
excludedApps = listOf(excludedApp),
154+
includedApps = listOf(includedApp),
155+
showSystemApps = false
156+
)
139157
),
140158
onExcludeAppClick = mockedClickHandler
141159
)
@@ -168,10 +186,14 @@ class SplitTunnelingScreenTest {
168186
setContentWithTheme {
169187
SplitTunnelingScreen(
170188
uiState =
171-
SplitTunnelingUiState.ShowAppList(
172-
excludedApps = listOf(excludedApp),
173-
includedApps = listOf(includedApp),
174-
showSystemApps = false
189+
SplitTunnelingUiState(
190+
enabled = true,
191+
appListState =
192+
AppListState.ShowAppList(
193+
excludedApps = listOf(excludedApp),
194+
includedApps = listOf(includedApp),
195+
showSystemApps = false
196+
)
175197
),
176198
onIncludeAppClick = mockedClickHandler
177199
)
@@ -204,10 +226,14 @@ class SplitTunnelingScreenTest {
204226
setContentWithTheme {
205227
SplitTunnelingScreen(
206228
uiState =
207-
SplitTunnelingUiState.ShowAppList(
208-
excludedApps = listOf(excludedApp),
209-
includedApps = listOf(includedApp),
210-
showSystemApps = false
229+
SplitTunnelingUiState(
230+
enabled = true,
231+
appListState =
232+
AppListState.ShowAppList(
233+
excludedApps = listOf(excludedApp),
234+
includedApps = listOf(includedApp),
235+
showSystemApps = false
236+
)
211237
),
212238
onShowSystemAppsClick = mockedClickHandler
213239
)

android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SplitTunnelingViewModelTest.kt

+91-28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
1919
import kotlinx.coroutines.test.runTest
2020
import net.mullvad.mullvadvpn.applist.AppData
2121
import net.mullvad.mullvadvpn.applist.ApplicationsProvider
22+
import net.mullvad.mullvadvpn.compose.state.AppListState
2223
import net.mullvad.mullvadvpn.compose.state.SplitTunnelingUiState
2324
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
2425
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionContainer
@@ -57,7 +58,7 @@ class SplitTunnelingViewModelTest {
5758
initTestSubject(emptyList())
5859
val actualState: SplitTunnelingUiState = testSubject.uiState.value
5960

60-
val initialExpectedState = SplitTunnelingUiState.Loading
61+
val initialExpectedState = SplitTunnelingUiState()
6162

6263
assertEquals(initialExpectedState, actualState)
6364

@@ -70,12 +71,20 @@ class SplitTunnelingViewModelTest {
7071
{
7172
lambda<(Set<String>) -> Unit>().invoke(emptySet())
7273
}
74+
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
75+
{
76+
lambda<(Boolean) -> Unit>().invoke(true)
77+
}
7378
initTestSubject(emptyList())
7479
val expectedState =
75-
SplitTunnelingUiState.ShowAppList(
76-
excludedApps = emptyList(),
77-
includedApps = emptyList(),
78-
showSystemApps = false
80+
SplitTunnelingUiState(
81+
enabled = true,
82+
appListState =
83+
AppListState.ShowAppList(
84+
excludedApps = emptyList(),
85+
includedApps = emptyList(),
86+
showSystemApps = false
87+
)
7988
)
8089
testSubject.uiState.test { assertEquals(expectedState, awaitItem()) }
8190
}
@@ -88,21 +97,29 @@ class SplitTunnelingViewModelTest {
8897
{
8998
lambda<(Set<String>) -> Unit>().invoke(setOf(appExcluded.packageName))
9099
}
100+
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
101+
{
102+
lambda<(Boolean) -> Unit>().invoke(true)
103+
}
91104

92105
initTestSubject(listOf(appExcluded, appNotExcluded))
93106

94107
val expectedState =
95-
SplitTunnelingUiState.ShowAppList(
96-
excludedApps = listOf(appExcluded),
97-
includedApps = listOf(appNotExcluded),
98-
showSystemApps = false
108+
SplitTunnelingUiState(
109+
enabled = true,
110+
appListState =
111+
AppListState.ShowAppList(
112+
excludedApps = listOf(appExcluded),
113+
includedApps = listOf(appNotExcluded),
114+
showSystemApps = false
115+
)
99116
)
100117

101118
testSubject.uiState.test {
102119
val actualState = awaitItem()
103120
assertEquals(expectedState, actualState)
104121
verifyAll {
105-
mockedSplitTunneling.enabled
122+
mockedSplitTunneling.enabledChange = any()
106123
mockedSplitTunneling.excludedAppsChange = any()
107124
}
108125
}
@@ -118,20 +135,32 @@ class SplitTunnelingViewModelTest {
118135
excludedAppsCallback = lambda()
119136
excludedAppsCallback.invoke(setOf(app.packageName))
120137
}
138+
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
139+
{
140+
lambda<(Boolean) -> Unit>().invoke(true)
141+
}
121142

122143
initTestSubject(listOf(app))
123144

124145
val expectedStateBeforeAction =
125-
SplitTunnelingUiState.ShowAppList(
126-
excludedApps = listOf(app),
127-
includedApps = emptyList(),
128-
showSystemApps = false
146+
SplitTunnelingUiState(
147+
enabled = true,
148+
appListState =
149+
AppListState.ShowAppList(
150+
excludedApps = listOf(app),
151+
includedApps = emptyList(),
152+
showSystemApps = false
153+
)
129154
)
130155
val expectedStateAfterAction =
131-
SplitTunnelingUiState.ShowAppList(
132-
excludedApps = emptyList(),
133-
includedApps = listOf(app),
134-
showSystemApps = false
156+
SplitTunnelingUiState(
157+
enabled = true,
158+
appListState =
159+
AppListState.ShowAppList(
160+
excludedApps = emptyList(),
161+
includedApps = listOf(app),
162+
showSystemApps = false
163+
)
135164
)
136165

137166
testSubject.uiState.test {
@@ -141,7 +170,7 @@ class SplitTunnelingViewModelTest {
141170
assertEquals(expectedStateAfterAction, awaitItem())
142171

143172
verifyAll {
144-
mockedSplitTunneling.enabled
173+
mockedSplitTunneling.enabledChange = any()
145174
mockedSplitTunneling.excludedAppsChange = any()
146175
mockedSplitTunneling.includeApp(app.packageName)
147176
}
@@ -158,21 +187,33 @@ class SplitTunnelingViewModelTest {
158187
excludedAppsCallback = lambda()
159188
excludedAppsCallback.invoke(emptySet())
160189
}
190+
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
191+
{
192+
lambda<(Boolean) -> Unit>().invoke(true)
193+
}
161194

162195
initTestSubject(listOf(app))
163196

164197
val expectedStateBeforeAction =
165-
SplitTunnelingUiState.ShowAppList(
166-
excludedApps = emptyList(),
167-
includedApps = listOf(app),
168-
showSystemApps = false
198+
SplitTunnelingUiState(
199+
enabled = true,
200+
appListState =
201+
AppListState.ShowAppList(
202+
excludedApps = emptyList(),
203+
includedApps = listOf(app),
204+
showSystemApps = false
205+
)
169206
)
170207

171208
val expectedStateAfterAction =
172-
SplitTunnelingUiState.ShowAppList(
173-
excludedApps = listOf(app),
174-
includedApps = emptyList(),
175-
showSystemApps = false
209+
SplitTunnelingUiState(
210+
enabled = true,
211+
appListState =
212+
AppListState.ShowAppList(
213+
excludedApps = listOf(app),
214+
includedApps = emptyList(),
215+
showSystemApps = false
216+
)
176217
)
177218

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

184225
verifyAll {
185-
mockedSplitTunneling.enabled
226+
mockedSplitTunneling.enabledChange = any()
186227
mockedSplitTunneling.excludedAppsChange = any()
187228
mockedSplitTunneling.excludeApp(app.packageName)
188229
}
189230
}
190231
}
191232

233+
@Test
234+
fun test_disabled_state() = runTest {
235+
every { mockedSplitTunneling.excludedAppsChange = captureLambda() } answers
236+
{
237+
lambda<(Set<String>) -> Unit>().invoke(emptySet())
238+
}
239+
every { mockedSplitTunneling.enabledChange = captureLambda() } answers
240+
{
241+
lambda<(Boolean) -> Unit>().invoke(false)
242+
}
243+
244+
initTestSubject(emptyList())
245+
246+
val expectedState =
247+
SplitTunnelingUiState(enabled = false, appListState = AppListState.Disabled)
248+
249+
testSubject.uiState.test {
250+
val actualState = awaitItem()
251+
assertEquals(expectedState, actualState)
252+
}
253+
}
254+
192255
private fun initTestSubject(appList: List<AppData>) {
193256
every { mockedApplicationsProvider.getAppsList() } returns appList
194257
every { mockedServiceConnectionManager.connectionState } returns

0 commit comments

Comments
 (0)