Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jun 3, 2024
1 parent 64c6040 commit 67d0423
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,14 +47,15 @@ fun ExpandableComposeCell(
title: String,
isExpanded: Boolean,
isEnabled: Boolean = true,
testTag: String = "",
onCellClicked: (Boolean) -> Unit = {},
onInfoClicked: (() -> Unit)? = null
) {
val titleModifier = Modifier.alpha(if (isEnabled) AlphaVisible else AlphaInactive)
val bodyViewModifier = Modifier

BaseCell(
modifier = Modifier.focusProperties { canFocus = false },
modifier = Modifier.testTag(testTag).focusProperties { canFocus = false },
headlineContent = {
BaseCellTitle(
title = title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))) }
)
Expand Down Expand Up @@ -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
)
}

Expand All @@ -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,
)
}

Expand All @@ -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
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 67d0423

Please sign in to comment.