summaryrefslogtreecommitdiff
path: root/toolkit/mozapps/extensions/test/xpcshell/test_bug659772.js
blob: c6e8ab4e2200a603e49e30b8d2205965b9f9099e (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// Tests that a pending upgrade during a schema update doesn't break things

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

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

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

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

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

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

  run_test_1();
}

// Tests whether a schema migration without app version change works
function run_test_1() {
  writeInstallRDFForExtension(addon1, profileDir);
  writeInstallRDFForExtension(addon2, profileDir);
  writeInstallRDFForExtension(addon3, profileDir);
  writeInstallRDFForExtension(addon4, profileDir);

  startupManager();

  AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org",
                               "addon2@tests.mozilla.org",
                               "addon3@tests.mozilla.org",
                               "addon4@tests.mozilla.org"],
                              function([a1, a2, a3, a4]) {
    do_check_neq(a1, null);
    do_check_eq(a1.version, "2.0");
    do_check_false(a1.appDisabled);
    do_check_false(a1.userDisabled);
    do_check_true(a1.isActive);
    do_check_true(isExtensionInAddonsList(profileDir, addon1.id));

    do_check_neq(a2, null);
    do_check_eq(a2.version, "2.0");
    do_check_false(a2.appDisabled);
    do_check_false(a2.userDisabled);
    do_check_true(a2.isActive);
    do_check_true(isExtensionInAddonsList(profileDir, addon2.id));

    do_check_neq(a3, null);
    do_check_eq(a3.version, "2.0");
    do_check_false(a3.appDisabled);
    do_check_false(a3.userDisabled);
    do_check_true(a3.isActive);
    do_check_true(isExtensionInAddonsList(profileDir, addon3.id));

    do_check_neq(a4, null);
    do_check_eq(a4.version, "2.0");
    do_check_true(a4.appDisabled);
    do_check_false(a4.userDisabled);
    do_check_false(a4.isActive);
    do_check_false(isExtensionInAddonsList(profileDir, addon4.id));

    // Prepare the add-on update, and a bootstrapped addon (bug 693714)
    installAllFiles([
      do_get_addon("test_bug659772"),
      do_get_addon("test_bootstrap1_1")
    ], function() {
      shutdownManager();

      // Make it look like the next time the app is started it has a new DB schema
      changeXPIDBVersion(1);
      Services.prefs.setIntPref("extensions.databaseSchema", 1);

      let jsonfile = gProfD.clone();
      jsonfile.append("extensions");
      jsonfile.append("staged");
      jsonfile.append("addon3@tests.mozilla.org.json");
      do_check_true(jsonfile.exists());

      // Remove an unnecessary property from the cached manifest
      let fis = AM_Cc["@mozilla.org/network/file-input-stream;1"].
                   createInstance(AM_Ci.nsIFileInputStream);
      let json = AM_Cc["@mozilla.org/dom/json;1"].
                 createInstance(AM_Ci.nsIJSON);
      fis.init(jsonfile, -1, 0, 0);
      let addonObj = json.decodeFromStream(fis, jsonfile.fileSize);
      fis.close();
      delete addonObj.optionsType;

      let stream = AM_Cc["@mozilla.org/network/file-output-stream;1"].
                   createInstance(AM_Ci.nsIFileOutputStream);
      let converter = AM_Cc["@mozilla.org/intl/converter-output-stream;1"].
                      createInstance(AM_Ci.nsIConverterOutputStream);
      stream.init(jsonfile, FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE |
                            FileUtils.MODE_TRUNCATE, FileUtils.PERMS_FILE,
                            0);
      converter.init(stream, "UTF-8", 0, 0x0000);
      converter.writeString(JSON.stringify(addonObj));
      converter.close();
      stream.close();

      Services.prefs.clearUserPref("bootstraptest.install_reason");
      Services.prefs.clearUserPref("bootstraptest.uninstall_reason");

      startupManager(false);

      AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org",
                                   "addon2@tests.mozilla.org",
                                   "addon3@tests.mozilla.org",
                                   "addon4@tests.mozilla.org"],
                                  function([a1, a2, a3, a4]) {
        do_check_neq(a1, null);
        do_check_eq(a1.version, "2.0");
        do_check_false(a1.appDisabled);
        do_check_false(a1.userDisabled);
        do_check_true(a1.isActive);
        do_check_true(isExtensionInAddonsList(profileDir, addon1.id));

        do_check_neq(a2, null);
        do_check_eq(a2.version, "2.0");
        do_check_false(a2.appDisabled);
        do_check_false(a2.userDisabled);
        do_check_true(a2.isActive);
        do_check_true(isExtensionInAddonsList(profileDir, addon2.id));

        // Should stay enabled because we migrate the compat info from
        // the previous version of the DB
        do_check_neq(a3, null);
        do_check_eq(a3.version, "2.0");
        todo_check_false(a3.appDisabled); // XXX unresolved issue
        do_check_false(a3.userDisabled);
        todo_check_true(a3.isActive); // XXX same
        todo_check_true(isExtensionInAddonsList(profileDir, addon3.id)); // XXX same

        do_check_neq(a4, null);
        do_check_eq(a4.version, "2.0");
        do_check_true(a4.appDisabled);
        do_check_false(a4.userDisabled);
        do_check_false(a4.isActive);
        do_check_false(isExtensionInAddonsList(profileDir, addon4.id));

        // Check that install and uninstall haven't been called on the bootstrapped addon
        do_check_false(Services.prefs.prefHasUserValue("bootstraptest.install_reason"));
        do_check_false(Services.prefs.prefHasUserValue("bootstraptest.uninstall_reason"));

        a1.uninstall();
        a2.uninstall();
        a3.uninstall();
        a4.uninstall();
        do_execute_soon(run_test_2);
      });
    });
  });
}

