| 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 |
|
| 15 |
/** |
| 16 |
* @fileoverview Defines DeferredTestCase class. By calling waitForDeferred(), |
| 17 |
* tests in DeferredTestCase can wait for a Deferred object to complete its |
| 18 |
* callbacks before continuing to the next test. |
| 19 |
* |
| 20 |
* Example Usage: |
| 21 |
* |
| 22 |
* var deferredTestCase = goog.testing.DeferredTestCase.createAndInstall(); |
| 23 |
* // Optionally, set a longer-than-usual step timeout. |
| 24 |
* deferredTestCase.stepTimeout = 15 * 1000; // 15 seconds |
| 25 |
* |
| 26 |
* function testDeferredCallbacks() { |
| 27 |
* var callbackTime = goog.now(); |
| 28 |
* var callbacks = new goog.async.Deferred(); |
| 29 |
* deferredTestCase.addWaitForAsync('Waiting for 1st callback', callbacks); |
| 30 |
* callbacks.addCallback( |
| 31 |
* function() { |
| 32 |
* assertTrue( |
| 33 |
* 'We\'re going back in time!', goog.now() >= callbackTime); |
| 34 |
* callbackTime = goog.now(); |
| 35 |
* }); |
| 36 |
* deferredTestCase.addWaitForAsync('Waiting for 2nd callback', callbacks); |
| 37 |
* callbacks.addCallback( |
| 38 |
* function() { |
| 39 |
* assertTrue( |
| 40 |
* 'We\'re going back in time!', goog.now() >= callbackTime); |
| 41 |
* callbackTime = goog.now(); |
| 42 |
* }); |
| 43 |
* deferredTestCase.addWaitForAsync('Waiting for last callback', callbacks); |
| 44 |
* callbacks.addCallback( |
| 45 |
* function() { |
| 46 |
* assertTrue( |
| 47 |
* 'We\'re going back in time!', goog.now() >= callbackTime); |
| 48 |
* callbackTime = goog.now(); |
| 49 |
* }); |
| 50 |
* |
| 51 |
* deferredTestCase.waitForDeferred(callbacks); |
| 52 |
* } |
| 53 |
* |
| 54 |
* Note that DeferredTestCase still preserves the functionality of |
| 55 |
* AsyncTestCase. |
| 56 |
* |
| 57 |
* @see.goog.async.Deferred |
| 58 |
* @see goog.testing.AsyncTestCase |
| 59 |
*/ |
| 60 |
|
| 61 |
goog.setTestOnly('goog.testing.DeferredTestCase'); |
| 62 |
goog.provide('goog.testing.DeferredTestCase'); |
| 63 |
|
| 64 |
goog.require('goog.testing.AsyncTestCase'); |
| 65 |
goog.require('goog.testing.TestCase'); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* A test case that can asynchronously wait on a Deferred object. |
| 71 |
* @param {string=} opt_name A descriptive name for the test case. |
| 72 |
* @constructor |
| 73 |
* @extends {goog.testing.AsyncTestCase} |
| 74 |
* @deprecated Use goog.testing.TestCase instead. goog.testing.TestCase now |
| 75 |
* supports async testing using promises. |
| 76 |
*/ |
| 77 |
goog.testing.DeferredTestCase = function(opt_name) { |
| 78 |
goog.testing.AsyncTestCase.call(this, opt_name); |
| 79 |
}; |
| 80 |
goog.inherits(goog.testing.DeferredTestCase, goog.testing.AsyncTestCase); |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Preferred way of creating a DeferredTestCase. Creates one and initializes it |
| 85 |
* with the G_testRunner. |
| 86 |
* @param {string=} opt_name A descriptive name for the test case. |
| 87 |
* @return {!goog.testing.DeferredTestCase} The created DeferredTestCase. |
| 88 |
*/ |
| 89 |
goog.testing.DeferredTestCase.createAndInstall = function(opt_name) { |
| 90 |
var deferredTestCase = new goog.testing.DeferredTestCase(opt_name); |
| 91 |
goog.testing.TestCase.initializeTestRunner(deferredTestCase); |
| 92 |
return deferredTestCase; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Handler for when the test produces an error. |
| 98 |
* @param {Error|string} err The error object. |
| 99 |
* @protected |
| 100 |
* @throws Always throws a ControlBreakingException. |
| 101 |
*/ |
| 102 |
goog.testing.DeferredTestCase.prototype.onError = function(err) { |
| 103 |
this.doAsyncError(err); |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Handler for when the test succeeds. |
| 109 |
* @protected |
| 110 |
*/ |
| 111 |
goog.testing.DeferredTestCase.prototype.onSuccess = function() { |
| 112 |
this.continueTesting(); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Adds a callback to update the wait message of this async test case. Using |
| 118 |
* this method generously also helps to document the test flow. |
| 119 |
* @param {string} msg The update wait status message. |
| 120 |
* @param {goog.async.Deferred} d The deferred object to add the waitForAsync |
| 121 |
* callback to. |
| 122 |
* @see goog.testing.AsyncTestCase#waitForAsync |
| 123 |
*/ |
| 124 |
goog.testing.DeferredTestCase.prototype.addWaitForAsync = function(msg, d) { |
| 125 |
d.addCallback(goog.bind(this.waitForAsync, this, msg)); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Wires up given Deferred object to the test case, then starts the |
| 131 |
* goog.async.Deferred object's callback. |
| 132 |
* @param {string|!goog.async.Deferred} a The wait status message or the |
| 133 |
* deferred object to wait for. |
| 134 |
* @param {goog.async.Deferred=} opt_b The deferred object to wait for. |
| 135 |
*/ |
| 136 |
goog.testing.DeferredTestCase.prototype.waitForDeferred = function(a, opt_b) { |
| 137 |
var waitMsg; |
| 138 |
var deferred; |
| 139 |
switch (arguments.length) { |
| 140 |
case 1: |
| 141 |
deferred = a; |
| 142 |
waitMsg = null; |
| 143 |
break; |
| 144 |
case 2: |
| 145 |
deferred = opt_b; |
| 146 |
waitMsg = a; |
| 147 |
break; |
| 148 |
default: // Shouldn't be here in compiled mode |
| 149 |
throw new Error('Invalid number of arguments'); |
| 150 |
} |
| 151 |
deferred.addCallbacks(this.onSuccess, this.onError, this); |
| 152 |
if (!waitMsg) { |
| 153 |
waitMsg = 'Waiting for deferred in ' + this.getCurrentStepName(); |
| 154 |
} |
| 155 |
this.waitForAsync(/** @type {!string} */ (waitMsg)); |
| 156 |
deferred.callback(true); |
| 157 |
}; |