Skip to content

Commit

Permalink
Move and fixed some ui tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Aug 26, 2024
1 parent 85f4ca0 commit 73b5ce3
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ private val DUMMY_RELAY_COUNTRY_2 =
)

private val DUMMY_WIREGUARD_PORT_RANGES = ArrayList<PortRange>()
private val DUMMY_WIREGUARD_ENDPOINT_DATA = WireguardEndpointData(DUMMY_WIREGUARD_PORT_RANGES)
private val DUMMY_SHADOWSOCKS_PORT_RANGES = emptyList<PortRange>()
private val DUMMY_WIREGUARD_ENDPOINT_DATA =
WireguardEndpointData(DUMMY_WIREGUARD_PORT_RANGES, DUMMY_SHADOWSOCKS_PORT_RANGES)

val DUMMY_RELAY_COUNTRIES = listOf(DUMMY_RELAY_COUNTRY_1, DUMMY_RELAY_COUNTRY_2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ class CustomPortDialogTest {
@SuppressLint("ComposableNaming")
@Composable
private fun testWireguardCustomPortDialog(
title: String = "",
initialPort: Port? = null,
allowedPortRanges: List<PortRange> = emptyList(),
onSave: (Port?) -> Unit = { _ -> },
onDismiss: () -> Unit = {},
) {

WireguardCustomPortDialog(
CustomPortDialog(
title = title,
initialPort = initialPort,
allowedPortRanges = allowedPortRanges,
onSave = onSave,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package net.mullvad.mullvadvpn.compose.screen

import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import net.mullvad.mullvadvpn.compose.createEdgeToEdgeComposeExtension
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.ShadowsocksSettingsState
import net.mullvad.mullvadvpn.compose.test.SHADOWSOCKS_CUSTOM_PORT_TEXT_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SHADOWSOCKS_PORT_ITEM_X_TEST_TAG
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.onNodeWithTagAndText
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension

@OptIn(ExperimentalTestApi::class)
class ShadowsocksSettingsScreenTest {
@JvmField @RegisterExtension val composeExtension = createEdgeToEdgeComposeExtension()

@Test
fun testSelectTcpOverUdpPortOption() =
composeExtension.use {
// Arrange
val onObfuscationPortSelected: (Constraint<Port>) -> Unit = mockk(relaxed = true)
setContentWithTheme {
ShadowsocksSettingsScreen(
state = ShadowsocksSettingsState(port = Constraint.Any),
onObfuscationPortSelected = onObfuscationPortSelected
)
}

// Act
onNodeWithTagAndText(
testTag = String.format(SHADOWSOCKS_PORT_ITEM_X_TEST_TAG, 443),
text = "443"
)
.assertExists()
.performClick()

// Assert
coVerify(exactly = 1) { onObfuscationPortSelected.invoke(Constraint.Only(Port(443))) }
}

@Test
fun testShowShadowsocksCustomPort() =
composeExtension.use {
// Arrange
setContentWithTheme {
ShadowsocksSettingsScreen(
state = ShadowsocksSettingsState(customPort = Port(4000)),
)
}

// Assert
onNodeWithText("4000").assertExists()
}

@Test
fun testSelectShadowsocksCustomPort() =
composeExtension.use {
// Arrange
val onObfuscationPortSelected: (Constraint<Port>) -> Unit = mockk(relaxed = true)
setContentWithTheme {
ShadowsocksSettingsScreen(
state =
ShadowsocksSettingsState(
port = Constraint.Only(Port(4000)),
customPort = Port(4000)
),
onObfuscationPortSelected = onObfuscationPortSelected
)
}

// Act
onNodeWithTag(testTag = SHADOWSOCKS_CUSTOM_PORT_TEXT_TEST_TAG).performClick()

// Assert
verify { onObfuscationPortSelected.invoke(Constraint.Only(Port(4000))) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.mullvad.mullvadvpn.compose.screen

import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.performClick
import io.mockk.coVerify
import io.mockk.mockk
import net.mullvad.mullvadvpn.compose.createEdgeToEdgeComposeExtension
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.Udp2TcpSettingsState
import net.mullvad.mullvadvpn.compose.test.UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.onNodeWithTagAndText
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension

@OptIn(ExperimentalTestApi::class)
class Udp2TcpSettingsScreenTest {
@JvmField @RegisterExtension val composeExtension = createEdgeToEdgeComposeExtension()

@Test
fun testSelectTcpOverUdpPortOption() =
composeExtension.use {
// Arrange
val onObfuscationPortSelected: (Constraint<Port>) -> Unit = mockk(relaxed = true)
setContentWithTheme {
Udp2TcpSettingsScreen(
state = Udp2TcpSettingsState(port = Constraint.Any),
onObfuscationPortSelected = onObfuscationPortSelected
)
}

// Act
onNodeWithTagAndText(
testTag = String.format(UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, 5001),
text = "5001"
)
.assertExists()
.performClick()

// Assert
coVerify(exactly = 1) { onObfuscationPortSelected.invoke(Constraint.Only(Port(5001))) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToNode
import io.mockk.MockKAnnotations
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import net.mullvad.mullvadvpn.compose.createEdgeToEdgeComposeExtension
Expand All @@ -18,8 +17,6 @@ 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 All @@ -28,7 +25,6 @@ import net.mullvad.mullvadvpn.lib.model.Mtu
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.lib.model.PortRange
import net.mullvad.mullvadvpn.lib.model.QuantumResistantState
import net.mullvad.mullvadvpn.lib.model.SelectedObfuscation
import net.mullvad.mullvadvpn.onNodeWithTagAndText
import net.mullvad.mullvadvpn.viewmodel.CustomDnsItem
import org.junit.jupiter.api.BeforeEach
Expand Down Expand Up @@ -210,74 +206,6 @@ class VpnSettingsScreenTest {
onNodeWithContentDescription(LOCAL_DNS_SERVER_WARNING).assertExists()
}

@Test
fun testSelectTcpOverUdpPortOption() =
composeExtension.use {
// Arrange
val onObfuscationPortSelected: (Constraint<Port>) -> Unit = mockk(relaxed = true)
setContentWithTheme {
VpnSettingsScreen(
state =
VpnSettingsUiState.createDefault(
selectedObfuscation = SelectedObfuscation.Udp2Tcp,
selectedObfuscationPort = Constraint.Only(Port(5001))
),
onObfuscationPortSelected = onObfuscationPortSelected
)
}

// 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()
.performClick()

coVerify(exactly = 1) { onObfuscationPortSelected.invoke(Constraint.Only(Port(5001))) }
}

@Test
fun testAttemptSelectTcpOverUdpPortOption() =
composeExtension.use {
// Arrange
val onObfuscationPortSelected: (Constraint<Port>) -> Unit = mockk(relaxed = true)
setContentWithTheme {
VpnSettingsScreen(
state =
VpnSettingsUiState.createDefault(
selectedObfuscation = SelectedObfuscation.Off,
),
onObfuscationPortSelected = onObfuscationPortSelected
)
}

// 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
onNodeWithTag(String.format(LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, 5001))
.assertExists()
.performClick()

verify(exactly = 0) { onObfuscationPortSelected.invoke(any()) }
}

@Test
fun testShowSelectedTunnelQuantumOption() =
composeExtension.use {
Expand Down Expand Up @@ -487,9 +415,6 @@ class VpnSettingsScreenTest {
}

// Act

onNodeWithTag(LAZY_LIST_TEST_TAG)
.performScrollToNode(hasTestTag(LAZY_LIST_UDP_OVER_TCP_PORT_TEST_TAG))
onNodeWithText("WireGuard obfuscation").performClick()

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import net.mullvad.mullvadvpn.compose.component.ScaffoldWithMediumTopBar
import net.mullvad.mullvadvpn.compose.dialog.CustomPortNavArgs
import net.mullvad.mullvadvpn.compose.extensions.itemWithDivider
import net.mullvad.mullvadvpn.compose.state.ShadowsocksSettingsState
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.SHADOWSOCKS_CUSTOM_PORT_TEXT_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SHADOWSOCKS_PORT_ITEM_AUTOMATIC_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SHADOWSOCKS_PORT_ITEM_X_TEST_TAG
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromRightTransition
import net.mullvad.mullvadvpn.compose.util.OnNavResultValue
import net.mullvad.mullvadvpn.constant.SHADOWSOCKS_PRESET_PORTS
Expand Down Expand Up @@ -102,7 +103,7 @@ fun ShadowsocksSettingsScreen(
title = stringResource(id = R.string.automatic),
isSelected = state.port is Constraint.Any,
onCellClicked = { onObfuscationPortSelected(Constraint.Any) },
testTag = LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG,
testTag = SHADOWSOCKS_PORT_ITEM_AUTOMATIC_TEST_TAG,
)
}
itemWithDivider {
Expand All @@ -111,8 +112,7 @@ fun ShadowsocksSettingsScreen(
title = port.toString(),
isSelected = state.port.hasValue(port),
onCellClicked = { onObfuscationPortSelected(Constraint.Only(Port(port))) },
testTag =
String.format(null, LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, port)
testTag = String.format(null, SHADOWSOCKS_PORT_ITEM_X_TEST_TAG, port)
)
}
}
Expand All @@ -129,6 +129,7 @@ fun ShadowsocksSettingsScreen(
}
},
onPortCellClicked = navigateToCustomPortDialog,
mainTestTag = SHADOWSOCKS_CUSTOM_PORT_TEXT_TEST_TAG
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import net.mullvad.mullvadvpn.compose.component.NavigateBackIconButton
import net.mullvad.mullvadvpn.compose.component.ScaffoldWithMediumTopBar
import net.mullvad.mullvadvpn.compose.extensions.itemWithDivider
import net.mullvad.mullvadvpn.compose.state.Udp2TcpSettingsState
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.UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromRightTransition
import net.mullvad.mullvadvpn.constant.UDP2TCP_PRESET_PORTS
import net.mullvad.mullvadvpn.lib.model.Constraint
Expand Down Expand Up @@ -72,7 +72,7 @@ fun Udp2TcpSettingsScreen(
title = stringResource(id = R.string.automatic),
isSelected = state.port is Constraint.Any,
onCellClicked = { onObfuscationPortSelected(Constraint.Any) },
testTag = LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG,
testTag = UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG,
)
}
itemWithDivider {
Expand All @@ -81,8 +81,7 @@ fun Udp2TcpSettingsScreen(
title = port.toString(),
isSelected = state.port.hasValue(port),
onCellClicked = { onObfuscationPortSelected(Constraint.Only(Port(port))) },
testTag =
String.format(null, LAZY_LIST_UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, port)
testTag = String.format(null, UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG, port)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ 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 LAZY_LIST_SHADOWSOCKS_PORT_ITEM_AUTOMATIC_TEST_TAG =
"lazy_list_shadowsocks_item_automatic_test_tag"
const val LAZY_LIST_SHADOWSOCKS_PORT_ITEM_X_TEST_TAG = "lazy_list_shadowsocks_item_%d_test_tag"
const val CUSTOM_PORT_DIALOG_INPUT_TEST_TAG = "custom_port_dialog_input_test_tag"

// SelectLocationScreen, ConnectScreen, CustomListLocationsScreen
Expand Down Expand Up @@ -105,3 +98,12 @@ const val API_ACCESS_TEST_METHOD_BUTTON = "api_access_details_test_method_test_t

// EditApiAccessMethodScreen
const val EDIT_API_ACCESS_NAME_INPUT = "edit_api_access_name_input"

// Udp2TcpSettingScreen
const val UDP_OVER_TCP_PORT_ITEM_AUTOMATIC_TEST_TAG = "udp_over_tcp_item_automatic_test_tag"
const val UDP_OVER_TCP_PORT_ITEM_X_TEST_TAG = "udp_over_tcp_item_%d_test_tag"

// ShadowsocksSettingsScreen
const val SHADOWSOCKS_PORT_ITEM_AUTOMATIC_TEST_TAG = "shadowsocks_item_automatic_test_tag"
const val SHADOWSOCKS_PORT_ITEM_X_TEST_TAG = "shadowsocks_item_%d_test_tag"
const val SHADOWSOCKS_CUSTOM_PORT_TEXT_TEST_TAG = "shadowsocks_custom_port_text_test_tag"
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.mullvad.mullvadvpn.util

import net.mullvad.mullvadvpn.constant.WIREGUARD_PRESET_PORTS
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.lib.model.PortRange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ class Udp2TcpSettingsViewModel(private val repository: SettingsRepository) : Vie
)

fun onObfuscationPortSelected(port: Constraint<Port>) {
viewModelScope.launch { repository.setCustomUdp2TcpObfuscationPort(port).onLeft {
Logger.e("Select udp to tcp port error $it")
} }
viewModelScope.launch {
repository.setCustomUdp2TcpObfuscationPort(port).onLeft {
Logger.e("Select udp to tcp port error $it")
}
}
}
}

0 comments on commit 73b5ce3

Please sign in to comment.