michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: Cu.import("resource://gre/modules/Task.jsm"); michael@0: Cu.import("resource://gre/modules/osfile.jsm"); michael@0: Cu.import("resource://services-sync/healthreport.jsm", this); michael@0: Cu.import("resource://testing-common/services/healthreport/utils.jsm", this); michael@0: Cu.import("resource://gre/modules/services/healthreport/providers.jsm"); michael@0: Cu.import("resource://testing-common/AddonManagerTesting.jsm"); michael@0: michael@0: const PREF_EXPERIMENTS_ENABLED = "experiments.enabled"; michael@0: const PREF_LOGGING_LEVEL = "experiments.logging.level"; michael@0: const PREF_LOGGING_DUMP = "experiments.logging.dump"; michael@0: const PREF_MANIFEST_URI = "experiments.manifest.uri"; michael@0: const PREF_FETCHINTERVAL = "experiments.manifest.fetchIntervalSeconds"; michael@0: const PREF_TELEMETRY_ENABLED = "toolkit.telemetry.enabled"; michael@0: const PREF_HEALTHREPORT_ENABLED = "datareporting.healthreport.service.enabled"; michael@0: michael@0: function getExperimentPath(base) { michael@0: let p = do_get_cwd(); michael@0: p.append(base); michael@0: return p.path; michael@0: } michael@0: michael@0: function sha1File(path) { michael@0: let f = Cc["@mozilla.org/file/local;1"] michael@0: .createInstance(Ci.nsILocalFile); michael@0: f.initWithPath(path); michael@0: let hasher = Cc["@mozilla.org/security/hash;1"] michael@0: .createInstance(Ci.nsICryptoHash); michael@0: hasher.init(hasher.SHA1); michael@0: michael@0: let is = Cc["@mozilla.org/network/file-input-stream;1"] michael@0: .createInstance(Ci.nsIFileInputStream); michael@0: is.init(f, -1, 0, 0); michael@0: hasher.updateFromStream(is, Math.pow(2, 32) - 1); michael@0: is.close(); michael@0: let bytes = hasher.finish(false); michael@0: michael@0: return [("0" + bytes.charCodeAt(byte).toString(16)).slice(-2) michael@0: for (byte in bytes)] michael@0: .join(""); michael@0: } michael@0: michael@0: const EXPERIMENT1_ID = "test-experiment-1@tests.mozilla.org"; michael@0: const EXPERIMENT1_XPI_NAME = "experiment-1.xpi"; michael@0: const EXPERIMENT1_NAME = "Test experiment 1"; michael@0: const EXPERIMENT1_PATH = getExperimentPath(EXPERIMENT1_XPI_NAME); michael@0: const EXPERIMENT1_XPI_SHA1 = "sha1:" + sha1File(EXPERIMENT1_PATH); michael@0: michael@0: michael@0: const EXPERIMENT1A_XPI_NAME = "experiment-1a.xpi"; michael@0: const EXPERIMENT1A_NAME = "Test experiment 1.1"; michael@0: const EXPERIMENT1A_PATH = getExperimentPath(EXPERIMENT1A_XPI_NAME); michael@0: const EXPERIMENT1A_XPI_SHA1 = "sha1:" + sha1File(EXPERIMENT1A_PATH); michael@0: michael@0: const EXPERIMENT2_ID = "test-experiment-2@tests.mozilla.org" michael@0: const EXPERIMENT2_XPI_NAME = "experiment-2.xpi"; michael@0: const EXPERIMENT2_PATH = getExperimentPath(EXPERIMENT2_XPI_NAME); michael@0: const EXPERIMENT2_XPI_SHA1 = "sha1:" + sha1File(EXPERIMENT2_PATH); michael@0: michael@0: const EXPERIMENT3_ID = "test-experiment-3@tests.mozilla.org"; michael@0: const EXPERIMENT4_ID = "test-experiment-4@tests.mozilla.org"; michael@0: michael@0: const DEFAULT_BUILDID = "2014060601"; michael@0: michael@0: const FAKE_EXPERIMENTS_1 = [ michael@0: { michael@0: id: "id1", michael@0: name: "experiment1", michael@0: description: "experiment 1", michael@0: active: true, michael@0: detailUrl: "https://dummy/experiment1", michael@0: branch: "foo", michael@0: }, michael@0: ]; michael@0: michael@0: const FAKE_EXPERIMENTS_2 = [ michael@0: { michael@0: id: "id2", michael@0: name: "experiment2", michael@0: description: "experiment 2", michael@0: active: false, michael@0: endDate: new Date(2014, 2, 11, 2, 4, 35, 42).getTime(), michael@0: detailUrl: "https://dummy/experiment2", michael@0: branch: null, michael@0: }, michael@0: { michael@0: id: "id1", michael@0: name: "experiment1", michael@0: description: "experiment 1", michael@0: active: false, michael@0: endDate: new Date(2014, 2, 10, 0, 0, 0, 0).getTime(), michael@0: detailURL: "https://dummy/experiment1", michael@0: branch: null, michael@0: }, michael@0: ]; michael@0: michael@0: let gAppInfo = null; michael@0: michael@0: function getReporter(name, uri, inspected) { michael@0: return Task.spawn(function init() { michael@0: let reporter = getHealthReporter(name, uri, inspected); michael@0: yield reporter.init(); michael@0: michael@0: yield reporter._providerManager.registerProviderFromType( michael@0: HealthReportProvider); michael@0: michael@0: throw new Task.Result(reporter); michael@0: }); michael@0: } michael@0: michael@0: function removeCacheFile() { michael@0: let path = OS.Path.join(OS.Constants.Path.profileDir, "experiments.json"); michael@0: return OS.File.remove(path); michael@0: } michael@0: michael@0: function disableCertificateChecks() { michael@0: let pref = "experiments.manifest.cert.checkAttributes"; michael@0: Services.prefs.setBoolPref(pref, false); michael@0: do_register_cleanup(() => Services.prefs.clearUserPref(pref)); michael@0: } michael@0: michael@0: function patchPolicy(policy, data) { michael@0: for (let key of Object.keys(data)) { michael@0: Object.defineProperty(policy, key, { michael@0: value: data[key], michael@0: writable: true, michael@0: }); michael@0: } michael@0: } michael@0: michael@0: function defineNow(policy, time) { michael@0: patchPolicy(policy, { now: () => new Date(time) }); michael@0: } michael@0: michael@0: function futureDate(date, offset) { michael@0: return new Date(date.getTime() + offset); michael@0: } michael@0: michael@0: function dateToSeconds(date) { michael@0: return date.getTime() / 1000; michael@0: } michael@0: michael@0: let gGlobalScope = this; michael@0: function loadAddonManager() { michael@0: let ns = {}; michael@0: Cu.import("resource://gre/modules/Services.jsm", ns); michael@0: let head = "../../../../toolkit/mozapps/extensions/test/xpcshell/head_addons.js"; michael@0: let file = do_get_file(head); michael@0: let uri = ns.Services.io.newFileURI(file); michael@0: ns.Services.scriptloader.loadSubScript(uri.spec, gGlobalScope); michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); michael@0: startupManager(); michael@0: } michael@0: michael@0: function getExperimentAddons(previous=false) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: AddonManager.getAddonsByTypes(["experiment"], (addons) => { michael@0: if (previous) { michael@0: deferred.resolve(addons); michael@0: } else { michael@0: deferred.resolve([a for (a of addons) if (!a.appDisabled)]); michael@0: } michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function createAppInfo(optionsIn) { michael@0: const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; michael@0: const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}"); michael@0: michael@0: let options = optionsIn || {}; michael@0: let id = options.id || "xpcshell@tests.mozilla.org"; michael@0: let name = options.name || "XPCShell"; michael@0: let version = options.version || "1.0"; michael@0: let platformVersion = options.platformVersion || "1.0"; michael@0: let date = options.date || new Date(); michael@0: michael@0: let buildID = options.buildID || DEFAULT_BUILDID; michael@0: michael@0: gAppInfo = { michael@0: // nsIXULAppInfo michael@0: vendor: "Mozilla", michael@0: name: name, michael@0: ID: id, michael@0: version: version, michael@0: appBuildID: buildID, michael@0: platformVersion: platformVersion ? platformVersion : "1.0", michael@0: platformBuildID: buildID, michael@0: michael@0: // nsIXULRuntime michael@0: inSafeMode: false, michael@0: logConsoleErrors: true, michael@0: OS: "XPCShell", michael@0: XPCOMABI: "noarch-spidermonkey", michael@0: invalidateCachesOnRestart: function invalidateCachesOnRestart() { michael@0: // Do nothing michael@0: }, michael@0: michael@0: // nsICrashReporter michael@0: annotations: {}, michael@0: michael@0: annotateCrashReport: function(key, data) { michael@0: this.annotations[key] = data; michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, michael@0: Ci.nsIXULRuntime, michael@0: Ci.nsICrashReporter, michael@0: Ci.nsISupports]) michael@0: }; michael@0: michael@0: let XULAppInfoFactory = { michael@0: createInstance: function (outer, iid) { michael@0: if (outer != null) { michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: } michael@0: return gAppInfo.QueryInterface(iid); michael@0: } michael@0: }; michael@0: michael@0: let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo", michael@0: XULAPPINFO_CONTRACTID, XULAppInfoFactory); michael@0: } michael@0: michael@0: /** michael@0: * Replace the experiments on an Experiments with a new list. michael@0: * michael@0: * This monkeypatches getExperiments(). It doesn't monkeypatch the internal michael@0: * experiments list. So its utility is not as great as it could be. michael@0: */ michael@0: function replaceExperiments(experiment, list) { michael@0: Object.defineProperty(experiment, "getExperiments", { michael@0: writable: true, michael@0: value: () => { michael@0: return Promise.resolve(list); michael@0: }, michael@0: }); michael@0: }