asynctestcase_async_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2009 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.AsyncTestCaseAsyncTest');
  15. goog.setTestOnly('goog.testing.AsyncTestCaseAsyncTest');
  16. goog.require('goog.testing.AsyncTestCase');
  17. goog.require('goog.testing.TestCase');
  18. goog.require('goog.testing.jsunit');
  19. // Has the setUp() function been called.
  20. var setUpCalled = false;
  21. // Has the current test function completed. This helps us to ensure that
  22. // the next test is not started before the previous completed.
  23. var curTestIsDone = true;
  24. // Use an asynchronous test runner for our tests.
  25. var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(document.title);
  26. /**
  27. * Uses window.setTimeout() to perform asynchronous behaviour and uses
  28. * asyncTestCase.waitForAsync() and asyncTestCase.continueTesting() to mark
  29. * the beginning and end of it.
  30. * @param {number} numAsyncCalls The number of asynchronous calls to make.
  31. * @param {string} name The name of the current step.
  32. */
  33. function doAsyncStuff(numAsyncCalls, name) {
  34. if (numAsyncCalls > 0) {
  35. curTestIsDone = false;
  36. asyncTestCase.waitForAsync(
  37. 'doAsyncStuff-' + name + '(' + numAsyncCalls + ')');
  38. window.setTimeout(function() { doAsyncStuff(numAsyncCalls - 1, name); }, 0);
  39. } else {
  40. curTestIsDone = true;
  41. asyncTestCase.continueTesting();
  42. }
  43. }
  44. function setUpPage() {
  45. debug('setUpPage was called.');
  46. doAsyncStuff(3, 'setUpPage');
  47. }
  48. function setUp() {
  49. assertTrue(curTestIsDone);
  50. doAsyncStuff(3, 'setUp');
  51. }
  52. function tearDown() {
  53. assertTrue(curTestIsDone);
  54. }
  55. function test1() {
  56. assertTrue(curTestIsDone);
  57. doAsyncStuff(1, 'test1');
  58. }
  59. function test2_asyncContinueThenWait() {
  60. var activeTest = asyncTestCase.activeTest_;
  61. function async1() {
  62. asyncTestCase.continueTesting();
  63. asyncTestCase.waitForAsync('2');
  64. window.setTimeout(async2, 0);
  65. }
  66. function async2() {
  67. asyncTestCase.continueTesting();
  68. assertEquals(
  69. 'Did not wait for inner waitForAsync', activeTest,
  70. asyncTestCase.activeTest_);
  71. }
  72. asyncTestCase.waitForAsync('1');
  73. window.setTimeout(async1, 0);
  74. }
  75. function test3() {
  76. assertTrue(curTestIsDone);
  77. doAsyncStuff(2, 'test3');
  78. }
  79. function tearDownPage() {
  80. debug('tearDownPage was called.');
  81. assertTrue(curTestIsDone);
  82. }
  83. var callback = function() {
  84. curTestIsDone = true;
  85. asyncTestCase.signal();
  86. };
  87. var doAsyncSignals = function() {
  88. curTestIsDone = false;
  89. window.setTimeout(callback, 0);
  90. };
  91. function testSignalsReturn() {
  92. doAsyncSignals();
  93. doAsyncSignals();
  94. doAsyncSignals();
  95. asyncTestCase.waitForSignals(3);
  96. }
  97. function testSignalsMixedSyncAndAsync() {
  98. asyncTestCase.signal();
  99. doAsyncSignals();
  100. doAsyncSignals();
  101. asyncTestCase.waitForSignals(3);
  102. }
  103. function testSignalsMixedSyncAndAsyncMultipleWaits() {
  104. asyncTestCase.signal();
  105. doAsyncSignals();
  106. asyncTestCase.waitForSignals(1);
  107. doAsyncSignals();
  108. asyncTestCase.waitForSignals(2);
  109. }
  110. function testSignalsCallContinueTestingBeforeFinishing() {
  111. doAsyncSignals();
  112. asyncTestCase.waitForSignals(2);
  113. window.setTimeout(function() {
  114. var thrown = assertThrows(function() { asyncTestCase.continueTesting(); });
  115. assertEquals('Still waiting for 1 signals.', thrown.message);
  116. }, 0);
  117. doAsyncSignals(); // To not timeout.
  118. }
  119. function testCurrentTestName() {
  120. var currentTestName = goog.testing.TestCase.currentTestName;
  121. assertEquals('testCurrentTestName', currentTestName);
  122. }
  123. function testCurrentTestNameAsync() {
  124. var getAssertSameTest = function() {
  125. var expectedTestCase = goog.testing.TestCase.getActiveTestCase();
  126. var expectedTestName = (expectedTestCase ? expectedTestCase.getName() :
  127. '<no active TestCase>') +
  128. '.' +
  129. (goog.testing.TestCase.currentTestName || '<no active test name>');
  130. var assertSameTest = function() {
  131. var currentTestCase = goog.testing.TestCase.getActiveTestCase();
  132. var currentTestName = (currentTestCase ? currentTestCase.getName() :
  133. '<no active TestCase>') +
  134. '.' + goog.testing.TestCase.currentTestName ||
  135. '<no active test name>';
  136. assertEquals(expectedTestName, currentTestName);
  137. assertEquals(expectedTestCase, currentTestCase);
  138. };
  139. return assertSameTest;
  140. };
  141. var assertSameTest = getAssertSameTest();
  142. // do something asynchronously...
  143. asyncTestCase.waitForSignals(1, 'Awaiting asynchronous callback');
  144. setTimeout(function() {
  145. // ... and ensure the later half runs during the same test:
  146. assertSameTest();
  147. asyncTestCase.signal();
  148. });
  149. }