diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/ext/SyncedTabsViewErrorType.kt b/app/src/main/java/org/mozilla/fenix/tabstray/ext/SyncedTabsViewErrorType.kt new file mode 100644 index 0000000000..97a9b964d8 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/tabstray/ext/SyncedTabsViewErrorType.kt @@ -0,0 +1,46 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.tabstray.ext + +import android.content.Context +import androidx.navigation.NavController +import mozilla.components.feature.syncedtabs.view.SyncedTabsView +import org.mozilla.fenix.NavGraphDirections +import org.mozilla.fenix.R +import org.mozilla.fenix.tabstray.syncedtabs.SyncedTabsListItem + +/** + * Converts [SyncedTabsView.ErrorType] to [SyncedTabsListItem.Error] with a lambda for ONLY + * [SyncedTabsView.ErrorType.SYNC_UNAVAILABLE] + * + * @param context Context used to obtain the strings. + * @param navController The controller used to handle any navigation necessary for error scenarios. + */ +fun SyncedTabsView.ErrorType.toSyncedTabsListItem(context: Context, navController: NavController) = + when (this) { + SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE -> + SyncedTabsListItem.Error(errorText = context.getString(R.string.synced_tabs_connect_another_device)) + + SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE -> + SyncedTabsListItem.Error(errorText = context.getString(R.string.synced_tabs_enable_tab_syncing)) + + SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION -> + SyncedTabsListItem.Error(errorText = context.getString(R.string.synced_tabs_reauth)) + + SyncedTabsView.ErrorType.NO_TABS_AVAILABLE -> + SyncedTabsListItem.Error( + errorText = context.getString(R.string.synced_tabs_no_tabs), + ) + + SyncedTabsView.ErrorType.SYNC_UNAVAILABLE -> + SyncedTabsListItem.Error( + errorText = context.getString(R.string.synced_tabs_sign_in_message), + errorButton = SyncedTabsListItem.ErrorButton( + buttonText = context.getString(R.string.synced_tabs_sign_in_button) + ) { + navController.navigate(NavGraphDirections.actionGlobalTurnOnSync()) + }, + ) + } diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/syncedtabs/SyncedTabsIntegration.kt b/app/src/main/java/org/mozilla/fenix/tabstray/syncedtabs/SyncedTabsIntegration.kt index 97d5a08485..ef1e6e0efe 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/syncedtabs/SyncedTabsIntegration.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/syncedtabs/SyncedTabsIntegration.kt @@ -15,13 +15,12 @@ import mozilla.components.service.fxa.manager.FxaAccountManager import mozilla.components.support.base.feature.LifecycleAwareFeature import mozilla.components.support.base.observer.Observable import mozilla.components.support.base.observer.ObserverRegistry -import org.mozilla.fenix.NavGraphDirections -import org.mozilla.fenix.R import org.mozilla.fenix.ext.settings import org.mozilla.fenix.tabstray.FloatingActionButtonBinding import org.mozilla.fenix.tabstray.TabsTrayAction import org.mozilla.fenix.tabstray.TabsTrayStore import org.mozilla.fenix.tabstray.ext.toComposeList +import org.mozilla.fenix.tabstray.ext.toSyncedTabsListItem /** * TabsTrayFragment delegate to handle all layout updates needed to display synced tabs and any errors. @@ -78,7 +77,7 @@ class SyncedTabsIntegration( // We may still be displaying a "loading" spinner, hide it. stopLoading() - store.dispatch(TabsTrayAction.UpdateSyncedTabs(listOf(error.toSyncedTabsListItem()))) + store.dispatch(TabsTrayAction.UpdateSyncedTabs(listOf(error.toSyncedTabsListItem(context, navController)))) } /** @@ -99,34 +98,4 @@ class SyncedTabsIntegration( ) ) } - - /** - * Converts [SyncedTabsView.ErrorType] to [SyncedTabsListItem.Error] with a lambda for ONLY - * [SyncedTabsView.ErrorType.SYNC_UNAVAILABLE] - */ - private fun SyncedTabsView.ErrorType.toSyncedTabsListItem() = when (this) { - SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE -> - SyncedTabsListItem.Error(errorText = context.getString(R.string.synced_tabs_connect_another_device)) - - SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE -> - SyncedTabsListItem.Error(errorText = context.getString(R.string.synced_tabs_enable_tab_syncing)) - - SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION -> - SyncedTabsListItem.Error(errorText = context.getString(R.string.synced_tabs_reauth)) - - SyncedTabsView.ErrorType.NO_TABS_AVAILABLE -> - SyncedTabsListItem.Error( - errorText = context.getString(R.string.synced_tabs_no_tabs), - ) - - SyncedTabsView.ErrorType.SYNC_UNAVAILABLE -> - SyncedTabsListItem.Error( - errorText = context.getString(R.string.synced_tabs_sign_in_message), - errorButton = SyncedTabsListItem.ErrorButton( - buttonText = context.getString(R.string.synced_tabs_sign_in_button) - ) { - navController.navigate(NavGraphDirections.actionGlobalTurnOnSync()) - }, - ) - } } diff --git a/app/src/test/java/org/mozilla/fenix/tabstray/syncedtabs/SyncedTabsViewErrorTypeTest.kt b/app/src/test/java/org/mozilla/fenix/tabstray/syncedtabs/SyncedTabsViewErrorTypeTest.kt new file mode 100644 index 0000000000..a239e8d742 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/tabstray/syncedtabs/SyncedTabsViewErrorTypeTest.kt @@ -0,0 +1,40 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.tabstray.syncedtabs + +import androidx.navigation.NavController +import io.mockk.mockk +import mozilla.components.feature.syncedtabs.view.SyncedTabsView +import mozilla.components.support.test.robolectric.testContext +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.R +import org.mozilla.fenix.helpers.FenixRobolectricTestRunner +import org.mozilla.fenix.tabstray.ext.toSyncedTabsListItem + +@RunWith(FenixRobolectricTestRunner::class) +class SyncedTabsViewErrorTypeTest { + + @Test + fun `GIVEN synced tabs error types WHEN the synced tabs update process errors THEN the correct error text should be displayed`() { + val context = testContext + val navController: NavController = mockk(relaxed = true) + val multipleDevicesUnavailable = SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE.toSyncedTabsListItem(context, navController) + val syncEngineUnavailable = SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE.toSyncedTabsListItem(context, navController) + val syncNeedsReauthentication = SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION.toSyncedTabsListItem(context, navController) + val noTabsAvailable = SyncedTabsView.ErrorType.NO_TABS_AVAILABLE.toSyncedTabsListItem(context, navController) + val syncUnavailable = SyncedTabsView.ErrorType.SYNC_UNAVAILABLE.toSyncedTabsListItem(context, navController) + + assertEquals(testContext.getString(R.string.synced_tabs_connect_another_device), multipleDevicesUnavailable.errorText) + assertEquals(testContext.getString(R.string.synced_tabs_enable_tab_syncing), syncEngineUnavailable.errorText) + assertEquals(testContext.getString(R.string.synced_tabs_reauth), syncNeedsReauthentication.errorText) + assertEquals(testContext.getString(R.string.synced_tabs_no_tabs), noTabsAvailable.errorText) + assertEquals(testContext.getString(R.string.synced_tabs_sign_in_message), syncUnavailable.errorText) + assertNotNull(syncUnavailable.errorButton) + assertEquals(testContext.getString(R.string.synced_tabs_sign_in_button), syncUnavailable.errorButton!!.buttonText) + } +}