deferredtestcase_test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. goog.provide('goog.testing.DeferredTestCaseTest');
  15. goog.setTestOnly('goog.testing.DeferredTestCaseTest');
  16. goog.require('goog.async.Deferred');
  17. goog.require('goog.testing.DeferredTestCase');
  18. goog.require('goog.testing.TestCase');
  19. goog.require('goog.testing.TestRunner');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.recordFunction');
  22. var deferredTestCase =
  23. goog.testing.DeferredTestCase.createAndInstall(document.title);
  24. var testTestCase;
  25. var runner;
  26. // Optionally, set a longer-than-usual step timeout.
  27. deferredTestCase.stepTimeout = 15 * 1000; // 15 seconds
  28. // This is the sample code in deferredtestcase.js
  29. function testDeferredCallbacks() {
  30. var callbackTime = goog.now();
  31. var callbacks = new goog.async.Deferred();
  32. deferredTestCase.addWaitForAsync('Waiting for 1st callback', callbacks);
  33. callbacks.addCallback(function() {
  34. assertTrue('We\'re going back in time!', goog.now() >= callbackTime);
  35. callbackTime = goog.now();
  36. });
  37. deferredTestCase.addWaitForAsync('Waiting for 2nd callback', callbacks);
  38. callbacks.addCallback(function() {
  39. assertTrue('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(function() {
  44. assertTrue('We\'re going back in time!', goog.now() >= callbackTime);
  45. callbackTime = goog.now();
  46. });
  47. deferredTestCase.waitForDeferred(callbacks);
  48. }
  49. function createDeferredTestCase(d) {
  50. testTestCase = new goog.testing.DeferredTestCase('Foobar TestCase');
  51. testTestCase.add(new goog.testing.TestCase.Test('Foobar Test', function() {
  52. this.waitForDeferred(d);
  53. }, testTestCase));
  54. var testCompleteCallback = new goog.async.Deferred();
  55. testTestCase.setCompletedCallback(function() {
  56. testCompleteCallback.callback(true);
  57. });
  58. // We're not going to use the runner to run the test, but we attach one
  59. // here anyway because without a runner TestCase throws an exception in
  60. // finalize().
  61. var runner = new goog.testing.TestRunner();
  62. runner.initialize(testTestCase);
  63. return testCompleteCallback;
  64. }
  65. function testDeferredWait() {
  66. var d = new goog.async.Deferred();
  67. deferredTestCase.addWaitForAsync('Foobar', d);
  68. d.addCallback(function() { return goog.async.Deferred.succeed(true); });
  69. deferredTestCase.waitForDeferred(d);
  70. }
  71. function testNonAsync() {
  72. assertTrue(true);
  73. }
  74. function testPassWithTestRunner() {
  75. var d = new goog.async.Deferred();
  76. d.addCallback(function() { return goog.async.Deferred.succeed(true); });
  77. var testCompleteDeferred = createDeferredTestCase(d);
  78. testTestCase.execute();
  79. var deferredCallbackOnPass = new goog.async.Deferred();
  80. deferredCallbackOnPass.addCallback(function() {
  81. return testCompleteDeferred;
  82. });
  83. deferredCallbackOnPass.addCallback(function() {
  84. assertTrue('Test case should have succeeded.', testTestCase.isSuccess());
  85. });
  86. deferredTestCase.waitForDeferred(deferredCallbackOnPass);
  87. }
  88. function testFailWithTestRunner() {
  89. var d = new goog.async.Deferred();
  90. d.addCallback(function() { return goog.async.Deferred.fail(true); });
  91. var testCompleteDeferred = createDeferredTestCase(d);
  92. // Mock doAsyncError to instead let the test completes successfully,
  93. // but record the failure. The test works as is because the failing
  94. // deferred is not actually asynchronous.
  95. var mockDoAsyncError = goog.testing.recordFunction(function() {
  96. testTestCase.continueTesting();
  97. });
  98. testTestCase.doAsyncError = mockDoAsyncError;
  99. testTestCase.execute();
  100. assertEquals(1, mockDoAsyncError.getCallCount());
  101. }