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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
// Copyright 2014 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.
goog.provide('goog.labs.testing.Environment');
goog.require('goog.Thenable');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.debug.Console');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.MockControl');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.TestCase');
goog.require('goog.testing.jsunit');
/**
* JsUnit environments allow developers to customize the existing testing
* lifecycle by hitching additional setUp and tearDown behaviors to tests.
*
* Environments will run their setUp steps in the order in which they
* are instantiated and registered. During tearDown, the environments will
* unwind the setUp and execute in reverse order.
*
* See http://go/jsunit-env for more information.
*/
goog.labs.testing.Environment = goog.defineClass(null, {
/** @constructor */
constructor: function() {
var testcase = goog.labs.testing.EnvironmentTestCase_.getInstance();
testcase.registerEnvironment_(this);
// Record the active test case, in normal usage this is a singleton,
// but while testing this case it is reset.
goog.labs.testing.Environment.activeTestCase_ = testcase;
/** @type {goog.testing.MockControl} */
this.mockControl = null;
/** @type {goog.testing.MockClock} */
this.mockClock = null;
/** @private {boolean} */
this.shouldMakeMockControl_ = false;
/** @private {boolean} */
this.shouldMakeMockClock_ = false;
/** @const {!goog.debug.Console} */
this.console = goog.labs.testing.Environment.console_;
/** @const {!goog.testing.PropertyReplacer} */
this.replacer = new goog.testing.PropertyReplacer();
},
/**
* Runs immediately before the setUpPage phase of JsUnit tests.
* @return {!goog.Promise<?>|undefined} An optional Promise which must be
* resolved before the test is executed.
*/
setUpPage: function() {
if (this.mockClock && this.mockClock.isDisposed()) {
this.mockClock = new goog.testing.MockClock(true);
}
},
/** Runs immediately after the tearDownPage phase of JsUnit tests. */
tearDownPage: function() {
// If we created the mockClock, we'll also dispose it.
if (this.shouldMakeMockClock_) {
this.mockClock.dispose();
}
},
/**
* Runs immediately before the setUp phase of JsUnit tests.
* @return {!goog.Promise<?>|undefined} An optional Promise which must be
* resolved before the test case is executed.
*/
setUp: goog.nullFunction,
/** Runs immediately after the tearDown phase of JsUnit tests. */
tearDown: function() {
// Make sure promises and other stuff that may still be scheduled, get a
// chance to run (and throw errors).
if (this.mockClock) {
for (var i = 0; i < 100; i++) {
this.mockClock.tick(1000);
}
// If we created the mockClock, we'll also reset it.
if (this.shouldMakeMockClock_) {
this.mockClock.reset();
}
}
// Reset all changes made by the PropertyReplacer.
this.replacer.reset();
// Make sure the user did not forget to call $replayAll & $verifyAll in
// their test. This is a noop if they did.
// This is important because:
// - Engineers thinks that not all their tests need to replay and verify.
// That lets tests sneak in that call mocks but never replay those calls.
// - Then some well meaning maintenance engineer wants to update the test
// with some new mock, adds a replayAll and BOOM the test fails
// because completely unrelated mocks now get replayed.
if (this.mockControl) {
try {
this.mockControl.$verifyAll();
this.mockControl.$replayAll();
this.mockControl.$verifyAll();
} finally {
this.mockControl.$resetAll();
if (this.shouldMakeMockControl_) {
// If we created the mockControl, we'll also tear it down.
this.mockControl.$tearDown();
}
}
}
// Verifying the mockControl may throw, so if cleanup needs to happen,
// add it further up in the function.
},
/**
* Create a new {@see goog.testing.MockControl} accessible via
* {@code env.mockControl} for each test. If your test has more than one
* testing environment, don't call this on more than one of them.
* @return {!goog.labs.testing.Environment} For chaining.
*/
withMockControl: function() {
if (!this.shouldMakeMockControl_) {
this.shouldMakeMockControl_ = true;
this.mockControl = new goog.testing.MockControl();
}
return this;
},
/**
* Create a {@see goog.testing.MockClock} for each test. The clock will be
* installed (override i.e. setTimeout) by default. It can be accessed
* using {@code env.mockClock}. If your test has more than one testing
* environment, don't call this on more than one of them.
* @return {!goog.labs.testing.Environment} For chaining.
*/
withMockClock: function() {
if (!this.shouldMakeMockClock_) {
this.shouldMakeMockClock_ = true;
this.mockClock = new goog.testing.MockClock(true);
}
return this;
},
/**
* Creates a basic strict mock of a {@code toMock}. For more advanced mocking,
* please use the MockControl directly.
* @param {Function} toMock
* @return {!goog.testing.StrictMock}
*/
mock: function(toMock) {
if (!this.shouldMakeMockControl_) {
throw new Error(
'MockControl not available on this environment. ' +
'Call withMockControl if this environment is expected ' +
'to contain a MockControl.');
}
return this.mockControl.createStrictMock(toMock);
}
});
/**
* @private {?goog.testing.TestCase}
*/
goog.labs.testing.Environment.activeTestCase_ = null;
// TODO(johnlenz): make this package private when it moves out of labs.
/**
* @return {?goog.testing.TestCase}
*/
goog.labs.testing.Environment.getTestCaseIfActive = function() {
return goog.labs.testing.Environment.activeTestCase_;
};
/** @private @const {!goog.debug.Console} */
goog.labs.testing.Environment.console_ = new goog.debug.Console();
// Activate logging to the browser's console by default.
goog.labs.testing.Environment.console_.setCapturing(true);
/**
* An internal TestCase used to hook environments into the JsUnit test runner.
* Environments cannot be used in conjunction with custom TestCases for JsUnit.
* @private @final @constructor
* @extends {goog.testing.TestCase}
*/
goog.labs.testing.EnvironmentTestCase_ = function() {
goog.labs.testing.EnvironmentTestCase_.base(this, 'constructor');
/** @private {!Array<!goog.labs.testing.Environment>}> */
this.environments_ = [];
/** @private {!Object} */
this.testobj_ = goog.global; // default
// Automatically install this TestCase when any environment is used in a test.
goog.testing.TestCase.initializeTestRunner(this);
};
goog.inherits(goog.labs.testing.EnvironmentTestCase_, goog.testing.TestCase);
goog.addSingletonGetter(goog.labs.testing.EnvironmentTestCase_);
/**
* @param {!Object} obj An object providing the test and life cycle methods.
* @override
*/
goog.labs.testing.EnvironmentTestCase_.prototype.setTestObj = function(obj) {
goog.asserts.assert(
this.testobj_ == goog.global,
'A test method object has already been provided ' +
'and only one is supported.');
this.testobj_ = obj;
goog.labs.testing.EnvironmentTestCase_.base(this, 'setTestObj', obj);
};
/**
* Override the default global scope discovery of lifecycle functions to prevent
* overriding the custom environment setUp(Page)/tearDown(Page) logic.
* @override
*/
goog.labs.testing.EnvironmentTestCase_.prototype.autoDiscoverLifecycle =
function() {
if (this.testobj_['runTests']) {
this.runTests = goog.bind(this.testobj_['runTests'], this.testobj_);
}
if (this.testobj_['shouldRunTests']) {
this.shouldRunTests =
goog.bind(this.testobj_['shouldRunTests'], this.testobj_);
}
};
/**
* @override
*/
goog.labs.testing.EnvironmentTestCase_.prototype.createTest = function(
name, ref, scope, objChain) {
return new goog.labs.testing.EnvironmentTest_(name, ref, scope, objChain);
};
/**
* Adds an environment to the JsUnit test.
* @param {!goog.labs.testing.Environment} env
* @private
*/
goog.labs.testing.EnvironmentTestCase_.prototype.registerEnvironment_ =
function(env) {
this.environments_.push(env);
};
/** @override */
goog.labs.testing.EnvironmentTestCase_.prototype.setUpPage = function() {
var setUpPageFns = goog.array.map(this.environments_, function(env) {
return goog.bind(env.setUpPage, env);
}, this);
// User defined setUpPage method.
if (this.testobj_['setUpPage']) {
setUpPageFns.push(goog.bind(this.testobj_['setUpPage'], this.testobj_));
}
return this.callAndChainPromises_(setUpPageFns);
};
/** @override */
goog.labs.testing.EnvironmentTestCase_.prototype.setUp = function() {
var setUpFns = [];
// User defined configure method.
if (this.testobj_['configureEnvironment']) {
setUpFns.push(
goog.bind(this.testobj_['configureEnvironment'], this.testobj_));
}
var test = this.getCurrentTest();
if (test instanceof goog.labs.testing.EnvironmentTest_) {
goog.array.extend(setUpFns, test.configureEnvironments);
}
goog.array.forEach(this.environments_, function(env) {
setUpFns.push(goog.bind(env.setUp, env));
}, this);
// User defined setUp method.
if (this.testobj_['setUp']) {
setUpFns.push(goog.bind(this.testobj_['setUp'], this.testobj_));
}
return this.callAndChainPromises_(setUpFns);
};
/**
* Calls a chain of methods and makes sure to properly chain them if any of the
* methods returns a thenable.
* @param {!Array<function()>} fns
* @return {!goog.Thenable|undefined}
* @private
*/
goog.labs.testing.EnvironmentTestCase_.prototype.callAndChainPromises_ =
function(fns) {
return goog.array.reduce(fns, function(previousResult, fn) {
if (goog.Thenable.isImplementedBy(previousResult)) {
return previousResult.then(function() {
return fn();
});
}
return fn();
}, undefined /* initialValue */, this);
};
/** @override */
goog.labs.testing.EnvironmentTestCase_.prototype.tearDown = function() {
var firstException;
// User defined tearDown method.
if (this.testobj_['tearDown']) {
try {
this.testobj_['tearDown']();
} catch (e) {
if (!firstException) {
firstException = e || new Error('Exception thrown: ' + String(e));
}
}
}
// Execute the tearDown methods for the environment in the reverse order
// in which they were registered to "unfold" the setUp.
goog.array.forEachRight(this.environments_, function(env) {
// For tearDowns between tests make sure they run as much as possible to
// avoid interference between tests.
try {
env.tearDown();
} catch (e) {
if (!firstException) {
firstException = e || new Error('Exception thrown: ' + String(e));
}
}
});
if (firstException) {
throw firstException;
}
};
/** @override */
goog.labs.testing.EnvironmentTestCase_.prototype.tearDownPage = function() {
// User defined tearDownPage method.
if (this.testobj_['tearDownPage']) {
this.testobj_['tearDownPage']();
}
goog.array.forEachRight(
this.environments_, function(env) { env.tearDownPage(); });
};
/**
* An internal Test used to hook environments into the JsUnit test runner.
* @param {string} name The test name.
* @param {function()} ref Reference to the test function or test object.
* @param {?Object=} scope Optional scope that the test function should be
* called in.
* @param {!Array<!Object>=} objChain A chain of objects used to populate setUps
* and tearDowns.
* @private
* @final
* @constructor
* @extends {goog.testing.TestCase.Test}
*/
goog.labs.testing.EnvironmentTest_ = function(name, ref, scope, objChain) {
goog.labs.testing.EnvironmentTest_.base(
this, 'constructor', name, ref, scope, objChain);
/**
* @type {!Array<function()>}
*/
this.configureEnvironments = goog.array.map(
goog.array.filter(
objChain || [],
function(obj) {
return goog.isFunction(obj.configureEnvironment);
}), /**
* @param {{configureEnvironment: function()}} obj
* @return {function()}
*/
function(obj) {
return goog.bind(obj.configureEnvironment, obj);
});
};
goog.inherits(goog.labs.testing.EnvironmentTest_, goog.testing.TestCase.Test);
|