summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_cache.js
blob: 5c4e873fefce17d84b7856a8a63e2ba94e5b7058 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// Tests caching in AddonRepository.jsm

var gServer;

const HOST = "example.com";
const BASE_URL = "http://example.com";

const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled";
const PREF_GETADDONS_CACHE_TYPES = "extensions.getAddons.cache.types";
const GETADDONS_RESULTS = BASE_URL + "/data/test_AddonRepository_cache.json";
const EMPTY_RESULT = BASE_URL + "/data/test_AddonRepository_empty.json";
const FAILED_RESULT = BASE_URL + "/data/test_AddonRepository_fail.json";

const FILE_DATABASE = "addons.json";

const ADDONS = [
  {
    manifest: {
      name: "XPI Add-on 1",
      version: "1.1",

      description: "XPI Add-on 1 - Description",
      developer: {
        name: "XPI Add-on 1 - Author",
      },

      homepage_url: "http://example.com/xpi/1/homepage.html",
      icons: {
        32: "icon.png",
      },

      options_ui: {
        page: "options.html",
      },

      browser_specific_settings: {
        gecko: { id: "test_AddonRepository_1@tests.mozilla.org" },
      },
    },
  },
  {
    manifest: {
      name: "XPI Add-on 2",
      version: "1.2",
      theme: {},
      browser_specific_settings: {
        gecko: { id: "test_AddonRepository_2@tests.mozilla.org" },
      },
    },
  },
  {
    manifest: {
      name: "XPI Add-on 3",
      version: "1.3",
      icons: {
        32: "icon.png",
      },
      theme: {},
      browser_specific_settings: {
        gecko: { id: "test_AddonRepository_3@tests.mozilla.org" },
      },
    },
    files: {
      "preview.png": "",
    },
  },
];

const ADDON_IDS = ADDONS.map(
  addon => addon.manifest.browser_specific_settings.gecko.id
);
const ADDON_FILES = ADDONS.map(addon =>
  AddonTestUtils.createTempWebExtensionFile(addon)
);

const PREF_ADDON0_CACHE_ENABLED =
  "extensions." + ADDON_IDS[0] + ".getAddons.cache.enabled";
const PREF_ADDON1_CACHE_ENABLED =
  "extensions." + ADDON_IDS[1] + ".getAddons.cache.enabled";

// Properties of an individual add-on that should be checked
// Note: updateDate is checked separately
const ADDON_PROPERTIES = [
  "id",
  "type",
  "name",
  "version",
  "developers",
  "description",
  "fullDescription",
  "icons",
  "screenshots",
  "homepageURL",
  "supportURL",
  "optionsURL",
  "averageRating",
  "reviewCount",
  "reviewURL",
  "weeklyDownloads",
  "sourceURI",
];

// The updateDate property is annoying to test for XPI add-ons.
// However, since we only care about whether the repository value vs. the
// XPI value is used, we can just test if the property value matches
// the repository value
const REPOSITORY_UPDATEDATE = 9;

// Get the URI of a subfile locating directly in the folder of
// the add-on corresponding to the specified id
function get_subfile_uri(aId, aFilename) {
  let file = gProfD.clone();
  file.append("extensions");
  return do_get_addon_root_uri(file, aId) + aFilename;
}

