2010-07-05 Adam Barth <abarth@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         'resources/adoption01.dat',
63         'resources/inbody01.dat',
64         'resources/tables01.dat'
65     ],
66     tests = [],
67     iframe = document.getElementsByTagName("iframe")[0],
68     stat = document.getElementsByTagName("p")[0].firstChild,
69     file = "",
70     test_number = 1,
71     fail_list = [],
72     log = "";
73
74 iframe.contentWindow.document.open()
75 iframe.contentWindow.document.write("Test");
76 iframe.contentWindow.document.close();
77 var write = iframe.contentWindow.document.lastChild.lastChild.lastChild !== null;
78 var ignoreTitle = iframe.contentWindow.document.getElementsByTagName("title")[0] !== undefined;
79
80 window.onload = function()
81 {
82     stat.data = "Running";
83     run();
84 }
85
86 function run()
87 {
88     var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
89     if (file = test_files.shift())
90     {
91         stat.data = "Retriving " + file;
92         test_number = 1;
93         fail_list = [];
94         log = "";
95         xhr.open("GET", file);
96         xhr.onreadystatechange = function()
97         {
98             if (xhr.readyState === 4)
99             {
100                 tests = xhr.responseText.split(/(?:^|\n\n)#data\n/);
101                 tests.shift();
102                 test();
103             }
104         }
105         xhr.send(null);
106     }
107 }
108
109 function test()
110 {
111     var input, errorsStart, fragmentStart, contextElement, domStart, dom;
112     if (data = tests.shift())
113     {
114         stat.data = "Running test " + test_number + " of " + (test_number + tests.length) + " in " + file;
115         errorsStart = data.indexOf("\n#errors\n");
116         if (errorsStart !== -1)
117         {
118             input = data.substring(0, errorsStart);
119             fragmentStart = data.indexOf("\n#document-fragment\n")
120             domStart = data.indexOf("\n#document\n")
121             if (fragmentStart !== -1)
122             {
123                 contextElement = data.substring(fragmentStart + 20, domStart);
124             }
125             if (domStart !== -1)
126             {
127                 dom = data.substring(domStart + 11);
128                 if (dom.substring(dom.length - 1) === "\n")
129                 {
130                     dom = dom.substring(0, dom.length - 1);
131                 }
132                 run_test(input, contextElement, dom);
133                 return;
134             }
135         }
136         alert("Invalid test: " + data);
137         test();
138         return;
139     }
140     else
141     {
142         stat.data = "Finished running " + file;
143         var overview = document.createElement("p");
144         if (fail_list.length)
145         {
146             overview.innerHTML = file + ":<br>" + fail_list.join("<br>");
147             overview.className = "overview";
148             overview.title = "Click for more details";
149             overview.onclick = function()
150             {
151                 this.nextSibling.style.display = this.nextSibling.style.display == "none" ? "block" : "none";
152             }
153             var detail = document.createElement("pre");
154             detail.appendChild(document.createTextNode(log.substring(2)));
155             detail.style.display = "block";
156             document.body.appendChild(overview);
157             document.body.appendChild(detail);
158         }
159         else
160         {
161             overview.innerHTML = file + ": PASS";
162             document.body.appendChild(overview);
163         }
164         stat.data = "";
165         run();
166     }
167 }
168
169 function run_test(input, contextElement, expected)
170 {
171     if (contextElement)
172     {
173         var element = document.createElement(contextElement);
174         try
175         {
176             element.innerHTML = input;
177         }
178         catch(e) {}
179         process_result(input, element, expected);
180     }
181     else if (write)
182     {
183         iframe.contentWindow.document.open();
184         try
185         {
186             iframe.contentWindow.document.write(input);
187         }
188         catch(e) {}
189         iframe.contentWindow.document.close();
190         if (ignoreTitle)
191         {
192             var title = iframe.contentWindow.document.getElementsByTagName("title")[0];
193             if (!title.innerHTML)
194             {
195                 title.parentElement.removeChild(title);
196             }
197         }
198         process_result(input, iframe.contentWindow.document, expected);
199     }
200     else
201     {
202         iframe.onload = function()
203         {
204             if (ignoreTitle)
205             {
206                 var title = iframe.contentWindow.document.getElementsByTagName("title")[0];
207                 if (!title.innerHTML)
208                 {
209                     title.parentElement.removeChild(title);
210                 }
211             }
212             process_result(input, iframe.contentWindow.document, expected);
213         }
214         iframe.src = "data:text/html," + encodeURIComponent(input);
215     }
216 }
217
218 function process_result(input, result, expected)
219 {
220     result = dom2string(result);
221     if (result !== expected)
222     {
223         fail_list.push(test_number);
224         log += "\n\nTest " + (test_number) + " of " + (test_number + tests.length) + " in " + file + " failed. Input:\n" + input + "\nGot:\n" + result + "\nExpected:\n" + expected;
225     }
226     test_number++;
227     test();
228 }
229 </script>