123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- goog.setTestOnly('goog.testing.ContinuationTestCase');
- goog.provide('goog.testing.ContinuationTestCase');
- goog.provide('goog.testing.ContinuationTestCase.ContinuationTest');
- goog.provide('goog.testing.ContinuationTestCase.Step');
- goog.require('goog.array');
- goog.require('goog.events.EventHandler');
- goog.require('goog.testing.TestCase');
- goog.require('goog.testing.asserts');
- goog.testing.ContinuationTestCase = function(opt_name) {
- goog.testing.TestCase.call(this, opt_name);
-
- this.handler_ = new goog.events.EventHandler(this);
- };
- goog.inherits(goog.testing.ContinuationTestCase, goog.testing.TestCase);
- goog.testing.ContinuationTestCase.MAX_TIMEOUT = 1000;
- goog.testing.ContinuationTestCase.locked_ = false;
- goog.testing.ContinuationTestCase.prototype.currentTest_ = null;
- goog.testing.ContinuationTestCase.prototype.enableWaitFunctions_ = function(
- enable) {
- if (enable) {
- goog.exportSymbol(
- 'waitForCondition', goog.bind(this.waitForCondition, this));
- goog.exportSymbol('waitForEvent', goog.bind(this.waitForEvent, this));
- goog.exportSymbol('waitForTimeout', goog.bind(this.waitForTimeout, this));
- } else {
-
- goog.global['waitForCondition'] = undefined;
- goog.global['waitForEvent'] = undefined;
- goog.global['waitForTimeout'] = undefined;
- }
- };
- goog.testing.ContinuationTestCase.prototype.runTests = function() {
- this.enableWaitFunctions_(true);
- goog.testing.ContinuationTestCase.superClass_.runTests.call(this);
- };
- goog.testing.ContinuationTestCase.prototype.finalize = function() {
- this.enableWaitFunctions_(false);
- goog.testing.ContinuationTestCase.superClass_.finalize.call(this);
- };
- goog.testing.ContinuationTestCase.prototype.cycleTests = function() {
-
- if (!this.currentTest_) {
- this.currentTest_ = this.createNextTest_();
- }
-
- if (this.currentTest_) {
- this.runNextStep_();
- } else {
- this.finalize();
- }
- };
- goog.testing.ContinuationTestCase.prototype.createNextTest_ = function() {
- var test = this.next();
- if (!test) {
- return null;
- }
- var name = test.name;
- goog.testing.TestCase.currentTestName = name;
- this.result_.runCount++;
- this.log('Running test: ' + name);
- return new goog.testing.ContinuationTestCase.ContinuationTest(
- new goog.testing.TestCase.Test(name, this.setUp, this), test,
- new goog.testing.TestCase.Test(name, this.tearDown, this));
- };
- goog.testing.ContinuationTestCase.prototype.finishTest_ = function() {
- var err = this.currentTest_.getError();
- if (err) {
- this.doError(this.currentTest_, err);
- } else {
- this.doSuccess(this.currentTest_);
- }
- goog.testing.TestCase.currentTestName = null;
- this.currentTest_ = null;
- this.locked_ = false;
- this.handler_.removeAll();
- this.timeout(goog.bind(this.cycleTests, this), 0);
- };
- goog.testing.ContinuationTestCase.prototype.runNextStep_ = function() {
- if (this.locked_) {
-
-
- return;
- }
- var phase = this.currentTest_.getCurrentPhase();
- if (!phase || !phase.length) {
-
- this.finishTest_();
- return;
- }
-
- var stepIndex =
- goog.array.findIndex(phase, function(step) { return !step.waiting; });
- if (stepIndex < 0) {
-
- return;
- }
- this.locked_ = true;
- var step = phase[stepIndex];
- try {
- step.execute();
-
-
- goog.array.removeAt(phase, stepIndex);
- } catch (e) {
- this.currentTest_.setError(e);
-
-
- this.currentTest_.cancelCurrentPhase();
-
-
- this.currentTest_.cancelTestPhase();
- }
- this.locked_ = false;
- this.runNextStep_();
- };
- goog.testing.ContinuationTestCase.prototype.waitForTimeout = function(
- continuation, opt_duration) {
- var step = this.addStep_(continuation);
- step.setTimeout(
- goog.bind(this.handleComplete_, this, step), opt_duration || 0);
- };
- goog.testing.ContinuationTestCase.prototype.waitForEvent = function(
- eventTarget, eventType, continuation) {
- var step = this.addStep_(continuation);
- var duration = goog.testing.ContinuationTestCase.MAX_TIMEOUT;
- step.setTimeout(
- goog.bind(this.handleTimeout_, this, step, duration), duration);
- this.handler_.listenOnce(
- eventTarget, eventType, goog.bind(this.handleComplete_, this, step));
- };
- goog.testing.ContinuationTestCase.prototype.waitForCondition = function(
- condition, continuation, opt_interval, opt_maxTimeout) {
- var interval = opt_interval || 100;
- var timeout = opt_maxTimeout || goog.testing.ContinuationTestCase.MAX_TIMEOUT;
- var step = this.addStep_(continuation);
- this.testCondition_(step, condition, goog.now(), interval, timeout);
- };
- goog.testing.ContinuationTestCase.prototype.addStep_ = function(func) {
- if (!this.currentTest_) {
- throw Error('Cannot add test steps outside of a running test.');
- }
- var step = new goog.testing.ContinuationTestCase.Step(
- this.currentTest_.name, func, this.currentTest_.scope);
- this.currentTest_.addStep(step);
- return step;
- };
- goog.testing.ContinuationTestCase.prototype.handleComplete_ = function(step) {
- step.clearTimeout();
- step.waiting = false;
- this.runNextStep_();
- };
- goog.testing.ContinuationTestCase.prototype.handleTimeout_ = function(
- step, duration) {
- step.ref = function() {
- fail('Continuation timed out after ' + duration + 'ms.');
- };
-
- this.handler_.removeAll();
- this.handleComplete_(step);
- };
- goog.testing.ContinuationTestCase.prototype.testCondition_ = function(
- step, condition, startTime, interval, timeout) {
- var duration = goog.now() - startTime;
- if (condition()) {
- this.handleComplete_(step);
- } else if (duration < timeout) {
- step.setTimeout(
- goog.bind(
- this.testCondition_, this, step, condition, startTime, interval,
- timeout),
- interval);
- } else {
- this.handleTimeout_(step, duration);
- }
- };
- goog.testing.ContinuationTestCase.ContinuationTest = function(
- setUp, test, tearDown) {
-
- goog.testing.TestCase.Test.call(this, test.name, null, null);
-
- this.setUp_ = [setUp];
-
- this.test_ = [test];
-
- this.tearDown_ = [tearDown];
- };
- goog.inherits(
- goog.testing.ContinuationTestCase.ContinuationTest,
- goog.testing.TestCase.Test);
- goog.testing.ContinuationTestCase.ContinuationTest.prototype.error_ = null;
- goog.testing.ContinuationTestCase.ContinuationTest.prototype.getError =
- function() {
- return this.error_;
- };
- goog.testing.ContinuationTestCase.ContinuationTest.prototype.setError =
- function(e) {
- this.error_ = this.error_ || e;
- };
- goog.testing.ContinuationTestCase.ContinuationTest.prototype.getCurrentPhase =
- function() {
- if (this.setUp_.length) {
- return this.setUp_;
- }
- if (this.test_.length) {
- return this.test_;
- }
- if (this.tearDown_.length) {
- return this.tearDown_;
- }
- return null;
- };
- goog.testing.ContinuationTestCase.ContinuationTest.prototype.addStep = function(
- step) {
- var phase = this.getCurrentPhase();
- if (phase) {
- phase.push(step);
- } else {
- throw Error('Attempted to add a step to a completed test.');
- }
- };
- goog.testing.ContinuationTestCase.ContinuationTest.prototype
- .cancelCurrentPhase = function() {
- this.cancelPhase_(this.getCurrentPhase());
- };
- goog.testing.ContinuationTestCase.ContinuationTest.prototype.cancelTestPhase =
- function() {
- this.cancelPhase_(this.setUp_);
- this.cancelPhase_(this.test_);
- };
- goog.testing.ContinuationTestCase.ContinuationTest.prototype.cancelPhase_ =
- function(phase) {
- while (phase && phase.length) {
- var step = phase.pop();
- if (step instanceof goog.testing.ContinuationTestCase.Step) {
- step.clearTimeout();
- }
- }
- };
- goog.testing.ContinuationTestCase.Step = function(name, ref, opt_scope) {
- goog.testing.TestCase.Test.call(this, name, ref, opt_scope);
- };
- goog.inherits(
- goog.testing.ContinuationTestCase.Step, goog.testing.TestCase.Test);
- goog.testing.ContinuationTestCase.Step.prototype.waiting = true;
- goog.testing.ContinuationTestCase.Step.protectedClearTimeout_ =
- window.clearTimeout;
- goog.testing.ContinuationTestCase.Step.protectedSetTimeout_ = window.setTimeout;
- goog.testing.ContinuationTestCase.Step.prototype.timeout_;
- goog.testing.ContinuationTestCase.Step.prototype.setTimeout = function(
- func, duration) {
- this.clearTimeout();
- var setTimeout = goog.testing.ContinuationTestCase.Step.protectedSetTimeout_;
- this.timeout_ = setTimeout(func, duration);
- };
- goog.testing.ContinuationTestCase.Step.prototype.clearTimeout = function() {
- if (this.timeout_) {
- var clear = goog.testing.ContinuationTestCase.Step.protectedClearTimeout_;
- clear(this.timeout_);
- delete this.timeout_;
- }
- };
|