1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/whatwg/test_postMessage.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,166 @@ 1.4 +<!DOCTYPE html> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage 1.8 +--> 1.9 +<head> 1.10 + <title>Basic postMessage tests</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <script type="text/javascript" src="browserFu.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=postMessage">Mozilla Bug 387706</a> 1.17 +<p id="display"></p> 1.18 +<div id="content" style="display: none"></div> 1.19 + 1.20 +<iframe src="http://mochi.test:8888/tests/dom/tests/mochitest/whatwg/postMessage_helper.html" 1.21 + name="otherSameDomain"></iframe> 1.22 +<iframe src="http://example.org:8000/tests/dom/tests/mochitest/whatwg/postMessage_helper.html" 1.23 + name="otherCrossDomain"></iframe> 1.24 + 1.25 + 1.26 +<pre id="test"> 1.27 +<script class="testbody" type="application/javascript"> 1.28 +/** Test for Bug 387706 **/ 1.29 + 1.30 +SimpleTest.waitForExplicitFinish(); 1.31 + 1.32 +/** Variable for receivers to attempt to get. */ 1.33 +window.privateVariable = 17; 1.34 + 1.35 +/** For sentinel finish, if necessary in deficient browsers. */ 1.36 +var finished = false; 1.37 + 1.38 +/** Ends testing if it isn't already done. */ 1.39 +function finish() 1.40 +{ 1.41 + if (!finished) 1.42 + { 1.43 + finished = true; 1.44 + SimpleTest.finish(); 1.45 + } 1.46 +} 1.47 + 1.48 +/** Receives MessageEvents. */ 1.49 +function messageReceiver(evt) 1.50 +{ 1.51 + try 1.52 + { 1.53 + ok(evt instanceof MessageEvent, "umm, how did we get this?"); 1.54 + is(evt.lastEventId, "", 1.55 + "postMessage creates events with empty lastEventId"); 1.56 + is(evt.type, "message", "expected events of type 'message'"); 1.57 + 1.58 + if (isMozilla) 1.59 + { 1.60 + ok(evt.isTrusted === false, "shouldn't have been a trusted event"); 1.61 + } 1.62 + 1.63 + var data = evt.data; 1.64 + 1.65 + // Check for the message we send to ourselves; it can't be 1.66 + // counted as a test, and it's conceptually distinct from 1.67 + // the other cases, so just return after handling it. 1.68 + if (data === "post-to-self") 1.69 + { 1.70 + respondToSelf(evt); 1.71 + return; 1.72 + } 1.73 + 1.74 + switch (evt.data) 1.75 + { 1.76 + case "post-to-self-response": 1.77 + receiveSelf(evt); 1.78 + break; 1.79 + 1.80 + case "post-to-other-same-domain-response": 1.81 + receiveOtherSameDomain(evt); 1.82 + break; 1.83 + 1.84 + case "post-to-other-cross-domain-response": 1.85 + receiveOtherCrossDomain(evt); 1.86 + 1.87 + // All the tests have executed, so we're done. 1.88 + finish(); 1.89 + break; 1.90 + 1.91 + default: 1.92 + ok(false, "unexpected message: " + evt.data); 1.93 + finish(); 1.94 + break; 1.95 + } 1.96 + } 1.97 + catch (e) 1.98 + { 1.99 + ok(false, "error processing event with data '" + evt.data + "': " + e); 1.100 + finish(); 1.101 + } 1.102 +} 1.103 + 1.104 + 1.105 +/****************** 1.106 + * SELF-RESPONDER * 1.107 + ******************/ 1.108 + 1.109 +function respondToSelf(evt) 1.110 +{ 1.111 + is(evt.origin, "http://mochi.test:8888", "event has wrong origin"); 1.112 + is(evt.source, window, "we posted this message!"); 1.113 + 1.114 + evt.source.postMessage("post-to-self-response", evt.origin); 1.115 +} 1.116 + 1.117 + 1.118 +/************* 1.119 + * RECEIVERS * 1.120 + *************/ 1.121 + 1.122 +function receiveSelf(evt) 1.123 +{ 1.124 + is(evt.origin, "http://mochi.test:8888", "event has wrong origin"); 1.125 + is(evt.source, window, "we posted this message!"); 1.126 + 1.127 + window.frames.otherSameDomain.postMessage("post-to-other-same-domain", 1.128 + "http://mochi.test:8888"); 1.129 +} 1.130 + 1.131 +function receiveOtherSameDomain(evt) 1.132 +{ 1.133 + is(evt.origin, "http://mochi.test:8888", 1.134 + "same-domain response event has wrong origin"); 1.135 + is(evt.source, window.frames.otherSameDomain, 1.136 + "wrong source for same-domain message!"); 1.137 + 1.138 + window.frames.otherCrossDomain.postMessage("post-to-other-cross-domain", 1.139 + "http://example.org:8000"); 1.140 +} 1.141 + 1.142 +function receiveOtherCrossDomain(evt) 1.143 +{ 1.144 + is(evt.origin, "http://example.org:8000", 1.145 + "same-domain response event has wrong origin"); 1.146 + 1.147 + // can't use |is| here, because ok tries to get properties on its arguments 1.148 + // for creating a formatted logging message 1.149 + ok(evt.source === window.frames.otherCrossDomain, 1.150 + "wrong source for cross-domain message!"); 1.151 +} 1.152 + 1.153 + 1.154 +/************** 1.155 + * TEST SETUP * 1.156 + **************/ 1.157 + 1.158 +function start() 1.159 +{ 1.160 + window.postMessage("post-to-self", "http://mochi.test:8888"); 1.161 +} 1.162 + 1.163 +window.addEventListener("load", start, false); 1.164 +window.addEventListener("message", messageReceiver, false); 1.165 + 1.166 +</script> 1.167 +</pre> 1.168 +</body> 1.169 +</html>