Revert "Bug 1861459 - Remove BrowsingModeManager"

This reverts commit 38af1c1363aa34b564305a1b2c809249e1be5b1b.
fenix/123.0
Matthew Tighe 5 months ago committed by mergify[bot]
parent 04d83965a4
commit d080cf6e70

@ -33,7 +33,7 @@ import org.mozilla.fenix.helpers.HomeActivityTestRule
* *
* Say no to main thread IO! 🙅 * 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 * The number of times we call the `runBlocking` coroutine method on the main thread during this

@ -19,9 +19,9 @@ import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.utils.Settings 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 themeManager Theme will be updated based on state changes.
* @param retrieveWindow Get window to update privacy flags for. * @param retrieveWindow Get window to update privacy flags for.
* @param settings Determine user settings for privacy features. * @param settings Determine user settings for privacy features.

@ -140,6 +140,7 @@ import org.mozilla.fenix.shortcut.NewTabShortcutIntentProcessor.Companion.ACTION
import org.mozilla.fenix.tabhistory.TabHistoryDialogFragment import org.mozilla.fenix.tabhistory.TabHistoryDialogFragment
import org.mozilla.fenix.tabstray.TabsTrayFragment import org.mozilla.fenix.tabstray.TabsTrayFragment
import org.mozilla.fenix.theme.DefaultThemeManager import org.mozilla.fenix.theme.DefaultThemeManager
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.utils.Settings import org.mozilla.fenix.utils.Settings
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.Locale import java.util.Locale
@ -160,7 +161,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
private lateinit var binding: ActivityHomeBinding private lateinit var binding: ActivityHomeBinding
val themeManager by lazy { val themeManager by lazy {
DefaultThemeManager(components.appStore.state.mode, this) createThemeManager()
} }
private var isVisuallyComplete = false private var isVisuallyComplete = false
@ -1149,6 +1150,10 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
return super.getSystemService(name) return super.getSystemService(name)
} }
protected open fun createThemeManager(): ThemeManager {
return DefaultThemeManager(components.appStore.state.mode, this)
}
private fun openPopup(webExtensionState: WebExtensionState) { private fun openPopup(webExtensionState: WebExtensionState) {
val action = NavGraphDirections.actionGlobalWebExtensionActionPopupFragment( val action = NavGraphDirections.actionGlobalWebExtensionActionPopupFragment(
webExtensionId = webExtensionState.id, webExtensionId = webExtensionState.id,

@ -4,6 +4,8 @@
package org.mozilla.fenix.browser.browsingmode package org.mozilla.fenix.browser.browsingmode
import org.mozilla.fenix.utils.Settings
/** /**
* Enum that represents whether or not private browsing is active. * 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 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
}
}

@ -221,11 +221,6 @@ sealed class AppAction : Action {
val wallpaper: Wallpaper, val wallpaper: Wallpaper,
val imageState: Wallpaper.ImageFileState, val imageState: Wallpaper.ImageFileState,
) : WallpaperAction() ) : 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() { sealed class HomeAction : 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()
/** /**
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()
} }
/** /**

@ -10,12 +10,10 @@ import mozilla.components.service.pocket.PocketStory.PocketSponsoredStory
import mozilla.components.service.pocket.ext.recordNewImpression import mozilla.components.service.pocket.ext.recordNewImpression
import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.components.AppStore 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.components.appstate.shopping.ShoppingStateReducer
import org.mozilla.fenix.ext.filterOutTab import org.mozilla.fenix.ext.filterOutTab
import org.mozilla.fenix.ext.getFilteredStories import org.mozilla.fenix.ext.getFilteredStories
import org.mozilla.fenix.home.HomeReducer
import org.mozilla.fenix.home.intent.IntentReducer import org.mozilla.fenix.home.intent.IntentReducer
import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesSelectedCategory import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesSelectedCategory
import org.mozilla.fenix.home.recentsyncedtabs.RecentSyncedTabState import org.mozilla.fenix.home.recentsyncedtabs.RecentSyncedTabState
@ -215,6 +213,25 @@ internal object AppStoreReducer {
is AppAction.UndoPendingDeletionSet -> is AppAction.UndoPendingDeletionSet ->
state.copy(pendingDeletionHistoryItems = state.pendingDeletionHistoryItems - action.historyItems) 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 -> { is AppAction.AppLifecycleAction.ResumeAction -> {
state.copy(isForeground = true) state.copy(isForeground = true)
} }
@ -227,10 +244,8 @@ internal object AppStoreReducer {
) )
is AppAction.ShoppingAction -> ShoppingStateReducer.reduce(state, action) 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.IntentAction -> IntentReducer.reduce(state, action)
is AppAction.ToolbarAction -> ToolbarReducer.reduce(state, action)
is AppAction.TabsTrayAction -> TabsTrayReducer.reduce(state, action)
} }
} }

@ -26,8 +26,8 @@ class BrowserStoreBinding(
flow.distinctUntilChangedBy { it.selectedTabId } flow.distinctUntilChangedBy { it.selectedTabId }
.collectLatest { state -> .collectLatest { state ->
state.selectedTab?.let { tab -> state.selectedTab?.let { tab ->
// Ignore re-emissions of the selected tab from BrowserStore when re-observing due // Ignore re-emissions due to lifecycle events, or other pieces of state like
// to lifecycle events, otherwise pieces of state like [mode] may get overwritten. // [mode] may get overwritten
if (appStore.state.selectedTabId != tab.id) { if (appStore.state.selectedTabId != tab.id) {
appStore.dispatch(AppAction.SelectedTabChanged(tab)) appStore.dispatch(AppAction.SelectedTabChanged(tab))
} }

@ -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)
}
}

@ -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)
}
}

@ -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)
}
}

@ -23,6 +23,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.browser.BrowserAnimator import org.mozilla.fenix.browser.BrowserAnimator
import org.mozilla.fenix.browser.BrowserAnimator.Companion.getToolbarNavOptions import org.mozilla.fenix.browser.BrowserAnimator.Companion.getToolbarNavOptions
import org.mozilla.fenix.browser.BrowserFragmentDirections import org.mozilla.fenix.browser.BrowserFragmentDirections
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.browser.readermode.ReaderModeController import org.mozilla.fenix.browser.readermode.ReaderModeController
import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.components.appstate.AppAction import org.mozilla.fenix.components.appstate.AppAction
@ -177,13 +178,13 @@ class DefaultBrowserToolbarController(
} }
} }
is TabCounterMenu.Item.NewTab -> { is TabCounterMenu.Item.NewTab -> {
appStore.dispatch(AppAction.ToolbarAction.NewTab) appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.Normal))
navController.navigate( navController.navigate(
BrowserFragmentDirections.actionGlobalHome(focusOnAddressBar = true), BrowserFragmentDirections.actionGlobalHome(focusOnAddressBar = true),
) )
} }
is TabCounterMenu.Item.NewPrivateTab -> { is TabCounterMenu.Item.NewPrivateTab -> {
appStore.dispatch(AppAction.ToolbarAction.NewPrivateTab) appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.Private))
navController.navigate( navController.navigate(
BrowserFragmentDirections.actionGlobalHome(focusOnAddressBar = true), BrowserFragmentDirections.actionGlobalHome(focusOnAddressBar = true),
) )

@ -554,12 +554,9 @@ class HomeFragment : Fragment() {
navController = findNavController(), navController = findNavController(),
tabCounter = binding.tabButton, tabCounter = binding.tabButton,
mode = requireComponents.appStore.state.mode, mode = requireComponents.appStore.state.mode,
onBrowsingModeChanged = { newMode -> itemTapped = {
val action = when (newMode) { newMode ->
BrowsingMode.Normal -> AppAction.ToolbarAction.NewTab requireComponents.appStore.dispatch(AppAction.HomeAction.OpenToHome(newMode))
BrowsingMode.Private -> AppAction.ToolbarAction.NewPrivateTab
}
requireComponents.appStore.dispatch(action)
}, },
) )
@ -812,7 +809,7 @@ class HomeFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.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) activity?.window?.setBackgroundDrawableResource(R.drawable.private_home_background_gradient)
} }
@ -828,7 +825,7 @@ class HomeFragment : Fragment() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
if (requireComponents.appStore.state.mode.isPrivate) { if (requireComponents.appStore.state.mode == BrowsingMode.Private) {
activity?.window?.setBackgroundDrawable( activity?.window?.setBackgroundDrawable(
ColorDrawable( ColorDrawable(
ContextCompat.getColor( ContextCompat.getColor(

@ -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)
}
}

@ -29,14 +29,14 @@ import org.mozilla.fenix.tabstray.Page
* @param navController [NavController] used for navigation. * @param navController [NavController] used for navigation.
* @param tabCounter The [TabCounter] that will be setup with event handlers. * @param tabCounter The [TabCounter] that will be setup with event handlers.
* @param mode The current [BrowsingMode]. * @param mode The current [BrowsingMode].
* @param onBrowsingModeChanged Callback to update the [BrowsingMode]. * @param itemTapped Callback to update the [BrowsingMode].
*/ */
class TabCounterView( class TabCounterView(
private val context: Context, private val context: Context,
private val navController: NavController, private val navController: NavController,
private val tabCounter: TabCounter, private val tabCounter: TabCounter,
private val mode: BrowsingMode, private val mode: BrowsingMode,
private val onBrowsingModeChanged: (BrowsingMode) -> Unit, private val itemTapped: (BrowsingMode) -> Unit,
) { ) {
init { init {
@ -103,9 +103,9 @@ class TabCounterView(
*/ */
internal fun onItemTapped(item: TabCounterMenu.Item) { internal fun onItemTapped(item: TabCounterMenu.Item) {
if (item is TabCounterMenu.Item.NewTab) { if (item is TabCounterMenu.Item.NewTab) {
onBrowsingModeChanged(BrowsingMode.Normal) itemTapped(BrowsingMode.Normal)
} else if (item is TabCounterMenu.Item.NewPrivateTab) { } else if (item is TabCounterMenu.Item.NewPrivateTab) {
onBrowsingModeChanged(BrowsingMode.Private) itemTapped(BrowsingMode.Private)
} }
} }
} }

@ -99,10 +99,11 @@ class DefaultBookmarkController(
override fun handleBookmarkTapped(item: BookmarkNode) { override fun handleBookmarkTapped(item: BookmarkNode) {
val fromHomeFragment = val fromHomeFragment =
navController.previousBackStackEntry?.destination?.id == R.id.homeFragment navController.previousBackStackEntry?.destination?.id == R.id.homeFragment
val isPrivate = appStore.state.mode == BrowsingMode.Private
val flags = EngineSession.LoadUrlFlags.select(EngineSession.LoadUrlFlags.ALLOW_JAVASCRIPT_URL) val flags = EngineSession.LoadUrlFlags.select(EngineSession.LoadUrlFlags.ALLOW_JAVASCRIPT_URL)
openInNewTabAndShow( openInNewTabAndShow(
item.url!!, item.url!!,
appStore.state.mode.isPrivate || fromHomeFragment, isPrivate || fromHomeFragment,
BrowserDirection.FromBookmarks, BrowserDirection.FromBookmarks,
flags, flags,
) )

@ -116,7 +116,7 @@ class HistoryFragment : LibraryPageFragment<History>(), UserInteractionHandler,
onBackPressed = requireActivity().onBackPressedDispatcher::onBackPressed, onBackPressed = requireActivity().onBackPressedDispatcher::onBackPressed,
), ),
HistoryTelemetryMiddleware( HistoryTelemetryMiddleware(
isInPrivateMode = requireComponents.appStore.state.mode.isPrivate, isInPrivateMode = requireComponents.appStore.state.mode == BrowsingMode.Private,
), ),
HistorySyncMiddleware( HistorySyncMiddleware(
accountManager = requireContext().components.backgroundServices.accountManager, accountManager = requireContext().components.backgroundServices.accountManager,

@ -21,6 +21,7 @@ import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.GleanMetrics.Wallpapers import org.mozilla.fenix.GleanMetrics.Wallpapers
import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.components.FenixSnackbar import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.appstate.AppAction import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.requireComponents
@ -110,7 +111,7 @@ class WallpaperSettingsFragment : Fragment() {
.setText(view.context.getString(R.string.wallpaper_updated_snackbar_message)) .setText(view.context.getString(R.string.wallpaper_updated_snackbar_message))
.setAction(requireContext().getString(R.string.wallpaper_updated_snackbar_action)) { .setAction(requireContext().getString(R.string.wallpaper_updated_snackbar_action)) {
requireComponents.appStore.dispatch( requireComponents.appStore.dispatch(
AppAction.WallpaperAction.OpenToHome, AppAction.HomeAction.OpenToHome(BrowsingMode.Normal),
) )
findNavController().popBackStack(R.id.homeFragment, false) findNavController().popBackStack(R.id.homeFragment, false)
} }

@ -31,6 +31,7 @@ import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.TabsTray import org.mozilla.fenix.GleanMetrics.TabsTray
import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.collections.CollectionsDialog import org.mozilla.fenix.collections.CollectionsDialog
import org.mozilla.fenix.collections.show import org.mozilla.fenix.collections.show
import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.AppStore
@ -241,12 +242,7 @@ class DefaultTabsTrayController(
*/ */
private fun openNewTab(isPrivate: Boolean) { private fun openNewTab(isPrivate: Boolean) {
val startTime = profiler?.getProfilerTime() val startTime = profiler?.getProfilerTime()
val action = if (isPrivate) { appStore.dispatch(AppAction.HomeAction.OpenToHome(BrowsingMode.fromBoolean(isPrivate)))
AppAction.TabsTrayAction.NewPrivateTab
} else {
AppAction.TabsTrayAction.NewTab
}
appStore.dispatch(action)
navController.navigate( navController.navigate(
TabsTrayFragmentDirections.actionGlobalHome(focusOnAddressBar = true), TabsTrayFragmentDirections.actionGlobalHome(focusOnAddressBar = true),
) )

@ -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 }
}
}

@ -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

@ -20,6 +20,7 @@ import mozilla.components.service.pocket.PocketStory
import mozilla.components.service.pocket.PocketStory.PocketRecommendedStory import mozilla.components.service.pocket.PocketStory.PocketRecommendedStory
import mozilla.components.service.pocket.PocketStory.PocketSponsoredStory import mozilla.components.service.pocket.PocketStory.PocketSponsoredStory
import mozilla.components.service.pocket.PocketStory.PocketSponsoredStoryCaps import mozilla.components.service.pocket.PocketStory.PocketSponsoredStoryCaps
import mozilla.components.support.test.ext.joinBlocking
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertSame import org.junit.Assert.assertSame
@ -27,6 +28,7 @@ import org.junit.Assert.assertTrue
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.mozilla.fenix.browser.browsingmode.BrowsingMode 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
import org.mozilla.fenix.components.appstate.AppAction.MessagingAction.UpdateMessageToShow import org.mozilla.fenix.components.appstate.AppAction.MessagingAction.UpdateMessageToShow
import org.mozilla.fenix.components.appstate.AppState import org.mozilla.fenix.components.appstate.AppState
@ -49,6 +51,7 @@ class AppStoreTest {
private lateinit var context: Context private lateinit var context: Context
private lateinit var accountManager: FxaAccountManager private lateinit var accountManager: FxaAccountManager
private lateinit var onboarding: FenixOnboarding private lateinit var onboarding: FenixOnboarding
private lateinit var browsingModeManager: BrowsingModeManager
private lateinit var appState: AppState private lateinit var appState: AppState
private lateinit var appStore: AppStore private lateinit var appStore: AppStore
private lateinit var recentSyncedTabsList: List<RecentSyncedTab> private lateinit var recentSyncedTabsList: List<RecentSyncedTab>
@ -58,6 +61,7 @@ class AppStoreTest {
context = mockk(relaxed = true) context = mockk(relaxed = true)
accountManager = mockk(relaxed = true) accountManager = mockk(relaxed = true)
onboarding = mockk(relaxed = true) onboarding = mockk(relaxed = true)
browsingModeManager = mockk(relaxed = true)
recentSyncedTabsList = listOf( recentSyncedTabsList = listOf(
RecentSyncedTab( RecentSyncedTab(
deviceDisplayName = "", deviceDisplayName = "",
@ -70,10 +74,12 @@ class AppStoreTest {
every { context.components.backgroundServices.accountManager } returns accountManager every { context.components.backgroundServices.accountManager } returns accountManager
every { onboarding.userHasBeenOnboarded() } returns true every { onboarding.userHasBeenOnboarded() } returns true
every { browsingModeManager.mode } returns BrowsingMode.Normal
appState = AppState( appState = AppState(
collections = emptyList(), collections = emptyList(),
expandedCollections = emptySet(), expandedCollections = emptySet(),
mode = browsingModeManager.mode,
topSites = emptyList(), topSites = emptyList(),
showCollectionPlaceholder = true, showCollectionPlaceholder = true,
recentTabs = emptyList(), recentTabs = emptyList(),
@ -533,4 +539,22 @@ class AppStoreTest {
assertEquals(recentHistory - group2, recentHistory.filterOut("Title2")) assertEquals(recentHistory - group2, recentHistory.filterOut("Title2"))
assertEquals(recentHistory - group3, recentHistory.filterOut("title3")) 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)
}
} }

@ -27,6 +27,8 @@ import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.NavGraphDirections 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.components
import org.mozilla.fenix.ext.getIntentSource import org.mozilla.fenix.ext.getIntentSource
import org.mozilla.fenix.ext.getNavDirections import org.mozilla.fenix.ext.getNavDirections
@ -55,6 +57,8 @@ class ExternalAppBrowserActivityTest {
@Test @Test
fun `navigateToBrowserOnColdStart does nothing for external app browser activity`() { fun `navigateToBrowserOnColdStart does nothing for external app browser activity`() {
val activity = spyk(ExternalAppBrowserActivity()) val activity = spyk(ExternalAppBrowserActivity())
val browsingModeManager: BrowsingModeManager = mockk()
every { browsingModeManager.mode } returns BrowsingMode.Normal
val settings: Settings = mockk() val settings: Settings = mockk()
every { settings.shouldReturnToBrowser } returns true every { settings.shouldReturnToBrowser } returns true

@ -1121,7 +1121,7 @@ class DefaultSessionControlControllerTest {
val wallpaperState = WallpaperState.default.copy( val wallpaperState = WallpaperState.default.copy(
availableWallpapers = makeFakeRemoteWallpapers( availableWallpapers = makeFakeRemoteWallpapers(
THUMBNAILS_SELECTION_COUNT, THUMBNAILS_SELECTION_COUNT,
true, false,
), ),
) )

@ -174,6 +174,6 @@ class TabCounterViewTest {
navController = navController, navController = navController,
tabCounter = tabCounter, tabCounter = tabCounter,
mode = mode, mode = mode,
onBrowsingModeChanged = itemTapped, itemTapped = itemTapped,
) )
} }

@ -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)
}
}

@ -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)
}
}

@ -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)
}
}
Loading…
Cancel
Save