browser/experiments/test/xpcshell/test_activate.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 "use strict";
michael@0 5
michael@0 6 Cu.import("resource://testing-common/httpd.js");
michael@0 7 Cu.import("resource:///modules/experiments/Experiments.jsm");
michael@0 8
michael@0 9 const FILE_MANIFEST = "experiments.manifest";
michael@0 10 const SEC_IN_ONE_DAY = 24 * 60 * 60;
michael@0 11 const MS_IN_ONE_DAY = SEC_IN_ONE_DAY * 1000;
michael@0 12
michael@0 13 let gProfileDir = null;
michael@0 14 let gHttpServer = null;
michael@0 15 let gHttpRoot = null;
michael@0 16 let gReporter = null;
michael@0 17 let gPolicy = null;
michael@0 18
michael@0 19 function ManifestEntry(data) {
michael@0 20 this.id = data.id || EXPERIMENT1_ID;
michael@0 21 this.xpiURL = data.xpiURL || gHttpRoot + EXPERIMENT1_XPI_NAME;
michael@0 22 this.xpiHash = data.xpiHash || EXPERIMENT1_XPI_SHA1;
michael@0 23 this.appName = data.appName || ["XPCShell"];
michael@0 24 this.channel = data.appName || ["nightly"];
michael@0 25 this.startTime = data.startTime || new Date(2010, 0, 1, 12).getTime() / 1000;
michael@0 26 this.endTime = data.endTime || new Date(9001, 0, 1, 12).getTime() / 1000;
michael@0 27 this.maxActiveSeconds = data.maxActiveSeconds || 5 * SEC_IN_ONE_DAY;
michael@0 28 }
michael@0 29
michael@0 30 function run_test() {
michael@0 31 run_next_test();
michael@0 32 }
michael@0 33
michael@0 34 add_task(function* test_setup() {
michael@0 35 loadAddonManager();
michael@0 36 gProfileDir = do_get_profile();
michael@0 37 gPolicy = new Experiments.Policy();
michael@0 38
michael@0 39 gHttpServer = new HttpServer();
michael@0 40 gHttpServer.start(-1);
michael@0 41 let port = gHttpServer.identity.primaryPort;
michael@0 42 gHttpRoot = "http://localhost:" + port + "/";
michael@0 43 gHttpServer.registerDirectory("/", do_get_cwd());
michael@0 44 do_register_cleanup(() => gHttpServer.stop(() => {}));
michael@0 45
michael@0 46 gReporter = yield getReporter("json_payload_simple");
michael@0 47 yield gReporter.collectMeasurements();
michael@0 48 let payload = yield gReporter.getJSONPayload(true);
michael@0 49 do_register_cleanup(() => gReporter._shutdown());
michael@0 50
michael@0 51 patchPolicy(gPolicy, {
michael@0 52 updatechannel: () => "nightly",
michael@0 53 healthReportPayload: () => Promise.resolve(payload),
michael@0 54 });
michael@0 55
michael@0 56 Services.prefs.setBoolPref(PREF_EXPERIMENTS_ENABLED, true);
michael@0 57 Services.prefs.setIntPref(PREF_LOGGING_LEVEL, 0);
michael@0 58 Services.prefs.setBoolPref(PREF_LOGGING_DUMP, true);
michael@0 59
michael@0 60 let experiments = new Experiments.Experiments();
michael@0 61 });
michael@0 62
michael@0 63 function isApplicable(experiment) {
michael@0 64 let deferred = Promise.defer();
michael@0 65 experiment.isApplicable().then(
michael@0 66 result => deferred.resolve({ applicable: true, reason: null }),
michael@0 67 reason => deferred.resolve({ applicable: false, reason: reason })
michael@0 68 );
michael@0 69
michael@0 70 return deferred.promise;
michael@0 71 }
michael@0 72
michael@0 73 add_task(function* test_startStop() {
michael@0 74 let baseDate = new Date(2014, 5, 1, 12);
michael@0 75 let startDate = futureDate(baseDate, 30 * MS_IN_ONE_DAY);
michael@0 76 let endDate = futureDate(baseDate, 60 * MS_IN_ONE_DAY);
michael@0 77 let manifestData = new ManifestEntry({
michael@0 78 startTime: dateToSeconds(startDate),
michael@0 79 endTime: dateToSeconds(endDate),
michael@0 80 maxActiveSeconds: 10 * SEC_IN_ONE_DAY,
michael@0 81 });
michael@0 82 let experiment = new Experiments.ExperimentEntry(gPolicy);
michael@0 83 experiment.initFromManifestData(manifestData);
michael@0 84
michael@0 85 // We need to associate it with the singleton so the onInstallStarted
michael@0 86 // Addon Manager listener will know about it.
michael@0 87 Experiments.instance()._experiments = new Map();
michael@0 88 Experiments.instance()._experiments.set(experiment.id, experiment);
michael@0 89
michael@0 90 let result;
michael@0 91
michael@0 92 defineNow(gPolicy, baseDate);
michael@0 93 result = yield isApplicable(experiment);
michael@0 94 Assert.equal(result.applicable, false, "Experiment should not be applicable.");
michael@0 95 Assert.equal(experiment.enabled, false, "Experiment should not be enabled.");
michael@0 96
michael@0 97 let addons = yield getExperimentAddons();
michael@0 98 Assert.equal(addons.length, 0, "No experiment add-ons are installed.");
michael@0 99
michael@0 100 defineNow(gPolicy, futureDate(startDate, 5 * MS_IN_ONE_DAY));
michael@0 101 result = yield isApplicable(experiment);
michael@0 102 Assert.equal(result.applicable, true, "Experiment should now be applicable.");
michael@0 103 Assert.equal(experiment.enabled, false, "Experiment should not be enabled.");
michael@0 104
michael@0 105 let changes = yield experiment.start();
michael@0 106 Assert.equal(changes, experiment.ADDON_CHANGE_INSTALL, "Add-on was installed.");
michael@0 107 addons = yield getExperimentAddons();
michael@0 108 Assert.equal(experiment.enabled, true, "Experiment should now be enabled.");
michael@0 109 Assert.equal(addons.length, 1, "1 experiment add-on is installed.");
michael@0 110 Assert.equal(addons[0].id, experiment._addonId, "The add-on is the one we expect.");
michael@0 111 Assert.equal(addons[0].userDisabled, false, "The add-on is not userDisabled.");
michael@0 112 Assert.ok(addons[0].isActive, "The add-on is active.");
michael@0 113
michael@0 114 changes = yield experiment.stop();
michael@0 115 Assert.equal(changes, experiment.ADDON_CHANGE_UNINSTALL, "Add-on was uninstalled.");
michael@0 116 addons = yield getExperimentAddons();
michael@0 117 Assert.equal(experiment.enabled, false, "Experiment should not be enabled.");
michael@0 118 Assert.equal(addons.length, 0, "Experiment should be uninstalled from the Addon Manager.");
michael@0 119
michael@0 120 changes = yield experiment.start();
michael@0 121 Assert.equal(changes, experiment.ADDON_CHANGE_INSTALL, "Add-on was installed.");
michael@0 122 addons = yield getExperimentAddons();
michael@0 123 Assert.equal(experiment.enabled, true, "Experiment should now be enabled.");
michael@0 124 Assert.equal(addons.length, 1, "1 experiment add-on is installed.");
michael@0 125 Assert.equal(addons[0].id, experiment._addonId, "The add-on is the one we expect.");
michael@0 126 Assert.equal(addons[0].userDisabled, false, "The add-on is not userDisabled.");
michael@0 127 Assert.ok(addons[0].isActive, "The add-on is active.");
michael@0 128
michael@0 129 let result = yield experiment.shouldStop();
michael@0 130 Assert.equal(result.shouldStop, false, "shouldStop should be false.");
michael@0 131 Assert.equal(experiment.enabled, true, "Experiment should be enabled.");
michael@0 132 addons = yield getExperimentAddons();
michael@0 133 Assert.equal(addons.length, 1, "Experiment still in add-ons manager.");
michael@0 134 Assert.ok(addons[0].isActive, "The add-on is still active.");
michael@0 135
michael@0 136 defineNow(gPolicy, futureDate(endDate, MS_IN_ONE_DAY));
michael@0 137 result = yield experiment.shouldStop();
michael@0 138 Assert.equal(result.shouldStop, true, "shouldStop should now be true.");
michael@0 139 changes = yield experiment.stop();
michael@0 140 Assert.equal(changes, experiment.ADDON_CHANGE_UNINSTALL, "Add-on should be uninstalled.");
michael@0 141 Assert.equal(experiment.enabled, false, "Experiment should be disabled.");
michael@0 142 addons = yield getExperimentAddons();
michael@0 143 Assert.equal(addons.length, 0, "Experiment add-on is uninstalled.");
michael@0 144
michael@0 145 // Ensure hash validation works.
michael@0 146 // We set an incorrect hash and expect the install to fail.
michael@0 147 experiment._manifestData.xpiHash = "sha1:41014dcc66b4dcedcd973491a1530a32f0517d8a";
michael@0 148 let errored = false;
michael@0 149 try {
michael@0 150 yield experiment.start();
michael@0 151 } catch (ex) {
michael@0 152 errored = true;
michael@0 153 }
michael@0 154 Assert.ok(experiment._failedStart, "Experiment failed to start.");
michael@0 155 Assert.ok(errored, "start() threw an exception.");
michael@0 156
michael@0 157 // Make sure "ignore hashes" mode works.
michael@0 158 gPolicy.ignoreHashes = true;
michael@0 159 let changes = yield experiment.start();
michael@0 160 Assert.equal(changes, experiment.ADDON_CHANGE_INSTALL);
michael@0 161 yield experiment.stop();
michael@0 162 gPolicy.ignoreHashes = false;
michael@0 163 });

mercurial