summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_distribution.js
blob: 720b454cc897658b6bc2c9976a7d58aa6eb8a1c4 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* 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);

var addon1_1 = {
  id: "addon1@tests.mozilla.org",
  version: "1.0",
  name: "Test version 1",
  targetApplications: [{
    id: "xpcshell@tests.mozilla.org",
    minVersion: "1",
    maxVersion: "5"
  }]
};

var addon1_2 = {
  id: "addon1@tests.mozilla.org",
  version: "2.0",
  name: "Test version 2",
  targetApplications: [{
    id: "xpcshell@tests.mozilla.org",
    minVersion: "1",
    maxVersion: "5"
  }]
};

var addon1_3 = {
  id: "addon1@tests.mozilla.org",
  version: "3.0",
  name: "Test version 3",
  targetApplications: [{
    id: "xpcshell@tests.mozilla.org",
    minVersion: "1",
    maxVersion: "5"
  }]
};

function getActiveVersion() {
  return Services.prefs.getIntPref("bootstraptest.active_version");
}

function getInstalledVersion() {
  return Services.prefs.getIntPref("bootstraptest.installed_version");
}

function setOldModificationTime() {
  // Make sure the installed extension has an old modification time so any
  // changes will be detected
  shutdownManager()
  let extension = gProfD.clone();
  extension.append("extensions");
  if (Services.prefs.getBoolPref("extensions.alwaysUnpack"))
    extension.append("addon1@tests.mozilla.org");
  else
    extension.append("addon1@tests.mozilla.org.xpi");
  setExtensionModifiedTime(extension, Date.now() - MAKE_FILE_OLD_DIFFERENCE);
  startupManager(false);
}

function run_test() {
  do_test_pending();

  run_test_1();
}

// Tests that on the first startup the add-on gets installed, with now as the
// profile modifiedTime.
function run_test_1() {
  let extension = writeInstallRDFForExtension(addon1_1, distroDir);
  setExtensionModifiedTime(extension, Date.now() - MAKE_FILE_OLD_DIFFERENCE);

  startupManager();

  AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
    do_check_neq(a1, null);
    do_check_eq(a1.version, "1.0");
    do_check_true(a1.isActive);
    do_check_eq(a1.scope, AddonManager.SCOPE_PROFILE);
    do_check_false(a1.foreignInstall);

    // Modification time should be updated when the addon is copied to the
    // profile.
    let testURI = a1.getResourceURI(TEST_UNPACKED ? "install.rdf" : "");
    let testFile = testURI.QueryInterface(Components.interfaces.nsIFileURL).file;

    do_check_true(testFile.exists());
    let difference = testFile.lastModifiedTime - Date.now();
    do_check_true(Math.abs(difference) < MAX_TIME_DIFFERENCE);

    do_execute_soon(run_test_2);
  });
}

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

  writeInstallRDFForExtension(addon1_2, distroDir);

  restartManager();

  AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
    do_check_neq(a1, null);
    do_check_eq(a1.version, "1.0");
    do_check_true(a1.isActive);
    do_check_eq(a1.scope, AddonManager.SCOPE_PROFILE);

    do_execute_soon(run_test_3);
  });
}

// Test that an app upgrade installs the newer version
function run_test_3() {
  restartManager("2");

  AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
    do_check_neq(a1, null);
    do_check_eq(a1.version, "2.0");
    do_check_true(a1.isActive);
    do_check_eq(a1.scope, AddonManager.SCOPE_PROFILE);
    do_check_false(a1.foreignInstall);

    do_execute_soon(run_test_4);
  });
}

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

  writeInstallRDFForExtension(addon1_1, distroDir);

  restartManager("3");

  AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
    do_check_neq(a1, null);
    do_check_eq(a1.version, "2.0");
    do_check_true(a1.isActive);
    do_check_eq(a1.scope, AddonManager.SCOPE_PROFILE);

    do_execute_soon(run_test_5);
  });
}

// Tests that after uninstalling a restart doesn't re-install the extension
function run_test_5() {
  AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) {
    a1.uninstall();

    restartManager();

    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1_2) {
      do_check_eq(a1_2, null);

      do_execute_soon(run_test_6);
    });
  }));
}

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

  AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
    do_check_eq(a1, null);

    do_execute_soon(run_test_7);
  });
}

// Tests that a pending install of a newer version of a distributed add-on
// at app change still gets applied
function run_test_7() {
  Services.prefs.clearUserPref("extensions.installedDistroAddon.addon1@tests.mozilla.org");

  installAllFiles([do_get_addon("test_distribution1_2")], function() {
    restartManager(2);

    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
      do_check_neq(a1, null);
      do_check_eq(a1.version, "2.0");
      do_check_true(a1.isActive);
      do_check_eq(a1.scope, AddonManager.SCOPE_PROFILE);

      a1.uninstall();
      do_execute_soon(run_test_8);
    });
  });
}

// Tests that a pending install of a older version of a distributed add-on
// at app change gets replaced by the distributed version
function run_test_8() {
  restartManager();

  writeInstallRDFForExtension(addon1_3, distroDir);

  installAllFiles([do_get_addon("test_distribution1_2")], function() {
    restartManager(3);

    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
      do_check_neq(a1, null);
      do_check_eq(a1.version, "3.0");
      do_check_true(a1.isActive);
      do_check_eq(a1.scope, AddonManager.SCOPE_PROFILE);

      a1.uninstall();
      do_execute_soon(run_test_9);
    });
  });
}

// Tests that bootstrapped add-ons distributed start up correctly, also that
// add-ons with multiple directories get copied fully
function run_test_9() {
  restartManager();

  // Copy the test add-on to the distro dir
  let addon = do_get_file("data/test_distribution2_2");
  addon.copyTo(distroDir, "addon2@tests.mozilla.org");

  restartManager("5");

  AddonManager.getAddonByID("addon2@tests.mozilla.org", function(a2) {
    do_check_neq(a2, null);
    do_check_true(a2.isActive);

    do_check_eq(getInstalledVersion(), 2);
    do_check_eq(getActiveVersion(), 2);

    do_check_true(a2.hasResource("bootstrap.js"));
    do_check_true(a2.hasResource("subdir/dummy.txt"));
    do_check_true(a2.hasResource("subdir/subdir2/dummy2.txt"));

    // Currently installs are unpacked if the source is a directory regardless
    // of the install.rdf property or the global preference

    let addonDir = profileDir.clone();
    addonDir.append("addon2@tests.mozilla.org");
    do_check_true(addonDir.exists());
    do_check_true(addonDir.isDirectory());
    addonDir.append("subdir");
    do_check_true(addonDir.exists());
    do_check_true(addonDir.isDirectory());
    addonDir.append("subdir2");
    do_check_true(addonDir.exists());
    do_check_true(addonDir.isDirectory());
    addonDir.append("dummy2.txt");
    do_check_true(addonDir.exists());
    do_check_true(addonDir.isFile());

    a2.uninstall();

    do_execute_soon(do_test_finished);
  });
}