// Expected repository add-ons
const REPOSITORY_ADDONS = [
  {
    id: ADDON_IDS[0],
    type: "extension",
    name: "Repo Add-on 1",
    version: "2.1",
    developers: [
      {
        name: "Repo Add-on 1 - First Developer",
        url: BASE_URL + "/repo/1/firstDeveloper.html",
      },
      {
        name: "Repo Add-on 1 - Second Developer",
        url: BASE_URL + "/repo/1/secondDeveloper.html",
      },
    ],
    description: "Repo Add-on 1 - Description\nSecond line",
    fullDescription: "Repo Add-on 1 - Full Description & some extra",
    icons: { 32: BASE_URL + "/repo/1/icon.png" },
    homepageURL: BASE_URL + "/repo/1/homepage.html",
    supportURL: BASE_URL + "/repo/1/support.html",
    averageRating: 1,
    reviewCount: 1111,
    reviewURL: BASE_URL + "/repo/1/review.html",
    weeklyDownloads: 3331,
    sourceURI: BASE_URL + "/repo/1/install.xpi",
  },
  {
    id: ADDON_IDS[1],
    type: "theme",
    name: "Repo Add-on 2",
    version: "2.2",
    developers: [
      {
        name: "Repo Add-on 2 - First Developer",
        url: BASE_URL + "/repo/2/firstDeveloper.html",
      },
      {
        name: "Repo Add-on 2 - Second Developer",
        url: BASE_URL + "/repo/2/secondDeveloper.html",
      },
    ],
    description: "Repo Add-on 2 - Description",
    fullDescription: "Repo Add-on 2 - Full Description",
    icons: { 32: BASE_URL + "/repo/2/icon.png" },
    screenshots: [
      {
        url: BASE_URL + "/repo/2/firstFull.png",
        thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png",
        caption: "Repo Add-on 2 - First Caption",
      },
      {
        url: BASE_URL + "/repo/2/secondFull.png",
        thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png",
        caption: "Repo Add-on 2 - Second Caption",
      },
    ],
    homepageURL: BASE_URL + "/repo/2/homepage.html",
    supportURL: BASE_URL + "/repo/2/support.html",
    averageRating: 2,
    reviewCount: 1112,
    reviewURL: BASE_URL + "/repo/2/review.html",
    weeklyDownloads: 3332,
    sourceURI: BASE_URL + "/repo/2/install.xpi",
  },
  {
    id: ADDON_IDS[2],
    type: "theme",
    name: "Repo Add-on 3",
    version: "2.3",
    icons: { 32: BASE_URL + "/repo/3/icon.png" },
    screenshots: [
      {
        url: BASE_URL + "/repo/3/firstFull.png",
        thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png",
        caption: "Repo Add-on 3 - First Caption",
      },
      {
        url: BASE_URL + "/repo/3/secondFull.png",
        thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png",
        caption: "Repo Add-on 3 - Second Caption",
      },
    ],
  },
];

function extensionURL(id, path) {
  return WebExtensionPolicy.getByID(id).getURL(path);
}

// Expected add-ons when not using cache
const WITHOUT_CACHE = [
  {
    id: ADDON_IDS[0],
    type: "extension",
    name: "XPI Add-on 1",
    version: "1.1",
    authors: [{ name: "XPI Add-on 1 - Author" }],
    description: "XPI Add-on 1 - Description",
    get icons() {
      return { 32: get_subfile_uri(ADDON_IDS[0], "icon.png") };
    },
    homepageURL: `${BASE_URL}/xpi/1/homepage.html`,
    get optionsURL() {
      return extensionURL(ADDON_IDS[0], "options.html");
    },
    sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec,
  },
  {
    id: ADDON_IDS[1],
    type: "theme",
    name: "XPI Add-on 2",
    version: "1.2",
    sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec,
    icons: {},
  },
  {
    id: ADDON_IDS[2],
    type: "theme",
    name: "XPI Add-on 3",
    version: "1.3",
    get icons() {
      return { 32: get_subfile_uri(ADDON_IDS[2], "icon.png") };
    },
    screenshots: [
      {
        get url() {
          return get_subfile_uri(ADDON_IDS[2], "preview.png");
        },
      },
    ],
    sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec,
  },
];

