dom/tests/mochitest/beacon/test_beaconFrame.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/beacon/test_beaconFrame.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=936340
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for beacon</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936340">Mozilla Bug 936340</a>
    1.16 +<p id="display"></p>
    1.17 +
    1.18 +<div id="content">
    1.19 +</div>
    1.20 +
    1.21 +<pre id="test">
    1.22 +<script class="testbody" type="text/javascript">
    1.23 +
    1.24 +// not enabled by default yet.
    1.25 +SimpleTest.waitForExplicitFinish();
    1.26 +SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, runNextTest);
    1.27 +
    1.28 +function getBeaconServerStatus(callback) {
    1.29 +    var request = new XMLHttpRequest();
    1.30 +    request.open("GET", "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs?getLastBeacon", true);
    1.31 +    request.onload = function() {
    1.32 +        if (request.readyState === request.DONE) {
    1.33 +            callback(request.responseText);
    1.34 +        }
    1.35 +    };
    1.36 +    request.send(null);
    1.37 +}
    1.38 +
    1.39 +function createIframeWithData(data, mimetype, convert) {
    1.40 +    beaconConvert = convert;
    1.41 +
    1.42 +    var frame = document.createElement("IFRAME");
    1.43 +    frame.setAttribute("src", "beacon-frame.html");
    1.44 +    frame.id = "frame";
    1.45 +    frame.setAttribute("data", data.toString());
    1.46 +    frame.setAttribute("mimetype", mimetype);
    1.47 +    var c = document.getElementById("content");
    1.48 +    c.appendChild(frame);
    1.49 +}
    1.50 +
    1.51 +function beaconSent(result) {
    1.52 +    // This function gets called from beacon-frame.html in the inner frame
    1.53 +    // Check that the beacon was actually sent
    1.54 +    ok(result, "Beacon was not sent")
    1.55 +    
    1.56 +    // remove the frame.
    1.57 +    var frame = document.getElementById("frame");
    1.58 +    var data = frame.getAttribute("data");
    1.59 +    var mimetype = frame.getAttribute("mimetype");
    1.60 +
    1.61 +    var c = document.getElementById("content");
    1.62 +    c.removeChild(frame);
    1.63 +
    1.64 +    getBeaconServerStatus( function(response) {
    1.65 +      console.log(response);
    1.66 +        var result = JSON.parse(response);
    1.67 +        
    1.68 +        is(result.data, data, "Beacon status should match expected.  is: " + result.data + " should be: " + data);
    1.69 +        is(result.mimetype, mimetype, "Beacon mimetype should match expected.  is: " + result.mimetype + " should be: " + mimetype);
    1.70 +
    1.71 +        runNextTest();
    1.72 +    });
    1.73 +}
    1.74 +
    1.75 +function runNextTest() {
    1.76 +    var test = tests.shift();
    1.77 +    setTimeout(test, 0);
    1.78 +}
    1.79 +
    1.80 +var beaconConvert = function() {};
    1.81 +
    1.82 +function stringToArrayBuffer(input) {
    1.83 +
    1.84 +    var buffer = new ArrayBuffer(input.length * 2);
    1.85 +    var array = new Uint16Array(buffer);
    1.86 +
    1.87 +    // dumbly copy over the bytes
    1.88 +    for (var i = 0, len = input.length; i < len; i++) {
    1.89 +        array[i] = input.charCodeAt(i);
    1.90 +    }
    1.91 +    return array;
    1.92 +}
    1.93 +
    1.94 +function stringToBlob(input) {
    1.95 +    var blob = new Blob([input], {type : 'text/html'});
    1.96 +    return blob;
    1.97 +}
    1.98 +
    1.99 +function stringToFormData(input) {
   1.100 +    var formdata = new FormData();
   1.101 +    formdata.append(input, new Blob(['hi']));
   1.102 +    return formdata;
   1.103 +}
   1.104 +
   1.105 +function identity(data) {
   1.106 +    return data;
   1.107 +}
   1.108 +
   1.109 +var tests = [
   1.110 +    function() { createIframeWithData("hi!", "text/plain;charset=UTF-8", identity); },
   1.111 +    function() { createIframeWithData("123", "application/octet-stream", stringToArrayBuffer); },
   1.112 +    function() { createIframeWithData("abc", "text/html", stringToBlob); },
   1.113 +    function() { createIframeWithData("qwerty", "multipart/form-data", stringToFormData); },
   1.114 +    function() { SimpleTest.finish(); },
   1.115 +];
   1.116 +
   1.117 +</script>
   1.118 +</pre>
   1.119 +</body>
   1.120 +</html>
   1.121 +

mercurial