// Tests whether a schema migration with app version change works
function run_test_2() {
  restartManager();

  shutdownManager();

  writeInstallRDFForExtension(addon1, profileDir);
  writeInstallRDFForExtension(addon2, profileDir);
  writeInstallRDFForExtension(addon3, profileDir);
  writeInstallRDFForExtension(addon4, profileDir);

  startupManager();

  AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org",
                               "addon2@tests.mozilla.org",
                               "addon3@tests.mozilla.org",
                               "addon4@tests.mozilla.org"],
                              function([a1, a2, a3, a4]) {
    do_check_neq(a1, null);
    do_check_eq(a1.version, "2.0");
    do_check_false(a1.appDisabled);
    do_check_false(a1.userDisabled);
    do_check_true(a1.isActive);
    do_check_true(isExtensionInAddonsList(profileDir, addon1.id));

    do_check_neq(a2, null);
    do_check_eq(a2.version, "2.0");
    do_check_false(a2.appDisabled);
    do_check_false(a2.userDisabled);
    do_check_true(a2.isActive);
    do_check_true(isExtensionInAddonsList(profileDir, addon2.id));

    do_check_neq(a3, null);
    do_check_eq(a3.version, "2.0");
    do_check_false(a3.appDisabled);
    do_check_false(a3.userDisabled);
    do_check_true(a3.isActive);
    do_check_true(isExtensionInAddonsList(profileDir, addon3.id));

    do_check_neq(a4, null);
    do_check_eq(a4.version, "2.0");
    do_check_true(a4.appDisabled);
    do_check_false(a4.userDisabled);
    do_check_false(a4.isActive);
    do_check_false(isExtensionInAddonsList(profileDir, addon4.id));

    // Prepare the add-on update, and a bootstrapped addon (bug 693714)
    installAllFiles([
      do_get_addon("test_bug659772"),
      do_get_addon("test_bootstrap1_1")
    ], function() { do_execute_soon(prepare_schema_migrate); });

    function prepare_schema_migrate() {
      shutdownManager();

      // Make it look like the next time the app is started it has a new DB schema
      changeXPIDBVersion(1);
      Services.prefs.setIntPref("extensions.databaseSchema", 1);

      let jsonfile = gProfD.clone();
      jsonfile.append("extensions");
      jsonfile.append("staged");
      jsonfile.append("addon3@tests.mozilla.org.json");
      do_check_true(jsonfile.exists());

      // Remove an unnecessary property from the cached manifest
      let fis = AM_Cc["@mozilla.org/network/file-input-stream;1"].
                   createInstance(AM_Ci.nsIFileInputStream);
      let json = AM_Cc["@mozilla.org/dom/json;1"].
                 createInstance(AM_Ci.nsIJSON);
      fis.init(jsonfile, -1, 0, 0);
      let addonObj = json.decodeFromStream(fis, jsonfile.fileSize);
      fis.close();
      delete addonObj.optionsType;

      let stream = AM_Cc["@mozilla.org/network/file-output-stream;1"].
                   createInstance(AM_Ci.nsIFileOutputStream);
      let converter = AM_Cc["@mozilla.org/intl/converter-output-stream;1"].
                      createInstance(AM_Ci.nsIConverterOutputStream);
      stream.init(jsonfile, FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE |
                            FileUtils.MODE_TRUNCATE, FileUtils.PERMS_FILE,
                            0);
      converter.init(stream, "UTF-8", 0, 0x0000);
      converter.writeString(JSON.stringify(addonObj));
      converter.close();
      stream.close();

      Services.prefs.clearUserPref("bootstraptest.install_reason");
      Services.prefs.clearUserPref("bootstraptest.uninstall_reason");

      gAppInfo.version = "2";
      startupManager(true);

      AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org",
                                   "addon2@tests.mozilla.org",
                                   "addon3@tests.mozilla.org",
                                   "addon4@tests.mozilla.org"],
                                  callback_soon(function([a1, a2, a3, a4]) {
        do_check_neq(a1, null);
        do_check_eq(a1.version, "2.0");
        do_check_true(a1.appDisabled);
        do_check_false(a1.userDisabled);
        do_check_false(a1.isActive);
        do_check_false(isExtensionInAddonsList(profileDir, addon1.id));

        do_check_neq(a2, null);
        do_check_eq(a2.version, "2.0");
        do_check_false(a2.appDisabled);
        do_check_false(a2.userDisabled);
        do_check_true(a2.isActive);
        do_check_true(isExtensionInAddonsList(profileDir, addon2.id));

        // Should become appDisabled because we migrate the compat info from
        // the previous version of the DB
        do_check_neq(a3, null);
        do_check_eq(a3.version, "2.0");
        todo_check_true(a3.appDisabled);
        do_check_false(a3.userDisabled);
        todo_check_false(a3.isActive);
        todo_check_false(isExtensionInAddonsList(profileDir, addon3.id));

        do_check_neq(a4, null);
        do_check_eq(a4.version, "2.0");
        do_check_false(a4.appDisabled);
        do_check_false(a4.userDisabled);
        do_check_true(a4.isActive);
        do_check_true(isExtensionInAddonsList(profileDir, addon4.id));

        // Check that install and uninstall haven't been called on the bootstrapped addon
        do_check_false(Services.prefs.prefHasUserValue("bootstraptest.install_reason"));
        do_check_false(Services.prefs.prefHasUserValue("bootstraptest.uninstall_reason"));

        a1.uninstall();
        a2.uninstall();
        a3.uninstall();
        a4.uninstall();
        restartManager();

        shutdownManager();

        do_test_finished();
      }));
    };
  });
}