|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /** |
|
7 * Runs html5lib-formatted test cases in the browser. Requires SimpleTest. |
|
8 * |
|
9 * Define an array named parserDatFiles before loading this script, |
|
10 * and it will load each of those dat files into an array, then run |
|
11 * the test parser on each and run the tests by assigning the input |
|
12 * data to an iframe's url. |
|
13 * |
|
14 * Your test document should have an element with id "display" and |
|
15 * an iframe with id "testframe". |
|
16 */ |
|
17 |
|
18 var functionsToRunAsync = []; |
|
19 |
|
20 window.addEventListener("message", function(event) { |
|
21 if (event.source == window && event.data == "async-run") { |
|
22 event.stopPropagation(); |
|
23 var fn = functionsToRunAsync.shift(); |
|
24 fn(); |
|
25 } |
|
26 }, true); |
|
27 |
|
28 function asyncRun(fn) { |
|
29 functionsToRunAsync.push(fn); |
|
30 window.postMessage("async-run", "*"); |
|
31 } |
|
32 |
|
33 function writeErrorSummary(input, expected, got, isTodo) { |
|
34 if (isTodo) { |
|
35 $("display").appendChild(createEl('h2', null, "Unexpected Success:")); |
|
36 } else { |
|
37 $("display").appendChild(createEl('h2', null, "Unexpected Failure:")); |
|
38 } |
|
39 $("display").appendChild(createEl('br')); |
|
40 $("display").appendChild(createEl('span', null, "Matched: ")); |
|
41 $("display").appendChild(document.createTextNode("" + (expected == got))); |
|
42 var pre = createEl('pre'); |
|
43 pre.appendChild(document.createTextNode("Input: \n" + input, "\n-\n")); |
|
44 pre.appendChild(document.createTextNode("Expected:\n" + expected, "\n-\n")); |
|
45 pre.appendChild(document.createTextNode("Output:\n" + got + "\n-\n")); |
|
46 $("display").appendChild(pre); |
|
47 $("display").appendChild(createEl('hr')); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Control will bounce back and forth between nextTest() and the |
|
52 * event handler returned by makeTestChecker() or the callback returned by |
|
53 * makeFragmentTestChecker() until the 'testcases' iterator is spent. |
|
54 */ |
|
55 function makeTestChecker(input, expected, errors) { |
|
56 return function (e) { |
|
57 var domAsString = docToTestOutput(e.target.contentDocument); |
|
58 if (html5Exceptions[input]) { |
|
59 todo_is(domAsString, expected, "HTML5 expected success."); |
|
60 if (domAsString == expected) { |
|
61 writeErrorSummary(input, expected, domAsString, true); |
|
62 } |
|
63 } else { |
|
64 is(domAsString, expected, "HTML5 expected success."); |
|
65 if (domAsString != expected) { |
|
66 writeErrorSummary(input, expected, domAsString, false); |
|
67 } |
|
68 } |
|
69 nextTest(e.target); |
|
70 } |
|
71 } |
|
72 |
|
73 function makeFragmentTestChecker(input, |
|
74 expected, |
|
75 errors, |
|
76 fragment, |
|
77 testframe) { |
|
78 return function () { |
|
79 var context = document.createElementNS("http://www.w3.org/1999/xhtml", |
|
80 fragment); |
|
81 context.innerHTML = input; |
|
82 var domAsString = fragmentToTestOutput(context); |
|
83 is(domAsString, expected, "HTML5 expected success. " + new Date()); |
|
84 if (domAsString != expected) { |
|
85 writeErrorSummary(input, expected, domAsString, false); |
|
86 } |
|
87 nextTest(testframe); |
|
88 } |
|
89 } |
|
90 |
|
91 var testcases; |
|
92 function nextTest(testframe) { |
|
93 var test = 0; |
|
94 try { |
|
95 var [input, output, errors, fragment] = testcases.next(); |
|
96 if (fragment) { |
|
97 asyncRun(makeFragmentTestChecker(input, |
|
98 output, |
|
99 errors, |
|
100 fragment, |
|
101 testframe)); |
|
102 } else { |
|
103 dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(input); |
|
104 testframe.onload = makeTestChecker(input, output, errors); |
|
105 testframe.src = dataURL; |
|
106 } |
|
107 } catch (err if err instanceof StopIteration) { |
|
108 SimpleTest.finish(); |
|
109 } |
|
110 } |
|
111 |
|
112 var testFileContents = []; |
|
113 function loadNextTestFile() { |
|
114 var datFile = parserDatFiles.shift(); |
|
115 if (datFile) { |
|
116 var xhr = new XMLHttpRequest(); |
|
117 xhr.onreadystatechange = function () { |
|
118 if (this.readyState == 4) { |
|
119 testFileContents.push(this.responseText); |
|
120 loadNextTestFile(); |
|
121 } |
|
122 }; |
|
123 xhr.open("GET", "html5lib_tree_construction/" + datFile); |
|
124 xhr.send(); |
|
125 } else { |
|
126 testcases = test_parser(testFileContents); |
|
127 nextTest($("testframe")); |
|
128 } |
|
129 } |
|
130 |
|
131 addLoadEvent(loadNextTestFile); |
|
132 SimpleTest.waitForExplicitFinish(); |