BIT-1654 Add URI option menu (#877)

This commit is contained in:
Oleg Semenenko
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent a92d9ff823
commit 95b4aaf605
9 changed files with 493 additions and 67 deletions
@@ -29,6 +29,7 @@ import androidx.compose.ui.test.performScrollTo
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.test.performTouchInput
import com.bitwarden.core.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
@@ -720,7 +721,7 @@ class VaultAddEditScreenTest : BaseComposeTest() {
}
@Test
fun `in ItemType_Login state changing URI text field should trigger UriTextChange`() {
fun `in ItemType_Login state changing URI text field should trigger UriValueChange`() {
mutableStateFlow.update { currentState ->
updateLoginType(currentState) {
copy(uriList = listOf(UriItem("TestId", "URI", null)))
@@ -733,7 +734,7 @@ class VaultAddEditScreenTest : BaseComposeTest() {
verify {
viewModel.trySendAction(
VaultAddEditAction.ItemType.LoginType.UriTextChange(
VaultAddEditAction.ItemType.LoginType.UriValueChange(
UriItem("TestId", "TestURI", null),
),
)
@@ -759,20 +760,184 @@ class VaultAddEditScreenTest : BaseComposeTest() {
@Suppress("MaxLineLength")
@Test
fun `in ItemType_Login state clicking the URI settings action should trigger UriSettingsClick`() {
fun `in ItemType_Login Uri settings dialog should be dismissed on cancel click`() {
composeTestRule
.onNodeWithTextAfterScroll(text = "URI")
.onSiblings()
.filterToOne(hasContentDescription(value = "Options"))
.performClick()
composeTestRule
.onNodeWithText("Cancel")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `in ItemType_Login Uri settings dialog should send RemoveUriClick action if remove is clicked`() {
mutableStateFlow.update { currentState ->
updateLoginType(currentState) {
copy(uriList = listOf(UriItem("TestId", null, null)))
}
}
composeTestRule
.onNodeWithTextAfterScroll(text = "URI")
.onSiblings()
.filterToOne(hasContentDescription(value = "Options"))
.performClick()
composeTestRule
.onNodeWithText("Remove")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule.assertNoDialogExists()
verify {
viewModel.trySendAction(
VaultAddEditAction.ItemType.LoginType.UriSettingsClick,
VaultAddEditAction.ItemType.LoginType.RemoveUriClick(
UriItem(
"TestId",
null,
null,
),
),
)
}
}
@Suppress("MaxLineLength")
@Test
fun `in ItemType_Login Uri settings dialog with open match detection click should open list of options`() {
composeTestRule
.onNodeWithTextAfterScroll(text = "URI")
.onSiblings()
.filterToOne(hasContentDescription(value = "Options"))
.performClick()
composeTestRule
.onNodeWithText("Match detection")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onAllNodesWithText("URI match detection")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Default")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Base domain")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Host")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Starts with")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Regular expression")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Exact")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
}
@Suppress("MaxLineLength")
@Test
fun `in ItemType_Login on URI settings click and on match detection click and option click should emit UriValueChange action`() {
mutableStateFlow.update { currentState ->
updateLoginType(currentState) {
copy(uriList = listOf(UriItem("TestId", null, null)))
}
}
composeTestRule
.onNodeWithTextAfterScroll(text = "URI")
.onSiblings()
.filterToOne(hasContentDescription(value = "Options"))
.performClick()
composeTestRule
.onNodeWithText("Match detection")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onNodeWithText("URI match detection")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Exact")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule.assertNoDialogExists()
verify {
viewModel.trySendAction(
VaultAddEditAction.ItemType.LoginType.UriValueChange(
UriItem(
"TestId",
null,
UriMatchType.EXACT,
),
),
)
}
}
@Suppress("MaxLineLength")
@Test
fun `in ItemType_Login on URI settings click and on match detection click and cancel click should dismiss the dialog`() {
mutableStateFlow.update { currentState ->
updateLoginType(currentState) {
copy(uriList = listOf(UriItem("TestId", null, null)))
}
}
composeTestRule
.onNodeWithTextAfterScroll(text = "URI")
.onSiblings()
.filterToOne(hasContentDescription(value = "Options"))
.performClick()
composeTestRule
.onNodeWithText("Match detection")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onAllNodesWithText("URI match detection")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Cancel")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule.assertNoDialogExists()
}
@Test
fun `in ItemType_Login state clicking the New URI button should trigger AddNewUriClick`() {
composeTestRule
@@ -1884,7 +2049,7 @@ class VaultAddEditScreenTest : BaseComposeTest() {
@Suppress("MaxLineLength")
@Test
fun `Ownership option should send OwnershipChange action`() {
mutableStateFlow.value = DEFAULT_STATE_SECURE_NOTES
mutableStateFlow.value = DEFAULT_STATE_SECURE_NOTES
updateStateWithOwners()
@@ -882,23 +882,6 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
assertEquals(expectedState, viewModel.stateFlow.value)
}
@Test
fun `UriTextChange should update uri in LoginItem`() = runTest {
val action = VaultAddEditAction.ItemType.LoginType.UriTextChange(
UriItem("testId", "TestUri", null),
)
viewModel.actionChannel.trySend(action)
val expectedState = createVaultAddItemState(
typeContentViewState = createLoginTypeContentViewState(
uri = listOf(UriItem("testId", "TestUri", null)),
),
)
assertEquals(expectedState, viewModel.stateFlow.value)
}
@Suppress("MaxLineLength")
@Test
fun `OpenUsernameGeneratorClick should emit NavigateToGeneratorModal with username GeneratorMode`() =
@@ -1110,13 +1093,65 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
@Suppress("MaxLineLength")
@Test
fun `UriSettingsClick should emit ShowToast with 'URI Settings' message`() = runTest {
val viewModel = createAddVaultItemViewModel()
fun `UriValueChange should update URI value in state`() = runTest {
val viewModel = createAddVaultItemViewModel(
savedStateHandle = createSavedStateHandleWithState(
state = createVaultAddItemState(
typeContentViewState = createLoginTypeContentViewState(
uri = listOf(UriItem("testID", null, null)),
),
),
vaultAddEditType = VaultAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
),
)
val expectedState = loginInitialState.copy(
viewState = VaultAddEditState.ViewState.Content(
common = createCommonContentViewState(),
type = createLoginTypeContentViewState(
uri = listOf(UriItem("testID", "Test", null)),
),
),
)
viewModel.eventFlow.test {
viewModel.actionChannel.trySend(VaultAddEditAction.ItemType.LoginType.UriSettingsClick)
assertEquals(VaultAddEditEvent.ShowToast("URI Settings".asText()), awaitItem())
}
viewModel.trySendAction(
VaultAddEditAction.ItemType.LoginType.UriValueChange(
uriItem = UriItem("testID", "Test", null),
),
)
assertEquals(expectedState, viewModel.stateFlow.value)
}
@Suppress("MaxLineLength")
@Test
fun `RemoveUriClick should remove URI value in state`() = runTest {
val viewModel = createAddVaultItemViewModel(
savedStateHandle = createSavedStateHandleWithState(
state = createVaultAddItemState(
typeContentViewState = createLoginTypeContentViewState(
uri = listOf(UriItem("testID", null, null)),
),
),
vaultAddEditType = VaultAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
),
)
val expectedState = loginInitialState.copy(
viewState = VaultAddEditState.ViewState.Content(
common = createCommonContentViewState(),
type = createLoginTypeContentViewState(
uri = listOf(),
),
),
)
viewModel.trySendAction(
VaultAddEditAction.ItemType.LoginType.RemoveUriClick(
uriItem = UriItem("testID", null, null),
),
)
assertEquals(expectedState, viewModel.stateFlow.value)
}
@Test
@@ -0,0 +1,40 @@
package com.x8bit.bitwarden.ui.vault.feature.addedit.util
import com.bitwarden.core.UriMatchType
import com.x8bit.bitwarden.ui.vault.feature.addedit.model.UriMatchDisplayType
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class UriMatchDisplayUtilTest {
@Test
fun `toDisplayMatchType should correctly convert to UriMatchDisplayType`() {
URI_MATCH_TYPE_MAP.forEach {
assertEquals(
it.key.toDisplayMatchType(),
it.value,
)
}
}
@Test
fun `toUriMatchType should correctly convert to UriMatchType`() {
URI_MATCH_TYPE_MAP.forEach {
assertEquals(
it.key,
it.value.toUriMatchType(),
)
}
}
}
private val URI_MATCH_TYPE_MAP: Map<UriMatchType?, UriMatchDisplayType> =
mapOf(
Pair(null, UriMatchDisplayType.DEFAULT),
Pair(UriMatchType.DOMAIN, UriMatchDisplayType.BASE_DOMAIN),
Pair(UriMatchType.HOST, UriMatchDisplayType.HOST),
Pair(UriMatchType.EXACT, UriMatchDisplayType.EXACT),
Pair(UriMatchType.STARTS_WITH, UriMatchDisplayType.STARTS_WITH),
Pair(UriMatchType.REGULAR_EXPRESSION, UriMatchDisplayType.REGULAR_EXPRESSION),
Pair(UriMatchType.EXACT, UriMatchDisplayType.EXACT),
Pair(UriMatchType.NEVER, UriMatchDisplayType.NEVER),
)