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 5db390c2ec..31cc67e95a 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayController.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/TabsTrayController.kt @@ -430,6 +430,7 @@ class DefaultTabsTrayController( override fun handleTabSelected(tab: TabSessionState, source: String?) { TabsTray.openedExistingTab.record(TabsTray.OpenedExistingTabExtra(source ?: "unknown")) tabsUseCases.selectTab(tab.id) + browsingModeManager.mode = BrowsingMode.fromBoolean(tab.content.private) handleNavigateToBrowser() } diff --git a/app/src/test/java/org/mozilla/fenix/tabstray/DefaultTabsTrayControllerTest.kt b/app/src/test/java/org/mozilla/fenix/tabstray/DefaultTabsTrayControllerTest.kt index 63b6282d5b..b3cc09db40 100644 --- a/app/src/test/java/org/mozilla/fenix/tabstray/DefaultTabsTrayControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/tabstray/DefaultTabsTrayControllerTest.kt @@ -18,6 +18,7 @@ import io.mockk.spyk import io.mockk.unmockkStatic import io.mockk.verify import io.mockk.verifyOrder +import mozilla.components.browser.state.action.TabListAction import mozilla.components.browser.state.selector.findTab import mozilla.components.browser.state.selector.getNormalOrPrivateTabs import mozilla.components.browser.state.state.BrowserState @@ -31,6 +32,7 @@ import mozilla.components.browser.storage.sync.TabEntry import mozilla.components.concept.base.profiler.Profiler import mozilla.components.feature.tabs.TabsUseCases import mozilla.components.service.glean.testing.GleanTestRule +import mozilla.components.support.test.ext.joinBlocking import mozilla.components.support.test.libstate.ext.waitUntilIdle import mozilla.components.support.test.robolectric.testContext import org.junit.Assert.assertEquals @@ -48,7 +50,9 @@ 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.browser.browsingmode.BrowsingModeManager +import org.mozilla.fenix.browser.browsingmode.DefaultBrowsingModeManager import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.ext.maxActiveTime import org.mozilla.fenix.ext.potentialInactiveTabs @@ -868,6 +872,49 @@ class DefaultTabsTrayControllerTest { verify { controller.handleNavigateToBrowser() } } + @Test + fun `GIVEN a private tab is open and selected with a normal tab also open WHEN the private tab is closed and private home page shown and normal tab is selected from tabs tray THEN normal tab is displayed `() { + val normalTab = TabSessionState( + content = ContentState(url = "https://simulate.com", private = false), + id = "normalTab", + ) + val privateTab = TabSessionState( + content = ContentState(url = "https://mozilla.com", private = true), + id = "privateTab", + ) + browserStore = BrowserStore( + initialState = BrowserState( + tabs = listOf(normalTab, privateTab), + ), + ) + browsingModeManager = spyk( + DefaultBrowsingModeManager( + _mode = BrowsingMode.Private, + settings = settings, + modeDidChange = mockk(relaxed = true), + ), + ) + val controller = spyk(createController()) + + try { + mockkStatic("mozilla.components.browser.state.selector.SelectorsKt") + browserStore.dispatch(TabListAction.SelectTabAction(privateTab.id)).joinBlocking() + controller.handleTabSelected(privateTab, null) + + assertEquals(privateTab.id, browserStore.state.selectedTabId) + assertEquals(true, browsingModeManager.mode.isPrivate) + + controller.handleTabDeletion("privateTab") + browserStore.dispatch(TabListAction.SelectTabAction(normalTab.id)).joinBlocking() + controller.handleTabSelected(normalTab, null) + + assertEquals(normalTab.id, browserStore.state.selectedTabId) + assertEquals(false, browsingModeManager.mode.isPrivate) + } finally { + unmockkStatic("mozilla.components.browser.state.selector.SelectorsKt") + } + } + @Test fun `GIVEN a normal tab is selected WHEN the last private tab is deleted THEN that private tab is removed and an undo snackbar is shown and original normal tab is still displayed`() { val currentTab = TabSessionState(content = ContentState(url = "https://simulate.com", private = false), id = "currentTab")