Commit 4ac0c1f0 authored by ekager's avatar ekager
Browse files

Closes #6651 - Do not restore Session source, adds Session.Source.RESTORED

parent 281a63fd
......@@ -174,7 +174,12 @@ class Session(
/**
* User entered a URL or search term
*/
USER_ENTERED
USER_ENTERED,
/**
* This session was restored
*/
RESTORED
}
/**
......
......@@ -83,10 +83,12 @@ class SnapshotSerializer(
fun itemFromJSON(engine: Engine, json: JSONObject): SessionManager.Snapshot.Item {
val sessionJson = json.getJSONObject(Keys.SESSION_KEY)
val session = deserializeSession(sessionJson, restoreSessionIds, restoreParentIds)
val readerState = ReaderState(active = sessionJson.optBoolean(Keys.SESSION_READER_MODE_KEY, false))
val readerState =
ReaderState(active = sessionJson.optBoolean(Keys.SESSION_READER_MODE_KEY, false))
val engineState = engine.createSessionState(sessionJson)
return SessionManager.Snapshot.Item(session,
return SessionManager.Snapshot.Item(
session,
engineSession = null,
engineSessionState = engineState,
readerState = readerState
......@@ -99,7 +101,6 @@ class SnapshotSerializer(
internal fun serializeSession(session: Session): JSONObject {
return JSONObject().apply {
put(Keys.SESSION_URL_KEY, session.url)
put(Keys.SESSION_SOURCE_KEY, session.source.name)
put(Keys.SESSION_UUID_KEY, session.id)
put(Keys.SESSION_PARENT_UUID_KEY, session.parentId ?: "")
put(Keys.SESSION_TITLE, session.title)
......@@ -109,17 +110,16 @@ internal fun serializeSession(session: Session): JSONObject {
@Throws(JSONException::class)
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun deserializeSession(json: JSONObject, restoreId: Boolean, restoreParentId: Boolean): Session {
val source = try {
Session.Source.valueOf(json.getString(Keys.SESSION_SOURCE_KEY))
} catch (e: IllegalArgumentException) {
Session.Source.NONE
}
internal fun deserializeSession(
json: JSONObject,
restoreId: Boolean,
restoreParentId: Boolean
): Session {
val session = Session(
json.getString(Keys.SESSION_URL_KEY),
// Currently, snapshot cannot contain private sessions.
false,
source,
Session.Source.RESTORED,
if (restoreId) {
json.getString(Keys.SESSION_UUID_KEY)
} else {
......@@ -138,7 +138,6 @@ private object Keys {
const val SELECTED_SESSION_INDEX_KEY = "selectedSessionIndex"
const val SESSION_STATE_TUPLES_KEY = "sessionStateTuples"
const val SESSION_SOURCE_KEY = "source"
const val SESSION_URL_KEY = "url"
const val SESSION_UUID_KEY = "uuid"
const val SESSION_CONTEXT_ID_KEY = "contextId"
......
......@@ -452,14 +452,20 @@ class SessionStorageTest {
json.put("url", "testUrl")
json.put("source", Source.NEW_TAB.name)
json.put("parentUuid", "")
assertEquals(Source.NEW_TAB, deserializeSession(json, restoreId = true, restoreParentId = true).source)
assertEquals(
Source.RESTORED,
deserializeSession(json, restoreId = true, restoreParentId = true).source
)
val jsonInvalid = JSONObject()
jsonInvalid.put("uuid", "testId")
jsonInvalid.put("url", "testUrl")
jsonInvalid.put("source", "invalidSource")
jsonInvalid.put("parentUuid", "")
assertEquals(Source.NONE, deserializeSession(jsonInvalid, restoreId = true, restoreParentId = true).source)
assertEquals(
Source.RESTORED,
deserializeSession(jsonInvalid, restoreId = true, restoreParentId = true).source
)
}
@Suppress("UNCHECKED_CAST")
......
......@@ -29,7 +29,8 @@ class SnapshotSerializerTest {
"https://www.mozilla.org",
source = Session.Source.ACTION_VIEW,
id = "test-id",
contextId = "test-context-id").apply {
contextId = "test-context-id"
).apply {
title = "Hello World"
}
......@@ -37,7 +38,7 @@ class SnapshotSerializerTest {
val restoredSession = deserializeSession(json, restoreId = true, restoreParentId = false)
assertEquals("https://www.mozilla.org", restoredSession.url)
assertEquals(Session.Source.ACTION_VIEW, restoredSession.source)
assertEquals(Session.Source.RESTORED, restoredSession.source)
assertEquals("test-id", restoredSession.id)
assertEquals("test-context-id", restoredSession.contextId)
assertEquals("Hello World", restoredSession.title)
......@@ -51,16 +52,22 @@ class SnapshotSerializerTest {
"https://www.mozilla.org",
source = Session.Source.ACTION_VIEW,
id = "test-id",
contextId = "test-context-id").apply {
contextId = "test-context-id"
).apply {
title = "Hello World"
}
val serializer = SnapshotSerializer()
val json = serializer.itemToJSON(Snapshot.Item(originalSession, readerState = ReaderState(active = true)))
val json = serializer.itemToJSON(
Snapshot.Item(
originalSession,
readerState = ReaderState(active = true)
)
)
val restoredItem = serializer.itemFromJSON(engine, json)
assertEquals("https://www.mozilla.org", restoredItem.session.url)
assertEquals(Session.Source.ACTION_VIEW, restoredItem.session.source)
assertEquals(Session.Source.RESTORED, restoredItem.session.source)
assertEquals("test-id", restoredItem.session.id)
assertEquals("test-context-id", restoredItem.session.contextId)
assertEquals("Hello World", restoredItem.session.title)
......@@ -76,10 +83,14 @@ class SnapshotSerializerTest {
put("parentUuid", "")
}
val restoredSession = deserializeSession(json, restoreId = true, restoreParentId = false)
val restoredSession = deserializeSession(
json,
restoreId = true,
restoreParentId = false
)
assertEquals("https://www.mozilla.org", restoredSession.url)
assertEquals(Session.Source.ACTION_VIEW, restoredSession.source)
assertEquals(Session.Source.RESTORED, restoredSession.source)
assertEquals("test-id", restoredSession.id)
}
......@@ -102,7 +113,8 @@ class SnapshotSerializerTest {
val originalSession = Session(
"https://www.mozilla.org",
source = Session.Source.ACTION_VIEW,
id = "test-id").apply {
id = "test-id"
).apply {
title = "Hello World"
}
......@@ -110,12 +122,13 @@ class SnapshotSerializerTest {
val restoredSession = deserializeSession(json, restoreId = false, restoreParentId = false)
assertEquals("https://www.mozilla.org", restoredSession.url)
assertEquals(Session.Source.ACTION_VIEW, restoredSession.source)
assertEquals(Session.Source.RESTORED, restoredSession.source)
assertNotEquals("test-id", restoredSession.id)
assertTrue(restoredSession.id.isNotBlank())
assertEquals("Hello World", restoredSession.title)
val restoredSessionAgain = deserializeSession(json, restoreId = false, restoreParentId = false)
val restoredSessionAgain =
deserializeSession(json, restoreId = false, restoreParentId = false)
assertNotEquals("test-id", restoredSessionAgain.id)
assertNotEquals(restoredSession.id, restoredSessionAgain.id)
assertTrue(restoredSessionAgain.id.isNotBlank())
......@@ -124,9 +137,10 @@ class SnapshotSerializerTest {
@Test
fun `Deserialize session without restoring parent id`() {
val originalSession = Session(
"https://www.mozilla.org",
source = Session.Source.ACTION_VIEW,
id = "test-id").apply {
"https://www.mozilla.org",
source = Session.Source.ACTION_VIEW,
id = "test-id"
).apply {
parentId = "test-parent-id"
title = "Hello World"
}
......@@ -134,13 +148,14 @@ class SnapshotSerializerTest {
val json = serializeSession(originalSession)
val restoredSession = deserializeSession(json, restoreId = true, restoreParentId = true)
assertEquals("https://www.mozilla.org", restoredSession.url)
assertEquals(Session.Source.ACTION_VIEW, restoredSession.source)
assertEquals(Session.Source.RESTORED, restoredSession.source)
assertEquals("test-id", restoredSession.id)
assertEquals("test-parent-id", restoredSession.parentId)
assertTrue(restoredSession.id.isNotBlank())
assertEquals("Hello World", restoredSession.title)
val restoredSessionAgain = deserializeSession(json, restoreId = true, restoreParentId = false)
val restoredSessionAgain =
deserializeSession(json, restoreId = true, restoreParentId = false)
assertEquals("test-id", restoredSessionAgain.id)
assertNull(restoredSessionAgain.parentId)
}
......
......@@ -12,6 +12,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Config.kt)
* **browser-session**
* `SnapshotSerializer` no longer restores source of a `Session`, added `Session.Source.RESTORED`
* **feature-downloads**
* Fixed issue [#6764](https://github.com/mozilla-mobile/android-components/issues/6764).
......@@ -52,7 +55,7 @@ permalink: /changelog/
* **browser-tabstray**
* The iconView is no longer required in the template.
* The URL text for items may be styled.
* **service-glean**
* Glean was updated to v28.0.0
* The baseline ping is now sent when the application goes to foreground, in addition to background and dirty-startup.
......
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