// Expected add-ons when using cache
const WITH_CACHE = [
  {
    id: ADDON_IDS[0],
    type: "extension",
    name: "XPI Add-on 1",
    version: "1.1",
    developers: [
      {
        name: "Repo Add-on 1 - First Developer",
        url: BASE_URL + "/repo/1/firstDeveloper.html",
      },
      {
        name: "Repo Add-on 1 - Second Developer",
        url: BASE_URL + "/repo/1/secondDeveloper.html",
      },
    ],
    description: "XPI Add-on 1 - Description",
    fullDescription: "Repo Add-on 1 - Full Description & some extra",
    get icons() {
      return { 32: get_subfile_uri(ADDON_IDS[0], "icon.png") };
    },
    homepageURL: BASE_URL + "/xpi/1/homepage.html",
    supportURL: BASE_URL + "/repo/1/support.html",
    get optionsURL() {
      return extensionURL(ADDON_IDS[0], "options.html");
    },
    averageRating: 1,
    reviewCount: 1111,
    reviewURL: BASE_URL + "/repo/1/review.html",
    weeklyDownloads: 3331,
    sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec,
  },
  {
    id: ADDON_IDS[1],
    type: "theme",
    name: "XPI Add-on 2",
    version: "1.2",
    developers: [
      {
        name: "Repo Add-on 2 - First Developer",
        url: BASE_URL + "/repo/2/firstDeveloper.html",
      },
      {
        name: "Repo Add-on 2 - Second Developer",
        url: BASE_URL + "/repo/2/secondDeveloper.html",
      },
    ],
    description: "Repo Add-on 2 - Description",
    fullDescription: "Repo Add-on 2 - Full Description",
    icons: { 32: BASE_URL + "/repo/2/icon.png" },
    screenshots: [
      {
        url: BASE_URL + "/repo/2/firstFull.png",
        thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png",
        caption: "Repo Add-on 2 - First Caption",
      },
      {
        url: BASE_URL + "/repo/2/secondFull.png",
        thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png",
        caption: "Repo Add-on 2 - Second Caption",
      },
    ],
    homepageURL: BASE_URL + "/repo/2/homepage.html",
    supportURL: BASE_URL + "/repo/2/support.html",
    averageRating: 2,
    reviewCount: 1112,
    reviewURL: BASE_URL + "/repo/2/review.html",
    weeklyDownloads: 3332,
    sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec,
  },
  {
    id: ADDON_IDS[2],
    type: "theme",
    name: "XPI Add-on 3",
    version: "1.3",
    get iconURL() {
      return get_subfile_uri(ADDON_IDS[2], "icon.png");
    },
    get icons() {
      return { 32: get_subfile_uri(ADDON_IDS[2], "icon.png") };
    },
    screenshots: [
      {
        url: BASE_URL + "/repo/3/firstFull.png",
        thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png",
        caption: "Repo Add-on 3 - First Caption",
      },
      {
        url: BASE_URL + "/repo/3/secondFull.png",
        thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png",
        caption: "Repo Add-on 3 - Second Caption",
      },
    ],
    sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec,
  },
];

// Expected add-ons when using cache
const WITH_EXTENSION_CACHE = [
  {
    id: ADDON_IDS[0],
    type: "extension",
    name: "XPI Add-on 1",
    version: "1.1",
    developers: [
      {
        name: "Repo Add-on 1 - First Developer",
        url: BASE_URL + "/repo/1/firstDeveloper.html",
      },
      {
        name: "Repo Add-on 1 - Second Developer",
        url: BASE_URL + "/repo/1/secondDeveloper.html",
      },
    ],
    description: "XPI Add-on 1 - Description",
    fullDescription: "Repo Add-on 1 - Full Description & some extra",
    get icons() {
      return { 32: get_subfile_uri(ADDON_IDS[0], "icon.png") };
    },
    homepageURL: BASE_URL + "/xpi/1/homepage.html",
    supportURL: BASE_URL + "/repo/1/support.html",
    get optionsURL() {
      return extensionURL(ADDON_IDS[0], "options.html");
    },
    averageRating: 1,
    reviewCount: 1111,
    reviewURL: BASE_URL + "/repo/1/review.html",
    weeklyDownloads: 3331,
    sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec,
  },
  {
    id: ADDON_IDS[1],
    type: "theme",
    name: "XPI Add-on 2",
    version: "1.2",
    sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec,
    icons: {},
  },
  {
    id: ADDON_IDS[2],
    type: "theme",
    name: "XPI Add-on 3",
    version: "1.3",
    get iconURL() {
      return get_subfile_uri(ADDON_IDS[2], "icon.png");
    },
    get icons() {
      return { 32: get_subfile_uri(ADDON_IDS[2], "icon.png") };
    },
    screenshots: [
      {
        get url() {
          return get_subfile_uri(ADDON_IDS[2], "preview.png");
        },
      },
    ],
    sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec,
  },
];

var gDBFile = gProfD.clone();
gDBFile.append(FILE_DATABASE);

/*
 * Check the actual add-on results against the expected add-on results
 *
 * @param  aActualAddons
 *         The array of actual add-ons to check
 * @param  aExpectedAddons
 *         The array of expected add-ons to check against
 * @param  aFromRepository
 *         An optional boolean representing if the add-ons are from
 *         the repository
 */
