toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_cache.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial