diff --git a/components/browser/state/src/main/java/mozilla/components/browser/state/action/BrowserAction.kt b/components/browser/state/src/main/java/mozilla/components/browser/state/action/BrowserAction.kt index 2533cfb98dc..f98735a324c 100644 --- a/components/browser/state/src/main/java/mozilla/components/browser/state/action/BrowserAction.kt +++ b/components/browser/state/src/main/java/mozilla/components/browser/state/action/BrowserAction.kt @@ -54,6 +54,11 @@ sealed class BrowserAction : Action */ object InitAction : BrowserAction() +/** + * [BrowserAction] to indicate that restoring [BrowserState] is complete. + */ +object RestoreCompleteAction : BrowserAction() + /** * [BrowserAction] implementations to react to system events. */ diff --git a/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/BrowserStateReducer.kt b/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/BrowserStateReducer.kt index 52f5c320b94..e3ede6e9b26 100644 --- a/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/BrowserStateReducer.kt +++ b/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/BrowserStateReducer.kt @@ -19,6 +19,7 @@ import mozilla.components.browser.state.action.SystemAction import mozilla.components.browser.state.action.TabListAction import mozilla.components.browser.state.action.LastAccessAction import mozilla.components.browser.state.action.RecentlyClosedAction +import mozilla.components.browser.state.action.RestoreCompleteAction import mozilla.components.browser.state.action.TrackingProtectionAction import mozilla.components.browser.state.action.UndoAction import mozilla.components.browser.state.action.WebExtensionAction @@ -39,6 +40,7 @@ internal object BrowserStateReducer { fun reduce(state: BrowserState, action: BrowserAction): BrowserState { return when (action) { is InitAction -> state + is RestoreCompleteAction -> state.copy(restoreComplete = true) is ContainerAction -> ContainerReducer.reduce(state, action) is RecentlyClosedAction -> RecentlyClosedReducer.reduce(state, action) is ContentAction -> ContentStateReducer.reduce(state, action) diff --git a/components/browser/state/src/main/java/mozilla/components/browser/state/state/BrowserState.kt b/components/browser/state/src/main/java/mozilla/components/browser/state/state/BrowserState.kt index 79e864dd3c0..b0ad9faaca8 100644 --- a/components/browser/state/src/main/java/mozilla/components/browser/state/state/BrowserState.kt +++ b/components/browser/state/src/main/java/mozilla/components/browser/state/state/BrowserState.kt @@ -23,6 +23,8 @@ import mozilla.components.lib.state.State * @property search the state of search for this browser state. * @property downloads Downloads ([DownloadState]s) mapped to their IDs. * @property undoHistory History of recently closed tabs to support "undo" (Requires UndoMiddleware). + * @property restoreComplete Whether or not restoring [BrowserState] has completed. This can be used + * on application startup e.g. as an indicator that tabs have been restored. */ data class BrowserState( val tabs: List = emptyList(), @@ -34,5 +36,6 @@ data class BrowserState( val media: MediaState = MediaState(), val downloads: Map = emptyMap(), val search: SearchState = SearchState(), - val undoHistory: UndoHistoryState = UndoHistoryState() + val undoHistory: UndoHistoryState = UndoHistoryState(), + val restoreComplete: Boolean = false ) : State diff --git a/components/browser/state/src/test/java/mozilla/components/browser/state/store/BrowserStoreTest.kt b/components/browser/state/src/test/java/mozilla/components/browser/state/store/BrowserStoreTest.kt index 91f0652340d..8b294e2cd6c 100644 --- a/components/browser/state/src/test/java/mozilla/components/browser/state/store/BrowserStoreTest.kt +++ b/components/browser/state/src/test/java/mozilla/components/browser/state/store/BrowserStoreTest.kt @@ -7,12 +7,15 @@ package mozilla.components.browser.state.store import kotlinx.coroutines.runBlocking import mozilla.components.browser.state.action.BrowserAction import mozilla.components.browser.state.action.InitAction +import mozilla.components.browser.state.action.RestoreCompleteAction import mozilla.components.browser.state.action.TabListAction import mozilla.components.browser.state.state.BrowserState import mozilla.components.browser.state.state.createTab import mozilla.components.lib.state.Middleware +import mozilla.components.support.test.ext.joinBlocking import mozilla.components.support.test.libstate.ext.waitUntilIdle import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test @@ -77,4 +80,16 @@ class BrowserStoreTest { store.waitUntilIdle() assertTrue(initActionObserved) } + + @Test + fun `RestoreCompleteAction updates state`() { + val store = BrowserStore() + assertFalse(store.state.restoreComplete) + + store.dispatch(RestoreCompleteAction).joinBlocking() + assertTrue(store.state.restoreComplete) + + store.dispatch(RestoreCompleteAction).joinBlocking() + assertTrue(store.state.restoreComplete) + } }