Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 3 | */ |
michael@0 | 4 | |
michael@0 | 5 | // Test cancelling add-on update checks while in progress (bug 925389) |
michael@0 | 6 | |
michael@0 | 7 | Components.utils.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | // The test extension uses an insecure update url. |
michael@0 | 10 | Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false); |
michael@0 | 11 | Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false); |
michael@0 | 12 | |
michael@0 | 13 | createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); |
michael@0 | 14 | |
michael@0 | 15 | // Set up an HTTP server to respond to update requests |
michael@0 | 16 | Components.utils.import("resource://testing-common/httpd.js"); |
michael@0 | 17 | |
michael@0 | 18 | const profileDir = gProfD.clone(); |
michael@0 | 19 | profileDir.append("extensions"); |
michael@0 | 20 | |
michael@0 | 21 | // Return a promise that resolves with an addon retrieved by |
michael@0 | 22 | // AddonManager.getAddonByID() |
michael@0 | 23 | function promiseGetAddon(aID) { |
michael@0 | 24 | let p = Promise.defer(); |
michael@0 | 25 | AddonManager.getAddonByID(aID, p.resolve); |
michael@0 | 26 | return p.promise; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | function run_test() { |
michael@0 | 30 | // Kick off the task-based tests... |
michael@0 | 31 | run_next_test(); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | // Install one extension |
michael@0 | 35 | // Start download of update check (but delay HTTP response) |
michael@0 | 36 | // Cancel update check |
michael@0 | 37 | // - ensure we get cancel notification |
michael@0 | 38 | // complete HTTP response |
michael@0 | 39 | // - ensure no callbacks after cancel |
michael@0 | 40 | // - ensure update is gone |
michael@0 | 41 | |
michael@0 | 42 | // Create an addon update listener containing a promise |
michael@0 | 43 | // that resolves when the update is cancelled |
michael@0 | 44 | function makeCancelListener() { |
michael@0 | 45 | let updated = Promise.defer(); |
michael@0 | 46 | return { |
michael@0 | 47 | onUpdateAvailable: function(addon, install) { |
michael@0 | 48 | updated.reject("Should not have seen onUpdateAvailable notification"); |
michael@0 | 49 | }, |
michael@0 | 50 | |
michael@0 | 51 | onUpdateFinished: function(aAddon, aError) { |
michael@0 | 52 | do_print("onUpdateCheckFinished: " + aAddon.id + " " + aError); |
michael@0 | 53 | updated.resolve(aError); |
michael@0 | 54 | }, |
michael@0 | 55 | promise: updated.promise |
michael@0 | 56 | }; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | // Set up the HTTP server so that we can control when it responds |
michael@0 | 60 | let httpReceived = Promise.defer(); |
michael@0 | 61 | function dataHandler(aRequest, aResponse) { |
michael@0 | 62 | asyncResponse = aResponse; |
michael@0 | 63 | aResponse.processAsync(); |
michael@0 | 64 | httpReceived.resolve([aRequest, aResponse]); |
michael@0 | 65 | } |
michael@0 | 66 | var testserver = new HttpServer(); |
michael@0 | 67 | testserver.registerDirectory("/addons/", do_get_file("addons")); |
michael@0 | 68 | testserver.registerPathHandler("/data/test_update.rdf", dataHandler); |
michael@0 | 69 | testserver.start(-1); |
michael@0 | 70 | gPort = testserver.identity.primaryPort; |
michael@0 | 71 | |
michael@0 | 72 | // Set up an add-on for update check |
michael@0 | 73 | writeInstallRDFForExtension({ |
michael@0 | 74 | id: "addon1@tests.mozilla.org", |
michael@0 | 75 | version: "1.0", |
michael@0 | 76 | updateURL: "http://localhost:" + gPort + "/data/test_update.rdf", |
michael@0 | 77 | targetApplications: [{ |
michael@0 | 78 | id: "xpcshell@tests.mozilla.org", |
michael@0 | 79 | minVersion: "1", |
michael@0 | 80 | maxVersion: "1" |
michael@0 | 81 | }], |
michael@0 | 82 | name: "Test Addon 1", |
michael@0 | 83 | }, profileDir); |
michael@0 | 84 | |
michael@0 | 85 | add_task(function cancel_during_check() { |
michael@0 | 86 | startupManager(); |
michael@0 | 87 | |
michael@0 | 88 | let a1 = yield promiseGetAddon("addon1@tests.mozilla.org"); |
michael@0 | 89 | do_check_neq(a1, null); |
michael@0 | 90 | |
michael@0 | 91 | let listener = makeCancelListener(); |
michael@0 | 92 | a1.findUpdates(listener, AddonManager.UPDATE_WHEN_USER_REQUESTED); |
michael@0 | 93 | |
michael@0 | 94 | // Wait for the http request to arrive |
michael@0 | 95 | let [request, response] = yield httpReceived.promise; |
michael@0 | 96 | |
michael@0 | 97 | // cancelUpdate returns true if there is an update check in progress |
michael@0 | 98 | do_check_true(a1.cancelUpdate()); |
michael@0 | 99 | |
michael@0 | 100 | let updateResult = yield listener.promise; |
michael@0 | 101 | do_check_eq(AddonManager.UPDATE_STATUS_CANCELLED, updateResult); |
michael@0 | 102 | |
michael@0 | 103 | // Now complete the HTTP request |
michael@0 | 104 | let file = do_get_cwd(); |
michael@0 | 105 | file.append("data"); |
michael@0 | 106 | file.append("test_update.rdf"); |
michael@0 | 107 | let data = loadFile(file); |
michael@0 | 108 | response.write(data); |
michael@0 | 109 | response.finish(); |
michael@0 | 110 | |
michael@0 | 111 | // trying to cancel again should return false, i.e. nothing to cancel |
michael@0 | 112 | do_check_false(a1.cancelUpdate()); |
michael@0 | 113 | |
michael@0 | 114 | yield true; |
michael@0 | 115 | }); |
michael@0 | 116 | |
michael@0 | 117 | // Test that update check is cancelled if the XPI provider shuts down while |
michael@0 | 118 | // the update check is in progress |
michael@0 | 119 | add_task(function shutdown_during_check() { |
michael@0 | 120 | // Reset our HTTP listener |
michael@0 | 121 | httpReceived = Promise.defer(); |
michael@0 | 122 | |
michael@0 | 123 | let a1 = yield promiseGetAddon("addon1@tests.mozilla.org"); |
michael@0 | 124 | do_check_neq(a1, null); |
michael@0 | 125 | |
michael@0 | 126 | let listener = makeCancelListener(); |
michael@0 | 127 | a1.findUpdates(listener, AddonManager.UPDATE_WHEN_USER_REQUESTED); |
michael@0 | 128 | |
michael@0 | 129 | // Wait for the http request to arrive |
michael@0 | 130 | let [request, response] = yield httpReceived.promise; |
michael@0 | 131 | |
michael@0 | 132 | shutdownManager(); |
michael@0 | 133 | |
michael@0 | 134 | let updateResult = yield listener.promise; |
michael@0 | 135 | do_check_eq(AddonManager.UPDATE_STATUS_CANCELLED, updateResult); |
michael@0 | 136 | |
michael@0 | 137 | // Now complete the HTTP request |
michael@0 | 138 | let file = do_get_cwd(); |
michael@0 | 139 | file.append("data"); |
michael@0 | 140 | file.append("test_update.rdf"); |
michael@0 | 141 | let data = loadFile(file); |
michael@0 | 142 | response.write(data); |
michael@0 | 143 | response.finish(); |
michael@0 | 144 | |
michael@0 | 145 | // trying to cancel again should return false, i.e. nothing to cancel |
michael@0 | 146 | do_check_false(a1.cancelUpdate()); |
michael@0 | 147 | |
michael@0 | 148 | yield testserver.stop(Promise.defer().resolve); |
michael@0 | 149 | }); |