From b13c89b6880016ca6824a373dcab2460b214e06a Mon Sep 17 00:00:00 2001 From: Patrick Honkonen <1883101+SaintPatrck@users.noreply.github.com> Date: Thu, 28 Mar 2024 16:44:08 -0400 Subject: [PATCH] Display environment label in account switcher (#1179) --- .../util/EnvironmentUrlDataJsonExtensions.kt | 19 ++++++++++++++++++- .../account/BitwardenAccountSwitcher.kt | 7 +++++++ .../BitwardenAccountSwitcherTestHelpers.kt | 13 ++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/repository/util/EnvironmentUrlDataJsonExtensions.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/repository/util/EnvironmentUrlDataJsonExtensions.kt index 9c9716051..785a5628e 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/repository/util/EnvironmentUrlDataJsonExtensions.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/repository/util/EnvironmentUrlDataJsonExtensions.kt @@ -73,6 +73,8 @@ val EnvironmentUrlDataJson.baseIconUrl: String /** * Returns the appropriate pre-defined labels for environments matching the known US/EU values. * Otherwise returns the host of the custom base URL. + * + * @see getSelfHostedUrlOrNull */ val EnvironmentUrlDataJson.labelOrBaseUrlHost: String get() = when (this) { @@ -83,12 +85,27 @@ val EnvironmentUrlDataJson.labelOrBaseUrlHost: String // Ex: // - "https://www.abc.com/path-1/path-1" -> "www.abc.com" URI - .create(this.base) + .create(getSelfHostedUrlOrNull().orEmpty()) .host .orEmpty() } } +/** + * Returns the first self-hosted environment URL from + * [EnvironmentUrlDataJson.webVault], [EnvironmentUrlDataJson.base], + * [EnvironmentUrlDataJson.api], and finally [EnvironmentUrlDataJson.identity]. Returns `null` if + * all self-host environment URLs are null. + */ +private fun EnvironmentUrlDataJson.getSelfHostedUrlOrNull(): String? = + webVault.takeIf { !it.isNullOrBlank() } + ?: base + .takeIf { it.isNotBlank() } + ?: api + .takeIf { !it.isNullOrBlank() } + ?: identity + .takeIf { !it.isNullOrBlank() } + /** * Converts a raw [EnvironmentUrlDataJson] to an externally-consumable [Environment]. */ diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/account/BitwardenAccountSwitcher.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/account/BitwardenAccountSwitcher.kt index 71ab24df4..200c29132 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/account/BitwardenAccountSwitcher.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/account/BitwardenAccountSwitcher.kt @@ -293,6 +293,13 @@ private fun AccountSummaryItem( modifier = Modifier.semantics { testTag = "AccountEmailLabel" }, ) + Text( + text = accountSummary.environmentLabel, + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.semantics { testTag = "AccountEnvironmentLabel" }, + ) + accountSummary.supportingTextResOrNull?.let { supportingTextResId -> Text( text = stringResource(id = supportingTextResId).lowercaseWithCurrentLocal(), diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/util/BitwardenAccountSwitcherTestHelpers.kt b/app/src/test/java/com/x8bit/bitwarden/ui/util/BitwardenAccountSwitcherTestHelpers.kt index e67b90027..427331973 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/util/BitwardenAccountSwitcherTestHelpers.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/util/BitwardenAccountSwitcherTestHelpers.kt @@ -3,6 +3,7 @@ package com.x8bit.bitwarden.ui.util import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.filterToOne import androidx.compose.ui.test.hasAnyAncestor +import androidx.compose.ui.test.hasTextExactly import androidx.compose.ui.test.isDialog import androidx.compose.ui.test.junit4.ComposeContentTestRule import androidx.compose.ui.test.longClick @@ -26,7 +27,17 @@ fun ComposeContentTestRule.assertSwitcherIsDisplayed( isAddAccountButtonVisible: Boolean = true, ) { accountSummaries.forEach { accountSummary -> - this.onNodeWithText(accountSummary.email).assertIsDisplayed() + this.onNode( + hasTextExactly( + *listOfNotNull( + accountSummary.email, + accountSummary.environmentLabel, + "locked".takeUnless { accountSummary.isVaultUnlocked }, + ) + .toTypedArray(), + ), + ) + .assertIsDisplayed() } if (isAddAccountButtonVisible) {