Skip to content

Commit

Permalink
For mozilla-mobile#20654: Handle private mode switching from home beh…
Browse files Browse the repository at this point in the history
…ind search dialog.

Add tests for controller and interactor to handle private mode switches
  • Loading branch information
eliserichards authored and mergify[bot] committed Sep 14, 2021
1 parent c1289f6 commit 52975b4
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 16 deletions.
18 changes: 5 additions & 13 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -469,19 +469,11 @@ class HomeFragment : Fragment() {
openTabsTray()
}

PrivateBrowsingButtonView(
binding.privateBrowsingButton,
browsingModeManager
) { newMode ->
if (newMode == BrowsingMode.Private) {
requireContext().settings().incrementNumTimesPrivateModeOpened()
}

if (onboarding.userHasBeenOnboarded()) {
homeFragmentStore.dispatch(
HomeFragmentAction.ModeChange(Mode.fromBrowsingMode(newMode))
)
}
PrivateBrowsingButtonView(binding.privateBrowsingButton, browsingModeManager) { newMode ->
sessionControlInteractor.onPrivateModeButtonClicked(
newMode,
onboarding.userHasBeenOnboarded()
)
}

consumeFrom(requireComponents.core.store) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ data class Tab(
* @property expandedCollections A set containing the ids of the [TabCollection] that are expanded
* in the [HomeFragment].
* @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 tip The current [Tip] to show on the [HomeFragment].
* @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 recentBookmarks The list of recently saved [BookmarkNode]s to show on the [HomeFragment].
* @property historyMetadata The list of [HistoryMetadataGroup].
* @property pocketArticles The list of [PocketRecommendedStory].
*/
data class HomeFragmentState(
val collections: List<TabCollection> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import mozilla.components.support.ktx.kotlin.isUrl
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.BrowserFragmentDirections
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.collections.SaveCollectionStep
import org.mozilla.fenix.components.TabCollectionStorage
Expand All @@ -40,11 +41,14 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.openSetDefaultBrowserOption
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.HomeFragment
import org.mozilla.fenix.home.HomeFragmentAction
import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.home.HomeFragmentStore
import org.mozilla.fenix.home.Mode
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.settings.SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS
import org.mozilla.fenix.utils.Settings
import mozilla.components.feature.tab.collections.Tab as ComponentTab

Expand Down Expand Up @@ -178,6 +182,11 @@ interface SessionControlController {
* @see [ExperimentCardInteractor.onCloseExperimentCardClicked]
*/
fun handleCloseExperimentCard()

/**
* @see [TabSessionInteractor.onPrivateModeButtonClicked]
*/
fun handlePrivateModeButtonClicked(newMode: BrowsingMode, userHasBeenOnboarded: Boolean)
}

@Suppress("TooManyFunctions", "LargeClass")
Expand Down Expand Up @@ -296,8 +305,7 @@ class DefaultSessionControlController(
override fun handlePrivateBrowsingLearnMoreClicked() {
dismissSearchDialogIfDisplayed()
activity.openToBrowserAndLoad(
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic
(SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS),
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic(PRIVATE_BROWSING_MYTHS),
newTab = true,
from = BrowserDirection.FromHome
)
Expand Down Expand Up @@ -561,4 +569,27 @@ class DefaultSessionControlController(
metrics.track(Event.CloseExperimentCardClicked)
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
)
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import mozilla.components.concept.storage.HistoryMetadataKey
import mozilla.components.feature.tab.collections.Tab
import mozilla.components.feature.tab.collections.TabCollection
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.historymetadata.HistoryMetadataGroup
import org.mozilla.fenix.historymetadata.controller.HistoryMetadataController
Expand All @@ -27,6 +28,11 @@ interface TabSessionInteractor {
* "Common myths about private browsing" link in private mode.
*/
fun onPrivateBrowsingLearnMoreClicked()

/**
* Called when a user clicks on the Private Mode button on the homescreen.
*/
fun onPrivateModeButtonClicked(newMode: BrowsingMode, userHasBeenOnboarded: Boolean)
}

/**
Expand Down Expand Up @@ -309,6 +315,10 @@ class SessionControlInteractor(
controller.handlePrivateBrowsingLearnMoreClicked()
}

override fun onPrivateModeButtonClicked(newMode: BrowsingMode, userHasBeenOnboarded: Boolean) {
controller.handlePrivateModeButtonClicked(newMode, userHasBeenOnboarded)
}

override fun onPasteAndGo(clipboardText: String) {
controller.handlePasteAndGo(clipboardText)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package org.mozilla.fenix.home

import androidx.navigation.NavController
import androidx.navigation.NavDirections
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.spyk
Expand Down Expand Up @@ -43,6 +45,8 @@ import org.junit.Test
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
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.TabCollectionStorage
import org.mozilla.fenix.components.metrics.Event
Expand Down Expand Up @@ -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(
hideOnboarding: () -> Unit = { },
registerCollectionStorageObserver: () -> Unit = { },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import mozilla.components.feature.tab.collections.Tab
import mozilla.components.feature.tab.collections.TabCollection
import org.junit.Before
import org.junit.Test
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.historymetadata.HistoryMetadataGroup
import org.mozilla.fenix.historymetadata.controller.HistoryMetadataController
import org.mozilla.fenix.home.recentbookmarks.controller.RecentBookmarksController
Expand Down Expand Up @@ -227,4 +228,13 @@ class SessionControlInteractorTest {
interactor.onShowAllBookmarksClicked()
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) }
}
}

0 comments on commit 52975b4

Please sign in to comment.