| 1 |
// Copyright 2010 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 |
// All Rights Reserved. |
| 15 |
|
| 16 |
/** |
| 17 |
* @fileoverview A class representing a set of test functions that use |
| 18 |
* asynchronous functions that cannot be meaningfully mocked. |
| 19 |
* |
| 20 |
* To create a Google-compatible JsUnit test using this test case, put the |
| 21 |
* following snippet in your test: |
| 22 |
* |
| 23 |
* var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(); |
| 24 |
* |
| 25 |
* To make the test runner wait for your asynchronous behaviour, use: |
| 26 |
* |
| 27 |
* asyncTestCase.waitForAsync('Waiting for xhr to respond'); |
| 28 |
* |
| 29 |
* The next test will not start until the following call is made, or a |
| 30 |
* timeout occurs: |
| 31 |
* |
| 32 |
* asyncTestCase.continueTesting(); |
| 33 |
* |
| 34 |
* There does NOT need to be a 1:1 mapping of waitForAsync calls and |
| 35 |
* continueTesting calls. The next test will be run after a single call to |
| 36 |
* continueTesting is made, as long as there is no subsequent call to |
| 37 |
* waitForAsync in the same thread. |
| 38 |
* |
| 39 |
* Example: |
| 40 |
* // Returning here would cause the next test to be run. |
| 41 |
* asyncTestCase.waitForAsync('description 1'); |
| 42 |
* // Returning here would *not* cause the next test to be run. |
| 43 |
* // Only effect of additional waitForAsync() calls is an updated |
| 44 |
* // description in the case of a timeout. |
| 45 |
* asyncTestCase.waitForAsync('updated description'); |
| 46 |
* asyncTestCase.continueTesting(); |
| 47 |
* // Returning here would cause the next test to be run. |
| 48 |
* asyncTestCase.waitForAsync('just kidding, still running.'); |
| 49 |
* // Returning here would *not* cause the next test to be run. |
| 50 |
* |
| 51 |
* The test runner can also be made to wait for more than one asynchronous |
| 52 |
* event with: |
| 53 |
* |
| 54 |
* asyncTestCase.waitForSignals(n); |
| 55 |
* |
| 56 |
* The next test will not start until asyncTestCase.signal() is called n times, |
| 57 |
* or the test step timeout is exceeded. |
| 58 |
* |
| 59 |
* This class supports asynchronous behaviour in all test functions except for |
| 60 |
* tearDownPage. If such support is needed, it can be added. |
| 61 |
* |
| 62 |
* Example Usage: |
| 63 |
* |
| 64 |
* var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(); |
| 65 |
* // Optionally, set a longer-than-normal step timeout. |
| 66 |
* asyncTestCase.stepTimeout = 30 * 1000; |
| 67 |
* |
| 68 |
* function testSetTimeout() { |
| 69 |
* var step = 0; |
| 70 |
* function stepCallback() { |
| 71 |
* step++; |
| 72 |
* switch (step) { |
| 73 |
* case 1: |
| 74 |
* var startTime = goog.now(); |
| 75 |
* asyncTestCase.waitForAsync('step 1'); |
| 76 |
* window.setTimeout(stepCallback, 100); |
| 77 |
* break; |
| 78 |
* case 2: |
| 79 |
* assertTrue('Timeout fired too soon', |
| 80 |
* goog.now() - startTime >= 100); |
| 81 |
* asyncTestCase.waitForAsync('step 2'); |
| 82 |
* window.setTimeout(stepCallback, 100); |
| 83 |
* break; |
| 84 |
* case 3: |
| 85 |
* assertTrue('Timeout fired too soon', |
| 86 |
* goog.now() - startTime >= 200); |
| 87 |
* asyncTestCase.continueTesting(); |
| 88 |
* break; |
| 89 |
* default: |
| 90 |
* fail('Unexpected call to stepCallback'); |
| 91 |
* } |
| 92 |
* } |
| 93 |
* stepCallback(); |
| 94 |
* } |
| 95 |
* |
| 96 |
* Known Issues: |
| 97 |
* IE7 Exceptions: |
| 98 |
* As the failingtest.html will show, it appears as though ie7 does not |
| 99 |
* propagate an exception past a function called using the func.call() |
| 100 |
* syntax. This causes case 3 of the failing tests (exceptions) to show up |
| 101 |
* as timeouts in IE. |
| 102 |
* window.onerror: |
| 103 |
* This seems to catch errors only in ff2/ff3. It does not work in Safari or |
| 104 |
* IE7. The consequence of this is that exceptions that would have been |
| 105 |
* caught by window.onerror show up as timeouts. |
| 106 |
* |
| 107 |
* @author [email protected] (Andrew Grieve) |
| 108 |
*/ |
| 109 |
|
| 110 |
goog.setTestOnly('goog.testing.AsyncTestCase'); |
| 111 |
goog.provide('goog.testing.AsyncTestCase'); |
| 112 |
goog.provide('goog.testing.AsyncTestCase.ControlBreakingException'); |
| 113 |
|
| 114 |
goog.require('goog.testing.TestCase'); |
| 115 |
goog.require('goog.testing.asserts'); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* A test case that is capable of running tests that contain asynchronous logic. |
| 121 |
* @param {string=} opt_name A descriptive name for the test case. |
| 122 |
* @extends {goog.testing.TestCase} |
| 123 |
* @constructor |
| 124 |
* @deprecated Use goog.testing.TestCase instead. goog.testing.TestCase now |
| 125 |
* supports async testing using promises. |
| 126 |
*/ |
| 127 |
goog.testing.AsyncTestCase = function(opt_name) { |
| 128 |
goog.testing.TestCase.call(this, opt_name); |
| 129 |
}; |
| 130 |
goog.inherits(goog.testing.AsyncTestCase, goog.testing.TestCase); |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Represents result of top stack function call. |
| 135 |
* @typedef {{controlBreakingExceptionThrown: boolean, message: string}} |
| 136 |
* @private |
| 137 |
*/ |
| 138 |
goog.testing.AsyncTestCase.TopStackFuncResult_; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* An exception class used solely for control flow. |
| 144 |
* @param {string=} opt_message Error message. |
| 145 |
* @constructor |
| 146 |
* @extends {Error} |
| 147 |
* @final |
| 148 |
*/ |
| 149 |
goog.testing.AsyncTestCase.ControlBreakingException = function(opt_message) { |
| 150 |
goog.testing.AsyncTestCase.ControlBreakingException.base( |
| 151 |
this, 'constructor', opt_message); |
| 152 |
|
| 153 |
/** |
| 154 |
* The exception message. |
| 155 |
* @type {string} |
| 156 |
*/ |
| 157 |
this.message = opt_message || ''; |
| 158 |
}; |
| 159 |
goog.inherits(goog.testing.AsyncTestCase.ControlBreakingException, Error); |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Return value for .toString(). |
| 164 |
* @type {string} |
| 165 |
*/ |
| 166 |
goog.testing.AsyncTestCase.ControlBreakingException.TO_STRING = |
| 167 |
'[AsyncTestCase.ControlBreakingException]'; |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Marks this object as a ControlBreakingException |
| 172 |
* @type {boolean} |
| 173 |
*/ |
| 174 |
goog.testing.AsyncTestCase.ControlBreakingException.prototype |
| 175 |
.isControlBreakingException = true; |
| 176 |
|
| 177 |
|
| 178 |
/** @override */ |
| 179 |
goog.testing.AsyncTestCase.ControlBreakingException.prototype.toString = |
| 180 |
function() { |
| 181 |
// This shows up in the console when the exception is not caught. |
| 182 |
return goog.testing.AsyncTestCase.ControlBreakingException.TO_STRING; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* How long to wait for a single step of a test to complete in milliseconds. |
| 188 |
* A step starts when a call to waitForAsync() is made. |
| 189 |
* @type {number} |
| 190 |
*/ |
| 191 |
goog.testing.AsyncTestCase.prototype.stepTimeout = 1000; |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* How long to wait after a failed test before moving onto the next one. |
| 196 |
* The purpose of this is to allow any pending async callbacks from the failing |
| 197 |
* test to finish up and not cause the next test to fail. |
| 198 |
* @type {number} |
| 199 |
*/ |
| 200 |
goog.testing.AsyncTestCase.prototype.timeToSleepAfterFailure = 500; |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Turn on extra logging to help debug failing async. tests. |
| 205 |
* @type {boolean} |
| 206 |
* @private |
| 207 |
*/ |
| 208 |
goog.testing.AsyncTestCase.prototype.enableDebugLogs_ = false; |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* A reference to the original asserts.js assert_() function. |
| 213 |
* @private |
| 214 |
*/ |
| 215 |
goog.testing.AsyncTestCase.prototype.origAssert_; |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* A reference to the original asserts.js fail() function. |
| 220 |
* @private |
| 221 |
*/ |
| 222 |
goog.testing.AsyncTestCase.prototype.origFail_; |
| 223 |
|
| 224 |
|
| 225 |
/** |
| 226 |
* A reference to the original window.onerror function. |
| 227 |
* @type {Function|undefined} |
| 228 |
* @private |
| 229 |
*/ |
| 230 |
goog.testing.AsyncTestCase.prototype.origOnError_; |
| 231 |
|
| 232 |
|
| 233 |
/** |
| 234 |
* The stage of the test we are currently on. |
| 235 |
* @type {Function|undefined}} |
| 236 |
* @private |
| 237 |
*/ |
| 238 |
goog.testing.AsyncTestCase.prototype.curStepFunc_; |
| 239 |
|
| 240 |
|
| 241 |
/** |
| 242 |
* The name of the stage of the test we are currently on. |
| 243 |
* @type {string} |
| 244 |
* @private |
| 245 |
*/ |
| 246 |
goog.testing.AsyncTestCase.prototype.curStepName_ = ''; |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* The stage of the test we should run next. |
| 251 |
* @type {Function|undefined} |
| 252 |
* @private |
| 253 |
*/ |
| 254 |
goog.testing.AsyncTestCase.prototype.nextStepFunc_; |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* The name of the stage of the test we should run next. |
| 259 |
* @type {string} |
| 260 |
* @private |
| 261 |
*/ |
| 262 |
goog.testing.AsyncTestCase.prototype.nextStepName_ = ''; |
| 263 |
|
| 264 |
|
| 265 |
/** |
| 266 |
* The handle to the current setTimeout timer. |
| 267 |
* @type {number} |
| 268 |
* @private |
| 269 |
*/ |
| 270 |
goog.testing.AsyncTestCase.prototype.timeoutHandle_ = 0; |
| 271 |
|
| 272 |
|
| 273 |
/** |
| 274 |
* Marks if the cleanUp() function has been called for the currently running |
| 275 |
* test. |
| 276 |
* @type {boolean} |
| 277 |
* @private |
| 278 |
*/ |
| 279 |
goog.testing.AsyncTestCase.prototype.cleanedUp_ = false; |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* The currently active test. |
| 284 |
* @type {goog.testing.TestCase.Test|undefined} |
| 285 |
* @protected |
| 286 |
*/ |
| 287 |
goog.testing.AsyncTestCase.prototype.activeTest; |
| 288 |
|
| 289 |
|
| 290 |
/** |
| 291 |
* A flag to prevent recursive exception handling. |
| 292 |
* @type {boolean} |
| 293 |
* @private |
| 294 |
*/ |
| 295 |
goog.testing.AsyncTestCase.prototype.inException_ = false; |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* Flag used to determine if we can move to the next step in the testing loop. |
| 300 |
* @type {boolean} |
| 301 |
* @private |
| 302 |
*/ |
| 303 |
goog.testing.AsyncTestCase.prototype.isReady_ = true; |
| 304 |
|
| 305 |
|
| 306 |
/** |
| 307 |
* Number of signals to wait for before continuing testing when waitForSignals |
| 308 |
* is used. |
| 309 |
* @type {number} |
| 310 |
* @private |
| 311 |
*/ |
| 312 |
goog.testing.AsyncTestCase.prototype.expectedSignalCount_ = 0; |
| 313 |
|
| 314 |
|
| 315 |
/** |
| 316 |
* Number of signals received. |
| 317 |
* @type {number} |
| 318 |
* @private |
| 319 |
*/ |
| 320 |
goog.testing.AsyncTestCase.prototype.receivedSignalCount_ = 0; |
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* Flag that tells us if there is a function in the call stack that will make |
| 325 |
* a call to pump_(). |
| 326 |
* @type {boolean} |
| 327 |
* @private |
| 328 |
*/ |
| 329 |
goog.testing.AsyncTestCase.prototype.returnWillPump_ = false; |
| 330 |
|
| 331 |
|
| 332 |
/** |
| 333 |
* The number of times we have thrown a ControlBreakingException so that we |
| 334 |
* know not to complain in our window.onerror handler. In Webkit, window.onerror |
| 335 |
* is not supported, and so this counter will keep going up but we won't care |
| 336 |
* about it. |
| 337 |
* @type {number} |
| 338 |
* @private |
| 339 |
*/ |
| 340 |
goog.testing.AsyncTestCase.prototype.numControlExceptionsExpected_ = 0; |
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* The current step name. |
| 345 |
* @return {string} Step name. |
| 346 |
* @protected |
| 347 |
*/ |
| 348 |
goog.testing.AsyncTestCase.prototype.getCurrentStepName = function() { |
| 349 |
return this.curStepName_; |
| 350 |
}; |
| 351 |
|
| 352 |
|
| 353 |
/** |
| 354 |
* Preferred way of creating an AsyncTestCase. Creates one and initializes it |
| 355 |
* with the G_testRunner. |
| 356 |
* @param {string=} opt_name A descriptive name for the test case. |
| 357 |
* @return {!goog.testing.AsyncTestCase} The created AsyncTestCase. |
| 358 |
*/ |
| 359 |
goog.testing.AsyncTestCase.createAndInstall = function(opt_name) { |
| 360 |
var asyncTestCase = new goog.testing.AsyncTestCase(opt_name); |
| 361 |
goog.testing.TestCase.initializeTestRunner(asyncTestCase); |
| 362 |
return asyncTestCase; |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
/** |
| 367 |
* Informs the testcase not to continue to the next step in the test cycle |
| 368 |
* until continueTesting is called. |
| 369 |
* @param {string=} opt_name A description of what we are waiting for. |
| 370 |
*/ |
| 371 |
goog.testing.AsyncTestCase.prototype.waitForAsync = function(opt_name) { |
| 372 |
this.isReady_ = false; |
| 373 |
this.curStepName_ = opt_name || this.curStepName_; |
| 374 |
|
| 375 |
// Reset the timer that tracks if the async test takes too long. |
| 376 |
this.stopTimeoutTimer_(); |
| 377 |
this.startTimeoutTimer_(); |
| 378 |
}; |
| 379 |
|
| 380 |
|
| 381 |
/** |
| 382 |
* Continue with the next step in the test cycle. |
| 383 |
*/ |
| 384 |
goog.testing.AsyncTestCase.prototype.continueTesting = function() { |
| 385 |
if (this.receivedSignalCount_ < this.expectedSignalCount_) { |
| 386 |
var remaining = this.expectedSignalCount_ - this.receivedSignalCount_; |
| 387 |
throw new Error('Still waiting for ' + remaining + ' signals.'); |
| 388 |
} |
| 389 |
this.endCurrentStep_(); |
| 390 |
}; |
| 391 |
|
| 392 |
|
| 393 |
/** |
| 394 |
* Ends the current test step and queues the next test step to run. |
| 395 |
* @private |
| 396 |
*/ |
| 397 |
goog.testing.AsyncTestCase.prototype.endCurrentStep_ = function() { |
| 398 |
if (!this.isReady_) { |
| 399 |
// We are a potential entry point, so we pump. |
| 400 |
this.isReady_ = true; |
| 401 |
this.stopTimeoutTimer_(); |
| 402 |
// Run this in a setTimeout so that the caller has a chance to call |
| 403 |
// waitForAsync() again before we continue. |
| 404 |
this.timeout(goog.bind(this.pump_, this, null), 0); |
| 405 |
} |
| 406 |
}; |
| 407 |
|
| 408 |
|
| 409 |
/** |
| 410 |
* Informs the testcase not to continue to the next step in the test cycle |
| 411 |
* until signal is called the specified number of times. Within a test, this |
| 412 |
* function behaves additively if called multiple times; the number of signals |
| 413 |
* to wait for will be the sum of all expected number of signals this function |
| 414 |
* was called with. |
| 415 |
* @param {number} times The number of signals to receive before |
| 416 |
* continuing testing. |
| 417 |
* @param {string=} opt_name A description of what we are waiting for. |
| 418 |
*/ |
| 419 |
goog.testing.AsyncTestCase.prototype.waitForSignals = function( |
| 420 |
times, opt_name) { |
| 421 |
this.expectedSignalCount_ += times; |
| 422 |
if (this.receivedSignalCount_ < this.expectedSignalCount_) { |
| 423 |
this.waitForAsync(opt_name); |
| 424 |
} |
| 425 |
}; |
| 426 |
|
| 427 |
|
| 428 |
/** |
| 429 |
* Signals once to continue with the test. If this is the last signal that the |
| 430 |
* test was waiting on, call continueTesting. |
| 431 |
*/ |
| 432 |
goog.testing.AsyncTestCase.prototype.signal = function() { |
| 433 |
if (++this.receivedSignalCount_ === this.expectedSignalCount_ && |
| 434 |
this.expectedSignalCount_ > 0) { |
| 435 |
this.endCurrentStep_(); |
| 436 |
} |
| 437 |
}; |
| 438 |
|
| 439 |
|
| 440 |
/** |
| 441 |
* Handles an exception thrown by a test. |
| 442 |
* @param {*=} opt_e The exception object associated with the failure |
| 443 |
* or a string. |
| 444 |
* @throws Always throws a ControlBreakingException. |
| 445 |
*/ |
| 446 |
goog.testing.AsyncTestCase.prototype.doAsyncError = function(opt_e) { |
| 447 |
// If we've caught an exception that we threw, then just pass it along. This |
| 448 |
// can happen if doAsyncError() was called from a call to assert and then |
| 449 |
// again by pump_(). |
| 450 |
if (opt_e && opt_e.isControlBreakingException) { |
| 451 |
throw opt_e; |
| 452 |
} |
| 453 |
|
| 454 |
// Prevent another timeout error from triggering for this test step. |
| 455 |
this.stopTimeoutTimer_(); |
| 456 |
|
| 457 |
// doError() uses test.name. Here, we create a dummy test and give it a more |
| 458 |
// helpful name based on the step we're currently on. |
| 459 |
var fakeTestObj = |
| 460 |
new goog.testing.TestCase.Test(this.curStepName_, goog.nullFunction); |
| 461 |
if (this.activeTest) { |
| 462 |
fakeTestObj.name = this.activeTest.name + ' [' + fakeTestObj.name + ']'; |
| 463 |
} |
| 464 |
|
| 465 |
if (this.activeTest) { |
| 466 |
// Note: if the test has an error, and then tearDown has an error, they will |
| 467 |
// both be reported. |
| 468 |
this.doError(fakeTestObj, opt_e); |
| 469 |
} else { |
| 470 |
this.exceptionBeforeTest = opt_e; |
| 471 |
} |
| 472 |
|
| 473 |
// This is a potential entry point, so we pump. We also add in a bit of a |
| 474 |
// delay to try and prevent any async behavior from the failed test from |
| 475 |
// causing the next test to fail. |
| 476 |
this.timeout( |
| 477 |
goog.bind(this.pump_, this, this.doAsyncErrorTearDown_), |
| 478 |
this.timeToSleepAfterFailure); |
| 479 |
|
| 480 |
// We just caught an exception, so we do not want the code above us on the |
| 481 |
// stack to continue executing. If pump_ is in our call-stack, then it will |
| 482 |
// batch together multiple errors, so we only increment the count if pump_ is |
| 483 |
// not in the stack and let pump_ increment the count when it batches them. |
| 484 |
if (!this.returnWillPump_) { |
| 485 |
this.numControlExceptionsExpected_ += 1; |
| 486 |
this.dbgLog_( |
| 487 |
'doAsynError: numControlExceptionsExpected_ = ' + |
| 488 |
this.numControlExceptionsExpected_ + ' and throwing exception.'); |
| 489 |
} |
| 490 |
|
| 491 |
// Copy the error message to ControlBreakingException. |
| 492 |
var message = ''; |
| 493 |
if (typeof opt_e == 'string') { |
| 494 |
message = opt_e; |
| 495 |
} else if (opt_e && opt_e.message) { |
| 496 |
message = opt_e.message; |
| 497 |
} |
| 498 |
throw new goog.testing.AsyncTestCase.ControlBreakingException(message); |
| 499 |
}; |
| 500 |
|
| 501 |
|
| 502 |
/** |
| 503 |
* Sets up the test page and then waits until the test case has been marked |
| 504 |
* as ready before executing the tests. |
| 505 |
* @override |
| 506 |
*/ |
| 507 |
goog.testing.AsyncTestCase.prototype.runTests = function() { |
| 508 |
this.hookAssert_(); |
| 509 |
this.hookOnError_(); |
| 510 |
|
| 511 |
goog.testing.TestCase.currentTestName = null; |
| 512 |
this.setNextStep_(this.doSetUpPage_, 'setUpPage'); |
| 513 |
// We are an entry point, so we pump. |
| 514 |
this.pump_(); |
| 515 |
}; |
| 516 |
|
| 517 |
|
| 518 |
/** |
| 519 |
* Starts the tests. |
| 520 |
* @override |
| 521 |
*/ |
| 522 |
goog.testing.AsyncTestCase.prototype.cycleTests = function() { |
| 523 |
// We are an entry point, so we pump. |
| 524 |
this.saveMessage('Start'); |
| 525 |
this.setNextStep_(this.doIteration_, 'doIteration'); |
| 526 |
this.pump_(); |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
/** |
| 531 |
* Finalizes the test case, called when the tests have finished executing. |
| 532 |
* @override |
| 533 |
*/ |
| 534 |
goog.testing.AsyncTestCase.prototype.finalize = function() { |
| 535 |
this.unhookAll_(); |
| 536 |
this.setNextStep_(null, 'finalized'); |
| 537 |
goog.testing.AsyncTestCase.superClass_.finalize.call(this); |
| 538 |
}; |
| 539 |
|
| 540 |
|
| 541 |
/** |
| 542 |
* Enables verbose logging of what is happening inside of the AsyncTestCase. |
| 543 |
*/ |
| 544 |
goog.testing.AsyncTestCase.prototype.enableDebugLogging = function() { |
| 545 |
this.enableDebugLogs_ = true; |
| 546 |
}; |
| 547 |
|
| 548 |
|
| 549 |
/** |
| 550 |
* Logs the given debug message to the console (when enabled). |
| 551 |
* @param {string} message The message to log. |
| 552 |
* @private |
| 553 |
*/ |
| 554 |
goog.testing.AsyncTestCase.prototype.dbgLog_ = function(message) { |
| 555 |
if (this.enableDebugLogs_) { |
| 556 |
this.log('AsyncTestCase - ' + message); |
| 557 |
} |
| 558 |
}; |
| 559 |
|
| 560 |
|
| 561 |
/** |
| 562 |
* Wraps doAsyncError() for when we are sure that the test runner has no user |
| 563 |
* code above it in the stack. |
| 564 |
* @param {string|Error=} opt_e The exception object associated with the |
| 565 |
* failure or a string. |
| 566 |
* @private |
| 567 |
*/ |
| 568 |
goog.testing.AsyncTestCase.prototype.doTopOfStackAsyncError_ = function(opt_e) { |
| 569 |
|
| 570 |
try { |
| 571 |
this.doAsyncError(opt_e); |
| 572 |
} catch (e) { |
| 573 |
// We know that we are on the top of the stack, so there is no need to |
| 574 |
// throw this exception in this case. |
| 575 |
if (e.isControlBreakingException) { |
| 576 |
this.numControlExceptionsExpected_ -= 1; |
| 577 |
this.dbgLog_( |
| 578 |
'doTopOfStackAsyncError_: numControlExceptionsExpected_ = ' + |
| 579 |
this.numControlExceptionsExpected_ + ' and catching exception.'); |
| 580 |
} else { |
| 581 |
throw e; |
| 582 |
} |
| 583 |
} |
| 584 |
}; |
| 585 |
|
| 586 |
|
| 587 |
/** |
| 588 |
* Calls the tearDown function, catching any errors, and then moves on to |
| 589 |
* the next step in the testing cycle. |
| 590 |
* @private |
| 591 |
*/ |
| 592 |
goog.testing.AsyncTestCase.prototype.doAsyncErrorTearDown_ = function() { |
| 593 |
if (this.inException_) { |
| 594 |
// We get here if tearDown is throwing the error. |
| 595 |
// Upon calling continueTesting, the inline function 'doAsyncError' (set |
| 596 |
// below) is run. |
| 597 |
this.endCurrentStep_(); |
| 598 |
} else { |
| 599 |
this.inException_ = true; |
| 600 |
this.isReady_ = true; |
| 601 |
|
| 602 |
// The continue point is different depending on if the error happened in |
| 603 |
// setUpPage() or in setUp()/test*()/tearDown(). |
| 604 |
var stepFuncAfterError = this.nextStepFunc_; |
| 605 |
var stepNameAfterError = 'TestCase.execute (after error)'; |
| 606 |
if (this.activeTest) { |
| 607 |
stepFuncAfterError = this.doIteration_; |
| 608 |
stepNameAfterError = 'doIteration (after error)'; |
| 609 |
} |
| 610 |
|
| 611 |
// We must set the next step before calling tearDown. |
| 612 |
this.setNextStep_(function() { |
| 613 |
this.inException_ = false; |
| 614 |
// This is null when an error happens in setUpPage. |
| 615 |
this.setNextStep_(stepFuncAfterError, stepNameAfterError); |
| 616 |
}, 'doAsyncError'); |
| 617 |
|
| 618 |
// Call the test's tearDown(). |
| 619 |
if (!this.cleanedUp_) { |
| 620 |
this.cleanedUp_ = true; |
| 621 |
this.tearDown(); |
| 622 |
} |
| 623 |
} |
| 624 |
}; |
| 625 |
|
| 626 |
|
| 627 |
/** |
| 628 |
* Replaces the asserts.js assert_() and fail() functions with a wrappers to |
| 629 |
* catch the exceptions. |
| 630 |
* @private |
| 631 |
*/ |
| 632 |
goog.testing.AsyncTestCase.prototype.hookAssert_ = function() { |
| 633 |
if (!this.origAssert_) { |
| 634 |
this.origAssert_ = _assert; |
| 635 |
this.origFail_ = fail; |
| 636 |
var self = this; |
| 637 |
_assert = function() { |
| 638 |
|
| 639 |
try { |
| 640 |
self.origAssert_.apply(this, arguments); |
| 641 |
} catch (e) { |
| 642 |
self.dbgLog_('Wrapping failed assert()'); |
| 643 |
self.doAsyncError(e); |
| 644 |
} |
| 645 |
}; |
| 646 |
fail = function() { |
| 647 |
|
| 648 |
try { |
| 649 |
self.origFail_.apply(this, arguments); |
| 650 |
} catch (e) { |
| 651 |
self.dbgLog_('Wrapping fail()'); |
| 652 |
self.doAsyncError(e); |
| 653 |
} |
| 654 |
}; |
| 655 |
} |
| 656 |
}; |
| 657 |
|
| 658 |
|
| 659 |
/** |
| 660 |
* Sets a window.onerror handler for catching exceptions that happen in async |
| 661 |
* callbacks. Note that as of Safari 3.1, Safari does not support this. |
| 662 |
* @private |
| 663 |
*/ |
| 664 |
goog.testing.AsyncTestCase.prototype.hookOnError_ = function() { |
| 665 |
if (!this.origOnError_) { |
| 666 |
this.origOnError_ = window.onerror; |
| 667 |
var self = this; |
| 668 |
window.onerror = function(error, url, line) { |
| 669 |
// Ignore exceptions that we threw on purpose. |
| 670 |
var cbe = goog.testing.AsyncTestCase.ControlBreakingException.TO_STRING; |
| 671 |
if (String(error).indexOf(cbe) != -1 && |
| 672 |
self.numControlExceptionsExpected_) { |
| 673 |
self.numControlExceptionsExpected_ -= 1; |
| 674 |
self.dbgLog_( |
| 675 |
'window.onerror: numControlExceptionsExpected_ = ' + |
| 676 |
self.numControlExceptionsExpected_ + ' and ignoring exception. ' + |
| 677 |
error); |
| 678 |
// Tell the browser not to compain about the error. |
| 679 |
return true; |
| 680 |
} else { |
| 681 |
self.dbgLog_('window.onerror caught exception.'); |
| 682 |
var message = error + '\nURL: ' + url + '\nLine: ' + line; |
| 683 |
self.doTopOfStackAsyncError_(message); |
| 684 |
// Tell the browser to complain about the error. |
| 685 |
return false; |
| 686 |
} |
| 687 |
}; |
| 688 |
} |
| 689 |
}; |
| 690 |
|
| 691 |
|
| 692 |
/** |
| 693 |
* Unhooks window.onerror and _assert. |
| 694 |
* @private |
| 695 |
*/ |
| 696 |
goog.testing.AsyncTestCase.prototype.unhookAll_ = function() { |
| 697 |
if (this.origOnError_) { |
| 698 |
window.onerror = this.origOnError_; |
| 699 |
this.origOnError_ = null; |
| 700 |
_assert = this.origAssert_; |
| 701 |
this.origAssert_ = null; |
| 702 |
fail = this.origFail_; |
| 703 |
this.origFail_ = null; |
| 704 |
} |
| 705 |
}; |
| 706 |
|
| 707 |
|
| 708 |
/** |
| 709 |
* Enables the timeout timer. This timer fires unless continueTesting is |
| 710 |
* called. |
| 711 |
* @private |
| 712 |
*/ |
| 713 |
goog.testing.AsyncTestCase.prototype.startTimeoutTimer_ = function() { |
| 714 |
if (!this.timeoutHandle_ && this.stepTimeout > 0) { |
| 715 |
this.timeoutHandle_ = this.timeout(goog.bind(function() { |
| 716 |
this.dbgLog_('Timeout timer fired with id ' + this.timeoutHandle_); |
| 717 |
this.timeoutHandle_ = 0; |
| 718 |
|
| 719 |
this.doTopOfStackAsyncError_( |
| 720 |
'Timed out while waiting for ' + |
| 721 |
'continueTesting() to be called.'); |
| 722 |
}, this, null), this.stepTimeout); |
| 723 |
this.dbgLog_('Started timeout timer with id ' + this.timeoutHandle_); |
| 724 |
} |
| 725 |
}; |
| 726 |
|
| 727 |
|
| 728 |
/** |
| 729 |
* Disables the timeout timer. |
| 730 |
* @private |
| 731 |
*/ |
| 732 |
goog.testing.AsyncTestCase.prototype.stopTimeoutTimer_ = function() { |
| 733 |
if (this.timeoutHandle_) { |
| 734 |
this.dbgLog_('Clearing timeout timer with id ' + this.timeoutHandle_); |
| 735 |
this.clearTimeout(this.timeoutHandle_); |
| 736 |
this.timeoutHandle_ = 0; |
| 737 |
} |
| 738 |
}; |
| 739 |
|
| 740 |
|
| 741 |
/** |
| 742 |
* Sets the next function to call in our sequence of async callbacks. |
| 743 |
* @param {Function} func The function that executes the next step. |
| 744 |
* @param {string} name A description of the next step. |
| 745 |
* @private |
| 746 |
*/ |
| 747 |
goog.testing.AsyncTestCase.prototype.setNextStep_ = function(func, name) { |
| 748 |
this.nextStepFunc_ = func && goog.bind(func, this); |
| 749 |
this.nextStepName_ = name; |
| 750 |
}; |
| 751 |
|
| 752 |
|
| 753 |
/** |
| 754 |
* Calls the given function, redirecting any exceptions to doAsyncError. |
| 755 |
* @param {Function} func The function to call. |
| 756 |
* @return {!goog.testing.AsyncTestCase.TopStackFuncResult_} Returns a |
| 757 |
* TopStackFuncResult_. |
| 758 |
* @private |
| 759 |
*/ |
| 760 |
goog.testing.AsyncTestCase.prototype.callTopOfStackFunc_ = function(func) { |
| 761 |
|
| 762 |
try { |
| 763 |
func.call(this); |
| 764 |
return {controlBreakingExceptionThrown: false, message: ''}; |
| 765 |
} catch (e) { |
| 766 |
this.dbgLog_('Caught exception in callTopOfStackFunc_'); |
| 767 |
|
| 768 |
try { |
| 769 |
this.doAsyncError(e); |
| 770 |
return {controlBreakingExceptionThrown: false, message: ''}; |
| 771 |
} catch (e2) { |
| 772 |
if (!e2.isControlBreakingException) { |
| 773 |
throw e2; |
| 774 |
} |
| 775 |
return {controlBreakingExceptionThrown: true, message: e2.message}; |
| 776 |
} |
| 777 |
} |
| 778 |
}; |
| 779 |
|
| 780 |
|
| 781 |
/** |
| 782 |
* Calls the next callback when the isReady_ flag is true. |
| 783 |
* @param {Function=} opt_doFirst A function to call before pumping. |
| 784 |
* @private |
| 785 |
* @throws Throws a ControlBreakingException if there were any failing steps. |
| 786 |
*/ |
| 787 |
goog.testing.AsyncTestCase.prototype.pump_ = function(opt_doFirst) { |
| 788 |
// If this function is already above us in the call-stack, then we should |
| 789 |
// return rather than pumping in order to minimize call-stack depth. |
| 790 |
if (!this.returnWillPump_) { |
| 791 |
this.setBatchTime(this.now()); |
| 792 |
this.returnWillPump_ = true; |
| 793 |
var topFuncResult = {}; |
| 794 |
|
| 795 |
if (opt_doFirst) { |
| 796 |
topFuncResult = this.callTopOfStackFunc_(opt_doFirst); |
| 797 |
} |
| 798 |
// Note: we don't check for this.running here because it is not set to true |
| 799 |
// while executing setUpPage and tearDownPage. |
| 800 |
// Also, if isReady_ is false, then one of two things will happen: |
| 801 |
// 1. Our timeout callback will be called. |
| 802 |
// 2. The tests will call continueTesting(), which will call pump_() again. |
| 803 |
while (this.isReady_ && this.nextStepFunc_ && |
| 804 |
!topFuncResult.controlBreakingExceptionThrown) { |
| 805 |
this.curStepFunc_ = this.nextStepFunc_; |
| 806 |
this.curStepName_ = this.nextStepName_; |
| 807 |
this.nextStepFunc_ = null; |
| 808 |
this.nextStepName_ = ''; |
| 809 |
|
| 810 |
this.dbgLog_('Performing step: ' + this.curStepName_); |
| 811 |
topFuncResult = |
| 812 |
this.callTopOfStackFunc_(/** @type {Function} */ (this.curStepFunc_)); |
| 813 |
|
| 814 |
// If the max run time is exceeded call this function again async so as |
| 815 |
// not to block the browser. |
| 816 |
var delta = this.now() - this.getBatchTime(); |
| 817 |
if (delta > goog.testing.TestCase.maxRunTime && |
| 818 |
!topFuncResult.controlBreakingExceptionThrown) { |
| 819 |
this.saveMessage('Breaking async'); |
| 820 |
var self = this; |
| 821 |
this.timeout(function() { self.pump_(); }, 100); |
| 822 |
break; |
| 823 |
} |
| 824 |
} |
| 825 |
this.returnWillPump_ = false; |
| 826 |
} else if (opt_doFirst) { |
| 827 |
opt_doFirst.call(this); |
| 828 |
} |
| 829 |
}; |
| 830 |
|
| 831 |
|
| 832 |
/** |
| 833 |
* Sets up the test page and then waits until the test case has been marked |
| 834 |
* as ready before executing the tests. |
| 835 |
* @private |
| 836 |
*/ |
| 837 |
goog.testing.AsyncTestCase.prototype.doSetUpPage_ = function() { |
| 838 |
this.setNextStep_(this.execute, 'TestCase.execute'); |
| 839 |
this.setUpPage(); |
| 840 |
}; |
| 841 |
|
| 842 |
|
| 843 |
/** |
| 844 |
* Step 1: Move to the next test. |
| 845 |
* @private |
| 846 |
*/ |
| 847 |
goog.testing.AsyncTestCase.prototype.doIteration_ = function() { |
| 848 |
this.expectedSignalCount_ = 0; |
| 849 |
this.receivedSignalCount_ = 0; |
| 850 |
this.activeTest = this.next(); |
| 851 |
goog.testing.TestCase.currentTestName = |
| 852 |
this.activeTest ? this.activeTest.name : null; |
| 853 |
if (this.activeTest && this.running) { |
| 854 |
this.result_.runCount++; |
| 855 |
// If this test should be marked as having failed, doIteration will go |
| 856 |
// straight to the next test. |
| 857 |
if (this.maybeFailTestEarly(this.activeTest)) { |
| 858 |
this.setNextStep_(this.doIteration_, 'doIteration'); |
| 859 |
} else { |
| 860 |
this.setNextStep_(this.doSetUp_, 'setUp'); |
| 861 |
} |
| 862 |
} else { |
| 863 |
// All tests done. |
| 864 |
this.finalize(); |
| 865 |
} |
| 866 |
}; |
| 867 |
|
| 868 |
|
| 869 |
/** |
| 870 |
* Step 2: Call setUp(). |
| 871 |
* @private |
| 872 |
*/ |
| 873 |
goog.testing.AsyncTestCase.prototype.doSetUp_ = function() { |
| 874 |
this.log('Running test: ' + this.activeTest.name); |
| 875 |
this.cleanedUp_ = false; |
| 876 |
this.setNextStep_(this.doExecute_, this.activeTest.name); |
| 877 |
this.setUp(); |
| 878 |
}; |
| 879 |
|
| 880 |
|
| 881 |
/** |
| 882 |
* Step 3: Call test.execute(). |
| 883 |
* @private |
| 884 |
*/ |
| 885 |
goog.testing.AsyncTestCase.prototype.doExecute_ = function() { |
| 886 |
this.setNextStep_(this.doTearDown_, 'tearDown'); |
| 887 |
this.activeTest.execute(); |
| 888 |
}; |
| 889 |
|
| 890 |
|
| 891 |
/** |
| 892 |
* Step 4: Call tearDown(). |
| 893 |
* @private |
| 894 |
*/ |
| 895 |
goog.testing.AsyncTestCase.prototype.doTearDown_ = function() { |
| 896 |
this.cleanedUp_ = true; |
| 897 |
this.setNextStep_(this.doNext_, 'doNext'); |
| 898 |
this.tearDown(); |
| 899 |
}; |
| 900 |
|
| 901 |
|
| 902 |
/** |
| 903 |
* Step 5: Call doSuccess() |
| 904 |
* @private |
| 905 |
*/ |
| 906 |
goog.testing.AsyncTestCase.prototype.doNext_ = function() { |
| 907 |
this.setNextStep_(this.doIteration_, 'doIteration'); |
| 908 |
this.doSuccess(/** @type {goog.testing.TestCase.Test} */ (this.activeTest)); |
| 909 |
}; |