Bug 905508 - Create test for download interruption prompts. r=paolo

This commit is contained in:
Andres Hernandez 2013-08-23 16:33:52 -06:00
parent 3886623ff8
commit 3f46d6884a
3 changed files with 166 additions and 4 deletions

View File

@ -110,7 +110,9 @@ const kPrefImportedFromSqlite = "browser.download.importedFromSqlite";
this.DownloadIntegration = {
// For testing only
_testMode: false,
dontLoad: false,
testPromptDownloads: 0,
dontLoadList: false,
dontLoadObservers: false,
dontCheckParentalControls: false,
shouldBlockInTest: false,
dontOpenFileAndFolder: false,
@ -149,7 +151,7 @@ this.DownloadIntegration = {
*/
initializePublicDownloadList: function(aList) {
return Task.spawn(function task_DI_initializePublicDownloadList() {
if (this.dontLoad) {
if (this.dontLoadList) {
return;
}
@ -616,7 +618,7 @@ this.DownloadIntegration = {
* @resolves When the views and observers are added.
*/
addListObservers: function DI_addListObservers(aList, aIsPrivate) {
if (this.dontLoad) {
if (this.dontLoadObservers) {
return Promise.resolve();
}
@ -724,6 +726,11 @@ this.DownloadObserver = {
if ((aCancel instanceof Ci.nsISupportsPRBool) && aCancel.data) {
return;
}
// Handle test mode
if (DownloadIntegration.testMode) {
DownloadIntegration.testPromptDownloads = aDownloadsCount;
return;
}
// If there are no active downloads, then do nothing.
if (aDownloadsCount <= 0) {
return;

View File

@ -662,7 +662,8 @@ add_task(function test_common_initialize()
});
// Disable integration with the host application requiring profile access.
DownloadIntegration.dontLoad = true;
DownloadIntegration.dontLoadList = true;
DownloadIntegration.dontLoadObservers = true;
// Disable the parental controls checking.
DownloadIntegration.dontCheckParentalControls = true;
// Disable the calls to the OS to launch files and open containing folders

View File

@ -7,6 +7,55 @@
"use strict";
////////////////////////////////////////////////////////////////////////////////
//// Globals
/**
* Enable test mode for the _confirmCancelDownloads method to return
* the number of downloads instead of showing the prompt to cancel or not.
*/
function enableObserversTestMode() {
DownloadIntegration.testMode = true;
DownloadIntegration.dontLoadObservers = false;
function cleanup() {
DownloadIntegration.testMode = false;
DownloadIntegration.dontLoadObservers = true;
}
do_register_cleanup(cleanup);
}
/**
* Notifies the prompt observers and verify the expected downloads count.
*
* @param aIsPrivate
* Flag to know is test private observers.
* @param aExpectedCount
* the expected downloads count for quit and offline observers.
* @param aExpectedPBCount
* the expected downloads count for private browsing observer.
*/
function notifyPromptObservers(aIsPrivate, aExpectedCount, aExpectedPBCount) {
let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].
createInstance(Ci.nsISupportsPRBool);
// Notify quit application requested observer.
DownloadIntegration.testPromptDownloads = -1;
Services.obs.notifyObservers(cancelQuit, "quit-application-requested", null);
do_check_eq(DownloadIntegration.testPromptDownloads, aExpectedCount);
// Notify offline requested observer.
DownloadIntegration.testPromptDownloads = -1;
Services.obs.notifyObservers(cancelQuit, "offline-requested", null);
do_check_eq(DownloadIntegration.testPromptDownloads, aExpectedCount);
if (aIsPrivate) {
// Notify last private browsing requested observer.
DownloadIntegration.testPromptDownloads = -1;
Services.obs.notifyObservers(cancelQuit, "last-pb-context-exiting", null);
do_check_eq(DownloadIntegration.testPromptDownloads, aExpectedPBCount);
}
}
////////////////////////////////////////////////////////////////////////////////
//// Tests
@ -147,3 +196,108 @@ add_task(function test_getTemporaryDownloadsDirectory()
}
});
////////////////////////////////////////////////////////////////////////////////
//// Tests DownloadObserver
/**
* Tests notifications prompts when observers are notified if there are public
* and private active downloads.
*/
add_task(function test_notifications()
{
enableObserversTestMode();
mustInterruptResponses();
for (let isPrivate of [false, true]) {
let list = isPrivate ? yield promiseNewPrivateDownloadList()
: yield promiseNewDownloadList();
let download1 = yield promiseNewDownload(httpUrl("interruptible.txt"));
let download2 = yield promiseNewDownload(httpUrl("interruptible.txt"));
let download3 = yield promiseNewDownload(httpUrl("interruptible.txt"));
let promiseAttempt1 = download1.start();
let promiseAttempt2 = download2.start();
download3.start();
// Add downloads to list.
list.add(download1);
list.add(download2);
list.add(download3);
// Cancel third download
yield download3.cancel();
notifyPromptObservers(isPrivate, 2, 2);
// Allow the downloads to complete.
continueResponses();
yield promiseAttempt1;
yield promiseAttempt2;
// Clean up.
list.remove(download1);
list.remove(download2);
list.remove(download3);
}
});
/**
* Tests that notifications prompts observers are not notified if there are no
* public or private active downloads.
*/
add_task(function test_no_notifications()
{
enableObserversTestMode();
for (let isPrivate of [false, true]) {
let list = isPrivate ? yield promiseNewPrivateDownloadList()
: yield promiseNewDownloadList();
let download1 = yield promiseNewDownload(httpUrl("interruptible.txt"));
let download2 = yield promiseNewDownload(httpUrl("interruptible.txt"));
download1.start();
download2.start();
// Add downloads to list.
list.add(download1);
list.add(download2);
yield download1.cancel();
yield download2.cancel();
notifyPromptObservers(isPrivate, 0, 0);
// Clean up.
list.remove(download1);
list.remove(download2);
}
});
/**
* Tests notifications prompts when observers are notified if there are public
* and private active downloads at the same time.
*/
add_task(function test_mix_notifications()
{
enableObserversTestMode();
mustInterruptResponses();
let publicList = yield promiseNewDownloadList();
let privateList = yield promiseNewPrivateDownloadList();
let download1 = yield promiseNewDownload(httpUrl("interruptible.txt"));
let download2 = yield promiseNewDownload(httpUrl("interruptible.txt"));
let promiseAttempt1 = download1.start();
let promiseAttempt2 = download2.start();
// Add downloads to lists.
publicList.add(download1);
privateList.add(download2);
notifyPromptObservers(true, 2, 1);
// Allow the downloads to complete.
continueResponses();
yield promiseAttempt1;
yield promiseAttempt2;
// Clean up.
publicList.remove(download1);
privateList.remove(download2);
});