test_beaconFrame.html (3537B)
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 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(id, callback) { 26 var request = new XMLHttpRequest(); 27 let getBeaconUri = "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs?getBeacon=" + id; 28 request.open("GET", getBeaconUri, true); 29 request.onload = function() { 30 if (request.readyState === request.DONE) { 31 callback(request.responseText); 32 } 33 }; 34 request.send(null); 35 } 36 37 function createIframeWithData(data, mimetype, convert) { 38 beaconConvert = convert; 39 40 var frame = document.createElement("IFRAME"); 41 frame.setAttribute("src", "beacon-frame.html"); 42 frame.id = "frame"; 43 frame.setAttribute("data", data.toString()); 44 frame.setAttribute("mimetype", mimetype); 45 var c = document.getElementById("content"); 46 c.appendChild(frame); 47 } 48 49 function beaconSent(result) { 50 // This function gets called from beacon-frame.html in the inner frame 51 // Check that the beacon was actually sent 52 ok(result, "Beacon should be sent"); 53 54 // remove the frame. 55 var frame = document.getElementById("frame"); 56 var id = frame.getAttribute("data"); 57 var data = frame.getAttribute("data"); 58 var mimetype = frame.getAttribute("mimetype"); 59 60 var c = document.getElementById("content"); 61 c.removeChild(frame); 62 63 getBeaconServerStatus(id, function(response) { 64 console.log(response); 65 var result = JSON.parse(response); 66 is(result.data, data, "Beacon status should match expected. is: " + result.data + " should be: " + data); 67 is(result.mimetype, mimetype, "Beacon mimetype should match expected. is: " + result.mimetype + " should be: " + mimetype); 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 // "data" is used as an id, so should be unique for each call to createIframeWithData 108 function() { createIframeWithData("hi!", "text/plain;charset=UTF-8", identity); }, 109 function() { createIframeWithData("123", "", stringToArrayBuffer); }, 110 function() { createIframeWithData("abc", "text/html", stringToBlob); }, 111 function() { createIframeWithData("qwerty", "multipart/form-data", stringToFormData); }, 112 function() { SimpleTest.finish(); }, 113 ]; 114 115 </script> 116 </pre> 117 </body> 118 </html>