| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Utilities for working with JsUnit. Writes out the JsUnit file |
| 17 |
* that needs to be included in every unit test. |
| 18 |
* |
| 19 |
* Testing code should not have dependencies outside of goog.testing so as to |
| 20 |
* reduce the chance of masking missing dependencies. |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
goog.setTestOnly('goog.testing.jsunit'); |
| 25 |
goog.provide('goog.testing.jsunit'); |
| 26 |
|
| 27 |
goog.require('goog.dom.TagName'); |
| 28 |
goog.require('goog.testing.TestCase'); |
| 29 |
goog.require('goog.testing.TestRunner'); |
| 30 |
goog.require('goog.userAgent'); |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* @define {boolean} If this code is being parsed by JsTestC, we let it disable |
| 35 |
* the onload handler to avoid running the test in JsTestC. |
| 36 |
*/ |
| 37 |
goog.define('goog.testing.jsunit.AUTO_RUN_ONLOAD', true); |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* @define {number} Sets a delay in milliseconds after the window onload event |
| 42 |
* and running the tests. Used as a workaround for IE failing to report load |
| 43 |
* event if the page has iframes. The appropriate value is zero; |
| 44 |
* maximum should be 500. Do not use this value to support asynchronous tests. |
| 45 |
*/ |
| 46 |
goog.define('goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS', 0); |
| 47 |
|
| 48 |
|
| 49 |
(function() { |
| 50 |
// Only allow one global test runner to be created on a page. |
| 51 |
if (goog.global['G_testRunner'] instanceof goog.testing.TestRunner) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
// Increases the maximum number of stack frames in Google Chrome from the |
| 56 |
// default 10 to 50 to get more useful stack traces. |
| 57 |
Error.stackTraceLimit = 50; |
| 58 |
|
| 59 |
// Store a reference to the window's timeout so that it can't be overridden |
| 60 |
// by tests. |
| 61 |
/** @type {!Function} */ |
| 62 |
var realTimeout = window.setTimeout; |
| 63 |
|
| 64 |
// Create a test runner. |
| 65 |
var tr = new goog.testing.TestRunner(); |
| 66 |
|
| 67 |
// Export it so that it can be queried by Selenium and tests that use a |
| 68 |
// compiled test runner. |
| 69 |
goog.exportSymbol('G_testRunner', tr); |
| 70 |
goog.exportSymbol('G_testRunner.initialize', tr.initialize); |
| 71 |
goog.exportSymbol('G_testRunner.isInitialized', tr.isInitialized); |
| 72 |
goog.exportSymbol('G_testRunner.isFinished', tr.isFinished); |
| 73 |
goog.exportSymbol('G_testRunner.getUniqueId', tr.getUniqueId); |
| 74 |
goog.exportSymbol('G_testRunner.isSuccess', tr.isSuccess); |
| 75 |
goog.exportSymbol('G_testRunner.getReport', tr.getReport); |
| 76 |
goog.exportSymbol('G_testRunner.getRunTime', tr.getRunTime); |
| 77 |
goog.exportSymbol('G_testRunner.getNumFilesLoaded', tr.getNumFilesLoaded); |
| 78 |
goog.exportSymbol('G_testRunner.setStrict', tr.setStrict); |
| 79 |
goog.exportSymbol('G_testRunner.logTestFailure', tr.logTestFailure); |
| 80 |
goog.exportSymbol('G_testRunner.getTestResults', tr.getTestResults); |
| 81 |
goog.exportSymbol( |
| 82 |
'G_testRunner.getTestResultsAsJson', tr.getTestResultsAsJson); |
| 83 |
|
| 84 |
// Export debug as a global function for JSUnit compatibility. This just |
| 85 |
// calls log on the current test case. |
| 86 |
if (!goog.global['debug']) { |
| 87 |
goog.exportSymbol('debug', goog.bind(tr.log, tr)); |
| 88 |
} |
| 89 |
|
| 90 |
// If the application has defined a global error filter, set it now. This |
| 91 |
// allows users who use a base test include to set the error filter before |
| 92 |
// the testing code is loaded. |
| 93 |
if (goog.global['G_errorFilter']) { |
| 94 |
tr.setErrorFilter(goog.global['G_errorFilter']); |
| 95 |
} |
| 96 |
|
| 97 |
var maybeGetStack = function(error) { |
| 98 |
if (typeof error == 'object') { |
| 99 |
var stack = error.stack; |
| 100 |
if (stack && typeof stack == 'string') { |
| 101 |
// non-empty string |
| 102 |
return stack; |
| 103 |
} |
| 104 |
} |
| 105 |
return ''; |
| 106 |
}; |
| 107 |
|
| 108 |
// Add an error handler to report errors that may occur during |
| 109 |
// initialization of the page. |
| 110 |
var onerror = window.onerror; |
| 111 |
window.onerror = function(messageOrEvent, url, line) { |
| 112 |
// TODO(johnlenz): fix this function parameters once the "onerror" |
| 113 |
// definition has been corrected. |
| 114 |
// colno and errObj were added later. |
| 115 |
var colno = arguments[3]; |
| 116 |
var errObj = arguments[4]; |
| 117 |
// Call any existing onerror handlers, except our boot handler. |
| 118 |
if (onerror && onerror != window["__onerror_at_boot"]) { |
| 119 |
onerror.apply(window, arguments); |
| 120 |
} |
| 121 |
var stack = maybeGetStack(errObj || messageOrEvent); |
| 122 |
if (stack) { |
| 123 |
tr.logError(stack); |
| 124 |
} else if (typeof messageOrEvent == 'object') { |
| 125 |
var error = messageOrEvent; |
| 126 |
// Some older webkit browsers pass an event object as the only argument |
| 127 |
// to window.onerror. It doesn't contain an error message, url or line |
| 128 |
// number. We therefore log as much info as we can. |
| 129 |
if (error.target && error.target.tagName == goog.dom.TagName.SCRIPT) { |
| 130 |
tr.logError('UNKNOWN ERROR: Script ' + error.target.src); |
| 131 |
} else { |
| 132 |
tr.logError('UNKNOWN ERROR: No error information available.'); |
| 133 |
} |
| 134 |
} else { |
| 135 |
// Add the column if it is available, older browsers won't have it. |
| 136 |
var colstr = colno != null ? '\nColumn: ' + colno : ''; |
| 137 |
tr.logError( |
| 138 |
'JS ERROR: ' + messageOrEvent + '\nURL: ' + url + '\nLine: ' + line + |
| 139 |
colstr); |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
/** |
| 144 |
* The onerror handler that may have been set by the test runner. |
| 145 |
* @type {?function(string, string=, number=, number=, Object=)} |
| 146 |
*/ |
| 147 |
window["__onerror_at_boot"] = window["__onerror_at_boot"] || null; |
| 148 |
/** |
| 149 |
* The arguments for any call to window.onerror occuring before this point. |
| 150 |
* @type {Array<!Array<?>>} */ |
| 151 |
window["__errors_since_boot"] = window["__errors_since_boot"] || null; |
| 152 |
|
| 153 |
if (window["__onerror_at_boot"]) { |
| 154 |
if (window['__errors_since_boot']) { |
| 155 |
for (var i = 0; i < window['__errors_since_boot'].length; i++) { |
| 156 |
var args = window['__errors_since_boot'][i]; |
| 157 |
window.onerror.apply(window, args); |
| 158 |
} |
| 159 |
} |
| 160 |
// http://perfectionkills.com/understanding-delete/#ie_bugs |
| 161 |
window["__onerror_at_boot"] = null; |
| 162 |
} |
| 163 |
|
| 164 |
// Create an onload handler, if the test runner hasn't been initialized then |
| 165 |
// no test has been registered with the test runner by the test file. We |
| 166 |
// then create a new test case and auto discover any tests in the global |
| 167 |
// scope. If this code is being parsed by JsTestC, we let it disable the |
| 168 |
// onload handler to avoid running the test in JsTestC. |
| 169 |
if (goog.testing.jsunit.AUTO_RUN_ONLOAD) { |
| 170 |
var onload = window.onload; |
| 171 |
window.onload = function(e) { |
| 172 |
// Call any existing onload handlers. |
| 173 |
if (onload) { |
| 174 |
onload(e); |
| 175 |
} |
| 176 |
// Execute the test on the next turn, to allow the WebDriver.get() |
| 177 |
// operation to return to the test runner and begin polling. |
| 178 |
var executionDelayAfterLoad = goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS; |
| 179 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('11')) { |
| 180 |
// Older IE Webdriver will not return onload if the page uses iframes. |
| 181 |
executionDelayAfterLoad = |
| 182 |
Math.max(goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS, 500); |
| 183 |
} |
| 184 |
|
| 185 |
realTimeout(function() { |
| 186 |
if (!tr.initialized) { |
| 187 |
var testCase = new goog.testing.TestCase(document.title); |
| 188 |
goog.testing.TestCase.initializeTestCase(testCase); |
| 189 |
tr.initialize(testCase); |
| 190 |
} |
| 191 |
tr.execute(); |
| 192 |
}, executionDelayAfterLoad); |
| 193 |
window.onload = null; |
| 194 |
}; |
| 195 |
} |
| 196 |
})(); |