From 67d04230edcec4055b3c29174faa9bdc06554d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Mon, 3 Jun 2024 14:25:18 +0200 Subject: [PATCH] Add test --- .../compose/screen/VpnSettingsScreenTest.kt | 55 +++++++++++++++++++ .../compose/cell/ExpandableComposeCell.kt | 4 +- .../compose/screen/VpnSettingsScreen.kt | 20 +++++-- .../compose/test/ComposeTestTagConstants.kt | 6 +- .../viewmodel/VpnSettingsViewModelTest.kt | 13 +++++ 5 files changed, 92 insertions(+), 6 deletions(-) diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt index ca7a01a0a950..15b0d935b20a 100644 --- a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt @@ -17,6 +17,8 @@ import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_LAST_ITEM_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_QUANTUM_ITEM_OFF_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_QUANTUM_ITEM_ON_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_TEST_TAG +import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG +import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_UDP_OVER_TCP_PORT_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_CUSTOM_PORT_NUMBER_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_CUSTOM_PORT_TEXT_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_PORT_ITEM_X_TEST_TAG @@ -206,6 +208,36 @@ class VpnSettingsScreenTest { onNodeWithContentDescription(LOCAL_DNS_SERVER_WARNING).assertExists() } + @Test + fun testShowTcpOverUdpPortOptions() = + composeExtension.use { + // Arrange + setContentWithTheme { + VpnSettingsScreen( + state = + VpnSettingsUiState.createDefault( + selectedObfuscationPort = Constraint.Only(Port(5001)) + ), + ) + } + + // Act + onNodeWithTag(LAZY_LIST_TEST_TAG) + .performScrollToNode(hasTestTag(LAZY_LIST_UDP_OVER_TCP_PORT_TEST_TAG)) + onNodeWithText("UDP-over-TCP port").performClick() + onNodeWithTag(LAZY_LIST_TEST_TAG) + .performScrollToNode( + hasTestTag(String.format(LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, 5001)) + ) + + // Assert + onNodeWithTagAndText( + testTag = String.format(LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, 5001), + text = "5001" + ) + .assertExists() + } + @Test fun testShowSelectedTunnelQuantumOption() = composeExtension.use { @@ -401,6 +433,29 @@ class VpnSettingsScreenTest { verify { mockedClickHandler.invoke(null, null) } } + @Test + fun testShowObfuscationInfo() = + composeExtension.use { + val mockedNavigateToObfuscationInfo: () -> Unit = mockk(relaxed = true) + + // Arrange + setContentWithTheme { + VpnSettingsScreen( + state = VpnSettingsUiState.createDefault(), + navigateToObfuscationInfo = mockedNavigateToObfuscationInfo + ) + } + + // Act + + onNodeWithTag(LAZY_LIST_TEST_TAG) + .performScrollToNode(hasTestTag(LAZY_LIST_UDP_OVER_TCP_PORT_TEST_TAG)) + onNodeWithText("WireGuard obfuscation").performClick() + + // Assert + verify(exactly = 1) { mockedNavigateToObfuscationInfo() } + } + @Test fun testShowTunnelQuantumInfo() = composeExtension.use { diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/cell/ExpandableComposeCell.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/cell/ExpandableComposeCell.kt index 73a6a5283d36..b19f53445699 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/cell/ExpandableComposeCell.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/cell/ExpandableComposeCell.kt @@ -13,6 +13,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.focus.focusProperties +import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight @@ -46,6 +47,7 @@ fun ExpandableComposeCell( title: String, isExpanded: Boolean, isEnabled: Boolean = true, + testTag: String = "", onCellClicked: (Boolean) -> Unit = {}, onInfoClicked: (() -> Unit)? = null ) { @@ -53,7 +55,7 @@ fun ExpandableComposeCell( val bodyViewModifier = Modifier BaseCell( - modifier = Modifier.focusProperties { canFocus = false }, + modifier = Modifier.testTag(testTag).focusProperties { canFocus = false }, headlineContent = { BaseCellTitle( title = title, diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreen.kt index 46420583c3fb..3332aa264ef8 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreen.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreen.kt @@ -77,6 +77,9 @@ import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_LAST_ITEM_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_QUANTUM_ITEM_OFF_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_QUANTUM_ITEM_ON_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_TEST_TAG +import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG +import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG +import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_UDP_OVER_TCP_PORT_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_CUSTOM_PORT_NUMBER_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_CUSTOM_PORT_TEXT_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_PORT_ITEM_X_TEST_TAG @@ -531,7 +534,8 @@ fun VpnSettingsScreen( itemWithDivider { SelectableCell( title = port.toString(), - testTag = String.format(LAZY_LIST_WIREGUARD_PORT_ITEM_X_TEST_TAG, port), + testTag = + String.format(null, LAZY_LIST_WIREGUARD_PORT_ITEM_X_TEST_TAG, port), isSelected = state.selectedWireguardPort.hasValue(port), onCellClicked = { onWireguardPortSelected(Constraint.Only(Port(port))) } ) @@ -591,7 +595,8 @@ fun VpnSettingsScreen( title = stringResource(R.string.udp_over_tcp_port_title), isExpanded = expandUdp2TcpPortSettings, onInfoClicked = navigateUdp2TcpInfo, - onCellClicked = { expandUdp2TcpPortSettings = !expandUdp2TcpPortSettings } + onCellClicked = { expandUdp2TcpPortSettings = !expandUdp2TcpPortSettings }, + testTag = LAZY_LIST_UDP_OVER_TCP_PORT_TEST_TAG ) } @@ -600,7 +605,8 @@ fun VpnSettingsScreen( SelectableCell( title = stringResource(id = R.string.automatic), isSelected = state.selectedObfuscationPort is Constraint.Any, - onCellClicked = { onObfuscationPortSelected(Constraint.Any) } + onCellClicked = { onObfuscationPortSelected(Constraint.Any) }, + testTag = LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG, ) } @@ -611,7 +617,13 @@ fun VpnSettingsScreen( isSelected = state.selectedObfuscationPort.hasValue(port), onCellClicked = { onObfuscationPortSelected(Constraint.Only(Port(port))) - } + }, + testTag = + String.format( + null, + LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, + port + ) ) } } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt index 8ebdaede335c..0111fc7a4656 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt @@ -9,11 +9,15 @@ const val LAZY_LIST_TEST_TAG = "lazy_list_test_tag" const val LAZY_LIST_LAST_ITEM_TEST_TAG = "lazy_list_last_item_test_tag" const val LAZY_LIST_QUANTUM_ITEM_OFF_TEST_TAG = "lazy_list_quantum_item_off_test_tag" const val LAZY_LIST_QUANTUM_ITEM_ON_TEST_TAG = "lazy_list_quantum_item_on_test_tag" -const val LAZY_LIST_WIREGUARD_PORT_ITEM_X_TEST_TAG = "lazy_list_quantum_item_%d_test_tag" +const val LAZY_LIST_WIREGUARD_PORT_ITEM_X_TEST_TAG = "lazy_list_wireguard_item_%d_test_tag" const val LAZY_LIST_WIREGUARD_CUSTOM_PORT_TEXT_TEST_TAG = "lazy_list_wireguard_custom_port_text_test_tag" const val LAZY_LIST_WIREGUARD_CUSTOM_PORT_NUMBER_TEST_TAG = "lazy_list_wireguard_custom_port_number_test_tag" +const val LAZY_LIST_UDP_OVER_TCP_PORT_TEST_TAG = "lazy_list_udp_over_tcp_port_test_tag" +const val LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG = + "lazy_list_udp_over_tcp_item_automatic_test_tag" +const val LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG = "lazy_list_udp_over_tcp_item_%d_test_tag" const val CUSTOM_PORT_DIALOG_INPUT_TEST_TAG = "custom_port_dialog_input_test_tag" // SelectLocationScreen, ConnectScreen, CustomListLocationsScreen diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VpnSettingsViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VpnSettingsViewModelTest.kt index 29a6c764ba4c..a62b5d4bb318 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VpnSettingsViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VpnSettingsViewModelTest.kt @@ -66,6 +66,19 @@ class VpnSettingsViewModelTest { unmockkAll() } + @Test + fun `onSelectCustomTcpOverUdpPort should invoke setCustomObfuscationPort on SettingsRepository`() = + runTest { + val customPort = Port(5001) + coEvery { + mockSettingsRepository.setCustomObfuscationPort(Constraint.Only(customPort)) + } returns Unit.right() + viewModel.onObfuscationPortSelected(Constraint.Only(customPort)) + coVerify(exactly = 1) { + mockSettingsRepository.setCustomObfuscationPort(Constraint.Only(customPort)) + } + } + @Test fun `onSelectQuantumResistanceSetting should invoke setWireguardQuantumResistant on SettingsRepository`() = runTest {