Path: blob/trunk/third_party/closure/goog/testing/jsunit.js
4501 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Utilities for working with JsUnit. Writes out the JsUnit file8* that needs to be included in every unit test.9*10* Testing code should not have dependencies outside of goog.testing so as to11* reduce the chance of masking missing dependencies.12*/1314goog.setTestOnly('goog.testing.jsunit');15goog.provide('goog.testing.jsunit');1617goog.require('goog.dom.TagName');18goog.require('goog.testing.TestCase');19goog.require('goog.testing.TestRunner');20goog.require('goog.testing.asserts');212223/**24* @define {boolean} If this code is being parsed by JsTestC, we let it disable25* the onload handler to avoid running the test in JsTestC.26*/27goog.testing.jsunit.AUTO_RUN_ONLOAD =28goog.define('goog.testing.jsunit.AUTO_RUN_ONLOAD', true);293031/**32* @define {number} Sets a delay in milliseconds after the window onload event33* and running the tests. See goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS.34*/35goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT =36goog.define('goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT', 0);3738/**39* @type {number} Sets a delay in milliseconds after the window onload event40* and running the tests. Used as a workaround for IE failing to report load41* event if the page has iframes. The appropriate value is zero;42* maximum should be 500. Do not use this value to support asynchronous tests.43*/44goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS =45goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT;464748(function() {49'use strict';50// Only allow one global test runner to be created on a page.51if (goog.global['G_testRunner'] instanceof goog.testing.TestRunner) {52return;53}5455// Increases the maximum number of stack frames in Google Chrome from the56// default 10 to 50 to get more useful stack traces.57Error.stackTraceLimit = 50;5859// Store a reference to the window's timeout so that it can't be overridden60// by tests.61/** @type {!Function} */62var realTimeout = window.setTimeout;6364// Create a test runner.65var tr = new goog.testing.TestRunner();6667// Export it so that it can be queried by Selenium and tests that use a68// compiled test runner.69goog.exportSymbol('G_testRunner', tr);70goog.exportSymbol('G_testRunner.initialize', tr.initialize);71goog.exportSymbol('G_testRunner.isInitialized', tr.isInitialized);72goog.exportSymbol('G_testRunner.isFinished', tr.isFinished);73goog.exportSymbol('G_testRunner.getUniqueId', tr.getUniqueId);74goog.exportSymbol('G_testRunner.isSuccess', tr.isSuccess);75goog.exportSymbol('G_testRunner.getReport', tr.getReport);76goog.exportSymbol('G_testRunner.getRunTime', tr.getRunTime);77goog.exportSymbol('G_testRunner.getNumFilesLoaded', tr.getNumFilesLoaded);78goog.exportSymbol('G_testRunner.setStrict', tr.setStrict);79goog.exportSymbol('G_testRunner.logTestFailure', tr.logTestFailure);80goog.exportSymbol('G_testRunner.getTestResults', tr.getTestResults);81goog.exportSymbol('G_testRunner.getTestResultsAsJson', tr.getTestResultsAsJson);8283// Export debug as a global function for JSUnit compatibility. This just84// calls log on the current test case.85if (!goog.global['debug']) {86goog.exportSymbol('debug', goog.bind(tr.log, tr));87}8889// If the application has defined a global error filter, set it now. This90// allows users who use a base test include to set the error filter before91// the testing code is loaded.92if (goog.global['G_errorFilter']) {93tr.setErrorFilter(goog.global['G_errorFilter']);94}9596var maybeGetStack = function(error) {97'use strict';98var stack = error && error.stack;99return typeof stack === 'string' ? stack : '';100};101102// Add an error handler to report errors that may occur during103// initialization of the page.104var onerror = window.onerror;105window.onerror = function(messageOrEvent, url, line) {106'use strict';107// TODO(johnlenz): fix this function parameters once the "onerror"108// definition has been corrected.109// colno and errObj were added later.110var colno = arguments[3];111var errObj = arguments[4];112// Call any existing onerror handlers, except our boot handler.113if (onerror && onerror != window['__onerror_at_boot']) {114onerror.apply(window, arguments);115}116var stack = maybeGetStack(errObj || messageOrEvent);117if (stack) {118tr.logError(String(messageOrEvent) + '\n' + stack);119} else if (typeof messageOrEvent == 'object') {120var error = /** @type {{target: ?}} */ (messageOrEvent);121// Some older webkit browsers pass an event object as the only argument122// to window.onerror. It doesn't contain an error message, url or line123// number. We therefore log as much info as we can.124if (error.target && error.target.tagName == goog.dom.TagName.SCRIPT) {125tr.logError('UNKNOWN ERROR: Script ' + error.target.src);126} else {127tr.logError('UNKNOWN ERROR: No error information available.');128}129} else {130// Add the column if it is available, older browsers won't have it.131var colstr = colno != null ? '\nColumn: ' + colno : '';132tr.logError(133'JS ERROR: ' + messageOrEvent + '\nURL: ' + url + '\nLine: ' + line +134colstr);135}136};137138/**139* The onerror handler that may have been set by the test runner.140* @type {?function(string, string=, number=, number=, Object=)}141*/142window['__onerror_at_boot'] = window['__onerror_at_boot'] || null;143/**144* The arguments for any call to window.onerror occuring before this point.145* @type {?Array<!Array<?>>}146*/147window['__errors_since_boot'] = window['__errors_since_boot'] || null;148149if (window['__onerror_at_boot']) {150if (window['__errors_since_boot']) {151for (var i = 0; i < window['__errors_since_boot'].length; i++) {152var args = window['__errors_since_boot'][i];153window.onerror.apply(window, args);154}155}156// http://perfectionkills.com/understanding-delete/#ie_bugs157window['__onerror_at_boot'] = null;158}159160// Create an onload handler, if the test runner hasn't been initialized then161// no test has been registered with the test runner by the test file. We162// then create a new test case and auto discover any tests in the global163// scope. If this code is being parsed by JsTestC, we let it disable the164// onload handler to avoid running the test in JsTestC.165if (goog.testing.jsunit.AUTO_RUN_ONLOAD) {166var onload = window.onload;167window.onload = function(e) {168'use strict';169// Call any existing onload handlers.170if (onload) {171onload(e);172}173// Execute the test on the next turn, to allow the WebDriver.get()174// operation to return to the test runner and begin polling.175var executionDelayAfterLoad = goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS;176177realTimeout(function() {178'use strict';179if (!tr.initialized) {180var testCase = new goog.testing.TestCase(document.title);181goog.testing.TestCase.initializeTestCase(testCase);182tr.initialize(testCase);183}184tr.execute();185}, executionDelayAfterLoad);186window.onload = null;187};188}189})();190191192