diff --git a/app/src/androidTest/java/org/mozilla/fenix/perf/StartupExcessiveResourceUseTest.kt b/app/src/androidTest/java/org/mozilla/fenix/perf/StartupExcessiveResourceUseTest.kt index 5a386eefc..1d17b8e04 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/perf/StartupExcessiveResourceUseTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/perf/StartupExcessiveResourceUseTest.kt @@ -33,7 +33,7 @@ import org.mozilla.fenix.helpers.HomeActivityTestRule * * Say no to main thread IO! 🙅 */ -private const val EXPECTED_SUPPRESSION_COUNT = 15 +private const val EXPECTED_SUPPRESSION_COUNT = 16 /** * The number of times we call the `runBlocking` coroutine method on the main thread during this diff --git a/app/src/main/java/org/mozilla/fenix/BrowsingModeBinding.kt b/app/src/main/java/org/mozilla/fenix/BrowsingModeBinding.kt index 84bf736ee..3bcc2dc8f 100644 --- a/app/src/main/java/org/mozilla/fenix/BrowsingModeBinding.kt +++ b/app/src/main/java/org/mozilla/fenix/BrowsingModeBinding.kt @@ -19,9 +19,9 @@ import org.mozilla.fenix.theme.ThemeManager import org.mozilla.fenix.utils.Settings /** - * Binding to react to [BrowsingMode] changes in [AppState]. + * Binding to react to Private Browsing Mode changes in AppState. * - * @param appStore [AppStore] to observe state changes from. + * @param appStore AppStore to observe state changes from. * @param themeManager Theme will be updated based on state changes. * @param retrieveWindow Get window to update privacy flags for. * @param settings Determine user settings for privacy features. diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 8dd8ae226..151139baa 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -140,6 +140,7 @@ import org.mozilla.fenix.shortcut.NewTabShortcutIntentProcessor.Companion.ACTION import org.mozilla.fenix.tabhistory.TabHistoryDialogFragment import org.mozilla.fenix.tabstray.TabsTrayFragment import org.mozilla.fenix.theme.DefaultThemeManager +import org.mozilla.fenix.theme.ThemeManager import org.mozilla.fenix.utils.Settings import java.lang.ref.WeakReference import java.util.Locale @@ -160,7 +161,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { private lateinit var binding: ActivityHomeBinding val themeManager by lazy { - DefaultThemeManager(components.appStore.state.mode, this) + createThemeManager() } private var isVisuallyComplete = false @@ -1149,6 +1150,10 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { return super.getSystemService(name) } + protected open fun createThemeManager(): ThemeManager { + return DefaultThemeManager(components.appStore.state.mode, this) + } + private fun openPopup(webExtensionState: WebExtensionState) { val action = NavGraphDirections.actionGlobalWebExtensionActionPopupFragment( webExtensionId = webExtensionState.id, diff --git a/app/src/main/java/org/mozilla/fenix/browser/browsingmode/BrowsingMode.kt b/app/src/main/java/org/mozilla/fenix/browser/browsingmode/BrowsingModeManager.kt similarity index 59% rename from app/src/main/java/org/mozilla/fenix/browser/browsingmode/BrowsingMode.kt rename to app/src/main/java/org/mozilla/fenix/browser/browsingmode/BrowsingModeManager.kt index 023a678ea..ef0a5e3df 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/browsingmode/BrowsingMode.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/browsingmode/BrowsingModeManager.kt @@ -4,6 +4,8 @@ package org.mozilla.fenix.browser.browsingmode +import org.mozilla.fenix.utils.Settings + /** * Enum that represents whether or not private browsing is active. */ @@ -29,3 +31,25 @@ enum class BrowsingMode { fun fromBoolean(isPrivate: Boolean) = if (isPrivate) Private else Normal } } + +interface BrowsingModeManager { + var mode: BrowsingMode +} + +/** + * Wraps a [BrowsingMode] and executes a callback whenever [mode] is updated. + */ +class DefaultBrowsingModeManager( + private var _mode: BrowsingMode, + private val settings: Settings, + private val modeDidChange: (BrowsingMode) -> Unit, +) : BrowsingModeManager { + + override var mode: BrowsingMode + get() = _mode + set(value) { + _mode = value + modeDidChange(value) + settings.lastKnownMode = value + } +} diff --git a/app/src/main/java/org/mozilla/fenix/components/appstate/AppAction.kt b/app/src/main/java/org/mozilla/fenix/components/appstate/AppAction.kt index ef9646838..15378b9e9 100644 --- a/app/src/main/java/org/mozilla/fenix/components/appstate/AppAction.kt +++ b/app/src/main/java/org/mozilla/fenix/components/appstate/AppAction.kt @@ -221,11 +221,6 @@ sealed class AppAction : Action { val wallpaper: Wallpaper, val imageState: Wallpaper.ImageFileState, ) : WallpaperAction() - - /** - * App should be opened to the home screen in [BrowsingMode.Normal]. - */ - object OpenToHome : WallpaperAction() } /** @@ -294,33 +289,13 @@ sealed class AppAction : Action { } /** - * Actions dispatched from the Toolbar that affect [AppState]. + * Actions related to the home screen. */ - sealed class ToolbarAction : AppAction() { - /** - * Handles clicks for new tabs in [BrowsingMode.Normal] from the long-press menu of the Toolbar. - */ - object NewTab : ToolbarAction() - - /** - * Handles clicks for new tabs in [BrowsingMode.Private] from the long-press menu of the Toolbar. - */ - object NewPrivateTab : ToolbarAction() - } - - /** - * Actions dispatched from the Tabs Tray that affect [AppState]. - */ - sealed class TabsTrayAction : AppAction() { - /** - * Handles clicks for new tabs in [BrowsingMode.Normal] from the FAB of the Tabs Tray. - */ - object NewTab : TabsTrayAction() - + sealed class HomeAction : AppAction() { /** - Handles clicks for new tabs in [BrowsingMode.Private] from the FAB of the Tabs Tray. + * App should be opened to the home screen in [mode]. */ - object NewPrivateTab : TabsTrayAction() + data class OpenToHome(val mode: BrowsingMode) : HomeAction() } /** diff --git a/app/src/main/java/org/mozilla/fenix/components/appstate/AppStoreReducer.kt b/app/src/main/java/org/mozilla/fenix/components/appstate/AppStoreReducer.kt index ecba61b69..a6dfda912 100644 --- a/app/src/main/java/org/mozilla/fenix/components/appstate/AppStoreReducer.kt +++ b/app/src/main/java/org/mozilla/fenix/components/appstate/AppStoreReducer.kt @@ -10,12 +10,10 @@ import mozilla.components.service.pocket.PocketStory.PocketSponsoredStory import mozilla.components.service.pocket.ext.recordNewImpression import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.components.AppStore -import org.mozilla.fenix.components.appstate.home.TabsTrayReducer -import org.mozilla.fenix.components.appstate.home.ToolbarReducer -import org.mozilla.fenix.components.appstate.home.WallpapersReducer import org.mozilla.fenix.components.appstate.shopping.ShoppingStateReducer import org.mozilla.fenix.ext.filterOutTab import org.mozilla.fenix.ext.getFilteredStories +import org.mozilla.fenix.home.HomeReducer import org.mozilla.fenix.home.intent.IntentReducer import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesSelectedCategory import org.mozilla.fenix.home.recentsyncedtabs.RecentSyncedTabState @@ -215,6 +213,25 @@ internal object AppStoreReducer { is AppAction.UndoPendingDeletionSet -> state.copy(pendingDeletionHistoryItems = state.pendingDeletionHistoryItems - action.historyItems) + is AppAction.WallpaperAction.UpdateCurrentWallpaper -> + state.copy( + wallpaperState = state.wallpaperState.copy(currentWallpaper = action.wallpaper), + ) + is AppAction.WallpaperAction.UpdateAvailableWallpapers -> + state.copy( + wallpaperState = state.wallpaperState.copy(availableWallpapers = action.wallpapers), + ) + is AppAction.WallpaperAction.UpdateWallpaperDownloadState -> { + val wallpapers = state.wallpaperState.availableWallpapers.map { + if (it.name == action.wallpaper.name) { + it.copy(assetsFileState = action.imageState) + } else { + it + } + } + val wallpaperState = state.wallpaperState.copy(availableWallpapers = wallpapers) + state.copy(wallpaperState = wallpaperState) + } is AppAction.AppLifecycleAction.ResumeAction -> { state.copy(isForeground = true) } @@ -227,10 +244,8 @@ internal object AppStoreReducer { ) is AppAction.ShoppingAction -> ShoppingStateReducer.reduce(state, action) - is AppAction.WallpaperAction -> WallpapersReducer.reduce(state, action) + is AppAction.HomeAction -> HomeReducer.reduce(state, action) is AppAction.IntentAction -> IntentReducer.reduce(state, action) - is AppAction.ToolbarAction -> ToolbarReducer.reduce(state, action) - is AppAction.TabsTrayAction -> TabsTrayReducer.reduce(state, action) } } diff --git a/app/src/main/java/org/mozilla/fenix/components/appstate/bindings/BrowserStoreBinding.kt b/app/src/main/java/org/mozilla/fenix/components/appstate/bindings/BrowserStoreBinding.kt index f7752097b..4bd1faaa7 100644 --- a/app/src/main/java/org/mozilla/fenix/components/appstate/bindings/BrowserStoreBinding.kt +++ b/app/src/main/java/org/mozilla/fenix/components/appstate/bindings/BrowserStoreBinding.kt @@ -26,8 +26,8 @@ class BrowserStoreBinding( flow.distinctUntilChangedBy { it.selectedTabId } .collectLatest { state -> state.selectedTab?.let { tab -> - // Ignore re-emissions of the selected tab from BrowserStore when re-observing due - // to lifecycle events, otherwise pieces of state like [mode] may get overwritten. + // Ignore re-emissions due to lifecycle events, or other pieces of state like + // [mode] may get overwritten if (appStore.state.selectedTabId != tab.id) { appStore.dispatch(AppAction.SelectedTabChanged(tab)) } diff --git a/app/src/main/java/org/mozilla/fenix/components/appstate/home/TabsTrayReducer.kt b/app/src/main/java/org/mozilla/fenix/components/appstate/home/TabsTrayReducer.kt deleted file mode 100644 index 324580800..000000000 --- a/app/src/main/java/org/mozilla/fenix/components/appstate/home/TabsTrayReducer.kt +++ /dev/null @@ -1,23 +0,0 @@ -/* 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.components.appstate.home - -import org.mozilla.fenix.browser.browsingmode.BrowsingMode -import org.mozilla.fenix.components.appstate.AppAction.TabsTrayAction -import org.mozilla.fenix.components.appstate.AppState - -/** - * Reducer to handle updating [AppState] with the result of a [TabsTrayAction]. - */ -object TabsTrayReducer { - - /** - * Reducer to handle updating [AppState] with the result of a [TabsTrayAction]. - */ - fun reduce(state: AppState, action: TabsTrayAction): AppState = when (action) { - is TabsTrayAction.NewTab -> state.copy(mode = BrowsingMode.Normal) - is TabsTrayAction.NewPrivateTab -> state.copy(mode = BrowsingMode.Private) - } -} diff --git a/app/src/main/java/org/mozilla/fenix/components/appstate/home/ToolbarReducer.kt b/app/src/main/java/org/mozilla/fenix/components/appstate/home/ToolbarReducer.kt deleted file mode 100644 index 926deabaa..000000000 --- a/app/src/main/java/org/mozilla/fenix/components/appstate/home/ToolbarReducer.kt +++ /dev/null @@ -1,23 +0,0 @@ -/* 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.components.appstate.home - -import org.mozilla.fenix.browser.browsingmode.BrowsingMode -import org.mozilla.fenix.components.appstate.AppAction.ToolbarAction -import org.mozilla.fenix.components.appstate.AppState - -/** - * Reducer to handle updating [AppState] with the result of a [ToolbarAction]. - */ -object ToolbarReducer { - - /** - * Reducer to handle updating [AppState] with the result of a [ToolbarAction]. - */ - fun reduce(state: AppState, action: ToolbarAction): AppState = when (action) { - is ToolbarAction.NewTab -> state.copy(mode = BrowsingMode.Normal) - is ToolbarAction.NewPrivateTab -> state.copy(mode = BrowsingMode.Private) - } -} diff --git a/app/src/main/java/org/mozilla/fenix/components/appstate/home/WallpapersReducer.kt b/app/src/main/java/org/mozilla/fenix/components/appstate/home/WallpapersReducer.kt deleted file mode 100644 index f0b6ccb9a..000000000 --- a/app/src/main/java/org/mozilla/fenix/components/appstate/home/WallpapersReducer.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* 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.components.appstate.home - -import org.mozilla.fenix.browser.browsingmode.BrowsingMode -import org.mozilla.fenix.components.appstate.AppAction.WallpaperAction -import org.mozilla.fenix.components.appstate.AppState - -/** - * Reducer to handle updating [AppState] with the result of an [WallpaperAction]. - */ -object WallpapersReducer { - - /** - * Reducer to handle updating [AppState] with the result of an [WallpaperAction]. - */ - fun reduce(state: AppState, action: WallpaperAction): AppState = when (action) { - is WallpaperAction.UpdateCurrentWallpaper -> - state.copy( - wallpaperState = state.wallpaperState.copy(currentWallpaper = action.wallpaper), - ) - is WallpaperAction.UpdateAvailableWallpapers -> - state.copy( - wallpaperState = state.wallpaperState.copy(availableWallpapers = action.wallpapers), - ) - is WallpaperAction.UpdateWallpaperDownloadState -> { - val wallpapers = state.wallpaperState.availableWallpapers.map { - if (it.name == action.wallpaper.name) { - it.copy(assetsFileState = action.imageState) - } else { - it - } - } - val wallpaperState = state.wallpaperState.copy(availableWallpapers = wallpapers) - state.copy(wallpaperState = wallpaperState) - } - is WallpaperAction.OpenToHome -> state.copy(mode = BrowsingMode.Normal) - } -} diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt index f2210b40a..7b03f1b94 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt @@ -23,6 +23,7 @@ import org.mozilla.fenix.R import org.mozilla.fenix.browser.BrowserAnimator import org.mozilla.fenix.browser.BrowserAnimator.Companion.getToolbarNavOptions import org.mozilla.fenix.browser.BrowserFragmentDirections +import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.readermode.ReaderModeController import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction @@ -177,13 +178,13 @@ class DefaultBrowserToolbarController( } } is TabCounterMenu.Item.NewTab -> { - appStore.dispatch(AppAction.ToolbarAction.NewTab) + appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.Normal)) navController.navigate( BrowserFragmentDirections.actionGlobalHome(focusOnAddressBar = true), ) } is TabCounterMenu.Item.NewPrivateTab -> { - appStore.dispatch(AppAction.ToolbarAction.NewPrivateTab) + appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.Private)) navController.navigate( BrowserFragmentDirections.actionGlobalHome(focusOnAddressBar = true), ) diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 93af30471..c1509f561 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -554,12 +554,9 @@ class HomeFragment : Fragment() { navController = findNavController(), tabCounter = binding.tabButton, mode = requireComponents.appStore.state.mode, - onBrowsingModeChanged = { newMode -> - val action = when (newMode) { - BrowsingMode.Normal -> AppAction.ToolbarAction.NewTab - BrowsingMode.Private -> AppAction.ToolbarAction.NewPrivateTab - } - requireComponents.appStore.dispatch(action) + itemTapped = { + newMode -> + requireComponents.appStore.dispatch(AppAction.HomeAction.OpenToHome(newMode)) }, ) @@ -812,7 +809,7 @@ class HomeFragment : Fragment() { override fun onResume() { super.onResume() - if (requireComponents.appStore.state.mode.isPrivate) { + if (requireComponents.appStore.state.mode == BrowsingMode.Private) { activity?.window?.setBackgroundDrawableResource(R.drawable.private_home_background_gradient) } @@ -828,7 +825,7 @@ class HomeFragment : Fragment() { override fun onPause() { super.onPause() - if (requireComponents.appStore.state.mode.isPrivate) { + if (requireComponents.appStore.state.mode == BrowsingMode.Private) { activity?.window?.setBackgroundDrawable( ColorDrawable( ContextCompat.getColor( diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeReducer.kt b/app/src/main/java/org/mozilla/fenix/home/HomeReducer.kt new file mode 100644 index 000000000..8f94a69c8 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/home/HomeReducer.kt @@ -0,0 +1,21 @@ +/* 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.home + +import org.mozilla.fenix.components.appstate.AppAction.HomeAction +import org.mozilla.fenix.components.appstate.AppState + +/** + * Reducer to handle updating [AppState] with the result of an [HomeAction]. + */ +object HomeReducer { + + /** + * Reducer to handle updating [AppState] with the result of an [HomeAction]. + */ + fun reduce(state: AppState, action: HomeAction): AppState = when (action) { + is HomeAction.OpenToHome -> state.copy(mode = action.mode) + } +} diff --git a/app/src/main/java/org/mozilla/fenix/home/TabCounterView.kt b/app/src/main/java/org/mozilla/fenix/home/TabCounterView.kt index 8c65c72c0..aac8fbb56 100644 --- a/app/src/main/java/org/mozilla/fenix/home/TabCounterView.kt +++ b/app/src/main/java/org/mozilla/fenix/home/TabCounterView.kt @@ -29,14 +29,14 @@ import org.mozilla.fenix.tabstray.Page * @param navController [NavController] used for navigation. * @param tabCounter The [TabCounter] that will be setup with event handlers. * @param mode The current [BrowsingMode]. - * @param onBrowsingModeChanged Callback to update the [BrowsingMode]. + * @param itemTapped Callback to update the [BrowsingMode]. */ class TabCounterView( private val context: Context, private val navController: NavController, private val tabCounter: TabCounter, private val mode: BrowsingMode, - private val onBrowsingModeChanged: (BrowsingMode) -> Unit, + private val itemTapped: (BrowsingMode) -> Unit, ) { init { @@ -103,9 +103,9 @@ class TabCounterView( */ internal fun onItemTapped(item: TabCounterMenu.Item) { if (item is TabCounterMenu.Item.NewTab) { - onBrowsingModeChanged(BrowsingMode.Normal) + itemTapped(BrowsingMode.Normal) } else if (item is TabCounterMenu.Item.NewPrivateTab) { - onBrowsingModeChanged(BrowsingMode.Private) + itemTapped(BrowsingMode.Private) } } } diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt index 2fe72f7c7..12507f4ab 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt @@ -99,10 +99,11 @@ class DefaultBookmarkController( override fun handleBookmarkTapped(item: BookmarkNode) { val fromHomeFragment = navController.previousBackStackEntry?.destination?.id == R.id.homeFragment + val isPrivate = appStore.state.mode == BrowsingMode.Private val flags = EngineSession.LoadUrlFlags.select(EngineSession.LoadUrlFlags.ALLOW_JAVASCRIPT_URL) openInNewTabAndShow( item.url!!, - appStore.state.mode.isPrivate || fromHomeFragment, + isPrivate || fromHomeFragment, BrowserDirection.FromBookmarks, flags, ) diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt index da9fab840..a2e9d0dfa 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt @@ -116,7 +116,7 @@ class HistoryFragment : LibraryPageFragment(), UserInteractionHandler, onBackPressed = requireActivity().onBackPressedDispatcher::onBackPressed, ), HistoryTelemetryMiddleware( - isInPrivateMode = requireComponents.appStore.state.mode.isPrivate, + isInPrivateMode = requireComponents.appStore.state.mode == BrowsingMode.Private, ), HistorySyncMiddleware( accountManager = requireContext().components.backgroundServices.accountManager, diff --git a/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt index 29e4488be..ebd2dd5cf 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt @@ -21,6 +21,7 @@ import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.GleanMetrics.Wallpapers import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R +import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.components.FenixSnackbar import org.mozilla.fenix.components.appstate.AppAction import org.mozilla.fenix.ext.requireComponents @@ -110,7 +111,7 @@ class WallpaperSettingsFragment : Fragment() { .setText(view.context.getString(R.string.wallpaper_updated_snackbar_message)) .setAction(requireContext().getString(R.string.wallpaper_updated_snackbar_action)) { requireComponents.appStore.dispatch( - AppAction.WallpaperAction.OpenToHome, + AppAction.HomeAction.OpenToHome(BrowsingMode.Normal), ) findNavController().popBackStack(R.id.homeFragment, false) } diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayController.kt b/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayController.kt index de967d74c..9adb08d4c 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayController.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayController.kt @@ -31,6 +31,7 @@ import org.mozilla.fenix.GleanMetrics.Events import org.mozilla.fenix.GleanMetrics.TabsTray import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R +import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.collections.CollectionsDialog import org.mozilla.fenix.collections.show import org.mozilla.fenix.components.AppStore @@ -241,12 +242,7 @@ class DefaultTabsTrayController( */ private fun openNewTab(isPrivate: Boolean) { val startTime = profiler?.getProfilerTime() - val action = if (isPrivate) { - AppAction.TabsTrayAction.NewPrivateTab - } else { - AppAction.TabsTrayAction.NewTab - } - appStore.dispatch(action) + appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.fromBoolean(isPrivate))) navController.navigate( TabsTrayFragmentDirections.actionGlobalHome(focusOnAddressBar = true), ) diff --git a/app/src/test/java/org/mozilla/fenix/browser/browsingmode/DefaultBrowsingModeManagerTest.kt b/app/src/test/java/org/mozilla/fenix/browser/browsingmode/DefaultBrowsingModeManagerTest.kt new file mode 100644 index 000000000..869b16ad9 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/browser/browsingmode/DefaultBrowsingModeManagerTest.kt @@ -0,0 +1,69 @@ +/* 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.browser.browsingmode + +import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.just +import io.mockk.verify +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.mozilla.fenix.helpers.MockkRetryTestRule +import org.mozilla.fenix.utils.Settings + +class DefaultBrowsingModeManagerTest { + + @MockK lateinit var settings: Settings + + @MockK(relaxed = true) + lateinit var callback: (BrowsingMode) -> Unit + lateinit var manager: BrowsingModeManager + + private val initMode = BrowsingMode.Normal + + @get:Rule + val mockkRule = MockkRetryTestRule() + + @Before + fun before() { + MockKAnnotations.init(this) + + manager = DefaultBrowsingModeManager(initMode, settings, callback) + every { settings.lastKnownMode = any() } just Runs + } + + @Test + fun `WHEN mode is updated THEN callback is invoked`() { + verify(exactly = 0) { callback.invoke(any()) } + + manager.mode = BrowsingMode.Private + manager.mode = BrowsingMode.Private + manager.mode = BrowsingMode.Private + + verify(exactly = 3) { callback.invoke(BrowsingMode.Private) } + + manager.mode = BrowsingMode.Normal + manager.mode = BrowsingMode.Normal + + verify(exactly = 2) { callback.invoke(BrowsingMode.Normal) } + } + + @Test + fun `WHEN mode is updated THEN it should be returned from get`() { + assertEquals(BrowsingMode.Normal, manager.mode) + + manager.mode = BrowsingMode.Private + assertEquals(BrowsingMode.Private, manager.mode) + verify { settings.lastKnownMode = BrowsingMode.Private } + + manager.mode = BrowsingMode.Normal + assertEquals(BrowsingMode.Normal, manager.mode) + verify { settings.lastKnownMode = BrowsingMode.Normal } + } +} diff --git a/app/src/test/java/org/mozilla/fenix/browser/browsingmode/SimpleBrowsingModeManager.kt b/app/src/test/java/org/mozilla/fenix/browser/browsingmode/SimpleBrowsingModeManager.kt new file mode 100644 index 000000000..cb0ef51f1 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/browser/browsingmode/SimpleBrowsingModeManager.kt @@ -0,0 +1,9 @@ +/* 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.browser.browsingmode + +data class SimpleBrowsingModeManager( + override var mode: BrowsingMode, +) : BrowsingModeManager diff --git a/app/src/test/java/org/mozilla/fenix/components/AppStoreTest.kt b/app/src/test/java/org/mozilla/fenix/components/AppStoreTest.kt index 95362fea0..ee4412636 100644 --- a/app/src/test/java/org/mozilla/fenix/components/AppStoreTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/AppStoreTest.kt @@ -20,6 +20,7 @@ import mozilla.components.service.pocket.PocketStory import mozilla.components.service.pocket.PocketStory.PocketRecommendedStory import mozilla.components.service.pocket.PocketStory.PocketSponsoredStory import mozilla.components.service.pocket.PocketStory.PocketSponsoredStoryCaps +import mozilla.components.support.test.ext.joinBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertSame @@ -27,6 +28,7 @@ import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test import org.mozilla.fenix.browser.browsingmode.BrowsingMode +import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager import org.mozilla.fenix.components.appstate.AppAction import org.mozilla.fenix.components.appstate.AppAction.MessagingAction.UpdateMessageToShow import org.mozilla.fenix.components.appstate.AppState @@ -49,6 +51,7 @@ class AppStoreTest { private lateinit var context: Context private lateinit var accountManager: FxaAccountManager private lateinit var onboarding: FenixOnboarding + private lateinit var browsingModeManager: BrowsingModeManager private lateinit var appState: AppState private lateinit var appStore: AppStore private lateinit var recentSyncedTabsList: List @@ -58,6 +61,7 @@ class AppStoreTest { context = mockk(relaxed = true) accountManager = mockk(relaxed = true) onboarding = mockk(relaxed = true) + browsingModeManager = mockk(relaxed = true) recentSyncedTabsList = listOf( RecentSyncedTab( deviceDisplayName = "", @@ -70,10 +74,12 @@ class AppStoreTest { every { context.components.backgroundServices.accountManager } returns accountManager every { onboarding.userHasBeenOnboarded() } returns true + every { browsingModeManager.mode } returns BrowsingMode.Normal appState = AppState( collections = emptyList(), expandedCollections = emptySet(), + mode = browsingModeManager.mode, topSites = emptyList(), showCollectionPlaceholder = true, recentTabs = emptyList(), @@ -533,4 +539,22 @@ class AppStoreTest { assertEquals(recentHistory - group2, recentHistory.filterOut("Title2")) assertEquals(recentHistory - group3, recentHistory.filterOut("title3")) } + + @Test + fun `WHEN new tab clicked THEN mode is updated to normal`() { + appStore = AppStore(AppState(mode = BrowsingMode.Private)) + + appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.Normal)).joinBlocking() + + assertEquals(BrowsingMode.Normal, appStore.state.mode) + } + + @Test + fun `WHEN new private tab clicked THEN mode is updated to private`() { + appStore = AppStore(AppState(mode = BrowsingMode.Normal)) + + appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.Private)).joinBlocking() + + assertEquals(BrowsingMode.Private, appStore.state.mode) + } } diff --git a/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt b/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt index 7b443a50c..5341b1824 100644 --- a/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt @@ -27,6 +27,8 @@ import org.junit.Test import org.junit.runner.RunWith import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.NavGraphDirections +import org.mozilla.fenix.browser.browsingmode.BrowsingMode +import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.getIntentSource import org.mozilla.fenix.ext.getNavDirections @@ -55,6 +57,8 @@ class ExternalAppBrowserActivityTest { @Test fun `navigateToBrowserOnColdStart does nothing for external app browser activity`() { val activity = spyk(ExternalAppBrowserActivity()) + val browsingModeManager: BrowsingModeManager = mockk() + every { browsingModeManager.mode } returns BrowsingMode.Normal val settings: Settings = mockk() every { settings.shouldReturnToBrowser } returns true diff --git a/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt b/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt index 273b15a78..80ff5dc1f 100644 --- a/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt @@ -1121,7 +1121,7 @@ class DefaultSessionControlControllerTest { val wallpaperState = WallpaperState.default.copy( availableWallpapers = makeFakeRemoteWallpapers( THUMBNAILS_SELECTION_COUNT, - true, + false, ), ) diff --git a/app/src/test/java/org/mozilla/fenix/home/TabCounterViewTest.kt b/app/src/test/java/org/mozilla/fenix/home/TabCounterViewTest.kt index d13e201b6..6608cfdbb 100644 --- a/app/src/test/java/org/mozilla/fenix/home/TabCounterViewTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/TabCounterViewTest.kt @@ -174,6 +174,6 @@ class TabCounterViewTest { navController = navController, tabCounter = tabCounter, mode = mode, - onBrowsingModeChanged = itemTapped, + itemTapped = itemTapped, ) } diff --git a/app/src/test/java/org/mozilla/fenix/home/TabsTrayReducerTest.kt b/app/src/test/java/org/mozilla/fenix/home/TabsTrayReducerTest.kt deleted file mode 100644 index 9a9a04b24..000000000 --- a/app/src/test/java/org/mozilla/fenix/home/TabsTrayReducerTest.kt +++ /dev/null @@ -1,29 +0,0 @@ -package org.mozilla.fenix.home - -import mozilla.components.support.test.ext.joinBlocking -import org.junit.Assert.assertEquals -import org.junit.Test -import org.mozilla.fenix.browser.browsingmode.BrowsingMode -import org.mozilla.fenix.components.AppStore -import org.mozilla.fenix.components.appstate.AppAction -import org.mozilla.fenix.components.appstate.AppState - -class TabsTrayReducerTest { - @Test - fun `WHEN new tab clicked THEN mode is updated to normal`() { - val appStore = AppStore(AppState(mode = BrowsingMode.Private)) - - appStore.dispatch(AppAction.TabsTrayAction.NewTab).joinBlocking() - - assertEquals(BrowsingMode.Normal, appStore.state.mode) - } - - @Test - fun `WHEN new private tab clicked THEN mode is updated to private`() { - val appStore = AppStore(AppState(mode = BrowsingMode.Normal)) - - appStore.dispatch(AppAction.TabsTrayAction.NewPrivateTab).joinBlocking() - - assertEquals(BrowsingMode.Private, appStore.state.mode) - } -} diff --git a/app/src/test/java/org/mozilla/fenix/home/ToolbarReducerTest.kt b/app/src/test/java/org/mozilla/fenix/home/ToolbarReducerTest.kt deleted file mode 100644 index 0c244a0da..000000000 --- a/app/src/test/java/org/mozilla/fenix/home/ToolbarReducerTest.kt +++ /dev/null @@ -1,29 +0,0 @@ -package org.mozilla.fenix.home - -import mozilla.components.support.test.ext.joinBlocking -import org.junit.Assert.assertEquals -import org.junit.Test -import org.mozilla.fenix.browser.browsingmode.BrowsingMode -import org.mozilla.fenix.components.AppStore -import org.mozilla.fenix.components.appstate.AppAction -import org.mozilla.fenix.components.appstate.AppState - -class ToolbarReducerTest { - @Test - fun `WHEN new tab clicked THEN mode is updated to normal`() { - val appStore = AppStore(AppState(mode = BrowsingMode.Private)) - - appStore.dispatch(AppAction.ToolbarAction.NewTab).joinBlocking() - - assertEquals(BrowsingMode.Normal, appStore.state.mode) - } - - @Test - fun `WHEN new private tab clicked THEN mode is updated to private`() { - val appStore = AppStore(AppState(mode = BrowsingMode.Normal)) - - appStore.dispatch(AppAction.ToolbarAction.NewPrivateTab).joinBlocking() - - assertEquals(BrowsingMode.Private, appStore.state.mode) - } -} diff --git a/app/src/test/java/org/mozilla/fenix/home/WallpapersReducerTest.kt b/app/src/test/java/org/mozilla/fenix/home/WallpapersReducerTest.kt deleted file mode 100644 index 33c0d30ea..000000000 --- a/app/src/test/java/org/mozilla/fenix/home/WallpapersReducerTest.kt +++ /dev/null @@ -1,20 +0,0 @@ -package org.mozilla.fenix.home - -import mozilla.components.support.test.ext.joinBlocking -import org.junit.Assert.assertEquals -import org.junit.Test -import org.mozilla.fenix.browser.browsingmode.BrowsingMode -import org.mozilla.fenix.components.AppStore -import org.mozilla.fenix.components.appstate.AppAction -import org.mozilla.fenix.components.appstate.AppState - -class WallpapersReducerTest { - @Test - fun `WHEN OpenToHome dispatched THEN mode is updated to normal`() { - val appStore = AppStore(AppState(mode = BrowsingMode.Private)) - - appStore.dispatch(AppAction.WallpaperAction.OpenToHome).joinBlocking() - - assertEquals(BrowsingMode.Normal, appStore.state.mode) - } -}