Bug 1971400 - Part 1 - Create new Secure Tab Binding r=android-reviewers,npoon

Differential Revision: https://phabricator.services.mozilla.com/D255849
This commit is contained in:
Noah Bond authored and [email protected] committed 2025-07-22 02:41:44 +00:00
1 parent 9495e7aaac
commit 1467537f8a
3 files changed
+160 -5

No files matched your search

@@ -0,0 +1,48 @@
/* 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.tabstray.binding
import androidx.fragment.app.Fragment
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import mozilla.components.lib.state.helpers.AbstractBinding
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifAnyChanged
import org.mozilla.fenix.ext.removeSecure
import org.mozilla.fenix.ext.secure
import org.mozilla.fenix.tabstray.Page
import org.mozilla.fenix.tabstray.TabsTrayState
import org.mozilla.fenix.tabstray.TabsTrayStore
import org.mozilla.fenix.tabstray.ui.TabManagementFragment
import org.mozilla.fenix.utils.Settings
/**
* Sets [TabManagementFragment] flags to secure when private tabs list is selected.
*/
@OptIn(ExperimentalCoroutinesApi::class)
class SecureTabManagerBinding(
store: TabsTrayStore,
private val settings: Settings,
private val fragment: Fragment,
) : AbstractBinding<TabsTrayState>(store) {
override suspend fun onState(flow: Flow<TabsTrayState>) {
flow.map { it }
.ifAnyChanged { state ->
arrayOf(
state.selectedPage,
)
}
.collect { state ->
if (state.selectedPage == Page.PrivateTabs &&
!settings.allowScreenshotsInPrivateMode
) {
fragment.secure()
} else if (!settings.lastKnownMode.isPrivate) {
fragment.removeSecure()
}
}
}
}
@@ -75,7 +75,6 @@ import org.mozilla.fenix.tabstray.DefaultTabsTrayController
import org.mozilla.fenix.tabstray.DefaultTabsTrayInteractor
import org.mozilla.fenix.tabstray.InactiveTabsBinding
import org.mozilla.fenix.tabstray.Page
import org.mozilla.fenix.tabstray.SecureTabsTrayBinding
import org.mozilla.fenix.tabstray.TabSheetBehaviorManager
import org.mozilla.fenix.tabstray.TabsTrayAccessPoint
import org.mozilla.fenix.tabstray.TabsTrayAction
@@ -85,6 +84,7 @@ import org.mozilla.fenix.tabstray.TabsTrayState
import org.mozilla.fenix.tabstray.TabsTrayStore
import org.mozilla.fenix.tabstray.TabsTrayTelemetryMiddleware
import org.mozilla.fenix.tabstray.TraySheetBehaviorCallback
import org.mozilla.fenix.tabstray.binding.SecureTabManagerBinding
import org.mozilla.fenix.tabstray.browser.TabSorter
import org.mozilla.fenix.tabstray.syncedtabs.SyncedTabsIntegration
import org.mozilla.fenix.tabstray.ui.fab.TabsTrayFab
@@ -118,7 +118,7 @@ class TabManagementFragment : AppCompatDialogFragment() {
@VisibleForTesting internal lateinit var trayBehaviorManager: TabSheetBehaviorManager
private val inactiveTabsBinding = ViewBoundFeatureWrapper<InactiveTabsBinding>()
private val secureTabsTrayBinding = ViewBoundFeatureWrapper<SecureTabsTrayBinding>()
private val secureTabManagerBinding = ViewBoundFeatureWrapper<SecureTabManagerBinding>()
private val tabsFeature = ViewBoundFeatureWrapper<TabsFeature>()
private val syncedTabsIntegration = ViewBoundFeatureWrapper<SyncedTabsIntegration>()
@@ -503,12 +503,11 @@ class TabManagementFragment : AppCompatDialogFragment() {
view = view,
)
secureTabsTrayBinding.set(
feature = SecureTabsTrayBinding(
secureTabManagerBinding.set(
feature = SecureTabManagerBinding(
store = tabsTrayStore,
settings = requireComponents.settings,
fragment = this,
dialog = dialog as TabsTrayDialog,
),
owner = this,
view = view,
@@ -0,0 +1,108 @@
/* 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.tabstray.binding
import androidx.fragment.app.Fragment
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.ext.removeSecure
import org.mozilla.fenix.ext.secure
import org.mozilla.fenix.tabstray.Page
import org.mozilla.fenix.tabstray.TabsTrayAction
import org.mozilla.fenix.tabstray.TabsTrayState
import org.mozilla.fenix.tabstray.TabsTrayStore
import org.mozilla.fenix.utils.Settings
class SecureTabManagerBindingTest {
@OptIn(ExperimentalCoroutinesApi::class)
@get:Rule
val coroutinesTestRule = MainCoroutineRule()
private val settings: Settings = mockk(relaxed = true)
private val fragment: Fragment = mockk(relaxed = true)
@Before
fun setup() {
every { fragment.secure() } just Runs
every { fragment.removeSecure() } just Runs
}
@Test
fun `WHEN tab selected page switches to private THEN set fragment to secure`() {
val tabsTrayStore = TabsTrayStore(TabsTrayState())
val secureTabManagerBinding = SecureTabManagerBinding(
store = tabsTrayStore,
settings = settings,
fragment = fragment,
)
secureTabManagerBinding.start()
tabsTrayStore.dispatch(TabsTrayAction.PageSelected(Page.positionToPage(Page.PrivateTabs.ordinal)))
tabsTrayStore.waitUntilIdle()
verify { fragment.secure() }
}
@Test
fun `WHEN tab selected page switches to private and allowScreenshotsInPrivateMode true THEN set fragment to un-secure`() {
val tabsTrayStore = TabsTrayStore(TabsTrayState())
val secureTabManagerBinding = SecureTabManagerBinding(
store = tabsTrayStore,
settings = settings,
fragment = fragment,
)
every { settings.allowScreenshotsInPrivateMode } returns true
secureTabManagerBinding.start()
tabsTrayStore.dispatch(TabsTrayAction.PageSelected(Page.positionToPage(Page.PrivateTabs.ordinal)))
tabsTrayStore.waitUntilIdle()
verify { fragment.removeSecure() }
}
@Test
fun `GIVEN not in private mode WHEN tab selected page switches to normal tabs from private THEN set fragment to un-secure`() {
every { settings.lastKnownMode.isPrivate } returns false
val tabsTrayStore = TabsTrayStore(TabsTrayState())
val secureTabManagerBinding = SecureTabManagerBinding(
store = tabsTrayStore,
settings = settings,
fragment = fragment,
)
secureTabManagerBinding.start()
tabsTrayStore.dispatch(TabsTrayAction.PageSelected(Page.positionToPage(Page.NormalTabs.ordinal)))
tabsTrayStore.waitUntilIdle()
verify { fragment.removeSecure() }
}
@Test
fun `GIVEN private mode WHEN tab selected page switches to normal tabs from private THEN do nothing`() {
every { settings.lastKnownMode.isPrivate } returns true
val tabsTrayStore = TabsTrayStore(TabsTrayState())
val secureTabManagerBinding = SecureTabManagerBinding(
store = tabsTrayStore,
settings = settings,
fragment = fragment,
)
secureTabManagerBinding.start()
tabsTrayStore.dispatch(TabsTrayAction.PageSelected(Page.positionToPage(Page.NormalTabs.ordinal)))
tabsTrayStore.waitUntilIdle()
verify(exactly = 0) { fragment.removeSecure() }
}
}