Commit 12dd6989 authored by Christian Sadilek's avatar Christian Sadilek
Browse files

Closes #1908: AutoSave doesn't work if no sessions left

parent 1a784cd1
......@@ -37,11 +37,11 @@ class SessionManager(
* Produces a snapshot of this manager's state, suitable for restoring via [SessionManager.restore].
* Only regular sessions are included in the snapshot. Private and Custom Tab sessions are omitted.
*
* @return [Snapshot] or null if no sessions are present.
* @return [Snapshot] of the current session state.
*/
fun createSnapshot(): Snapshot? = synchronized(values) {
fun createSnapshot(): Snapshot = synchronized(values) {
if (values.isEmpty()) {
return null
return Snapshot.empty()
}
// Filter out CustomTab and private sessions.
......@@ -60,7 +60,7 @@ class SessionManager(
// We might have some sessions (private, custom tab) but none we'd include in the snapshot.
if (sessionStateTuples.isEmpty()) {
return null
return Snapshot.empty()
}
// We need to find out the index of our selected session in the filtered list. If we have a
......@@ -530,6 +530,10 @@ class SessionManager(
val engineSession: EngineSession? = null,
val engineSessionState: EngineSessionState? = null
)
companion object {
fun empty() = Snapshot(emptyList(), NO_SELECTION)
}
}
}
......
......@@ -108,9 +108,7 @@ class AutoSave(
try {
val snapshot = sessionManager.createSnapshot()
if (snapshot != null) {
sessionStorage.save(snapshot)
}
sessionStorage.save(snapshot)
} finally {
val took = now() - start
logger.debug("Saved state to disk [${took}ms]")
......
......@@ -300,7 +300,7 @@ class SessionManagerTest {
@Test
fun `createSnapshot works when manager has no sessions`() {
val manager = SessionManager(mock())
assertNull(manager.createSnapshot())
assertTrue(manager.createSnapshot().isEmpty())
}
@Test
......@@ -309,7 +309,7 @@ class SessionManagerTest {
val session = Session("http://mozilla.org", true)
manager.add(session)
assertNull(manager.createSnapshot())
assertTrue(manager.createSnapshot().isEmpty())
}
@Test
......@@ -319,7 +319,7 @@ class SessionManagerTest {
session.customTabConfig = Mockito.mock(CustomTabConfig::class.java)
manager.add(session)
assertNull(manager.createSnapshot())
assertTrue(manager.createSnapshot().isEmpty())
}
@Test
......@@ -329,7 +329,7 @@ class SessionManagerTest {
session.customTabConfig = Mockito.mock(CustomTabConfig::class.java)
manager.add(session)
assertNull(manager.createSnapshot())
assertTrue(manager.createSnapshot().isEmpty())
}
@Test(expected = IllegalArgumentException::class)
......
......@@ -287,6 +287,35 @@ class SessionStorageTest {
}
}
@Test
fun `AutoSave - when no sessions left`() {
runBlocking {
val session = Session("https://www.firefox.com")
val sessionManager = SessionManager(mock())
sessionManager.add(session)
val sessionStorage: SessionStorage = mock()
val autoSave = AutoSave(
sessionManager = sessionManager,
sessionStorage = sessionStorage,
minimumIntervalMs = 0
).whenSessionsChange()
assertNull(autoSave.saveJob)
verify(sessionStorage, never()).save(any())
// We didn't specify a default session lambda so this will
// leave us without a session
sessionManager.remove(session)
assertEquals(0, sessionManager.size)
autoSave.saveJob?.join()
verify(sessionStorage).save(any())
}
}
@Test
fun `AutoSave - when session gets selected`() {
runBlocking {
......
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