Revert "Bug 1861459 - Remove Home Screen usages of BrowsingModeManager"
This reverts commit 86e0f3c0ef1dd3a16194eeaff330c8517d18b49f.fenix/123.0
parent
d080cf6e70
commit
452f7147d0
@ -1,56 +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
|
||||
|
||||
import android.view.Window
|
||||
import android.view.WindowManager
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
||||
import kotlinx.coroutines.withContext
|
||||
import mozilla.components.lib.state.helpers.AbstractBinding
|
||||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
|
||||
import org.mozilla.fenix.components.AppStore
|
||||
import org.mozilla.fenix.components.appstate.AppState
|
||||
import org.mozilla.fenix.theme.ThemeManager
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
/**
|
||||
* Binding to react to Private Browsing Mode changes in AppState.
|
||||
*
|
||||
* @param appStore AppStore to observe state changes from.
|
||||
* @param themeManager Theme will be updated based on state changes.
|
||||
* @param retrieveWindow Get window to update privacy flags for.
|
||||
* @param settings Determine user settings for privacy features.
|
||||
* @param ioDispatcher Dispatcher to launch disk reads. Exposed for test.
|
||||
*/
|
||||
class BrowsingModeBinding(
|
||||
appStore: AppStore,
|
||||
private val themeManager: ThemeManager,
|
||||
private val retrieveWindow: () -> Window,
|
||||
private val settings: Settings,
|
||||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
|
||||
) : AbstractBinding<AppState>(appStore) {
|
||||
override suspend fun onState(flow: Flow<AppState>) {
|
||||
flow.distinctUntilChangedBy { it.mode }.collect {
|
||||
themeManager.currentTheme = it.mode
|
||||
setWindowPrivacy(it.mode)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun setWindowPrivacy(mode: BrowsingMode) {
|
||||
if (mode == BrowsingMode.Private) {
|
||||
val allowScreenshots = withContext(ioDispatcher) {
|
||||
settings.allowScreenshotsInPrivateMode
|
||||
}
|
||||
if (!allowScreenshots) {
|
||||
retrieveWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
} else {
|
||||
retrieveWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,49 +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
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import mozilla.components.lib.state.Middleware
|
||||
import mozilla.components.lib.state.MiddlewareContext
|
||||
import org.mozilla.fenix.components.appstate.AppAction
|
||||
import org.mozilla.fenix.components.appstate.AppState
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
/**
|
||||
* Middleware for controlling side-effects relating to Private Browsing Mode.
|
||||
*
|
||||
* @param settings Used to update disk-cache related PBM data.
|
||||
* @param scope Scope used for disk writes and reads. Exposed for test overrides.
|
||||
*/
|
||||
class BrowsingModePersistenceMiddleware(
|
||||
private val settings: Settings,
|
||||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO),
|
||||
) : Middleware<AppState, AppAction> {
|
||||
override fun invoke(
|
||||
context: MiddlewareContext<AppState, AppAction>,
|
||||
next: (AppAction) -> Unit,
|
||||
action: AppAction,
|
||||
) {
|
||||
val initialMode = context.state.mode
|
||||
next(action)
|
||||
val updatedMode = context.state.mode
|
||||
if (initialMode != context.state.mode) {
|
||||
scope.launch {
|
||||
settings.lastKnownMode = updatedMode
|
||||
}
|
||||
}
|
||||
when (action) {
|
||||
is AppAction.Init -> {
|
||||
scope.launch {
|
||||
val mode = settings.lastKnownMode
|
||||
context.store.dispatch(AppAction.BrowsingModeLoaded(mode))
|
||||
}
|
||||
}
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
@ -1,101 +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
|
||||
|
||||
import android.view.Window
|
||||
import android.view.WindowManager
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import mozilla.components.support.test.ext.joinBlocking
|
||||
import mozilla.components.support.test.libstate.ext.waitUntilIdle
|
||||
import mozilla.components.support.test.mock
|
||||
import mozilla.components.support.test.rule.MainCoroutineRule
|
||||
import mozilla.components.support.test.rule.runTestOnMain
|
||||
import mozilla.components.support.test.whenever
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.mockito.Mockito.anyInt
|
||||
import org.mockito.Mockito.never
|
||||
import org.mockito.Mockito.times
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
|
||||
import org.mozilla.fenix.components.AppStore
|
||||
import org.mozilla.fenix.components.appstate.AppAction
|
||||
import org.mozilla.fenix.theme.ThemeManager
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
class BrowsingModeBindingTest {
|
||||
@get:Rule
|
||||
val coroutinesTestRule = MainCoroutineRule()
|
||||
|
||||
private lateinit var themeManager: ThemeManager
|
||||
private lateinit var window: Window
|
||||
private lateinit var settings: Settings
|
||||
private val retrieveWindow = { window }
|
||||
|
||||
private lateinit var appStore: AppStore
|
||||
|
||||
private lateinit var binding: BrowsingModeBinding
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
themeManager = mock()
|
||||
window = mock()
|
||||
settings = mock()
|
||||
whenever(window.clearFlags(anyInt())).then { }
|
||||
|
||||
appStore = AppStore()
|
||||
// wait for Init action
|
||||
appStore.waitUntilIdle()
|
||||
binding = BrowsingModeBinding(
|
||||
appStore,
|
||||
themeManager,
|
||||
retrieveWindow,
|
||||
settings,
|
||||
coroutinesTestRule.testDispatcher,
|
||||
)
|
||||
binding.start()
|
||||
}
|
||||
|
||||
@After
|
||||
fun teardown() {
|
||||
binding.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN mode updated THEN theme manager is also updated`() = runTest {
|
||||
appStore.dispatch(AppAction.ModeChange(BrowsingMode.Private)).joinBlocking()
|
||||
|
||||
verify(themeManager).currentTheme = BrowsingMode.Private
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN screenshots not allowed in private mode WHEN mode changes to private THEN secure flag added to window`() = runTestOnMain {
|
||||
whenever(window.addFlags(anyInt())).then { }
|
||||
whenever(settings.allowScreenshotsInPrivateMode).thenReturn(false)
|
||||
|
||||
appStore.dispatch(AppAction.ModeChange(BrowsingMode.Private)).joinBlocking()
|
||||
|
||||
verify(window).addFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN screenshots allowed in private mode when mode changes to private THEN secure flag not added to window`() = runTest {
|
||||
whenever(settings.allowScreenshotsInPrivateMode).thenReturn(true)
|
||||
|
||||
appStore.dispatch(AppAction.ModeChange(BrowsingMode.Private)).joinBlocking()
|
||||
|
||||
verify(window, never()).addFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN mode changed to normal THEN secure flag cleared from window`() {
|
||||
appStore.dispatch(AppAction.ModeChange(BrowsingMode.Private)).joinBlocking()
|
||||
appStore.dispatch(AppAction.ModeChange(BrowsingMode.Normal)).joinBlocking()
|
||||
|
||||
verify(window, times(2)).clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
}
|
@ -1,65 +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
|
||||
|
||||
import kotlinx.coroutines.test.advanceUntilIdle
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import mozilla.components.support.test.ext.joinBlocking
|
||||
import mozilla.components.support.test.libstate.ext.waitUntilIdle
|
||||
import mozilla.components.support.test.mock
|
||||
import mozilla.components.support.test.rule.MainCoroutineRule
|
||||
import mozilla.components.support.test.whenever
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
|
||||
import org.mozilla.fenix.components.AppStore
|
||||
import org.mozilla.fenix.components.appstate.AppAction
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
class BrowsingModePersistenceMiddlewareTest {
|
||||
@get:Rule
|
||||
val coroutinesTestRule = MainCoroutineRule()
|
||||
|
||||
private lateinit var settings: Settings
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
settings = mock()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN store initialization intercepted THEN mode read from settings and update dispatched`() = runTest {
|
||||
val cachedMode = BrowsingMode.Private
|
||||
whenever(settings.lastKnownMode).thenReturn(cachedMode)
|
||||
|
||||
val middleware = BrowsingModePersistenceMiddleware(settings, this)
|
||||
val store = AppStore(middlewares = listOf(middleware))
|
||||
// Wait for Init action
|
||||
store.waitUntilIdle()
|
||||
// Wait for middleware launched coroutine
|
||||
this.advanceUntilIdle()
|
||||
// Wait for ModeChange action
|
||||
store.waitUntilIdle()
|
||||
|
||||
assertEquals(cachedMode, store.state.mode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN mode change THEN mode updated on disk`() = runTest {
|
||||
val cachedMode = BrowsingMode.Normal
|
||||
whenever(settings.lastKnownMode).thenReturn(cachedMode)
|
||||
val updatedMode = BrowsingMode.Private
|
||||
|
||||
val middleware = BrowsingModePersistenceMiddleware(settings, this)
|
||||
val store = AppStore(middlewares = listOf(middleware))
|
||||
store.dispatch(AppAction.ModeChange(updatedMode)).joinBlocking()
|
||||
this.advanceUntilIdle()
|
||||
|
||||
verify(settings).lastKnownMode = updatedMode
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue