Skip to content

Commit

Permalink
Fix more things
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Mar 18, 2024
1 parent e2f9650 commit 1be222f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package net.mullvad.mullvadvpn.compose.dialog

import androidx.compose.ui.test.ExperimentalTestApi
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 org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension

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

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

@Test
fun givenNameShouldShowDeleteNameTitle() =
composeExtension.use {
// Arrange
val name = "List should be deleted"
setContentWithTheme { DeleteCustomListConfirmationDialog(name = name) }

// Assert
onNodeWithText(DELETE_TITLE.format(name)).assertExists()
}

@Test
fun whenDeleteIsClickedShouldCallOnDelete() =
composeExtension.use {
// Arrange
val name = "List should be deleted"
val mockedOnDelete: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
DeleteCustomListConfirmationDialog(name = name, onDelete = mockedOnDelete)
}

// Act
onNodeWithText(DELETE_BUTTON_TEXT).performClick()

// Assert
verify { mockedOnDelete.invoke() }
}

@Test
fun whenCancelIsClickedShouldCallOnBack() =
composeExtension.use {
// Arrange
val name = "List should be deleted"
val mockedOnBack: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
DeleteCustomListConfirmationDialog(name = name, onBack = mockedOnBack)
}

// Act
onNodeWithText(CANCEL_BUTTON_TEXT).performClick()

// Assert
verify { mockedOnBack.invoke() }
}

