aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/jsunit.js
blob: d02e747c2840bb90885b17c0accf6f9ec9debdc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Utilities for working with JsUnit.  Writes out the JsUnit file
 * that needs to be included in every unit test.
 *
 * Testing code should not have dependencies outside of goog.testing so as to
 * reduce the chance of masking missing dependencies.
 *
 */

goog.provide('goog.testing.jsunit');

goog.require('goog.testing.TestCase');
goog.require('goog.testing.TestRunner');


/**
 * Base path for JsUnit app files, relative to Closure's base path.
 * @type {string}
 */
goog.testing.jsunit.BASE_PATH =
    '../../third_party/java/jsunit/core/app/';


/**
 * Filename for the core JS Unit script.
 * @type {string}
 */
goog.testing.jsunit.CORE_SCRIPT =
    goog.testing.jsunit.BASE_PATH + 'jsUnitCore.js';


/**
 * @define {boolean} If this code is being parsed by JsTestC, we let it disable
 * the onload handler to avoid running the test in JsTestC.
 */
goog.testing.jsunit.AUTO_RUN_ONLOAD = true;


(function() {
  // Increases the maximum number of stack frames in Google Chrome from the
  // default 10 to 50 to get more useful stack traces.
  Error.stackTraceLimit = 50;

  // Store a reference to the window's timeout so that it can't be overridden
  // by tests.
  /** @type {!Function} */
  var realTimeout = window.setTimeout;

  // Check for JsUnit's test runner (need to check for >2.2 and <=2.2)
  if (top['JsUnitTestManager'] || top['jsUnitTestManager']) {
    // Running inside JsUnit so add support code.
    var path = goog.basePath + goog.testing.jsunit.CORE_SCRIPT;
    document.write('<script type="text/javascript" src="' +
                   path + '"></' + 'script>');

  } else {

    // Create a test runner.
    var tr = new goog.testing.TestRunner();

    // Export it so that it can be queried by Selenium and tests that use a
    // compiled test runner.
    goog.exportSymbol('G_testRunner', tr);
    goog.exportSymbol('G_testRunner.initialize', tr.initialize);
    goog.exportSymbol('G_testRunner.isInitialized', tr.isInitialized);
    goog.exportSymbol('G_testRunner.isFinished', tr.isFinished);
    goog.exportSymbol('G_testRunner.isSuccess', tr.isSuccess);
    goog.exportSymbol('G_testRunner.getReport', tr.getReport);
    goog.exportSymbol('G_testRunner.getRunTime', tr.getRunTime);
    goog.exportSymbol('G_testRunner.getNumFilesLoaded', tr.getNumFilesLoaded);
    goog.exportSymbol('G_testRunner.setStrict', tr.setStrict);
    goog.exportSymbol('G_testRunner.logTestFailure', tr.logTestFailure);

    // Export debug as a global function for JSUnit compatibility.  This just
    // calls log on the current test case.
    if (!goog.global['debug']) {
      goog.exportSymbol('debug', goog.bind(tr.log, tr));
    }

    // If the application has defined a global error filter, set it now.  This
    // allows users who use a base test include to set the error filter before
    // the testing code is loaded.
    if (goog.global['G_errorFilter']) {
      tr.setErrorFilter(goog.global['G_errorFilter']);
    }

    // Add an error handler to report errors that may occur during
    // initialization of the page.
    var onerror = window.onerror;
    window.onerror = function(error, url, line) {
      // Call any existing onerror handlers.
      if (onerror) {
        onerror(error, url, line);
      }
      if (typeof error == 'object') {
        // Webkit started passing an event object as the only argument to
        // window.onerror.  It doesn't contain an error message, url or line
        // number.  We therefore log as much info as we can.
        if (error.target && error.target.tagName == 'SCRIPT') {
          tr.logError('UNKNOWN ERROR: Script ' + error.target.src);
        } else {
          tr.logError('UNKNOWN ERROR: No error information available.');
        }
      } else {
        tr.logError('JS ERROR: ' + error + '\nURL: ' + url + '\nLine: ' + line);
      }
    };

    // Create an onload handler, if the test runner hasn't been initialized then
    // no test has been registered with the test runner by the test file.  We
    // then create a new test case and auto discover any tests in the global
    // scope. If this code is being parsed by JsTestC, we let it disable the
    // onload handler to avoid running the test in JsTestC.
    if (goog.testing.jsunit.AUTO_RUN_ONLOAD) {
      var onload = window.onload;
      window.onload = function(e) {
        // Call any existing onload handlers.
        if (onload) {
          onload(e);
        }
        // Wait 500ms longer so that we don't interfere with Selenium.
        realTimeout(function() {
          if (!tr.initialized) {
            var test = new goog.testing.TestCase(document.title);
            test.autoDiscoverTests();
            tr.initialize(test);
          }
          tr.execute();
        }, 500);
        window.onload = null;
      };
    }
  }
})();