From 49411f3e2f8aafcd4bcf381dc852925b0733ce59 Mon Sep 17 00:00:00 2001 From: David Perez Date: Tue, 9 Jan 2024 20:35:05 -0600 Subject: [PATCH] Update the Send max access count logic (#557) --- .../feature/send/addsend/AddSendContent.kt | 1 + .../feature/send/addsend/AddSendViewModel.kt | 5 ++-- .../feature/send/addsend/AddSendScreenTest.kt | 23 ------------------- .../send/addsend/AddSendViewModelTest.kt | 21 ++++++++++++++++- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendContent.kt b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendContent.kt index 85c3fb2c8..b7a8e63cd 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendContent.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendContent.kt @@ -228,6 +228,7 @@ private fun AddSendOptions( value = state.common.maxAccessCount, onValueChange = addSendHandlers.onMaxAccessCountChange, isDecrementEnabled = state.common.maxAccessCount != null, + range = 0..Int.MAX_VALUE, modifier = Modifier .padding(horizontal = 16.dp), ) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModel.kt index d8d9bd29c..f99237e74 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModel.kt @@ -261,7 +261,9 @@ class AddSendViewModel @Inject constructor( } private fun handleMaxAccessCountChange(action: AddSendAction.MaxAccessCountChange) { - updateCommonContent { it.copy(maxAccessCount = action.value) } + updateCommonContent { common -> + common.copy(maxAccessCount = action.value.takeUnless { it == 0 }) + } } private inline fun onContent( @@ -355,7 +357,6 @@ data class AddSendState( @Parcelize data class Common( val name: String, - // Null here means "not set" val maxAccessCount: Int?, val passwordInput: String, val noteInput: String, diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreenTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreenTest.kt index dbebcde73..f0472cde2 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreenTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreenTest.kt @@ -306,29 +306,6 @@ class AddSendScreenTest : BaseComposeTest() { verify { viewModel.trySendAction(AddSendAction.MaxAccessCountChange(2)) } } - @Test - fun `max access count decrement when set to 1 should do nothing`() = - runTest { - mutableStateFlow.update { - it.copy( - viewState = DEFAULT_VIEW_STATE.copy( - common = DEFAULT_COMMON_STATE.copy(maxAccessCount = 1), - ), - ) - } - // Expand options section: - composeTestRule - .onNodeWithText("Options") - .performScrollTo() - .performClick() - - composeTestRule - .onNodeWithContentDescription("\u2212") - .performScrollTo() - .performClick() - verify(exactly = 0) { viewModel.trySendAction(any()) } - } - @Test fun `on max access count increment should send MaxAccessCountChange`() = runTest { // Expand options section: diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModelTest.kt index 3112372a7..f5c4f9ae3 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendViewModelTest.kt @@ -287,7 +287,7 @@ class AddSendViewModelTest : BaseViewModelTest() { } @Test - fun `MaxAccessCountChange should update maxAccessCount`() = runTest { + fun `MaxAccessCountChange should update maxAccessCount to value when non-zero`() = runTest { val viewModel = createViewModel() val expectedViewState = DEFAULT_VIEW_STATE.copy( common = DEFAULT_COMMON_STATE.copy(maxAccessCount = 5), @@ -300,6 +300,25 @@ class AddSendViewModelTest : BaseViewModelTest() { } } + @Test + fun `MaxAccessCountChange should update maxAccessCount to null when zero`() = runTest { + val initialState = DEFAULT_STATE.copy( + viewState = DEFAULT_VIEW_STATE.copy( + common = DEFAULT_COMMON_STATE.copy(maxAccessCount = 5), + ), + ) + val expectedViewState = DEFAULT_VIEW_STATE.copy( + common = DEFAULT_COMMON_STATE.copy(maxAccessCount = null), + ) + val viewModel = createViewModel(initialState) + + viewModel.stateFlow.test { + assertEquals(initialState, awaitItem()) + viewModel.trySendAction(AddSendAction.MaxAccessCountChange(0)) + assertEquals(initialState.copy(viewState = expectedViewState), awaitItem()) + } + } + @Test fun `TextChange should update text input`() = runTest { val viewModel = createViewModel()