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