Skip to content

Commit

Permalink
Fix remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Mar 18, 2024
1 parent 54879de commit 2377535
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package net.mullvad.mullvadvpn.compose.screen

import androidx.compose.runtime.Composable
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.MockKAnnotations
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.test.SERVER_IP_OVERRIDES_IMPORT_BY_FILE_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SERVER_IP_OVERRIDES_IMPORT_BY_TEXT_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SERVER_IP_OVERRIDE_IMPORT_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SERVER_IP_OVERRIDE_INFO_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SERVER_IP_OVERRIDE_MORE_VERT_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SERVER_IP_OVERRIDE_RESET_OVERRIDES_TEST_TAG
import net.mullvad.mullvadvpn.viewmodel.ServerIpOverridesViewState
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension

@ExperimentalTestApi
class ServerIpOverridesScreenTest {
@JvmField @RegisterExtension val composeExtension = createEdgeToEdgeComposeExtension()

@BeforeEach
fun setup() {
MockKAnnotations.init(this)
}

@Composable
private fun Screen(
state: ServerIpOverridesViewState,
onBackClick: () -> Unit = {},
onInfoClick: () -> Unit = {},
onResetOverridesClick: () -> Unit = {},
onImportByFile: () -> Unit = {},
onImportByText: () -> Unit = {},
) {
ServerIpOverridesScreen(
state = state,
onBackClick = onBackClick,
onInfoClick = onInfoClick,
onResetOverridesClick = onResetOverridesClick,
onImportByFile = onImportByFile,
onImportByText = onImportByText
)
}

@Test
fun testOverridesInactive() =
composeExtension.use {
// Arrange
setContentWithTheme { Screen(state = ServerIpOverridesViewState(false)) }

// Assert
onNodeWithText("Overrides inactive").assertExists()
}

@Test
fun testOverridesActive() =
composeExtension.use {
// Arrange
setContentWithTheme { Screen(state = ServerIpOverridesViewState(true)) }

// Assert
onNodeWithText("Overrides active").assertExists()
}

@Test
fun testOverridesActiveShowsWarningOnImport() =
composeExtension.use {
// Arrange
setContentWithTheme { Screen(state = ServerIpOverridesViewState(true)) }

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

// Assert
onNodeWithText(
"Importing new overrides might replace some previously imported overrides."
)
.assertExists()
}

@Test
fun testInfoClick() =
composeExtension.use {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(state = ServerIpOverridesViewState(false), onInfoClick = clickHandler)
}

// Act
onNodeWithTag(SERVER_IP_OVERRIDE_INFO_TEST_TAG).performClick()

// Assert
verify { clickHandler() }
}

@Test
fun testResetClick() =
composeExtension.use {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(
state = ServerIpOverridesViewState(false),
onResetOverridesClick = clickHandler
)
}

// Act
onNodeWithTag(SERVER_IP_OVERRIDE_MORE_VERT_TEST_TAG).performClick()
onNodeWithTag(SERVER_IP_OVERRIDE_RESET_OVERRIDES_TEST_TAG).performClick()

// Assert
verify { clickHandler() }
}

@Test
fun testImportByFile() =
composeExtension.use {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(state = ServerIpOverridesViewState(false), onImportByFile = clickHandler)
}

// Act
onNodeWithTag(SERVER_IP_OVERRIDE_IMPORT_TEST_TAG).performClick()
onNodeWithTag(SERVER_IP_OVERRIDES_IMPORT_BY_FILE_TEST_TAG).performClick()

// Assert
verify { clickHandler() }
}

@Test
fun testImportByText() =
composeExtension.use {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(state = ServerIpOverridesViewState(false), onImportByText = clickHandler)
}

// Act
onNodeWithTag(SERVER_IP_OVERRIDE_IMPORT_TEST_TAG).performClick()
onNodeWithTag(SERVER_IP_OVERRIDES_IMPORT_BY_TEXT_TEST_TAG).performClick()

