deferredtestcase.js 5.2 KB

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