From 1be222fa7178da131c752ada7a78eb4de2f8e502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Mon, 18 Mar 2024 15:54:50 +0100 Subject: [PATCH] Fix more things --- ...ServerIPOverridesConfirmationDialogTest.kt | 76 +++++++++++++++++++ .../screen/ServerIpOverridesScreenTest.kt | 28 ++++--- ...esetServerIpOverridesConfirmationDialog.kt | 25 ++++-- .../compose/screen/ServerIpOverridesScreen.kt | 4 +- .../compose/test/ComposeTestTagConstants.kt | 1 + 5 files changed, 115 insertions(+), 19 deletions(-) create mode 100644 android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIPOverridesConfirmationDialogTest.kt diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIPOverridesConfirmationDialogTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIPOverridesConfirmationDialogTest.kt new file mode 100644 index 000000000000..af3522233823 --- /dev/null +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIPOverridesConfirmationDialogTest.kt @@ -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" + } +} diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreenTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreenTest.kt index 7513597754e9..87b85ee6c835 100644 --- a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreenTest.kt +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreenTest.kt @@ -30,8 +30,9 @@ class ServerIpOverridesScreenTest { MockKAnnotations.init(this) } + @Suppress("TestFunctionName") @Composable - private fun Screen( + private fun ScreenWithDefault( state: ServerIpOverridesViewState, onBackClick: () -> Unit = {}, onInfoClick: () -> Unit = {}, @@ -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() @@ -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() @@ -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() @@ -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 @@ -107,8 +111,8 @@ class ServerIpOverridesScreenTest { // Arrange val clickHandler: () -> Unit = mockk(relaxed = true) setContentWithTheme { - Screen( - state = ServerIpOverridesViewState(false), + ScreenWithDefault( + state = ServerIpOverridesViewState(true), onResetOverridesClick = clickHandler ) } @@ -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 @@ -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 diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIpOverridesConfirmationDialog.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIpOverridesConfirmationDialog.kt index dd3c31ce21ae..2f478d174210 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIpOverridesConfirmationDialog.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/ResetServerIpOverridesConfirmationDialog.kt @@ -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 @@ -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 = { @@ -67,6 +76,6 @@ fun ResetServerIpOverridesConfirmationDialog(navigator: DestinationsNavigator) { style = MaterialTheme.typography.bodySmall, ) }, - onDismissRequest = navigator::navigateUp + onDismissRequest = onNavigateBack ) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreen.kt index 7bc5fbfed98f..848af5bce745 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreen.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ServerIpOverridesScreen.kt @@ -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 @@ -136,7 +136,7 @@ fun ServerIpOverrides( }, onResetOverridesClick = { navigator.navigate( - ResetServerIpOverridesConfirmationDialogDestination, + ResetServerIpOverridesConfirmationDestination, onlyIfResumed = true ) }, diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt index 8d45f1a6f076..6827dc62982b 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt @@ -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"