function check_results(aActualAddons, aExpectedAddons, aFromRepository) {
  aFromRepository = !!aFromRepository;

  do_check_addons(aActualAddons, aExpectedAddons, ADDON_PROPERTIES);

  // Separately test updateDate (it should only be equal to the
  // REPOSITORY values if it is from the repository)
  aActualAddons.forEach(function (aActualAddon) {
    if (aActualAddon.updateDate) {
      let time = aActualAddon.updateDate.getTime();
      Assert.equal(time === 1000 * REPOSITORY_UPDATEDATE, aFromRepository);
    }
  });
}

/*
 * Check the add-ons in the cache. This function also tests
 * AddonRepository.getCachedAddonByID()
 *
 * @param  aExpectedToFind
 *         An array of booleans representing which REPOSITORY_ADDONS are
 *         expected to be found in the cache
 * @param  aExpectedImmediately
 *         A boolean representing if results from the cache are expected
 *         immediately. Results are not immediate if the cache has not been
 *         initialized yet.
 * @return Promise{null}
 *         Resolves once the checks are complete
 */
function check_cache(aExpectedToFind, aExpectedImmediately) {
  Assert.equal(aExpectedToFind.length, REPOSITORY_ADDONS.length);

  let lookups = [];

  for (let i = 0; i < REPOSITORY_ADDONS.length; i++) {
    lookups.push(
      new Promise((resolve, reject) => {
        let immediatelyFound = true;
        let expected = aExpectedToFind[i] ? REPOSITORY_ADDONS[i] : null;
        // can't Promise-wrap this because we're also testing whether the callback is
        // sync or async
        AddonRepository.getCachedAddonByID(
          REPOSITORY_ADDONS[i].id,
          function (aAddon) {
            Assert.equal(immediatelyFound, aExpectedImmediately);
            if (expected == null) {
              Assert.equal(aAddon, null);
            } else {
              check_results([aAddon], [expected], true);
            }
            resolve();
          }
        );
        immediatelyFound = false;
      })
    );
  }
  return Promise.all(lookups);
}

/*
 * Task to check an initialized cache by checking the cache, then restarting the
 * manager, and checking the cache. This checks that the cache is consistent
 * across manager restarts.
 *
 * @param  aExpectedToFind
 *         An array of booleans representing which REPOSITORY_ADDONS are
 *         expected to be found in the cache
 */
async function check_initialized_cache(aExpectedToFind) {
  await check_cache(aExpectedToFind, true);
  await promiseRestartManager();

  // If cache is disabled, then expect results immediately
  let cacheEnabled = Services.prefs.getBoolPref(PREF_GETADDONS_CACHE_ENABLED);
  await check_cache(aExpectedToFind, !cacheEnabled);
}

add_task(async function setup() {
  // Setup for test
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");

  await promiseStartupManager();

  // Install XPI add-ons
  await promiseInstallAllFiles(ADDON_FILES);
  await promiseRestartManager();

  gServer = AddonTestUtils.createHttpServer({ hosts: [HOST] });
  gServer.registerDirectory("/data/", do_get_file("data"));
});

// Tests AddonRepository.cacheEnabled
add_task(async function run_test_1() {
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
  Assert.ok(!AddonRepository.cacheEnabled);
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
  Assert.ok(AddonRepository.cacheEnabled);
});

// Tests that the cache and database begin as empty
add_task(async function run_test_2() {
  Assert.ok(!gDBFile.exists());
  await check_cache([false, false, false], false);
  await AddonRepository.flush();
});

// Tests repopulateCache when the search fails
add_task(async function run_test_3() {
  Assert.ok(gDBFile.exists());
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, FAILED_RESULT);

  await AddonRepository.backgroundUpdateCheck();
  await check_initialized_cache([false, false, false]);
});

// Tests repopulateCache when search returns no results
add_task(async function run_test_4() {
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, EMPTY_RESULT);

  await AddonRepository.backgroundUpdateCheck();
  await check_initialized_cache([false, false, false]);
});

// Tests repopulateCache when search returns results
add_task(async function run_test_5() {
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);

  await AddonRepository.backgroundUpdateCheck();
  await check_initialized_cache([true, true, true]);
});

// Tests repopulateCache when caching is disabled for a single add-on
add_task(async function run_test_5_1() {
  Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, false);

  await AddonRepository.backgroundUpdateCheck();

  // Reset pref for next test
  Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, true);

  await check_initialized_cache([false, true, true]);
});

