2010-06-30 Eric Seidel <eric@webkit.org>
[WebKit-https.git] / LayoutTests / html5lib / runner.html
1 <!doctype html>
2 <!--
3 Copyright (c) 2008 Geoffrey Sneddon
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 -->
23 <title>html5lib test runner</title>
24 <script src=resources/dom2string.js></script>
25 <script>
26 if (window.layoutTestController)
27     layoutTestController.dumpAsText();
28 </script>
29 <style>
30 .overview:hover {
31 background: #ccc;
32 }
33 iframe {
34 display: none;
35 }
36 </style>
37 <p>Script did not run</p>
38 <iframe></iframe>
39 <script>
40 var test_files = [
41         'resources/tests1.dat',
42         'resources/tests2.dat',
43         'resources/tests3.dat',
44         'resources/tests4.dat',
45         'resources/tests5.dat',
46         'resources/tests6.dat',
47         'resources/tests7.dat',
48         'resources/tests8.dat',
49         'resources/tests9.dat',
50         'resources/tests10.dat',
51         'resources/tests11.dat',
52         'resources/tests12.dat',
53         'resources/tests14.dat',
54         'resources/tests15.dat',
55         'resources/tests16.dat',
56         'resources/webkit01.dat',
57         'resources/doctype01.dat',
58         'resources/scriptdata01.dat',
59         'resources/entities01.dat',
60         'resources/entities02.dat',
61         'resources/comments01.dat'
62     ],
63     tests = [],
64     iframe = document.getElementsByTagName("iframe")[0],
65     stat = document.getElementsByTagName("p")[0].firstChild,
66     file = "",
67     test_number = 1,
68     fail_list = [],
69     log = "";
70
71 iframe.contentWindow.document.open()
72 iframe.contentWindow.document.write("Test");
73 iframe.contentWindow.document.close();
74 var write = iframe.contentWindow.document.lastChild.lastChild.lastChild !== null;
75 var ignoreTitle = iframe.contentWindow.document.getElementsByTagName("title")[0] !== undefined;
76
77 window.onload = function()
78 {
79     stat.data = "Running";
80     run();
81 }
82
83 function run()
84 {
85     var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
86     if (file = test_files.shift())
87     {
88         stat.data = "Retriving " + file;
89         test_number = 1;
90         fail_list = [];
91         log = "";
92         xhr.open("GET", file);
93         xhr.onreadystatechange = function()
94         {
95             if (xhr.readyState === 4)
96             {
97                 tests = xhr.responseText.split(/(?:^|\n\n)#data\n/);
98                 tests.shift();
99                 test();
100             }
101         }
102         xhr.send(null);
103     }
104 }
105
106 function test()
107 {
108     var input, errorsStart, fragmentStart, contextElement, domStart, dom;
109     if (data = tests.shift())
110     {
111         stat.data = "Running test " + test_number + " of " + (test_number + tests.length) + " in " + file;
112         errorsStart = data.indexOf("\n#errors\n");
113         if (errorsStart !== -1)
114         {
115             input = data.substring(0, errorsStart);
116             fragmentStart = data.indexOf("\n#document-fragment\n")
117             domStart = data.indexOf("\n#document\n")
118             if (fragmentStart !== -1)
119             {
120                 contextElement = data.substring(fragmentStart + 20, domStart);
121             }
122             if (domStart !== -1)
123             {
124                 dom = data.substring(domStart + 11);
125                 if (dom.substring(dom.length - 1) === "\n")
126                 {
127                     dom = dom.substring(0, dom.length - 1);
128                 }
129                 run_test(input, contextElement, dom);
130                 return;
131             }
132         }
133         alert("Invalid test: " + data);
134         test();
135         return;
136     }
137     else
138     {
139         stat.data = "Finished running " + file;
140         var overview = document.createElement("p");
141         if (fail_list.length)
142         {
143             overview.innerHTML = file + ":<br>" + fail_list.join("<br>");
144             overview.className = "overview";
145             overview.title = "Click for more details";
146             overview.onclick = function()
147             {
148                 this.nextSibling.style.display = this.nextSibling.style.display == "none" ? "block" : "none";
149             }
150             var detail = document.createElement("pre");
151             detail.appendChild(document.createTextNode(log.substring(2)));
152             detail.style.display = "block";
153             document.body.appendChild(overview);
154             document.body.appendChild(detail);
155         }
156         else
157         {
158             overview.innerHTML = file + ": PASS";
159             document.body.appendChild(overview);
160         }
161         stat.data = "";
162         run();
163     }
164 }
165
166 function run_test(input, contextElement, expected)
167 {
168     if (contextElement)
169     {
170         var element = document.createElement(contextElement);
171         try
172         {
173             element.innerHTML = input;
174         }
175         catch(e) {}
176         process_result(input, element, expected);
177     }
178     else if (write)
179     {
180         iframe.contentWindow.document.open();
181         try
182         {
183             iframe.contentWindow.document.write(input);
184         }
185         catch(e) {}
186         iframe.contentWindow.document.close();
187         if (ignoreTitle)
188         {
189             var title = iframe.contentWindow.document.getElementsByTagName("title")[0];
190             if (!title.innerHTML)
191             {
192                 title.parentElement.removeChild(title);
193             }
194         }
195         process_result(input, iframe.contentWindow.document, expected);
196     }
197     else
198     {
199         iframe.onload = function()
200         {
201             if (ignoreTitle)
202             {
203                 var title = iframe.contentWindow.document.getElementsByTagName("title")[0];
204                 if (!title.innerHTML)
205                 {
206                     title.parentElement.removeChild(title);
207                 }
208             }
209             process_result(input, iframe.contentWindow.document, expected);
210         }
211         iframe.src = "data:text/html," + encodeURIComponent(input);
212     }
213 }
214
215 function process_result(input, result, expected)
216 {
217     result = dom2string(result);
218     if (result !== expected)
219     {
220         fail_list.push(test_number);
221         log += "\n\nTest " + (test_number) + " of " + (test_number + tests.length) + " in " + file + " failed. Input:\n" + input + "\nGot:\n" + result + "\nExpected:\n" + expected;
222     }
223     test_number++;
224     test();
225 }
226 </script>