asynctestcase_noasync_test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.AsyncTestCaseSyncTest');
  15. goog.setTestOnly('goog.testing.AsyncTestCaseSyncTest');
  16. goog.require('goog.testing.AsyncTestCase');
  17. goog.require('goog.testing.jsunit');
  18. // Has the setUp() function been called.
  19. var setUpCalled = false;
  20. // Has the current test function completed. This helps us to ensure that the
  21. // next test is not started before the previous completed.
  22. var curTestIsDone = true;
  23. // For restoring it later.
  24. var oldTimeout = window.setTimeout;
  25. // Use an asynchronous test runner for our tests.
  26. var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(document.title);
  27. /**
  28. * Uses window.setTimeout() to perform asynchronous behaviour and uses
  29. * asyncTestCase.waitForAsync() and asyncTestCase.continueTesting() to mark
  30. * the beginning and end of it.
  31. * @param {number} numAsyncCalls The number of asynchronous calls to make.
  32. * @param {string} name The name of the current step.
  33. */
  34. function doAsyncStuff(numAsyncCalls, name) {
  35. if (numAsyncCalls > 0) {
  36. curTestIsDone = false;
  37. asyncTestCase.waitForAsync(
  38. 'doAsyncStuff-' + name + '(' + numAsyncCalls + ')');
  39. window.setTimeout(function() { doAsyncStuff(numAsyncCalls - 1, name); }, 0);
  40. } else {
  41. curTestIsDone = true;
  42. asyncTestCase.continueTesting();
  43. }
  44. }
  45. function setUpPage() {
  46. debug('setUpPage was called.');
  47. // Don't do anything asynchronously.
  48. window.setTimeout = function(callback, time) { callback(); };
  49. doAsyncStuff(3, 'setUpPage');
  50. }
  51. function setUp() {
  52. assertTrue(curTestIsDone);
  53. doAsyncStuff(3, 'setUp');
  54. }
  55. function tearDown() {
  56. assertTrue(curTestIsDone);
  57. }
  58. function test1() {
  59. assertTrue(curTestIsDone);
  60. doAsyncStuff(1, 'test1');
  61. }
  62. function test2() {
  63. assertTrue(curTestIsDone);
  64. doAsyncStuff(2, 'test2');
  65. }
  66. function test3() {
  67. assertTrue(curTestIsDone);
  68. doAsyncStuff(5, 'test3');
  69. }
  70. var callback = function() {
  71. curTestIsDone = true;
  72. asyncTestCase.signal();
  73. };
  74. var doAsyncSignals = function() {
  75. curTestIsDone = false;
  76. window.setTimeout(callback, 0);
  77. };
  78. function testSignalsReturn() {
  79. doAsyncSignals();
  80. doAsyncSignals();
  81. doAsyncSignals();
  82. asyncTestCase.waitForSignals(3);
  83. }
  84. function testSignalsCallContinueTestingBeforeFinishing() {
  85. doAsyncSignals();
  86. asyncTestCase.waitForSignals(2);
  87. window.setTimeout(function() {
  88. var thrown = assertThrows(function() { asyncTestCase.continueTesting(); });
  89. assertEquals('Still waiting for 1 signals.', thrown.message);
  90. }, 0);
  91. doAsyncSignals(); // To not timeout.
  92. }
  93. function tearDownPage() {
  94. debug('tearDownPage was called.');
  95. assertTrue(curTestIsDone);
  96. window.setTimeout = oldTimeout;
  97. }