Wed, 31 Dec 2014 06:09:35 +0100
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://testing-common/AddonManagerTesting.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | XPCOMUtils.defineLazyModuleGetter(this, "Experiments", |
michael@0 | 10 | "resource:///modules/experiments/Experiments.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const FILE_MANIFEST = "experiments.manifest"; |
michael@0 | 13 | const MANIFEST_HANDLER = "manifests/handler"; |
michael@0 | 14 | |
michael@0 | 15 | const SEC_IN_ONE_DAY = 24 * 60 * 60; |
michael@0 | 16 | const MS_IN_ONE_DAY = SEC_IN_ONE_DAY * 1000; |
michael@0 | 17 | |
michael@0 | 18 | let gProfileDir = null; |
michael@0 | 19 | let gHttpServer = null; |
michael@0 | 20 | let gHttpRoot = null; |
michael@0 | 21 | let gDataRoot = null; |
michael@0 | 22 | let gReporter = null; |
michael@0 | 23 | let gPolicy = null; |
michael@0 | 24 | let gManifestObject = null; |
michael@0 | 25 | let gManifestHandlerURI = null; |
michael@0 | 26 | |
michael@0 | 27 | function run_test() { |
michael@0 | 28 | run_next_test(); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | add_task(function* test_setup() { |
michael@0 | 32 | loadAddonManager(); |
michael@0 | 33 | gProfileDir = do_get_profile(); |
michael@0 | 34 | |
michael@0 | 35 | gHttpServer = new HttpServer(); |
michael@0 | 36 | gHttpServer.start(-1); |
michael@0 | 37 | let port = gHttpServer.identity.primaryPort; |
michael@0 | 38 | gHttpRoot = "http://localhost:" + port + "/"; |
michael@0 | 39 | gDataRoot = gHttpRoot + "data/"; |
michael@0 | 40 | gManifestHandlerURI = gHttpRoot + MANIFEST_HANDLER; |
michael@0 | 41 | gHttpServer.registerDirectory("/data/", do_get_cwd()); |
michael@0 | 42 | gHttpServer.registerPathHandler("/" + MANIFEST_HANDLER, (request, response) => { |
michael@0 | 43 | response.setStatusLine(null, 200, "OK"); |
michael@0 | 44 | response.write(JSON.stringify(gManifestObject)); |
michael@0 | 45 | response.processAsync(); |
michael@0 | 46 | response.finish(); |
michael@0 | 47 | }); |
michael@0 | 48 | do_register_cleanup(() => gHttpServer.stop(() => {})); |
michael@0 | 49 | |
michael@0 | 50 | disableCertificateChecks(); |
michael@0 | 51 | |
michael@0 | 52 | Services.prefs.setBoolPref(PREF_EXPERIMENTS_ENABLED, true); |
michael@0 | 53 | Services.prefs.setIntPref(PREF_LOGGING_LEVEL, 0); |
michael@0 | 54 | Services.prefs.setBoolPref(PREF_LOGGING_DUMP, true); |
michael@0 | 55 | Services.prefs.setCharPref(PREF_MANIFEST_URI, gManifestHandlerURI); |
michael@0 | 56 | Services.prefs.setIntPref(PREF_FETCHINTERVAL, 0); |
michael@0 | 57 | |
michael@0 | 58 | gReporter = yield getReporter("json_payload_simple"); |
michael@0 | 59 | yield gReporter.collectMeasurements(); |
michael@0 | 60 | let payload = yield gReporter.getJSONPayload(true); |
michael@0 | 61 | do_register_cleanup(() => gReporter._shutdown()); |
michael@0 | 62 | |
michael@0 | 63 | gPolicy = new Experiments.Policy(); |
michael@0 | 64 | patchPolicy(gPolicy, { |
michael@0 | 65 | updatechannel: () => "nightly", |
michael@0 | 66 | healthReportPayload: () => Promise.resolve(payload), |
michael@0 | 67 | oneshotTimer: (callback, timeout, thisObj, name) => {}, |
michael@0 | 68 | }); |
michael@0 | 69 | }); |
michael@0 | 70 | |
michael@0 | 71 | // Test disabling the feature stops current and future experiments. |
michael@0 | 72 | |
michael@0 | 73 | add_task(function* test_disableExperiments() { |
michael@0 | 74 | const OBSERVER_TOPIC = "experiments-changed"; |
michael@0 | 75 | let observerFireCount = 0; |
michael@0 | 76 | let expectedObserverFireCount = 0; |
michael@0 | 77 | let observer = () => ++observerFireCount; |
michael@0 | 78 | Services.obs.addObserver(observer, OBSERVER_TOPIC, false); |
michael@0 | 79 | |
michael@0 | 80 | // Dates the following tests are based on. |
michael@0 | 81 | |
michael@0 | 82 | let baseDate = new Date(2014, 5, 1, 12); |
michael@0 | 83 | let startDate1 = futureDate(baseDate, 50 * MS_IN_ONE_DAY); |
michael@0 | 84 | let endDate1 = futureDate(baseDate, 100 * MS_IN_ONE_DAY); |
michael@0 | 85 | let startDate2 = futureDate(baseDate, 150 * MS_IN_ONE_DAY); |
michael@0 | 86 | let endDate2 = futureDate(baseDate, 200 * MS_IN_ONE_DAY); |
michael@0 | 87 | |
michael@0 | 88 | // The manifest data we test with. |
michael@0 | 89 | |
michael@0 | 90 | gManifestObject = { |
michael@0 | 91 | "version": 1, |
michael@0 | 92 | experiments: [ |
michael@0 | 93 | { |
michael@0 | 94 | id: EXPERIMENT2_ID, |
michael@0 | 95 | xpiURL: gDataRoot + EXPERIMENT2_XPI_NAME, |
michael@0 | 96 | xpiHash: EXPERIMENT2_XPI_SHA1, |
michael@0 | 97 | startTime: dateToSeconds(startDate2), |
michael@0 | 98 | endTime: dateToSeconds(endDate2), |
michael@0 | 99 | maxActiveSeconds: 10 * SEC_IN_ONE_DAY, |
michael@0 | 100 | appName: ["XPCShell"], |
michael@0 | 101 | channel: ["nightly"], |
michael@0 | 102 | }, |
michael@0 | 103 | { |
michael@0 | 104 | id: EXPERIMENT1_ID, |
michael@0 | 105 | xpiURL: gDataRoot + EXPERIMENT1_XPI_NAME, |
michael@0 | 106 | xpiHash: EXPERIMENT1_XPI_SHA1, |
michael@0 | 107 | startTime: dateToSeconds(startDate1), |
michael@0 | 108 | endTime: dateToSeconds(endDate1), |
michael@0 | 109 | maxActiveSeconds: 10 * SEC_IN_ONE_DAY, |
michael@0 | 110 | appName: ["XPCShell"], |
michael@0 | 111 | channel: ["nightly"], |
michael@0 | 112 | }, |
michael@0 | 113 | ], |
michael@0 | 114 | }; |
michael@0 | 115 | |
michael@0 | 116 | let experiments = new Experiments.Experiments(gPolicy); |
michael@0 | 117 | |
michael@0 | 118 | // Trigger update, clock set to before any activation. |
michael@0 | 119 | // Use updateManifest() to provide for coverage of that path. |
michael@0 | 120 | |
michael@0 | 121 | let now = baseDate; |
michael@0 | 122 | defineNow(gPolicy, now); |
michael@0 | 123 | |
michael@0 | 124 | yield experiments.updateManifest(); |
michael@0 | 125 | Assert.equal(observerFireCount, ++expectedObserverFireCount, |
michael@0 | 126 | "Experiments observer should have been called."); |
michael@0 | 127 | let list = yield experiments.getExperiments(); |
michael@0 | 128 | Assert.equal(list.length, 0, "Experiment list should be empty."); |
michael@0 | 129 | let addons = yield getExperimentAddons(); |
michael@0 | 130 | Assert.equal(addons.length, 0, "Precondition: No experiment add-ons are installed."); |
michael@0 | 131 | |
michael@0 | 132 | // Trigger update, clock set for experiment 1 to start. |
michael@0 | 133 | |
michael@0 | 134 | now = futureDate(startDate1, 5 * MS_IN_ONE_DAY); |
michael@0 | 135 | defineNow(gPolicy, now); |
michael@0 | 136 | |
michael@0 | 137 | yield experiments.updateManifest(); |
michael@0 | 138 | Assert.equal(observerFireCount, ++expectedObserverFireCount, |
michael@0 | 139 | "Experiments observer should have been called."); |
michael@0 | 140 | |
michael@0 | 141 | list = yield experiments.getExperiments(); |
michael@0 | 142 | Assert.equal(list.length, 1, "Experiment list should have 1 entry now."); |
michael@0 | 143 | Assert.equal(list[0].active, true, "Experiment should be active."); |
michael@0 | 144 | addons = yield getExperimentAddons(); |
michael@0 | 145 | Assert.equal(addons.length, 1, "An experiment add-on was installed."); |
michael@0 | 146 | |
michael@0 | 147 | // Disable the experiments feature. Check that we stop the running experiment. |
michael@0 | 148 | |
michael@0 | 149 | Services.prefs.setBoolPref(PREF_EXPERIMENTS_ENABLED, false); |
michael@0 | 150 | yield experiments._mainTask; |
michael@0 | 151 | |
michael@0 | 152 | Assert.equal(observerFireCount, ++expectedObserverFireCount, |
michael@0 | 153 | "Experiments observer should have been called."); |
michael@0 | 154 | |
michael@0 | 155 | list = yield experiments.getExperiments(); |
michael@0 | 156 | Assert.equal(list.length, 1, "Experiment list should have 1 entry."); |
michael@0 | 157 | Assert.equal(list[0].active, false, "Experiment entry should not be active."); |
michael@0 | 158 | addons = yield getExperimentAddons(); |
michael@0 | 159 | Assert.equal(addons.length, 0, "The experiment add-on should be uninstalled."); |
michael@0 | 160 | |
michael@0 | 161 | // Trigger update, clock set for experiment 2 to start. Verify we don't start it. |
michael@0 | 162 | |
michael@0 | 163 | now = startDate2; |
michael@0 | 164 | defineNow(gPolicy, now); |
michael@0 | 165 | |
michael@0 | 166 | try { |
michael@0 | 167 | yield experiments.updateManifest(); |
michael@0 | 168 | } catch (e if e.message == "experiments are disabled") { |
michael@0 | 169 | // This exception is expected. |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | experiments.notify(); |
michael@0 | 173 | yield experiments._mainTask; |
michael@0 | 174 | |
michael@0 | 175 | Assert.equal(observerFireCount, expectedObserverFireCount, |
michael@0 | 176 | "Experiments observer should not have been called."); |
michael@0 | 177 | |
michael@0 | 178 | list = yield experiments.getExperiments(); |
michael@0 | 179 | Assert.equal(list.length, 1, "Experiment list should still have 1 entry."); |
michael@0 | 180 | Assert.equal(list[0].active, false, "Experiment entry should not be active."); |
michael@0 | 181 | addons = yield getExperimentAddons(); |
michael@0 | 182 | Assert.equal(addons.length, 0, "There should still be no experiment add-on installed."); |
michael@0 | 183 | |
michael@0 | 184 | // Cleanup. |
michael@0 | 185 | |
michael@0 | 186 | Services.obs.removeObserver(observer, OBSERVER_TOPIC); |
michael@0 | 187 | yield experiments.uninit(); |
michael@0 | 188 | yield removeCacheFile(); |
michael@0 | 189 | }); |