summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_updateid.js
blob: f7e3e21e56b2a5c2e9e59d4a29f1b8e5f947cf0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This verifies that updating an add-on to a new ID works

// The test extension uses an insecure update url.
Services.prefs.setBoolPref("extensions.checkUpdateSecurity", false);

const profileDir = gProfD.clone();
profileDir.append("extensions");

function promiseInstallUpdate(install) {
  return new Promise((resolve, reject) => {
    install.addListener({
      onDownloadFailed: () => {
        let err = new Error("download error");
        err.code = install.error;
        reject(err);
      },
      onInstallFailed: () => {
        let err = new Error("install error");
        err.code = install.error;
        reject(err);
      },
      onInstallEnded: resolve,
    });

    install.install();
  });
}

// Create and configure the HTTP server.
let testserver = createHttpServer(4444);
testserver.registerDirectory("/data/", do_get_file("data"));
testserver.registerDirectory("/addons/", do_get_file("addons"));

function run_test() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
  startupManager();
  run_next_test();
}

// Verify that an update to an add-on with a new ID fails
add_task(function* test_update_new_id() {
  yield promiseInstallFile(do_get_addon("test_updateid1"));

  let addon = yield promiseAddonByID("addon1@tests.mozilla.org");
  do_check_neq(addon, null);
  do_check_eq(addon.version, "1.0");

  let update = yield promiseFindAddonUpdates(addon, AddonManager.UPDATE_WHEN_USER_REQUESTED);
  let install = update.updateAvailable;
  do_check_eq(install.name, addon.name);
  do_check_eq(install.version, "2.0");
  do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
  do_check_eq(install.existingAddon, addon);

  yield Assert.rejects(promiseInstallUpdate(install),
                       function(err) { return err.code == AddonManager.ERROR_INCORRECT_ID },
                       "Upgrade to a different ID fails");

  addon.uninstall();
});

// Verify that an update to a multi-package xpi fails
add_task(function* test_update_new_id() {
  yield promiseInstallFile(do_get_addon("test_update_multi1"));

  let addon = yield promiseAddonByID("updatemulti@tests.mozilla.org");
  do_check_neq(addon, null);
  do_check_eq(addon.version, "1.0");

  let update = yield promiseFindAddonUpdates(addon, AddonManager.UPDATE_WHEN_USER_REQUESTED);
  let install = update.updateAvailable;
  do_check_eq(install.name, addon.name);
  do_check_eq(install.version, "2.0");
  do_check_eq(install.state, AddonManager.STATE_AVAILABLE);
  do_check_eq(install.existingAddon, addon);

  yield Assert.rejects(promiseInstallUpdate(install),
                       function(err) { return err.code == AddonManager.ERROR_UNEXPECTED_ADDON_TYPE },
                       "Upgrade to a multipackage xpi fails");

  addon.uninstall();
});