browser/experiments/test/xpcshell/head.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 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
michael@0 5
michael@0 6 Cu.import("resource://gre/modules/Services.jsm");
michael@0 7 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 8 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 9 Cu.import("resource://gre/modules/Task.jsm");
michael@0 10 Cu.import("resource://gre/modules/osfile.jsm");
michael@0 11 Cu.import("resource://services-sync/healthreport.jsm", this);
michael@0 12 Cu.import("resource://testing-common/services/healthreport/utils.jsm", this);
michael@0 13 Cu.import("resource://gre/modules/services/healthreport/providers.jsm");
michael@0 14 Cu.import("resource://testing-common/AddonManagerTesting.jsm");
michael@0 15
michael@0 16 const PREF_EXPERIMENTS_ENABLED = "experiments.enabled";
michael@0 17 const PREF_LOGGING_LEVEL = "experiments.logging.level";
michael@0 18 const PREF_LOGGING_DUMP = "experiments.logging.dump";
michael@0 19 const PREF_MANIFEST_URI = "experiments.manifest.uri";
michael@0 20 const PREF_FETCHINTERVAL = "experiments.manifest.fetchIntervalSeconds";
michael@0 21 const PREF_TELEMETRY_ENABLED = "toolkit.telemetry.enabled";
michael@0 22 const PREF_HEALTHREPORT_ENABLED = "datareporting.healthreport.service.enabled";
michael@0 23
michael@0 24 function getExperimentPath(base) {
michael@0 25 let p = do_get_cwd();
michael@0 26 p.append(base);
michael@0 27 return p.path;
michael@0 28 }
michael@0 29
michael@0 30 function sha1File(path) {
michael@0 31 let f = Cc["@mozilla.org/file/local;1"]
michael@0 32 .createInstance(Ci.nsILocalFile);
michael@0 33 f.initWithPath(path);
michael@0 34 let hasher = Cc["@mozilla.org/security/hash;1"]
michael@0 35 .createInstance(Ci.nsICryptoHash);
michael@0 36 hasher.init(hasher.SHA1);
michael@0 37
michael@0 38 let is = Cc["@mozilla.org/network/file-input-stream;1"]
michael@0 39 .createInstance(Ci.nsIFileInputStream);
michael@0 40 is.init(f, -1, 0, 0);
michael@0 41 hasher.updateFromStream(is, Math.pow(2, 32) - 1);
michael@0 42 is.close();
michael@0 43 let bytes = hasher.finish(false);
michael@0 44
michael@0 45 return [("0" + bytes.charCodeAt(byte).toString(16)).slice(-2)
michael@0 46 for (byte in bytes)]
michael@0 47 .join("");
michael@0 48 }
michael@0 49
michael@0 50 const EXPERIMENT1_ID = "test-experiment-1@tests.mozilla.org";
michael@0 51 const EXPERIMENT1_XPI_NAME = "experiment-1.xpi";
michael@0 52 const EXPERIMENT1_NAME = "Test experiment 1";
michael@0 53 const EXPERIMENT1_PATH = getExperimentPath(EXPERIMENT1_XPI_NAME);
michael@0 54 const EXPERIMENT1_XPI_SHA1 = "sha1:" + sha1File(EXPERIMENT1_PATH);
michael@0 55
michael@0 56
michael@0 57 const EXPERIMENT1A_XPI_NAME = "experiment-1a.xpi";
michael@0 58 const EXPERIMENT1A_NAME = "Test experiment 1.1";
michael@0 59 const EXPERIMENT1A_PATH = getExperimentPath(EXPERIMENT1A_XPI_NAME);
michael@0 60 const EXPERIMENT1A_XPI_SHA1 = "sha1:" + sha1File(EXPERIMENT1A_PATH);
michael@0 61
michael@0 62 const EXPERIMENT2_ID = "test-experiment-2@tests.mozilla.org"
michael@0 63 const EXPERIMENT2_XPI_NAME = "experiment-2.xpi";
michael@0 64 const EXPERIMENT2_PATH = getExperimentPath(EXPERIMENT2_XPI_NAME);
michael@0 65 const EXPERIMENT2_XPI_SHA1 = "sha1:" + sha1File(EXPERIMENT2_PATH);
michael@0 66
michael@0 67 const EXPERIMENT3_ID = "test-experiment-3@tests.mozilla.org";
michael@0 68 const EXPERIMENT4_ID = "test-experiment-4@tests.mozilla.org";
michael@0 69
michael@0 70 const DEFAULT_BUILDID = "2014060601";
michael@0 71
michael@0 72 const FAKE_EXPERIMENTS_1 = [
michael@0 73 {
michael@0 74 id: "id1",
michael@0 75 name: "experiment1",
michael@0 76 description: "experiment 1",
michael@0 77 active: true,
michael@0 78 detailUrl: "https://dummy/experiment1",
michael@0 79 branch: "foo",
michael@0 80 },
michael@0 81 ];
michael@0 82
michael@0 83 const FAKE_EXPERIMENTS_2 = [
michael@0 84 {
michael@0 85 id: "id2",
michael@0 86 name: "experiment2",
michael@0 87 description: "experiment 2",
michael@0 88 active: false,
michael@0 89 endDate: new Date(2014, 2, 11, 2, 4, 35, 42).getTime(),
michael@0 90 detailUrl: "https://dummy/experiment2",
michael@0 91 branch: null,
michael@0 92 },
michael@0 93 {
michael@0 94 id: "id1",
michael@0 95 name: "experiment1",
michael@0 96 description: "experiment 1",
michael@0 97 active: false,
michael@0 98 endDate: new Date(2014, 2, 10, 0, 0, 0, 0).getTime(),
michael@0 99 detailURL: "https://dummy/experiment1",
michael@0 100 branch: null,
michael@0 101 },
michael@0 102 ];
michael@0 103
michael@0 104 let gAppInfo = null;
michael@0 105
michael@0 106 function getReporter(name, uri, inspected) {
michael@0 107 return Task.spawn(function init() {
michael@0 108 let reporter = getHealthReporter(name, uri, inspected);
michael@0 109 yield reporter.init();
michael@0 110
michael@0 111 yield reporter._providerManager.registerProviderFromType(
michael@0 112 HealthReportProvider);
michael@0 113
michael@0 114 throw new Task.Result(reporter);
michael@0 115 });
michael@0 116 }
michael@0 117
michael@0 118 function removeCacheFile() {
michael@0 119 let path = OS.Path.join(OS.Constants.Path.profileDir, "experiments.json");
michael@0 120 return OS.File.remove(path);
michael@0 121 }
michael@0 122
michael@0 123 function disableCertificateChecks() {
michael@0 124 let pref = "experiments.manifest.cert.checkAttributes";
michael@0 125 Services.prefs.setBoolPref(pref, false);
michael@0 126 do_register_cleanup(() => Services.prefs.clearUserPref(pref));
michael@0 127 }
michael@0 128
michael@0 129 function patchPolicy(policy, data) {
michael@0 130 for (let key of Object.keys(data)) {
michael@0 131 Object.defineProperty(policy, key, {
michael@0 132 value: data[key],
michael@0 133 writable: true,
michael@0 134 });
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138 function defineNow(policy, time) {
michael@0 139 patchPolicy(policy, { now: () => new Date(time) });
michael@0 140 }
michael@0 141
michael@0 142 function futureDate(date, offset) {
michael@0 143 return new Date(date.getTime() + offset);
michael@0 144 }
michael@0 145
michael@0 146 function dateToSeconds(date) {
michael@0 147 return date.getTime() / 1000;
michael@0 148 }
michael@0 149
michael@0 150 let gGlobalScope = this;
michael@0 151 function loadAddonManager() {
michael@0 152 let ns = {};
michael@0 153 Cu.import("resource://gre/modules/Services.jsm", ns);
michael@0 154 let head = "../../../../toolkit/mozapps/extensions/test/xpcshell/head_addons.js";
michael@0 155 let file = do_get_file(head);
michael@0 156 let uri = ns.Services.io.newFileURI(file);
michael@0 157 ns.Services.scriptloader.loadSubScript(uri.spec, gGlobalScope);
michael@0 158 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
michael@0 159 startupManager();
michael@0 160 }
michael@0 161
michael@0 162 function getExperimentAddons(previous=false) {
michael@0 163 let deferred = Promise.defer();
michael@0 164
michael@0 165 AddonManager.getAddonsByTypes(["experiment"], (addons) => {
michael@0 166 if (previous) {
michael@0 167 deferred.resolve(addons);
michael@0 168 } else {
michael@0 169 deferred.resolve([a for (a of addons) if (!a.appDisabled)]);
michael@0 170 }
michael@0 171 });
michael@0 172
michael@0 173 return deferred.promise;
michael@0 174 }
michael@0 175
michael@0 176 function createAppInfo(optionsIn) {
michael@0 177 const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
michael@0 178 const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
michael@0 179
michael@0 180 let options = optionsIn || {};
michael@0 181 let id = options.id || "xpcshell@tests.mozilla.org";
michael@0 182 let name = options.name || "XPCShell";
michael@0 183 let version = options.version || "1.0";
michael@0 184 let platformVersion = options.platformVersion || "1.0";
michael@0 185 let date = options.date || new Date();
michael@0 186
michael@0 187 let buildID = options.buildID || DEFAULT_BUILDID;
michael@0 188
michael@0 189 gAppInfo = {
michael@0 190 // nsIXULAppInfo
michael@0 191 vendor: "Mozilla",
michael@0 192 name: name,
michael@0 193 ID: id,
michael@0 194 version: version,
michael@0 195 appBuildID: buildID,
michael@0 196 platformVersion: platformVersion ? platformVersion : "1.0",
michael@0 197 platformBuildID: buildID,
michael@0 198
michael@0 199 // nsIXULRuntime
michael@0 200 inSafeMode: false,
michael@0 201 logConsoleErrors: true,
michael@0 202 OS: "XPCShell",
michael@0 203 XPCOMABI: "noarch-spidermonkey",
michael@0 204 invalidateCachesOnRestart: function invalidateCachesOnRestart() {
michael@0 205 // Do nothing
michael@0 206 },
michael@0 207
michael@0 208 // nsICrashReporter
michael@0 209 annotations: {},
michael@0 210
michael@0 211 annotateCrashReport: function(key, data) {
michael@0 212 this.annotations[key] = data;
michael@0 213 },
michael@0 214
michael@0 215 QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo,
michael@0 216 Ci.nsIXULRuntime,
michael@0 217 Ci.nsICrashReporter,
michael@0 218 Ci.nsISupports])
michael@0 219 };
michael@0 220
michael@0 221 let XULAppInfoFactory = {
michael@0 222 createInstance: function (outer, iid) {
michael@0 223 if (outer != null) {
michael@0 224 throw Cr.NS_ERROR_NO_AGGREGATION;
michael@0 225 }
michael@0 226 return gAppInfo.QueryInterface(iid);
michael@0 227 }
michael@0 228 };
michael@0 229
michael@0 230 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 231 registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
michael@0 232 XULAPPINFO_CONTRACTID, XULAppInfoFactory);
michael@0 233 }
michael@0 234
michael@0 235 /**
michael@0 236 * Replace the experiments on an Experiments with a new list.
michael@0 237 *
michael@0 238 * This monkeypatches getExperiments(). It doesn't monkeypatch the internal
michael@0 239 * experiments list. So its utility is not as great as it could be.
michael@0 240 */
michael@0 241 function replaceExperiments(experiment, list) {
michael@0 242 Object.defineProperty(experiment, "getExperiments", {
michael@0 243 writable: true,
michael@0 244 value: () => {
michael@0 245 return Promise.resolve(list);
michael@0 246 },
michael@0 247 });
michael@0 248 }

mercurial