2008-01-29 17:31:29 -08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>postMessage chrome tests</title>
|
|
|
|
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
2010-08-24 10:29:34 -07:00
|
|
|
<script type="application/javascript" src="chrome://mochikit/content/chrome-harness.js"></script>
|
2008-01-29 17:31:29 -08:00
|
|
|
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage">Mozilla Bug 387706</a>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none"></div>
|
|
|
|
|
|
|
|
<iframe src="http://example.org/tests/dom/tests/mochitest/whatwg/postMessage_chrome_helper.html"
|
|
|
|
name="contentDomain"></iframe>
|
|
|
|
|
|
|
|
|
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="application/javascript">
|
|
|
|
/** Test for Bug 387706 **/
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
2008-05-02 12:26:47 -07:00
|
|
|
var finished = false;
|
|
|
|
function finish()
|
|
|
|
{
|
|
|
|
if (!finished)
|
|
|
|
{
|
|
|
|
finished = true;
|
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
|
|
|
}
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
/** Receives MessageEvents to this window. */
|
|
|
|
function messageReceiver(evt)
|
|
|
|
{
|
|
|
|
ok(evt instanceof MessageEvent, "umm, how did we get this?");
|
|
|
|
is(evt.type, "message", "expected events of type 'message'");
|
2008-05-02 12:26:47 -07:00
|
|
|
is(evt.lastEventId, "", "postMessage creates events with empty lastEventId");
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
switch (evt.data)
|
|
|
|
{
|
2010-09-30 17:10:19 -07:00
|
|
|
case "path-is-set":
|
|
|
|
chromePathIsSet(evt);
|
|
|
|
break;
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
case "post-to-self":
|
|
|
|
checkSelf(evt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "post-to-content-response":
|
|
|
|
receiveContent(evt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ok(false, "unexpected message: " + evt.data);
|
2008-05-02 12:26:47 -07:00
|
|
|
finish();
|
2008-01-29 17:31:29 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************
|
|
|
|
* SELF-RESPONDER *
|
|
|
|
******************/
|
|
|
|
|
|
|
|
function checkSelf(evt)
|
|
|
|
{
|
2010-08-24 10:29:34 -07:00
|
|
|
var prepath = getChromePrePath(window.location.href);
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
is(evt.isTrusted, true, "should have sent a trusted event");
|
2010-08-24 10:29:34 -07:00
|
|
|
is(evt.origin, prepath, "wrong origin for chrome: URL");
|
2008-01-29 17:31:29 -08:00
|
|
|
is(evt.source, null, "chrome posters get a null source, for security");
|
2008-05-02 12:26:47 -07:00
|
|
|
|
2010-09-30 17:10:19 -07:00
|
|
|
window.frames.contentDomain.postMessage(prepath, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function chromePathIsSet(evt)
|
|
|
|
{
|
2008-05-02 12:26:47 -07:00
|
|
|
window.frames.contentDomain.postMessage("post-to-content",
|
|
|
|
"http://example.org");
|
2008-01-29 17:31:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************
|
|
|
|
* RECEIVERS *
|
|
|
|
*************/
|
|
|
|
|
|
|
|
function receiveContent(evt)
|
|
|
|
{
|
|
|
|
is(evt.isTrusted, true, "should have sent a trusted event");
|
2008-02-26 19:48:54 -08:00
|
|
|
is(evt.origin, "http://example.org", "content response event has wrong URI");
|
2008-01-29 17:31:29 -08:00
|
|
|
is(evt.source, window.frames.contentDomain,
|
|
|
|
"wrong source for same-domain message!");
|
2008-05-02 12:26:47 -07:00
|
|
|
|
|
|
|
finish();
|
2008-01-29 17:31:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**************
|
|
|
|
* TEST SETUP *
|
|
|
|
**************/
|
|
|
|
|
2008-05-02 12:26:47 -07:00
|
|
|
function run()
|
2008-01-29 17:31:29 -08:00
|
|
|
{
|
2008-05-02 12:26:47 -07:00
|
|
|
window.addEventListener("message", messageReceiver, false);
|
|
|
|
window.postMessage("post-to-self", "*");
|
2008-01-29 17:31:29 -08:00
|
|
|
}
|
|
|
|
|
2008-05-02 12:26:47 -07:00
|
|
|
window.addEventListener("load", run, false);
|
2008-01-29 17:31:29 -08:00
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|