For #20654: Handle private mode switching from home behind search dialog.

Add tests for controller and interactor to handle private mode switches
upstream-sync
Elise Richards 3 years ago committed by mergify[bot]
parent c1289f657d
commit 52975b4f05

@ -469,19 +469,11 @@ class HomeFragment : Fragment() {
openTabsTray() openTabsTray()
} }
PrivateBrowsingButtonView( PrivateBrowsingButtonView(binding.privateBrowsingButton, browsingModeManager) { newMode ->
binding.privateBrowsingButton, sessionControlInteractor.onPrivateModeButtonClicked(
browsingModeManager newMode,
) { newMode -> onboarding.userHasBeenOnboarded()
if (newMode == BrowsingMode.Private) { )
requireContext().settings().incrementNumTimesPrivateModeOpened()
}
if (onboarding.userHasBeenOnboarded()) {
homeFragmentStore.dispatch(
HomeFragmentAction.ModeChange(Mode.fromBrowsingMode(newMode))
)
}
} }
consumeFrom(requireComponents.core.store) { consumeFrom(requireComponents.core.store) {

@ -43,13 +43,14 @@ data class Tab(
* @property expandedCollections A set containing the ids of the [TabCollection] that are expanded * @property expandedCollections A set containing the ids of the [TabCollection] that are expanded
* in the [HomeFragment]. * in the [HomeFragment].
* @property mode The state of the [HomeFragment] UI. * @property mode The state of the [HomeFragment] UI.
* @property tabs The list of opened [Tab] in the [HomeFragment].
* @property topSites The list of [TopSite] in the [HomeFragment]. * @property topSites The list of [TopSite] in the [HomeFragment].
* @property tip The current [Tip] to show on the [HomeFragment]. * @property tip The current [Tip] to show on the [HomeFragment].
* @property showCollectionPlaceholder If true, shows a placeholder when there are no collections. * @property showCollectionPlaceholder If true, shows a placeholder when there are no collections.
* @property showSetAsDefaultBrowserCard If true, shows the default browser card
* @property recentTabs The list of recent [TabSessionState] in the [HomeFragment]. * @property recentTabs The list of recent [TabSessionState] in the [HomeFragment].
* @property recentBookmarks The list of recently saved [BookmarkNode]s to show on the [HomeFragment]. * @property recentBookmarks The list of recently saved [BookmarkNode]s to show on the [HomeFragment].
* @property historyMetadata The list of [HistoryMetadataGroup]. * @property historyMetadata The list of [HistoryMetadataGroup].
* @property pocketArticles The list of [PocketRecommendedStory].
*/ */
data class HomeFragmentState( data class HomeFragmentState(
val collections: List<TabCollection> = emptyList(), val collections: List<TabCollection> = emptyList(),

@ -29,6 +29,7 @@ import mozilla.components.support.ktx.kotlin.isUrl
import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.BrowserDirection
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.BrowserFragmentDirections
import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.collections.SaveCollectionStep import org.mozilla.fenix.collections.SaveCollectionStep
import org.mozilla.fenix.components.TabCollectionStorage import org.mozilla.fenix.components.TabCollectionStorage
@ -40,11 +41,14 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.metrics import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.openSetDefaultBrowserOption import org.mozilla.fenix.ext.openSetDefaultBrowserOption
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.HomeFragment import org.mozilla.fenix.home.HomeFragment
import org.mozilla.fenix.home.HomeFragmentAction import org.mozilla.fenix.home.HomeFragmentAction
import org.mozilla.fenix.home.HomeFragmentDirections import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.home.HomeFragmentStore import org.mozilla.fenix.home.HomeFragmentStore
import org.mozilla.fenix.home.Mode
import org.mozilla.fenix.settings.SupportUtils import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.settings.SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS
import org.mozilla.fenix.utils.Settings import org.mozilla.fenix.utils.Settings
import mozilla.components.feature.tab.collections.Tab as ComponentTab import mozilla.components.feature.tab.collections.Tab as ComponentTab
@ -178,6 +182,11 @@ interface SessionControlController {
* @see [ExperimentCardInteractor.onCloseExperimentCardClicked] * @see [ExperimentCardInteractor.onCloseExperimentCardClicked]
*/ */
fun handleCloseExperimentCard() fun handleCloseExperimentCard()
/**
* @see [TabSessionInteractor.onPrivateModeButtonClicked]
*/
fun handlePrivateModeButtonClicked(newMode: BrowsingMode, userHasBeenOnboarded: Boolean)
} }
@Suppress("TooManyFunctions", "LargeClass") @Suppress("TooManyFunctions", "LargeClass")
@ -296,8 +305,7 @@ class DefaultSessionControlController(
override fun handlePrivateBrowsingLearnMoreClicked() { override fun handlePrivateBrowsingLearnMoreClicked() {
dismissSearchDialogIfDisplayed() dismissSearchDialogIfDisplayed()
activity.openToBrowserAndLoad( activity.openToBrowserAndLoad(
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic searchTermOrURL = SupportUtils.getGenericSumoURLForTopic(PRIVATE_BROWSING_MYTHS),
(SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS),
newTab = true, newTab = true,
from = BrowserDirection.FromHome from = BrowserDirection.FromHome
) )
@ -561,4 +569,27 @@ class DefaultSessionControlController(
metrics.track(Event.CloseExperimentCardClicked) metrics.track(Event.CloseExperimentCardClicked)
fragmentStore.dispatch(HomeFragmentAction.RemoveSetDefaultBrowserCard) fragmentStore.dispatch(HomeFragmentAction.RemoveSetDefaultBrowserCard)
} }
override fun handlePrivateModeButtonClicked(
newMode: BrowsingMode,
userHasBeenOnboarded: Boolean
) {
if (newMode == BrowsingMode.Private) {
activity.settings().incrementNumTimesPrivateModeOpened()
}
if (userHasBeenOnboarded) {
fragmentStore.dispatch(
HomeFragmentAction.ModeChange(Mode.fromBrowsingMode(newMode))
)
if (navController.currentDestination?.id == R.id.searchDialogFragment) {
navController.navigate(
BrowserFragmentDirections.actionGlobalSearchDialog(
sessionId = null
)
)
}
}
}
} }

@ -9,6 +9,7 @@ import mozilla.components.concept.storage.HistoryMetadataKey
import mozilla.components.feature.tab.collections.Tab import mozilla.components.feature.tab.collections.Tab
import mozilla.components.feature.tab.collections.TabCollection import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.components.tips.Tip import org.mozilla.fenix.components.tips.Tip
import org.mozilla.fenix.historymetadata.HistoryMetadataGroup import org.mozilla.fenix.historymetadata.HistoryMetadataGroup
import org.mozilla.fenix.historymetadata.controller.HistoryMetadataController import org.mozilla.fenix.historymetadata.controller.HistoryMetadataController
@ -27,6 +28,11 @@ interface TabSessionInteractor {
* "Common myths about private browsing" link in private mode. * "Common myths about private browsing" link in private mode.
*/ */
fun onPrivateBrowsingLearnMoreClicked() fun onPrivateBrowsingLearnMoreClicked()
/**
* Called when a user clicks on the Private Mode button on the homescreen.
*/
fun onPrivateModeButtonClicked(newMode: BrowsingMode, userHasBeenOnboarded: Boolean)
} }
/** /**
@ -309,6 +315,10 @@ class SessionControlInteractor(
controller.handlePrivateBrowsingLearnMoreClicked() controller.handlePrivateBrowsingLearnMoreClicked()
} }
override fun onPrivateModeButtonClicked(newMode: BrowsingMode, userHasBeenOnboarded: Boolean) {
controller.handlePrivateModeButtonClicked(newMode, userHasBeenOnboarded)
}
override fun onPasteAndGo(clipboardText: String) { override fun onPasteAndGo(clipboardText: String) {
controller.handlePasteAndGo(clipboardText) controller.handlePasteAndGo(clipboardText)
} }

@ -6,7 +6,9 @@ package org.mozilla.fenix.home
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavDirections import androidx.navigation.NavDirections
import io.mockk.Runs
import io.mockk.every import io.mockk.every
import io.mockk.just
import io.mockk.mockk import io.mockk.mockk
import io.mockk.mockkStatic import io.mockk.mockkStatic
import io.mockk.spyk import io.mockk.spyk
@ -43,6 +45,8 @@ import org.junit.Test
import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.BrowserDirection
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.BrowserFragmentDirections
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.components.Analytics import org.mozilla.fenix.components.Analytics
import org.mozilla.fenix.components.TabCollectionStorage import org.mozilla.fenix.components.TabCollectionStorage
import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.metrics.Event
@ -721,6 +725,91 @@ class DefaultSessionControlControllerTest {
} }
} }
@Test
fun `WHEN private mode button is selected from home THEN handle mode change`() {
every { navController.currentDestination } returns mockk {
every { id } returns R.id.homeFragment
}
every { settings.incrementNumTimesPrivateModeOpened() } just Runs
val newMode = BrowsingMode.Private
val hasBeenOnboarded = true
createController().handlePrivateModeButtonClicked(newMode, hasBeenOnboarded)
verify {
settings.incrementNumTimesPrivateModeOpened()
HomeFragmentAction.ModeChange(Mode.fromBrowsingMode(newMode))
}
}
@Test
fun `WHEN private mode is selected on home from behind search THEN handle mode change`() {
every { navController.currentDestination } returns mockk {
every { id } returns R.id.searchDialogFragment
}
every { settings.incrementNumTimesPrivateModeOpened() } just Runs
val url = "https://mozilla.org"
val tab = createTab(
id = "otherTab",
url = url,
private = false,
engineSession = mockk(relaxed = true)
)
store.dispatch(TabListAction.AddTabAction(tab, select = true)).joinBlocking()
val newMode = BrowsingMode.Private
val hasBeenOnboarded = true
createController().handlePrivateModeButtonClicked(newMode, hasBeenOnboarded)
verify {
settings.incrementNumTimesPrivateModeOpened()
HomeFragmentAction.ModeChange(Mode.fromBrowsingMode(newMode))
navController.navigate(
BrowserFragmentDirections.actionGlobalSearchDialog(
sessionId = null
)
)
}
}
@Test
fun `WHEN private mode is deselected on home from behind search THEN handle mode change`() {
every { navController.currentDestination } returns mockk {
every { id } returns R.id.searchDialogFragment
}
val url = "https://mozilla.org"
val tab = createTab(
id = "otherTab",
url = url,
private = true,
engineSession = mockk(relaxed = true)
)
store.dispatch(TabListAction.AddTabAction(tab, select = true)).joinBlocking()
val newMode = BrowsingMode.Normal
val hasBeenOnboarded = true
createController().handlePrivateModeButtonClicked(newMode, hasBeenOnboarded)
verify(exactly = 0) {
settings.incrementNumTimesPrivateModeOpened()
}
verify {
HomeFragmentAction.ModeChange(Mode.fromBrowsingMode(newMode))
navController.navigate(
BrowserFragmentDirections.actionGlobalSearchDialog(
sessionId = null
)
)
}
}
private fun createController( private fun createController(
hideOnboarding: () -> Unit = { }, hideOnboarding: () -> Unit = { },
registerCollectionStorageObserver: () -> Unit = { }, registerCollectionStorageObserver: () -> Unit = { },

@ -15,6 +15,7 @@ import mozilla.components.feature.tab.collections.Tab
import mozilla.components.feature.tab.collections.TabCollection import mozilla.components.feature.tab.collections.TabCollection
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.historymetadata.HistoryMetadataGroup import org.mozilla.fenix.historymetadata.HistoryMetadataGroup
import org.mozilla.fenix.historymetadata.controller.HistoryMetadataController import org.mozilla.fenix.historymetadata.controller.HistoryMetadataController
import org.mozilla.fenix.home.recentbookmarks.controller.RecentBookmarksController import org.mozilla.fenix.home.recentbookmarks.controller.RecentBookmarksController
@ -227,4 +228,13 @@ class SessionControlInteractorTest {
interactor.onShowAllBookmarksClicked() interactor.onShowAllBookmarksClicked()
verify { recentBookmarksController.handleShowAllBookmarksClicked() } verify { recentBookmarksController.handleShowAllBookmarksClicked() }
} }
@Test
fun `WHEN private mode button is clicked THEN the click is handled`() {
val newMode = BrowsingMode.Private
val hasBeenOnboarded = true
interactor.onPrivateModeButtonClicked(newMode, hasBeenOnboarded)
verify { controller.handlePrivateModeButtonClicked(newMode, hasBeenOnboarded) }
}
} }

Loading…
Cancel
Save