/parser/htmlparser/tests/mochitest/parser_web_testrunner.js

http://github.com/zpao/v8monkey · JavaScript · 166 lines · 102 code · 10 blank · 54 comment · 22 complexity · b32167b8e2d346e1cb5747cd40139ff9 MD5 · raw file

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is Mozilla Foundation.
  18. *
  19. * Portions created by the Initial Developer are Copyright (C) 2007
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Robert Sayre <sayrer@gmail.com>
  24. * Henri Sivonen <hsivonen@iki.fi>
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either the GNU General Public License Version 2 or later (the "GPL"), or
  28. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK *****/
  39. /**
  40. * Runs html5lib-formatted test cases in the browser. Requires SimpleTest.
  41. *
  42. * Define an array named parserDatFiles before loading this script,
  43. * and it will load each of those dat files into an array, then run
  44. * the test parser on each and run the tests by assigning the input
  45. * data to an iframe's url.
  46. *
  47. * Your test document should have an element with id "display" and
  48. * an iframe with id "testframe".
  49. */
  50. var functionsToRunAsync = [];
  51. window.addEventListener("message", function(event) {
  52. if (event.source == window && event.data == "async-run") {
  53. event.stopPropagation();
  54. var fn = functionsToRunAsync.shift();
  55. fn();
  56. }
  57. }, true);
  58. function asyncRun(fn) {
  59. functionsToRunAsync.push(fn);
  60. window.postMessage("async-run", "*");
  61. }
  62. function writeErrorSummary(input, expected, got, isTodo) {
  63. if (isTodo) {
  64. $("display").appendChild(createEl('h2', null, "Unexpected Success:"));
  65. } else {
  66. $("display").appendChild(createEl('h2', null, "Unexpected Failure:"));
  67. }
  68. $("display").appendChild(createEl('br'));
  69. $("display").appendChild(createEl('span', null, "Matched: "));
  70. $("display").appendChild(document.createTextNode("" + (expected == got)));
  71. var pre = createEl('pre');
  72. pre.appendChild(createTextNode("Input: \n" + input, "\n-\n"));
  73. pre.appendChild(createTextNode("Expected:\n" + expected, "\n-\n"));
  74. pre.appendChild(createTextNode("Output:\n" + got + "\n-\n"));
  75. $("display").appendChild(pre);
  76. $("display").appendChild(createEl('hr'));
  77. }
  78. /**
  79. * Control will bounce back and forth between nextTest() and the
  80. * event handler returned by makeTestChecker() or the callback returned by
  81. * makeFragmentTestChecker() until the 'testcases' iterator is spent.
  82. */
  83. function makeTestChecker(input, expected, errors) {
  84. return function (e) {
  85. var domAsString = docToTestOutput(e.target.contentDocument);
  86. if (html5Exceptions[input]) {
  87. todo_is(domAsString, expected, "HTML5 expected success.");
  88. if (domAsString == expected) {
  89. writeErrorSummary(input, expected, domAsString, true);
  90. }
  91. } else {
  92. is(domAsString, expected, "HTML5 expected success.");
  93. if (domAsString != expected) {
  94. writeErrorSummary(input, expected, domAsString, false);
  95. }
  96. }
  97. nextTest(e.target);
  98. }
  99. }
  100. function makeFragmentTestChecker(input,
  101. expected,
  102. errors,
  103. fragment,
  104. testframe) {
  105. return function () {
  106. var context = document.createElementNS("http://www.w3.org/1999/xhtml",
  107. fragment);
  108. context.innerHTML = input;
  109. var domAsString = fragmentToTestOutput(context);
  110. is(domAsString, expected, "HTML5 expected success. " + new Date());
  111. if (domAsString != expected) {
  112. writeErrorSummary(input, expected, domAsString, false);
  113. }
  114. nextTest(testframe);
  115. }
  116. }
  117. var testcases;
  118. function nextTest(testframe) {
  119. var test = 0;
  120. try {
  121. var [input, output, errors, fragment] = testcases.next();
  122. if (fragment) {
  123. asyncRun(makeFragmentTestChecker(input,
  124. output,
  125. errors,
  126. fragment,
  127. testframe));
  128. } else {
  129. dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(input);
  130. testframe.onload = makeTestChecker(input, output, errors);
  131. testframe.src = dataURL;
  132. }
  133. } catch (err if err instanceof StopIteration) {
  134. SimpleTest.finish();
  135. }
  136. }
  137. var testFileContents = [];
  138. function loadNextTestFile() {
  139. var datFile = parserDatFiles.shift();
  140. if (datFile) {
  141. var xhr = new XMLHttpRequest();
  142. xhr.onreadystatechange = function () {
  143. if (this.readyState == 4) {
  144. testFileContents.push(this.responseText);
  145. loadNextTestFile();
  146. }
  147. };
  148. xhr.open("GET", "html5lib_tree_construction/" + datFile);
  149. xhr.send();
  150. } else {
  151. testcases = test_parser(testFileContents);
  152. nextTest($("testframe"));
  153. }
  154. }
  155. addLoadEvent(loadNextTestFile);
  156. SimpleTest.waitForExplicitFinish();