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