summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/whatwg/postMessage_onOther.html
blob: dd4cf370a18320df0f7e71476d6c1b67d7bf2680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<html>
<head>
  <title>postMessage called through another frame</title>
  <script type="application/javascript">
    var PATH = "/tests/dom/tests/mochitest/whatwg/postMessage_onOther.html";

    function receiveMessage(evt)
    {
      if (evt.lastEventId !== "")
      {
        fail("unexpected non-empty lastEventId");
        return;
      }

      switch (window.location.href)
      {
        case "http://example.com" + PATH:
          receiveTopDomain(evt);
          break;

        case "http://test1.example.com" + PATH:
          receiveSubDomain(evt);
          break;

        default:
          fail("unexpected location");
      }
    }

    function fail(msg)
    {
      window.parent.postMessage("FAIL " + msg, "*");
    }

    // The parent frame sends "start-test" to the subdomain frame to start.
    // The subdomain frame then sets document.domain to the top domain so that
    // the top domain frame can access it.  It then sends a message to the top
    // domain frame to tell it to do likewise; once that happens, the top domain
    // frame can then call a method on the subdomain frame window, which will
    // call a method *on the top domain window* to send a message to the parent
    // window.  We thus expect to see an event whose source is the subdomain
    // window -- *not* the top domain window.  Therefore, its .origin should be:
    //
    //   http://test1.example.com
    //
    // and not
    //
    //   http://example.com

    function receiveSubDomain(evt)
    {
      if (evt.origin !== "http://mochi.test:8888")
      {
        fail("wrong top-domain origin: " + evt.origin);
        return;
      }
      if (evt.data !== "start-test")
      {
        fail("wrong top-domain message: " + evt.origin);
        return;
      }

      document.domain = "example.com";
      window.parent.topDomainFrame.postMessage("domain-switch",
                                               "http://example.com");
    }
    
    function receiveTopDomain(evt)
    {
      if (evt.origin !== "http://test1.example.com")
      {
        fail("wrong subdomain origin: " + evt.origin);
        return;
      }
      if (evt.data !== "domain-switch")
      {
        fail("wrong subdomain message: " + evt.origin);
        return;
      }
      if (evt.source !== window.parent.subDomainFrame)
      {
        fail("wrong source on message from subdomain");
        return;
      }

      document.domain = "example.com";
      window.parent.subDomainFrame.testSiblingPostMessage();
    }

    function testSiblingPostMessage()
    {
      window.parent.postMessage("test-finished", "http://mochi.test:8888");
    }

    function setup()
    {
      var target = document.getElementById("location");
      target.textContent = location.hostname + ":" + (location.port || 80);
    }

    window.addEventListener("message", receiveMessage, false);
    window.addEventListener("load", setup, false);
  </script>
</head>
<body>
<h1 id="location">No location!</h1>
</body>
</html>