summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_distribution.js
blob: 61231160d8b381afd7b603cfacb1aa475feba928 (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
113
114
115
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This verifies that add-ons distributed with the application get installed
// correctly

// Allow distributed add-ons to install
Services.prefs.setBoolPref("extensions.installDistroAddons", true);

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");

const profileDir = gProfD.clone();
profileDir.append("extensions");
const distroDir = gProfD.clone();
distroDir.append("distribution");
distroDir.append("extensions");
registerDirectory("XREAppDist", distroDir.parent);

async function setOldModificationTime() {
  // Make sure the installed extension has an old modification time so any
  // changes will be detected
  await promiseShutdownManager();
  let extension = gProfD.clone();
  extension.append("extensions");
  extension.append(`${ID}.xpi`);
  setExtensionModifiedTime(extension, Date.now() - MAKE_FILE_OLD_DIFFERENCE);
  await promiseStartupManager();
}

const ID = "addon@tests.mozilla.org";

async function writeDistroAddon(version) {
  let xpi = await createTempWebExtensionFile({
    manifest: {
      version,
      browser_specific_settings: { gecko: { id: ID } },
    },
  });
  xpi.copyTo(distroDir, `${ID}.xpi`);
}

// Tests that on the first startup the add-on gets installed
add_task(async function run_test_1() {
  await writeDistroAddon("1.0");
  await promiseStartupManager();

  let a1 = await AddonManager.getAddonByID(ID);
  Assert.notEqual(a1, null);
  Assert.equal(a1.version, "1.0");
  Assert.ok(a1.isActive);
  Assert.equal(a1.scope, AddonManager.SCOPE_PROFILE);
  Assert.ok(!a1.foreignInstall);
});

// Tests that starting with a newer version in the distribution dir doesn't
// install it yet
add_task(async function run_test_2() {
  await setOldModificationTime();

  await writeDistroAddon("2.0");
  await promiseRestartManager();

  let a1 = await AddonManager.getAddonByID(ID);
  Assert.notEqual(a1, null);
  Assert.equal(a1.version, "1.0");
  Assert.ok(a1.isActive);
  Assert.equal(a1.scope, AddonManager.SCOPE_PROFILE);
});

// Test that an app upgrade installs the newer version
add_task(async function run_test_3() {
  await promiseRestartManager("2");

  let a1 = await AddonManager.getAddonByID(ID);
  Assert.notEqual(a1, null);
  Assert.equal(a1.version, "2.0");
  Assert.ok(a1.isActive);
  Assert.equal(a1.scope, AddonManager.SCOPE_PROFILE);
  Assert.ok(!a1.foreignInstall);
});

// Test that an app upgrade doesn't downgrade the extension
add_task(async function run_test_4() {
  await setOldModificationTime();

  await writeDistroAddon("1.0");
  await promiseRestartManager("3");

  let a1 = await AddonManager.getAddonByID(ID);
  Assert.notEqual(a1, null);
  Assert.equal(a1.version, "2.0");
  Assert.ok(a1.isActive);
  Assert.equal(a1.scope, AddonManager.SCOPE_PROFILE);
});

// Tests that after uninstalling a restart doesn't re-install the extension
add_task(async function run_test_5() {
  let a1 = await AddonManager.getAddonByID(ID);
  await a1.uninstall();

  await promiseRestartManager();

  let a1_2 = await AddonManager.getAddonByID(ID);
  Assert.equal(a1_2, null);
});

// Tests that upgrading the application still doesn't re-install the uninstalled
// extension
add_task(async function run_test_6() {
  await promiseRestartManager("4");

  let a1 = await AddonManager.getAddonByID(ID);
  Assert.equal(a1, null);
});