PageRenderTime 36ms CodeModel.GetById 12ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/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/**
 41 * Runs html5lib-formatted test cases in the browser. Requires SimpleTest.
 42 *
 43 * Define an array named parserDatFiles before loading this script,
 44 * and it will load each of those dat files into an array, then run
 45 * the test parser on each and run the tests by assigning the input
 46 * data to an iframe's url.
 47 *
 48 * Your test document should have an element with id "display" and
 49 * an iframe with id "testframe".
 50 */
 51
 52var functionsToRunAsync = [];
 53
 54window.addEventListener("message", function(event) {
 55  if (event.source == window && event.data == "async-run") {
 56    event.stopPropagation();
 57    var fn = functionsToRunAsync.shift();
 58    fn();
 59  }
 60}, true);
 61
 62function asyncRun(fn) {
 63  functionsToRunAsync.push(fn);
 64  window.postMessage("async-run", "*");
 65}
 66
 67function writeErrorSummary(input, expected, got, isTodo) {
 68  if (isTodo) {
 69    $("display").appendChild(createEl('h2', null, "Unexpected Success:"));
 70  } else {
 71    $("display").appendChild(createEl('h2', null, "Unexpected Failure:"));
 72  }
 73  $("display").appendChild(createEl('br'));
 74  $("display").appendChild(createEl('span', null, "Matched: "));
 75  $("display").appendChild(document.createTextNode("" + (expected == got)));
 76  var pre = createEl('pre');
 77  pre.appendChild(createTextNode("Input: \n" + input, "\n-\n"));
 78  pre.appendChild(createTextNode("Expected:\n" + expected, "\n-\n"));
 79  pre.appendChild(createTextNode("Output:\n" + got + "\n-\n"));
 80  $("display").appendChild(pre);
 81  $("display").appendChild(createEl('hr'));
 82}
 83
 84/**
 85 * Control will bounce back and forth between nextTest() and the
 86 * event handler returned by makeTestChecker() or the callback returned by
 87 * makeFragmentTestChecker() until the 'testcases' iterator is spent.
 88 */
 89function makeTestChecker(input, expected, errors) {
 90  return function (e) {
 91    var domAsString = docToTestOutput(e.target.contentDocument);
 92    if (html5Exceptions[input]) {
 93      todo_is(domAsString, expected, "HTML5 expected success.");
 94      if (domAsString == expected) {
 95        writeErrorSummary(input, expected, domAsString, true);
 96      }
 97    } else {
 98      is(domAsString, expected, "HTML5 expected success.");
 99      if (domAsString != expected) {
100        writeErrorSummary(input, expected, domAsString, false);
101      }
102    }
103    nextTest(e.target);
104  } 
105}
106
107function makeFragmentTestChecker(input, 
108                                 expected, 
109                                 errors, 
110                                 fragment, 
111                                 testframe) {
112  return function () {
113    var context = document.createElementNS("http://www.w3.org/1999/xhtml",
114                                           fragment);
115    context.innerHTML = input;
116    var domAsString = fragmentToTestOutput(context);
117    is(domAsString, expected, "HTML5 expected success. " + new Date());
118    if (domAsString != expected) {
119      writeErrorSummary(input, expected, domAsString, false);
120    }
121    nextTest(testframe);
122  } 
123}
124
125var testcases;
126function nextTest(testframe) {
127  var test = 0;
128  try {
129    var [input, output, errors, fragment] = testcases.next();
130    if (fragment) {
131      asyncRun(makeFragmentTestChecker(input, 
132                                       output, 
133                                       errors, 
134                                       fragment, 
135                                       testframe));
136    } else {
137      dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(input);
138      testframe.onload = makeTestChecker(input, output, errors);
139      testframe.src = dataURL;
140    }
141  } catch (err if err instanceof StopIteration) {
142    SimpleTest.finish();
143  }
144}
145
146var testFileContents = [];
147function loadNextTestFile() {
148  var datFile = parserDatFiles.shift();
149  if (datFile) {
150    var xhr = new XMLHttpRequest();
151    xhr.onreadystatechange = function () {
152      if (this.readyState == 4) {
153        testFileContents.push(this.responseText);
154        loadNextTestFile();
155      }
156    };
157    xhr.open("GET", "html5lib_tree_construction/" + datFile);
158    xhr.send();
159  } else {
160    testcases = test_parser(testFileContents);
161    nextTest($("testframe"));
162  }
163}
164
165addLoadEvent(loadNextTestFile);
166SimpleTest.waitForExplicitFinish();