// Tests repopulateCache when caching is disabled
add_task(async function run_test_6() {
  Assert.ok(gDBFile.exists());
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);

  await AddonRepository.backgroundUpdateCheck();
  // Database should have been deleted
  Assert.ok(!gDBFile.exists());

  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
  await check_cache([false, false, false], false);
  await AddonRepository.flush();
});

// Tests cacheAddons when the search fails
add_task(async function run_test_7() {
  Assert.ok(gDBFile.exists());
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, FAILED_RESULT);

  await AddonRepository.cacheAddons(ADDON_IDS);
  await check_initialized_cache([false, false, false]);
});

// Tests cacheAddons when the search returns no results
add_task(async function run_test_8() {
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, EMPTY_RESULT);

  await AddonRepository.cacheAddons(ADDON_IDS);
  await check_initialized_cache([false, false, false]);
});

// Tests cacheAddons for a single add-on when search returns results
add_task(async function run_test_9() {
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);

  await AddonRepository.cacheAddons([ADDON_IDS[0]]);
  await check_initialized_cache([true, false, false]);
});

// Tests cacheAddons when caching is disabled for a single add-on
add_task(async function run_test_9_1() {
  Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, false);

  await AddonRepository.cacheAddons(ADDON_IDS);

  // Reset pref for next test
  Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, true);

  await check_initialized_cache([true, false, true]);
});

// Tests cacheAddons for multiple add-ons, some already in the cache,
add_task(async function run_test_10() {
  await AddonRepository.cacheAddons(ADDON_IDS);
  await check_initialized_cache([true, true, true]);
});

// Tests cacheAddons when caching is disabled
add_task(async function run_test_11() {
  Assert.ok(gDBFile.exists());
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);

  await AddonRepository.cacheAddons(ADDON_IDS);
  Assert.ok(gDBFile.exists());

  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
  await check_initialized_cache([true, true, true]);
});

// Tests that XPI add-ons do not use any of the repository properties if
// caching is disabled, even if there are repository properties available
add_task(async function run_test_12() {
  Assert.ok(gDBFile.exists());
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);

  let addons = await promiseAddonsByIDs(ADDON_IDS);
  check_results(addons, WITHOUT_CACHE);
});

// Tests that a background update with caching disabled deletes the add-ons
// database, and that XPI add-ons still do not use any of repository properties
add_task(async function run_test_13() {
  Assert.ok(gDBFile.exists());
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, EMPTY_RESULT);

  await AddonManagerPrivate.backgroundUpdateCheck();
  // Database should have been deleted
  Assert.ok(!gDBFile.exists());

  let aAddons = await promiseAddonsByIDs(ADDON_IDS);
  check_results(aAddons, WITHOUT_CACHE);
});

// Tests that the XPI add-ons have the correct properties if caching is
// enabled but has no information
add_task(async function run_test_14() {
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);

  await AddonManagerPrivate.backgroundUpdateCheck();
  await AddonRepository.flush();
  Assert.ok(gDBFile.exists());

  let aAddons = await promiseAddonsByIDs(ADDON_IDS);
  check_results(aAddons, WITHOUT_CACHE);
});

// Tests that the XPI add-ons correctly use the repository properties when
// caching is enabled and the repository information is available
add_task(async function run_test_15() {
  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);

  await AddonManagerPrivate.backgroundUpdateCheck();
  let aAddons = await promiseAddonsByIDs(ADDON_IDS);
  check_results(aAddons, WITH_CACHE);
});

// Tests that restarting the manager does not change the checked properties
// on the XPI add-ons (repository properties still exist and are still properly
// used)
add_task(async function run_test_16() {
  await promiseRestartManager();

  let aAddons = await promiseAddonsByIDs(ADDON_IDS);
  check_results(aAddons, WITH_CACHE);
});

// Tests that setting a list of types to cache works
add_task(async function run_test_17() {
  Services.prefs.setCharPref(
    PREF_GETADDONS_CACHE_TYPES,
    "foo,bar,extension,baz"
  );

  await AddonManagerPrivate.backgroundUpdateCheck();
  let aAddons = await promiseAddonsByIDs(ADDON_IDS);
  check_results(aAddons, WITH_EXTENSION_CACHE);
});