Commit 01eb63f0 authored by Sebastian Kaspari's avatar Sebastian Kaspari
Browse files

Issue #3557: Dispatch BrowserStore action from SessionManager.restore().

parent 67dc5b26
......@@ -122,7 +122,32 @@ class SessionManager(
* @param snapshot A [Snapshot] which may be produced by [createSnapshot].
* @param updateSelection Whether the selected session should be updated from the restored snapshot.
*/
fun restore(snapshot: Snapshot, updateSelection: Boolean = true) = delegate.restore(snapshot, updateSelection)
fun restore(snapshot: Snapshot, updateSelection: Boolean = true) {
delegate.restore(snapshot, updateSelection)
val tabs = snapshot.sessions.mapNotNull { item ->
if (!item.session.isCustomTabSession()) {
item.session.toTabSessionState()
} else {
// A restored snapshot should not contain any custom tab so we should be able to safely ignore
// them here.
null
}
}
val selectedTabId = if (updateSelection && snapshot.selectedSessionIndex != NO_SELECTION) {
val index = snapshot.selectedSessionIndex
if (index in 0..snapshot.sessions.lastIndex) {
snapshot.sessions[index].session.id
} else {
null
}
} else {
null
}
store?.syncDispatch(TabListAction.RestoreAction(tabs, selectedTabId))
}
/**
* Gets the linked engine session for the provided session (if it exists).
......
......@@ -200,4 +200,100 @@ class SessionManagerMigrationTest {
assertNull(manager.selectedSession)
assertNull(store.state.selectedTabId)
}
@Test
fun `Restoring snapshot with invalid index`() {
val store = BrowserStore(BrowserState())
val manager = SessionManager(engine = mock(), store = store)
manager.restore(SessionManager.Snapshot(listOf(), selectedSessionIndex = 0))
assertEquals(0, manager.sessions.size)
assertEquals(0, store.state.tabs.size)
assertNull(manager.selectedSession)
assertNull(store.state.selectedTabId)
}
@Test
fun `Restoring snapshot without updating selection`() {
val session: Session
val store = BrowserStore(BrowserState())
val manager = SessionManager(engine = mock(), store = store).apply {
session = Session("https://getpocket.com")
add(Session("https://www.mozilla.org"))
add(session)
add(Session("https://www.firefox.com"))
}
val item = manager.createSessionSnapshot(session)
manager.remove(session)
manager.restore(SessionManager.Snapshot.singleItem(item), updateSelection = false)
assertEquals(3, manager.size)
assertEquals(3, store.state.tabs.size)
assertEquals("https://www.mozilla.org", manager.selectedSessionOrThrow.url)
assertEquals("https://www.mozilla.org", store.state.selectedTab!!.content.url)
assertEquals("https://getpocket.com", manager.sessions[0].url)
assertEquals("https://getpocket.com", store.state.tabs[0].content.url)
assertEquals("https://www.mozilla.org", manager.sessions[1].url)
assertEquals("https://www.mozilla.org", store.state.tabs[1].content.url)
assertEquals("https://www.firefox.com", manager.sessions[2].url)
assertEquals("https://www.firefox.com", store.state.tabs[2].content.url)
}
@Test
fun `Restoring snapshot into empty state`() {
val snapshot = SessionManager.Snapshot(
sessions = listOf(
SessionManager.Snapshot.Item(
Session(id = "regular1", initialUrl = "https://www.mozilla.org", private = false)),
SessionManager.Snapshot.Item(
Session(id = "private1", initialUrl = "https://example.org/private1", private = true)),
SessionManager.Snapshot.Item(
Session(id = "private2", initialUrl = "https://example.org/private2", private = true)),
SessionManager.Snapshot.Item(
Session(id = "regular2", initialUrl = "https://www.firefox.com", private = false)),
SessionManager.Snapshot.Item(
Session(id = "regular3", initialUrl = "https://www.firefox.org", private = false))
),
selectedSessionIndex = 2
)
val store = BrowserStore(BrowserState())
val manager = SessionManager(engine = mock(), store = store)
manager.restore(snapshot)
assertEquals(5, manager.sessions.size)
assertEquals(5, store.state.tabs.size)
assertEquals("https://example.org/private2", manager.selectedSessionOrThrow.url)
assertEquals("https://example.org/private2", store.state.selectedTab!!.content.url)
assertEquals("private2", store.state.selectedTabId)
assertEquals("https://www.mozilla.org", manager.sessions[0].url)
assertEquals("https://www.mozilla.org", store.state.tabs[0].content.url)
assertEquals("https://example.org/private1", manager.sessions[1].url)
assertEquals("https://example.org/private1", store.state.tabs[1].content.url)
assertEquals("https://example.org/private2", manager.sessions[2].url)
assertEquals("https://example.org/private2", store.state.tabs[2].content.url)
assertEquals("https://www.firefox.com", manager.sessions[3].url)
assertEquals("https://www.firefox.com", store.state.tabs[3].content.url)
assertEquals("https://www.firefox.org", manager.sessions[4].url)
assertEquals("https://www.firefox.org", store.state.tabs[4].content.url)
}
}
......@@ -85,8 +85,8 @@ class AllSessionsObserverTest {
AllSessionsObserver(sessionManager, observer)
.start()
val session1: Session = mock()
val session2: Session = mock()
val session1: Session = spy(Session("https://www.mozilla.org"))
val session2: Session = spy(Session("https://www.firefox.com"))
val snapshot = SessionManager.Snapshot(
sessions = listOf(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment