|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=936340 |
|
5 --> |
|
6 <head> |
|
7 <title>Test for beacon</title> |
|
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
10 </head> |
|
11 <body> |
|
12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936340">Mozilla Bug 936340</a> |
|
13 <p id="display"></p> |
|
14 |
|
15 <div id="content"> |
|
16 </div> |
|
17 |
|
18 <pre id="test"> |
|
19 <script class="testbody" type="text/javascript"> |
|
20 |
|
21 // not enabled by default yet. |
|
22 SimpleTest.waitForExplicitFinish(); |
|
23 SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, runNextTest); |
|
24 |
|
25 function getBeaconServerStatus(callback) { |
|
26 var request = new XMLHttpRequest(); |
|
27 request.open("GET", "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs?getLastBeacon", true); |
|
28 request.onload = function() { |
|
29 if (request.readyState === request.DONE) { |
|
30 callback(request.responseText); |
|
31 } |
|
32 }; |
|
33 request.send(null); |
|
34 } |
|
35 |
|
36 function createIframeWithData(data, mimetype, convert) { |
|
37 beaconConvert = convert; |
|
38 |
|
39 var frame = document.createElement("IFRAME"); |
|
40 frame.setAttribute("src", "beacon-frame.html"); |
|
41 frame.id = "frame"; |
|
42 frame.setAttribute("data", data.toString()); |
|
43 frame.setAttribute("mimetype", mimetype); |
|
44 var c = document.getElementById("content"); |
|
45 c.appendChild(frame); |
|
46 } |
|
47 |
|
48 function beaconSent(result) { |
|
49 // This function gets called from beacon-frame.html in the inner frame |
|
50 // Check that the beacon was actually sent |
|
51 ok(result, "Beacon was not sent") |
|
52 |
|
53 // remove the frame. |
|
54 var frame = document.getElementById("frame"); |
|
55 var data = frame.getAttribute("data"); |
|
56 var mimetype = frame.getAttribute("mimetype"); |
|
57 |
|
58 var c = document.getElementById("content"); |
|
59 c.removeChild(frame); |
|
60 |
|
61 getBeaconServerStatus( function(response) { |
|
62 console.log(response); |
|
63 var result = JSON.parse(response); |
|
64 |
|
65 is(result.data, data, "Beacon status should match expected. is: " + result.data + " should be: " + data); |
|
66 is(result.mimetype, mimetype, "Beacon mimetype should match expected. is: " + result.mimetype + " should be: " + mimetype); |
|
67 |
|
68 runNextTest(); |
|
69 }); |
|
70 } |
|
71 |
|
72 function runNextTest() { |
|
73 var test = tests.shift(); |
|
74 setTimeout(test, 0); |
|
75 } |
|
76 |
|
77 var beaconConvert = function() {}; |
|
78 |
|
79 function stringToArrayBuffer(input) { |
|
80 |
|
81 var buffer = new ArrayBuffer(input.length * 2); |
|
82 var array = new Uint16Array(buffer); |
|
83 |
|
84 // dumbly copy over the bytes |
|
85 for (var i = 0, len = input.length; i < len; i++) { |
|
86 array[i] = input.charCodeAt(i); |
|
87 } |
|
88 return array; |
|
89 } |
|
90 |
|
91 function stringToBlob(input) { |
|
92 var blob = new Blob([input], {type : 'text/html'}); |
|
93 return blob; |
|
94 } |
|
95 |
|
96 function stringToFormData(input) { |
|
97 var formdata = new FormData(); |
|
98 formdata.append(input, new Blob(['hi'])); |
|
99 return formdata; |
|
100 } |
|
101 |
|
102 function identity(data) { |
|
103 return data; |
|
104 } |
|
105 |
|
106 var tests = [ |
|
107 function() { createIframeWithData("hi!", "text/plain;charset=UTF-8", identity); }, |
|
108 function() { createIframeWithData("123", "application/octet-stream", stringToArrayBuffer); }, |
|
109 function() { createIframeWithData("abc", "text/html", stringToBlob); }, |
|
110 function() { createIframeWithData("qwerty", "multipart/form-data", stringToFormData); }, |
|
111 function() { SimpleTest.finish(); }, |
|
112 ]; |
|
113 |
|
114 </script> |
|
115 </pre> |
|
116 </body> |
|
117 </html> |
|
118 |