Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>postMessage called through another frame</title>
5 <script type="application/javascript">
6 var PATH = "/tests/dom/tests/mochitest/whatwg/postMessage_onOther.html";
8 function receiveMessage(evt)
9 {
10 if (evt.lastEventId !== "")
11 {
12 fail("unexpected non-empty lastEventId");
13 return;
14 }
16 switch (window.location.href)
17 {
18 case "http://example.com" + PATH:
19 receiveTopDomain(evt);
20 break;
22 case "http://test1.example.com" + PATH:
23 receiveSubDomain(evt);
24 break;
26 default:
27 fail("unexpected location");
28 }
29 }
31 function fail(msg)
32 {
33 window.parent.postMessage("FAIL " + msg, "*");
34 }
36 // The parent frame sends "start-test" to the subdomain frame to start.
37 // The subdomain frame then sets document.domain to the top domain so that
38 // the top domain frame can access it. It then sends a message to the top
39 // domain frame to tell it to do likewise; once that happens, the top domain
40 // frame can then call a method on the subdomain frame window, which will
41 // call a method *on the top domain window* to send a message to the parent
42 // window. We thus expect to see an event whose source is the subdomain
43 // window -- *not* the top domain window. Therefore, its .origin should be:
44 //
45 // http://test1.example.com
46 //
47 // and not
48 //
49 // http://example.com
51 function receiveSubDomain(evt)
52 {
53 if (evt.origin !== "http://mochi.test:8888")
54 {
55 fail("wrong top-domain origin: " + evt.origin);
56 return;
57 }
58 if (evt.data !== "start-test")
59 {
60 fail("wrong top-domain message: " + evt.origin);
61 return;
62 }
64 document.domain = "example.com";
65 window.parent.topDomainFrame.postMessage("domain-switch",
66 "http://example.com");
67 }
69 function receiveTopDomain(evt)
70 {
71 if (evt.origin !== "http://test1.example.com")
72 {
73 fail("wrong subdomain origin: " + evt.origin);
74 return;
75 }
76 if (evt.data !== "domain-switch")
77 {
78 fail("wrong subdomain message: " + evt.origin);
79 return;
80 }
81 if (evt.source !== window.parent.subDomainFrame)
82 {
83 fail("wrong source on message from subdomain");
84 return;
85 }
87 document.domain = "example.com";
88 window.parent.subDomainFrame.testSiblingPostMessage();
89 }
91 function testSiblingPostMessage()
92 {
93 window.parent.postMessage("test-finished", "http://mochi.test:8888");
94 }
96 function setup()
97 {
98 var target = document.getElementById("location");
99 target.textContent = location.hostname + ":" + (location.port || 80);
100 }
102 window.addEventListener("message", receiveMessage, false);
103 window.addEventListener("load", setup, false);
104 </script>
105 </head>
106 <body>
107 <h1 id="location">No location!</h1>
108 </body>
109 </html>