From 1234610a27d92bb2673ee70924350290d4102baa Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Wed, 7 Apr 2010 11:16:10 -0700 Subject: [PATCH] Bug 553022: Remove cancelled and failed installs from the active installs list. r=robstrong --- toolkit/mozapps/extensions/XPIProvider.jsm | 12 +++ .../extensions/test/xpcshell/head_addons.js | 13 ++- .../extensions/test/xpcshell/test_install.js | 95 +++++++++++++++++++ .../extensions/test/xpcshell/test_startup.js | 1 + .../plugins/tests/browser_bug435788.js | 8 +- 5 files changed, 123 insertions(+), 6 deletions(-) diff --git a/toolkit/mozapps/extensions/XPIProvider.jsm b/toolkit/mozapps/extensions/XPIProvider.jsm index 14d4e6b69590..914de0e3db78 100644 --- a/toolkit/mozapps/extensions/XPIProvider.jsm +++ b/toolkit/mozapps/extensions/XPIProvider.jsm @@ -1644,6 +1644,10 @@ var XPIProvider = { }, file); }, + removeActiveInstall: function XPI_removeActiveInstall(install) { + this.installs = this.installs.filter(function(i) i != install); + }, + /** * Called to get an Addon with a particular ID. * @@ -3353,6 +3357,7 @@ AddonInstall.prototype = { case AddonManager.STATE_DOWNLOADED: LOG("Cancelling download of " + this.sourceURL.spec); this.state = AddonManager.STATE_CANCELLED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onDownloadCancelled", this.listeners, this.wrapper); if (this.file && !(this.sourceURL instanceof Ci.nsIFileURL)) @@ -3369,6 +3374,7 @@ AddonInstall.prototype = { if (stagedJSON.exists()) stagedJSON.remove(true); this.state = AddonManager.STATE_CANCELLED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onInstallCancelled", this.listeners, this.wrapper); break; @@ -3472,6 +3478,7 @@ AddonInstall.prototype = { if (!AddonManagerPrivate.callInstallListeners("onDownloadStarted", this.listeners, this.wrapper)) { this.state = AddonManager.STATE_CANCELLED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onDownloadCancelled", this.listeners, this.wrapper) return; @@ -3488,6 +3495,7 @@ AddonInstall.prototype = { catch (e) { WARN("Unknown hash algorithm " + alg); this.state = AddonManager.STATE_DOWNLOAD_FAILED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onDownloadFailed", this.listeners, this.wrapper, AddonManager.ERROR_INCORRECT_HASH); @@ -3528,6 +3536,7 @@ AddonInstall.prototype = { catch (e) { WARN("Failed to start download: " + e); this.state = AddonManager.STATE_DOWNLOAD_FAILED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onDownloadFailed", this.listeners, this.wrapper, AddonManager.ERROR_NETWORK_FAILURE); @@ -3664,6 +3673,7 @@ AddonInstall.prototype = { downloadFailed: function(reason, error) { WARN("Download failed: " + error + "\n"); this.state = AddonManager.STATE_DOWNLOAD_FAILED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onDownloadFailed", this.listeners, this.wrapper, reason); this.file.remove(true); @@ -3695,6 +3705,7 @@ AddonInstall.prototype = { if (!AddonManagerPrivate.callInstallListeners("onInstallStarted", this.listeners, this.wrapper)) { this.state = AddonManager.STATE_DOWNLOADED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onInstallCancelled", this.listeners, this.wrapper) return; @@ -3830,6 +3841,7 @@ AddonInstall.prototype = { if (stagedAddon.exists()) stagedAddon.remove(true); this.state = AddonManager.STATE_INSTALL_FAILED; + XPIProvider.removeActiveInstall(this); AddonManagerPrivate.callInstallListeners("onInstallFailed", this.listeners, this.wrapper, e); diff --git a/toolkit/mozapps/extensions/test/xpcshell/head_addons.js b/toolkit/mozapps/extensions/test/xpcshell/head_addons.js index 21c6f8097f0b..9e30f37b21d6 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/head_addons.js +++ b/toolkit/mozapps/extensions/test/xpcshell/head_addons.js @@ -453,7 +453,6 @@ const InstallListener = { onDownloadEnded: function(install) { do_check_eq(install.state, AddonManager.STATE_DOWNLOADED); do_check_eq("onDownloadEnded", gExpectedInstalls.shift()); - // gNext should determine whether installation continues return check_test_completed(arguments); }, @@ -463,6 +462,12 @@ const InstallListener = { return check_test_completed(arguments); }, + onDownloadCancelled: function(install) { + do_check_eq(install.state, AddonManager.STATE_CANCELLED); + do_check_eq("onDownloadCancelled", gExpectedInstalls.shift()); + return check_test_completed(arguments); + }, + onInstallStarted: function(install) { do_check_eq(install.state, AddonManager.STATE_INSTALLING); do_check_eq("onInstallStarted", gExpectedInstalls.shift()); @@ -481,6 +486,12 @@ const InstallListener = { return check_test_completed(arguments); }, + onInstallCancelled: function(install) { + do_check_eq(install.state, AddonManager.STATE_CANCELLED); + do_check_eq("onInstallCancelled", gExpectedInstalls.shift()); + return check_test_completed(arguments); + }, + onExternalInstall: function(addon, existingAddon, requiresRestart) { do_check_eq("onExternalInstall", gExpectedInstalls.shift()); do_check_false(requiresRestart); diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_install.js b/toolkit/mozapps/extensions/test/xpcshell/test_install.js index e2f9b67058dc..3e9e9fa945f5 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/test_install.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_install.js @@ -441,6 +441,101 @@ function check_test_8() { a3.uninstall(); restartManager(0); + run_test_9(); + }); +} + +// Test that after cancelling a download it is removed from the active installs +function run_test_9() { + prepare_test({ }, [ + "onNewInstall" + ]); + + let url = "http://localhost:4444/addons/test_install3.xpi"; + AddonManager.getInstallForURL(url, function(install) { + ensure_test_completed(); + + do_check_neq(install, null); + do_check_eq(install.version, "1.0"); + do_check_eq(install.name, "Real Test 4"); + do_check_eq(install.state, AddonManager.STATE_AVAILABLE); + + AddonManager.getInstalls(null, function(activeInstalls) { + do_check_eq(activeInstalls.length, 1); + do_check_eq(activeInstalls[0], install); + + prepare_test({}, [ + "onDownloadStarted", + "onDownloadEnded", + ], check_test_9); + install.install(); + }); + }, "application/x-xpinstall", null, "Real Test 4", null, "1.0"); +} + +function check_test_9(install) { + prepare_test({}, [ + "onDownloadCancelled" + ]); + + install.cancel(); + + ensure_test_completed(); + + AddonManager.getInstalls(null, function(activeInstalls) { + do_check_eq(activeInstalls.length, 0); + + run_test_10(); + }); +} + +// Tests that after cancelling a pending install it is removed from the active +// installs +function run_test_10() { + prepare_test({ }, [ + "onNewInstall" + ]); + + let url = "http://localhost:4444/addons/test_install3.xpi"; + AddonManager.getInstallForURL(url, function(install) { + ensure_test_completed(); + + do_check_neq(install, null); + do_check_eq(install.version, "1.0"); + do_check_eq(install.name, "Real Test 4"); + do_check_eq(install.state, AddonManager.STATE_AVAILABLE); + + AddonManager.getInstalls(null, function(activeInstalls) { + do_check_eq(activeInstalls.length, 1); + do_check_eq(activeInstalls[0], install); + + prepare_test({ + "addon3@tests.mozilla.org": [ + "onInstalling" + ] + }, [ + "onDownloadStarted", + "onDownloadEnded", + "onInstallStarted", + "onInstallEnded" + ], check_test_10); + install.install(); + }); + }, "application/x-xpinstall", null, "Real Test 4", null, "1.0"); +} + +function check_test_10(install) { + prepare_test({}, [ + "onInstallCancelled" + ]); + + install.cancel(); + + ensure_test_completed(); + + AddonManager.getInstalls(null, function(activeInstalls) { + do_check_eq(activeInstalls.length, 0); + end_test(); }); } diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_startup.js b/toolkit/mozapps/extensions/test/xpcshell/test_startup.js index 233a8ac7ac28..690952e6d725 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/test_startup.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_startup.js @@ -180,6 +180,7 @@ function run_test_1() { AddonManager.getAddonsByTypes(["extension"], function(extensionAddons) { do_check_eq(extensionAddons.length, 3); + run_test_2(); }); }); diff --git a/toolkit/mozapps/plugins/tests/browser_bug435788.js b/toolkit/mozapps/plugins/tests/browser_bug435788.js index 8245a30f4fbf..11c3774954a1 100644 --- a/toolkit/mozapps/plugins/tests/browser_bug435788.js +++ b/toolkit/mozapps/plugins/tests/browser_bug435788.js @@ -417,10 +417,8 @@ function test_7_complete() { is(item.status, "Failed", "Should have been a failed install"); AddonManager.getInstalls(null, function(installs) { - is(installs.length, 4, "Should be just one install"); - is(installs[3].state, AddonManager.STATE_DOWNLOAD_FAILED, "Should have failed to download"); - is(installs[3].addon.id, "bug435788_1@tests.mozilla.org", "Should have installed the extension"); - installs[3].cancel(); + is(installs.length, 1, "Should be one active installs"); + installs[0].cancel(); gPFS.document.documentElement.getButton("finish").click(); }); @@ -473,7 +471,7 @@ function test_8_complete() { is(item.status, "Failed", "Should have not been a successful install"); AddonManager.getInstalls(null, function(installs) { - is(installs.length, 5, "Should not be any installs"); + is(installs.length, 0, "Should not be any installs"); gPFS.document.documentElement.getButton("finish").click(); });