michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Test cancelling add-on update checks while in progress (bug 925389) michael@0: michael@0: Components.utils.import("resource://gre/modules/Promise.jsm"); michael@0: michael@0: // The test extension uses an insecure update url. michael@0: Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false); michael@0: Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false); michael@0: michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); michael@0: michael@0: // Set up an HTTP server to respond to update requests michael@0: Components.utils.import("resource://testing-common/httpd.js"); michael@0: michael@0: const profileDir = gProfD.clone(); michael@0: profileDir.append("extensions"); michael@0: michael@0: // Return a promise that resolves with an addon retrieved by michael@0: // AddonManager.getAddonByID() michael@0: function promiseGetAddon(aID) { michael@0: let p = Promise.defer(); michael@0: AddonManager.getAddonByID(aID, p.resolve); michael@0: return p.promise; michael@0: } michael@0: michael@0: function run_test() { michael@0: // Kick off the task-based tests... michael@0: run_next_test(); michael@0: } michael@0: michael@0: // Install one extension michael@0: // Start download of update check (but delay HTTP response) michael@0: // Cancel update check michael@0: // - ensure we get cancel notification michael@0: // complete HTTP response michael@0: // - ensure no callbacks after cancel michael@0: // - ensure update is gone michael@0: michael@0: // Create an addon update listener containing a promise michael@0: // that resolves when the update is cancelled michael@0: function makeCancelListener() { michael@0: let updated = Promise.defer(); michael@0: return { michael@0: onUpdateAvailable: function(addon, install) { michael@0: updated.reject("Should not have seen onUpdateAvailable notification"); michael@0: }, michael@0: michael@0: onUpdateFinished: function(aAddon, aError) { michael@0: do_print("onUpdateCheckFinished: " + aAddon.id + " " + aError); michael@0: updated.resolve(aError); michael@0: }, michael@0: promise: updated.promise michael@0: }; michael@0: } michael@0: michael@0: // Set up the HTTP server so that we can control when it responds michael@0: let httpReceived = Promise.defer(); michael@0: function dataHandler(aRequest, aResponse) { michael@0: asyncResponse = aResponse; michael@0: aResponse.processAsync(); michael@0: httpReceived.resolve([aRequest, aResponse]); michael@0: } michael@0: var testserver = new HttpServer(); michael@0: testserver.registerDirectory("/addons/", do_get_file("addons")); michael@0: testserver.registerPathHandler("/data/test_update.rdf", dataHandler); michael@0: testserver.start(-1); michael@0: gPort = testserver.identity.primaryPort; michael@0: michael@0: // Set up an add-on for update check michael@0: writeInstallRDFForExtension({ michael@0: id: "addon1@tests.mozilla.org", michael@0: version: "1.0", michael@0: updateURL: "http://localhost:" + gPort + "/data/test_update.rdf", michael@0: targetApplications: [{ michael@0: id: "xpcshell@tests.mozilla.org", michael@0: minVersion: "1", michael@0: maxVersion: "1" michael@0: }], michael@0: name: "Test Addon 1", michael@0: }, profileDir); michael@0: michael@0: add_task(function cancel_during_check() { michael@0: startupManager(); michael@0: michael@0: let a1 = yield promiseGetAddon("addon1@tests.mozilla.org"); michael@0: do_check_neq(a1, null); michael@0: michael@0: let listener = makeCancelListener(); michael@0: a1.findUpdates(listener, AddonManager.UPDATE_WHEN_USER_REQUESTED); michael@0: michael@0: // Wait for the http request to arrive michael@0: let [request, response] = yield httpReceived.promise; michael@0: michael@0: // cancelUpdate returns true if there is an update check in progress michael@0: do_check_true(a1.cancelUpdate()); michael@0: michael@0: let updateResult = yield listener.promise; michael@0: do_check_eq(AddonManager.UPDATE_STATUS_CANCELLED, updateResult); michael@0: michael@0: // Now complete the HTTP request michael@0: let file = do_get_cwd(); michael@0: file.append("data"); michael@0: file.append("test_update.rdf"); michael@0: let data = loadFile(file); michael@0: response.write(data); michael@0: response.finish(); michael@0: michael@0: // trying to cancel again should return false, i.e. nothing to cancel michael@0: do_check_false(a1.cancelUpdate()); michael@0: michael@0: yield true; michael@0: }); michael@0: michael@0: // Test that update check is cancelled if the XPI provider shuts down while michael@0: // the update check is in progress michael@0: add_task(function shutdown_during_check() { michael@0: // Reset our HTTP listener michael@0: httpReceived = Promise.defer(); michael@0: michael@0: let a1 = yield promiseGetAddon("addon1@tests.mozilla.org"); michael@0: do_check_neq(a1, null); michael@0: michael@0: let listener = makeCancelListener(); michael@0: a1.findUpdates(listener, AddonManager.UPDATE_WHEN_USER_REQUESTED); michael@0: michael@0: // Wait for the http request to arrive michael@0: let [request, response] = yield httpReceived.promise; michael@0: michael@0: shutdownManager(); michael@0: michael@0: let updateResult = yield listener.promise; michael@0: do_check_eq(AddonManager.UPDATE_STATUS_CANCELLED, updateResult); michael@0: michael@0: // Now complete the HTTP request michael@0: let file = do_get_cwd(); michael@0: file.append("data"); michael@0: file.append("test_update.rdf"); michael@0: let data = loadFile(file); michael@0: response.write(data); michael@0: response.finish(); michael@0: michael@0: // trying to cancel again should return false, i.e. nothing to cancel michael@0: do_check_false(a1.cancelUpdate()); michael@0: michael@0: yield testserver.stop(Promise.defer().resolve); michael@0: });