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://gre/modules/Promise.jsm"); |
michael@0 | 7 | Cu.import("resource:///modules/experiments/Experiments.jsm"); |
michael@0 | 8 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 9 | |
michael@0 | 10 | let gDataRoot; |
michael@0 | 11 | let gHttpServer; |
michael@0 | 12 | let gManifestObject; |
michael@0 | 13 | |
michael@0 | 14 | function run_test() { |
michael@0 | 15 | run_next_test(); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | add_task(function test_setup() { |
michael@0 | 19 | loadAddonManager(); |
michael@0 | 20 | do_get_profile(); |
michael@0 | 21 | |
michael@0 | 22 | gHttpServer = new HttpServer(); |
michael@0 | 23 | gHttpServer.start(-1); |
michael@0 | 24 | let httpRoot = "http://localhost:" + gHttpServer.identity.primaryPort + "/"; |
michael@0 | 25 | gDataRoot = httpRoot + "data/"; |
michael@0 | 26 | gHttpServer.registerDirectory("/data/", do_get_cwd()); |
michael@0 | 27 | gHttpServer.registerPathHandler("/manifests/handler", (req, res) => { |
michael@0 | 28 | res.setStatusLine(null, 200, "OK"); |
michael@0 | 29 | res.write(JSON.stringify(gManifestObject)); |
michael@0 | 30 | res.processAsync(); |
michael@0 | 31 | res.finish(); |
michael@0 | 32 | }); |
michael@0 | 33 | do_register_cleanup(() => gHttpServer.stop(() => {})); |
michael@0 | 34 | |
michael@0 | 35 | Services.prefs.setBoolPref("experiments.enabled", true); |
michael@0 | 36 | Services.prefs.setCharPref("experiments.manifest.uri", |
michael@0 | 37 | httpRoot + "manifests/handler"); |
michael@0 | 38 | Services.prefs.setBoolPref("experiments.logging.dump", true); |
michael@0 | 39 | Services.prefs.setCharPref("experiments.logging.level", "Trace"); |
michael@0 | 40 | disableCertificateChecks(); |
michael@0 | 41 | }); |
michael@0 | 42 | |
michael@0 | 43 | add_task(function* test_provider_basic() { |
michael@0 | 44 | let e = Experiments.instance(); |
michael@0 | 45 | |
michael@0 | 46 | let provider = new Experiments.PreviousExperimentProvider(e); |
michael@0 | 47 | e._setPreviousExperimentsProvider(provider); |
michael@0 | 48 | |
michael@0 | 49 | let deferred = Promise.defer(); |
michael@0 | 50 | provider.getAddonsByTypes(["experiment"], (addons) => { |
michael@0 | 51 | deferred.resolve(addons); |
michael@0 | 52 | }); |
michael@0 | 53 | let addons = yield deferred.promise; |
michael@0 | 54 | Assert.ok(Array.isArray(addons), "getAddonsByTypes returns an Array."); |
michael@0 | 55 | Assert.equal(addons.length, 0, "No previous add-ons returned."); |
michael@0 | 56 | |
michael@0 | 57 | gManifestObject = { |
michael@0 | 58 | version: 1, |
michael@0 | 59 | experiments: [ |
michael@0 | 60 | { |
michael@0 | 61 | id: EXPERIMENT1_ID, |
michael@0 | 62 | xpiURL: gDataRoot + EXPERIMENT1_XPI_NAME, |
michael@0 | 63 | xpiHash: EXPERIMENT1_XPI_SHA1, |
michael@0 | 64 | startTime: Date.now() / 1000 - 60, |
michael@0 | 65 | endTime: Date.now() / 1000 + 60, |
michael@0 | 66 | maxActiveSeconds: 60, |
michael@0 | 67 | appName: ["XPCShell"], |
michael@0 | 68 | channel: [e._policy.updatechannel()], |
michael@0 | 69 | }, |
michael@0 | 70 | ], |
michael@0 | 71 | }; |
michael@0 | 72 | |
michael@0 | 73 | yield e.updateManifest(); |
michael@0 | 74 | |
michael@0 | 75 | deferred = Promise.defer(); |
michael@0 | 76 | provider.getAddonsByTypes(["experiment"], (addons) => { |
michael@0 | 77 | deferred.resolve(addons); |
michael@0 | 78 | }); |
michael@0 | 79 | addons = yield deferred.promise; |
michael@0 | 80 | Assert.equal(addons.length, 0, "Still no previous experiment."); |
michael@0 | 81 | |
michael@0 | 82 | let experiments = yield e.getExperiments(); |
michael@0 | 83 | Assert.equal(experiments.length, 1, "1 experiment present."); |
michael@0 | 84 | Assert.ok(experiments[0].active, "It is active."); |
michael@0 | 85 | |
michael@0 | 86 | // Deactivate it. |
michael@0 | 87 | defineNow(e._policy, new Date(gManifestObject.experiments[0].endTime * 1000 + 1000)); |
michael@0 | 88 | yield e.updateManifest(); |
michael@0 | 89 | |
michael@0 | 90 | experiments = yield e.getExperiments(); |
michael@0 | 91 | Assert.equal(experiments.length, 1, "1 experiment present."); |
michael@0 | 92 | Assert.equal(experiments[0].active, false, "It isn't active."); |
michael@0 | 93 | |
michael@0 | 94 | deferred = Promise.defer(); |
michael@0 | 95 | provider.getAddonsByTypes(["experiment"], (addons) => { |
michael@0 | 96 | deferred.resolve(addons); |
michael@0 | 97 | }); |
michael@0 | 98 | addons = yield deferred.promise; |
michael@0 | 99 | Assert.equal(addons.length, 1, "1 previous add-on known."); |
michael@0 | 100 | Assert.equal(addons[0].id, EXPERIMENT1_ID, "ID matches expected."); |
michael@0 | 101 | |
michael@0 | 102 | deferred = Promise.defer(); |
michael@0 | 103 | provider.getAddonByID(EXPERIMENT1_ID, (addon) => { |
michael@0 | 104 | deferred.resolve(addon); |
michael@0 | 105 | }); |
michael@0 | 106 | let addon = yield deferred.promise; |
michael@0 | 107 | Assert.ok(addon, "We got an add-on from its ID."); |
michael@0 | 108 | Assert.equal(addon.id, EXPERIMENT1_ID, "ID matches expected."); |
michael@0 | 109 | Assert.ok(addon.appDisabled, "Add-on is a previous experiment."); |
michael@0 | 110 | Assert.ok(addon.userDisabled, "Add-on is disabled."); |
michael@0 | 111 | Assert.equal(addon.type, "experiment", "Add-on is an experiment."); |
michael@0 | 112 | Assert.equal(addon.isActive, false, "Add-on is not active."); |
michael@0 | 113 | Assert.equal(addon.permissions, 0, "Add-on has no permissions."); |
michael@0 | 114 | |
michael@0 | 115 | deferred = Promise.defer(); |
michael@0 | 116 | AddonManager.getAddonsByTypes(["experiment"], (addons) => { |
michael@0 | 117 | deferred.resolve(addons); |
michael@0 | 118 | }); |
michael@0 | 119 | addons = yield deferred.promise; |
michael@0 | 120 | Assert.equal(addons.length, 1, "Got 1 experiment from add-on manager."); |
michael@0 | 121 | Assert.equal(addons[0].id, EXPERIMENT1_ID, "ID matches expected."); |
michael@0 | 122 | Assert.ok(addons[0].appDisabled, "It is a previous experiment add-on."); |
michael@0 | 123 | }); |
michael@0 | 124 | |
michael@0 | 125 | add_task(function* test_active_and_previous() { |
michael@0 | 126 | // Building on the previous test, activate experiment 2. |
michael@0 | 127 | let e = Experiments.instance(); |
michael@0 | 128 | let provider = new Experiments.PreviousExperimentProvider(e); |
michael@0 | 129 | e._setPreviousExperimentsProvider(provider); |
michael@0 | 130 | |
michael@0 | 131 | gManifestObject = { |
michael@0 | 132 | version: 1, |
michael@0 | 133 | experiments: [ |
michael@0 | 134 | { |
michael@0 | 135 | id: EXPERIMENT2_ID, |
michael@0 | 136 | xpiURL: gDataRoot + EXPERIMENT2_XPI_NAME, |
michael@0 | 137 | xpiHash: EXPERIMENT2_XPI_SHA1, |
michael@0 | 138 | startTime: Date.now() / 1000 - 60, |
michael@0 | 139 | endTime: Date.now() / 1000 + 60, |
michael@0 | 140 | maxActiveSeconds: 60, |
michael@0 | 141 | appName: ["XPCShell"], |
michael@0 | 142 | channel: [e._policy.updatechannel()], |
michael@0 | 143 | }, |
michael@0 | 144 | ], |
michael@0 | 145 | }; |
michael@0 | 146 | |
michael@0 | 147 | defineNow(e._policy, new Date()); |
michael@0 | 148 | yield e.updateManifest(); |
michael@0 | 149 | |
michael@0 | 150 | let experiments = yield e.getExperiments(); |
michael@0 | 151 | Assert.equal(experiments.length, 2, "2 experiments known."); |
michael@0 | 152 | |
michael@0 | 153 | let deferred = Promise.defer(); |
michael@0 | 154 | provider.getAddonsByTypes(["experiment"], (addons) => { |
michael@0 | 155 | deferred.resolve(addons); |
michael@0 | 156 | }); |
michael@0 | 157 | let addons = yield deferred.promise; |
michael@0 | 158 | Assert.equal(addons.length, 1, "1 previous experiment."); |
michael@0 | 159 | |
michael@0 | 160 | deferred = Promise.defer(); |
michael@0 | 161 | AddonManager.getAddonsByTypes(["experiment"], (addons) => { |
michael@0 | 162 | deferred.resolve(addons); |
michael@0 | 163 | }); |
michael@0 | 164 | addons = yield deferred.promise; |
michael@0 | 165 | Assert.equal(addons.length, 2, "2 experiment add-ons known."); |
michael@0 | 166 | |
michael@0 | 167 | for (let addon of addons) { |
michael@0 | 168 | if (addon.id == EXPERIMENT1_ID) { |
michael@0 | 169 | Assert.equal(addon.isActive, false, "Add-on is not active."); |
michael@0 | 170 | Assert.ok(addon.appDisabled, "Should be a previous experiment."); |
michael@0 | 171 | } |
michael@0 | 172 | else if (addon.id == EXPERIMENT2_ID) { |
michael@0 | 173 | Assert.ok(addon.isActive, "Add-on is active."); |
michael@0 | 174 | Assert.ok(!addon.appDisabled, "Should not be a previous experiment."); |
michael@0 | 175 | } |
michael@0 | 176 | else { |
michael@0 | 177 | throw new Error("Unexpected add-on ID: " + addon.id); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | }); |