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 | |
michael@0 | 5 | XPCOMUtils.defineLazyModuleGetter(this, "Promise", |
michael@0 | 6 | "resource://gre/modules/Promise.jsm"); |
michael@0 | 7 | XPCOMUtils.defineLazyModuleGetter(this, "Task", |
michael@0 | 8 | "resource://gre/modules/Task.jsm"); |
michael@0 | 9 | XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts", |
michael@0 | 10 | "resource://gre/modules/FxAccounts.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const CHROME_BASE = "chrome://mochitests/content/browser/browser/base/content/test/general/"; |
michael@0 | 13 | // Preference helpers. |
michael@0 | 14 | let changedPrefs = new Set(); |
michael@0 | 15 | |
michael@0 | 16 | function setPref(name, value) { |
michael@0 | 17 | changedPrefs.add(name); |
michael@0 | 18 | Services.prefs.setCharPref(name, value); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | registerCleanupFunction(function() { |
michael@0 | 22 | // Ensure we don't pollute prefs for next tests. |
michael@0 | 23 | for (let name of changedPrefs) { |
michael@0 | 24 | Services.prefs.clearUserPref(name); |
michael@0 | 25 | } |
michael@0 | 26 | }); |
michael@0 | 27 | |
michael@0 | 28 | let gTests = [ |
michael@0 | 29 | { |
michael@0 | 30 | desc: "Test the remote commands", |
michael@0 | 31 | teardown: function* () { |
michael@0 | 32 | gBrowser.removeCurrentTab(); |
michael@0 | 33 | yield fxAccounts.signOut(); |
michael@0 | 34 | }, |
michael@0 | 35 | run: function* () |
michael@0 | 36 | { |
michael@0 | 37 | setPref("identity.fxaccounts.remote.signup.uri", |
michael@0 | 38 | "https://example.com/browser/browser/base/content/test/general/accounts_testRemoteCommands.html"); |
michael@0 | 39 | yield promiseNewTabLoadEvent("about:accounts"); |
michael@0 | 40 | |
michael@0 | 41 | let deferred = Promise.defer(); |
michael@0 | 42 | |
michael@0 | 43 | let results = 0; |
michael@0 | 44 | try { |
michael@0 | 45 | let win = gBrowser.contentWindow; |
michael@0 | 46 | win.addEventListener("message", function testLoad(e) { |
michael@0 | 47 | if (e.data.type == "testResult") { |
michael@0 | 48 | ok(e.data.pass, e.data.info); |
michael@0 | 49 | results++; |
michael@0 | 50 | } |
michael@0 | 51 | else if (e.data.type == "testsComplete") { |
michael@0 | 52 | is(results, e.data.count, "Checking number of results received matches the number of tests that should have run"); |
michael@0 | 53 | win.removeEventListener("message", testLoad, false, true); |
michael@0 | 54 | deferred.resolve(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | }, false, true); |
michael@0 | 58 | |
michael@0 | 59 | } catch(e) { |
michael@0 | 60 | ok(false, "Failed to get all commands"); |
michael@0 | 61 | deferred.reject(); |
michael@0 | 62 | } |
michael@0 | 63 | yield deferred.promise; |
michael@0 | 64 | } |
michael@0 | 65 | }, |
michael@0 | 66 | { |
michael@0 | 67 | desc: "Test action=signin - no user logged in", |
michael@0 | 68 | teardown: () => gBrowser.removeCurrentTab(), |
michael@0 | 69 | run: function* () |
michael@0 | 70 | { |
michael@0 | 71 | // When this loads with no user logged-in, we expect the "normal" URL |
michael@0 | 72 | const expected_url = "https://example.com/?is_sign_in"; |
michael@0 | 73 | setPref("identity.fxaccounts.remote.signin.uri", expected_url); |
michael@0 | 74 | let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=signin"); |
michael@0 | 75 | is(url, expected_url, "action=signin got the expected URL"); |
michael@0 | 76 | // we expect the remote iframe to be shown. |
michael@0 | 77 | yield checkVisibilities(tab, { |
michael@0 | 78 | stage: false, // parent of 'manage' and 'intro' |
michael@0 | 79 | manage: false, |
michael@0 | 80 | intro: false, // this is "get started" |
michael@0 | 81 | remote: true |
michael@0 | 82 | }); |
michael@0 | 83 | } |
michael@0 | 84 | }, |
michael@0 | 85 | { |
michael@0 | 86 | desc: "Test action=signin - user logged in", |
michael@0 | 87 | teardown: function* () { |
michael@0 | 88 | gBrowser.removeCurrentTab(); |
michael@0 | 89 | yield signOut(); |
michael@0 | 90 | }, |
michael@0 | 91 | run: function* () |
michael@0 | 92 | { |
michael@0 | 93 | // When this loads with a user logged-in, we expect the normal URL to |
michael@0 | 94 | // have been ignored and the "manage" page to be shown. |
michael@0 | 95 | const expected_url = "https://example.com/?is_sign_in"; |
michael@0 | 96 | setPref("identity.fxaccounts.remote.signin.uri", expected_url); |
michael@0 | 97 | yield setSignedInUser(); |
michael@0 | 98 | let tab = yield promiseNewTabLoadEvent("about:accounts?action=signin"); |
michael@0 | 99 | // about:accounts initializes after fetching the current user from Fxa - |
michael@0 | 100 | // so we also request it - by the time we get it we know it should have |
michael@0 | 101 | // done its thing. |
michael@0 | 102 | yield fxAccounts.getSignedInUser(); |
michael@0 | 103 | // we expect "manage" to be shown. |
michael@0 | 104 | yield checkVisibilities(tab, { |
michael@0 | 105 | stage: true, // parent of 'manage' and 'intro' |
michael@0 | 106 | manage: true, |
michael@0 | 107 | intro: false, // this is "get started" |
michael@0 | 108 | remote: false |
michael@0 | 109 | }); |
michael@0 | 110 | } |
michael@0 | 111 | }, |
michael@0 | 112 | { |
michael@0 | 113 | desc: "Test action=signup - no user logged in", |
michael@0 | 114 | teardown: () => gBrowser.removeCurrentTab(), |
michael@0 | 115 | run: function* () |
michael@0 | 116 | { |
michael@0 | 117 | const expected_url = "https://example.com/?is_sign_up"; |
michael@0 | 118 | setPref("identity.fxaccounts.remote.signup.uri", expected_url); |
michael@0 | 119 | let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=signup"); |
michael@0 | 120 | is(url, expected_url, "action=signup got the expected URL"); |
michael@0 | 121 | // we expect the remote iframe to be shown. |
michael@0 | 122 | yield checkVisibilities(tab, { |
michael@0 | 123 | stage: false, // parent of 'manage' and 'intro' |
michael@0 | 124 | manage: false, |
michael@0 | 125 | intro: false, // this is "get started" |
michael@0 | 126 | remote: true |
michael@0 | 127 | }); |
michael@0 | 128 | }, |
michael@0 | 129 | }, |
michael@0 | 130 | { |
michael@0 | 131 | desc: "Test action=signup - user logged in", |
michael@0 | 132 | teardown: () => gBrowser.removeCurrentTab(), |
michael@0 | 133 | run: function* () |
michael@0 | 134 | { |
michael@0 | 135 | const expected_url = "https://example.com/?is_sign_up"; |
michael@0 | 136 | setPref("identity.fxaccounts.remote.signup.uri", expected_url); |
michael@0 | 137 | yield setSignedInUser(); |
michael@0 | 138 | let tab = yield promiseNewTabLoadEvent("about:accounts?action=signup"); |
michael@0 | 139 | yield fxAccounts.getSignedInUser(); |
michael@0 | 140 | // we expect "manage" to be shown. |
michael@0 | 141 | yield checkVisibilities(tab, { |
michael@0 | 142 | stage: true, // parent of 'manage' and 'intro' |
michael@0 | 143 | manage: true, |
michael@0 | 144 | intro: false, // this is "get started" |
michael@0 | 145 | remote: false |
michael@0 | 146 | }); |
michael@0 | 147 | }, |
michael@0 | 148 | }, |
michael@0 | 149 | { |
michael@0 | 150 | desc: "Test action=reauth", |
michael@0 | 151 | teardown: function* () { |
michael@0 | 152 | gBrowser.removeCurrentTab(); |
michael@0 | 153 | yield signOut(); |
michael@0 | 154 | }, |
michael@0 | 155 | run: function* () |
michael@0 | 156 | { |
michael@0 | 157 | const expected_url = "https://example.com/?is_force_auth"; |
michael@0 | 158 | setPref("identity.fxaccounts.remote.force_auth.uri", expected_url); |
michael@0 | 159 | let userData = { |
michael@0 | 160 | email: "foo@example.com", |
michael@0 | 161 | uid: "1234@lcip.org", |
michael@0 | 162 | assertion: "foobar", |
michael@0 | 163 | sessionToken: "dead", |
michael@0 | 164 | kA: "beef", |
michael@0 | 165 | kB: "cafe", |
michael@0 | 166 | verified: true |
michael@0 | 167 | }; |
michael@0 | 168 | |
michael@0 | 169 | yield setSignedInUser(); |
michael@0 | 170 | let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=reauth"); |
michael@0 | 171 | // The current user will be appended to the url |
michael@0 | 172 | let expected = expected_url + "&email=foo%40example.com"; |
michael@0 | 173 | is(url, expected, "action=reauth got the expected URL"); |
michael@0 | 174 | }, |
michael@0 | 175 | }, |
michael@0 | 176 | { |
michael@0 | 177 | desc: "Test observers about:accounts", |
michael@0 | 178 | teardown: function() { |
michael@0 | 179 | gBrowser.removeCurrentTab(); |
michael@0 | 180 | }, |
michael@0 | 181 | run: function* () { |
michael@0 | 182 | setPref("identity.fxaccounts.remote.signup.uri", "https://example.com/"); |
michael@0 | 183 | yield setSignedInUser(); |
michael@0 | 184 | let tab = yield promiseNewTabLoadEvent("about:accounts"); |
michael@0 | 185 | // sign the user out - the tab should have action=signin |
michael@0 | 186 | yield signOut(); |
michael@0 | 187 | // wait for the new load. |
michael@0 | 188 | yield promiseOneMessage(tab, "test:document:load"); |
michael@0 | 189 | is(tab.linkedBrowser.contentDocument.location.href, "about:accounts?action=signin"); |
michael@0 | 190 | } |
michael@0 | 191 | }, |
michael@0 | 192 | ]; // gTests |
michael@0 | 193 | |
michael@0 | 194 | function test() |
michael@0 | 195 | { |
michael@0 | 196 | waitForExplicitFinish(); |
michael@0 | 197 | |
michael@0 | 198 | Task.spawn(function () { |
michael@0 | 199 | for (let test of gTests) { |
michael@0 | 200 | info(test.desc); |
michael@0 | 201 | try { |
michael@0 | 202 | yield test.run(); |
michael@0 | 203 | } finally { |
michael@0 | 204 | yield test.teardown(); |
michael@0 | 205 | } |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | finish(); |
michael@0 | 209 | }); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | function promiseOneMessage(tab, messageName) { |
michael@0 | 213 | let mm = tab.linkedBrowser.messageManager; |
michael@0 | 214 | let deferred = Promise.defer(); |
michael@0 | 215 | mm.addMessageListener(messageName, function onmessage(message) { |
michael@0 | 216 | mm.removeMessageListener(messageName, onmessage); |
michael@0 | 217 | deferred.resolve(message); |
michael@0 | 218 | }); |
michael@0 | 219 | return deferred.promise; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | function promiseNewTabLoadEvent(aUrl) |
michael@0 | 223 | { |
michael@0 | 224 | let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl); |
michael@0 | 225 | let browser = tab.linkedBrowser; |
michael@0 | 226 | let mm = browser.messageManager; |
michael@0 | 227 | |
michael@0 | 228 | // give it an e10s-friendly content script to help with our tests. |
michael@0 | 229 | mm.loadFrameScript(CHROME_BASE + "content_aboutAccounts.js", true); |
michael@0 | 230 | // and wait for it to tell us about the load. |
michael@0 | 231 | return promiseOneMessage(tab, "test:document:load").then( |
michael@0 | 232 | () => tab |
michael@0 | 233 | ); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | // Returns a promise which is resolved with the iframe's URL after a new |
michael@0 | 237 | // tab is created and the iframe in that tab loads. |
michael@0 | 238 | function promiseNewTabWithIframeLoadEvent(aUrl) { |
michael@0 | 239 | let deferred = Promise.defer(); |
michael@0 | 240 | let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl); |
michael@0 | 241 | let browser = tab.linkedBrowser; |
michael@0 | 242 | let mm = browser.messageManager; |
michael@0 | 243 | |
michael@0 | 244 | // give it an e10s-friendly content script to help with our tests. |
michael@0 | 245 | mm.loadFrameScript(CHROME_BASE + "content_aboutAccounts.js", true); |
michael@0 | 246 | // and wait for it to tell us about the iframe load. |
michael@0 | 247 | mm.addMessageListener("test:iframe:load", function onFrameLoad(message) { |
michael@0 | 248 | mm.removeMessageListener("test:iframe:load", onFrameLoad); |
michael@0 | 249 | deferred.resolve([tab, message.data.url]); |
michael@0 | 250 | }); |
michael@0 | 251 | return deferred.promise; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | function checkVisibilities(tab, data) { |
michael@0 | 255 | let ids = Object.keys(data); |
michael@0 | 256 | let mm = tab.linkedBrowser.messageManager; |
michael@0 | 257 | let deferred = Promise.defer(); |
michael@0 | 258 | mm.addMessageListener("test:check-visibilities-response", function onResponse(message) { |
michael@0 | 259 | mm.removeMessageListener("test:check-visibilities-response", onResponse); |
michael@0 | 260 | for (let id of ids) { |
michael@0 | 261 | is(message.data[id], data[id], "Element '" + id + "' has correct visibility"); |
michael@0 | 262 | } |
michael@0 | 263 | deferred.resolve(); |
michael@0 | 264 | }); |
michael@0 | 265 | mm.sendAsyncMessage("test:check-visibilities", {ids: ids}); |
michael@0 | 266 | return deferred.promise; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | // watch out - these will fire observers which if you aren't careful, may |
michael@0 | 270 | // interfere with the tests. |
michael@0 | 271 | function setSignedInUser(data) { |
michael@0 | 272 | if (!data) { |
michael@0 | 273 | data = { |
michael@0 | 274 | email: "foo@example.com", |
michael@0 | 275 | uid: "1234@lcip.org", |
michael@0 | 276 | assertion: "foobar", |
michael@0 | 277 | sessionToken: "dead", |
michael@0 | 278 | kA: "beef", |
michael@0 | 279 | kB: "cafe", |
michael@0 | 280 | verified: true |
michael@0 | 281 | } |
michael@0 | 282 | } |
michael@0 | 283 | return fxAccounts.setSignedInUser(data); |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | function signOut() { |
michael@0 | 287 | return fxAccounts.signOut(); |
michael@0 | 288 | } |