tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_postMessage.html (4118B)


      1 <!DOCTYPE html>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage
      5 -->
      6 <head>
      7  <title>Basic postMessage tests</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=postMessage">Mozilla Bug 387706</a>
     13 <p id="display"></p>
     14 <div id="content" style="display: none"></div>
     15 
     16 <iframe src="http://mochi.test:8888/tests/dom/tests/mochitest/whatwg/postMessage_helper.html" 
     17        name="otherSameDomain"></iframe>
     18 <iframe src="http://example.org:8000/tests/dom/tests/mochitest/whatwg/postMessage_helper.html"
     19        name="otherCrossDomain"></iframe>
     20 
     21 
     22 <pre id="test">
     23 <script class="testbody" type="application/javascript">
     24 /** Test for Bug 387706 */
     25 
     26 SimpleTest.waitForExplicitFinish();
     27 
     28 /** Variable for receivers to attempt to get. */
     29 window.privateVariable = 17;
     30 
     31 /** For sentinel finish, if necessary in deficient browsers. */
     32 var finished = false;
     33 
     34 /** Ends testing if it isn't already done. */
     35 function finish()
     36 {
     37  if (!finished)
     38  {
     39    finished = true;
     40    SimpleTest.finish();
     41  }
     42 }
     43 
     44 /** Receives MessageEvents. */
     45 function messageReceiver(evt)
     46 {
     47  try
     48  {
     49    ok(evt instanceof MessageEvent, "umm, how did we get this?");
     50    is(evt.lastEventId, "",
     51       "postMessage creates events with empty lastEventId");
     52    is(evt.type, "message", "expected events of type 'message'");
     53    ok(evt.isTrusted === true, "should have been a trusted event");
     54  
     55    var data = evt.data;
     56  
     57    // Check for the message we send to ourselves; it can't be
     58    // counted as a test, and it's conceptually distinct from
     59    // the other cases, so just return after handling it.
     60    if (data === "post-to-self")
     61    {
     62      respondToSelf(evt);
     63      return;
     64    }
     65 
     66    switch (evt.data)
     67    {
     68      case "post-to-self-response":
     69        receiveSelf(evt);
     70        break;
     71  
     72      case "post-to-other-same-domain-response":
     73        receiveOtherSameDomain(evt);
     74        break;
     75  
     76      case "post-to-other-cross-domain-response":
     77        receiveOtherCrossDomain(evt);
     78        
     79        // All the tests have executed, so we're done.
     80        finish();
     81        break;
     82  
     83      default:
     84        ok(false, "unexpected message: " + evt.data);
     85        finish();
     86        break;
     87    }
     88  }
     89  catch (e)
     90  {
     91    ok(false, "error processing event with data '" + evt.data + "': " + e);
     92    finish();
     93  }
     94 }
     95 
     96 
     97 /******************
     98 * SELF-RESPONDER *
     99 ******************/
    100 
    101 function respondToSelf(evt)
    102 {
    103  is(evt.origin, "http://mochi.test:8888", "event has wrong origin");
    104  is(evt.source, window, "we posted this message!");
    105  
    106  evt.source.postMessage("post-to-self-response", evt.origin);
    107 }
    108 
    109 
    110 /*************
    111 * RECEIVERS *
    112 *************/
    113 
    114 function receiveSelf(evt)
    115 {
    116  is(evt.origin, "http://mochi.test:8888", "event has wrong origin");
    117  is(evt.source, window, "we posted this message!");
    118 
    119  window.frames.otherSameDomain.postMessage("post-to-other-same-domain",
    120                                            "http://mochi.test:8888");
    121 }
    122 
    123 function receiveOtherSameDomain(evt)
    124 {
    125  is(evt.origin, "http://mochi.test:8888",
    126     "same-domain response event has wrong origin");
    127  is(evt.source, window.frames.otherSameDomain,
    128     "wrong source for same-domain message!");
    129 
    130  window.frames.otherCrossDomain.postMessage("post-to-other-cross-domain",
    131                                            "http://example.org:8000");
    132 }
    133 
    134 function receiveOtherCrossDomain(evt)
    135 {
    136  is(evt.origin, "http://example.org:8000",
    137     "same-domain response event has wrong origin");
    138 
    139  // can't use |is| here, because ok tries to get properties on its arguments
    140  // for creating a formatted logging message
    141  ok(evt.source === window.frames.otherCrossDomain,
    142     "wrong source for cross-domain message!");
    143 }
    144 
    145 
    146 /**************
    147 * TEST SETUP *
    148 **************/
    149 
    150 function start()
    151 {
    152  window.postMessage("post-to-self", "http://mochi.test:8888");
    153 }
    154 
    155 window.addEventListener("load", start);
    156 window.addEventListener("message", messageReceiver);
    157 
    158 </script>
    159 </pre>
    160 </body>
    161 </html>