# lib/goog/testing/jsunit.js

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.provide('goog.testing.jsunit');
25 |
26 | goog.require('goog.dom.TagName');
27 | goog.require('goog.testing.TestCase');
28 | goog.require('goog.testing.TestRunner');
29 |
30 |
31 | /**
32 | * Base path for JsUnit app files, relative to Closure's base path.
33 | * @type {string}
34 | */
35 | goog.testing.jsunit.BASE_PATH =
36 | '../../third_party/java/jsunit/core/app/';
37 |
38 |
39 | /**
40 | * Filename for the core JS Unit script.
41 | * @type {string}
42 | */
43 | goog.testing.jsunit.CORE_SCRIPT =
44 | goog.testing.jsunit.BASE_PATH + 'jsUnitCore.js';
45 |
46 |
47 | /**
48 | * @define {boolean} If this code is being parsed by JsTestC, we let it disable
49 | * the onload handler to avoid running the test in JsTestC.
50 | */
51 | goog.define('goog.testing.jsunit.AUTO_RUN_ONLOAD', true);
52 |
53 |
54 | /**
55 | * @define {number} Sets a delay in milliseconds after the window onload event
56 | * and running the tests. Used to prevent interference with Selenium and give
57 | * tests with asynchronous operations time to finish loading.
58 | */
59 | goog.define('goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS', 500);
60 |
61 |
62 | (function() {
63 | // Only allow one global test runner to be created on a page.
64 | if (goog.global['G_testRunner'] instanceof goog.testing.TestRunner) {
65 | return;
66 | }
67 |
68 | // Increases the maximum number of stack frames in Google Chrome from the
69 | // default 10 to 50 to get more useful stack traces.
70 | Error.stackTraceLimit = 50;
71 |
72 | // Store a reference to the window's timeout so that it can't be overridden
73 | // by tests.
74 | /** @type {!Function} */
75 | var realTimeout = window.setTimeout;
76 |
77 | // Check for JsUnit's test runner (need to check for >2.2 and <=2.2)
78 | if (top['JsUnitTestManager'] || top['jsUnitTestManager']) {
79 | // Running inside JsUnit so add support code.
80 | var path = goog.basePath + goog.testing.jsunit.CORE_SCRIPT;
81 | document.write('<script type="text/javascript" src="' +
82 | path + '"></' + 'script>');
83 |
84 | } else {
85 |
86 | // Create a test runner.
87 | var tr = new goog.testing.TestRunner();
88 |
89 | // Export it so that it can be queried by Selenium and tests that use a
90 | // compiled test runner.
91 | goog.exportSymbol('G_testRunner', tr);
92 | goog.exportSymbol('G_testRunner.initialize', tr.initialize);
93 | goog.exportSymbol('G_testRunner.isInitialized', tr.isInitialized);
94 | goog.exportSymbol('G_testRunner.isFinished', tr.isFinished);
95 | goog.exportSymbol('G_testRunner.isSuccess', tr.isSuccess);
96 | goog.exportSymbol('G_testRunner.getReport', tr.getReport);
97 | goog.exportSymbol('G_testRunner.getRunTime', tr.getRunTime);
98 | goog.exportSymbol('G_testRunner.getNumFilesLoaded', tr.getNumFilesLoaded);
99 | goog.exportSymbol('G_testRunner.setStrict', tr.setStrict);
100 | goog.exportSymbol('G_testRunner.logTestFailure', tr.logTestFailure);
101 | goog.exportSymbol('G_testRunner.getTestResults', tr.getTestResults);
102 |
103 | // Export debug as a global function for JSUnit compatibility. This just
104 | // calls log on the current test case.
105 | if (!goog.global['debug']) {
106 | goog.exportSymbol('debug', goog.bind(tr.log, tr));
107 | }
108 |
109 | // If the application has defined a global error filter, set it now. This
110 | // allows users who use a base test include to set the error filter before
111 | // the testing code is loaded.
112 | if (goog.global['G_errorFilter']) {
113 | tr.setErrorFilter(goog.global['G_errorFilter']);
114 | }
115 |
116 | // Add an error handler to report errors that may occur during
117 | // initialization of the page.
118 | var onerror = window.onerror;
119 | window.onerror = function(error, url, line) {
120 | // Call any existing onerror handlers.
121 | if (onerror) {
122 | onerror(error, url, line);
123 | }
124 | if (typeof error == 'object') {
125 | // Webkit started passing an event object as the only argument to
126 | // window.onerror. It doesn't contain an error message, url or line
127 | // number. We therefore log as much info as we can.
128 | if (error.target && error.target.tagName == goog.dom.TagName.SCRIPT) {
129 | tr.logError('UNKNOWN ERROR: Script ' + error.target.src);
130 | } else {
131 | tr.logError('UNKNOWN ERROR: No error information available.');
132 | }
133 | } else {
134 | tr.logError('JS ERROR: ' + error + '\nURL: ' + url + '\nLine: ' + line);
135 | }
136 | };
137 |
138 | // Create an onload handler, if the test runner hasn't been initialized then
139 | // no test has been registered with the test runner by the test file. We
140 | // then create a new test case and auto discover any tests in the global
141 | // scope. If this code is being parsed by JsTestC, we let it disable the
142 | // onload handler to avoid running the test in JsTestC.
143 | if (goog.testing.jsunit.AUTO_RUN_ONLOAD) {
144 | var onload = window.onload;
145 | window.onload = function(e) {
146 | // Call any existing onload handlers.
147 | if (onload) {
148 | onload(e);
149 | }
150 | // Wait so that we don't interfere with WebDriver.
151 | realTimeout(function() {
152 | if (!tr.initialized) {
153 | var testCase = new goog.testing.TestCase(document.title);
154 | goog.testing.TestCase.initializeTestRunner(testCase);
155 | }
156 | tr.execute();
157 | }, goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS);
158 | window.onload = null;
159 | };
160 | }
161 | }
162 | })();