summaryrefslogtreecommitdiff
path: root/docshell/test/chrome/bug113934_window.xul
blob: 9dbfc3b8f4c67ff531e380ea2aa34b9864c3fd46 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window title="Mozilla Bug 113934" onload="doTheTest()"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <hbox>
    <vbox id="box1">
    </vbox>
    <vbox id="box2">
    </vbox>
    <spacer flex="1"/>
  </hbox>
  
  <!-- test code goes here -->
  <script type="application/javascript"><![CDATA[
    var imports = [ "SimpleTest", "is", "isnot", "ok", "snapshotWindow",
                    "compareSnapshots", "onerror" ];
    for (var name of imports) {
      window[name] = window.opener.wrappedJSObject[name];
    }

    function $(id) {
      return document.getElementById(id);
    }

    function addBrowser(parent, id, width, height) {
      var b =
        document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "browser");
      var type = window.location.search.slice(1);
      is(type == "chrome" || type == "content", true, "Unexpected type");
      b.setAttribute("type", type);
      b.setAttribute("id", id);
      b.setAttribute("width", width);
      b.setAttribute("height", height);
      $(parent).appendChild(b);
    }
    addBrowser("box1", "f1", 300, 200);
    addBrowser("box1", "f2", 300, 200);
    addBrowser("box2", "f3", 30, 200);

    /** Test for Bug 113934 **/
    var doc1 =
    "data:text/html,<html><body onbeforeunload='document.documentElement.textContent = \"\"' onunload='document.documentElement.textContent = \"\"' onpagehide='document.documentElement.textContent = \"\"'>This is a test</body></html>";
    var doc2 = "data:text/html,<html><head></head><body>This is a second test</body></html>";


    $("f1").setAttribute("src", doc1);
    $("f2").setAttribute("src", doc2);
    $("f3").setAttribute("src", doc2);

    function doTheTest() {
      var s1 = snapshotWindow($("f1").contentWindow);
      var s2 = snapshotWindow($("f2").contentWindow);
      var s3 = snapshotWindow($("f3").contentWindow);

      // This test is broken - see bug 1090274
      //ok(!compareSnapshots(s2, s3, true)[0],
      //   "Should look different due to different sizing");

      function getDOM(id) {
        return $(id).contentDocument.documentElement.innerHTML;
      }

      var dom1 = getDOM("f1");

      var dom2 = getDOM("f2");
      $("f2").contentDocument.body.textContent = "Modified the text";
      var dom2star = getDOM("f2");
      isnot(dom2, dom2star, "We changed the DOM!");

      $("f1").swapDocShells($("f2"));
      // now we have doms 2*, 1, 2 in the frames

      is(getDOM("f1"), dom2star, "Shouldn't have changed the DOM on swap");
      is(getDOM("f2"), dom1, "Shouldn't have fired event handlers");

      // Test for bug 480149
      // The DOMLink* events are dispatched asynchronously, thus I cannot
      // just include the <link> element in the initial DOM and swap the
      // docshells. Instead, the link element is added now. Then, when the
      // first DOMLinkAdded event (which is a result of the actual addition)
      // is dispatched, the docshells are swapped and the pageshow and pagehide
      // events are tested. Only then, we wait for the DOMLink* events,
      // which are a result of swapping the docshells.
      var DOMLinkListener = {
        _afterFirst: false,
        _removedDispatched: false,
        _addedDispatched: false,
        handleEvent: function(aEvent) {
          if (!this._afterFirst) {
            is(aEvent.type, "DOMLinkAdded");

            var strs = { "f1": "", "f3" : "" };
            function attachListener(node, type) {
              var listener = function(e) {
                if (strs[node.id]) strs[node.id] += " ";
                strs[node.id] += node.id + ".page" + type;
              }
              node.addEventListener("page" + type, listener, false);

              listener.detach = function() {
                node.removeEventListener("page" + type, listener, false);
              }
              return listener;
            }

            var l1 = attachListener($("f1"), "show");
            var l2 = attachListener($("f1"), "hide");
            var l3 = attachListener($("f3"), "show");
            var l4 = attachListener($("f3"), "hide");

            $("f1").swapDocShells($("f3"));
            // now we have DOMs 2, 1, 2* in the frames

            l1.detach();
            l2.detach();
            l3.detach();
            l4.detach();

            var s1_new = snapshotWindow($("f1").contentWindow);
            var [same, first, second] = compareSnapshots(s1_new, s2, true);
            ok(same, "Should reflow on swap", "Expected " + second + " but got " + first);

            is(strs["f1"], "f1.pagehide f1.pageshow");
            is(strs["f3"], "f3.pagehide f3.pageshow");
            this._afterFirst = true;
            return;
          }
          if (aEvent.type == "DOMLinkAdded") {
            is(this._addedDispatched, false);
            this._addedDispatched = true;
          }
          else {
            is(this._removedDispatched, false);
            this._removedDispatched = true;
          }

          if (this._addedDispatched && this._removedDispatched) {
            $("f1").removeEventListener("DOMLinkAdded", this, false);
            $("f1").removeEventListener("DOMLinkRemoved", this, false);
            $("f3").removeEventListener("DOMLinkAdded", this, false);
            $("f3").removeEventListener("DOMLinkRemoved", this, false);
            window.close();
            SimpleTest.finish();
          }
        }
      };

      $("f1").addEventListener("DOMLinkAdded", DOMLinkListener, false);
      $("f1").addEventListener("DOMLinkRemoved", DOMLinkListener, false);
      $("f3").addEventListener("DOMLinkAdded", DOMLinkListener, false);
      $("f3").addEventListener("DOMLinkRemoved", DOMLinkListener, false);

      var linkElement = $("f1").contentDocument.createElement("link");
      linkElement.setAttribute("rel", "alternate");
      linkElement.setAttribute("href", "about:blank");
      $("f1").contentDocument.documentElement.firstChild.appendChild(linkElement);
    }

  ]]></script>
</window>