|
1 <html> |
|
2 <head> |
|
3 <meta charset="utf-8"> |
|
4 |
|
5 <script type="text/javascript;version=1.8"> |
|
6 |
|
7 function init() { |
|
8 window.addEventListener("message", function process(e) {doTest(e)}, false); |
|
9 // unless we relinquish the eventloop, |
|
10 // tests will run before the chrome event handlers are ready |
|
11 setTimeout(doTest, 0); |
|
12 } |
|
13 |
|
14 function checkStatusValue(payload, expectedValue) { |
|
15 return payload.status == expectedValue; |
|
16 } |
|
17 |
|
18 let tests = [ |
|
19 { |
|
20 info: "Check account log in", |
|
21 event: "login", |
|
22 data: { |
|
23 email: "foo@example.com", |
|
24 uid: "1234@lcip.org", |
|
25 assertion: "foobar", |
|
26 sessionToken: "dead", |
|
27 kA: "beef", |
|
28 kB: "cafe", |
|
29 verified: true |
|
30 }, |
|
31 payloadType: "message", |
|
32 validateResponse: function(payload) { |
|
33 return checkStatusValue(payload, "login"); |
|
34 }, |
|
35 }, |
|
36 ]; |
|
37 |
|
38 let currentTest = -1; |
|
39 function doTest(evt) { |
|
40 if (evt) { |
|
41 if (currentTest < 0 || !evt.data.content) |
|
42 return; // not yet testing |
|
43 |
|
44 let test = tests[currentTest]; |
|
45 if (evt.data.type != test.payloadType) |
|
46 return; // skip unrequested events |
|
47 |
|
48 let error = JSON.stringify(evt.data.content); |
|
49 let pass = false; |
|
50 try { |
|
51 pass = test.validateResponse(evt.data.content) |
|
52 } catch (e) {} |
|
53 reportResult(test.info, pass, error); |
|
54 } |
|
55 // start the next test if there are any left |
|
56 if (tests[++currentTest]) |
|
57 sendToBrowser(tests[currentTest].event, tests[currentTest].data); |
|
58 else |
|
59 reportFinished(); |
|
60 } |
|
61 |
|
62 function reportResult(info, pass, error) { |
|
63 let data = {type: "testResult", info: info, pass: pass, error: error}; |
|
64 window.parent.postMessage(data, "*"); |
|
65 } |
|
66 |
|
67 function reportFinished(cmd) { |
|
68 let data = {type: "testsComplete", count: tests.length}; |
|
69 window.parent.postMessage(data, "*"); |
|
70 } |
|
71 |
|
72 function sendToBrowser(type, data) { |
|
73 let event = new CustomEvent("FirefoxAccountsCommand", {detail: {command: type, data: data}, bubbles: true}); |
|
74 document.dispatchEvent(event); |
|
75 } |
|
76 |
|
77 </script> |
|
78 </head> |
|
79 <body onload="init()"> |
|
80 </body> |
|
81 </html> |