dom/base/test/test_messageChannel_pingpong.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/test/test_messageChannel_pingpong.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=677638
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Test for Bug 677638 - port cloning</title>
    1.12 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.14 +</head>
    1.15 +<body>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a>
    1.17 +<div id="content"></div>
    1.18 +<pre id="test">
    1.19 +</pre>
    1.20 +  <script type="application/javascript">
    1.21 +
    1.22 +  function runTest() {
    1.23 +    var MAX = 100;
    1.24 +
    1.25 +    var a = new MessageChannel();
    1.26 +    ok(a, "MessageChannel created");
    1.27 +
    1.28 +    // Populate the message queue of this port.
    1.29 +    for (var i = 0; i < MAX; ++i) {
    1.30 +      a.port1.postMessage(i);
    1.31 +    }
    1.32 +
    1.33 +    window.addEventListener('message', receiveMessage, false);
    1.34 +    function receiveMessage(evt) {
    1.35 +
    1.36 +      // This test sends the port from this window to the iframe and viceversa.
    1.37 +      if (evt.data.type == 'PORT') {
    1.38 +        var port = evt.data.port;
    1.39 +        var counter = 0;
    1.40 +        port.onmessage = function(evt) {
    1.41 +          // only 1 message should be received by this port.
    1.42 +          if (counter++ == 0) {
    1.43 +            ok(evt.data % 2, "The number " + evt.data + " has been received correctly by the main window");
    1.44 +
    1.45 +            if (evt.data < MAX - 1) {
    1.46 +              ifr.contentWindow.postMessage({ type: 'PORT', port: port }, '*', [port]);
    1.47 +            } else {
    1.48 +              SimpleTest.finish();
    1.49 +            }
    1.50 +          } else {
    1.51 +            ok(false, "Wrong message!");
    1.52 +          }
    1.53 +        }
    1.54 +      } else if (evt.data.type == 'OK') {
    1.55 +        ok(true, evt.data.msg);
    1.56 +      } else if (evt.data.type == 'KO') {
    1.57 +        ok(false, evt.data.msg);
    1.58 +      } else {
    1.59 +        ok(false, "Unknown message");
    1.60 +      }
    1.61 +    }
    1.62 +
    1.63 +    var div = document.getElementById("content");
    1.64 +    ok(div, "Parent exists");
    1.65 +
    1.66 +    var ifr = document.createElement("iframe");
    1.67 +    ifr.addEventListener("load", iframeLoaded, false);
    1.68 +    ifr.setAttribute('src', "iframe_messageChannel_pingpong.html");
    1.69 +    div.appendChild(ifr);
    1.70 +
    1.71 +    function iframeLoaded() {
    1.72 +      ifr.contentWindow.postMessage({ type: 'PORT', port: a.port2 }, '*', [a.port2]);
    1.73 +    }
    1.74 +  }
    1.75 +
    1.76 +  SimpleTest.waitForExplicitFinish();
    1.77 +  SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTest);
    1.78 +  </script>
    1.79 +</body>
    1.80 +</html>

mercurial