companion object {
private const val DELETE_TITLE = "Delete \"%s\"?"
private const val CANCEL_BUTTON_TEXT = "Cancel"
private const val DELETE_BUTTON_TEXT = "Delete"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class ServerIpOverridesScreenTest {
MockKAnnotations.init(this)
}

@Suppress("TestFunctionName")
@Composable
private fun Screen(
private fun ScreenWithDefault(
state: ServerIpOverridesViewState,
onBackClick: () -> Unit = {},
onInfoClick: () -> Unit = {},
Expand All @@ -53,7 +54,7 @@ class ServerIpOverridesScreenTest {
fun testOverridesInactive() =
composeExtension.use {
// Arrange
setContentWithTheme { Screen(state = ServerIpOverridesViewState(false)) }
setContentWithTheme { ScreenWithDefault(state = ServerIpOverridesViewState(false)) }

// Assert
onNodeWithText("Overrides inactive").assertExists()
Expand All @@ -63,7 +64,7 @@ class ServerIpOverridesScreenTest {
fun testOverridesActive() =
composeExtension.use {
// Arrange
setContentWithTheme { Screen(state = ServerIpOverridesViewState(true)) }
setContentWithTheme { ScreenWithDefault(state = ServerIpOverridesViewState(true)) }

// Assert
onNodeWithText("Overrides active").assertExists()
Expand All @@ -73,7 +74,7 @@ class ServerIpOverridesScreenTest {
fun testOverridesActiveShowsWarningOnImport() =
composeExtension.use {
// Arrange
setContentWithTheme { Screen(state = ServerIpOverridesViewState(true)) }
setContentWithTheme { ScreenWithDefault(state = ServerIpOverridesViewState(true)) }

// Act
onNodeWithTag(testTag = SERVER_IP_OVERRIDE_IMPORT_TEST_TAG).performClick()
Expand All @@ -91,7 +92,10 @@ class ServerIpOverridesScreenTest {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(state = ServerIpOverridesViewState(false), onInfoClick = clickHandler)
ScreenWithDefault(
state = ServerIpOverridesViewState(false),
onInfoClick = clickHandler
)
}

// Act
Expand All @@ -107,8 +111,8 @@ class ServerIpOverridesScreenTest {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(
state = ServerIpOverridesViewState(false),
ScreenWithDefault(
state = ServerIpOverridesViewState(true),
onResetOverridesClick = clickHandler
)
}
Expand All @@ -127,7 +131,10 @@ class ServerIpOverridesScreenTest {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(state = ServerIpOverridesViewState(false), onImportByFile = clickHandler)
ScreenWithDefault(
state = ServerIpOverridesViewState(false),
onImportByFile = clickHandler
)
}

// Act
Expand All @@ -144,7 +151,10 @@ class ServerIpOverridesScreenTest {
// Arrange
val clickHandler: () -> Unit = mockk(relaxed = true)
setContentWithTheme {
Screen(state = ServerIpOverridesViewState(false), onImportByText = clickHandler)
ScreenWithDefault(
state = ServerIpOverridesViewState(false),
onImportByText = clickHandler
)
}

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.NegativeButton
Expand All @@ -24,34 +23,44 @@ import org.koin.androidx.compose.koinViewModel
@Preview
@Composable
private fun PreviewResetServerIpOverridesConfirmationDialog() {
AppTheme { ResetServerIpOverridesConfirmationDialog(EmptyDestinationsNavigator) }
AppTheme { ResetServerIpOverridesConfirmationDialog({}, {}) }
}

@Destination(style = DestinationStyle.Dialog::class)
@Composable
fun ResetServerIpOverridesConfirmationDialog(navigator: DestinationsNavigator) {
val viewModel: ResetServerIpOverridesConfirmationViewModel = koinViewModel()
CollectSideEffectWithLifecycle(viewModel.uiSideEffect) {
fun ResetServerIpOverridesConfirmation(navigator: DestinationsNavigator) {
val vm: ResetServerIpOverridesConfirmationViewModel = koinViewModel()
CollectSideEffectWithLifecycle(vm.uiSideEffect) {
when (it) {
ResetServerIpOverridesConfirmationUiSideEffect.OverridesCleared ->
navigator.navigateUp()
}
}
ResetServerIpOverridesConfirmationDialog(
onClearAllOverrides = vm::clearAllOverrides,
navigator::navigateUp
)
}

@Composable
fun ResetServerIpOverridesConfirmationDialog(
onClearAllOverrides: () -> Unit,
onNavigateBack: () -> Unit
) {
AlertDialog(
containerColor = MaterialTheme.colorScheme.background,
confirmButton = {
NegativeButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(id = R.string.server_ip_overrides_reset_reset_button),
onClick = viewModel::clearAllOverrides
onClick = onClearAllOverrides
)
},
dismissButton = {
PrimaryButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(R.string.cancel),
onClick = navigator::navigateUp
onClick = onNavigateBack
)
},
title = {
Expand All @@ -67,6 +76,6 @@ fun ResetServerIpOverridesConfirmationDialog(navigator: DestinationsNavigator) {
style = MaterialTheme.typography.bodySmall,
)
},
onDismissRequest = navigator::navigateUp
onDismissRequest = onNavigateBack
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import net.mullvad.mullvadvpn.compose.component.MullvadModalBottomSheet
import net.mullvad.mullvadvpn.compose.component.MullvadSnackbar
import net.mullvad.mullvadvpn.compose.component.ScaffoldWithMediumTopBar
import net.mullvad.mullvadvpn.compose.destinations.ImportOverridesByTextDestination
import net.mullvad.mullvadvpn.compose.destinations.ResetServerIpOverridesConfirmationDialogDestination
import net.mullvad.mullvadvpn.compose.destinations.ResetServerIpOverridesConfirmationDestination
import net.mullvad.mullvadvpn.compose.destinations.ServerIpOverridesInfoDialogDestination
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
Expand Down Expand Up @@ -136,7 +136,7 @@ fun ServerIpOverrides(
},
onResetOverridesClick = {
navigator.navigate(
ResetServerIpOverridesConfirmationDialogDestination,
ResetServerIpOverridesConfirmationDestination,
onlyIfResumed = true
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const val SELECT_LOCATION_CUSTOM_LIST_BOTTOM_SHEET_TEST_TAG =
const val SELECT_LOCATION_LOCATION_BOTTOM_SHEET_TEST_TAG =
"select_location_location_bottom_sheet_test_tag"

// ServerIpOverridesScreen
const val SERVER_IP_OVERRIDE_IMPORT_TEST_TAG = "server_ip_override_import_button_test_tag"
const val SERVER_IP_OVERRIDE_INFO_TEST_TAG = "server_ip_override_info_button_test_tag"
const val SERVER_IP_OVERRIDE_MORE_VERT_TEST_TAG = "server_ip_override_more_vert_button_test_tag"
Expand Down

0 comments on commit 1be222f

Please sign in to comment.