// Assert
verify { clickHandler() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import net.mullvad.mullvadvpn.R

@Composable
fun InfoIconButton(onClick: () -> Unit, modifier: Modifier = Modifier) {
fun InfoIconButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
contentDescription: String? = null,
iconTint: Color = MaterialTheme.colorScheme.onPrimary
) {
IconButton(modifier = modifier, onClick = onClick) {
Icon(
painter = painterResource(id = R.drawable.icon_info),
contentDescription = null,
tint = MaterialTheme.colorScheme.onPrimary
contentDescription = contentDescription,
tint = iconTint
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ private fun PreviewIconCell() {
@Composable
fun IconCell(
iconId: Int?,
contentDescription: String? = null,
title: String,
modifier: Modifier = Modifier,
contentDescription: String? = null,
titleStyle: TextStyle = MaterialTheme.typography.labelLarge,
titleColor: Color = MaterialTheme.colorScheme.onPrimary,
onClick: () -> Unit = {},
background: Color = MaterialTheme.colorScheme.primary,
enabled: Boolean = true,
enabled: Boolean = true
) {
BaseCell(
headlineContent = {
Expand All @@ -49,6 +50,7 @@ fun IconCell(
},
onCellClicked = onClick,
background = background,
isRowEnabled = enabled
isRowEnabled = enabled,
modifier = modifier
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package net.mullvad.mullvadvpn.compose.cell

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
Expand All @@ -39,43 +33,38 @@ fun ServerIpOverridesCell(
activeColor: Color = MaterialTheme.colorScheme.selected,
inactiveColor: Color = MaterialTheme.colorScheme.error,
) {
Row(
modifier =
modifier
.wrapContentHeight()
.height(IntrinsicSize.Min)
.background(MaterialTheme.colorScheme.primary)
.padding(horizontal = Dimens.sideMargin)
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier =
Modifier.size(Dimens.relayCircleSize)
.background(
color =
when {
active -> activeColor
else -> inactiveColor
},
shape = CircleShape
)
)
Text(
text =
if (active) stringResource(id = R.string.server_ip_overrides_active)
else stringResource(id = R.string.server_ip_overrides_inactive),
color = MaterialTheme.colorScheme.onPrimary,
modifier =
Modifier.weight(1f)
.alpha(
if (active) {
AlphaVisible
} else {
AlphaInactive
}
)
.padding(horizontal = Dimens.smallPadding, vertical = Dimens.mediumPadding)
)
}
BaseCell(
iconView = {
Box(
modifier =
Modifier.size(Dimens.relayCircleSize)
.background(
color =
when {
active -> activeColor
else -> inactiveColor
},
shape = CircleShape
)
)
},
headlineContent = {
Text(
text =
if (active) stringResource(id = R.string.server_ip_overrides_active)
else stringResource(id = R.string.server_ip_overrides_inactive),
color = MaterialTheme.colorScheme.onPrimary,
modifier =
Modifier.weight(1f)
.alpha(
if (active) {
AlphaVisible
} else {
AlphaInactive
}
)
.padding(horizontal = Dimens.smallPadding, vertical = Dimens.mediumPadding)
)
}
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.mullvad.mullvadvpn.compose.dialog

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
Expand All @@ -18,7 +16,6 @@ import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.NegativeButton
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens

@Preview
@Composable
Expand All @@ -34,30 +31,29 @@ fun ResetServerIpOverridesConfirmationDialog(
AlertDialog(
containerColor = MaterialTheme.colorScheme.background,
confirmButton = {
Column(verticalArrangement = Arrangement.spacedBy(Dimens.buttonSpacing)) {
NegativeButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(id = R.string.server_ip_overrides_reset_reset_button),
onClick = { resultNavigator.navigateBack(result = true) }
)

PrimaryButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(R.string.cancel),
onClick = resultNavigator::navigateBack
)
}
NegativeButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(id = R.string.server_ip_overrides_reset_reset_button),
onClick = { resultNavigator.navigateBack(result = true) }
)
},
dismissButton = {
PrimaryButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(R.string.cancel),
onClick = resultNavigator::navigateBack
)
},
title = {
Text(
text = stringResource(id = R.string.server_ip_overrides_reset_title),
color = MaterialTheme.colorScheme.onPrimary
color = MaterialTheme.colorScheme.onBackground
)
},
text = {
Text(
text = stringResource(id = R.string.server_ip_overrides_reset_body),
color = MaterialTheme.colorScheme.onPrimary,
color = MaterialTheme.colorScheme.onBackground,
style = MaterialTheme.typography.bodySmall,
)
},
Expand Down
Loading

0 comments on commit 2377535

Please sign in to comment.