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.
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 Cu.import("resource://gre/modules/Promise.jsm");
7 Cu.import("resource://gre/modules/DOMIdentity.jsm");
9 XPCOMUtils.defineLazyModuleGetter(this, "FirefoxAccounts",
10 "resource://gre/modules/identity/FirefoxAccounts.jsm");
12 // Make the profile dir available; this is necessary so that
13 // services/fxaccounts/FxAccounts.jsm can read and write its signed-in user
14 // data.
15 do_get_profile();
17 function MockFXAManager() {
18 this.signedInUser = true;
19 }
20 MockFXAManager.prototype = {
21 getAssertion: function(audience) {
22 let result = this.signedInUser ? TEST_ASSERTION : null;
23 return Promise.resolve(result);
24 },
26 signOut: function() {
27 this.signedInUser = false;
28 return Promise.resolve(null);
29 },
31 signIn: function(user) {
32 this.signedInUser = user;
33 return Promise.resolve(user);
34 },
35 }
37 let originalManager = FirefoxAccounts.fxAccountsManager;
38 FirefoxAccounts.fxAccountsManager = new MockFXAManager();
39 do_register_cleanup(() => {
40 log("restoring fxaccountsmanager");
41 FirefoxAccounts.fxAccountsManager = originalManager;
42 });
44 function withNobodySignedIn() {
45 return FirefoxAccounts.fxAccountsManager.signOut();
46 }
48 function withSomebodySignedIn() {
49 return FirefoxAccounts.fxAccountsManager.signIn('Pertelote');
50 }
52 function test_overall() {
53 do_check_neq(FirefoxAccounts, null);
54 run_next_test();
55 }
57 function test_mock() {
58 do_test_pending();
60 withSomebodySignedIn().then(() => {
61 FirefoxAccounts.fxAccountsManager.getAssertion().then(assertion => {
62 do_check_eq(assertion, TEST_ASSERTION);
63 do_test_finished();
64 run_next_test();
65 });
66 });
67 }
69 function test_watch_signed_in() {
70 do_test_pending();
72 let received = [];
74 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) {
75 received.push([method, data]);
77 if (method == "ready") {
78 // confirm that we were signed in and then ready was called
79 do_check_eq(received.length, 2);
80 do_check_eq(received[0][0], "login");
81 do_check_eq(received[0][1], TEST_ASSERTION);
82 do_check_eq(received[1][0], "ready");
83 do_test_finished();
84 run_next_test();
85 }
86 });
88 withSomebodySignedIn().then(() => {
89 FirefoxAccounts.RP.watch(mockedRP);
90 });
91 }
93 function test_watch_signed_out() {
94 do_test_pending();
96 let received = [];
98 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) {
99 received.push(method);
101 if (method == "ready") {
102 // confirm that we were signed out and then ready was called
103 do_check_eq(received.length, 2);
104 do_check_eq(received[0], "logout");
105 do_check_eq(received[1], "ready");
107 do_test_finished();
108 run_next_test();
109 }
110 });
112 withNobodySignedIn().then(() => {
113 FirefoxAccounts.RP.watch(mockedRP);
114 });
115 }
117 function test_request() {
118 do_test_pending();
120 let received = [];
122 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) {
123 received.push([method, data]);
125 // On watch(), we are signed out. Then we call request().
126 if (received.length === 2) {
127 do_check_eq(received[0][0], "logout");
128 do_check_eq(received[1][0], "ready");
130 // Pretend request() showed ux and the user signed in
131 withSomebodySignedIn().then(() => {
132 FirefoxAccounts.RP.request(mockedRP.id);
133 });
134 }
136 if (received.length === 3) {
137 do_check_eq(received[2][0], "login");
138 do_check_eq(received[2][1], TEST_ASSERTION);
140 do_test_finished();
141 run_next_test();
142 }
143 });
145 // First, call watch() with nobody signed in
146 withNobodySignedIn().then(() => {
147 FirefoxAccounts.RP.watch(mockedRP);
148 });
149 }
151 function test_logout() {
152 do_test_pending();
154 let received = [];
156 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) {
157 received.push(method);
159 // At first, watch() signs us in automatically. Then we sign out.
160 if (received.length === 2) {
161 do_check_eq(received[0], "login");
162 do_check_eq(received[1], "ready");
164 FirefoxAccounts.RP.logout(mockedRP.id);
165 }
167 if (received.length === 3) {
168 do_check_eq(received[2], "logout");
169 do_test_finished();
170 run_next_test();
171 }
172 });
174 // First, call watch()
175 withSomebodySignedIn().then(() => {
176 FirefoxAccounts.RP.watch(mockedRP);
177 });
178 }
180 function test_error() {
181 do_test_pending();
183 let received = [];
185 // Mock the fxAccountsManager so that getAssertion rejects its promise and
186 // triggers our onerror handler. (This is the method that's used internally
187 // by FirefoxAccounts.RP.request().)
188 let originalGetAssertion = FirefoxAccounts.fxAccountsManager.getAssertion;
189 FirefoxAccounts.fxAccountsManager.getAssertion = function(audience) {
190 return Promise.reject(new Error("barf!"));
191 };
193 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, message) {
194 // We will immediately receive an error, due to watch()'s attempt
195 // to getAssertion().
196 do_check_eq(method, "error");
197 do_check_true(/barf/.test(message));
199 // Put things back the way they were
200 FirefoxAccounts.fxAccountsManager.getAssertion = originalGetAssertion;
202 do_test_finished();
203 run_next_test();
204 });
206 // First, call watch()
207 withSomebodySignedIn().then(() => {
208 FirefoxAccounts.RP.watch(mockedRP);
209 });
210 }
212 function test_child_process_shutdown() {
213 do_test_pending();
214 let rpCount = FirefoxAccounts.RP._rpFlows.size;
216 makeObserver("identity-child-process-shutdown", (aTopic, aSubject, aData) => {
217 // Last of all, the shutdown observer message will be fired.
218 // This takes place after the RP has a chance to delete flows
219 // and clean up.
220 do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount);
221 do_test_finished();
222 run_next_test();
223 });
225 let mockedRP = mock_fxa_rp(null, TEST_URL, (method) => {
226 // We should enter this function for 'ready' and 'child-process-shutdown'.
227 // After we have a chance to do our thing, the shutdown observer message
228 // will fire and be caught by the function above.
229 do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount + 1);
230 switch (method) {
231 case "ready":
232 DOMIdentity._childProcessShutdown("my message manager");
233 break;
235 case "child-process-shutdown":
236 // We have to call this explicitly because there's no real
237 // dom window here.
238 FirefoxAccounts.RP.childProcessShutdown(mockedRP._mm);
239 break;
241 default:
242 break;
243 }
244 });
246 mockedRP._mm = "my message manager";
247 withSomebodySignedIn().then(() => {
248 FirefoxAccounts.RP.watch(mockedRP);
249 });
251 // fake a dom window context
252 DOMIdentity.newContext(mockedRP, mockedRP._mm);
253 }
255 let TESTS = [
256 test_overall,
257 test_mock,
258 test_watch_signed_in,
259 test_watch_signed_out,
260 test_request,
261 test_logout,
262 test_error,
263 test_child_process_shutdown,
264 ];
266 TESTS.forEach(add_test);
268 function run_test() {
269 run_next_test();
270 }