How to use install method in Puppeteer

Best JavaScript code snippet using puppeteer

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

browser_installssl.js

Source: browser_installssl.js Github

copy
1/* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
4
5const xpi = RELATIVE_DIR + "addons/browser_installssl.xpi";
6const redirect = RELATIVE_DIR + "redirect.sjs?";
7const SUCCESS = 0;
8const NETWORK_FAILURE = AddonManager.ERROR_NETWORK_FAILURE;
9
10const HTTP = "http://example.com/";
11const HTTPS = "https://example.com/";
12const NOCERT = "https://nocert.example.com/";
13const SELFSIGNED = "https://self-signed.example.com/";
14const UNTRUSTED = "https://untrusted.example.com/";
15const EXPIRED = "https://expired.example.com/";
16
17const PREF_INSTALL_REQUIREBUILTINCERTS = "extensions.install.requireBuiltInCerts";
18
19var gTests = [];
20var gStart = 0;
21var gLast = 0;
22var gPendingInstall = null;
23
24function test() {
25  gStart = Date.now();
26  requestLongerTimeout(4);
27  waitForExplicitFinish();
28
29  registerCleanupFunction(function() {
30    var cos = Cc["@mozilla.org/security/certoverride;1"].
31              getService(Ci.nsICertOverrideService);
32    cos.clearValidityOverride("nocert.example.com", -1);
33    cos.clearValidityOverride("self-signed.example.com", -1);
34    cos.clearValidityOverride("untrusted.example.com", -1);
35    cos.clearValidityOverride("expired.example.com", -1);
36
37    try {
38      Services.prefs.clearUserPref(PREF_INSTALL_REQUIREBUILTINCERTS);
39    } catch (e) {
40    }
41
42    if (gPendingInstall) {
43      gTests = [];
44      ok(false, "Timed out in the middle of downloading " + gPendingInstall.sourceURI.spec);
45      try {
46        gPendingInstall.cancel();
47      } catch (e) {
48      }
49    }
50  });
51
52  run_next_test();
53}
54
55function end_test() {
56  info("All tests completed in " + (Date.now() - gStart) + "ms");
57  finish();
58}
59
60function add_install_test(mainURL, redirectURL, expectedStatus) {
61  gTests.push([mainURL, redirectURL, expectedStatus]);
62}
63
64function run_install_tests(callback) {
65  function run_next_install_test() {
66    if (gTests.length == 0) {
67      callback();
68      return;
69    }
70    gLast = Date.now();
71
72    let [mainURL, redirectURL, expectedStatus] = gTests.shift();
73    if (redirectURL) {
74      var url = mainURL + redirect + redirectURL + xpi;
75      var message = "Should have seen the right result for an install redirected from " +
76                    mainURL + " to " + redirectURL;
77    } else {
78      url = mainURL + xpi;
79      message = "Should have seen the right result for an install from " +
80                mainURL;
81    }
82
83    AddonManager.getInstallForURL(url, function(install) {
84      gPendingInstall = install;
85      install.addListener({
86        onDownloadEnded(install) {
87          is(SUCCESS, expectedStatus, message);
88          info("Install test ran in " + (Date.now() - gLast) + "ms");
89          // Don't proceed with the install
90          install.cancel();
91          gPendingInstall = null;
92          run_next_install_test();
93          return false;
94        },
95
96        onDownloadFailed(install) {
97          is(install.error, expectedStatus, message);
98          info("Install test ran in " + (Date.now() - gLast) + "ms");
99          gPendingInstall = null;
100          run_next_install_test();
101        }
102      });
103      install.install();
104    }, "application/x-xpinstall");
105  }
106
107  run_next_install_test();
108}
109
110// Add overrides for the bad certificates
111function addCertOverrides() {
112  addCertOverride("nocert.example.com", Ci.nsICertOverrideService.ERROR_MISMATCH);
113  addCertOverride("self-signed.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED);
114  addCertOverride("untrusted.example.com", Ci.nsICertOverrideService.ERROR_UNTRUSTED);
115  addCertOverride("expired.example.com", Ci.nsICertOverrideService.ERROR_TIME);
116}
117
118// Runs tests with built-in certificates required, no certificate exceptions
119// and no hashes
120add_test(function() {
121  // Tests that a simple install works as expected.
122  add_install_test(HTTP, null, SUCCESS);
123  add_install_test(HTTPS, null, NETWORK_FAILURE);
124  add_install_test(NOCERT, null, NETWORK_FAILURE);
125  add_install_test(SELFSIGNED, null, NETWORK_FAILURE);
126  add_install_test(UNTRUSTED, null, NETWORK_FAILURE);
127  add_install_test(EXPIRED, null, NETWORK_FAILURE);
128
129  // Tests that redirecting from http to other servers works as expected
130  add_install_test(HTTP, HTTP, SUCCESS);
131  add_install_test(HTTP, HTTPS, SUCCESS);
132  add_install_test(HTTP, NOCERT, NETWORK_FAILURE);
133  add_install_test(HTTP, SELFSIGNED, NETWORK_FAILURE);
134  add_install_test(HTTP, UNTRUSTED, NETWORK_FAILURE);
135  add_install_test(HTTP, EXPIRED, NETWORK_FAILURE);
136
137  // Tests that redirecting from valid https to other servers works as expected
138  add_install_test(HTTPS, HTTP, NETWORK_FAILURE);
139  add_install_test(HTTPS, HTTPS, NETWORK_FAILURE);
140  add_install_test(HTTPS, NOCERT, NETWORK_FAILURE);
141  add_install_test(HTTPS, SELFSIGNED, NETWORK_FAILURE);
142  add_install_test(HTTPS, UNTRUSTED, NETWORK_FAILURE);
143  add_install_test(HTTPS, EXPIRED, NETWORK_FAILURE);
144
145  // Tests that redirecting from nocert https to other servers works as expected
146  add_install_test(NOCERT, HTTP, NETWORK_FAILURE);
147  add_install_test(NOCERT, HTTPS, NETWORK_FAILURE);
148  add_install_test(NOCERT, NOCERT, NETWORK_FAILURE);
149  add_install_test(NOCERT, SELFSIGNED, NETWORK_FAILURE);
150  add_install_test(NOCERT, UNTRUSTED, NETWORK_FAILURE);
151  add_install_test(NOCERT, EXPIRED, NETWORK_FAILURE);
152
153  // Tests that redirecting from self-signed https to other servers works as expected
154  add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE);
155  add_install_test(SELFSIGNED, HTTPS, NETWORK_FAILURE);
156  add_install_test(SELFSIGNED, NOCERT, NETWORK_FAILURE);
157  add_install_test(SELFSIGNED, SELFSIGNED, NETWORK_FAILURE);
158  add_install_test(SELFSIGNED, UNTRUSTED, NETWORK_FAILURE);
159  add_install_test(SELFSIGNED, EXPIRED, NETWORK_FAILURE);
160
161  // Tests that redirecting from untrusted https to other servers works as expected
162  add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE);
163  add_install_test(UNTRUSTED, HTTPS, NETWORK_FAILURE);
164  add_install_test(UNTRUSTED, NOCERT, NETWORK_FAILURE);
165  add_install_test(UNTRUSTED, SELFSIGNED, NETWORK_FAILURE);
166  add_install_test(UNTRUSTED, UNTRUSTED, NETWORK_FAILURE);
167  add_install_test(UNTRUSTED, EXPIRED, NETWORK_FAILURE);
168
169  // Tests that redirecting from expired https to other servers works as expected
170  add_install_test(EXPIRED, HTTP, NETWORK_FAILURE);
171  add_install_test(EXPIRED, HTTPS, NETWORK_FAILURE);
172  add_install_test(EXPIRED, NOCERT, NETWORK_FAILURE);
173  add_install_test(EXPIRED, SELFSIGNED, NETWORK_FAILURE);
174  add_install_test(EXPIRED, UNTRUSTED, NETWORK_FAILURE);
175  add_install_test(EXPIRED, EXPIRED, NETWORK_FAILURE);
176
177  run_install_tests(run_next_test);
178});
179
180// Runs tests without requiring built-in certificates, no certificate
181// exceptions and no hashes
182add_test(function() {
183  Services.prefs.setBoolPref(PREF_INSTALL_REQUIREBUILTINCERTS, false);
184
185  // Tests that a simple install works as expected.
186  add_install_test(HTTP, null, SUCCESS);
187  add_install_test(HTTPS, null, SUCCESS);
188  add_install_test(NOCERT, null, NETWORK_FAILURE);
189  add_install_test(SELFSIGNED, null, NETWORK_FAILURE);
190  add_install_test(UNTRUSTED, null, NETWORK_FAILURE);
191  add_install_test(EXPIRED, null, NETWORK_FAILURE);
192
193  // Tests that redirecting from http to other servers works as expected
194  add_install_test(HTTP, HTTP, SUCCESS);
195  add_install_test(HTTP, HTTPS, SUCCESS);
196  add_install_test(HTTP, NOCERT, NETWORK_FAILURE);
197  add_install_test(HTTP, SELFSIGNED, NETWORK_FAILURE);
198  add_install_test(HTTP, UNTRUSTED, NETWORK_FAILURE);
199  add_install_test(HTTP, EXPIRED, NETWORK_FAILURE);
200
201  // Tests that redirecting from valid https to other servers works as expected
202  add_install_test(HTTPS, HTTP, NETWORK_FAILURE);
203  add_install_test(HTTPS, HTTPS, SUCCESS);
204  add_install_test(HTTPS, NOCERT, NETWORK_FAILURE);
205  add_install_test(HTTPS, SELFSIGNED, NETWORK_FAILURE);
206  add_install_test(HTTPS, UNTRUSTED, NETWORK_FAILURE);
207  add_install_test(HTTPS, EXPIRED, NETWORK_FAILURE);
208
209  // Tests that redirecting from nocert https to other servers works as expected
210  add_install_test(NOCERT, HTTP, NETWORK_FAILURE);
211  add_install_test(NOCERT, HTTPS, NETWORK_FAILURE);
212  add_install_test(NOCERT, NOCERT, NETWORK_FAILURE);
213  add_install_test(NOCERT, SELFSIGNED, NETWORK_FAILURE);
214  add_install_test(NOCERT, UNTRUSTED, NETWORK_FAILURE);
215  add_install_test(NOCERT, EXPIRED, NETWORK_FAILURE);
216
217  // Tests that redirecting from self-signed https to other servers works as expected
218  add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE);
219  add_install_test(SELFSIGNED, HTTPS, NETWORK_FAILURE);
220  add_install_test(SELFSIGNED, NOCERT, NETWORK_FAILURE);
221  add_install_test(SELFSIGNED, SELFSIGNED, NETWORK_FAILURE);
222  add_install_test(SELFSIGNED, UNTRUSTED, NETWORK_FAILURE);
223  add_install_test(SELFSIGNED, EXPIRED, NETWORK_FAILURE);
224
225  // Tests that redirecting from untrusted https to other servers works as expected
226  add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE);
227  add_install_test(UNTRUSTED, HTTPS, NETWORK_FAILURE);
228  add_install_test(UNTRUSTED, NOCERT, NETWORK_FAILURE);
229  add_install_test(UNTRUSTED, SELFSIGNED, NETWORK_FAILURE);
230  add_install_test(UNTRUSTED, UNTRUSTED, NETWORK_FAILURE);
231  add_install_test(UNTRUSTED, EXPIRED, NETWORK_FAILURE);
232
233  // Tests that redirecting from expired https to other servers works as expected
234  add_install_test(EXPIRED, HTTP, NETWORK_FAILURE);
235  add_install_test(EXPIRED, HTTPS, NETWORK_FAILURE);
236  add_install_test(EXPIRED, NOCERT, NETWORK_FAILURE);
237  add_install_test(EXPIRED, SELFSIGNED, NETWORK_FAILURE);
238  add_install_test(EXPIRED, UNTRUSTED, NETWORK_FAILURE);
239  add_install_test(EXPIRED, EXPIRED, NETWORK_FAILURE);
240
241  run_install_tests(run_next_test);
242});
243
244// Runs tests with built-in certificates required, all certificate exceptions
245// and no hashes
246add_test(function() {
247  Services.prefs.clearUserPref(PREF_INSTALL_REQUIREBUILTINCERTS);
248  addCertOverrides();
249
250  // Tests that a simple install works as expected.
251  add_install_test(HTTP, null, SUCCESS);
252  add_install_test(HTTPS, null, NETWORK_FAILURE);
253  add_install_test(NOCERT, null, NETWORK_FAILURE);
254  add_install_test(SELFSIGNED, null, NETWORK_FAILURE);
255  add_install_test(UNTRUSTED, null, NETWORK_FAILURE);
256  add_install_test(EXPIRED, null, NETWORK_FAILURE);
257
258  // Tests that redirecting from http to other servers works as expected
259  add_install_test(HTTP, HTTP, SUCCESS);
260  add_install_test(HTTP, HTTPS, SUCCESS);
261  add_install_test(HTTP, NOCERT, SUCCESS);
262  add_install_test(HTTP, SELFSIGNED, SUCCESS);
263  add_install_test(HTTP, UNTRUSTED, SUCCESS);
264  add_install_test(HTTP, EXPIRED, SUCCESS);
265
266  // Tests that redirecting from valid https to other servers works as expected
267  add_install_test(HTTPS, HTTP, NETWORK_FAILURE);
268  add_install_test(HTTPS, HTTPS, NETWORK_FAILURE);
269  add_install_test(HTTPS, NOCERT, NETWORK_FAILURE);
270  add_install_test(HTTPS, SELFSIGNED, NETWORK_FAILURE);
271  add_install_test(HTTPS, UNTRUSTED, NETWORK_FAILURE);
272  add_install_test(HTTPS, EXPIRED, NETWORK_FAILURE);
273
274  // Tests that redirecting from nocert https to other servers works as expected
275  add_install_test(NOCERT, HTTP, NETWORK_FAILURE);
276  add_install_test(NOCERT, HTTPS, NETWORK_FAILURE);
277  add_install_test(NOCERT, NOCERT, NETWORK_FAILURE);
278  add_install_test(NOCERT, SELFSIGNED, NETWORK_FAILURE);
279  add_install_test(NOCERT, UNTRUSTED, NETWORK_FAILURE);
280  add_install_test(NOCERT, EXPIRED, NETWORK_FAILURE);
281
282  // Tests that redirecting from self-signed https to other servers works as expected
283  add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE);
284  add_install_test(SELFSIGNED, HTTPS, NETWORK_FAILURE);
285  add_install_test(SELFSIGNED, NOCERT, NETWORK_FAILURE);
286  add_install_test(SELFSIGNED, SELFSIGNED, NETWORK_FAILURE);
287  add_install_test(SELFSIGNED, UNTRUSTED, NETWORK_FAILURE);
288  add_install_test(SELFSIGNED, EXPIRED, NETWORK_FAILURE);
289
290  // Tests that redirecting from untrusted https to other servers works as expected
291  add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE);
292  add_install_test(UNTRUSTED, HTTPS, NETWORK_FAILURE);
293  add_install_test(UNTRUSTED, NOCERT, NETWORK_FAILURE);
294  add_install_test(UNTRUSTED, SELFSIGNED, NETWORK_FAILURE);
295  add_install_test(UNTRUSTED, UNTRUSTED, NETWORK_FAILURE);
296  add_install_test(UNTRUSTED, EXPIRED, NETWORK_FAILURE);
297
298  // Tests that redirecting from expired https to other servers works as expected
299  add_install_test(EXPIRED, HTTP, NETWORK_FAILURE);
300  add_install_test(EXPIRED, HTTPS, NETWORK_FAILURE);
301  add_install_test(EXPIRED, NOCERT, NETWORK_FAILURE);
302  add_install_test(EXPIRED, SELFSIGNED, NETWORK_FAILURE);
303  add_install_test(EXPIRED, UNTRUSTED, NETWORK_FAILURE);
304  add_install_test(EXPIRED, EXPIRED, NETWORK_FAILURE);
305
306  run_install_tests(run_next_test);
307});
308
309// Runs tests without requiring built-in certificates, all certificate
310// exceptions and no hashes
311add_test(function() {
312  Services.prefs.setBoolPref(PREF_INSTALL_REQUIREBUILTINCERTS, false);
313
314  // Tests that a simple install works as expected.
315  add_install_test(HTTP, null, SUCCESS);
316  add_install_test(HTTPS, null, SUCCESS);
317  add_install_test(NOCERT, null, SUCCESS);
318  add_install_test(SELFSIGNED, null, SUCCESS);
319  add_install_test(UNTRUSTED, null, SUCCESS);
320  add_install_test(EXPIRED, null, SUCCESS);
321
322  // Tests that redirecting from http to other servers works as expected
323  add_install_test(HTTP, HTTP, SUCCESS);
324  add_install_test(HTTP, HTTPS, SUCCESS);
325  add_install_test(HTTP, NOCERT, SUCCESS);
326  add_install_test(HTTP, SELFSIGNED, SUCCESS);
327  add_install_test(HTTP, UNTRUSTED, SUCCESS);
328  add_install_test(HTTP, EXPIRED, SUCCESS);
329
330  // Tests that redirecting from valid https to other servers works as expected
331  add_install_test(HTTPS, HTTP, NETWORK_FAILURE);
332  add_install_test(HTTPS, HTTPS, SUCCESS);
333  add_install_test(HTTPS, NOCERT, SUCCESS);
334  add_install_test(HTTPS, SELFSIGNED, SUCCESS);
335  add_install_test(HTTPS, UNTRUSTED, SUCCESS);
336  add_install_test(HTTPS, EXPIRED, SUCCESS);
337
338  // Tests that redirecting from nocert https to other servers works as expected
339  add_install_test(NOCERT, HTTP, NETWORK_FAILURE);
340  add_install_test(NOCERT, HTTPS, SUCCESS);
341  add_install_test(NOCERT, NOCERT, SUCCESS);
342  add_install_test(NOCERT, SELFSIGNED, SUCCESS);
343  add_install_test(NOCERT, UNTRUSTED, SUCCESS);
344  add_install_test(NOCERT, EXPIRED, SUCCESS);
345
346  // Tests that redirecting from self-signed https to other servers works as expected
347  add_install_test(SELFSIGNED, HTTP, NETWORK_FAILURE);
348  add_install_test(SELFSIGNED, HTTPS, SUCCESS);
349  add_install_test(SELFSIGNED, NOCERT, SUCCESS);
350  add_install_test(SELFSIGNED, SELFSIGNED, SUCCESS);
351  add_install_test(SELFSIGNED, UNTRUSTED, SUCCESS);
352  add_install_test(SELFSIGNED, EXPIRED, SUCCESS);
353
354  // Tests that redirecting from untrusted https to other servers works as expected
355  add_install_test(UNTRUSTED, HTTP, NETWORK_FAILURE);
356  add_install_test(UNTRUSTED, HTTPS, SUCCESS);
357  add_install_test(UNTRUSTED, NOCERT, SUCCESS);
358  add_install_test(UNTRUSTED, SELFSIGNED, SUCCESS);
359  add_install_test(UNTRUSTED, UNTRUSTED, SUCCESS);
360  add_install_test(UNTRUSTED, EXPIRED, SUCCESS);
361
362  // Tests that redirecting from expired https to other servers works as expected
363  add_install_test(EXPIRED, HTTP, NETWORK_FAILURE);
364  add_install_test(EXPIRED, HTTPS, SUCCESS);
365  add_install_test(EXPIRED, NOCERT, SUCCESS);
366  add_install_test(EXPIRED, SELFSIGNED, SUCCESS);
367  add_install_test(EXPIRED, UNTRUSTED, SUCCESS);
368  add_install_test(EXPIRED, EXPIRED, SUCCESS);
369
370  run_install_tests(run_next_test);
371});
372
Full Screen

test_install.js

Source: test_install.js Github

copy
1/* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
4
5// This verifies that add-ons can be installed from XPI files
6var Cc = Components.classes;
7var Ci = Components.interfaces;
8var Cu = Components.utils;
9
10// install.rdf size, icon.png, icon64.png size
11const ADDON1_SIZE = 705 + 16 + 16;
12
13Cu.import("resource://gre/modules/Services.jsm");
14Cu.import("resource://gre/modules/NetUtil.jsm");
15Cu.import("resource://testing-common/httpd.js");
16
17var testserver;
18var gInstallDate;
19
20// The test extension uses an insecure update url.
21Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false);
22Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false);
23
24const profileDir = gProfD.clone();
25profileDir.append("extensions");
26
27function run_test() {
28  createAppInfo("[email protected]", "XPCShell", "1", "1.9.2");
29
30  startupManager();
31  // Make sure we only register once despite multiple calls
32  AddonManager.addInstallListener(InstallListener);
33  AddonManager.addAddonListener(AddonListener);
34  AddonManager.addInstallListener(InstallListener);
35  AddonManager.addAddonListener(AddonListener);
36
37  // Create and configure the HTTP server.
38  testserver = new HttpServer();
39  testserver.registerDirectory("/addons/", do_get_file("addons"));
40  testserver.registerDirectory("/data/", do_get_file("data"));
41  testserver.registerPathHandler("/redirect", function(aRequest, aResponse) {
42    aResponse.setStatusLine(null, 301, "Moved Permanently");
43    let url = aRequest.host + ":" + aRequest.port + aRequest.queryString;
44    aResponse.setHeader("Location", "http://" + url);
45  });
46  testserver.start(-1);
47  gPort = testserver.identity.primaryPort;
48
49  do_test_pending();
50  run_test_1();
51}
52
53function end_test() {
54  testserver.stop(do_test_finished);
55}
56
57// Checks that an install from a local file proceeds as expected
58function run_test_1() {
59  prepare_test({ }, [
60    "onNewInstall"
61  ]);
62
63  AddonManager.getInstallForFile(do_get_addon("test_install1"), function(install) {
64    ensure_test_completed();
65
66    do_check_neq(install, null);
67    do_check_eq(install.type, "extension");
68    do_check_eq(install.version, "1.0");
69    do_check_eq(install.name, "Test 1");
70    do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
71    do_check_true(install.addon.hasResource("install.rdf"));
72    do_check_neq(install.addon.syncGUID, null);
73    do_check_eq(install.addon.install, install);
74    do_check_eq(install.addon.size, ADDON1_SIZE);
75    do_check_true(hasFlag(install.addon.operationsRequiringRestart,
76                          AddonManager.OP_NEEDS_RESTART_INSTALL));
77    let file = do_get_addon("test_install1");
78    let uri = Services.io.newFileURI(file).spec;
79    do_check_eq(install.addon.getResourceURI("install.rdf").spec, "jar:" + uri + "!/install.rdf");
80    do_check_eq(install.addon.iconURL, "jar:" + uri + "!/icon.png");
81    do_check_eq(install.addon.icon64URL, "jar:" + uri + "!/icon64.png");
82    do_check_eq(install.iconURL, null);
83
84    do_check_eq(install.sourceURI.spec, uri);
85    do_check_eq(install.addon.sourceURI.spec, uri);
86
87    AddonManager.getAllInstalls(function(activeInstalls) {
88      do_check_eq(activeInstalls.length, 1);
89      do_check_eq(activeInstalls[0], install);
90
91      AddonManager.getInstallsByTypes(["foo"], function(fooInstalls) {
92        do_check_eq(fooInstalls.length, 0);
93
94        AddonManager.getInstallsByTypes(["extension"], function(extensionInstalls) {
95          do_check_eq(extensionInstalls.length, 1);
96          do_check_eq(extensionInstalls[0], install);
97
98          prepare_test({
99            "[email protected]": [
100              "onInstalling"
101            ]
102          }, [
103            "onInstallStarted",
104            "onInstallEnded",
105          ], function() {
106            check_test_1(install.addon.syncGUID);
107          });
108          install.install();
109        });
110      });
111    });
112  });
113}
114
115function check_test_1(installSyncGUID) {
116  ensure_test_completed();
117  AddonManager.getAddonByID("[email protected]", function(olda1) {
118    do_check_eq(olda1, null);
119
120    AddonManager.getAddonsWithOperationsByTypes(null, callback_soon(async function(pendingAddons) {
121      do_check_eq(pendingAddons.length, 1);
122      do_check_eq(pendingAddons[0].id, "[email protected]");
123      let uri = NetUtil.newURI(pendingAddons[0].iconURL);
124      if (uri instanceof AM_Ci.nsIJARURI) {
125        let jarURI = uri.QueryInterface(AM_Ci.nsIJARURI);
126        let archiveURI = jarURI.JARFile;
127        let archiveFile = archiveURI.QueryInterface(AM_Ci.nsIFileURL).file;
128        let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].
129                        createInstance(Ci.nsIZipReader);
130        try {
131          zipReader.open(archiveFile);
132          do_check_true(zipReader.hasEntry(jarURI.JAREntry));
133        } finally {
134          zipReader.close();
135        }
136      } else {
137        let iconFile = uri.QueryInterface(AM_Ci.nsIFileURL).file;
138        do_check_true(iconFile.exists());
139        // Make the iconFile predictably old.
140        iconFile.lastModifiedTime = Date.now() - MAKE_FILE_OLD_DIFFERENCE;
141      }
142
143      // Make the pending install have a sensible date
144      let updateDate = Date.now();
145      let extURI = pendingAddons[0].getResourceURI("");
146      let ext = extURI.QueryInterface(AM_Ci.nsIFileURL).file;
147      setExtensionModifiedTime(ext, updateDate);
148
149      // The pending add-on cannot be disabled or enabled.
150      do_check_false(hasFlag(pendingAddons[0].permissions, AddonManager.PERM_CAN_ENABLE));
151      do_check_false(hasFlag(pendingAddons[0].permissions, AddonManager.PERM_CAN_DISABLE));
152
153      await promiseRestartManager();
154
155      AddonManager.getAllInstalls(function(activeInstalls) {
156        do_check_eq(activeInstalls, 0);
157
158        AddonManager.getAddonByID("[email protected]", callback_soon(function(a1) {
159          do_check_neq(a1, null);
160          do_check_neq(a1.syncGUID, null);
161          do_check_true(a1.syncGUID.length >= 9);
162          do_check_eq(a1.syncGUID, installSyncGUID);
163          do_check_eq(a1.type, "extension");
164          do_check_eq(a1.version, "1.0");
165          do_check_eq(a1.name, "Test 1");
166          do_check_true(isExtensionInAddonsList(profileDir, a1.id));
167          do_check_true(do_get_addon("test_install1").exists());
168          do_check_in_crash_annotation(a1.id, a1.version);
169          do_check_eq(a1.size, ADDON1_SIZE);
170          do_check_false(a1.foreignInstall);
171
172          do_check_eq(a1.sourceURI.spec,
173                      Services.io.newFileURI(do_get_addon("test_install1")).spec);
174          let difference = a1.installDate.getTime() - updateDate;
175          if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
176            do_throw("Add-on install time was out by " + difference + "ms");
177
178          difference = a1.updateDate.getTime() - updateDate;
179          if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
180            do_throw("Add-on update time was out by " + difference + "ms");
181
182          do_check_true(a1.hasResource("install.rdf"));
183          do_check_false(a1.hasResource("foo.bar"));
184
185          let uri2 = do_get_addon_root_uri(profileDir, "[email protected]");
186          do_check_eq(a1.getResourceURI("install.rdf").spec, uri2 + "install.rdf");
187          do_check_eq(a1.iconURL, uri2 + "icon.png");
188          do_check_eq(a1.icon64URL, uri2 + "icon64.png");
189
190          // Ensure that extension bundle (or icon if unpacked) has updated
191          // lastModifiedDate.
192          let testURI = a1.getResourceURI(TEST_UNPACKED ? "icon.png" : "");
193          let testFile = testURI.QueryInterface(Components.interfaces.nsIFileURL).file;
194          do_check_true(testFile.exists());
195          difference = testFile.lastModifiedTime - Date.now();
196          do_check_true(Math.abs(difference) < MAX_TIME_DIFFERENCE);
197
198          a1.uninstall();
199          let { id, version } = a1;
200          restartManager();
201          do_check_not_in_crash_annotation(id, version);
202
203          do_execute_soon(run_test_2);
204        }));
205      });
206    }));
207  });
208}
209
210// Tests that an install from a url downloads.
211function run_test_2() {
212  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
213  AddonManager.getInstallForURL(url, function(install) {
214    do_check_neq(install, null);
215    do_check_eq(install.version, "1.0");
216    do_check_eq(install.name, "Test 2");
217    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
218    do_check_eq(install.iconURL, null);
219    do_check_eq(install.sourceURI.spec, url);
220
221    AddonManager.getAllInstalls(function(activeInstalls) {
222      do_check_eq(activeInstalls.length, 1);
223      do_check_eq(activeInstalls[0], install);
224
225      prepare_test({}, [
226        "onDownloadStarted",
227        "onDownloadEnded",
228      ], check_test_2);
229
230      install.addListener({
231        onDownloadProgress() {
232          do_execute_soon(function() {
233            Components.utils.forceGC();
234          });
235        }
236      });
237
238      install.install();
239    });
240  }, "application/x-xpinstall", null, "Test 2", null, "1.0");
241}
242
243function check_test_2(install) {
244  ensure_test_completed();
245  do_check_eq(install.version, "2.0");
246  do_check_eq(install.name, "Real Test 2");
247  do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
248  do_check_eq(install.addon.install, install);
249  do_check_true(hasFlag(install.addon.operationsRequiringRestart,
250                        AddonManager.OP_NEEDS_RESTART_INSTALL));
251  do_check_eq(install.iconURL, null);
252
253  // Pause the install here and start it again in run_test_3
254  do_execute_soon(function() { run_test_3(install); });
255  return false;
256}
257
258// Tests that the downloaded XPI installs ok
259function run_test_3(install) {
260  prepare_test({
261    "[email protected]": [
262      "onInstalling"
263    ]
264  }, [
265    "onInstallStarted",
266    "onInstallEnded",
267  ], check_test_3);
268  install.install();
269}
270
271function check_test_3(aInstall) {
272  // Make the pending install have a sensible date
273  let updateDate = Date.now();
274  let extURI = aInstall.addon.getResourceURI("");
275  let ext = extURI.QueryInterface(AM_Ci.nsIFileURL).file;
276  setExtensionModifiedTime(ext, updateDate);
277
278  ensure_test_completed();
279  AddonManager.getAddonByID("[email protected]", callback_soon(async function(olda2) {
280    do_check_eq(olda2, null);
281    await promiseRestartManager();
282
283    AddonManager.getAllInstalls(function(installs) {
284      do_check_eq(installs, 0);
285
286      AddonManager.getAddonByID("[email protected]", function(a2) {
287        do_check_neq(a2, null);
288        do_check_neq(a2.syncGUID, null);
289        do_check_eq(a2.type, "extension");
290        do_check_eq(a2.version, "2.0");
291        do_check_eq(a2.name, "Real Test 2");
292        do_check_true(isExtensionInAddonsList(profileDir, a2.id));
293        do_check_true(do_get_addon("test_install2_1").exists());
294        do_check_in_crash_annotation(a2.id, a2.version);
295        do_check_eq(a2.sourceURI.spec,
296                    "http://localhost:" + gPort + "/addons/test_install2_1.xpi");
297
298        let difference = a2.installDate.getTime() - updateDate;
299        if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
300          do_throw("Add-on install time was out by " + difference + "ms");
301
302        difference = a2.updateDate.getTime() - updateDate;
303        if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
304          do_throw("Add-on update time was out by " + difference + "ms");
305
306        gInstallDate = a2.installDate.getTime();
307
308        run_test_4();
309      });
310    });
311  }));
312}
313
314// Tests that installing a new version of an existing add-on works
315function run_test_4() {
316  prepare_test({ }, [
317    "onNewInstall"
318  ]);
319
320  let url = "http://localhost:" + gPort + "/addons/test_install2_2.xpi";
321  AddonManager.getInstallForURL(url, function(install) {
322    ensure_test_completed();
323
324    do_check_neq(install, null);
325    do_check_eq(install.version, "3.0");
326    do_check_eq(install.name, "Test 3");
327    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
328
329    AddonManager.getAllInstalls(function(activeInstalls) {
330      do_check_eq(activeInstalls.length, 1);
331      do_check_eq(activeInstalls[0], install);
332      do_check_eq(install.existingAddon, null);
333
334      prepare_test({}, [
335        "onDownloadStarted",
336        "onDownloadEnded",
337      ], check_test_4);
338      install.install();
339    });
340  }, "application/x-xpinstall", null, "Test 3", null, "3.0");
341}
342
343function check_test_4(install) {
344  ensure_test_completed();
345
346  do_check_eq(install.version, "3.0");
347  do_check_eq(install.name, "Real Test 3");
348  do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
349  do_check_neq(install.existingAddon);
350  do_check_eq(install.existingAddon.id, "[email protected]");
351  do_check_eq(install.addon.install, install);
352  do_check_true(hasFlag(install.addon.operationsRequiringRestart,
353                        AddonManager.OP_NEEDS_RESTART_INSTALL));
354
355  run_test_5();
356  // Installation will continue when there is nothing returned.
357}
358
359// Continue installing the new version
360function run_test_5() {
361  prepare_test({
362    "[email protected]": [
363      "onInstalling"
364    ]
365  }, [
366    "onInstallStarted",
367    "onInstallEnded",
368  ], check_test_5);
369}
370
371function check_test_5(install) {
372  ensure_test_completed();
373
374  do_check_eq(install.existingAddon.pendingUpgrade.install, install);
375
376  AddonManager.getAddonByID("[email protected]", function(olda2) {
377    do_check_neq(olda2, null);
378    do_check_true(hasFlag(olda2.pendingOperations, AddonManager.PENDING_UPGRADE));
379
380    AddonManager.getInstallsByTypes(null, callback_soon(function(installs) {
381      do_check_eq(installs.length, 1);
382      do_check_eq(installs[0].addon, olda2.pendingUpgrade);
383      restartManager();
384
385      AddonManager.getInstallsByTypes(null, function(installs2) {
386        do_check_eq(installs2.length, 0);
387
388        AddonManager.getAddonByID("[email protected]", function(a2) {
389          do_check_neq(a2, null);
390          do_check_eq(a2.type, "extension");
391          do_check_eq(a2.version, "3.0");
392          do_check_eq(a2.name, "Real Test 3");
393          do_check_true(a2.isActive);
394          do_check_true(isExtensionInAddonsList(profileDir, a2.id));
395          do_check_true(do_get_addon("test_install2_2").exists());
396          do_check_in_crash_annotation(a2.id, a2.version);
397          do_check_eq(a2.sourceURI.spec,
398                      "http://localhost:" + gPort + "/addons/test_install2_2.xpi");
399          do_check_false(a2.foreignInstall);
400
401          do_check_eq(a2.installDate.getTime(), gInstallDate);
402          // Update date should be later (or the same if this test is too fast)
403          do_check_true(a2.installDate <= a2.updateDate);
404
405          a2.uninstall();
406          do_execute_soon(run_test_6);
407        });
408      });
409    }));
410  });
411}
412
413// Tests that an install that requires a compatibility update works
414function run_test_6() {
415  restartManager();
416
417  prepare_test({ }, [
418    "onNewInstall"
419  ]);
420
421  let url = "http://localhost:" + gPort + "/addons/test_install3.xpi";
422  AddonManager.getInstallForURL(url, function(install) {
423    ensure_test_completed();
424
425    do_check_neq(install, null);
426    do_check_eq(install.version, "1.0");
427    do_check_eq(install.name, "Real Test 4");
428    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
429
430    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
431      do_check_eq(activeInstalls.length, 1);
432      do_check_eq(activeInstalls[0], install);
433
434      prepare_test({}, [
435        "onDownloadStarted",
436        "onDownloadEnded",
437      ], check_test_6);
438      install.install();
439    });
440  }, "application/x-xpinstall", null, "Real Test 4", null, "1.0");
441}
442
443function check_test_6(install) {
444  ensure_test_completed();
445  do_check_eq(install.version, "1.0");
446  do_check_eq(install.name, "Real Test 4");
447  do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
448  do_check_eq(install.existingAddon, null);
449  do_check_false(install.addon.appDisabled);
450  run_test_7();
451  return true;
452}
453
454// Continue the install
455function run_test_7() {
456  prepare_test({
457    "[email protected]": [
458      "onInstalling"
459    ]
460  }, [
461    "onInstallStarted",
462    "onInstallEnded",
463  ], check_test_7);
464}
465
466function check_test_7() {
467  ensure_test_completed();
468  AddonManager.getAddonByID("[email protected]", callback_soon(async function(olda3) {
469    do_check_eq(olda3, null);
470    await promiseRestartManager();
471
472    AddonManager.getAllInstalls(function(installs) {
473      do_check_eq(installs, 0);
474
475      AddonManager.getAddonByID("[email protected]", function(a3) {
476        do_check_neq(a3, null);
477        do_check_neq(a3.syncGUID, null);
478        do_check_eq(a3.type, "extension");
479        do_check_eq(a3.version, "1.0");
480        do_check_eq(a3.name, "Real Test 4");
481        do_check_true(a3.isActive);
482        do_check_false(a3.appDisabled);
483        do_check_true(isExtensionInAddonsList(profileDir, a3.id));
484        do_check_true(do_get_addon("test_install3").exists());
485        a3.uninstall();
486        do_execute_soon(run_test_8);
487      });
488    });
489  }));
490}
491
492function run_test_8() {
493  restartManager();
494
495  AddonManager.addInstallListener(InstallListener);
496  AddonManager.addAddonListener(AddonListener);
497
498  prepare_test({ }, [
499    "onNewInstall"
500  ]);
501
502  AddonManager.getInstallForFile(do_get_addon("test_install3"), function(install) {
503    do_check_true(install.addon.isCompatible);
504
505    prepare_test({
506      "[email protected]": [
507        "onInstalling"
508      ]
509    }, [
510      "onInstallStarted",
511      "onInstallEnded",
512    ], callback_soon(check_test_8));
513    install.install();
514  });
515}
516
517async function check_test_8() {
518  await promiseRestartManager();
519
520  AddonManager.getAddonByID("[email protected]", function(a3) {
521    do_check_neq(a3, null);
522    do_check_neq(a3.syncGUID, null);
523    do_check_eq(a3.type, "extension");
524    do_check_eq(a3.version, "1.0");
525    do_check_eq(a3.name, "Real Test 4");
526    do_check_true(a3.isActive);
527    do_check_false(a3.appDisabled);
528    do_check_true(isExtensionInAddonsList(profileDir, a3.id));
529    do_check_true(do_get_addon("test_install3").exists());
530    a3.uninstall();
531    do_execute_soon(run_test_9);
532  });
533}
534
535// Test that after cancelling a download it is removed from the active installs
536function run_test_9() {
537  restartManager();
538
539  prepare_test({ }, [
540    "onNewInstall"
541  ]);
542
543  let url = "http://localhost:" + gPort + "/addons/test_install3.xpi";
544  AddonManager.getInstallForURL(url, function(install) {
545    ensure_test_completed();
546
547    do_check_neq(install, null);
548    do_check_eq(install.version, "1.0");
549    do_check_eq(install.name, "Real Test 4");
550    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
551
552    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
553      do_check_eq(activeInstalls.length, 1);
554      do_check_eq(activeInstalls[0], install);
555
556      prepare_test({}, [
557        "onDownloadStarted",
558        "onDownloadEnded",
559      ], check_test_9);
560      install.install();
561    });
562  }, "application/x-xpinstall", null, "Real Test 4", null, "1.0");
563}
564
565function check_test_9(install) {
566  prepare_test({}, [
567    "onDownloadCancelled"
568  ], function() {
569    let file = install.file;
570
571    // Allow the file removal to complete
572    do_execute_soon(function() {
573      AddonManager.getAllInstalls(function(activeInstalls) {
574        do_check_eq(activeInstalls.length, 0);
575        do_check_false(file.exists());
576
577        run_test_10();
578      });
579    });
580  });
581
582  install.cancel();
583}
584
585// Tests that after cancelling a pending install it is removed from the active
586// installs
587function run_test_10() {
588  prepare_test({ }, [
589    "onNewInstall"
590  ]);
591
592  let url = "http://localhost:" + gPort + "/addons/test_install3.xpi";
593  AddonManager.getInstallForURL(url, function(install) {
594    ensure_test_completed();
595
596    do_check_neq(install, null);
597    do_check_eq(install.version, "1.0");
598    do_check_eq(install.name, "Real Test 4");
599    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
600
601    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
602      do_check_eq(activeInstalls.length, 1);
603      do_check_eq(activeInstalls[0], install);
604
605      prepare_test({
606        "[email protected]": [
607          "onInstalling"
608        ]
609      }, [
610        "onDownloadStarted",
611        "onDownloadEnded",
612        "onInstallStarted",
613        "onInstallEnded"
614      ], check_test_10);
615      install.install();
616    });
617  }, "application/x-xpinstall", null, "Real Test 4", null, "1.0");
618}
619
620function check_test_10(install) {
621  prepare_test({
622    "[email protected]": [
623      "onOperationCancelled"
624    ]
625  }, [
626    "onInstallCancelled"
627  ]);
628
629  install.cancel();
630
631  ensure_test_completed();
632
633  AddonManager.getAllInstalls(callback_soon(function(activeInstalls) {
634    do_check_eq(activeInstalls.length, 0);
635
636    restartManager();
637
638    // Check that the install did not complete
639    AddonManager.getAddonByID("[email protected]", function(a3) {
640      do_check_eq(a3, null);
641
642      do_execute_soon(run_test_11);
643    });
644  }));
645}
646
647function run_test_11() {
648  // Tests 11 and 12 were removed, to avoid churn of renumbering,
649  // just jump ahead to 13 here
650  run_test_13();
651}
652
653
654// Tests that cancelling an upgrade leaves the original add-on's pendingOperations
655// correct
656function run_test_13() {
657  restartManager();
658
659  installAllFiles([do_get_addon("test_install2_1")], function() {
660    restartManager();
661
662    prepare_test({ }, [
663      "onNewInstall"
664    ]);
665
666    let url = "http://localhost:" + gPort + "/addons/test_install2_2.xpi";
667    AddonManager.getInstallForURL(url, function(install) {
668      ensure_test_completed();
669
670      do_check_neq(install, null);
671      do_check_eq(install.version, "3.0");
672      do_check_eq(install.name, "Test 3");
673      do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
674
675      AddonManager.getAllInstalls(function(activeInstalls) {
676        do_check_eq(activeInstalls.length, 1);
677        do_check_eq(activeInstalls[0], install);
678        do_check_eq(install.existingAddon, null);
679
680        prepare_test({
681          "[email protected]": [
682            "onInstalling"
683          ]
684        }, [
685          "onDownloadStarted",
686          "onDownloadEnded",
687          "onInstallStarted",
688          "onInstallEnded",
689        ], check_test_13);
690        install.install();
691      });
692    }, "application/x-xpinstall", null, "Test 3", null, "3.0");
693  });
694}
695
696function check_test_13(install) {
697  ensure_test_completed();
698
699  do_check_eq(install.version, "3.0");
700  do_check_eq(install.name, "Real Test 3");
701  do_check_eq(install.state, AddonManager.STATE_INSTALLED);
702  do_check_neq(install.existingAddon, null);
703  do_check_eq(install.existingAddon.id, "[email protected]");
704  do_check_eq(install.addon.install, install);
705
706  AddonManager.getAddonByID("[email protected]", callback_soon(function(olda2) {
707    do_check_neq(olda2, null);
708    do_check_true(hasFlag(olda2.pendingOperations, AddonManager.PENDING_UPGRADE));
709    do_check_eq(olda2.pendingUpgrade, install.addon);
710
711    do_check_true(hasFlag(install.addon.pendingOperations,
712                          AddonManager.PENDING_INSTALL));
713
714    prepare_test({
715      "[email protected]": [
716        "onOperationCancelled"
717      ]
718    }, [
719      "onInstallCancelled",
720    ]);
721
722    install.cancel();
723
724    do_check_false(hasFlag(install.addon.pendingOperations, AddonManager.PENDING_INSTALL));
725
726    do_check_false(hasFlag(olda2.pendingOperations, AddonManager.PENDING_UPGRADE));
727    do_check_eq(olda2.pendingUpgrade, null);
728
729    restartManager();
730
731    // Check that the upgrade did not complete
732    AddonManager.getAddonByID("[email protected]", function(a2) {
733      do_check_eq(a2.version, "2.0");
734
735      a2.uninstall();
736
737      do_execute_soon(run_test_14);
738    });
739  }));
740}
741
742// Check that cancelling the install from onDownloadStarted actually cancels it
743function run_test_14() {
744  restartManager();
745
746  prepare_test({ }, [
747    "onNewInstall"
748  ]);
749
750  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
751  AddonManager.getInstallForURL(url, function(install) {
752    ensure_test_completed();
753
754    do_check_eq(install.file, null);
755
756    prepare_test({ }, [
757      "onDownloadStarted"
758    ], check_test_14);
759    install.install();
760  }, "application/x-xpinstall");
761}
762
763function check_test_14(install) {
764  prepare_test({ }, [
765    "onDownloadCancelled"
766  ], function() {
767    let file = install.file;
768
769    install.addListener({
770      onDownloadProgress() {
771        do_throw("Download should not have continued");
772      },
773      onDownloadEnded() {
774        do_throw("Download should not have continued");
775      }
776    });
777
778    // Allow the listener to return to see if it continues downloading. The
779    // The listener only really tests if we give it time to see progress, the
780    // file check isn't ideal either
781    do_execute_soon(function() {
782      do_check_false(file.exists());
783
784      run_test_15();
785    });
786  });
787
788  // Wait for the channel to be ready to cancel
789  do_execute_soon(function() {
790    install.cancel();
791  });
792}
793
794// Checks that cancelling the install from onDownloadEnded actually cancels it
795function run_test_15() {
796  prepare_test({ }, [
797    "onNewInstall"
798  ]);
799
800  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
801  AddonManager.getInstallForURL(url, function(install) {
802    ensure_test_completed();
803
804    do_check_eq(install.file, null);
805
806    prepare_test({ }, [
807      "onDownloadStarted",
808      "onDownloadEnded"
809    ], check_test_15);
810    install.install();
811  }, "application/x-xpinstall");
812}
813
814function check_test_15(install) {
815  prepare_test({ }, [
816    "onDownloadCancelled"
817  ]);
818
819  install.cancel();
820
821  ensure_test_completed();
822
823  install.addListener({
824    onInstallStarted() {
825      do_throw("Install should not have continued");
826    }
827  });
828
829  // Allow the listener to return to see if it starts installing
830  do_execute_soon(run_test_16);
831}
832
833// Verify that the userDisabled value carries over to the upgrade by default
834function run_test_16() {
835  restartManager();
836
837  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
838  AddonManager.getInstallForURL(url, function(aInstall) {
839    aInstall.addListener({
840      onInstallStarted() {
841        do_check_false(aInstall.addon.userDisabled);
842        aInstall.addon.userDisabled = true;
843      },
844
845      onInstallEnded() {
846       do_execute_soon(function install2_1_ended() {
847        restartManager();
848
849        AddonManager.getAddonByID("[email protected]", function(a2) {
850          do_check_true(a2.userDisabled);
851          do_check_false(a2.isActive);
852
853          let url_2 = "http://localhost:" + gPort + "/addons/test_install2_2.xpi";
854          AddonManager.getInstallForURL(url_2, function(aInstall_2) {
855            aInstall_2.addListener({
856              onInstallEnded() {
857               do_execute_soon(function install2_2_ended() {
858                do_check_true(aInstall_2.addon.userDisabled);
859
860                restartManager();
861
862                AddonManager.getAddonByID("[email protected]", function(a2_2) {
863                  do_check_true(a2_2.userDisabled);
864                  do_check_false(a2_2.isActive);
865
866                  a2_2.uninstall();
867                  do_execute_soon(run_test_17);
868                });
869               });
870              }
871            });
872            aInstall_2.install();
873          }, "application/x-xpinstall");
874        });
875       });
876      }
877    });
878    aInstall.install();
879  }, "application/x-xpinstall");
880}
881
882// Verify that changing the userDisabled value before onInstallEnded works
883function run_test_17() {
884  restartManager();
885
886  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
887  AddonManager.getInstallForURL(url, function(aInstall) {
888    aInstall.addListener({
889      onInstallEnded() {
890       do_execute_soon(function install2_1_ended2() {
891        do_check_false(aInstall.addon.userDisabled);
892
893        restartManager();
894
895        AddonManager.getAddonByID("[email protected]", function(a2) {
896          do_check_false(a2.userDisabled);
897          do_check_true(a2.isActive);
898
899          let url_2 = "http://localhost:" + gPort + "/addons/test_install2_2.xpi";
900          AddonManager.getInstallForURL(url_2, function(aInstall_2) {
901            aInstall_2.addListener({
902              onInstallStarted() {
903                do_check_false(aInstall_2.addon.userDisabled);
904                aInstall_2.addon.userDisabled = true;
905              },
906
907              onInstallEnded() {
908               do_execute_soon(function install2_2_ended2() {
909                restartManager();
910
911                AddonManager.getAddonByID("[email protected]", function(a2_2) {
912                  do_check_true(a2_2.userDisabled);
913                  do_check_false(a2_2.isActive);
914
915                  a2_2.uninstall();
916                  do_execute_soon(run_test_18);
917                });
918               });
919              }
920            });
921            aInstall_2.install();
922          }, "application/x-xpinstall");
923        });
924       });
925      }
926    });
927    aInstall.install();
928  }, "application/x-xpinstall");
929}
930
931// Verify that changing the userDisabled value before onInstallEnded works
932function run_test_18() {
933  restartManager();
934
935  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
936  AddonManager.getInstallForURL(url, function(aInstall) {
937    aInstall.addListener({
938      onInstallStarted() {
939        do_check_false(aInstall.addon.userDisabled);
940        aInstall.addon.userDisabled = true;
941      },
942
943      onInstallEnded() {
944       do_execute_soon(function install_2_1_ended3() {
945        restartManager();
946
947        AddonManager.getAddonByID("[email protected]", function(a2) {
948          do_check_true(a2.userDisabled);
949          do_check_false(a2.isActive);
950
951          let url_2 = "http://localhost:" + gPort + "/addons/test_install2_2.xpi";
952          AddonManager.getInstallForURL(url_2, function(aInstall_2) {
953            aInstall_2.addListener({
954              onInstallStarted() {
955                do_check_true(aInstall_2.addon.userDisabled);
956                aInstall_2.addon.userDisabled = false;
957              },
958
959              onInstallEnded() {
960               do_execute_soon(function install_2_2_ended3() {
961                restartManager();
962
963                AddonManager.getAddonByID("[email protected]", function(a2_2) {
964                  do_check_false(a2_2.userDisabled);
965                  do_check_true(a2_2.isActive);
966
967                  a2_2.uninstall();
968                  do_execute_soon(run_test_18_1);
969                });
970               });
971              }
972            });
973            aInstall_2.install();
974          }, "application/x-xpinstall");
975        });
976       });
977      }
978    });
979    aInstall.install();
980  }, "application/x-xpinstall");
981}
982
983
984// Checks that metadata is not stored if the pref is set to false
985function run_test_18_1() {
986  restartManager();
987
988  Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", true);
989  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS,
990                             "http://localhost:" + gPort + "/data/test_install.xml");
991
992  Services.prefs.setBoolPref("[email protected]", false);
993
994  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
995  AddonManager.getInstallForURL(url, function(aInstall) {
996    aInstall.addListener({
997      onInstallEnded(unused, aAddon) {
998       do_execute_soon(function test18_1_install_ended() {
999        do_check_neq(aAddon.fullDescription, "Repository description");
1000
1001        restartManager();
1002
1003        AddonManager.getAddonByID("[email protected]", function(a2) {
1004          do_check_neq(a2.fullDescription, "Repository description");
1005
1006          a2.uninstall();
1007          do_execute_soon(run_test_19);
1008        });
1009       });
1010      }
1011    });
1012    aInstall.install();
1013  }, "application/x-xpinstall");
1014}
1015
1016// Checks that metadata is downloaded for new installs and is visible before and
1017// after restart
1018function run_test_19() {
1019  restartManager();
1020  Services.prefs.setBoolPref("[email protected]", true);
1021
1022  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
1023  AddonManager.getInstallForURL(url, function(aInstall) {
1024    aInstall.addListener({
1025      onInstallEnded(unused, aAddon) {
1026       do_execute_soon(function test19_install_ended() {
1027        do_check_eq(aAddon.fullDescription, "Repository description");
1028
1029        restartManager();
1030
1031        AddonManager.getAddonByID("[email protected]", function(a2) {
1032          do_check_eq(a2.fullDescription, "Repository description");
1033
1034          a2.uninstall();
1035          do_execute_soon(run_test_20);
1036        });
1037       });
1038      }
1039    });
1040    aInstall.install();
1041  }, "application/x-xpinstall");
1042}
1043
1044// Do the same again to make sure it works when the data is already in the cache
1045function run_test_20() {
1046  restartManager();
1047
1048  let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
1049  AddonManager.getInstallForURL(url, function(aInstall) {
1050    aInstall.addListener({
1051      onInstallEnded(unused, aAddon) {
1052       do_execute_soon(function test20_install_ended() {
1053        do_check_eq(aAddon.fullDescription, "Repository description");
1054
1055        restartManager();
1056
1057        AddonManager.getAddonByID("[email protected]", function(a2) {
1058          do_check_eq(a2.fullDescription, "Repository description");
1059
1060          a2.uninstall();
1061          do_execute_soon(run_test_21);
1062        });
1063       });
1064      }
1065    });
1066    aInstall.install();
1067  }, "application/x-xpinstall");
1068}
1069
1070// Verify that installing an add-on that is already pending install cancels the
1071// first install
1072function run_test_21() {
1073  restartManager();
1074  Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", false);
1075
1076  installAllFiles([do_get_addon("test_install2_1")], function() {
1077    AddonManager.getAllInstalls(function(aInstalls) {
1078      do_check_eq(aInstalls.length, 1);
1079
1080      prepare_test({
1081        "[email protected]": [
1082          "onOperationCancelled",
1083          "onInstalling"
1084        ]
1085      }, [
1086        "onNewInstall",
1087        "onDownloadStarted",
1088        "onDownloadEnded",
1089        "onInstallStarted",
1090        "onInstallCancelled",
1091        "onInstallEnded",
1092      ], check_test_21);
1093
1094      let url = "http://localhost:" + gPort + "/addons/test_install2_1.xpi";
1095      AddonManager.getInstallForURL(url, function(aInstall) {
1096        aInstall.install();
1097      }, "application/x-xpinstall");
1098    });
1099  });
1100}
1101
1102function check_test_21(aInstall) {
1103  AddonManager.getAllInstalls(callback_soon(function(aInstalls) {
1104    do_check_eq(aInstalls.length, 1);
1105    do_check_eq(aInstalls[0], aInstall);
1106
1107    prepare_test({
1108      "[email protected]": [
1109        "onOperationCancelled"
1110      ]
1111    }, [
1112      "onInstallCancelled",
1113    ]);
1114
1115    aInstall.cancel();
1116
1117    ensure_test_completed();
1118
1119    restartManager();
1120
1121    AddonManager.getAddonByID("[email protected]", function(a2) {
1122      do_check_eq(a2, null);
1123
1124      run_test_22();
1125    });
1126  }));
1127}
1128
1129// Tests that an install can be restarted after being cancelled
1130function run_test_22() {
1131  prepare_test({ }, [
1132    "onNewInstall"
1133  ]);
1134
1135  let url = "http://localhost:" + gPort + "/addons/test_install3.xpi";
1136  AddonManager.getInstallForURL(url, function(aInstall) {
1137    ensure_test_completed();
1138
1139    do_check_neq(aInstall, null);
1140    do_check_eq(aInstall.state, AddonManager.STATE_AVAILABLE);
1141
1142    prepare_test({}, [
1143      "onDownloadStarted",
1144      "onDownloadEnded",
1145    ], check_test_22);
1146    aInstall.install();
1147  }, "application/x-xpinstall");
1148}
1149
1150function check_test_22(aInstall) {
1151  prepare_test({}, [
1152    "onDownloadCancelled"
1153  ]);
1154
1155  aInstall.cancel();
1156
1157  ensure_test_completed();
1158
1159  prepare_test({
1160    "[email protected]": [
1161      "onInstalling"
1162    ]
1163  }, [
1164    "onDownloadStarted",
1165    "onDownloadEnded",
1166    "onInstallStarted",
1167    "onInstallEnded"
1168  ], finish_test_22);
1169
1170  aInstall.install();
1171}
1172
1173function finish_test_22(aInstall) {
1174  prepare_test({
1175    "[email protected]": [
1176      "onOperationCancelled"
1177    ]
1178  }, [
1179    "onInstallCancelled"
1180  ]);
1181
1182  aInstall.cancel();
1183
1184  ensure_test_completed();
1185
1186  run_test_23();
1187}
1188
1189// Tests that an install can be restarted after being cancelled when a hash
1190// was provided
1191function run_test_23() {
1192  prepare_test({ }, [
1193    "onNewInstall"
1194  ]);
1195
1196  let url = "http://localhost:" + gPort + "/addons/test_install3.xpi";
1197  AddonManager.getInstallForURL(url, function(aInstall) {
1198    ensure_test_completed();
1199
1200    do_check_neq(aInstall, null);
1201    do_check_eq(aInstall.state, AddonManager.STATE_AVAILABLE);
1202
1203    prepare_test({}, [
1204      "onDownloadStarted",
1205      "onDownloadEnded",
1206    ], check_test_23);
1207    aInstall.install();
1208  }, "application/x-xpinstall", do_get_addon_hash("test_install3"));
1209}
1210
1211function check_test_23(aInstall) {
1212  prepare_test({}, [
1213    "onDownloadCancelled"
1214  ]);
1215
1216  aInstall.cancel();
1217
1218  ensure_test_completed();
1219
1220  prepare_test({
1221    "[email protected]": [
1222      "onInstalling"
1223    ]
1224  }, [
1225    "onDownloadStarted",
1226    "onDownloadEnded",
1227    "onInstallStarted",
1228    "onInstallEnded"
1229  ], finish_test_23);
1230
1231  aInstall.install();
1232}
1233
1234function finish_test_23(aInstall) {
1235  prepare_test({
1236    "[email protected]": [
1237      "onOperationCancelled"
1238    ]
1239  }, [
1240    "onInstallCancelled"
1241  ]);
1242
1243  aInstall.cancel();
1244
1245  ensure_test_completed();
1246
1247  run_test_24();
1248}
1249
1250// Tests that an install with a bad hash can be restarted after it fails, though
1251// it will only fail again
1252function run_test_24() {
1253  prepare_test({ }, [
1254    "onNewInstall"
1255  ]);
1256
1257  let url = "http://localhost:" + gPort + "/addons/test_install3.xpi";
1258  AddonManager.getInstallForURL(url, function(aInstall) {
1259    ensure_test_completed();
1260
1261    do_check_neq(aInstall, null);
1262    do_check_eq(aInstall.state, AddonManager.STATE_AVAILABLE);
1263
1264    prepare_test({}, [
1265      "onDownloadStarted",
1266      "onDownloadFailed",
1267    ], check_test_24);
1268    aInstall.install();
1269  }, "application/x-xpinstall", "sha1:foo");
1270}
1271
1272function check_test_24(aInstall) {
1273  prepare_test({ }, [
1274    "onDownloadStarted",
1275    "onDownloadFailed"
1276  ], run_test_25);
1277
1278  aInstall.install();
1279}
1280
1281// Tests that installs with a hash for a local file work
1282function run_test_25() {
1283  prepare_test({ }, [
1284    "onNewInstall"
1285  ]);
1286
1287  let url = Services.io.newFileURI(do_get_addon("test_install3")).spec;
1288  AddonManager.getInstallForURL(url, function(aInstall) {
1289    ensure_test_completed();
1290
1291    do_check_neq(aInstall, null);
1292    do_check_eq(aInstall.state, AddonManager.STATE_DOWNLOADED);
1293    do_check_eq(aInstall.error, 0);
1294
1295    prepare_test({ }, [
1296      "onDownloadCancelled"
1297    ]);
1298
1299    aInstall.cancel();
1300
1301    ensure_test_completed();
1302
1303    run_test_26();
1304  }, "application/x-xpinstall", do_get_addon_hash("test_install3"));
1305}
1306
1307function run_test_26() {
1308  prepare_test({ }, [
1309    "onNewInstall",
1310    "onDownloadStarted",
1311    "onDownloadCancelled"
1312  ]);
1313
1314  let observerService = AM_Cc["@mozilla.org/network/http-activity-distributor;1"].
1315                        getService(AM_Ci.nsIHttpActivityDistributor);
1316  observerService.addObserver({
1317    observeActivity(aChannel, aType, aSubtype, aTimestamp, aSizeData,
1318                              aStringData) {
1319      aChannel.QueryInterface(AM_Ci.nsIChannel);
1320      // Wait for the final event for the redirected URL
1321      if (aChannel.URI.spec != "http://localhost:" + gPort + "/addons/test_install1.xpi" ||
1322          aType != AM_Ci.nsIHttpActivityObserver.ACTIVITY_TYPE_HTTP_TRANSACTION ||
1323          aSubtype != AM_Ci.nsIHttpActivityObserver.ACTIVITY_SUBTYPE_TRANSACTION_CLOSE)
1324        return;
1325
1326      // Request should have been cancelled
1327      do_check_eq(aChannel.status, Components.results.NS_BINDING_ABORTED);
1328
1329      observerService.removeObserver(this);
1330
1331      run_test_27();
1332    }
1333  });
1334
1335  let url = "http://localhost:" + gPort + "/redirect?/addons/test_install1.xpi";
1336  AddonManager.getInstallForURL(url, function(aInstall) {
1337    aInstall.addListener({
1338      onDownloadProgress(aDownloadProgressInstall) {
1339        aDownloadProgressInstall.cancel();
1340      }
1341    });
1342
1343    aInstall.install();
1344  }, "application/x-xpinstall");
1345}
1346
1347
1348// Tests that an install can be restarted during onDownloadCancelled after being
1349// cancelled in mid-download
1350function run_test_27() {
1351  prepare_test({ }, [
1352    "onNewInstall"
1353  ]);
1354
1355  let url = "http://localhost:" + gPort + "/addons/test_install3.xpi";
1356  AddonManager.getInstallForURL(url, function(aInstall) {
1357    ensure_test_completed();
1358
1359    do_check_neq(aInstall, null);
1360    do_check_eq(aInstall.state, AddonManager.STATE_AVAILABLE);
1361
1362    aInstall.addListener({
1363      onDownloadProgress() {
1364        aInstall.removeListener(this);
1365        aInstall.cancel();
1366      }
1367    });
1368
1369    prepare_test({}, [
1370      "onDownloadStarted",
1371      "onDownloadCancelled",
1372    ], check_test_27);
1373    aInstall.install();
1374  }, "application/x-xpinstall");
1375}
1376
1377function check_test_27(aInstall) {
1378  prepare_test({
1379    "[email protected]": [
1380      "onInstalling"
1381    ]
1382  }, [
1383    "onDownloadStarted",
1384    "onDownloadEnded",
1385    "onInstallStarted",
1386    "onInstallEnded"
1387  ], finish_test_27);
1388
1389  let file = aInstall.file;
1390  aInstall.install();
1391  do_check_neq(file.path, aInstall.file.path);
1392  do_check_false(file.exists());
1393}
1394
1395function finish_test_27(aInstall) {
1396  prepare_test({
1397    "[email protected]": [
1398      "onOperationCancelled"
1399    ]
1400  }, [
1401    "onInstallCancelled"
1402  ]);
1403
1404  aInstall.cancel();
1405
1406  ensure_test_completed();
1407
1408  run_test_28();
1409}
1410
1411// Tests that an install that isn't strictly compatible and has
1412// binary components correctly has appDisabled set (see bug 702868).
1413function run_test_28() {
1414  prepare_test({ }, [
1415    "onNewInstall"
1416  ]);
1417
1418  let url = "http://localhost:" + gPort + "/addons/test_install5.xpi";
1419  AddonManager.getInstallForURL(url, function(install) {
1420    ensure_test_completed();
1421
1422    do_check_neq(install, null);
1423    do_check_eq(install.version, "1.0");
1424    do_check_eq(install.name, "Real Test 5");
1425    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
1426
1427    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
1428      do_check_eq(activeInstalls.length, 1);
1429      do_check_eq(activeInstalls[0], install);
1430
1431      prepare_test({}, [
1432        "onDownloadStarted",
1433        "onDownloadEnded",
1434        "onInstallStarted"
1435      ], check_test_28);
1436      install.install();
1437    });
1438  }, "application/x-xpinstall", null, "Real Test 5", null, "1.0");
1439}
1440
1441function check_test_28(install) {
1442  ensure_test_completed();
1443  do_check_eq(install.version, "1.0");
1444  do_check_eq(install.name, "Real Test 5");
1445  do_check_eq(install.state, AddonManager.STATE_INSTALLING);
1446  do_check_eq(install.existingAddon, null);
1447  do_check_false(install.addon.isCompatible);
1448  do_check_true(install.addon.appDisabled);
1449
1450  prepare_test({}, [
1451    "onInstallCancelled"
1452  ], finish_test_28);
1453  return false;
1454}
1455
1456function finish_test_28(install) {
1457  prepare_test({}, [
1458    "onDownloadCancelled"
1459  ], run_test_29);
1460
1461  install.cancel();
1462}
1463
1464// Tests that an install with a matching compatibility override has appDisabled
1465// set correctly.
1466function run_test_29() {
1467  Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", true);
1468
1469  prepare_test({ }, [
1470    "onNewInstall"
1471  ]);
1472
1473  let url = "http://localhost:" + gPort + "/addons/test_install6.xpi";
1474  AddonManager.getInstallForURL(url, function(install) {
1475    ensure_test_completed();
1476
1477    do_check_neq(install, null);
1478    do_check_eq(install.version, "1.0");
1479    do_check_eq(install.name, "Addon Test 6");
1480    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
1481
1482    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
1483      do_check_eq(activeInstalls.length, 1);
1484      do_check_eq(activeInstalls[0], install);
1485
1486      prepare_test({}, [
1487        "onDownloadStarted",
1488        "onDownloadEnded"
1489      ], check_test_29);
1490      install.install();
1491    });
1492  }, "application/x-xpinstall", null, "Addon Test 6", null, "1.0");
1493}
1494
1495function check_test_29(install) {
1496  // ensure_test_completed();
1497  do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
1498  do_check_neq(install.addon, null);
1499  do_check_false(install.addon.isCompatible);
1500  do_check_true(install.addon.appDisabled);
1501
1502  prepare_test({}, [
1503    "onDownloadCancelled"
1504  ], run_test_30);
1505  install.cancel();
1506  return false;
1507}
1508
1509// Tests that a multi-package XPI with no add-ons inside shows up as a
1510// corrupt file
1511function run_test_30() {
1512  prepare_test({ }, [
1513    "onNewInstall"
1514  ]);
1515
1516  AddonManager.getInstallForFile(do_get_addon("test_install7"), function(install) {
1517    ensure_test_completed();
1518
1519    do_check_neq(install, null);
1520    do_check_eq(install.state, AddonManager.STATE_DOWNLOAD_FAILED);
1521    do_check_eq(install.error, AddonManager.ERROR_CORRUPT_FILE);
1522
1523    run_test_31();
1524  });
1525}
1526
1527// Tests that a multi-package XPI with no valid add-ons inside shows up as a
1528// corrupt file
1529function run_test_31() {
1530  prepare_test({ }, [
1531    "onNewInstall"
1532  ]);
1533
1534  AddonManager.getInstallForFile(do_get_addon("test_install8"), function(install) {
1535    ensure_test_completed();
1536
1537    do_check_neq(install, null);
1538    do_check_eq(install.state, AddonManager.STATE_DOWNLOAD_FAILED);
1539    do_check_eq(install.error, AddonManager.ERROR_CORRUPT_FILE);
1540
1541    end_test();
1542  });
1543}
1544
Full Screen

test_install_strictcompat.js

Source: test_install_strictcompat.js Github

copy
1/* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
4
5// This verifies that add-ons can be installed from XPI files
6var Cc = Components.classes;
7var Ci = Components.interfaces;
8var Cu = Components.utils;
9
10// install.rdf size, icon.png, icon64.png size
11const ADDON1_SIZE = 705 + 16 + 16;
12
13Cu.import("resource://gre/modules/Services.jsm");
14Cu.import("resource://gre/modules/NetUtil.jsm");
15Cu.import("resource://testing-common/httpd.js");
16
17var testserver;
18var gInstallDate;
19
20// The test extension uses an insecure update url.
21Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false);
22Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, true);
23
24
25const profileDir = gProfD.clone();
26profileDir.append("extensions");
27
28function run_test() {
29  createAppInfo("[email protected]", "XPCShell", "1", "1.9.2");
30
31  startupManager();
32  // Make sure we only register once despite multiple calls
33  AddonManager.addInstallListener(InstallListener);
34  AddonManager.addAddonListener(AddonListener);
35  AddonManager.addInstallListener(InstallListener);
36  AddonManager.addAddonListener(AddonListener);
37
38  // Create and configure the HTTP server.
39  testserver = new HttpServer();
40  testserver.registerDirectory("/addons/", do_get_file("addons"));
41  testserver.registerDirectory("/data/", do_get_file("data"));
42  testserver.registerPathHandler("/redirect", function(aRequest, aResponse) {
43    aResponse.setStatusLine(null, 301, "Moved Permanently");
44    let url = aRequest.host + ":" + aRequest.port + aRequest.queryString;
45    aResponse.setHeader("Location", "http://" + url);
46  });
47  testserver.start(4444);
48
49  do_test_pending();
50  run_test_1();
51}
52
53function end_test() {
54  testserver.stop(do_test_finished);
55}
56
57// Checks that an install from a local file proceeds as expected
58function run_test_1() {
59  prepare_test({ }, [
60    "onNewInstall"
61  ]);
62
63  AddonManager.getInstallForFile(do_get_addon("test_install1"), function(install) {
64    ensure_test_completed();
65
66    do_check_neq(install, null);
67    do_check_eq(install.type, "extension");
68    do_check_eq(install.version, "1.0");
69    do_check_eq(install.name, "Test 1");
70    do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
71    do_check_true(install.addon.hasResource("install.rdf"));
72    do_check_eq(install.addon.install, install);
73    do_check_eq(install.addon.size, ADDON1_SIZE);
74    do_check_true(hasFlag(install.addon.operationsRequiringRestart,
75                          AddonManager.OP_NEEDS_RESTART_INSTALL));
76    let file = do_get_addon("test_install1");
77    let uri = Services.io.newFileURI(file).spec;
78    do_check_eq(install.addon.getResourceURI("install.rdf").spec, "jar:" + uri + "!/install.rdf");
79    do_check_eq(install.addon.iconURL, "jar:" + uri + "!/icon.png");
80    do_check_eq(install.addon.icon64URL, "jar:" + uri + "!/icon64.png");
81    do_check_eq(install.iconURL, null);
82
83    do_check_eq(install.sourceURI.spec, uri);
84    do_check_eq(install.addon.sourceURI.spec, uri);
85
86    AddonManager.getAllInstalls(function(activeInstalls) {
87      do_check_eq(activeInstalls.length, 1);
88      do_check_eq(activeInstalls[0], install);
89
90      AddonManager.getInstallsByTypes(["foo"], function(fooInstalls) {
91        do_check_eq(fooInstalls.length, 0);
92
93        AddonManager.getInstallsByTypes(["extension"], function(extensionInstalls) {
94          do_check_eq(extensionInstalls.length, 1);
95          do_check_eq(extensionInstalls[0], install);
96
97          prepare_test({
98            "[email protected]": [
99              "onInstalling"
100            ]
101          }, [
102            "onInstallStarted",
103            "onInstallEnded",
104          ], check_test_1);
105          install.install();
106        });
107      });
108    });
109  });
110}
111
112function check_test_1() {
113  ensure_test_completed();
114  AddonManager.getAddonByID("[email protected]", function(olda1) {
115    do_check_eq(olda1, null);
116
117    AddonManager.getAddonsWithOperationsByTypes(null, callback_soon(async function(pendingAddons) {
118      do_check_eq(pendingAddons.length, 1);
119      do_check_eq(pendingAddons[0].id, "[email protected]");
120      let uri = NetUtil.newURI(pendingAddons[0].iconURL);
121      if (uri instanceof AM_Ci.nsIJARURI) {
122        let jarURI = uri.QueryInterface(AM_Ci.nsIJARURI);
123        let archiveURI = jarURI.JARFile;
124        let archiveFile = archiveURI.QueryInterface(AM_Ci.nsIFileURL).file;
125        let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].
126                        createInstance(Ci.nsIZipReader);
127        try {
128          zipReader.open(archiveFile);
129          do_check_true(zipReader.hasEntry(jarURI.JAREntry));
130        } finally {
131          zipReader.close();
132        }
133      } else {
134        let iconFile = uri.QueryInterface(AM_Ci.nsIFileURL).file;
135        do_check_true(iconFile.exists());
136      }
137
138      // Make the pending install have a sensible date
139      let updateDate = Date.now();
140      let extURI = pendingAddons[0].getResourceURI("");
141      let ext = extURI.QueryInterface(AM_Ci.nsIFileURL).file;
142      setExtensionModifiedTime(ext, updateDate);
143
144      // The pending add-on cannot be disabled or enabled.
145      do_check_false(hasFlag(pendingAddons[0].permissions, AddonManager.PERM_CAN_ENABLE));
146      do_check_false(hasFlag(pendingAddons[0].permissions, AddonManager.PERM_CAN_DISABLE));
147
148      await promiseRestartManager();
149
150      AddonManager.getAllInstalls(function(activeInstalls) {
151        do_check_eq(activeInstalls, 0);
152
153        AddonManager.getAddonByID("[email protected]", function(a1) {
154          do_check_neq(a1, null);
155          do_check_eq(a1.type, "extension");
156          do_check_eq(a1.version, "1.0");
157          do_check_eq(a1.name, "Test 1");
158          do_check_true(isExtensionInAddonsList(profileDir, a1.id));
159          do_check_true(do_get_addon("test_install1").exists());
160          do_check_in_crash_annotation(a1.id, a1.version);
161          do_check_eq(a1.size, ADDON1_SIZE);
162
163          do_check_eq(a1.sourceURI.spec,
164                      Services.io.newFileURI(do_get_addon("test_install1")).spec);
165          let difference = a1.installDate.getTime() - updateDate;
166          if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
167            do_throw("Add-on install time was out by " + difference + "ms");
168
169          difference = a1.updateDate.getTime() - updateDate;
170          if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
171            do_throw("Add-on update time was out by " + difference + "ms");
172
173          do_check_true(a1.hasResource("install.rdf"));
174          do_check_false(a1.hasResource("foo.bar"));
175
176          let root_uri = do_get_addon_root_uri(profileDir, "[email protected]");
177          do_check_eq(a1.getResourceURI("install.rdf").spec, root_uri + "install.rdf");
178          do_check_eq(a1.iconURL, root_uri + "icon.png");
179          do_check_eq(a1.icon64URL, root_uri + "icon64.png");
180
181          a1.uninstall();
182          do_execute_soon(function() { run_test_2(a1) });
183        });
184      });
185    }));
186  });
187}
188
189// Tests that an install from a url downloads.
190function run_test_2(aAddon) {
191  let { id, version } = aAddon;
192  restartManager();
193  do_check_not_in_crash_annotation(id, version);
194
195  let url = "http://localhost:4444/addons/test_install2_1.xpi";
196  AddonManager.getInstallForURL(url, function(install) {
197    do_check_neq(install, null);
198    do_check_eq(install.version, "1.0");
199    do_check_eq(install.name, "Test 2");
200    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
201    do_check_eq(install.iconURL, null);
202    do_check_eq(install.sourceURI.spec, url);
203
204    AddonManager.getAllInstalls(function(activeInstalls) {
205      do_check_eq(activeInstalls.length, 1);
206      do_check_eq(activeInstalls[0], install);
207
208      prepare_test({}, [
209        "onDownloadStarted",
210        "onDownloadEnded",
211      ], check_test_2);
212
213      install.addListener({
214        onDownloadProgress() {
215          do_execute_soon(function() {
216            Components.utils.forceGC();
217          });
218        }
219      });
220
221      install.install();
222    });
223  }, "application/x-xpinstall", null, "Test 2", null, "1.0");
224}
225
226function check_test_2(install) {
227  ensure_test_completed();
228  do_check_eq(install.version, "2.0");
229  do_check_eq(install.name, "Real Test 2");
230  do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
231  do_check_eq(install.addon.install, install);
232  do_check_true(hasFlag(install.addon.operationsRequiringRestart,
233                        AddonManager.OP_NEEDS_RESTART_INSTALL));
234  do_check_eq(install.iconURL, null);
235
236  // Pause the install here and start it again in run_test_3
237  do_execute_soon(function() { run_test_3(install); });
238  return false;
239}
240
241// Tests that the downloaded XPI installs ok
242function run_test_3(install) {
243  prepare_test({
244    "[email protected]": [
245      "onInstalling"
246    ]
247  }, [
248    "onInstallStarted",
249    "onInstallEnded",
250  ], check_test_3);
251  install.install();
252}
253
254function check_test_3(aInstall) {
255  // Make the pending install have a sensible date
256  let updateDate = Date.now();
257  let extURI = aInstall.addon.getResourceURI("");
258  let ext = extURI.QueryInterface(AM_Ci.nsIFileURL).file;
259  setExtensionModifiedTime(ext, updateDate);
260
261  ensure_test_completed();
262  AddonManager.getAddonByID("[email protected]", callback_soon(async function(olda2) {
263    do_check_eq(olda2, null);
264    await promiseRestartManager();
265
266    AddonManager.getAllInstalls(function(installs) {
267      do_check_eq(installs, 0);
268
269      AddonManager.getAddonByID("[email protected]", function(a2) {
270        do_check_neq(a2, null);
271        do_check_eq(a2.type, "extension");
272        do_check_eq(a2.version, "2.0");
273        do_check_eq(a2.name, "Real Test 2");
274        do_check_true(isExtensionInAddonsList(profileDir, a2.id));
275        do_check_true(do_get_addon("test_install2_1").exists());
276        do_check_in_crash_annotation(a2.id, a2.version);
277        do_check_eq(a2.sourceURI.spec,
278                    "http://localhost:4444/addons/test_install2_1.xpi");
279
280        let difference = a2.installDate.getTime() - updateDate;
281        if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
282          do_throw("Add-on install time was out by " + difference + "ms");
283
284        difference = a2.updateDate.getTime() - updateDate;
285        if (Math.abs(difference) > MAX_TIME_DIFFERENCE)
286          do_throw("Add-on update time was out by " + difference + "ms");
287
288        gInstallDate = a2.installDate.getTime();
289
290        run_test_4();
291      });
292    });
293  }));
294}
295
296// Tests that installing a new version of an existing add-on works
297function run_test_4() {
298  prepare_test({ }, [
299    "onNewInstall"
300  ]);
301
302  let url = "http://localhost:4444/addons/test_install2_2.xpi";
303  AddonManager.getInstallForURL(url, function(install) {
304    ensure_test_completed();
305
306    do_check_neq(install, null);
307    do_check_eq(install.version, "3.0");
308    do_check_eq(install.name, "Test 3");
309    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
310
311    AddonManager.getAllInstalls(function(activeInstalls) {
312      do_check_eq(activeInstalls.length, 1);
313      do_check_eq(activeInstalls[0], install);
314      do_check_eq(install.existingAddon, null);
315
316      prepare_test({}, [
317        "onDownloadStarted",
318        "onDownloadEnded",
319      ], check_test_4);
320      install.install();
321    });
322  }, "application/x-xpinstall", null, "Test 3", null, "3.0");
323}
324
325function check_test_4(install) {
326  ensure_test_completed();
327
328  do_check_eq(install.version, "3.0");
329  do_check_eq(install.name, "Real Test 3");
330  do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
331  do_check_neq(install.existingAddon);
332  do_check_eq(install.existingAddon.id, "[email protected]");
333  do_check_eq(install.addon.install, install);
334  do_check_true(hasFlag(install.addon.operationsRequiringRestart,
335                        AddonManager.OP_NEEDS_RESTART_INSTALL));
336
337  run_test_5();
338  // Installation will continue when there is nothing returned.
339}
340
341// Continue installing the new version
342function run_test_5() {
343  prepare_test({
344    "[email protected]": [
345      "onInstalling"
346    ]
347  }, [
348    "onInstallStarted",
349    "onInstallEnded",
350  ], check_test_5);
351}
352
353function check_test_5(install) {
354  ensure_test_completed();
355
356  do_check_eq(install.existingAddon.pendingUpgrade.install, install);
357
358  AddonManager.getAddonByID("[email protected]", function(olda2) {
359    do_check_neq(olda2, null);
360    do_check_true(hasFlag(olda2.pendingOperations, AddonManager.PENDING_UPGRADE));
361
362    AddonManager.getInstallsByTypes(null, callback_soon(async function(installs) {
363      do_check_eq(installs.length, 1);
364      do_check_eq(installs[0].addon, olda2.pendingUpgrade);
365      await promiseRestartManager();
366
367      AddonManager.getInstallsByTypes(null, function(installs2) {
368        do_check_eq(installs2.length, 0);
369
370        AddonManager.getAddonByID("[email protected]", function(a2) {
371          do_check_neq(a2, null);
372          do_check_eq(a2.type, "extension");
373          do_check_eq(a2.version, "3.0");
374          do_check_eq(a2.name, "Real Test 3");
375          do_check_true(a2.isActive);
376          do_check_true(isExtensionInAddonsList(profileDir, a2.id));
377          do_check_true(do_get_addon("test_install2_2").exists());
378          do_check_in_crash_annotation(a2.id, a2.version);
379          do_check_eq(a2.sourceURI.spec,
380                      "http://localhost:4444/addons/test_install2_2.xpi");
381
382          do_check_eq(a2.installDate.getTime(), gInstallDate);
383          // Update date should be later (or the same if this test is too fast)
384          do_check_true(a2.installDate <= a2.updateDate);
385
386          a2.uninstall();
387          do_execute_soon(run_test_6);
388        });
389      });
390    }));
391  });
392}
393
394// Tests that an install that requires a compatibility update works
395function run_test_6() {
396  restartManager();
397
398  prepare_test({ }, [
399    "onNewInstall"
400  ]);
401
402  let url = "http://localhost:4444/addons/test_install3.xpi";
403  AddonManager.getInstallForURL(url, function(install) {
404    ensure_test_completed();
405
406    do_check_neq(install, null);
407    do_check_eq(install.version, "1.0");
408    do_check_eq(install.name, "Real Test 4");
409    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
410
411    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
412      do_check_eq(activeInstalls.length, 1);
413      do_check_eq(activeInstalls[0], install);
414
415      prepare_test({}, [
416        "onDownloadStarted",
417        "onDownloadEnded",
418      ], check_test_6);
419      install.install();
420    });
421  }, "application/x-xpinstall", null, "Real Test 4", null, "1.0");
422}
423
424function check_test_6(install) {
425  ensure_test_completed();
426  do_check_eq(install.version, "1.0");
427  do_check_eq(install.name, "Real Test 4");
428  do_check_eq(install.state, AddonManager.STATE_DOWNLOADED);
429  do_check_eq(install.existingAddon, null);
430  do_check_false(install.addon.appDisabled);
431  run_test_7();
432  return true;
433}
434
435// Continue the install
436function run_test_7() {
437  prepare_test({
438    "[email protected]": [
439      "onInstalling"
440    ]
441  }, [
442    "onInstallStarted",
443    "onInstallEnded",
444  ], check_test_7);
445}
446
447function check_test_7() {
448  ensure_test_completed();
449  AddonManager.getAddonByID("[email protected]", callback_soon(async function(olda3) {
450    do_check_eq(olda3, null);
451    await promiseRestartManager();
452
453    AddonManager.getAllInstalls(function(installs) {
454      do_check_eq(installs, 0);
455
456      AddonManager.getAddonByID("[email protected]", function(a3) {
457        do_check_neq(a3, null);
458        do_check_eq(a3.type, "extension");
459        do_check_eq(a3.version, "1.0");
460        do_check_eq(a3.name, "Real Test 4");
461        do_check_true(a3.isActive);
462        do_check_false(a3.appDisabled);
463        do_check_true(isExtensionInAddonsList(profileDir, a3.id));
464        do_check_true(do_get_addon("test_install3").exists());
465        a3.uninstall();
466        do_execute_soon(run_test_8);
467      });
468    });
469  }));
470}
471
472function run_test_8() {
473  restartManager();
474
475  AddonManager.addInstallListener(InstallListener);
476  AddonManager.addAddonListener(AddonListener);
477
478  prepare_test({ }, [
479    "onNewInstall"
480  ]);
481
482  AddonManager.getInstallForFile(do_get_addon("test_install3"), function(install) {
483    do_check_true(install.addon.isCompatible);
484
485    prepare_test({
486      "[email protected]": [
487        "onInstalling"
488      ]
489    }, [
490      "onInstallStarted",
491      "onInstallEnded",
492    ], callback_soon(check_test_8));
493    install.install();
494  });
495}
496
497async function check_test_8() {
498  await promiseRestartManager();
499
500  AddonManager.getAddonByID("[email protected]", function(a3) {
501    do_check_neq(a3, null);
502    do_check_eq(a3.type, "extension");
503    do_check_eq(a3.version, "1.0");
504    do_check_eq(a3.name, "Real Test 4");
505    do_check_true(a3.isActive);
506    do_check_false(a3.appDisabled);
507    do_check_true(isExtensionInAddonsList(profileDir, a3.id));
508    do_check_true(do_get_addon("test_install3").exists());
509    a3.uninstall();
510    do_execute_soon(run_test_9);
511  });
512}
513
514// Test that after cancelling a download it is removed from the active installs
515function run_test_9() {
516  restartManager();
517
518  prepare_test({ }, [
519    "onNewInstall"
520  ]);
521
522  let url = "http://localhost:4444/addons/test_install3.xpi";
523  AddonManager.getInstallForURL(url, function(install) {
524    ensure_test_completed();
525
526    do_check_neq(install, null);
527    do_check_eq(install.version, "1.0");
528    do_check_eq(install.name, "Real Test 4");
529    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
530
531    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
532      do_check_eq(activeInstalls.length, 1);
533      do_check_eq(activeInstalls[0], install);
534
535      prepare_test({}, [
536        "onDownloadStarted",
537        "onDownloadEnded",
538      ], check_test_9);
539      install.install();
540    });
541  }, "application/x-xpinstall", null, "Real Test 4", null, "1.0");
542}
543
544function check_test_9(install) {
545  prepare_test({}, [
546    "onDownloadCancelled"
547  ], function() {
548    let file = install.file;
549
550    // Allow the file removal to complete
551    do_execute_soon(function() {
552      AddonManager.getAllInstalls(function(activeInstalls) {
553        do_check_eq(activeInstalls.length, 0);
554        do_check_false(file.exists());
555
556        run_test_10();
557      });
558    });
559  });
560
561  install.cancel();
562}
563
564// Tests that after cancelling a pending install it is removed from the active
565// installs
566function run_test_10() {
567  prepare_test({ }, [
568    "onNewInstall"
569  ]);
570
571  let url = "http://localhost:4444/addons/test_install3.xpi";
572  AddonManager.getInstallForURL(url, function(install) {
573    ensure_test_completed();
574
575    do_check_neq(install, null);
576    do_check_eq(install.version, "1.0");
577    do_check_eq(install.name, "Real Test 4");
578    do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
579
580    AddonManager.getInstallsByTypes(null, function(activeInstalls) {
581      do_check_eq(activeInstalls.length, 1);
582      do_check_eq(activeInstalls[0], install);
583
584      prepare_test({
585        "[email protected]": [
586          "onInstalling"
587        ]
588      }, [
589        "onDownloadStarted",
590        "onDownloadEnded",
591        "onInstallStarted",
592        "onInstallEnded"
593      ], check_test_10);
594      install.install();
595    });
596  }, "application/x-xpinstall", null, "Real Test 4", null, "1.0");
597}
598
599function check_test_10(install) {
600  prepare_test({
601    "[email protected]": [
602      "onOperationCancelled"
603    ]
604  }, [
605    "onInstallCancelled"
606  ]);
607
608  install.cancel();
609
610  ensure_test_completed();
611
612  AddonManager.getAllInstalls(callback_soon(function(activeInstalls) {
613    do_check_eq(activeInstalls.length, 0);
614
615    restartManager();
616
617    // Check that the install did not complete
618    AddonManager.getAddonByID("[email protected]", function(a3) {
619      do_check_eq(a3, null);
620
621      run_test_11();
622    });
623  }));
624}
625
626function run_test_11() {
627  // Tests 11 and 12 were removed, to avoid churn of renumbering,
628  // just jump ahead to 13 here
629  run_test_13();
630}
631
632
633// Tests that cancelling an upgrade leaves the original add-on's pendingOperations
634// correct
635function run_test_13() {
636  restartManager();
637
638  installAllFiles([do_get_addon("test_install2_1")], function() {
639    restartManager();
640
641    prepare_test({ }, [
642      "onNewInstall"
643    ]);
644
645    let url = "http://localhost:4444/addons/test_install2_2.xpi";
646    AddonManager.getInstallForURL(url, function(install) {
647      ensure_test_completed();
648
649      do_check_neq(install, null);
650      do_check_eq(install.version, "3.0");
651      do_check_eq(install.name, "Test 3");
652      do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
653
654      AddonManager.getAllInstalls(function(activeInstalls) {
655        do_check_eq(activeInstalls.length, 1);
656        do_check_eq(activeInstalls[0], install);
657        do_check_eq(install.existingAddon, null);
658
659        prepare_test({
660          "[email protected]": [
661            "onInstalling"
662          ]
663        }, [
664          "onDownloadStarted",
665          "onDownloadEnded",
666          "onInstallStarted",
667          "onInstallEnded",
668        ], check_test_13);
669        install.install();
670      });
671    }, "application/x-xpinstall", null, "Test 3", null, "3.0");
672  });
673}
674
675function check_test_13(install) {
676  ensure_test_completed();
677
678  do_check_eq(install.version, "3.0");
679  do_check_eq(install.name, "Real Test 3");
680  do_check_eq(install.state, AddonManager.STATE_INSTALLED);
681  do_check_neq(install.existingAddon, null);
682  do_check_eq(install.existingAddon.id, "[email protected]");
683  do_check_eq(install.addon.install, install);
684
685  AddonManager.getAddonByID("[email protected]", callback_soon(function(olda2) {
686    do_check_neq(olda2, null);
687    do_check_true(hasFlag(olda2.pendingOperations, AddonManager.PENDING_UPGRADE));
688    do_check_eq(olda2.pendingUpgrade, install.addon);
689
690    do_check_true(hasFlag(install.addon.pendingOperations,
691                          AddonManager.PENDING_INSTALL));
692
693    prepare_test({
694      "[email protected]": [
695        "onOperationCancelled"
696      ]
697    }, [
698      "onInstallCancelled",
699    ]);
700
701    install.cancel();
702
703    do_check_false(hasFlag(install.addon.pendingOperations, AddonManager.PENDING_INSTALL));
704
705    do_check_false(hasFlag(olda2.pendingOperations, AddonManager.PENDING_UPGRADE));
706    do_check_eq(olda2.pendingUpgrade, null);
707
708    restartManager();
709
710    // Check that the upgrade did not complete
711    AddonManager.getAddonByID("[email protected]", function(a2) {
712      do_check_eq(a2.version, "2.0");
713
714      a2.uninstall();
715
716      do_execute_soon(run_test_14);
717    });
718  }));
719}
720
721// Check that cancelling the install from onDownloadStarted actually cancels it
722function run_test_14() {
723  restartManager();
724
725  prepare_test({ }, [
726    "onNewInstall"
727  ]);
728
729  let url = "http://localhost:4444/addons/test_install2_1.xpi";
730  AddonManager.getInstallForURL(url, function(install) {
731    ensure_test_completed();
732
733    do_check_eq(install.file, null);
734
735    prepare_test({ }, [
736      "onDownloadStarted"
737    ], check_test_14);
738    install.install();
739  }, "application/x-xpinstall");
740}
741
742function check_test_14(install) {
743  prepare_test({ }, [
744    "onDownloadCancelled"
745  ], function() {
746    let file = install.file;
747
748    install.addListener({
749      onDownloadProgress() {
750        do_throw("Download should not have continued");
751      },
752      onDownloadEnded() {
753        do_throw("Download should not have continued");
754      }
755    });
756
757    // Allow the listener to return to see if it continues downloading. The
758    // The listener only really tests if we give it time to see progress, the
759    // file check isn't ideal either
760    do_execute_soon(function() {
761      do_check_false(file.exists());
762
763      run_test_15();
764    });
765  });
766
767  // Wait for the channel to be ready to cancel
768  do_execute_soon(function() {
769    install.cancel();
770  });
771}
772
773// Checks that cancelling the install from onDownloadEnded actually cancels it
774function run_test_15() {
775  prepare_test({ }, [
776    "onNewInstall"
777  ]);
778
779  let url = "http://localhost:4444/addons/test_install2_1.xpi";
780  AddonManager.getInstallForURL(url, function(install) {
781    ensure_test_completed();
782
783    do_check_eq(install.file, null);
784
785    prepare_test({ }, [
786      "onDownloadStarted",
787      "onDownloadEnded"
788    ], check_test_15);
789    install.install();
790  }, "application/x-xpinstall");
791}
792
793function check_test_15(install) {
794  prepare_test({ }, [
795    "onDownloadCancelled"
796  ]);
797
798  install.cancel();
799
800  ensure_test_completed();
801
802  install.addListener({
803    onInstallStarted() {
804      do_throw("Install should not have continued");
805    }
806  });
807
808  // Allow the listener to return to see if it starts installing
809  do_execute_soon(run_test_16);
810}
811
812// Verify that the userDisabled value carries over to the upgrade by default
813function run_test_16() {
814  restartManager();
815
816  let url = "http://localhost:4444/addons/test_install2_1.xpi";
817  AddonManager.getInstallForURL(url, function(aInstall) {
818    aInstall.addListener({
819      onInstallStarted() {
820        do_check_false(aInstall.addon.userDisabled);
821        aInstall.addon.userDisabled = true;
822      },
823
824      onInstallEnded() {
825       do_execute_soon(function test16_install1() {
826        restartManager();
827
828        AddonManager.getAddonByID("[email protected]", function(a2) {
829          do_check_true(a2.userDisabled);
830          do_check_false(a2.isActive);
831
832          let url_2 = "http://localhost:4444/addons/test_install2_2.xpi";
833          AddonManager.getInstallForURL(url_2, function(aInstall_2) {
834            aInstall_2.addListener({
835              onInstallEnded() {
836               do_execute_soon(function test16_install2() {
837                do_check_true(aInstall_2.addon.userDisabled);
838
839                restartManager();
840
841                AddonManager.getAddonByID("[email protected]", function(a2_2) {
842                  do_check_true(a2_2.userDisabled);
843                  do_check_false(a2_2.isActive);
844
845                  a2_2.uninstall();
846                  do_execute_soon(run_test_17);
847                });
848               });
849              }
850            });
851            aInstall_2.install();
852          }, "application/x-xpinstall");
853        });
854       });
855      }
856    });
857    aInstall.install();
858  }, "application/x-xpinstall");
859}
860
861// Verify that changing the userDisabled value before onInstallEnded works
862function run_test_17() {
863  restartManager();
864
865  let url = "http://localhost:4444/addons/test_install2_1.xpi";
866  AddonManager.getInstallForURL(url, function(aInstall) {
867    aInstall.addListener({
868      onInstallEnded() {
869       do_execute_soon(function() {
870        do_check_false(aInstall.addon.userDisabled);
871
872        restartManager();
873
874        AddonManager.getAddonByID("[email protected]", function(a2) {
875          do_check_false(a2.userDisabled);
876          do_check_true(a2.isActive);
877
878          let url_2 = "http://localhost:4444/addons/test_install2_2.xpi";
879          AddonManager.getInstallForURL(url_2, function(aInstall_2) {
880            aInstall_2.addListener({
881              onInstallStarted() {
882                do_check_false(aInstall_2.addon.userDisabled);
883                aInstall_2.addon.userDisabled = true;
884              },
885
886              onInstallEnded() {
887               do_execute_soon(function() {
888                restartManager();
889
890                AddonManager.getAddonByID("[email protected]", function(a2_2) {
891                  do_check_true(a2_2.userDisabled);
892                  do_check_false(a2_2.isActive);
893
894                  a2_2.uninstall();
895                  do_execute_soon(run_test_18);
896                });
897               });
898              }
899            });
900            aInstall_2.install();
901          }, "application/x-xpinstall");
902        });
903       });
904      }
905    });
906    aInstall.install();
907  }, "application/x-xpinstall");
908}
909
910// Verify that changing the userDisabled value before onInstallEnded works
911function run_test_18() {
912  restartManager();
913
914  let url = "http://localhost:4444/addons/test_install2_1.xpi";
915  AddonManager.getInstallForURL(url, function(aInstall) {
916    aInstall.addListener({
917      onInstallStarted() {
918        do_check_false(aInstall.addon.userDisabled);
919        aInstall.addon.userDisabled = true;
920      },
921
922      onInstallEnded() {
923       do_execute_soon(function test18_install1() {
924        restartManager();
925
926        AddonManager.getAddonByID("[email protected]", function(a2) {
927          do_check_true(a2.userDisabled);
928          do_check_false(a2.isActive);
929
930          let url_2 = "http://localhost:4444/addons/test_install2_2.xpi";
931          AddonManager.getInstallForURL(url_2, function(aInstall_2) {
932            aInstall_2.addListener({
933              onInstallStarted() {
934                do_check_true(aInstall_2.addon.userDisabled);
935                aInstall_2.addon.userDisabled = false;
936              },
937
938              onInstallEnded() {
939               do_execute_soon(function test18_install2() {
940                restartManager();
941
942                AddonManager.getAddonByID("[email protected]", function(a2_2) {
943                  do_check_false(a2_2.userDisabled);
944                  do_check_true(a2_2.isActive);
945
946                  a2_2.uninstall();
947                  do_execute_soon(run_test_18_1);
948                });
949               });
950              }
951            });
952            aInstall_2.install();
953          }, "application/x-xpinstall");
954        });
955       });
956      }
957    });
958    aInstall.install();
959  }, "application/x-xpinstall");
960}
961
962
963// Checks that metadata is not stored if the pref is set to false
964function run_test_18_1() {
965  restartManager();
966
967  Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", true);
968  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS,
969                             "http://localhost:4444/data/test_install.xml");
970
971  Services.prefs.setBoolPref("[email protected]", false);
972
973  let url = "http://localhost:4444/addons/test_install2_1.xpi";
974  AddonManager.getInstallForURL(url, function(aInstall) {
975    aInstall.addListener({
976      onInstallEnded(unused, aAddon) {
977       do_execute_soon(function test18_install() {
978        do_check_neq(aAddon.fullDescription, "Repository description");
979
980        restartManager();
981
982        AddonManager.getAddonByID("[email protected]", function(a2) {
983          do_check_neq(a2.fullDescription, "Repository description");
984
985          a2.uninstall();
986          do_execute_soon(run_test_19);
987        });
988       });
989      }
990    });
991    aInstall.install();
992  }, "application/x-xpinstall");
993}
994
995// Checks that metadata is downloaded for new installs and is visible before and
996// after restart
997function run_test_19() {
998  restartManager();
999  Services.prefs.setBoolPref("[email protected]", true);
1000
1001  let url = "http://localhost:4444/addons/test_install2_1.xpi";
1002  AddonManager.getInstallForURL(url, function(aInstall) {
1003    aInstall.addListener({
1004      onInstallEnded(unused, aAddon) {
1005       do_execute_soon(function test19_install() {
1006        do_check_eq(aAddon.fullDescription, "Repository description");
1007
1008        restartManager();
1009
1010        AddonManager.getAddonByID("[email protected]", function(a2) {
1011          do_check_eq(a2.fullDescription, "Repository description");
1012
1013          a2.uninstall();
1014          do_execute_soon(run_test_20);
1015        });
1016       });
1017      }
1018    });
1019    aInstall.install();
1020  }, "application/x-xpinstall");
1021}
1022
1023// Do the same again to make sure it works when the data is already in the cache
1024function run_test_20() {
1025  restartManager();
1026
1027  let url = "http://localhost:4444/addons/test_install2_1.xpi";
1028  AddonManager.getInstallForURL(url, function(aInstall) {
1029    aInstall.addListener({
1030      onInstallEnded(unused, aAddon) {
1031       do_execute_soon(function test20_install() {
1032        do_check_eq(aAddon.fullDescription, "Repository description");
1033
1034        restartManager();
1035
1036        AddonManager.getAddonByID("[email protected]", function(a2) {
1037          do_check_eq(a2.fullDescription, "Repository description");
1038
1039          a2.uninstall();
1040          do_execute_soon(run_test_21);
1041        });
1042       });
1043      }
1044    });
1045    aInstall.install();
1046  }, "application/x-xpinstall");
1047}
1048
1049// Verify that installing an add-on that is already pending install cancels the
1050// first install
1051function run_test_21() {
1052  restartManager();
1053  Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", false);
1054
1055  installAllFiles([do_get_addon("test_install2_1")], function() {
1056    AddonManager.getAllInstalls(function(aInstalls) {
1057      do_check_eq(aInstalls.length, 1);
1058
1059      prepare_test({
1060        "[email protected]": [
1061          "onOperationCancelled",
1062          "onInstalling"
1063        ]
1064      }, [
1065        "onNewInstall",
1066        "onDownloadStarted",
1067