summaryrefslogtreecommitdiffstats
path: root/dom/base/test/chrome/title_window.xhtml
blob: f48cdaaaf180016a5e3871feecd77b2d025b5b50 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<window title="Mozilla Bug 481777 subwindow"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="runTests()">

  <iframe type="content" id="html1" src="data:text/html,&lt;html&gt;&lt;head&gt;&lt;title id='t'&gt;Test&lt;/title&gt;&lt;/head&gt;&lt;/html&gt;"/>
  <iframe type="content" id="html2" src="data:text/html,&lt;html&gt;&lt;head&gt;&lt;title id='t'&gt;Test&lt;/title&gt;&lt;title&gt;Foo&lt;/title&gt;&lt;/head&gt;&lt;/html&gt;"/>
  <iframe type="content" id="html3" src="data:text/html,&lt;html&gt;&lt;/html&gt;"/>
  <iframe type="content" id="xhtml1" src="data:text/xml,&lt;html xmlns='http://www.w3.org/1999/xhtml'&gt;&lt;body&gt;&lt;title id='t'&gt;Test&lt;/title&gt;&lt;/body&gt;&lt;/html&gt;"/>
  <iframe type="content" id="xhtml2" src="data:text/xml,&lt;title xmlns='http://www.w3.org/1999/xhtml'&gt;Test&lt;/title&gt;"/>
  <iframe type="content" id="xhtml3" src="data:text/xml,&lt;title xmlns='http://www.w3.org/1999/xhtml'&gt;Te&lt;div&gt;bogus&lt;/div&gt;st&lt;/title&gt;"/>
  <iframe type="content" id="xhtml4" src="data:text/xml,&lt;html xmlns='http://www.w3.org/1999/xhtml'/>"/>
  <iframe type="content" id="xhtml5" src="data:text/xml,&lt;html xmlns='http://www.w3.org/1999/xhtml'&gt;&lt;head/>&lt;/html&gt;"/>
  <iframe type="content" id="xhtml6" src="data:text/xml,&lt;html xmlns='http://www.w3.org/1999/xhtml'&gt;&lt;head&gt;&lt;style/>&lt;/head&gt;&lt;/html&gt;"/>
  <iframe id="xul1" src="file_title.xhtml"/>
  <iframe id="xul2" src="file_title.xhtml"/>
  <iframe type="content" id="svg1" src="data:text/xml,&lt;svg xmlns='http://www.w3.org/2000/svg'&gt;&lt;title id='t'&gt;Test&lt;/title&gt;&lt;/svg&gt;"/>
  <iframe type="content" id="svg2" src="data:text/xml,&lt;svg xmlns='http://www.w3.org/2000/svg'&gt;&lt;title id='t'&gt;Test&lt;/title&gt;&lt;/svg&gt;"/>

  <script type="application/javascript">
    <![CDATA[
    var imports = [ "SimpleTest", "is", "isnot", "ok" ];
    for (var name of imports) {
      window[name] = window.arguments[0][name];
    }

    function testStatics() {
      function testStatic(id, expect, description) {
        is(document.getElementById(id).contentDocument.title, expect, description);
      }

      testStatic("html1", "Test", "HTML <title>");
      testStatic("html2", "Test", "choose the first HTML <title>");
      testStatic("html3", "", "No title");
      testStatic("xhtml1", "Test", "XHTML <title> in body");
      testStatic("xhtml2", "Test", "XHTML <title> as root element");
      testStatic("xhtml3", "Test", "XHTML <title> containing an element");
      testStatic("xul1", "Test", "XUL <window> title attribute");
      testStatic("svg1", "Test", "SVG <title>");

      // This one does nothing and won't fire an event
      document.getElementById("xhtml4").contentDocument.title = "Hello";
      is(document.getElementById("xhtml4").contentDocument.title, "", "Setting 'title' does nothing with no <head>");
    }

    function testDynamics() {
      var inProgress = {};
      var inProgressDoc = {};
      var inProgressWin = {};
      function testDynamic(id, expect, description, op, checkDOM) {
        inProgress[description] = true;
        inProgressDoc[description] = true;
        inProgressWin[description] = true;
        var frame = document.getElementById(id);

        function listener(ev) {
          inProgress[description] = false;
          is(frame.contentDocument.title, expect, "'title': " + description);
          is(frame.contentDocument, ev.target, "Unexpected target: " + description);
          if (typeof(checkDOM) != "undefined") {
            checkDOM(frame.contentDocument, "DOM: " + description);
          }
        }

        function listener2(ev) {
          inProgressDoc[description] = false;
        }
        function listener3(ev) {
          inProgressWin[description] = false;
        }
        frame.addEventListener("DOMTitleChanged", listener);
        frame.contentDocument.addEventListener("DOMTitleChanged", listener2);
        frame.contentWindow.addEventListener("DOMTitleChanged", listener3);

        op(frame.contentDocument);
      }

      var dynamicTests = [
        [ "html1", "Hello", "Setting HTML <title> text contents",
          function(doc){
            var t = doc.getElementById("t"); t.textContent = "Hello";
          } ],
        [ "html2", "Foo", "Removing HTML <title>",
          function(doc){
            var t = doc.getElementById("t"); t.remove();
          } ],
        [ "html3", "Hello", "Appending HTML <title> element to root element",
          function(doc){
            var t = doc.createElement("title"); t.textContent = "Hello"; doc.documentElement.appendChild(t);
          } ],
        [ "xhtml3", "Hello", "Setting 'title' clears existing <title> contents",
          function(doc){
            doc.title = "Hello";
          },
          function(doc, desc) {
            is(doc.documentElement.firstChild.data, "Hello", desc);
            is(doc.documentElement.firstChild.nextSibling, null, desc);
          } ],
        [ "xhtml5", "Hello", "Setting 'title' works with a <head>",
          function(doc){
            doc.title = "Hello";
          },
          function(doc, desc) {
            var head = doc.documentElement.firstChild;
            var title = head.firstChild;
            is(title.tagName.toLowerCase(), "title", desc);
            is(title.firstChild.data, "Hello", desc);
            is(title.firstChild.nextSibling, null, desc);
            is(title.nextSibling, null, desc);
          } ],
        [ "xhtml6", "Hello", "Setting 'title' appends to <head>",
          function(doc){
            doc.title = "Hello";
          },
          function(doc, desc) {
            var head = doc.documentElement.firstChild;
            is(head.firstChild.tagName.toLowerCase(), "style", desc);
            var title = head.firstChild.nextSibling;
            is(title.tagName.toLowerCase(), "title", desc);
            is(title.firstChild.data, "Hello", desc);
            is(title.firstChild.nextSibling, null, desc);
            is(title.nextSibling, null, desc);
          } ],
        [ "xul1", "Hello", "Setting XUL <window> title attribute",
          function(doc){
            doc.documentElement.setAttribute("title", "Hello");
          } ],
        [ "xul2", "Hello", "Setting 'title' in XUL",
          function(doc){
            doc.title = "Hello";
          },
          function(doc, desc) {
            is(doc.documentElement.getAttribute("title"), "Hello", desc);
            is(doc.documentElement.firstChild, null, desc);
          } ],
        [ "svg1", "Hello", "Setting SVG <title> text contents",
          function(doc){
            var t = doc.getElementById("t"); t.textContent = "Hello";
          } ],
        [ "svg2", "", "Removing SVG <title>",
          function(doc){
            var t = doc.getElementById("t"); t.remove();
          } ] ];

      var titleWindow = window;

      function runIndividualTest(i) {
        if (i == dynamicTests.length) {
          // Closing the window will nuke the global properties, since this
          // function is not really running on this window... or something
          // like that.  Thanks, executeSoon!
          var tester = SimpleTest;
          window.close();
          tester.finish();
        } else {
          var parameters = dynamicTests[i];
          var testElementId = parameters[0];
          var testExpectedValue = parameters[1];
          var testDescription = parameters[2];
          var testOp = parameters[3];
          var testCheckDOM = parameters[4];

          function checkTest() {
            ok(!inProgress[testDescription],
               testDescription + ": DOMTitleChange not fired");
            ok(inProgressDoc[testDescription],
               testDescription + ": DOMTitleChange fired on content document");
            ok(inProgressWin[testDescription],
               testDescription + ": DOMTitleChange fired on content window");
            // Run the next test in the context of the parent XUL window.
            titleWindow.setTimeout(runIndividualTest, 0, i+1);
          }
          function spinEventLoopOp(doc) {
            // Perform the test's operations.
            testOp(doc);
            // Spin the associated window's event loop to ensure we
            // drain any asynchronous changes and fire associated
            // events.
            doc.defaultView.setTimeout(checkTest, 0);
          }

          testDynamic(testElementId, testExpectedValue, testDescription,
                      spinEventLoopOp, testCheckDOM);
        }
      }

      window.setTimeout(runIndividualTest, 0, 0);
    }

    function runTests() {
      testStatics();
      testDynamics();
    }
    ]]>
  </script>
</window>