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