Skip to content

Commit

Permalink
Rename selected obfuscation and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Sep 16, 2024
1 parent 0341d75 commit cd1eb8a
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class VpnSettingsScreenTest {
VpnSettingsScreen(
state =
VpnSettingsUiState.createDefault(
customWireguardPort = Constraint.Only(Port(4000))
customWireguardPort = Port(4000)
)
)
}
Expand All @@ -339,7 +339,7 @@ class VpnSettingsScreenTest {
state =
VpnSettingsUiState.createDefault(
selectedWireguardPort = Constraint.Only(Port(4000)),
customWireguardPort = Constraint.Only(Port(4000)),
customWireguardPort = Port(4000),
),
onWireguardPortSelected = onWireguardPortSelected,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import androidx.compose.ui.tooling.preview.PreviewParameter
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.preview.SelectObfuscationCellPreviewParameterProvider
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.ObfuscationMode
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.lib.model.SelectedObfuscation
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.lib.theme.color.selected
Expand All @@ -31,13 +31,13 @@ import net.mullvad.mullvadvpn.lib.theme.typeface.listItemText

@Preview
@Composable
private fun PreviewSelectObfuscationCellCell(
private fun PreviewObfuscationCell(
@PreviewParameter(SelectObfuscationCellPreviewParameterProvider::class)
selectedObfuscationCellData: Triple<SelectedObfuscation, Constraint<Port>, Boolean>
selectedObfuscationCellData: Triple<ObfuscationMode, Constraint<Port>, Boolean>
) {
AppTheme {
SelectObfuscationCell(
selectedObfuscation = selectedObfuscationCellData.first,
ObfuscationModeCell(
obfuscationMode = selectedObfuscationCellData.first,
port = selectedObfuscationCellData.second,
isSelected = selectedObfuscationCellData.third,
onSelected = {},
Expand All @@ -47,11 +47,11 @@ private fun PreviewSelectObfuscationCellCell(
}

@Composable
fun SelectObfuscationCell(
selectedObfuscation: SelectedObfuscation,
fun ObfuscationModeCell(
obfuscationMode: ObfuscationMode,
port: Constraint<Port>,
isSelected: Boolean,
onSelected: (SelectedObfuscation) -> Unit,
onSelected: (ObfuscationMode) -> Unit,
onNavigate: () -> Unit = {},
) {
Row(
Expand All @@ -66,9 +66,9 @@ fun SelectObfuscationCell(
titleColor = MaterialTheme.colorScheme.onSurface,
subtitleStyle = MaterialTheme.typography.listItemSubText,
subtitleColor = MaterialTheme.colorScheme.onSurface,
titleText = selectedObfuscation.toTitle(),
titleText = obfuscationMode.toTitle(),
subtitleText = stringResource(id = R.string.port_x, port.toSubTitle()),
onCellClicked = { onSelected(selectedObfuscation) },
onCellClicked = { onSelected(obfuscationMode) },
minHeight = Dimens.cellHeight,
background =
if (isSelected) {
Expand Down Expand Up @@ -101,12 +101,12 @@ fun SelectObfuscationCell(
}

@Composable
private fun SelectedObfuscation.toTitle() =
private fun ObfuscationMode.toTitle() =
when (this) {
SelectedObfuscation.Auto -> stringResource(id = R.string.automatic)
SelectedObfuscation.Off -> stringResource(id = R.string.off)
SelectedObfuscation.Udp2Tcp -> stringResource(id = R.string.upd_over_tcp)
SelectedObfuscation.Shadowsocks -> stringResource(id = R.string.shadowsocks)
ObfuscationMode.Auto -> stringResource(id = R.string.automatic)
ObfuscationMode.Off -> stringResource(id = R.string.off)
ObfuscationMode.Udp2Tcp -> stringResource(id = R.string.upd_over_tcp)
ObfuscationMode.Shadowsocks -> stringResource(id = R.string.shadowsocks)
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package net.mullvad.mullvadvpn.compose.preview

import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.ObfuscationMode
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.lib.model.SelectedObfuscation

class SelectObfuscationCellPreviewParameterProvider :
PreviewParameterProvider<Triple<SelectedObfuscation, Constraint<Port>, Boolean>> {
override val values: Sequence<Triple<SelectedObfuscation, Constraint<Port>, Boolean>> =
PreviewParameterProvider<Triple<ObfuscationMode, Constraint<Port>, Boolean>> {
override val values: Sequence<Triple<ObfuscationMode, Constraint<Port>, Boolean>> =
sequenceOf(
Triple(SelectedObfuscation.Shadowsocks, Constraint.Any, false),
Triple(SelectedObfuscation.Shadowsocks, Constraint.Any, true),
Triple(SelectedObfuscation.Shadowsocks, Constraint.Only(Port(PORT)), false),
Triple(SelectedObfuscation.Shadowsocks, Constraint.Only(Port(PORT)), true),
Triple(SelectedObfuscation.Udp2Tcp, Constraint.Any, false),
Triple(SelectedObfuscation.Udp2Tcp, Constraint.Any, true),
Triple(SelectedObfuscation.Udp2Tcp, Constraint.Only(Port(PORT)), false),
Triple(SelectedObfuscation.Udp2Tcp, Constraint.Only(Port(PORT)), true),
Triple(ObfuscationMode.Shadowsocks, Constraint.Any, false),
Triple(ObfuscationMode.Shadowsocks, Constraint.Any, true),
Triple(ObfuscationMode.Shadowsocks, Constraint.Only(Port(PORT)), false),
Triple(ObfuscationMode.Shadowsocks, Constraint.Only(Port(PORT)), true),
Triple(ObfuscationMode.Udp2Tcp, Constraint.Any, false),
Triple(ObfuscationMode.Udp2Tcp, Constraint.Any, true),
Triple(ObfuscationMode.Udp2Tcp, Constraint.Only(Port(PORT)), false),
Triple(ObfuscationMode.Udp2Tcp, Constraint.Only(Port(PORT)), true),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import net.mullvad.mullvadvpn.compose.cell.MtuComposeCell
import net.mullvad.mullvadvpn.compose.cell.MtuSubtitle
import net.mullvad.mullvadvpn.compose.cell.NavigationComposeCell
import net.mullvad.mullvadvpn.compose.cell.NormalSwitchComposeCell
import net.mullvad.mullvadvpn.compose.cell.SelectObfuscationCell
import net.mullvad.mullvadvpn.compose.cell.ObfuscationModeCell
import net.mullvad.mullvadvpn.compose.cell.SelectableCell
import net.mullvad.mullvadvpn.compose.cell.SwitchComposeSubtitleCell
import net.mullvad.mullvadvpn.compose.communication.DnsDialogResult
Expand Down Expand Up @@ -93,10 +93,10 @@ import net.mullvad.mullvadvpn.compose.util.showSnackbarImmediately
import net.mullvad.mullvadvpn.constant.WIREGUARD_PRESET_PORTS
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.Mtu
import net.mullvad.mullvadvpn.lib.model.ObfuscationMode
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.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.viewmodel.CustomDnsItem
Expand Down Expand Up @@ -129,7 +129,7 @@ private fun PreviewVpnSettings() {
navigateToDns = { _, _ -> },
onToggleDnsClick = {},
onBackClick = {},
onSelectObfuscationSetting = {},
onSelectObfuscationMode = {},
onSelectQuantumResistanceSetting = {},
onWireguardPortSelected = {},
)
Expand Down Expand Up @@ -264,7 +264,7 @@ fun VpnSettings(
},
onToggleDnsClick = vm::onToggleCustomDns,
onBackClick = dropUnlessResumed { navigator.navigateUp() },
onSelectObfuscationSetting = vm::onSelectObfuscationSetting,
onSelectObfuscationMode = vm::onSelectObfuscationMode,
onSelectQuantumResistanceSetting = vm::onSelectQuantumResistanceSetting,
onWireguardPortSelected = vm::onWireguardPortSelected,
navigateToShadowSocksSettings =
Expand Down Expand Up @@ -305,7 +305,7 @@ fun VpnSettingsScreen(
navigateToDns: (index: Int?, address: String?) -> Unit = { _, _ -> },
onToggleDnsClick: (Boolean) -> Unit = {},
onBackClick: () -> Unit = {},
onSelectObfuscationSetting: (selectedObfuscation: SelectedObfuscation) -> Unit = {},
onSelectObfuscationMode: (obfuscationMode: ObfuscationMode) -> Unit = {},
onSelectQuantumResistanceSetting: (quantumResistant: QuantumResistantState) -> Unit = {},
onWireguardPortSelected: (port: Constraint<Port>) -> Unit = {},
navigateToShadowSocksSettings: () -> Unit = {},
Expand Down Expand Up @@ -593,33 +593,33 @@ fun VpnSettingsScreen(
itemWithDivider {
SelectableCell(
title = stringResource(id = R.string.automatic),
isSelected = state.selectedObfuscation == SelectedObfuscation.Auto,
onCellClicked = { onSelectObfuscationSetting(SelectedObfuscation.Auto) },
isSelected = state.obfuscationMode == ObfuscationMode.Auto,
onCellClicked = { onSelectObfuscationMode(ObfuscationMode.Auto) },
)
}
itemWithDivider {
SelectObfuscationCell(
selectedObfuscation = SelectedObfuscation.Shadowsocks,
isSelected = state.selectedObfuscation == SelectedObfuscation.Shadowsocks,
ObfuscationModeCell(
obfuscationMode = ObfuscationMode.Shadowsocks,
isSelected = state.obfuscationMode == ObfuscationMode.Shadowsocks,
port = state.selectedShadowsSocksObfuscationPort,
onSelected = onSelectObfuscationSetting,
onSelected = onSelectObfuscationMode,
onNavigate = navigateToShadowSocksSettings,
)
}
itemWithDivider {
SelectObfuscationCell(
selectedObfuscation = SelectedObfuscation.Udp2Tcp,
isSelected = state.selectedObfuscation == SelectedObfuscation.Udp2Tcp,
ObfuscationModeCell(
obfuscationMode = ObfuscationMode.Udp2Tcp,
isSelected = state.obfuscationMode == ObfuscationMode.Udp2Tcp,
port = state.selectedUdp2TcpObfuscationPort,
onSelected = onSelectObfuscationSetting,
onSelected = onSelectObfuscationMode,
onNavigate = navigateToUdp2TcpSettings,
)
}
itemWithDivider {
SelectableCell(
title = stringResource(id = R.string.off),
isSelected = state.selectedObfuscation == SelectedObfuscation.Off,
onCellClicked = { onSelectObfuscationSetting(SelectedObfuscation.Off) },
isSelected = state.obfuscationMode == ObfuscationMode.Off,
onCellClicked = { onSelectObfuscationMode(ObfuscationMode.Off) },
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package net.mullvad.mullvadvpn.compose.state
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.DefaultDnsOptions
import net.mullvad.mullvadvpn.lib.model.Mtu
import net.mullvad.mullvadvpn.lib.model.ObfuscationMode
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.viewmodel.CustomDnsItem

data class VpnSettingsUiState(
Expand All @@ -17,7 +17,7 @@ data class VpnSettingsUiState(
val isCustomDnsEnabled: Boolean,
val customDnsItems: List<CustomDnsItem>,
val contentBlockersOptions: DefaultDnsOptions,
val selectedObfuscation: SelectedObfuscation,
val obfuscationMode: ObfuscationMode,
val selectedUdp2TcpObfuscationPort: Constraint<Port>,
val selectedShadowsSocksObfuscationPort: Constraint<Port>,
val quantumResistant: QuantumResistantState,
Expand All @@ -39,7 +39,7 @@ data class VpnSettingsUiState(
isCustomDnsEnabled: Boolean = false,
customDnsItems: List<CustomDnsItem> = emptyList(),
contentBlockersOptions: DefaultDnsOptions = DefaultDnsOptions(),
selectedObfuscation: SelectedObfuscation = SelectedObfuscation.Off,
obfuscationMode: ObfuscationMode = ObfuscationMode.Off,
selectedUdp2TcpObfuscationPort: Constraint<Port> = Constraint.Any,
selectedShadowsSocksObfuscationPort: Constraint<Port> = Constraint.Any,
quantumResistant: QuantumResistantState = QuantumResistantState.Off,
Expand All @@ -56,7 +56,7 @@ data class VpnSettingsUiState(
isCustomDnsEnabled,
customDnsItems,
contentBlockersOptions,
selectedObfuscation,
obfuscationMode,
selectedUdp2TcpObfuscationPort,
selectedShadowsSocksObfuscationPort,
quantumResistant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import net.mullvad.mullvadvpn.lib.model.DefaultDnsOptions
import net.mullvad.mullvadvpn.lib.model.DnsOptions
import net.mullvad.mullvadvpn.lib.model.DnsState
import net.mullvad.mullvadvpn.lib.model.Mtu
import net.mullvad.mullvadvpn.lib.model.ObfuscationMode
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.lib.model.QuantumResistantState
import net.mullvad.mullvadvpn.lib.model.SelectedObfuscation
import net.mullvad.mullvadvpn.lib.model.Settings

class SettingsRepository(
Expand Down Expand Up @@ -65,7 +65,7 @@ class SettingsRepository(
suspend fun setWireguardQuantumResistant(value: QuantumResistantState) =
managementService.setWireguardQuantumResistant(value)

suspend fun setObfuscation(value: SelectedObfuscation) = managementService.setObfuscation(value)
suspend fun setObfuscation(value: ObfuscationMode) = managementService.setObfuscation(value)

suspend fun setAutoConnect(isEnabled: Boolean) = managementService.setAutoConnect(isEnabled)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import net.mullvad.mullvadvpn.constant.WIREGUARD_PRESET_PORTS
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.DefaultDnsOptions
import net.mullvad.mullvadvpn.lib.model.DnsState
import net.mullvad.mullvadvpn.lib.model.ObfuscationMode
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.lib.model.QuantumResistantState
import net.mullvad.mullvadvpn.lib.model.SelectedObfuscation
import net.mullvad.mullvadvpn.lib.model.Settings
import net.mullvad.mullvadvpn.lib.model.WireguardConstraints
import net.mullvad.mullvadvpn.repository.RelayListRepository
Expand Down Expand Up @@ -68,8 +68,7 @@ class VpnSettingsViewModel(
customDnsList = settings?.addresses()?.asStringAddressList() ?: listOf(),
contentBlockersOptions =
settings?.contentBlockersSettings() ?: DefaultDnsOptions(),
selectedObfuscation =
settings?.selectedObfuscationSettings() ?: SelectedObfuscation.Off,
obfuscationMode = settings?.selectedObfuscationMode() ?: ObfuscationMode.Off,
selectedUdp2TcpObfuscationPort =
settings?.obfuscationSettings?.udp2tcp?.port ?: Constraint.Any,
selectedShadowsocksObfuscationPort =
Expand Down Expand Up @@ -211,9 +210,9 @@ class VpnSettingsViewModel(
}
}

fun onSelectObfuscationSetting(selectedObfuscation: SelectedObfuscation) {
fun onSelectObfuscationMode(obfuscationMode: ObfuscationMode) {
viewModelScope.launch(dispatcher) {
repository.setObfuscation(selectedObfuscation).onLeft {
repository.setObfuscation(obfuscationMode).onLeft {
_uiSideEffect.send(VpnSettingsSideEffect.ShowToast.GenericError)
}
}
Expand Down Expand Up @@ -289,7 +288,7 @@ class VpnSettingsViewModel(

private fun Settings.contentBlockersSettings() = tunnelOptions.dnsOptions.defaultOptions

private fun Settings.selectedObfuscationSettings() = obfuscationSettings.selectedObfuscation
private fun Settings.selectedObfuscationMode() = obfuscationSettings.selectedObfuscationMode

private fun Settings.getWireguardPort() =
relaySettings.relayConstraints.wireguardConstraints.port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import net.mullvad.mullvadvpn.compose.state.VpnSettingsUiState
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.DefaultDnsOptions
import net.mullvad.mullvadvpn.lib.model.Mtu
import net.mullvad.mullvadvpn.lib.model.ObfuscationMode
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

data class VpnSettingsViewModelState(
val mtuValue: Mtu?,
Expand All @@ -17,7 +17,7 @@ data class VpnSettingsViewModelState(
val isCustomDnsEnabled: Boolean,
val customDnsList: List<CustomDnsItem>,
val contentBlockersOptions: DefaultDnsOptions,
val selectedObfuscation: SelectedObfuscation,
val obfuscationMode: ObfuscationMode,
val selectedUdp2TcpObfuscationPort: Constraint<Port>,
val selectedShadowsocksObfuscationPort: Constraint<Port>,
val quantumResistant: QuantumResistantState,
Expand All @@ -39,7 +39,7 @@ data class VpnSettingsViewModelState(
isCustomDnsEnabled,
customDnsList,
contentBlockersOptions,
selectedObfuscation,
obfuscationMode,
selectedUdp2TcpObfuscationPort,
selectedShadowsocksObfuscationPort,
quantumResistant,
Expand All @@ -59,7 +59,7 @@ data class VpnSettingsViewModelState(
isCustomDnsEnabled = false,
customDnsList = listOf(),
contentBlockersOptions = DefaultDnsOptions(),
selectedObfuscation = SelectedObfuscation.Auto,
obfuscationMode = ObfuscationMode.Auto,
selectedUdp2TcpObfuscationPort = Constraint.Any,
selectedShadowsocksObfuscationPort = Constraint.Any,
quantumResistant = QuantumResistantState.Off,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ class ShadowsocksSettingsViewModelTest {
@Test
fun `uiState should reflect latest port range value from relay list`() = runTest {
// Arrange
val mockSettings: Settings = mockk()
val port = Port(123)
every { mockSettings.obfuscationSettings.shadowsocks.port } returns Constraint.Only(port)
val mockPortRange: List<PortRange> = listOf(mockk())

portRangesFlow.update { mockPortRange }
settingsFlow.update { mockk(relaxed = true) }
settingsFlow.update { mockSettings }

// Act, Assert
viewModel.uiState.test {
Expand Down
Loading

0 comments on commit cd1eb8a

Please sign in to comment.