dom/tests/mochitest/whatwg/test_postMessage.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial