1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_aboutAccounts.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,288 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 + 1.8 +XPCOMUtils.defineLazyModuleGetter(this, "Promise", 1.9 + "resource://gre/modules/Promise.jsm"); 1.10 +XPCOMUtils.defineLazyModuleGetter(this, "Task", 1.11 + "resource://gre/modules/Task.jsm"); 1.12 +XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts", 1.13 + "resource://gre/modules/FxAccounts.jsm"); 1.14 + 1.15 +const CHROME_BASE = "chrome://mochitests/content/browser/browser/base/content/test/general/"; 1.16 +// Preference helpers. 1.17 +let changedPrefs = new Set(); 1.18 + 1.19 +function setPref(name, value) { 1.20 + changedPrefs.add(name); 1.21 + Services.prefs.setCharPref(name, value); 1.22 +} 1.23 + 1.24 +registerCleanupFunction(function() { 1.25 + // Ensure we don't pollute prefs for next tests. 1.26 + for (let name of changedPrefs) { 1.27 + Services.prefs.clearUserPref(name); 1.28 + } 1.29 +}); 1.30 + 1.31 +let gTests = [ 1.32 +{ 1.33 + desc: "Test the remote commands", 1.34 + teardown: function* () { 1.35 + gBrowser.removeCurrentTab(); 1.36 + yield fxAccounts.signOut(); 1.37 + }, 1.38 + run: function* () 1.39 + { 1.40 + setPref("identity.fxaccounts.remote.signup.uri", 1.41 + "https://example.com/browser/browser/base/content/test/general/accounts_testRemoteCommands.html"); 1.42 + yield promiseNewTabLoadEvent("about:accounts"); 1.43 + 1.44 + let deferred = Promise.defer(); 1.45 + 1.46 + let results = 0; 1.47 + try { 1.48 + let win = gBrowser.contentWindow; 1.49 + win.addEventListener("message", function testLoad(e) { 1.50 + if (e.data.type == "testResult") { 1.51 + ok(e.data.pass, e.data.info); 1.52 + results++; 1.53 + } 1.54 + else if (e.data.type == "testsComplete") { 1.55 + is(results, e.data.count, "Checking number of results received matches the number of tests that should have run"); 1.56 + win.removeEventListener("message", testLoad, false, true); 1.57 + deferred.resolve(); 1.58 + } 1.59 + 1.60 + }, false, true); 1.61 + 1.62 + } catch(e) { 1.63 + ok(false, "Failed to get all commands"); 1.64 + deferred.reject(); 1.65 + } 1.66 + yield deferred.promise; 1.67 + } 1.68 +}, 1.69 +{ 1.70 + desc: "Test action=signin - no user logged in", 1.71 + teardown: () => gBrowser.removeCurrentTab(), 1.72 + run: function* () 1.73 + { 1.74 + // When this loads with no user logged-in, we expect the "normal" URL 1.75 + const expected_url = "https://example.com/?is_sign_in"; 1.76 + setPref("identity.fxaccounts.remote.signin.uri", expected_url); 1.77 + let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=signin"); 1.78 + is(url, expected_url, "action=signin got the expected URL"); 1.79 + // we expect the remote iframe to be shown. 1.80 + yield checkVisibilities(tab, { 1.81 + stage: false, // parent of 'manage' and 'intro' 1.82 + manage: false, 1.83 + intro: false, // this is "get started" 1.84 + remote: true 1.85 + }); 1.86 + } 1.87 +}, 1.88 +{ 1.89 + desc: "Test action=signin - user logged in", 1.90 + teardown: function* () { 1.91 + gBrowser.removeCurrentTab(); 1.92 + yield signOut(); 1.93 + }, 1.94 + run: function* () 1.95 + { 1.96 + // When this loads with a user logged-in, we expect the normal URL to 1.97 + // have been ignored and the "manage" page to be shown. 1.98 + const expected_url = "https://example.com/?is_sign_in"; 1.99 + setPref("identity.fxaccounts.remote.signin.uri", expected_url); 1.100 + yield setSignedInUser(); 1.101 + let tab = yield promiseNewTabLoadEvent("about:accounts?action=signin"); 1.102 + // about:accounts initializes after fetching the current user from Fxa - 1.103 + // so we also request it - by the time we get it we know it should have 1.104 + // done its thing. 1.105 + yield fxAccounts.getSignedInUser(); 1.106 + // we expect "manage" to be shown. 1.107 + yield checkVisibilities(tab, { 1.108 + stage: true, // parent of 'manage' and 'intro' 1.109 + manage: true, 1.110 + intro: false, // this is "get started" 1.111 + remote: false 1.112 + }); 1.113 + } 1.114 +}, 1.115 +{ 1.116 + desc: "Test action=signup - no user logged in", 1.117 + teardown: () => gBrowser.removeCurrentTab(), 1.118 + run: function* () 1.119 + { 1.120 + const expected_url = "https://example.com/?is_sign_up"; 1.121 + setPref("identity.fxaccounts.remote.signup.uri", expected_url); 1.122 + let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=signup"); 1.123 + is(url, expected_url, "action=signup got the expected URL"); 1.124 + // we expect the remote iframe to be shown. 1.125 + yield checkVisibilities(tab, { 1.126 + stage: false, // parent of 'manage' and 'intro' 1.127 + manage: false, 1.128 + intro: false, // this is "get started" 1.129 + remote: true 1.130 + }); 1.131 + }, 1.132 +}, 1.133 +{ 1.134 + desc: "Test action=signup - user logged in", 1.135 + teardown: () => gBrowser.removeCurrentTab(), 1.136 + run: function* () 1.137 + { 1.138 + const expected_url = "https://example.com/?is_sign_up"; 1.139 + setPref("identity.fxaccounts.remote.signup.uri", expected_url); 1.140 + yield setSignedInUser(); 1.141 + let tab = yield promiseNewTabLoadEvent("about:accounts?action=signup"); 1.142 + yield fxAccounts.getSignedInUser(); 1.143 + // we expect "manage" to be shown. 1.144 + yield checkVisibilities(tab, { 1.145 + stage: true, // parent of 'manage' and 'intro' 1.146 + manage: true, 1.147 + intro: false, // this is "get started" 1.148 + remote: false 1.149 + }); 1.150 + }, 1.151 +}, 1.152 +{ 1.153 + desc: "Test action=reauth", 1.154 + teardown: function* () { 1.155 + gBrowser.removeCurrentTab(); 1.156 + yield signOut(); 1.157 + }, 1.158 + run: function* () 1.159 + { 1.160 + const expected_url = "https://example.com/?is_force_auth"; 1.161 + setPref("identity.fxaccounts.remote.force_auth.uri", expected_url); 1.162 + let userData = { 1.163 + email: "foo@example.com", 1.164 + uid: "1234@lcip.org", 1.165 + assertion: "foobar", 1.166 + sessionToken: "dead", 1.167 + kA: "beef", 1.168 + kB: "cafe", 1.169 + verified: true 1.170 + }; 1.171 + 1.172 + yield setSignedInUser(); 1.173 + let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=reauth"); 1.174 + // The current user will be appended to the url 1.175 + let expected = expected_url + "&email=foo%40example.com"; 1.176 + is(url, expected, "action=reauth got the expected URL"); 1.177 + }, 1.178 +}, 1.179 +{ 1.180 + desc: "Test observers about:accounts", 1.181 + teardown: function() { 1.182 + gBrowser.removeCurrentTab(); 1.183 + }, 1.184 + run: function* () { 1.185 + setPref("identity.fxaccounts.remote.signup.uri", "https://example.com/"); 1.186 + yield setSignedInUser(); 1.187 + let tab = yield promiseNewTabLoadEvent("about:accounts"); 1.188 + // sign the user out - the tab should have action=signin 1.189 + yield signOut(); 1.190 + // wait for the new load. 1.191 + yield promiseOneMessage(tab, "test:document:load"); 1.192 + is(tab.linkedBrowser.contentDocument.location.href, "about:accounts?action=signin"); 1.193 + } 1.194 +}, 1.195 +]; // gTests 1.196 + 1.197 +function test() 1.198 +{ 1.199 + waitForExplicitFinish(); 1.200 + 1.201 + Task.spawn(function () { 1.202 + for (let test of gTests) { 1.203 + info(test.desc); 1.204 + try { 1.205 + yield test.run(); 1.206 + } finally { 1.207 + yield test.teardown(); 1.208 + } 1.209 + } 1.210 + 1.211 + finish(); 1.212 + }); 1.213 +} 1.214 + 1.215 +function promiseOneMessage(tab, messageName) { 1.216 + let mm = tab.linkedBrowser.messageManager; 1.217 + let deferred = Promise.defer(); 1.218 + mm.addMessageListener(messageName, function onmessage(message) { 1.219 + mm.removeMessageListener(messageName, onmessage); 1.220 + deferred.resolve(message); 1.221 + }); 1.222 + return deferred.promise; 1.223 +} 1.224 + 1.225 +function promiseNewTabLoadEvent(aUrl) 1.226 +{ 1.227 + let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl); 1.228 + let browser = tab.linkedBrowser; 1.229 + let mm = browser.messageManager; 1.230 + 1.231 + // give it an e10s-friendly content script to help with our tests. 1.232 + mm.loadFrameScript(CHROME_BASE + "content_aboutAccounts.js", true); 1.233 + // and wait for it to tell us about the load. 1.234 + return promiseOneMessage(tab, "test:document:load").then( 1.235 + () => tab 1.236 + ); 1.237 +} 1.238 + 1.239 +// Returns a promise which is resolved with the iframe's URL after a new 1.240 +// tab is created and the iframe in that tab loads. 1.241 +function promiseNewTabWithIframeLoadEvent(aUrl) { 1.242 + let deferred = Promise.defer(); 1.243 + let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl); 1.244 + let browser = tab.linkedBrowser; 1.245 + let mm = browser.messageManager; 1.246 + 1.247 + // give it an e10s-friendly content script to help with our tests. 1.248 + mm.loadFrameScript(CHROME_BASE + "content_aboutAccounts.js", true); 1.249 + // and wait for it to tell us about the iframe load. 1.250 + mm.addMessageListener("test:iframe:load", function onFrameLoad(message) { 1.251 + mm.removeMessageListener("test:iframe:load", onFrameLoad); 1.252 + deferred.resolve([tab, message.data.url]); 1.253 + }); 1.254 + return deferred.promise; 1.255 +} 1.256 + 1.257 +function checkVisibilities(tab, data) { 1.258 + let ids = Object.keys(data); 1.259 + let mm = tab.linkedBrowser.messageManager; 1.260 + let deferred = Promise.defer(); 1.261 + mm.addMessageListener("test:check-visibilities-response", function onResponse(message) { 1.262 + mm.removeMessageListener("test:check-visibilities-response", onResponse); 1.263 + for (let id of ids) { 1.264 + is(message.data[id], data[id], "Element '" + id + "' has correct visibility"); 1.265 + } 1.266 + deferred.resolve(); 1.267 + }); 1.268 + mm.sendAsyncMessage("test:check-visibilities", {ids: ids}); 1.269 + return deferred.promise; 1.270 +} 1.271 + 1.272 +// watch out - these will fire observers which if you aren't careful, may 1.273 +// interfere with the tests. 1.274 +function setSignedInUser(data) { 1.275 + if (!data) { 1.276 + data = { 1.277 + email: "foo@example.com", 1.278 + uid: "1234@lcip.org", 1.279 + assertion: "foobar", 1.280 + sessionToken: "dead", 1.281 + kA: "beef", 1.282 + kB: "cafe", 1.283 + verified: true 1.284 + } 1.285 + } 1.286 + return fxAccounts.setSignedInUser(data); 1.287 +} 1.288 + 1.289 +function signOut() { 1.290 + return fxAccounts.signOut(); 1.291 +}