Skip to content

Commit 65d3f11

Browse files
committed
Fix and add tests
1 parent a44defd commit 65d3f11

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

app/src/androidTest/java/com/pcapplusplus/toyvpn/ToyVpnServiceManagerTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,14 @@ class ToyVpnServiceManagerTest {
128128
@Test
129129
fun testReceiveVpnStartedAndStoppedEvent() =
130130
runTest {
131-
context.sendBroadcast(Intent(BroadcastActions.VPN_SERVICE_STARTED))
131+
val intent =
132+
Intent(BroadcastActions.VPN_SERVICE_STARTED).apply {
133+
putExtra("clientAddress", "10.0.0.1")
134+
}
135+
136+
context.sendBroadcast(intent)
132137
waitFor { serviceManager.vpnServiceState.value == VpnConnectionState.CONNECTED }
138+
waitFor { serviceManager.clientAddress.value == "10.0.0.1" }
133139

134140
context.sendBroadcast(Intent(BroadcastActions.VPN_SERVICE_STOPPED))
135141
waitFor { serviceManager.vpnServiceState.value == VpnConnectionState.DISCONNECTED }

app/src/androidTest/java/com/pcapplusplus/toyvpn/ToyVpnViewModelTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.junit.runner.RunWith
2222
class ToyVpnViewModelTest {
2323
private val mockVpnServiceManager = mockk<ToyVpnServiceManager>(relaxed = true)
2424
private var vpnConnectionState = MutableStateFlow(VpnConnectionState.DISCONNECTED)
25+
private var clientAddress = MutableStateFlow<String?>(null)
2526
private var vpnConnectionError = MutableStateFlow<String?>(null)
2627
private lateinit var viewModel: ToyVpnViewModel
2728

@@ -31,6 +32,7 @@ class ToyVpnViewModelTest {
3132
@Before
3233
fun setup() {
3334
every { mockVpnServiceManager.vpnServiceState } returns vpnConnectionState.asStateFlow()
35+
every { mockVpnServiceManager.clientAddress } returns clientAddress.asStateFlow()
3436
every { mockVpnServiceManager.vpnConnectionError } returns vpnConnectionError.asStateFlow()
3537
viewModel = ToyVpnViewModel(mockVpnServiceManager)
3638
}
@@ -40,6 +42,8 @@ class ToyVpnViewModelTest {
4042
viewModel.onPacketDataArrives(arrayListOf(PacketData(isIPv4 = true, length = 10)))
4143
assertEquals(1, viewModel.ipv4PacketCount.value)
4244

45+
val mockClientAddress = "10.0.0.1"
46+
clientAddress.value = mockClientAddress
4347
val serverAddress = "12.13.14.15"
4448
val serverPort = 8001
4549
val secret = "secret"
@@ -214,4 +218,13 @@ class ToyVpnViewModelTest {
214218
vpnConnectionError.value = "Some error"
215219
waitFor { viewModel.vpnConnectionError.value == "Some error" }
216220
}
221+
222+
@Test
223+
fun testClientAddress() =
224+
runTest {
225+
assertNull(viewModel.clientAddress.value)
226+
227+
clientAddress.value = "10.0.0.1"
228+
waitFor { viewModel.clientAddress.value == "10.0.0.1" }
229+
}
217230
}

0 commit comments

Comments
 (0)