From 1777823f247e13aa5cd7932f475fcd9774a0c8fc Mon Sep 17 00:00:00 2001 From: Felipe Gomes Date: Thu, 12 Sep 2013 23:32:01 -0300 Subject: [PATCH] Bug 915968 - Fix downloads' startTime serialization. r=paolo --- .../jsdownloads/src/DownloadCore.jsm | 12 +++++++++++- .../jsdownloads/src/DownloadImport.jsm | 2 +- .../test/unit/common_test_Download.js | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/toolkit/components/jsdownloads/src/DownloadCore.jsm b/toolkit/components/jsdownloads/src/DownloadCore.jsm index cc80e9c29367..99cd06551020 100644 --- a/toolkit/components/jsdownloads/src/DownloadCore.jsm +++ b/toolkit/components/jsdownloads/src/DownloadCore.jsm @@ -832,6 +832,10 @@ Download.prototype = { serializable.error = { message: this.error.message }; } + if (this.startTime) { + serializable.startTime = this.startTime.toJSON(); + } + // These are serialized unless they are false, null, or empty strings. for (let property of kSerializableDownloadProperties) { if (property != "error" && this[property]) { @@ -869,7 +873,6 @@ const kSerializableDownloadProperties = [ "succeeded", "canceled", "error", - "startTime", "totalBytes", "hasPartialData", "tryToKeepPartialData", @@ -917,6 +920,13 @@ Download.fromSerializable = function (aSerializable) { } download.saver.download = download; + if ("startTime" in aSerializable) { + let time = aSerializable.startTime.getTime + ? aSerializable.startTime.getTime() + : aSerializable.startTime; + download.startTime = new Date(time); + } + for (let property of kSerializableDownloadProperties) { if (property in aSerializable) { download[property] = aSerializable[property]; diff --git a/toolkit/components/jsdownloads/src/DownloadImport.jsm b/toolkit/components/jsdownloads/src/DownloadImport.jsm index a2d83c7a4897..b73d3fcc7137 100644 --- a/toolkit/components/jsdownloads/src/DownloadImport.jsm +++ b/toolkit/components/jsdownloads/src/DownloadImport.jsm @@ -153,7 +153,7 @@ this.DownloadImport.prototype = { type: "copy", entityID: entityID }, - startTime: startTime, + startTime: new Date(startTime / 1000), totalBytes: maxBytes, hasPartialData: !!tempPath, tryToKeepPartialData: true, diff --git a/toolkit/components/jsdownloads/test/unit/common_test_Download.js b/toolkit/components/jsdownloads/test/unit/common_test_Download.js index b4d5ec5f40a1..29d2d9f260c4 100644 --- a/toolkit/components/jsdownloads/test/unit/common_test_Download.js +++ b/toolkit/components/jsdownloads/test/unit/common_test_Download.js @@ -1624,6 +1624,25 @@ add_task(function test_contentType() { do_check_eq("text/plain", download.contentType); }); +/** + * Tests that the serialization/deserialization of the startTime Date + * object works correctly. + */ +add_task(function test_toSerializable_startTime() +{ + let download1 = yield promiseStartDownload(httpUrl("source.txt")); + yield promiseDownloadStopped(download1); + + let serializable = download1.toSerializable(); + let reserialized = JSON.parse(JSON.stringify(serializable)); + + let download2 = yield Downloads.createDownload(reserialized); + + do_check_eq(download1.startTime.constructor.name, "Date"); + do_check_eq(download2.startTime.constructor.name, "Date"); + do_check_eq(download1.startTime.toJSON(), download2.startTime.toJSON()); +}); + /** * This test will call the platform specific operations within * DownloadPlatform::DownloadDone. While there is no test to verify the