CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / docshell / test / chrome / bug113934_window.xul
blobe9e3b245018ec2a1e6f45a5b64fc503117917490
1 <?xml version="1.0"?>
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
3 <window title="Mozilla Bug 113934" onload="doTheTest()"
4 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
6 <hbox>
7 <vbox id="box1">
8 </vbox>
9 <vbox id="box2">
10 </vbox>
11 <spacer flex="1"/>
12 </hbox>
14 <!-- test code goes here -->
15 <script type="application/javascript"><![CDATA[
16 var imports = [ "SimpleTest", "is", "isnot", "ok", "snapshotWindow",
17 "compareSnapshots", "onerror" ];
18 for each (var name in imports) {
19 window[name] = window.opener.wrappedJSObject[name];
22 function $(id) {
23 return document.getElementById(id);
26 function addBrowser(parent, id, width, height) {
27 var b =
28 document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "browser");
29 var type = window.location.search.slice(1);
30 is(type == "chrome" || type == "content", true, "Unexpected type");
31 b.setAttribute("type", type);
32 b.setAttribute("id", id);
33 b.setAttribute("width", width);
34 b.setAttribute("height", height);
35 $(parent).appendChild(b);
37 addBrowser("box1", "f1", 300, 200);
38 addBrowser("box1", "f2", 300, 200);
39 addBrowser("box2", "f3", 30, 200);
41 /** Test for Bug 113934 **/
42 var doc1 =
43 "data:text/html,<html><body onbeforeunload='document.documentElement.textContent = \"\"' onunload='document.documentElement.textContent = \"\"' onpagehide='document.documentElement.textContent = \"\"'>This is a test</body></html>";
44 var doc2 = "data:text/html,<html><head></head><body>This is a second test</body></html>";
47 $("f1").setAttribute("src", doc1);
48 $("f2").setAttribute("src", doc2);
49 $("f3").setAttribute("src", doc2);
51 function doTheTest() {
52 var s1 = snapshotWindow($("f1").contentWindow);
53 var s2 = snapshotWindow($("f2").contentWindow);
54 var s3 = snapshotWindow($("f3").contentWindow);
56 ok(!compareSnapshots(s2, s3, true)[0],
57 "Should look different due to different sizing");
59 function getDOM(id) {
60 return $(id).contentDocument.documentElement.innerHTML;
63 var dom1 = getDOM("f1");
65 var dom2 = getDOM("f2");
66 $("f2").contentDocument.body.textContent = "Modified the text";
67 var dom2star = getDOM("f2");
68 isnot(dom2, dom2star, "We changed the DOM!");
70 $("f1").swapDocShells($("f2"));
71 // now we have doms 2*, 1, 2 in the frames
73 is(getDOM("f1"), dom2star, "Shouldn't have changed the DOM on swap");
74 is(getDOM("f2"), dom1, "Shouldn't have fired event handlers");
76 // Test for bug 480149
77 // The DOMLink* events are dispatched asynchronously, thus I cannot
78 // just include the <link> element in the initial DOM and swap the
79 // docshells. Instead, the link element is added now. Then, when the
80 // first DOMLinkAdded event (which is a result of the actual addition)
81 // is dispatched, the docshells are swapped and the pageshow and pagehide
82 // events are tested. Only then, we wait for the DOMLink* events,
83 // which are a result of swapping the docshells.
84 var DOMLinkListener = {
85 _afterFirst: false,
86 _removedDispatched: false,
87 _addedDispatched: false,
88 handleEvent: function(aEvent) {
89 if (!this._afterFirst) {
90 is(aEvent.type, "DOMLinkAdded");
92 var strs = { "f1": "", "f3" : "" };
93 function attachListener(node, type) {
94 var listener = function(e) {
95 if (strs[node.id]) strs[node.id] += " ";
96 strs[node.id] += node.id + ".page" + type;
98 node.addEventListener("page" + type, listener, false);
100 listener.detach = function() {
101 node.removeEventListener("page" + type, listener, false);
103 return listener;
106 var l1 = attachListener($("f1"), "show");
107 var l2 = attachListener($("f1"), "hide");
108 var l3 = attachListener($("f3"), "show");
109 var l4 = attachListener($("f3"), "hide");
111 $("f1").swapDocShells($("f3"));
112 // now we have DOMs 2, 1, 2* in the frames
114 l1.detach();
115 l2.detach();
116 l3.detach();
117 l4.detach();
119 var s1_new = snapshotWindow($("f1").contentWindow);
120 var [same, first, second] = compareSnapshots(s1_new, s2, true);
121 ok(same, "Should reflow on swap", "Expected " + second + " but got " + first);
123 is(strs["f1"], "f1.pagehide f1.pageshow");
124 is(strs["f3"], "f3.pagehide f3.pageshow");
125 this._afterFirst = true;
126 return;
128 if (aEvent.type == "DOMLinkAdded") {
129 is(this._addedDispatched, false);
130 this._addedDispatched = true;
132 else {
133 is(this._removedDispatched, false);
134 this._removedDispatched = true;
137 if (this._addedDispatched && this._removedDispatched) {
138 $("f1").removeEventListener("DOMLinkAdded", this, false);
139 $("f1").removeEventListener("DOMLinkRemoved", this, false);
140 $("f3").removeEventListener("DOMLinkAdded", this, false);
141 $("f3").removeEventListener("DOMLinkRemoved", this, false);
142 window.close();
143 SimpleTest.finish();
148 $("f1").addEventListener("DOMLinkAdded", DOMLinkListener, false);
149 $("f1").addEventListener("DOMLinkRemoved", DOMLinkListener, false);
150 $("f3").addEventListener("DOMLinkAdded", DOMLinkListener, false);
151 $("f3").addEventListener("DOMLinkRemoved", DOMLinkListener, false);
153 var linkElement = $("f1").contentDocument.createElement("link");
154 linkElement.setAttribute("rel", "alternate");
155 linkElement.setAttribute("href", "about:blank");
156 $("f1").contentDocument.documentElement.firstChild.appendChild(linkElement);
159 ]]></script>
160 </window>