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