summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_update_compatmode.js
blob: 85ec556f954d3a0e5ec06e8442c1f7c7fe6b4afa (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This verifies that add-on update check correctly fills in the
// %COMPATIBILITY_MODE% token in the update URL.

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

let testserver = createHttpServer({ hosts: ["example.com"] });

let lastMode;
testserver.registerPathHandler("/update.json", (request, response) => {
  let params = new URLSearchParams(request.queryString);
  lastMode = params.get("mode");

  response.setHeader("content-type", "application/json", true);
  response.write(JSON.stringify({ addons: {} }));
});

const ID_NORMAL = "compatmode@tests.mozilla.org";
const ID_STRICT = "compatmode-strict@tests.mozilla.org";

add_task(async function setup() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");

  let xpi = await createAddon({
    id: ID_NORMAL,
    updateURL: "http://example.com/update.json?mode=%COMPATIBILITY_MODE%",
    targetApplications: [
      {
        id: "xpcshell@tests.mozilla.org",
        minVersion: "1",
        maxVersion: "1",
      },
    ],
  });
  await manuallyInstall(xpi, AddonTestUtils.profileExtensions, ID_NORMAL);

  xpi = await createAddon({
    id: ID_STRICT,
    updateURL: "http://example.com/update.json?mode=%COMPATIBILITY_MODE%",
    strictCompatibility: true,
    targetApplications: [
      {
        id: "xpcshell@tests.mozilla.org",
        minVersion: "1",
        maxVersion: "1",
      },
    ],
  });
  await manuallyInstall(xpi, AddonTestUtils.profileExtensions, ID_STRICT);

  await promiseStartupManager();
});

// Strict compatibility checking disabled.
add_task(async function test_strict_disabled() {
  Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false);
  let addon = await AddonManager.getAddonByID(ID_NORMAL);
  Assert.notEqual(addon, null);

  await promiseFindAddonUpdates(addon, AddonManager.UPDATE_WHEN_USER_REQUESTED);
  Assert.equal(
    lastMode,
    "normal",
    "COMPATIBIILITY_MODE normal was set correctly"
  );
});

// Strict compatibility checking enabled.
add_task(async function test_strict_enabled() {
  Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, true);
  let addon = await AddonManager.getAddonByID(ID_NORMAL);
  Assert.notEqual(addon, null);

  await promiseFindAddonUpdates(addon, AddonManager.UPDATE_WHEN_USER_REQUESTED);
  Assert.equal(
    lastMode,
    "strict",
    "COMPATIBILITY_MODE strict was set correctly"
  );
});

// Strict compatibility checking opt-in.
add_task(async function test_strict_optin() {
  Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false);
  let addon = await AddonManager.getAddonByID(ID_STRICT);
  Assert.notEqual(addon, null);

  await promiseFindAddonUpdates(addon, AddonManager.UPDATE_WHEN_USER_REQUESTED);
  Assert.equal(
    lastMode,
    "normal",
    "COMPATIBILITY_MODE is normal even for an addon with strictCompatibility"
  );
});

// Compatibility checking disabled.
add_task(async function test_compat_disabled() {
  AddonManager.checkCompatibility = false;
  let addon = await AddonManager.getAddonByID(ID_NORMAL);
  Assert.notEqual(addon, null);

  await promiseFindAddonUpdates(addon, AddonManager.UPDATE_WHEN_USER_REQUESTED);
  Assert.equal(
    lastMode,
    "ignore",
    "COMPATIBILITY_MODE ignore was set correctly"
  );
});