Close #20704: Add AppStore to Components

This commit is contained in:
Jonathan Almeida 2021-11-01 01:32:38 -04:00 committed by mergify[bot]
parent f2dafeb8b5
commit 9cef9357b1
7 changed files with 106 additions and 3 deletions

View File

@ -0,0 +1,22 @@
/* 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.components
import mozilla.components.lib.state.Middleware
import mozilla.components.lib.state.Store
import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.components.appstate.AppState
import org.mozilla.fenix.components.appstate.AppStoreReducer
/**
* A [Store] that holds the [AppState] for the app and reduces [AppAction]s
* dispatched to the store.
*
* This store is not persisted to disk and is scoped to the life-cycle of the application.
*/
class AppStore(
initialState: AppState = AppState(),
middlewares: List<Middleware<AppState, AppAction>> = emptyList()
) : Store<AppState, AppAction>(initialState, AppStoreReducer::reduce, middlewares)

View File

@ -176,6 +176,7 @@ class Components(private val context: Context) {
val appStartReasonProvider by lazyMonitored { AppStartReasonProvider() }
val startupActivityLog by lazyMonitored { StartupActivityLog() }
val startupStateProvider by lazyMonitored { StartupStateProvider(startupActivityLog, appStartReasonProvider) }
val appStore by lazyMonitored { AppStore() }
}
/**

View File

@ -0,0 +1,15 @@
/* 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.components.appstate
import mozilla.components.lib.state.Action
import org.mozilla.fenix.components.AppStore
/**
* [Action] implementation related to [AppStore].
*/
sealed class AppAction : Action {
data class UpdateInactiveExpanded(val expanded: Boolean) : AppAction()
}

View File

@ -0,0 +1,17 @@
/* 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.components.appstate
import mozilla.components.lib.state.State
/**
* Value type that represents the state of the tabs tray.
*
* @property inactiveTabsExpanded A flag to know if the Inactive Tabs section of the Tabs Tray
* should be expanded when the tray is opened.
*/
data class AppState(
val inactiveTabsExpanded: Boolean = false
) : State

View File

@ -0,0 +1,17 @@
/* 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.components.appstate
import org.mozilla.fenix.components.AppStore
/**
* Reducer for [AppStore].
*/
internal object AppStoreReducer {
fun reduce(state: AppState, action: AppAction): AppState = when (action) {
is AppAction.UpdateInactiveExpanded ->
state.copy(inactiveTabsExpanded = action.expanded)
}
}

View File

@ -0,0 +1,27 @@
/* 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.components.appstate
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.middleware.CaptureActionsMiddleware
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mozilla.fenix.components.AppStore
class AppActionTest {
private val capture = CaptureActionsMiddleware<AppState, AppAction>()
private val store = AppStore(middlewares = listOf(capture))
@Test
fun `WHEN UpdateInactiveExpanded is dispatched THEN update inactiveTabsExpanded`() {
assertFalse(store.state.inactiveTabsExpanded)
store.dispatch(AppAction.UpdateInactiveExpanded(true)).joinBlocking()
assertTrue(store.state.inactiveTabsExpanded)
}
}

View File

@ -14,10 +14,14 @@ import mozilla.components.browser.tabstray.TabsTray
import org.junit.Assert.assertEquals
import mozilla.components.browser.state.state.createTab as createTabState
import org.junit.Test
import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
class InactiveTabsControllerTest {
private val appStore = AppStore()
@Test
fun `WHEN expanded THEN notify filtered card`() {
val filter: (TabSessionState) -> Boolean = { !it.content.private }
@ -32,7 +36,7 @@ class InactiveTabsControllerTest {
)
val tray: TabsTray = mockk(relaxed = true)
val tabsSlot = slot<List<TabSessionState>>()
val controller = InactiveTabsController(store, filter, tray, mockk(relaxed = true))
val controller = InactiveTabsController(store, appStore, filter, tray, mockk(relaxed = true))
controller.updateCardExpansion(true)
@ -47,7 +51,7 @@ class InactiveTabsControllerTest {
val metrics: MetricController = mockk(relaxed = true)
val store = BrowserStore(BrowserState())
val controller = InactiveTabsController(
store, mockk(relaxed = true), mockk(relaxed = true), metrics
store, appStore, mockk(relaxed = true), mockk(relaxed = true), metrics
)
controller.updateCardExpansion(true)
@ -60,7 +64,7 @@ class InactiveTabsControllerTest {
val metrics: MetricController = mockk(relaxed = true)
val store = BrowserStore(BrowserState())
val controller = InactiveTabsController(
store, mockk(relaxed = true), mockk(relaxed = true), metrics
store, appStore, mockk(relaxed = true), mockk(relaxed = true), metrics
)
controller.updateCardExpansion(false)