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