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