2010-09-12 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/dump-as-markup.js></script>
25 <style>
26 .overview:hover {
27 background: #ccc;
28 }
29 iframe {
30 display: none;
31 }
32 </style>
33 <p>Script did not run</p>
34 <iframe></iframe>
35 <script>
36 Markup.noAutoDump();
37
38 var test_files = [
39         'resources/tests1.dat',
40         'resources/tests2.dat',
41         'resources/tests3.dat',
42         'resources/tests4.dat',
43         'resources/tests5.dat',
44         'resources/tests6.dat',
45         'resources/tests7.dat',
46         'resources/tests8.dat',
47         'resources/tests9.dat',
48         'resources/tests10.dat',
49         'resources/tests11.dat',
50         'resources/tests12.dat',
51         'resources/tests14.dat',
52         'resources/tests15.dat',
53         'resources/tests16.dat',
54         'resources/tests17.dat',
55         'resources/tests18.dat',
56         'resources/tests19.dat',
57         'resources/tests20.dat',
58         'resources/tests21.dat',
59         'resources/tests_innerHTML_1.dat',
60         'resources/webkit01.dat',
61         'resources/webkit02.dat',
62         'resources/doctype01.dat',
63         'resources/scriptdata01.dat',
64         'resources/html5test-com.dat',
65         'resources/entities01.dat',
66         'resources/entities02.dat',
67         'resources/comments01.dat',
68         'resources/adoption01.dat',
69         'resources/adoption02.dat',
70         'resources/inbody01.dat',
71         'resources/isindex.dat',
72         'resources/tables01.dat',
73         'resources/tricky01.dat',
74         'resources/scripted/adoption01.dat',
75         'resources/scripted/webkit01.dat'
76     ],
77     tests = [],
78     iframe = document.getElementsByTagName("iframe")[0],
79     stat = document.getElementsByTagName("p")[0].firstChild,
80     file = "",
81     test_number = 1,
82     fail_list = [],
83     log = "";
84
85 iframe.contentWindow.document.open()
86 iframe.contentWindow.document.write("Test");
87 iframe.contentWindow.document.close();
88 var write = iframe.contentWindow.document.lastChild.lastChild.lastChild !== null;
89 var ignoreTitle = iframe.contentWindow.document.getElementsByTagName("title")[0] !== undefined;
90
91 window.onload = function()
92 {
93     stat.data = "Running";
94     run();
95 }
96
97 function run()
98 {
99     var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
100     if (file = test_files.shift())
101     {
102         stat.data = "Retriving " + file;
103         test_number = 1;
104         fail_list = [];
105         log = "";
106         xhr.open("GET", file);
107         xhr.onreadystatechange = function()
108         {
109             if (xhr.readyState === 4)
110             {
111                 tests = xhr.responseText.split(/(?:^|\n\n)#data\n/);
112                 tests.shift();
113                 test();
114             }
115         }
116         xhr.send(null);
117     }
118 }
119
120 function test()
121 {
122     var input, errorsStart, fragmentStart, contextElement, domStart, dom;
123     if (data = tests.shift())
124     {
125         stat.data = "Running test " + test_number + " of " + (test_number + tests.length) + " in " + file;
126         errorsStart = data.indexOf("\n#errors\n");
127         if (errorsStart !== -1)
128         {
129             input = data.substring(0, errorsStart);
130             fragmentStart = data.indexOf("\n#document-fragment\n")
131             domStart = data.indexOf("\n#document\n")
132             if (fragmentStart !== -1)
133             {
134                 contextElement = data.substring(fragmentStart + 20, domStart);
135             }
136             if (domStart !== -1)
137             {
138                 dom = data.substring(domStart + 11);
139                 if (dom.substring(dom.length - 1) === "\n")
140                 {
141                     dom = dom.substring(0, dom.length - 1);
142                 }
143                 run_test(input, contextElement, dom);
144                 return;
145             }
146         }
147         alert("Invalid test: " + data);
148         test();
149         return;
150     }
151     else
152     {
153         stat.data = "Finished running " + file;
154         var overview = document.createElement("p");
155         if (fail_list.length)
156         {
157             overview.innerHTML = file + ":<br>" + fail_list.join("<br>");
158             overview.className = "overview";
159             overview.title = "Click for more details";
160             overview.onclick = function()
161             {
162                 this.nextSibling.style.display = this.nextSibling.style.display == "none" ? "block" : "none";
163             }
164             var detail = document.createElement("pre");
165             detail.appendChild(document.createTextNode(log.substring(2)));
166             detail.style.display = "block";
167             document.body.appendChild(overview);
168             document.body.appendChild(detail);
169         }
170         else
171         {
172             overview.innerHTML = file + ": PASS";
173             document.body.appendChild(overview);
174         }
175         stat.data = "";
176         run();
177     }
178 }
179
180 function run_test(input, contextElement, expected)
181 {
182     if (contextElement)
183     {
184         var element = document.createElement(contextElement);
185         try
186         {
187             element.innerHTML = input;
188         }
189         catch(e) {}
190         process_result(input, element, expected);
191     }
192     else if (write)
193     {
194         iframe.contentWindow.document.open();
195         try
196         {
197             iframe.contentWindow.document.write(input);
198         }
199         catch(e) {}
200         iframe.contentWindow.document.close();
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     else
212     {
213         iframe.onload = function()
214         {
215             if (ignoreTitle)
216             {
217                 var title = iframe.contentWindow.document.getElementsByTagName("title")[0];
218                 if (!title.innerHTML)
219                 {
220                     title.parentElement.removeChild(title);
221                 }
222             }
223             process_result(input, iframe.contentWindow.document, expected);
224         }
225         iframe.src = "data:text/html," + encodeURIComponent(input);
226     }
227 }
228
229 function process_result(input, result, expected)
230 {
231     result = Markup.get(result);
232     if (result !== expected)
233     {
234         fail_list.push(test_number);
235         log += "\n\nTest " + (test_number) + " of " + (test_number + tests.length) + " in " + file + " failed. Input:\n" + input + "\nGot:\n" + result + "\nExpected:\n" + expected;
236     }
237     test_number++;
238     test();
239 }
240 </script>