test_postMessage_chrome.html (2566B)
1 <!DOCTYPE html> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage 5 --> 6 <head> 7 <title>postMessage chrome tests</title> 8 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="chrome://mochikit/content/chrome-harness.js"></script> 10 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" /> 11 </head> 12 <body> 13 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage">Mozilla Bug 387706</a> 15 <p id="display"></p> 16 <div id="content" style="display: none"></div> 17 18 <iframe src="http://example.org/tests/dom/tests/mochitest/whatwg/postMessage_chrome_helper.html" 19 name="contentDomain"></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 var finished = false; 29 function finish() 30 { 31 if (!finished) 32 { 33 finished = true; 34 SimpleTest.finish(); 35 } 36 } 37 38 /** Receives MessageEvents to this window. */ 39 function messageReceiver(evt) 40 { 41 ok(evt instanceof MessageEvent, "umm, how did we get this?"); 42 is(evt.type, "message", "expected events of type 'message'"); 43 is(evt.lastEventId, "", "postMessage creates events with empty lastEventId"); 44 45 switch (evt.data) 46 { 47 case "path-is-set": 48 chromePathIsSet(evt); 49 break; 50 51 case "post-to-self": 52 checkSelf(evt); 53 break; 54 55 case "post-to-content-response": 56 receiveContent(evt); 57 break; 58 59 default: 60 ok(false, "unexpected message: " + evt.data); 61 finish(); 62 break; 63 } 64 } 65 66 67 /****************** 68 * SELF-RESPONDER * 69 ******************/ 70 71 function checkSelf(evt) 72 { 73 var prepath = getChromePrePath(window.location.href); 74 75 is(evt.isTrusted, true, "should have sent a trusted event"); 76 is(evt.origin, prepath, "wrong origin for chrome: URL"); 77 is(evt.source, null, "chrome posters get a null source, for security"); 78 79 window.frames.contentDomain.postMessage(prepath, "*"); 80 } 81 82 83 function chromePathIsSet(evt) 84 { 85 window.frames.contentDomain.postMessage("post-to-content", 86 "http://example.org"); 87 } 88 89 /************* 90 * RECEIVERS * 91 *************/ 92 93 function receiveContent(evt) 94 { 95 is(evt.isTrusted, true, "should have sent a trusted event"); 96 finish(); 97 } 98 99 100 /************** 101 * TEST SETUP * 102 **************/ 103 104 function run() 105 { 106 window.addEventListener("message", messageReceiver); 107 window.postMessage("post-to-self", "*"); 108 } 109 110 window.addEventListener("load", run); 111 </script> 112 </pre> 113 </body> 114 </html>