toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository.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 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 var gServer = new HttpServer();
michael@0 11 gServer.start(-1);
michael@0 12
michael@0 13 const PREF_GETADDONS_BROWSEADDONS = "extensions.getAddons.browseAddons";
michael@0 14 const PREF_GETADDONS_BROWSERECOMMENDED = "extensions.getAddons.recommended.browseURL";
michael@0 15 const PREF_GETADDONS_GETRECOMMENDED = "extensions.getAddons.recommended.url";
michael@0 16 const PREF_GETADDONS_BROWSESEARCHRESULTS = "extensions.getAddons.search.browseURL";
michael@0 17 const PREF_GETADDONS_GETSEARCHRESULTS = "extensions.getAddons.search.url";
michael@0 18
michael@0 19 const PORT = gServer.identity.primaryPort;
michael@0 20 const BASE_URL = "http://localhost:" + PORT;
michael@0 21 const DEFAULT_URL = "about:blank";
michael@0 22
michael@0 23 gPort = PORT;
michael@0 24
michael@0 25 // Path to source URI of installed add-on
michael@0 26 const INSTALL_URL1 = "/addons/test_AddonRepository_1.xpi";
michael@0 27 // Path to source URI of installing add-on
michael@0 28 const INSTALL_URL2 = "/addons/test_AddonRepository_2.xpi";
michael@0 29 // Path to source URI of non-active add-on (state = STATE_AVAILABLE)
michael@0 30 const INSTALL_URL3 = "/addons/test_AddonRepository_3.xpi";
michael@0 31
michael@0 32 // Properties of an individual add-on that should be checked
michael@0 33 // Note: name is checked separately
michael@0 34 var ADDON_PROPERTIES = ["id", "type", "version", "creator", "developers",
michael@0 35 "description", "fullDescription", "developerComments",
michael@0 36 "eula", "iconURL", "icons", "screenshots", "homepageURL",
michael@0 37 "supportURL", "contributionURL", "contributionAmount",
michael@0 38 "averageRating", "reviewCount", "reviewURL",
michael@0 39 "totalDownloads", "weeklyDownloads", "dailyUsers",
michael@0 40 "sourceURI", "repositoryStatus", "size", "updateDate",
michael@0 41 "purchaseURL", "purchaseAmount", "purchaseDisplayAmount",
michael@0 42 "compatibilityOverrides"];
michael@0 43
michael@0 44 // Results of getAddonsByIDs
michael@0 45 var GET_RESULTS = [{
michael@0 46 id: "test1@tests.mozilla.org",
michael@0 47 type: "extension",
michael@0 48 version: "1.1",
michael@0 49 creator: {
michael@0 50 name: "Test Creator 1",
michael@0 51 url: BASE_URL + "/creator1.html"
michael@0 52 },
michael@0 53 developers: [{
michael@0 54 name: "Test Developer 1",
michael@0 55 url: BASE_URL + "/developer1.html"
michael@0 56 }],
michael@0 57 description: "Test Summary 1",
michael@0 58 fullDescription: "Test Description 1",
michael@0 59 developerComments: "Test Developer Comments 1",
michael@0 60 eula: "Test EULA 1",
michael@0 61 iconURL: BASE_URL + "/icon1.png",
michael@0 62 icons: { "32": BASE_URL + "/icon1.png" },
michael@0 63 screenshots: [{
michael@0 64 url: BASE_URL + "/full1-1.png",
michael@0 65 width: 400,
michael@0 66 height: 300,
michael@0 67 thumbnailURL: BASE_URL + "/thumbnail1-1.png",
michael@0 68 thumbnailWidth: 200,
michael@0 69 thumbnailHeight: 150,
michael@0 70 caption: "Caption 1 - 1"
michael@0 71 }, {
michael@0 72 url: BASE_URL + "/full2-1.png",
michael@0 73 thumbnailURL: BASE_URL + "/thumbnail2-1.png",
michael@0 74 caption: "Caption 2 - 1"
michael@0 75 }],
michael@0 76 homepageURL: BASE_URL + "/learnmore1.html",
michael@0 77 learnmoreURL: BASE_URL + "/learnmore1.html",
michael@0 78 supportURL: BASE_URL + "/support1.html",
michael@0 79 contributionURL: BASE_URL + "/meetDevelopers1.html",
michael@0 80 contributionAmount: "$11.11",
michael@0 81 averageRating: 4,
michael@0 82 reviewCount: 1111,
michael@0 83 reviewURL: BASE_URL + "/review1.html",
michael@0 84 totalDownloads: 2222,
michael@0 85 weeklyDownloads: 3333,
michael@0 86 dailyUsers: 4444,
michael@0 87 sourceURI: BASE_URL + INSTALL_URL2,
michael@0 88 repositoryStatus: 8,
michael@0 89 size: 5555,
michael@0 90 updateDate: new Date(1265033045000),
michael@0 91 compatibilityOverrides: [{
michael@0 92 type: "incompatible",
michael@0 93 minVersion: 0.1,
michael@0 94 maxVersion: 0.2,
michael@0 95 appID: "xpcshell@tests.mozilla.org",
michael@0 96 appMinVersion: 3.0,
michael@0 97 appMaxVersion: 4.0
michael@0 98 }, {
michael@0 99 type: "incompatible",
michael@0 100 minVersion: 0.2,
michael@0 101 maxVersion: 0.3,
michael@0 102 appID: "xpcshell@tests.mozilla.org",
michael@0 103 appMinVersion: 5.0,
michael@0 104 appMaxVersion: 6.0
michael@0 105 }]
michael@0 106 }, {
michael@0 107 id: "test_AddonRepository_1@tests.mozilla.org",
michael@0 108 version: "1.4",
michael@0 109 repositoryStatus: 9999,
michael@0 110 icons: {}
michael@0 111 }];
michael@0 112
michael@0 113 // Results of retrieveRecommendedAddons and searchAddons
michael@0 114 var SEARCH_RESULTS = [{
michael@0 115 id: "test1@tests.mozilla.org",
michael@0 116 type: "extension",
michael@0 117 version: "1.1",
michael@0 118 creator: {
michael@0 119 name: "Test Creator 1",
michael@0 120 url: BASE_URL + "/creator1.html"
michael@0 121 },
michael@0 122 repositoryStatus: 8,
michael@0 123 sourceURI: BASE_URL + "/test1.xpi",
michael@0 124 icons: {}
michael@0 125 }, {
michael@0 126 id: "test2@tests.mozilla.org",
michael@0 127 type: "extension",
michael@0 128 version: "1.2",
michael@0 129 creator: {
michael@0 130 name: "Test Creator 2",
michael@0 131 url: BASE_URL + "/creator2.html"
michael@0 132 },
michael@0 133 developers: [{
michael@0 134 name: "Test Developer 2",
michael@0 135 url: BASE_URL + "/developer2.html"
michael@0 136 }],
michael@0 137 description: "Test Summary 2\n\nparagraph",
michael@0 138 fullDescription: "Test Description 2\nnewline",
michael@0 139 developerComments: "Test Developer\nComments 2",
michael@0 140 eula: "Test EULA 2",
michael@0 141 iconURL: BASE_URL + "/icon2-32.png",
michael@0 142 icons: {
michael@0 143 "32": BASE_URL + "/icon2-32.png",
michael@0 144 "48": BASE_URL + "/icon2-48.png",
michael@0 145 "64": BASE_URL + "/icon2-64.png"
michael@0 146 },
michael@0 147 screenshots: [{
michael@0 148 url: BASE_URL + "/full1-2.png",
michael@0 149 thumbnailURL: BASE_URL + "/thumbnail1-2.png"
michael@0 150 }, {
michael@0 151 url: BASE_URL + "/full2-2.png",
michael@0 152 thumbnailURL: BASE_URL + "/thumbnail2-2.png",
michael@0 153 caption: "Caption 2"
michael@0 154 }],
michael@0 155 homepageURL: BASE_URL + "/learnmore2.html",
michael@0 156 supportURL: BASE_URL + "/support2.html",
michael@0 157 learnmoreURL: BASE_URL + "/learnmore2.html",
michael@0 158 contributionURL: BASE_URL + "/meetDevelopers2.html",
michael@0 159 contributionAmount: null,
michael@0 160 repositoryStatus: 4,
michael@0 161 sourceURI: BASE_URL + "/test2.xpi"
michael@0 162 }, {
michael@0 163 id: "test3@tests.mozilla.org",
michael@0 164 type: "theme",
michael@0 165 version: "1.3",
michael@0 166 creator: {
michael@0 167 name: "Test Creator 3",
michael@0 168 url: BASE_URL + "/creator3.html"
michael@0 169 },
michael@0 170 developers: [{
michael@0 171 name: "First Test Developer 3",
michael@0 172 url: BASE_URL + "/developer1-3.html"
michael@0 173 }, {
michael@0 174 name: "Second Test Developer 3",
michael@0 175 url: BASE_URL + "/developer2-3.html"
michael@0 176 }],
michael@0 177 description: "Test Summary 3",
michael@0 178 fullDescription: "Test Description 3\n\n List item 1\n List item 2",
michael@0 179 developerComments: "Test Developer Comments 3",
michael@0 180 eula: "Test EULA 3",
michael@0 181 iconURL: BASE_URL + "/icon3.png",
michael@0 182 icons: { "32": BASE_URL + "/icon3.png" },
michael@0 183 screenshots: [{
michael@0 184 url: BASE_URL + "/full1-3.png",
michael@0 185 thumbnailURL: BASE_URL + "/thumbnail1-3.png",
michael@0 186 caption: "Caption 1 - 3"
michael@0 187 }, {
michael@0 188 url: BASE_URL + "/full2-3.png",
michael@0 189 caption: "Caption 2 - 3"
michael@0 190 }, {
michael@0 191 url: BASE_URL + "/full3-3.png",
michael@0 192 thumbnailURL: BASE_URL + "/thumbnail3-3.png",
michael@0 193 caption: "Caption 3 - 3"
michael@0 194 }],
michael@0 195 homepageURL: BASE_URL + "/homepage3.html",
michael@0 196 supportURL: BASE_URL + "/support3.html",
michael@0 197 learnmoreURL: BASE_URL + "/learnmore3.html",
michael@0 198 contributionURL: BASE_URL + "/meetDevelopers3.html",
michael@0 199 contributionAmount: "$11.11",
michael@0 200 averageRating: 2,
michael@0 201 reviewCount: 1111,
michael@0 202 reviewURL: BASE_URL + "/review3.html",
michael@0 203 totalDownloads: 2222,
michael@0 204 weeklyDownloads: 3333,
michael@0 205 dailyUsers: 4444,
michael@0 206 sourceURI: BASE_URL + "/test3.xpi",
michael@0 207 repositoryStatus: 8,
michael@0 208 size: 5555,
michael@0 209 updateDate: new Date(1265033045000),
michael@0 210
michael@0 211 }, {
michael@0 212 id: "purchase1@tests.mozilla.org",
michael@0 213 type: "extension",
michael@0 214 version: "2.0",
michael@0 215 creator: {
michael@0 216 name: "Test Creator - Last Passing",
michael@0 217 url: BASE_URL + "/creatorLastPassing.html"
michael@0 218 },
michael@0 219 averageRating: 5,
michael@0 220 repositoryStatus: 4,
michael@0 221 purchaseURL: "http://localhost:" + PORT + "/purchaseURL1",
michael@0 222 purchaseAmount: 5,
michael@0 223 purchaseDisplayAmount: "$5",
michael@0 224 icons: {}
michael@0 225 }, {
michael@0 226 id: "purchase2@tests.mozilla.org",
michael@0 227 type: "extension",
michael@0 228 version: "2.0",
michael@0 229 creator: {
michael@0 230 name: "Test Creator - Last Passing",
michael@0 231 url: BASE_URL + "/creatorLastPassing.html"
michael@0 232 },
michael@0 233 averageRating: 5,
michael@0 234 repositoryStatus: 4,
michael@0 235 purchaseURL: "http://localhost:" + PORT + "/purchaseURL2",
michael@0 236 purchaseAmount: 10,
michael@0 237 purchaseDisplayAmount: "$10",
michael@0 238 icons: {}
michael@0 239 }, {
michael@0 240 id: "test-lastPassing@tests.mozilla.org",
michael@0 241 type: "extension",
michael@0 242 version: "2.0",
michael@0 243 creator: {
michael@0 244 name: "Test Creator - Last Passing",
michael@0 245 url: BASE_URL + "/creatorLastPassing.html"
michael@0 246 },
michael@0 247 averageRating: 5,
michael@0 248 repositoryStatus: 4,
michael@0 249 sourceURI: BASE_URL + "/addons/test_AddonRepository_3.xpi",
michael@0 250 icons: {}
michael@0 251 }];
michael@0 252
michael@0 253 const TOTAL_RESULTS = 1111;
michael@0 254 const MAX_RESULTS = SEARCH_RESULTS.length;
michael@0 255
michael@0 256 // Used to differentiate between testing that a search success
michael@0 257 // or a search failure for retrieveRecommendedAddons and searchAddons
michael@0 258 const FAILED_MAX_RESULTS = 9999;
michael@0 259
michael@0 260 // Values for testing AddonRepository.getAddonsByIDs()
michael@0 261 var GET_TEST = {
michael@0 262 preference: PREF_GETADDONS_BYIDS,
michael@0 263 preferenceValue: BASE_URL + "/%OS%/%VERSION%/%API_VERSION%/" +
michael@0 264 "%API_VERSION%/%IDS%",
michael@0 265 failedIDs: ["test1@tests.mozilla.org"],
michael@0 266 failedURL: "/XPCShell/1/1.5/1.5/test1%40tests.mozilla.org",
michael@0 267 successfulIDs: ["test1@tests.mozilla.org",
michael@0 268 "{00000000-1111-2222-3333-444444444444}",
michael@0 269 "test_AddonRepository_1@tests.mozilla.org"],
michael@0 270 successfulURL: "/XPCShell/1/1.5/1.5/test1%40tests.mozilla.org," +
michael@0 271 "%7B00000000-1111-2222-3333-444444444444%7D," +
michael@0 272 "test_AddonRepository_1%40tests.mozilla.org"
michael@0 273 };
michael@0 274
michael@0 275 // Values for testing AddonRepository.retrieveRecommendedAddons()
michael@0 276 var RECOMMENDED_TEST = {
michael@0 277 preference: PREF_GETADDONS_GETRECOMMENDED,
michael@0 278 preferenceValue: BASE_URL + "/%OS%/%VERSION%/%API_VERSION%/" +
michael@0 279 "%API_VERSION%/%MAX_RESULTS%",
michael@0 280 failedURL: "/XPCShell/1/1.5/1.5/" + (2 * FAILED_MAX_RESULTS),
michael@0 281 successfulURL: "/XPCShell/1/1.5/1.5/" + (2 * MAX_RESULTS)
michael@0 282 };
michael@0 283
michael@0 284 // Values for testing AddonRepository.searchAddons()
michael@0 285 var SEARCH_TEST = {
michael@0 286 searchTerms: "odd=search:with&weird\"characters",
michael@0 287 preference: PREF_GETADDONS_GETSEARCHRESULTS,
michael@0 288 preferenceValue: BASE_URL + "/%OS%/%VERSION%/%API_VERSION%/" +
michael@0 289 "%API_VERSION%/%MAX_RESULTS%/%TERMS%",
michael@0 290 failedURL: "/XPCShell/1/1.5/1.5/" + (2 * FAILED_MAX_RESULTS) +
michael@0 291 "/odd%3Dsearch%3Awith%26weird%22characters",
michael@0 292 successfulURL: "/XPCShell/1/1.5/1.5/" + (2 * MAX_RESULTS) +
michael@0 293 "/odd%3Dsearch%3Awith%26weird%22characters"
michael@0 294 };
michael@0 295
michael@0 296 // Test that actual results and expected results are equal
michael@0 297 function check_results(aActualAddons, aExpectedAddons, aAddonCount, aInstallNull) {
michael@0 298 do_check_false(AddonRepository.isSearching);
michael@0 299
michael@0 300 do_check_eq(aActualAddons.length, aAddonCount);
michael@0 301 do_check_addons(aActualAddons, aExpectedAddons, ADDON_PROPERTIES);
michael@0 302
michael@0 303 // Additional tests
michael@0 304 aActualAddons.forEach(function check_each_addon(aActualAddon) {
michael@0 305 // Separately check name so better messages are output when test fails
michael@0 306 if (aActualAddon.name == "FAIL")
michael@0 307 do_throw(aActualAddon.id + " - " + aActualAddon.description);
michael@0 308 if (aActualAddon.name != "PASS")
michael@0 309 do_throw(aActualAddon.id + " - " + "invalid add-on name " + aActualAddon.name);
michael@0 310
michael@0 311 do_check_eq(aActualAddon.install == null, !!aInstallNull || !aActualAddon.sourceURI);
michael@0 312
michael@0 313 // Check that sourceURI property consistent within actual addon
michael@0 314 if (aActualAddon.install)
michael@0 315 do_check_eq(aActualAddon.install.sourceURI.spec, aActualAddon.sourceURI.spec);
michael@0 316 });
michael@0 317 }
michael@0 318
michael@0 319 // Complete a search, also testing cancelSearch() and isSearching
michael@0 320 function complete_search(aSearch, aSearchCallback) {
michael@0 321 var failCallback = {
michael@0 322 searchSucceeded: function(addons, length, total) {
michael@0 323 do_throw("failCallback.searchSucceeded should not be called");
michael@0 324 end_test();
michael@0 325 },
michael@0 326
michael@0 327 searchFailed: function() {
michael@0 328 do_throw("failCallback.searchFailed should not be called");
michael@0 329 end_test();
michael@0 330 }
michael@0 331 };
michael@0 332
michael@0 333 var callbackCalled = false;
michael@0 334 var testCallback = {
michael@0 335 searchSucceeded: function(addons, length, total) {
michael@0 336 do_throw("testCallback.searchSucceeded should not be called");
michael@0 337 end_test();
michael@0 338 },
michael@0 339
michael@0 340 searchFailed: function() {
michael@0 341 callbackCalled = true;
michael@0 342 }
michael@0 343 };
michael@0 344
michael@0 345 // Should fail because cancelled it immediately
michael@0 346 aSearch(failCallback);
michael@0 347 do_check_true(AddonRepository.isSearching);
michael@0 348 AddonRepository.cancelSearch();
michael@0 349 do_check_false(AddonRepository.isSearching);
michael@0 350
michael@0 351 aSearch(aSearchCallback);
michael@0 352 do_check_true(AddonRepository.isSearching);
michael@0 353
michael@0 354 // searchFailed should be called immediately because already searching
michael@0 355 aSearch(testCallback);
michael@0 356 do_check_true(callbackCalled);
michael@0 357 do_check_true(AddonRepository.isSearching);
michael@0 358 }
michael@0 359
michael@0 360
michael@0 361 function run_test() {
michael@0 362 // Setup for test
michael@0 363 do_test_pending();
michael@0 364 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
michael@0 365
michael@0 366 startupManager();
michael@0 367
michael@0 368 // Install an add-on so can check that it isn't returned in the results
michael@0 369 installAllFiles([do_get_addon("test_AddonRepository_1")], function addon_1_install_callback() {
michael@0 370 restartManager();
michael@0 371
michael@0 372 // Register other add-on XPI files
michael@0 373 gServer.registerFile(INSTALL_URL2,
michael@0 374 do_get_addon("test_AddonRepository_2"));
michael@0 375 gServer.registerFile(INSTALL_URL3,
michael@0 376 do_get_addon("test_AddonRepository_3"));
michael@0 377
michael@0 378 // Register files used to test search failure
michael@0 379 mapUrlToFile(GET_TEST.failedURL,
michael@0 380 do_get_file("data/test_AddonRepository_failed.xml"),
michael@0 381 gServer);
michael@0 382 mapUrlToFile(RECOMMENDED_TEST.failedURL,
michael@0 383 do_get_file("data/test_AddonRepository_failed.xml"),
michael@0 384 gServer);
michael@0 385 mapUrlToFile(SEARCH_TEST.failedURL,
michael@0 386 do_get_file("data/test_AddonRepository_failed.xml"),
michael@0 387 gServer);
michael@0 388
michael@0 389 // Register files used to test search success
michael@0 390 mapUrlToFile(GET_TEST.successfulURL,
michael@0 391 do_get_file("data/test_AddonRepository_getAddonsByIDs.xml"),
michael@0 392 gServer);
michael@0 393 mapUrlToFile(RECOMMENDED_TEST.successfulURL,
michael@0 394 do_get_file("data/test_AddonRepository.xml"),
michael@0 395 gServer);
michael@0 396 mapUrlToFile(SEARCH_TEST.successfulURL,
michael@0 397 do_get_file("data/test_AddonRepository.xml"),
michael@0 398 gServer);
michael@0 399
michael@0 400 // Create an active AddonInstall so can check that it isn't returned in the results
michael@0 401 AddonManager.getInstallForURL(BASE_URL + INSTALL_URL2, function addon_2_get(aInstall) {
michael@0 402 try {
michael@0 403 aInstall.install();
michael@0 404 }
michael@0 405 catch(e) {
michael@0 406 do_print("Failed to install add-on " + aInstall.sourceURI.spec);
michael@0 407 do_report_unexpected_exception(e);
michael@0 408 }
michael@0 409
michael@0 410 // Create a non-active AddonInstall so can check that it is returned in the results
michael@0 411 AddonManager.getInstallForURL(BASE_URL + INSTALL_URL3,
michael@0 412 run_test_1, "application/x-xpinstall");
michael@0 413 }, "application/x-xpinstall");
michael@0 414 });
michael@0 415 }
michael@0 416
michael@0 417 function end_test() {
michael@0 418 let testDir = gProfD.clone();
michael@0 419 testDir.append("extensions");
michael@0 420 testDir.append("staged");
michael@0 421 gServer.stop(function() {
michael@0 422 function loop() {
michael@0 423 if (!testDir.exists()) {
michael@0 424 do_print("Staged directory has been cleaned up");
michael@0 425 do_test_finished();
michael@0 426 }
michael@0 427 do_print("Waiting 1 second until cleanup is complete");
michael@0 428 do_timeout(1000, loop);
michael@0 429 }
michael@0 430 loop();
michael@0 431 });
michael@0 432 }
michael@0 433
michael@0 434 // Tests homepageURL, getRecommendedURL() and getSearchURL()
michael@0 435 function run_test_1() {
michael@0 436 function check_urls(aPreference, aGetURL, aTests) {
michael@0 437 aTests.forEach(function(aTest) {
michael@0 438 Services.prefs.setCharPref(aPreference, aTest.preferenceValue);
michael@0 439 do_check_eq(aGetURL(aTest), aTest.expectedURL);
michael@0 440 });
michael@0 441 }
michael@0 442
michael@0 443 var urlTests = [{
michael@0 444 preferenceValue: BASE_URL,
michael@0 445 expectedURL: BASE_URL
michael@0 446 }, {
michael@0 447 preferenceValue: BASE_URL + "/%OS%/%VERSION%",
michael@0 448 expectedURL: BASE_URL + "/XPCShell/1"
michael@0 449 }];
michael@0 450
michael@0 451 // Extra tests for AddonRepository.getSearchURL();
michael@0 452 var searchURLTests = [{
michael@0 453 searchTerms: "test",
michael@0 454 preferenceValue: BASE_URL + "/search?q=%TERMS%",
michael@0 455 expectedURL: BASE_URL + "/search?q=test"
michael@0 456 }, {
michael@0 457 searchTerms: "test search",
michael@0 458 preferenceValue: BASE_URL + "/%TERMS%",
michael@0 459 expectedURL: BASE_URL + "/test%20search"
michael@0 460 }, {
michael@0 461 searchTerms: "odd=search:with&weird\"characters",
michael@0 462 preferenceValue: BASE_URL + "/%TERMS%",
michael@0 463 expectedURL: BASE_URL + "/odd%3Dsearch%3Awith%26weird%22characters"
michael@0 464 }];
michael@0 465
michael@0 466 // Setup tests for homepageURL, getRecommendedURL() and getSearchURL()
michael@0 467 var tests = [{
michael@0 468 initiallyUndefined: true,
michael@0 469 preference: PREF_GETADDONS_BROWSEADDONS,
michael@0 470 urlTests: urlTests,
michael@0 471 getURL: function() AddonRepository.homepageURL
michael@0 472 }, {
michael@0 473 initiallyUndefined: true,
michael@0 474 preference: PREF_GETADDONS_BROWSERECOMMENDED,
michael@0 475 urlTests: urlTests,
michael@0 476 getURL: function() AddonRepository.getRecommendedURL()
michael@0 477 }, {
michael@0 478 initiallyUndefined: false,
michael@0 479 preference: PREF_GETADDONS_BROWSESEARCHRESULTS,
michael@0 480 urlTests: urlTests.concat(searchURLTests),
michael@0 481 getURL: function getSearchURL(aTest) {
michael@0 482 var searchTerms = aTest && aTest.searchTerms ? aTest.searchTerms
michael@0 483 : "unused terms";
michael@0 484 return AddonRepository.getSearchURL(searchTerms);
michael@0 485 }
michael@0 486 }];
michael@0 487
michael@0 488 tests.forEach(function url_test(aTest) {
michael@0 489 if (aTest.initiallyUndefined) {
michael@0 490 // Preference is not defined by default
michael@0 491 do_check_eq(Services.prefs.getPrefType(aTest.preference),
michael@0 492 Services.prefs.PREF_INVALID);
michael@0 493 do_check_eq(aTest.getURL(), DEFAULT_URL);
michael@0 494 }
michael@0 495
michael@0 496 check_urls(aTest.preference, aTest.getURL, aTest.urlTests);
michael@0 497 });
michael@0 498
michael@0 499 run_test_getAddonsByID_fails();
michael@0 500 }
michael@0 501
michael@0 502 // Tests failure of AddonRepository.getAddonsByIDs()
michael@0 503 function run_test_getAddonsByID_fails() {
michael@0 504 Services.prefs.setCharPref(GET_TEST.preference, GET_TEST.preferenceValue);
michael@0 505 var callback = {
michael@0 506 searchSucceeded: function(aAddonsList, aAddonCount, aTotalResults) {
michael@0 507 do_throw("searchAddons should not have succeeded");
michael@0 508 end_test();
michael@0 509 },
michael@0 510
michael@0 511 searchFailed: function() {
michael@0 512 do_check_false(AddonRepository.isSearching);
michael@0 513 run_test_getAddonsByID_succeeds();
michael@0 514 }
michael@0 515 };
michael@0 516
michael@0 517 complete_search(function complete_search_fail_callback(aCallback) {
michael@0 518 AddonRepository.getAddonsByIDs(GET_TEST.failedIDs, aCallback);
michael@0 519 }, callback);
michael@0 520 }
michael@0 521
michael@0 522 // Tests success of AddonRepository.getAddonsByIDs()
michael@0 523 function run_test_getAddonsByID_succeeds() {
michael@0 524 var callback = {
michael@0 525 searchSucceeded: function(aAddonsList, aAddonCount, aTotalResults) {
michael@0 526 do_check_eq(aTotalResults, -1);
michael@0 527 check_results(aAddonsList, GET_RESULTS, aAddonCount, true);
michael@0 528 run_test_retrieveRecommended_fails();
michael@0 529 },
michael@0 530
michael@0 531 searchFailed: function() {
michael@0 532 do_throw("searchAddons should not have failed");
michael@0 533 end_test();
michael@0 534 }
michael@0 535 };
michael@0 536
michael@0 537 complete_search(function complete_search_succeed_callback(aCallback) {
michael@0 538 AddonRepository.getAddonsByIDs(GET_TEST.successfulIDs, aCallback);
michael@0 539 }, callback);
michael@0 540 }
michael@0 541
michael@0 542 // Tests failure of AddonRepository.retrieveRecommendedAddons()
michael@0 543 function run_test_retrieveRecommended_fails() {
michael@0 544 Services.prefs.setCharPref(RECOMMENDED_TEST.preference,
michael@0 545 RECOMMENDED_TEST.preferenceValue);
michael@0 546 var callback = {
michael@0 547 searchSucceeded: function(aAddonsList, aAddonCount, aTotalResults) {
michael@0 548 do_throw("retrieveRecommendedAddons should not have succeeded");
michael@0 549 end_test();
michael@0 550 },
michael@0 551
michael@0 552 searchFailed: function() {
michael@0 553 do_check_false(AddonRepository.isSearching);
michael@0 554 run_test_retrieveRecommended_succeed();
michael@0 555 }
michael@0 556 };
michael@0 557
michael@0 558 complete_search(function retrieveRecommended_failing_callback(aCallback) {
michael@0 559 AddonRepository.retrieveRecommendedAddons(FAILED_MAX_RESULTS, aCallback);
michael@0 560 }, callback);
michael@0 561 }
michael@0 562
michael@0 563 // Tests success of AddonRepository.retrieveRecommendedAddons()
michael@0 564 function run_test_retrieveRecommended_succeed() {
michael@0 565 var callback = {
michael@0 566 searchSucceeded: function(aAddonsList, aAddonCount, aTotalResults) {
michael@0 567 do_check_eq(aTotalResults, -1);
michael@0 568 check_results(aAddonsList, SEARCH_RESULTS, aAddonCount);
michael@0 569 run_test_searchAddons_fails();
michael@0 570 },
michael@0 571
michael@0 572 searchFailed: function() {
michael@0 573 do_throw("retrieveRecommendedAddons should not have failed");
michael@0 574 end_test();
michael@0 575 }
michael@0 576 };
michael@0 577
michael@0 578 complete_search(function retrieveRecommended_succeed_callback(aCallback) {
michael@0 579 AddonRepository.retrieveRecommendedAddons(MAX_RESULTS, aCallback);
michael@0 580 }, callback);
michael@0 581 }
michael@0 582
michael@0 583 // Tests failure of AddonRepository.searchAddons()
michael@0 584 function run_test_searchAddons_fails() {
michael@0 585 Services.prefs.setCharPref(SEARCH_TEST.preference, SEARCH_TEST.preferenceValue);
michael@0 586 var callback = {
michael@0 587 searchSucceeded: function(aAddonsList, aAddonCount, aTotalResults) {
michael@0 588 do_throw("searchAddons should not have succeeded");
michael@0 589 end_test();
michael@0 590 },
michael@0 591
michael@0 592 searchFailed: function() {
michael@0 593 do_check_false(AddonRepository.isSearching);
michael@0 594 run_test_searchAddons_succeeds();
michael@0 595 }
michael@0 596 };
michael@0 597
michael@0 598 complete_search(function(aCallback) {
michael@0 599 var searchTerms = SEARCH_TEST.searchTerms;
michael@0 600 AddonRepository.searchAddons(searchTerms, FAILED_MAX_RESULTS, aCallback);
michael@0 601 }, callback);
michael@0 602 }
michael@0 603
michael@0 604 // Tests success of AddonRepository.searchAddons()
michael@0 605 function run_test_searchAddons_succeeds() {
michael@0 606 var callback = {
michael@0 607 searchSucceeded: function(aAddonsList, aAddonCount, aTotalResults) {
michael@0 608 do_check_eq(aTotalResults, TOTAL_RESULTS);
michael@0 609 check_results(aAddonsList, SEARCH_RESULTS, aAddonCount);
michael@0 610 end_test();
michael@0 611 },
michael@0 612
michael@0 613 searchFailed: function() {
michael@0 614 do_throw("searchAddons should not have failed");
michael@0 615 end_test();
michael@0 616 }
michael@0 617 };
michael@0 618
michael@0 619 complete_search(function(aCallback) {
michael@0 620 var searchTerms = SEARCH_TEST.searchTerms;
michael@0 621 AddonRepository.searchAddons(searchTerms, MAX_RESULTS, aCallback);
michael@0 622 }, callback);
michael@0 623 }
michael@0 624

mercurial