Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
5 // Tests caching in AddonRepository.jsm
7 Components.utils.import("resource://gre/modules/addons/AddonRepository.jsm");
9 Components.utils.import("resource://testing-common/httpd.js");
10 let gServer;
12 const PORT = 4444;
13 const BASE_URL = "http://localhost:" + PORT;
15 const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled";
16 const PREF_GETADDONS_CACHE_TYPES = "extensions.getAddons.cache.types";
17 const GETADDONS_RESULTS = BASE_URL + "/data/test_AddonRepository_cache.xml";
18 const GETADDONS_EMPTY = BASE_URL + "/data/test_AddonRepository_empty.xml";
19 const GETADDONS_FAILED = BASE_URL + "/data/test_AddonRepository_failed.xml";
21 const FILE_DATABASE = "addons.json";
22 const ADDON_NAMES = ["test_AddonRepository_1",
23 "test_AddonRepository_2",
24 "test_AddonRepository_3"];
25 const ADDON_IDS = ADDON_NAMES.map(function(aName) aName + "@tests.mozilla.org");
26 const ADDON_FILES = ADDON_NAMES.map(do_get_addon);
28 const PREF_ADDON0_CACHE_ENABLED = "extensions." + ADDON_IDS[0] + ".getAddons.cache.enabled";
29 const PREF_ADDON1_CACHE_ENABLED = "extensions." + ADDON_IDS[1] + ".getAddons.cache.enabled";
31 // Properties of an individual add-on that should be checked
32 // Note: size and updateDate are checked separately
33 const ADDON_PROPERTIES = ["id", "type", "name", "version", "creator",
34 "developers", "translators", "contributors",
35 "description", "fullDescription",
36 "developerComments", "eula", "iconURL", "icons",
37 "screenshots", "homepageURL", "supportURL",
38 "optionsURL", "aboutURL", "contributionURL",
39 "contributionAmount", "averageRating", "reviewCount",
40 "reviewURL", "totalDownloads", "weeklyDownloads",
41 "dailyUsers", "sourceURI", "repositoryStatus",
42 "compatibilityOverrides"];
44 // The size and updateDate properties are annoying to test for XPI add-ons.
45 // However, since we only care about whether the repository value vs. the
46 // XPI value is used, we can just test if the property value matches
47 // the repository value
48 const REPOSITORY_SIZE = 9;
49 const REPOSITORY_UPDATEDATE = 9;
51 // Get the URI of a subfile locating directly in the folder of
52 // the add-on corresponding to the specified id
53 function get_subfile_uri(aId, aFilename) {
54 let file = gProfD.clone();
55 file.append("extensions");
56 return do_get_addon_root_uri(file, aId) + aFilename;
57 }
60 // Expected repository add-ons
61 const REPOSITORY_ADDONS = [{
62 id: ADDON_IDS[0],
63 type: "extension",
64 name: "Repo Add-on 1",
65 version: "2.1",
66 creator: {
67 name: "Repo Add-on 1 - Creator",
68 url: BASE_URL + "/repo/1/creator.html"
69 },
70 developers: [{
71 name: "Repo Add-on 1 - First Developer",
72 url: BASE_URL + "/repo/1/firstDeveloper.html"
73 }, {
74 name: "Repo Add-on 1 - Second Developer",
75 url: BASE_URL + "/repo/1/secondDeveloper.html"
76 }],
77 description: "Repo Add-on 1 - Description\nSecond line",
78 fullDescription: "Repo Add-on 1 - Full Description & some extra",
79 developerComments: "Repo Add-on 1\nDeveloper Comments",
80 eula: "Repo Add-on 1 - EULA",
81 iconURL: BASE_URL + "/repo/1/icon.png",
82 icons: { "32": BASE_URL + "/repo/1/icon.png" },
83 homepageURL: BASE_URL + "/repo/1/homepage.html",
84 supportURL: BASE_URL + "/repo/1/support.html",
85 contributionURL: BASE_URL + "/repo/1/meetDevelopers.html",
86 contributionAmount: "$11.11",
87 averageRating: 1,
88 reviewCount: 1111,
89 reviewURL: BASE_URL + "/repo/1/review.html",
90 totalDownloads: 2221,
91 weeklyDownloads: 3331,
92 dailyUsers: 4441,
93 sourceURI: BASE_URL + "/repo/1/install.xpi",
94 repositoryStatus: 4,
95 compatibilityOverrides: [{
96 type: "incompatible",
97 minVersion: 0.1,
98 maxVersion: 0.2,
99 appID: "xpcshell@tests.mozilla.org",
100 appMinVersion: 3.0,
101 appMaxVersion: 4.0
102 }, {
103 type: "incompatible",
104 minVersion: 0.2,
105 maxVersion: 0.3,
106 appID: "xpcshell@tests.mozilla.org",
107 appMinVersion: 5.0,
108 appMaxVersion: 6.0
109 }]
110 }, {
111 id: ADDON_IDS[1],
112 type: "theme",
113 name: "Repo Add-on 2",
114 version: "2.2",
115 creator: {
116 name: "Repo Add-on 2 - Creator",
117 url: BASE_URL + "/repo/2/creator.html"
118 },
119 developers: [{
120 name: "Repo Add-on 2 - First Developer",
121 url: BASE_URL + "/repo/2/firstDeveloper.html"
122 }, {
123 name: "Repo Add-on 2 - Second Developer",
124 url: BASE_URL + "/repo/2/secondDeveloper.html"
125 }],
126 description: "Repo Add-on 2 - Description",
127 fullDescription: "Repo Add-on 2 - Full Description",
128 developerComments: "Repo Add-on 2 - Developer Comments",
129 eula: "Repo Add-on 2 - EULA",
130 iconURL: BASE_URL + "/repo/2/icon.png",
131 icons: { "32": BASE_URL + "/repo/2/icon.png" },
132 screenshots: [{
133 url: BASE_URL + "/repo/2/firstFull.png",
134 thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png",
135 caption: "Repo Add-on 2 - First Caption"
136 } , {
137 url: BASE_URL + "/repo/2/secondFull.png",
138 thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png",
139 caption: "Repo Add-on 2 - Second Caption"
140 }],
141 homepageURL: BASE_URL + "/repo/2/homepage.html",
142 supportURL: BASE_URL + "/repo/2/support.html",
143 contributionURL: BASE_URL + "/repo/2/meetDevelopers.html",
144 contributionAmount: null,
145 averageRating: 2,
146 reviewCount: 1112,
147 reviewURL: BASE_URL + "/repo/2/review.html",
148 totalDownloads: 2222,
149 weeklyDownloads: 3332,
150 dailyUsers: 4442,
151 sourceURI: BASE_URL + "/repo/2/install.xpi",
152 repositoryStatus: 9
153 }, {
154 id: ADDON_IDS[2],
155 name: "Repo Add-on 3",
156 version: "2.3",
157 iconURL: BASE_URL + "/repo/3/icon.png",
158 icons: { "32": BASE_URL + "/repo/3/icon.png" },
159 screenshots: [{
160 url: BASE_URL + "/repo/3/firstFull.png",
161 thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png",
162 caption: "Repo Add-on 3 - First Caption"
163 } , {
164 url: BASE_URL + "/repo/3/secondFull.png",
165 thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png",
166 caption: "Repo Add-on 3 - Second Caption"
167 }]
168 }];
171 // Expected add-ons when not using cache
172 const WITHOUT_CACHE = [{
173 id: ADDON_IDS[0],
174 type: "extension",
175 name: "XPI Add-on 1",
176 version: "1.1",
177 creator: { name: "XPI Add-on 1 - Creator" },
178 developers: [{ name: "XPI Add-on 1 - First Developer" },
179 { name: "XPI Add-on 1 - Second Developer" }],
180 translators: [{ name: "XPI Add-on 1 - First Translator" },
181 { name: "XPI Add-on 1 - Second Translator" }],
182 contributors: [{ name: "XPI Add-on 1 - First Contributor" },
183 { name: "XPI Add-on 1 - Second Contributor" }],
184 description: "XPI Add-on 1 - Description",
185 iconURL: BASE_URL + "/xpi/1/icon.png",
186 icons: { "32": BASE_URL + "/xpi/1/icon.png" },
187 homepageURL: BASE_URL + "/xpi/1/homepage.html",
188 optionsURL: BASE_URL + "/xpi/1/options.html",
189 aboutURL: BASE_URL + "/xpi/1/about.html",
190 sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec
191 }, {
192 id: ADDON_IDS[1],
193 type: "theme",
194 name: "XPI Add-on 2",
195 version: "1.2",
196 sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec,
197 icons: {}
198 }, {
199 id: ADDON_IDS[2],
200 type: "theme",
201 name: "XPI Add-on 3",
202 version: "1.3",
203 get iconURL () {
204 return get_subfile_uri(ADDON_IDS[2], "icon.png");
205 },
206 get icons () {
207 return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") };
208 },
209 screenshots: [{ get url () { return get_subfile_uri(ADDON_IDS[2], "preview.png"); } }],
210 sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec
211 }];
214 // Expected add-ons when using cache
215 const WITH_CACHE = [{
216 id: ADDON_IDS[0],
217 type: "extension",
218 name: "XPI Add-on 1",
219 version: "1.1",
220 creator: {
221 name: "Repo Add-on 1 - Creator",
222 url: BASE_URL + "/repo/1/creator.html"
223 },
224 developers: [{ name: "XPI Add-on 1 - First Developer" },
225 { name: "XPI Add-on 1 - Second Developer" }],
226 translators: [{ name: "XPI Add-on 1 - First Translator" },
227 { name: "XPI Add-on 1 - Second Translator" }],
228 contributors: [{ name: "XPI Add-on 1 - First Contributor" },
229 { name: "XPI Add-on 1 - Second Contributor" }],
230 description: "XPI Add-on 1 - Description",
231 fullDescription: "Repo Add-on 1 - Full Description & some extra",
232 developerComments: "Repo Add-on 1\nDeveloper Comments",
233 eula: "Repo Add-on 1 - EULA",
234 iconURL: BASE_URL + "/xpi/1/icon.png",
235 icons: { "32": BASE_URL + "/xpi/1/icon.png" },
236 homepageURL: BASE_URL + "/xpi/1/homepage.html",
237 supportURL: BASE_URL + "/repo/1/support.html",
238 optionsURL: BASE_URL + "/xpi/1/options.html",
239 aboutURL: BASE_URL + "/xpi/1/about.html",
240 contributionURL: BASE_URL + "/repo/1/meetDevelopers.html",
241 contributionAmount: "$11.11",
242 averageRating: 1,
243 reviewCount: 1111,
244 reviewURL: BASE_URL + "/repo/1/review.html",
245 totalDownloads: 2221,
246 weeklyDownloads: 3331,
247 dailyUsers: 4441,
248 sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec,
249 repositoryStatus: 4,
250 compatibilityOverrides: [{
251 type: "incompatible",
252 minVersion: 0.1,
253 maxVersion: 0.2,
254 appID: "xpcshell@tests.mozilla.org",
255 appMinVersion: 3.0,
256 appMaxVersion: 4.0
257 }, {
258 type: "incompatible",
259 minVersion: 0.2,
260 maxVersion: 0.3,
261 appID: "xpcshell@tests.mozilla.org",
262 appMinVersion: 5.0,
263 appMaxVersion: 6.0
264 }]
265 }, {
266 id: ADDON_IDS[1],
267 type: "theme",
268 name: "XPI Add-on 2",
269 version: "1.2",
270 creator: {
271 name: "Repo Add-on 2 - Creator",
272 url: BASE_URL + "/repo/2/creator.html"
273 },
274 developers: [{
275 name: "Repo Add-on 2 - First Developer",
276 url: BASE_URL + "/repo/2/firstDeveloper.html"
277 }, {
278 name: "Repo Add-on 2 - Second Developer",
279 url: BASE_URL + "/repo/2/secondDeveloper.html"
280 }],
281 description: "Repo Add-on 2 - Description",
282 fullDescription: "Repo Add-on 2 - Full Description",
283 developerComments: "Repo Add-on 2 - Developer Comments",
284 eula: "Repo Add-on 2 - EULA",
285 iconURL: BASE_URL + "/repo/2/icon.png",
286 icons: { "32": BASE_URL + "/repo/2/icon.png" },
287 screenshots: [{
288 url: BASE_URL + "/repo/2/firstFull.png",
289 thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png",
290 caption: "Repo Add-on 2 - First Caption"
291 } , {
292 url: BASE_URL + "/repo/2/secondFull.png",
293 thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png",
294 caption: "Repo Add-on 2 - Second Caption"
295 }],
296 homepageURL: BASE_URL + "/repo/2/homepage.html",
297 supportURL: BASE_URL + "/repo/2/support.html",
298 contributionURL: BASE_URL + "/repo/2/meetDevelopers.html",
299 contributionAmount: null,
300 averageRating: 2,
301 reviewCount: 1112,
302 reviewURL: BASE_URL + "/repo/2/review.html",
303 totalDownloads: 2222,
304 weeklyDownloads: 3332,
305 dailyUsers: 4442,
306 sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec,
307 repositoryStatus: 9
308 }, {
309 id: ADDON_IDS[2],
310 type: "theme",
311 name: "XPI Add-on 3",
312 version: "1.3",
313 get iconURL () {
314 return get_subfile_uri(ADDON_IDS[2], "icon.png");
315 },
316 get icons () {
317 return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") };
318 },
319 screenshots: [{
320 url: BASE_URL + "/repo/3/firstFull.png",
321 thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png",
322 caption: "Repo Add-on 3 - First Caption"
323 } , {
324 url: BASE_URL + "/repo/3/secondFull.png",
325 thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png",
326 caption: "Repo Add-on 3 - Second Caption"
327 }],
328 sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec
329 }];
331 // Expected add-ons when using cache
332 const WITH_EXTENSION_CACHE = [{
333 id: ADDON_IDS[0],
334 type: "extension",
335 name: "XPI Add-on 1",
336 version: "1.1",
337 creator: {
338 name: "Repo Add-on 1 - Creator",
339 url: BASE_URL + "/repo/1/creator.html"
340 },
341 developers: [{ name: "XPI Add-on 1 - First Developer" },
342 { name: "XPI Add-on 1 - Second Developer" }],
343 translators: [{ name: "XPI Add-on 1 - First Translator" },
344 { name: "XPI Add-on 1 - Second Translator" }],
345 contributors: [{ name: "XPI Add-on 1 - First Contributor" },
346 { name: "XPI Add-on 1 - Second Contributor" }],
347 description: "XPI Add-on 1 - Description",
348 fullDescription: "Repo Add-on 1 - Full Description & some extra",
349 developerComments: "Repo Add-on 1\nDeveloper Comments",
350 eula: "Repo Add-on 1 - EULA",
351 iconURL: BASE_URL + "/xpi/1/icon.png",
352 icons: { "32": BASE_URL + "/xpi/1/icon.png" },
353 homepageURL: BASE_URL + "/xpi/1/homepage.html",
354 supportURL: BASE_URL + "/repo/1/support.html",
355 optionsURL: BASE_URL + "/xpi/1/options.html",
356 aboutURL: BASE_URL + "/xpi/1/about.html",
357 contributionURL: BASE_URL + "/repo/1/meetDevelopers.html",
358 contributionAmount: "$11.11",
359 averageRating: 1,
360 reviewCount: 1111,
361 reviewURL: BASE_URL + "/repo/1/review.html",
362 totalDownloads: 2221,
363 weeklyDownloads: 3331,
364 dailyUsers: 4441,
365 sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec,
366 repositoryStatus: 4,
367 compatibilityOverrides: [{
368 type: "incompatible",
369 minVersion: 0.1,
370 maxVersion: 0.2,
371 appID: "xpcshell@tests.mozilla.org",
372 appMinVersion: 3.0,
373 appMaxVersion: 4.0
374 }, {
375 type: "incompatible",
376 minVersion: 0.2,
377 maxVersion: 0.3,
378 appID: "xpcshell@tests.mozilla.org",
379 appMinVersion: 5.0,
380 appMaxVersion: 6.0
381 }]
382 }, {
383 id: ADDON_IDS[1],
384 type: "theme",
385 name: "XPI Add-on 2",
386 version: "1.2",
387 sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec,
388 icons: {}
389 }, {
390 id: ADDON_IDS[2],
391 type: "theme",
392 name: "XPI Add-on 3",
393 version: "1.3",
394 get iconURL () {
395 return get_subfile_uri(ADDON_IDS[2], "icon.png");
396 },
397 get icons () {
398 return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") };
399 },
400 screenshots: [{ get url () { return get_subfile_uri(ADDON_IDS[2], "preview.png"); } }],
401 sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec
402 }];
405 /*
406 * Trigger an AddonManager background update check
407 *
408 * @return Promise{null}
409 * Resolves when the background update notification is received
410 */
411 function trigger_background_update() {
412 return new Promise((resolve, reject) => {
413 Services.obs.addObserver({
414 observe: function(aSubject, aTopic, aData) {
415 do_print("Observed " + aTopic);
416 Services.obs.removeObserver(this, "addons-background-update-complete");
417 resolve();
418 }
419 }, "addons-background-update-complete", false);
421 gInternalManager.notify(null);
422 });
423 }
425 let gDBFile = gProfD.clone();
426 gDBFile.append(FILE_DATABASE);
428 /*
429 * Check the actual add-on results against the expected add-on results
430 *
431 * @param aActualAddons
432 * The array of actual add-ons to check
433 * @param aExpectedAddons
434 * The array of expected add-ons to check against
435 * @param aFromRepository
436 * An optional boolean representing if the add-ons are from
437 * the repository
438 */
439 function check_results(aActualAddons, aExpectedAddons, aFromRepository) {
440 aFromRepository = !!aFromRepository;
442 do_check_addons(aActualAddons, aExpectedAddons, ADDON_PROPERTIES);
444 // Separately test size and updateDate (they should only be equal to the
445 // REPOSITORY values if they are from the repository)
446 aActualAddons.forEach(function(aActualAddon) {
447 if (aActualAddon.size)
448 do_check_eq(aActualAddon.size === REPOSITORY_SIZE, aFromRepository);
450 if (aActualAddon.updateDate) {
451 let time = aActualAddon.updateDate.getTime();
452 do_check_eq(time === 1000 * REPOSITORY_UPDATEDATE, aFromRepository);
453 }
454 });
455 }
457 /*
458 * Check the add-ons in the cache. This function also tests
459 * AddonRepository.getCachedAddonByID()
460 *
461 * @param aExpectedToFind
462 * An array of booleans representing which REPOSITORY_ADDONS are
463 * expected to be found in the cache
464 * @param aExpectedImmediately
465 * A boolean representing if results from the cache are expected
466 * immediately. Results are not immediate if the cache has not been
467 * initialized yet.
468 * @return Promise{null}
469 * Resolves once the checks are complete
470 */
471 function check_cache(aExpectedToFind, aExpectedImmediately) {
472 do_check_eq(aExpectedToFind.length, REPOSITORY_ADDONS.length);
474 let lookups = [];
476 for (let i = 0 ; i < REPOSITORY_ADDONS.length ; i++) {
477 lookups.push(new Promise((resolve, reject) => {
478 let immediatelyFound = true;
479 let expected = aExpectedToFind[i] ? REPOSITORY_ADDONS[i] : null;
480 // can't Promise-wrap this because we're also testing whether the callback is
481 // sync or async
482 AddonRepository.getCachedAddonByID(REPOSITORY_ADDONS[i].id, function(aAddon) {
483 do_check_eq(immediatelyFound, aExpectedImmediately);
484 if (expected == null)
485 do_check_eq(aAddon, null);
486 else
487 check_results([aAddon], [expected], true);
488 resolve();
489 });
490 immediatelyFound = false;
491 }));
492 }
493 return Promise.all(lookups);
494 }
496 /*
497 * Task to check an initialized cache by checking the cache, then restarting the
498 * manager, and checking the cache. This checks that the cache is consistent
499 * across manager restarts.
500 *
501 * @param aExpectedToFind
502 * An array of booleans representing which REPOSITORY_ADDONS are
503 * expected to be found in the cache
504 */
505 function* check_initialized_cache(aExpectedToFind) {
506 yield check_cache(aExpectedToFind, true);
507 yield promiseRestartManager();
509 // If cache is disabled, then expect results immediately
510 let cacheEnabled = Services.prefs.getBoolPref(PREF_GETADDONS_CACHE_ENABLED);
511 yield check_cache(aExpectedToFind, !cacheEnabled);
512 }
514 function run_test() {
515 run_next_test();
516 }
518 add_task(function* setup() {
519 // Setup for test
520 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
522 startupManager();
524 // Install XPI add-ons
525 yield promiseInstallAllFiles(ADDON_FILES);
526 yield promiseRestartManager();
528 gServer = new HttpServer();
529 gServer.registerDirectory("/data/", do_get_file("data"));
530 gServer.start(PORT);
531 });
533 // Tests AddonRepository.cacheEnabled
534 add_task(function* run_test_1() {
535 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
536 do_check_false(AddonRepository.cacheEnabled);
537 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
538 do_check_true(AddonRepository.cacheEnabled);
539 });
541 // Tests that the cache and database begin as empty
542 add_task(function* run_test_2() {
543 do_check_false(gDBFile.exists());
544 yield check_cache([false, false, false], false);
545 yield AddonRepository.flush();
546 });
548 // Tests repopulateCache when the search fails
549 add_task(function* run_test_3() {
550 do_check_true(gDBFile.exists());
551 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
552 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_FAILED);
554 yield new Promise((resolve, reject) =>
555 AddonRepository.repopulateCache(ADDON_IDS, resolve));
556 yield check_initialized_cache([false, false, false]);
557 });
559 // Tests repopulateCache when search returns no results
560 add_task(function* run_test_4() {
561 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_EMPTY);
563 yield new Promise((resolve, reject) =>
564 AddonRepository.repopulateCache(ADDON_IDS, resolve));
565 yield check_initialized_cache([false, false, false]);
566 });
568 // Tests repopulateCache when search returns results
569 add_task(function* run_test_5() {
570 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
572 yield new Promise((resolve, reject) =>
573 AddonRepository.repopulateCache(ADDON_IDS, resolve));
574 yield check_initialized_cache([true, true, true]);
575 });
577 // Tests repopulateCache when caching is disabled for a single add-on
578 add_task(function* run_test_5_1() {
579 Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, false);
581 yield new Promise((resolve, reject) =>
582 AddonRepository.repopulateCache(ADDON_IDS, resolve));
584 // Reset pref for next test
585 Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, true);
587 yield check_initialized_cache([false, true, true]);
588 });
590 // Tests repopulateCache when caching is disabled
591 add_task(function* run_test_6() {
592 do_check_true(gDBFile.exists());
593 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
595 yield new Promise((resolve, reject) =>
596 AddonRepository.repopulateCache(ADDON_IDS, resolve));
597 // Database should have been deleted
598 do_check_false(gDBFile.exists());
600 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
601 yield check_cache([false, false, false], false);
602 yield AddonRepository.flush();
603 });
605 // Tests cacheAddons when the search fails
606 add_task(function* run_test_7() {
607 do_check_true(gDBFile.exists());
608 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_FAILED);
610 yield new Promise((resolve, reject) =>
611 AddonRepository.cacheAddons(ADDON_IDS, resolve));
612 yield check_initialized_cache([false, false, false]);
613 });
615 // Tests cacheAddons when the search returns no results
616 add_task(function* run_test_8() {
617 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_EMPTY);
619 yield new Promise((resolve, reject) =>
620 AddonRepository.cacheAddons(ADDON_IDS, resolve));
621 yield check_initialized_cache([false, false, false]);
622 });
624 // Tests cacheAddons for a single add-on when search returns results
625 add_task(function* run_test_9() {
626 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
628 yield new Promise((resolve, reject) =>
629 AddonRepository.cacheAddons([ADDON_IDS[0]], resolve));
630 yield check_initialized_cache([true, false, false]);
631 });
633 // Tests cacheAddons when caching is disabled for a single add-on
634 add_task(function* run_test_9_1() {
635 Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, false);
637 yield new Promise((resolve, reject) =>
638 AddonRepository.cacheAddons(ADDON_IDS, resolve));
640 // Reset pref for next test
641 Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, true);
643 yield check_initialized_cache([true, false, true]);
644 });
646 // Tests cacheAddons for multiple add-ons, some already in the cache,
647 add_task(function* run_test_10() {
648 yield new Promise((resolve, reject) =>
649 AddonRepository.cacheAddons(ADDON_IDS, resolve));
650 yield check_initialized_cache([true, true, true]);
651 });
653 // Tests cacheAddons when caching is disabled
654 add_task(function* run_test_11() {
655 do_check_true(gDBFile.exists());
656 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
658 yield new Promise((resolve, reject) =>
659 AddonRepository.cacheAddons(ADDON_IDS, resolve));
660 do_check_true(gDBFile.exists());
662 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
663 yield check_initialized_cache([true, true, true]);
664 });
666 // Tests that XPI add-ons do not use any of the repository properties if
667 // caching is disabled, even if there are repository properties available
668 add_task(function* run_test_12() {
669 do_check_true(gDBFile.exists());
670 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
671 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
673 let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
674 check_results(aAddons, WITHOUT_CACHE);
675 });
677 // Tests that a background update with caching disabled deletes the add-ons
678 // database, and that XPI add-ons still do not use any of repository properties
679 add_task(function* run_test_13() {
680 do_check_true(gDBFile.exists());
681 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS_PERFORMANCE, GETADDONS_EMPTY);
683 yield trigger_background_update();
684 // Database should have been deleted
685 do_check_false(gDBFile.exists());
687 let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
688 check_results(aAddons, WITHOUT_CACHE);
689 });
691 // Tests that the XPI add-ons have the correct properties if caching is
692 // enabled but has no information
693 add_task(function* run_test_14() {
694 Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
696 yield trigger_background_update();
697 yield AddonRepository.flush();
698 do_check_true(gDBFile.exists());
700 let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
701 check_results(aAddons, WITHOUT_CACHE);
702 });
704 // Tests that the XPI add-ons correctly use the repository properties when
705 // caching is enabled and the repository information is available
706 add_task(function* run_test_15() {
707 Services.prefs.setCharPref(PREF_GETADDONS_BYIDS_PERFORMANCE, GETADDONS_RESULTS);
709 yield trigger_background_update();
710 let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
711 check_results(aAddons, WITH_CACHE);
712 });
714 // Tests that restarting the manager does not change the checked properties
715 // on the XPI add-ons (repository properties still exist and are still properly
716 // used)
717 add_task(function* run_test_16() {
718 yield promiseRestartManager();
720 let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
721 check_results(aAddons, WITH_CACHE);
722 });
724 // Tests that setting a list of types to cache works
725 add_task(function* run_test_17() {
726 Services.prefs.setCharPref(PREF_GETADDONS_CACHE_TYPES, "foo,bar,extension,baz");
728 yield trigger_background_update();
729 let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
730 check_results(aAddons, WITH_EXTENSION_CACHE);
731 });
733 add_task(function* end_test() {
734 yield new Promise((resolve, reject) => gServer.stop(resolve));
735 });