123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- 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');
- goog.labs.testing.Environment = goog.defineClass(null, {
-
- constructor: function() {
- var testcase = goog.labs.testing.EnvironmentTestCase_.getInstance();
- testcase.registerEnvironment_(this);
-
-
- goog.labs.testing.Environment.activeTestCase_ = testcase;
-
- this.mockControl = null;
-
- this.mockClock = null;
-
- this.shouldMakeMockControl_ = false;
-
- this.shouldMakeMockClock_ = false;
-
- this.console = goog.labs.testing.Environment.console_;
-
- this.replacer = new goog.testing.PropertyReplacer();
- },
-
- setUpPage: function() {
- if (this.mockClock && this.mockClock.isDisposed()) {
- this.mockClock = new goog.testing.MockClock(true);
- }
- },
-
- tearDownPage: function() {
-
- if (this.shouldMakeMockClock_) {
- this.mockClock.dispose();
- }
- },
-
- setUp: goog.nullFunction,
-
- tearDown: function() {
-
-
- if (this.mockClock) {
- for (var i = 0; i < 100; i++) {
- this.mockClock.tick(1000);
- }
-
- if (this.shouldMakeMockClock_) {
- this.mockClock.reset();
- }
- }
-
- this.replacer.reset();
-
-
-
-
-
-
-
-
- if (this.mockControl) {
- try {
- this.mockControl.$verifyAll();
- this.mockControl.$replayAll();
- this.mockControl.$verifyAll();
- } finally {
- this.mockControl.$resetAll();
- if (this.shouldMakeMockControl_) {
-
- this.mockControl.$tearDown();
- }
- }
- }
-
-
- },
-
- withMockControl: function() {
- if (!this.shouldMakeMockControl_) {
- this.shouldMakeMockControl_ = true;
- this.mockControl = new goog.testing.MockControl();
- }
- return this;
- },
-
- withMockClock: function() {
- if (!this.shouldMakeMockClock_) {
- this.shouldMakeMockClock_ = true;
- this.mockClock = new goog.testing.MockClock(true);
- }
- return this;
- },
-
- 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);
- }
- });
- goog.labs.testing.Environment.activeTestCase_ = null;
- goog.labs.testing.Environment.getTestCaseIfActive = function() {
- return goog.labs.testing.Environment.activeTestCase_;
- };
- goog.labs.testing.Environment.console_ = new goog.debug.Console();
- goog.labs.testing.Environment.console_.setCapturing(true);
- goog.labs.testing.EnvironmentTestCase_ = function() {
- goog.labs.testing.EnvironmentTestCase_.base(this, 'constructor');
-
- this.environments_ = [];
-
- this.testobj_ = goog.global;
-
- goog.testing.TestCase.initializeTestRunner(this);
- };
- goog.inherits(goog.labs.testing.EnvironmentTestCase_, goog.testing.TestCase);
- goog.addSingletonGetter(goog.labs.testing.EnvironmentTestCase_);
- 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);
- };
- 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_);
- }
- };
- goog.labs.testing.EnvironmentTestCase_.prototype.registerEnvironment_ =
- function(env) {
- this.environments_.push(env);
- };
- goog.labs.testing.EnvironmentTestCase_.prototype.setUpPage = function() {
- var setUpPageFns = goog.array.map(this.environments_, function(env) {
- return goog.bind(env.setUpPage, env);
- }, this);
-
- if (this.testobj_['setUpPage']) {
- setUpPageFns.push(goog.bind(this.testobj_['setUpPage'], this.testobj_));
- }
- return this.callAndChainPromises_(setUpPageFns);
- };
- goog.labs.testing.EnvironmentTestCase_.prototype.setUp = function() {
- var setUpFns = [];
-
- if (this.testobj_['configureEnvironment']) {
- setUpFns.push(
- goog.bind(this.testobj_['configureEnvironment'], this.testobj_));
- }
- goog.array.forEach(this.environments_, function(env) {
- setUpFns.push(goog.bind(env.setUp, env));
- }, this);
-
- if (this.testobj_['setUp']) {
- setUpFns.push(goog.bind(this.testobj_['setUp'], this.testobj_));
- }
- return this.callAndChainPromises_(setUpFns);
- };
- 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 , this);
- };
- goog.labs.testing.EnvironmentTestCase_.prototype.tearDown = function() {
- var firstException;
-
- if (this.testobj_['tearDown']) {
- try {
- this.testobj_['tearDown']();
- } catch (e) {
- if (!firstException) {
- firstException = e || new Error('Exception thrown: ' + String(e));
- }
- }
- }
-
-
- goog.array.forEachRight(this.environments_, function(env) {
-
-
- try {
- env.tearDown();
- } catch (e) {
- if (!firstException) {
- firstException = e || new Error('Exception thrown: ' + String(e));
- }
- }
- });
- if (firstException) {
- throw firstException;
- }
- };
- goog.labs.testing.EnvironmentTestCase_.prototype.tearDownPage = function() {
-
- if (this.testobj_['tearDownPage']) {
- this.testobj_['tearDownPage']();
- }
- goog.array.forEachRight(
- this.environments_, function(env) { env.tearDownPage(); });
- };
|