From a8d07a4b9ad017824e6ed92db34c60f625a5c3c9 Mon Sep 17 00:00:00 2001 From: Raymond Lee Date: Fri, 10 May 2013 20:26:10 +0800 Subject: [PATCH] Bug 852478 - Store the date and time of downloads. r=paolo --- .../jsdownloads/src/DownloadCore.jsm | 8 +++++++ .../components/jsdownloads/test/unit/head.js | 24 +++++++++++++++++++ .../test/unit/test_DownloadCore.js | 24 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/toolkit/components/jsdownloads/src/DownloadCore.jsm b/toolkit/components/jsdownloads/src/DownloadCore.jsm index 5f48bc1b8227..e1cb7b9d930b 100644 --- a/toolkit/components/jsdownloads/src/DownloadCore.jsm +++ b/toolkit/components/jsdownloads/src/DownloadCore.jsm @@ -128,6 +128,13 @@ Download.prototype = { */ error: null, + /** + * Indicates the start time of the download. When the download starts, + * this property is set to a valid Date object. The default value is null + * before the download starts. + */ + startTime: null, + /** * Indicates whether this download's "progress" property is able to report * partial progress while the download proceeds, and whether the value in @@ -237,6 +244,7 @@ Download.prototype = { this.progress = 0; this.totalBytes = 0; this.currentBytes = 0; + this.startTime = new Date(); // Create a new deferred object and an associated promise before starting // the actual download. We store it on the download as the current attempt. diff --git a/toolkit/components/jsdownloads/test/unit/head.js b/toolkit/components/jsdownloads/test/unit/head.js index 210bf63ba2f4..89afc61d3ba0 100644 --- a/toolkit/components/jsdownloads/test/unit/head.js +++ b/toolkit/components/jsdownloads/test/unit/head.js @@ -127,6 +127,20 @@ function promiseExecuteSoon() return deferred.promise; } +/** + * Waits for a pending events to be processed after a timeout. + * + * @return {Promise} + * @resolves When pending events have been processed. + * @rejects Never. + */ +function promiseTimeout(aTime) +{ + let deferred = Promise.defer(); + do_timeout(aTime, deferred.resolve); + return deferred.promise; +} + /** * Creates a new Download object, setting a temporary file as the target. * @@ -296,6 +310,16 @@ function registerInterruptibleHandler(aPath, aFirstPartFn, aSecondPartFn) }); } +/** + * Ensure the given date object is valid. + * + * @param aDate + * The date object to be checked. This value can be null. + */ +function isValidDate(aDate) { + return aDate && aDate.getTime && !isNaN(aDate.getTime()); +} + //////////////////////////////////////////////////////////////////////////////// //// Initialization functions common to all tests diff --git a/toolkit/components/jsdownloads/test/unit/test_DownloadCore.js b/toolkit/components/jsdownloads/test/unit/test_DownloadCore.js index 1cdf2a9f9064..ba109a934cfb 100644 --- a/toolkit/components/jsdownloads/test/unit/test_DownloadCore.js +++ b/toolkit/components/jsdownloads/test/unit/test_DownloadCore.js @@ -47,6 +47,7 @@ add_task(function test_download_initial_final_state() do_check_false(download.canceled); do_check_true(download.error === null); do_check_eq(download.progress, 0); + do_check_true(download.startTime === null); // Starts the download and waits for completion. yield download.start(); @@ -56,6 +57,7 @@ add_task(function test_download_initial_final_state() do_check_false(download.canceled); do_check_true(download.error === null); do_check_eq(download.progress, 100); + do_check_true(isValidDate(download.startTime)); }); /** @@ -674,3 +676,25 @@ add_task(function test_download_error_restart() yield promiseVerifyContents(download.target.file, TEST_DATA_SHORT); }); + + +/** + * Checks the startTime gets updated even after a restart. + */ +add_task(function test_download_cancel_immediately_restart_and_check_startTime() +{ + let download = yield promiseSimpleDownload(); + + download.start(); + let startTime = download.startTime; + do_check_true(isValidDate(download.startTime)); + + yield download.cancel(); + do_check_eq(download.startTime.getTime(), startTime.getTime()); + + // Wait for a timeout. + yield promiseTimeout(10); + + yield download.start(); + do_check_true(download.startTime.getTime() > startTime.getTime()); +});