From 862d9b5c9472e83a25fa4c9479ca868fb6f31c81 Mon Sep 17 00:00:00 2001 From: Brian Yencho Date: Wed, 24 Jan 2024 10:51:07 -0600 Subject: [PATCH] Rename URI detection method related VM and Screen items add add tests (#750) --- .../settings/autofill/AutoFillScreen.kt | 20 ++++---- .../settings/autofill/AutoFillViewModel.kt | 16 +++--- .../settings/autofill/AutoFillScreenTest.kt | 49 +++++++++++++++++-- .../autofill/AutoFillViewModelTest.kt | 11 +++-- 4 files changed, 69 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreen.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreen.kt index 2e9800f3a0..dfc891222b 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreen.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreen.kt @@ -172,10 +172,10 @@ fun AutoFillScreen( .fillMaxWidth() .padding(horizontal = 16.dp), ) - UriMatchDetectionDialog( - selectedUriDetection = state.uriDetectionMethod, - onDetectionSelect = remember(viewModel) { - { viewModel.trySendAction(AutoFillAction.UriDetectionMethodSelect(it)) } + DefaultUriMatchTypeRow( + selectedUriMatchType = state.defaultUriMatchType, + onUriMatchTypeSelect = remember(viewModel) { + { viewModel.trySendAction(AutoFillAction.DefaultUriMatchTypeSelect(it)) } }, ) BitwardenTextRow( @@ -194,9 +194,9 @@ fun AutoFillScreen( } @Composable -private fun UriMatchDetectionDialog( - selectedUriDetection: UriMatchType, - onDetectionSelect: (UriMatchType) -> Unit, +private fun DefaultUriMatchTypeRow( + selectedUriMatchType: UriMatchType, + onUriMatchTypeSelect: (UriMatchType) -> Unit, ) { var shouldShowDialog by rememberSaveable { mutableStateOf(false) } @@ -207,7 +207,7 @@ private fun UriMatchDetectionDialog( modifier = Modifier.fillMaxWidth(), ) { Text( - text = selectedUriDetection.displayLabel(), + text = selectedUriMatchType.displayLabel(), style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) @@ -222,10 +222,10 @@ private fun UriMatchDetectionDialog( uriMatchTypes.forEach { option -> BitwardenSelectionRow( text = option.displayLabel, - isSelected = option == selectedUriDetection, + isSelected = option == selectedUriMatchType, onClick = { shouldShowDialog = false - onDetectionSelect( + onUriMatchTypeSelect( uriMatchTypes.first { it == option }, ) }, diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModel.kt index 82c611d1c5..b7c6248426 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModel.kt @@ -33,7 +33,7 @@ class AutoFillViewModel @Inject constructor( isAutoFillServicesEnabled = settingsRepository.isAutofillEnabledStateFlow.value, isCopyTotpAutomaticallyEnabled = false, isUseInlineAutoFillEnabled = settingsRepository.isInlineAutofillEnabled, - uriDetectionMethod = settingsRepository.defaultUriMatchType, + defaultUriMatchType = settingsRepository.defaultUriMatchType, ), ) { @@ -56,7 +56,7 @@ class AutoFillViewModel @Inject constructor( is AutoFillAction.AutoFillServicesClick -> handleAutoFillServicesClick(action) AutoFillAction.BackClick -> handleBackClick() is AutoFillAction.CopyTotpAutomaticallyClick -> handleCopyTotpAutomaticallyClick(action) - is AutoFillAction.UriDetectionMethodSelect -> handleUriDetectionMethodSelect(action) + is AutoFillAction.DefaultUriMatchTypeSelect -> handleDefaultUriMatchTypeSelect(action) AutoFillAction.BlockAutoFillClick -> handleBlockAutoFillClick() is AutoFillAction.UseInlineAutofillClick -> handleUseInlineAutofillClick(action) is AutoFillAction.Internal.AutofillEnabledUpdateReceive -> { @@ -95,10 +95,10 @@ class AutoFillViewModel @Inject constructor( mutableStateFlow.update { it.copy(isUseInlineAutoFillEnabled = action.isEnabled) } } - private fun handleUriDetectionMethodSelect(action: AutoFillAction.UriDetectionMethodSelect) { - settingsRepository.defaultUriMatchType = action.uriDetectionMethod + private fun handleDefaultUriMatchTypeSelect(action: AutoFillAction.DefaultUriMatchTypeSelect) { + settingsRepository.defaultUriMatchType = action.defaultUriMatchType mutableStateFlow.update { - it.copy(uriDetectionMethod = action.uriDetectionMethod) + it.copy(defaultUriMatchType = action.defaultUriMatchType) } } @@ -124,7 +124,7 @@ data class AutoFillState( val isAutoFillServicesEnabled: Boolean, val isCopyTotpAutomaticallyEnabled: Boolean, val isUseInlineAutoFillEnabled: Boolean, - val uriDetectionMethod: UriMatchType, + val defaultUriMatchType: UriMatchType, ) : Parcelable { /** @@ -195,8 +195,8 @@ sealed class AutoFillAction { /** * User selected a [UriMatchType]. */ - data class UriDetectionMethodSelect( - val uriDetectionMethod: UriMatchType, + data class DefaultUriMatchTypeSelect( + val defaultUriMatchType: UriMatchType, ) : AutoFillAction() /** diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreenTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreenTest.kt index ce710e51ab..5ab2c78cd1 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreenTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillScreenTest.kt @@ -232,7 +232,8 @@ class AutoFillScreenTest : BaseComposeTest() { } @Test - fun `on default URI match detection toggle should display dialog`() { + fun `on default URI match type click should display dialog`() { + composeTestRule.assertNoDialogExists() composeTestRule .onNodeWithText("Default URI match detection") .performScrollTo() @@ -244,8 +245,48 @@ class AutoFillScreenTest : BaseComposeTest() { .assertExists() } + @Suppress("MaxLineLength") @Test - fun `default URI match detection add login should be updated on or off according to state`() { + fun `on default URI match type dialog item click should send DefaultUriMatchTypeSelect and close the dialog`() { + composeTestRule + .onNodeWithText("Default URI match detection") + .performScrollTo() + .performClick() + + composeTestRule + .onAllNodesWithText("Exact") + .filterToOne(hasAnyAncestor(isDialog())) + .performClick() + + verify { + viewModel.trySendAction( + AutoFillAction.DefaultUriMatchTypeSelect( + defaultUriMatchType = UriMatchType.EXACT, + ), + ) + } + composeTestRule.assertNoDialogExists() + } + + @Suppress("MaxLineLength") + @Test + fun `on default URI match type dialog cancel click should close the dialog`() { + composeTestRule + .onNodeWithText("Default URI match detection") + .performScrollTo() + .performClick() + + composeTestRule + .onAllNodesWithText("Cancel") + .filterToOne(hasAnyAncestor(isDialog())) + .performClick() + + verify(exactly = 0) { viewModel.trySendAction(any()) } + composeTestRule.assertNoDialogExists() + } + + @Test + fun `default URI match type should update according to state`() { composeTestRule .onNodeWithText("Base domain") .assertExists() @@ -253,7 +294,7 @@ class AutoFillScreenTest : BaseComposeTest() { .onNodeWithText("Starts with") .assertDoesNotExist() mutableStateFlow.update { - it.copy(uriDetectionMethod = UriMatchType.STARTS_WITH) + it.copy(defaultUriMatchType = UriMatchType.STARTS_WITH) } composeTestRule .onNodeWithText("Base domain") @@ -296,5 +337,5 @@ private val DEFAULT_STATE: AutoFillState = AutoFillState( isAutoFillServicesEnabled = false, isCopyTotpAutomaticallyEnabled = false, isUseInlineAutoFillEnabled = false, - uriDetectionMethod = UriMatchType.DOMAIN, + defaultUriMatchType = UriMatchType.DOMAIN, ) diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModelTest.kt index 2780aba007..7fef7a0fc2 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/platform/feature/settings/autofill/AutoFillViewModelTest.kt @@ -39,7 +39,7 @@ class AutoFillViewModelTest : BaseViewModelTest() { mutableIsAutofillEnabledStateFlow.value = true val state = DEFAULT_STATE.copy( isAutoFillServicesEnabled = true, - uriDetectionMethod = UriMatchType.REGULAR_EXPRESSION, + defaultUriMatchType = UriMatchType.REGULAR_EXPRESSION, ) val viewModel = createViewModel(state = state) assertEquals(state, viewModel.stateFlow.value) @@ -142,13 +142,14 @@ class AutoFillViewModelTest : BaseViewModelTest() { verify { settingsRepository.isInlineAutofillEnabled = false } } + @Suppress("MaxLineLength") @Test - fun `on UriDetectionMethodSelect should update the state and save the new value to settings`() { + fun `on DefaultUriMatchTypeSelect should update the state and save the new value to settings`() { val viewModel = createViewModel() val method = UriMatchType.EXACT - viewModel.trySendAction(AutoFillAction.UriDetectionMethodSelect(method)) + viewModel.trySendAction(AutoFillAction.DefaultUriMatchTypeSelect(method)) assertEquals( - DEFAULT_STATE.copy(uriDetectionMethod = method), + DEFAULT_STATE.copy(defaultUriMatchType = method), viewModel.stateFlow.value, ) verify { settingsRepository.defaultUriMatchType = method } @@ -176,5 +177,5 @@ private val DEFAULT_STATE: AutoFillState = AutoFillState( isAutoFillServicesEnabled = false, isCopyTotpAutomaticallyEnabled = false, isUseInlineAutoFillEnabled = true, - uriDetectionMethod = UriMatchType.DOMAIN, + defaultUriMatchType = UriMatchType.DOMAIN, )