michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Tests caching in AddonRepository.jsm michael@0: michael@0: Components.utils.import("resource://gre/modules/addons/AddonRepository.jsm"); michael@0: michael@0: Components.utils.import("resource://testing-common/httpd.js"); michael@0: let gServer; michael@0: michael@0: const PORT = 4444; michael@0: const BASE_URL = "http://localhost:" + PORT; michael@0: michael@0: const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled"; michael@0: const PREF_GETADDONS_CACHE_TYPES = "extensions.getAddons.cache.types"; michael@0: const GETADDONS_RESULTS = BASE_URL + "/data/test_AddonRepository_cache.xml"; michael@0: const GETADDONS_EMPTY = BASE_URL + "/data/test_AddonRepository_empty.xml"; michael@0: const GETADDONS_FAILED = BASE_URL + "/data/test_AddonRepository_failed.xml"; michael@0: michael@0: const FILE_DATABASE = "addons.json"; michael@0: const ADDON_NAMES = ["test_AddonRepository_1", michael@0: "test_AddonRepository_2", michael@0: "test_AddonRepository_3"]; michael@0: const ADDON_IDS = ADDON_NAMES.map(function(aName) aName + "@tests.mozilla.org"); michael@0: const ADDON_FILES = ADDON_NAMES.map(do_get_addon); michael@0: michael@0: const PREF_ADDON0_CACHE_ENABLED = "extensions." + ADDON_IDS[0] + ".getAddons.cache.enabled"; michael@0: const PREF_ADDON1_CACHE_ENABLED = "extensions." + ADDON_IDS[1] + ".getAddons.cache.enabled"; michael@0: michael@0: // Properties of an individual add-on that should be checked michael@0: // Note: size and updateDate are checked separately michael@0: const ADDON_PROPERTIES = ["id", "type", "name", "version", "creator", michael@0: "developers", "translators", "contributors", michael@0: "description", "fullDescription", michael@0: "developerComments", "eula", "iconURL", "icons", michael@0: "screenshots", "homepageURL", "supportURL", michael@0: "optionsURL", "aboutURL", "contributionURL", michael@0: "contributionAmount", "averageRating", "reviewCount", michael@0: "reviewURL", "totalDownloads", "weeklyDownloads", michael@0: "dailyUsers", "sourceURI", "repositoryStatus", michael@0: "compatibilityOverrides"]; michael@0: michael@0: // The size and updateDate properties are annoying to test for XPI add-ons. michael@0: // However, since we only care about whether the repository value vs. the michael@0: // XPI value is used, we can just test if the property value matches michael@0: // the repository value michael@0: const REPOSITORY_SIZE = 9; michael@0: const REPOSITORY_UPDATEDATE = 9; michael@0: michael@0: // Get the URI of a subfile locating directly in the folder of michael@0: // the add-on corresponding to the specified id michael@0: function get_subfile_uri(aId, aFilename) { michael@0: let file = gProfD.clone(); michael@0: file.append("extensions"); michael@0: return do_get_addon_root_uri(file, aId) + aFilename; michael@0: } michael@0: michael@0: michael@0: // Expected repository add-ons michael@0: const REPOSITORY_ADDONS = [{ michael@0: id: ADDON_IDS[0], michael@0: type: "extension", michael@0: name: "Repo Add-on 1", michael@0: version: "2.1", michael@0: creator: { michael@0: name: "Repo Add-on 1 - Creator", michael@0: url: BASE_URL + "/repo/1/creator.html" michael@0: }, michael@0: developers: [{ michael@0: name: "Repo Add-on 1 - First Developer", michael@0: url: BASE_URL + "/repo/1/firstDeveloper.html" michael@0: }, { michael@0: name: "Repo Add-on 1 - Second Developer", michael@0: url: BASE_URL + "/repo/1/secondDeveloper.html" michael@0: }], michael@0: description: "Repo Add-on 1 - Description\nSecond line", michael@0: fullDescription: "Repo Add-on 1 - Full Description & some extra", michael@0: developerComments: "Repo Add-on 1\nDeveloper Comments", michael@0: eula: "Repo Add-on 1 - EULA", michael@0: iconURL: BASE_URL + "/repo/1/icon.png", michael@0: icons: { "32": BASE_URL + "/repo/1/icon.png" }, michael@0: homepageURL: BASE_URL + "/repo/1/homepage.html", michael@0: supportURL: BASE_URL + "/repo/1/support.html", michael@0: contributionURL: BASE_URL + "/repo/1/meetDevelopers.html", michael@0: contributionAmount: "$11.11", michael@0: averageRating: 1, michael@0: reviewCount: 1111, michael@0: reviewURL: BASE_URL + "/repo/1/review.html", michael@0: totalDownloads: 2221, michael@0: weeklyDownloads: 3331, michael@0: dailyUsers: 4441, michael@0: sourceURI: BASE_URL + "/repo/1/install.xpi", michael@0: repositoryStatus: 4, michael@0: compatibilityOverrides: [{ michael@0: type: "incompatible", michael@0: minVersion: 0.1, michael@0: maxVersion: 0.2, michael@0: appID: "xpcshell@tests.mozilla.org", michael@0: appMinVersion: 3.0, michael@0: appMaxVersion: 4.0 michael@0: }, { michael@0: type: "incompatible", michael@0: minVersion: 0.2, michael@0: maxVersion: 0.3, michael@0: appID: "xpcshell@tests.mozilla.org", michael@0: appMinVersion: 5.0, michael@0: appMaxVersion: 6.0 michael@0: }] michael@0: }, { michael@0: id: ADDON_IDS[1], michael@0: type: "theme", michael@0: name: "Repo Add-on 2", michael@0: version: "2.2", michael@0: creator: { michael@0: name: "Repo Add-on 2 - Creator", michael@0: url: BASE_URL + "/repo/2/creator.html" michael@0: }, michael@0: developers: [{ michael@0: name: "Repo Add-on 2 - First Developer", michael@0: url: BASE_URL + "/repo/2/firstDeveloper.html" michael@0: }, { michael@0: name: "Repo Add-on 2 - Second Developer", michael@0: url: BASE_URL + "/repo/2/secondDeveloper.html" michael@0: }], michael@0: description: "Repo Add-on 2 - Description", michael@0: fullDescription: "Repo Add-on 2 - Full Description", michael@0: developerComments: "Repo Add-on 2 - Developer Comments", michael@0: eula: "Repo Add-on 2 - EULA", michael@0: iconURL: BASE_URL + "/repo/2/icon.png", michael@0: icons: { "32": BASE_URL + "/repo/2/icon.png" }, michael@0: screenshots: [{ michael@0: url: BASE_URL + "/repo/2/firstFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png", michael@0: caption: "Repo Add-on 2 - First Caption" michael@0: } , { michael@0: url: BASE_URL + "/repo/2/secondFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png", michael@0: caption: "Repo Add-on 2 - Second Caption" michael@0: }], michael@0: homepageURL: BASE_URL + "/repo/2/homepage.html", michael@0: supportURL: BASE_URL + "/repo/2/support.html", michael@0: contributionURL: BASE_URL + "/repo/2/meetDevelopers.html", michael@0: contributionAmount: null, michael@0: averageRating: 2, michael@0: reviewCount: 1112, michael@0: reviewURL: BASE_URL + "/repo/2/review.html", michael@0: totalDownloads: 2222, michael@0: weeklyDownloads: 3332, michael@0: dailyUsers: 4442, michael@0: sourceURI: BASE_URL + "/repo/2/install.xpi", michael@0: repositoryStatus: 9 michael@0: }, { michael@0: id: ADDON_IDS[2], michael@0: name: "Repo Add-on 3", michael@0: version: "2.3", michael@0: iconURL: BASE_URL + "/repo/3/icon.png", michael@0: icons: { "32": BASE_URL + "/repo/3/icon.png" }, michael@0: screenshots: [{ michael@0: url: BASE_URL + "/repo/3/firstFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png", michael@0: caption: "Repo Add-on 3 - First Caption" michael@0: } , { michael@0: url: BASE_URL + "/repo/3/secondFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png", michael@0: caption: "Repo Add-on 3 - Second Caption" michael@0: }] michael@0: }]; michael@0: michael@0: michael@0: // Expected add-ons when not using cache michael@0: const WITHOUT_CACHE = [{ michael@0: id: ADDON_IDS[0], michael@0: type: "extension", michael@0: name: "XPI Add-on 1", michael@0: version: "1.1", michael@0: creator: { name: "XPI Add-on 1 - Creator" }, michael@0: developers: [{ name: "XPI Add-on 1 - First Developer" }, michael@0: { name: "XPI Add-on 1 - Second Developer" }], michael@0: translators: [{ name: "XPI Add-on 1 - First Translator" }, michael@0: { name: "XPI Add-on 1 - Second Translator" }], michael@0: contributors: [{ name: "XPI Add-on 1 - First Contributor" }, michael@0: { name: "XPI Add-on 1 - Second Contributor" }], michael@0: description: "XPI Add-on 1 - Description", michael@0: iconURL: BASE_URL + "/xpi/1/icon.png", michael@0: icons: { "32": BASE_URL + "/xpi/1/icon.png" }, michael@0: homepageURL: BASE_URL + "/xpi/1/homepage.html", michael@0: optionsURL: BASE_URL + "/xpi/1/options.html", michael@0: aboutURL: BASE_URL + "/xpi/1/about.html", michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec michael@0: }, { michael@0: id: ADDON_IDS[1], michael@0: type: "theme", michael@0: name: "XPI Add-on 2", michael@0: version: "1.2", michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec, michael@0: icons: {} michael@0: }, { michael@0: id: ADDON_IDS[2], michael@0: type: "theme", michael@0: name: "XPI Add-on 3", michael@0: version: "1.3", michael@0: get iconURL () { michael@0: return get_subfile_uri(ADDON_IDS[2], "icon.png"); michael@0: }, michael@0: get icons () { michael@0: return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") }; michael@0: }, michael@0: screenshots: [{ get url () { return get_subfile_uri(ADDON_IDS[2], "preview.png"); } }], michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec michael@0: }]; michael@0: michael@0: michael@0: // Expected add-ons when using cache michael@0: const WITH_CACHE = [{ michael@0: id: ADDON_IDS[0], michael@0: type: "extension", michael@0: name: "XPI Add-on 1", michael@0: version: "1.1", michael@0: creator: { michael@0: name: "Repo Add-on 1 - Creator", michael@0: url: BASE_URL + "/repo/1/creator.html" michael@0: }, michael@0: developers: [{ name: "XPI Add-on 1 - First Developer" }, michael@0: { name: "XPI Add-on 1 - Second Developer" }], michael@0: translators: [{ name: "XPI Add-on 1 - First Translator" }, michael@0: { name: "XPI Add-on 1 - Second Translator" }], michael@0: contributors: [{ name: "XPI Add-on 1 - First Contributor" }, michael@0: { name: "XPI Add-on 1 - Second Contributor" }], michael@0: description: "XPI Add-on 1 - Description", michael@0: fullDescription: "Repo Add-on 1 - Full Description & some extra", michael@0: developerComments: "Repo Add-on 1\nDeveloper Comments", michael@0: eula: "Repo Add-on 1 - EULA", michael@0: iconURL: BASE_URL + "/xpi/1/icon.png", michael@0: icons: { "32": BASE_URL + "/xpi/1/icon.png" }, michael@0: homepageURL: BASE_URL + "/xpi/1/homepage.html", michael@0: supportURL: BASE_URL + "/repo/1/support.html", michael@0: optionsURL: BASE_URL + "/xpi/1/options.html", michael@0: aboutURL: BASE_URL + "/xpi/1/about.html", michael@0: contributionURL: BASE_URL + "/repo/1/meetDevelopers.html", michael@0: contributionAmount: "$11.11", michael@0: averageRating: 1, michael@0: reviewCount: 1111, michael@0: reviewURL: BASE_URL + "/repo/1/review.html", michael@0: totalDownloads: 2221, michael@0: weeklyDownloads: 3331, michael@0: dailyUsers: 4441, michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec, michael@0: repositoryStatus: 4, michael@0: compatibilityOverrides: [{ michael@0: type: "incompatible", michael@0: minVersion: 0.1, michael@0: maxVersion: 0.2, michael@0: appID: "xpcshell@tests.mozilla.org", michael@0: appMinVersion: 3.0, michael@0: appMaxVersion: 4.0 michael@0: }, { michael@0: type: "incompatible", michael@0: minVersion: 0.2, michael@0: maxVersion: 0.3, michael@0: appID: "xpcshell@tests.mozilla.org", michael@0: appMinVersion: 5.0, michael@0: appMaxVersion: 6.0 michael@0: }] michael@0: }, { michael@0: id: ADDON_IDS[1], michael@0: type: "theme", michael@0: name: "XPI Add-on 2", michael@0: version: "1.2", michael@0: creator: { michael@0: name: "Repo Add-on 2 - Creator", michael@0: url: BASE_URL + "/repo/2/creator.html" michael@0: }, michael@0: developers: [{ michael@0: name: "Repo Add-on 2 - First Developer", michael@0: url: BASE_URL + "/repo/2/firstDeveloper.html" michael@0: }, { michael@0: name: "Repo Add-on 2 - Second Developer", michael@0: url: BASE_URL + "/repo/2/secondDeveloper.html" michael@0: }], michael@0: description: "Repo Add-on 2 - Description", michael@0: fullDescription: "Repo Add-on 2 - Full Description", michael@0: developerComments: "Repo Add-on 2 - Developer Comments", michael@0: eula: "Repo Add-on 2 - EULA", michael@0: iconURL: BASE_URL + "/repo/2/icon.png", michael@0: icons: { "32": BASE_URL + "/repo/2/icon.png" }, michael@0: screenshots: [{ michael@0: url: BASE_URL + "/repo/2/firstFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png", michael@0: caption: "Repo Add-on 2 - First Caption" michael@0: } , { michael@0: url: BASE_URL + "/repo/2/secondFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png", michael@0: caption: "Repo Add-on 2 - Second Caption" michael@0: }], michael@0: homepageURL: BASE_URL + "/repo/2/homepage.html", michael@0: supportURL: BASE_URL + "/repo/2/support.html", michael@0: contributionURL: BASE_URL + "/repo/2/meetDevelopers.html", michael@0: contributionAmount: null, michael@0: averageRating: 2, michael@0: reviewCount: 1112, michael@0: reviewURL: BASE_URL + "/repo/2/review.html", michael@0: totalDownloads: 2222, michael@0: weeklyDownloads: 3332, michael@0: dailyUsers: 4442, michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec, michael@0: repositoryStatus: 9 michael@0: }, { michael@0: id: ADDON_IDS[2], michael@0: type: "theme", michael@0: name: "XPI Add-on 3", michael@0: version: "1.3", michael@0: get iconURL () { michael@0: return get_subfile_uri(ADDON_IDS[2], "icon.png"); michael@0: }, michael@0: get icons () { michael@0: return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") }; michael@0: }, michael@0: screenshots: [{ michael@0: url: BASE_URL + "/repo/3/firstFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png", michael@0: caption: "Repo Add-on 3 - First Caption" michael@0: } , { michael@0: url: BASE_URL + "/repo/3/secondFull.png", michael@0: thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png", michael@0: caption: "Repo Add-on 3 - Second Caption" michael@0: }], michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec michael@0: }]; michael@0: michael@0: // Expected add-ons when using cache michael@0: const WITH_EXTENSION_CACHE = [{ michael@0: id: ADDON_IDS[0], michael@0: type: "extension", michael@0: name: "XPI Add-on 1", michael@0: version: "1.1", michael@0: creator: { michael@0: name: "Repo Add-on 1 - Creator", michael@0: url: BASE_URL + "/repo/1/creator.html" michael@0: }, michael@0: developers: [{ name: "XPI Add-on 1 - First Developer" }, michael@0: { name: "XPI Add-on 1 - Second Developer" }], michael@0: translators: [{ name: "XPI Add-on 1 - First Translator" }, michael@0: { name: "XPI Add-on 1 - Second Translator" }], michael@0: contributors: [{ name: "XPI Add-on 1 - First Contributor" }, michael@0: { name: "XPI Add-on 1 - Second Contributor" }], michael@0: description: "XPI Add-on 1 - Description", michael@0: fullDescription: "Repo Add-on 1 - Full Description & some extra", michael@0: developerComments: "Repo Add-on 1\nDeveloper Comments", michael@0: eula: "Repo Add-on 1 - EULA", michael@0: iconURL: BASE_URL + "/xpi/1/icon.png", michael@0: icons: { "32": BASE_URL + "/xpi/1/icon.png" }, michael@0: homepageURL: BASE_URL + "/xpi/1/homepage.html", michael@0: supportURL: BASE_URL + "/repo/1/support.html", michael@0: optionsURL: BASE_URL + "/xpi/1/options.html", michael@0: aboutURL: BASE_URL + "/xpi/1/about.html", michael@0: contributionURL: BASE_URL + "/repo/1/meetDevelopers.html", michael@0: contributionAmount: "$11.11", michael@0: averageRating: 1, michael@0: reviewCount: 1111, michael@0: reviewURL: BASE_URL + "/repo/1/review.html", michael@0: totalDownloads: 2221, michael@0: weeklyDownloads: 3331, michael@0: dailyUsers: 4441, michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[0]).spec, michael@0: repositoryStatus: 4, michael@0: compatibilityOverrides: [{ michael@0: type: "incompatible", michael@0: minVersion: 0.1, michael@0: maxVersion: 0.2, michael@0: appID: "xpcshell@tests.mozilla.org", michael@0: appMinVersion: 3.0, michael@0: appMaxVersion: 4.0 michael@0: }, { michael@0: type: "incompatible", michael@0: minVersion: 0.2, michael@0: maxVersion: 0.3, michael@0: appID: "xpcshell@tests.mozilla.org", michael@0: appMinVersion: 5.0, michael@0: appMaxVersion: 6.0 michael@0: }] michael@0: }, { michael@0: id: ADDON_IDS[1], michael@0: type: "theme", michael@0: name: "XPI Add-on 2", michael@0: version: "1.2", michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[1]).spec, michael@0: icons: {} michael@0: }, { michael@0: id: ADDON_IDS[2], michael@0: type: "theme", michael@0: name: "XPI Add-on 3", michael@0: version: "1.3", michael@0: get iconURL () { michael@0: return get_subfile_uri(ADDON_IDS[2], "icon.png"); michael@0: }, michael@0: get icons () { michael@0: return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") }; michael@0: }, michael@0: screenshots: [{ get url () { return get_subfile_uri(ADDON_IDS[2], "preview.png"); } }], michael@0: sourceURI: NetUtil.newURI(ADDON_FILES[2]).spec michael@0: }]; michael@0: michael@0: michael@0: /* michael@0: * Trigger an AddonManager background update check michael@0: * michael@0: * @return Promise{null} michael@0: * Resolves when the background update notification is received michael@0: */ michael@0: function trigger_background_update() { michael@0: return new Promise((resolve, reject) => { michael@0: Services.obs.addObserver({ michael@0: observe: function(aSubject, aTopic, aData) { michael@0: do_print("Observed " + aTopic); michael@0: Services.obs.removeObserver(this, "addons-background-update-complete"); michael@0: resolve(); michael@0: } michael@0: }, "addons-background-update-complete", false); michael@0: michael@0: gInternalManager.notify(null); michael@0: }); michael@0: } michael@0: michael@0: let gDBFile = gProfD.clone(); michael@0: gDBFile.append(FILE_DATABASE); michael@0: michael@0: /* michael@0: * Check the actual add-on results against the expected add-on results michael@0: * michael@0: * @param aActualAddons michael@0: * The array of actual add-ons to check michael@0: * @param aExpectedAddons michael@0: * The array of expected add-ons to check against michael@0: * @param aFromRepository michael@0: * An optional boolean representing if the add-ons are from michael@0: * the repository michael@0: */ michael@0: function check_results(aActualAddons, aExpectedAddons, aFromRepository) { michael@0: aFromRepository = !!aFromRepository; michael@0: michael@0: do_check_addons(aActualAddons, aExpectedAddons, ADDON_PROPERTIES); michael@0: michael@0: // Separately test size and updateDate (they should only be equal to the michael@0: // REPOSITORY values if they are from the repository) michael@0: aActualAddons.forEach(function(aActualAddon) { michael@0: if (aActualAddon.size) michael@0: do_check_eq(aActualAddon.size === REPOSITORY_SIZE, aFromRepository); michael@0: michael@0: if (aActualAddon.updateDate) { michael@0: let time = aActualAddon.updateDate.getTime(); michael@0: do_check_eq(time === 1000 * REPOSITORY_UPDATEDATE, aFromRepository); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: /* michael@0: * Check the add-ons in the cache. This function also tests michael@0: * AddonRepository.getCachedAddonByID() michael@0: * michael@0: * @param aExpectedToFind michael@0: * An array of booleans representing which REPOSITORY_ADDONS are michael@0: * expected to be found in the cache michael@0: * @param aExpectedImmediately michael@0: * A boolean representing if results from the cache are expected michael@0: * immediately. Results are not immediate if the cache has not been michael@0: * initialized yet. michael@0: * @return Promise{null} michael@0: * Resolves once the checks are complete michael@0: */ michael@0: function check_cache(aExpectedToFind, aExpectedImmediately) { michael@0: do_check_eq(aExpectedToFind.length, REPOSITORY_ADDONS.length); michael@0: michael@0: let lookups = []; michael@0: michael@0: for (let i = 0 ; i < REPOSITORY_ADDONS.length ; i++) { michael@0: lookups.push(new Promise((resolve, reject) => { michael@0: let immediatelyFound = true; michael@0: let expected = aExpectedToFind[i] ? REPOSITORY_ADDONS[i] : null; michael@0: // can't Promise-wrap this because we're also testing whether the callback is michael@0: // sync or async michael@0: AddonRepository.getCachedAddonByID(REPOSITORY_ADDONS[i].id, function(aAddon) { michael@0: do_check_eq(immediatelyFound, aExpectedImmediately); michael@0: if (expected == null) michael@0: do_check_eq(aAddon, null); michael@0: else michael@0: check_results([aAddon], [expected], true); michael@0: resolve(); michael@0: }); michael@0: immediatelyFound = false; michael@0: })); michael@0: } michael@0: return Promise.all(lookups); michael@0: } michael@0: michael@0: /* michael@0: * Task to check an initialized cache by checking the cache, then restarting the michael@0: * manager, and checking the cache. This checks that the cache is consistent michael@0: * across manager restarts. michael@0: * michael@0: * @param aExpectedToFind michael@0: * An array of booleans representing which REPOSITORY_ADDONS are michael@0: * expected to be found in the cache michael@0: */ michael@0: function* check_initialized_cache(aExpectedToFind) { michael@0: yield check_cache(aExpectedToFind, true); michael@0: yield promiseRestartManager(); michael@0: michael@0: // If cache is disabled, then expect results immediately michael@0: let cacheEnabled = Services.prefs.getBoolPref(PREF_GETADDONS_CACHE_ENABLED); michael@0: yield check_cache(aExpectedToFind, !cacheEnabled); michael@0: } michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function* setup() { michael@0: // Setup for test michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); michael@0: michael@0: startupManager(); michael@0: michael@0: // Install XPI add-ons michael@0: yield promiseInstallAllFiles(ADDON_FILES); michael@0: yield promiseRestartManager(); michael@0: michael@0: gServer = new HttpServer(); michael@0: gServer.registerDirectory("/data/", do_get_file("data")); michael@0: gServer.start(PORT); michael@0: }); michael@0: michael@0: // Tests AddonRepository.cacheEnabled michael@0: add_task(function* run_test_1() { michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false); michael@0: do_check_false(AddonRepository.cacheEnabled); michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true); michael@0: do_check_true(AddonRepository.cacheEnabled); michael@0: }); michael@0: michael@0: // Tests that the cache and database begin as empty michael@0: add_task(function* run_test_2() { michael@0: do_check_false(gDBFile.exists()); michael@0: yield check_cache([false, false, false], false); michael@0: yield AddonRepository.flush(); michael@0: }); michael@0: michael@0: // Tests repopulateCache when the search fails michael@0: add_task(function* run_test_3() { michael@0: do_check_true(gDBFile.exists()); michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true); michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_FAILED); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.repopulateCache(ADDON_IDS, resolve)); michael@0: yield check_initialized_cache([false, false, false]); michael@0: }); michael@0: michael@0: // Tests repopulateCache when search returns no results michael@0: add_task(function* run_test_4() { michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_EMPTY); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.repopulateCache(ADDON_IDS, resolve)); michael@0: yield check_initialized_cache([false, false, false]); michael@0: }); michael@0: michael@0: // Tests repopulateCache when search returns results michael@0: add_task(function* run_test_5() { michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.repopulateCache(ADDON_IDS, resolve)); michael@0: yield check_initialized_cache([true, true, true]); michael@0: }); michael@0: michael@0: // Tests repopulateCache when caching is disabled for a single add-on michael@0: add_task(function* run_test_5_1() { michael@0: Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, false); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.repopulateCache(ADDON_IDS, resolve)); michael@0: michael@0: // Reset pref for next test michael@0: Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, true); michael@0: michael@0: yield check_initialized_cache([false, true, true]); michael@0: }); michael@0: michael@0: // Tests repopulateCache when caching is disabled michael@0: add_task(function* run_test_6() { michael@0: do_check_true(gDBFile.exists()); michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.repopulateCache(ADDON_IDS, resolve)); michael@0: // Database should have been deleted michael@0: do_check_false(gDBFile.exists()); michael@0: michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true); michael@0: yield check_cache([false, false, false], false); michael@0: yield AddonRepository.flush(); michael@0: }); michael@0: michael@0: // Tests cacheAddons when the search fails michael@0: add_task(function* run_test_7() { michael@0: do_check_true(gDBFile.exists()); michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_FAILED); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.cacheAddons(ADDON_IDS, resolve)); michael@0: yield check_initialized_cache([false, false, false]); michael@0: }); michael@0: michael@0: // Tests cacheAddons when the search returns no results michael@0: add_task(function* run_test_8() { michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_EMPTY); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.cacheAddons(ADDON_IDS, resolve)); michael@0: yield check_initialized_cache([false, false, false]); michael@0: }); michael@0: michael@0: // Tests cacheAddons for a single add-on when search returns results michael@0: add_task(function* run_test_9() { michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.cacheAddons([ADDON_IDS[0]], resolve)); michael@0: yield check_initialized_cache([true, false, false]); michael@0: }); michael@0: michael@0: // Tests cacheAddons when caching is disabled for a single add-on michael@0: add_task(function* run_test_9_1() { michael@0: Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, false); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.cacheAddons(ADDON_IDS, resolve)); michael@0: michael@0: // Reset pref for next test michael@0: Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, true); michael@0: michael@0: yield check_initialized_cache([true, false, true]); michael@0: }); michael@0: michael@0: // Tests cacheAddons for multiple add-ons, some already in the cache, michael@0: add_task(function* run_test_10() { michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.cacheAddons(ADDON_IDS, resolve)); michael@0: yield check_initialized_cache([true, true, true]); michael@0: }); michael@0: michael@0: // Tests cacheAddons when caching is disabled michael@0: add_task(function* run_test_11() { michael@0: do_check_true(gDBFile.exists()); michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false); michael@0: michael@0: yield new Promise((resolve, reject) => michael@0: AddonRepository.cacheAddons(ADDON_IDS, resolve)); michael@0: do_check_true(gDBFile.exists()); michael@0: michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true); michael@0: yield check_initialized_cache([true, true, true]); michael@0: }); michael@0: michael@0: // Tests that XPI add-ons do not use any of the repository properties if michael@0: // caching is disabled, even if there are repository properties available michael@0: add_task(function* run_test_12() { michael@0: do_check_true(gDBFile.exists()); michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false); michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS); michael@0: michael@0: let aAddons = yield promiseAddonsByIDs(ADDON_IDS); michael@0: check_results(aAddons, WITHOUT_CACHE); michael@0: }); michael@0: michael@0: // Tests that a background update with caching disabled deletes the add-ons michael@0: // database, and that XPI add-ons still do not use any of repository properties michael@0: add_task(function* run_test_13() { michael@0: do_check_true(gDBFile.exists()); michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS_PERFORMANCE, GETADDONS_EMPTY); michael@0: michael@0: yield trigger_background_update(); michael@0: // Database should have been deleted michael@0: do_check_false(gDBFile.exists()); michael@0: michael@0: let aAddons = yield promiseAddonsByIDs(ADDON_IDS); michael@0: check_results(aAddons, WITHOUT_CACHE); michael@0: }); michael@0: michael@0: // Tests that the XPI add-ons have the correct properties if caching is michael@0: // enabled but has no information michael@0: add_task(function* run_test_14() { michael@0: Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true); michael@0: michael@0: yield trigger_background_update(); michael@0: yield AddonRepository.flush(); michael@0: do_check_true(gDBFile.exists()); michael@0: michael@0: let aAddons = yield promiseAddonsByIDs(ADDON_IDS); michael@0: check_results(aAddons, WITHOUT_CACHE); michael@0: }); michael@0: michael@0: // Tests that the XPI add-ons correctly use the repository properties when michael@0: // caching is enabled and the repository information is available michael@0: add_task(function* run_test_15() { michael@0: Services.prefs.setCharPref(PREF_GETADDONS_BYIDS_PERFORMANCE, GETADDONS_RESULTS); michael@0: michael@0: yield trigger_background_update(); michael@0: let aAddons = yield promiseAddonsByIDs(ADDON_IDS); michael@0: check_results(aAddons, WITH_CACHE); michael@0: }); michael@0: michael@0: // Tests that restarting the manager does not change the checked properties michael@0: // on the XPI add-ons (repository properties still exist and are still properly michael@0: // used) michael@0: add_task(function* run_test_16() { michael@0: yield promiseRestartManager(); michael@0: michael@0: let aAddons = yield promiseAddonsByIDs(ADDON_IDS); michael@0: check_results(aAddons, WITH_CACHE); michael@0: }); michael@0: michael@0: // Tests that setting a list of types to cache works michael@0: add_task(function* run_test_17() { michael@0: Services.prefs.setCharPref(PREF_GETADDONS_CACHE_TYPES, "foo,bar,extension,baz"); michael@0: michael@0: yield trigger_background_update(); michael@0: let aAddons = yield promiseAddonsByIDs(ADDON_IDS); michael@0: check_results(aAddons, WITH_EXTENSION_CACHE); michael@0: }); michael@0: michael@0: add_task(function* end_test() { michael@0: yield new Promise((resolve, reject) => gServer.stop(resolve)); michael@0: });