Bug 1864073 - Stand up debug drawer Store and State
parent
56309b2673
commit
23ddff7ebb
@ -0,0 +1,23 @@
|
||||
/* 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.debugsettings.store
|
||||
|
||||
import mozilla.components.lib.state.Action
|
||||
|
||||
/**
|
||||
* [Action] implementation related to [DebugDrawerStore].
|
||||
*/
|
||||
sealed class DebugDrawerAction : Action {
|
||||
|
||||
/**
|
||||
* [DebugDrawerAction] fired when the user opens the drawer.
|
||||
*/
|
||||
object DrawerOpened : DebugDrawerAction()
|
||||
|
||||
/**
|
||||
* [DebugDrawerAction] fired when the user closes the drawer.
|
||||
*/
|
||||
object DrawerClosed : DebugDrawerAction()
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/* 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.debugsettings.store
|
||||
|
||||
import mozilla.components.lib.state.State
|
||||
|
||||
/**
|
||||
* UI state of the debug drawer feature.
|
||||
*
|
||||
* @property drawerStatus The [DrawerStatus] indicating the physical state of the drawer.
|
||||
*/
|
||||
data class DebugDrawerState(
|
||||
val drawerStatus: DrawerStatus = DrawerStatus.Closed,
|
||||
) : State {
|
||||
|
||||
/**
|
||||
* Possible values of [DebugDrawerState.drawerStatus].
|
||||
*/
|
||||
enum class DrawerStatus {
|
||||
/**
|
||||
* The state of the drawer when it is closed.
|
||||
*/
|
||||
Closed,
|
||||
|
||||
/**
|
||||
* The state of the drawer when it is open.
|
||||
*/
|
||||
Open,
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/* 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.debugsettings.store
|
||||
|
||||
import mozilla.components.lib.state.Middleware
|
||||
import mozilla.components.lib.state.Store
|
||||
|
||||
/**
|
||||
* A [Store] that holds the [DebugDrawerState] for the Debug Drawer and reduces [DebugDrawerAction]s
|
||||
* dispatched to the store.
|
||||
*/
|
||||
class DebugDrawerStore(
|
||||
initialState: DebugDrawerState = DebugDrawerState(),
|
||||
middlewares: List<Middleware<DebugDrawerState, DebugDrawerAction>> = emptyList(),
|
||||
) : Store<DebugDrawerState, DebugDrawerAction>(
|
||||
initialState,
|
||||
::reduce,
|
||||
middlewares,
|
||||
)
|
||||
|
||||
private fun reduce(state: DebugDrawerState, action: DebugDrawerAction): DebugDrawerState {
|
||||
return when (action) {
|
||||
is DebugDrawerAction.DrawerOpened -> state.copy(drawerStatus = DebugDrawerState.DrawerStatus.Open)
|
||||
is DebugDrawerAction.DrawerClosed -> state.copy(drawerStatus = DebugDrawerState.DrawerStatus.Closed)
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.mozilla.fenix.debugsettings
|
||||
|
||||
import mozilla.components.support.test.ext.joinBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.mozilla.fenix.debugsettings.store.DebugDrawerAction
|
||||
import org.mozilla.fenix.debugsettings.store.DebugDrawerState
|
||||
import org.mozilla.fenix.debugsettings.store.DebugDrawerStore
|
||||
|
||||
class DebugDrawerStoreTest {
|
||||
|
||||
@Test
|
||||
fun `GIVEN the drawer is closed WHEN the drawer is opened THEN the state should be set to open`() {
|
||||
val expected = DebugDrawerState.DrawerStatus.Open
|
||||
val store = createStore()
|
||||
|
||||
store.dispatch(DebugDrawerAction.DrawerOpened).joinBlocking()
|
||||
|
||||
assertEquals(expected, store.state.drawerStatus)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN the drawer is opened WHEN the drawer is closed THEN the state should be set to closed`() {
|
||||
val expected = DebugDrawerState.DrawerStatus.Closed
|
||||
val store = createStore(
|
||||
drawerStatus = DebugDrawerState.DrawerStatus.Open,
|
||||
)
|
||||
|
||||
store.dispatch(DebugDrawerAction.DrawerClosed).joinBlocking()
|
||||
|
||||
assertEquals(expected, store.state.drawerStatus)
|
||||
}
|
||||
|
||||
private fun createStore(
|
||||
drawerStatus: DebugDrawerState.DrawerStatus = DebugDrawerState.DrawerStatus.Closed,
|
||||
) = DebugDrawerStore(
|
||||
initialState = DebugDrawerState(
|
||||
drawerStatus = drawerStatus,
|
||||
),
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue