dom/tests/mochitest/whatwg/test_postMessage.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial