delay_test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2007 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.async.DelayTest');
  15. goog.setTestOnly('goog.async.DelayTest');
  16. goog.require('goog.async.Delay');
  17. goog.require('goog.testing.MockClock');
  18. goog.require('goog.testing.jsunit');
  19. var invoked = false;
  20. var delay = null;
  21. var clock = null;
  22. function callback() {
  23. invoked = true;
  24. }
  25. function setUp() {
  26. clock = new goog.testing.MockClock(true);
  27. invoked = false;
  28. delay = new goog.async.Delay(callback, 200);
  29. }
  30. function tearDown() {
  31. clock.dispose();
  32. delay.dispose();
  33. }
  34. function testDelay() {
  35. delay.start();
  36. assertFalse(invoked);
  37. clock.tick(100);
  38. assertFalse(invoked);
  39. clock.tick(100);
  40. assertTrue(invoked);
  41. }
  42. function testStop() {
  43. delay.start();
  44. clock.tick(100);
  45. assertFalse(invoked);
  46. delay.stop();
  47. clock.tick(100);
  48. assertFalse(invoked);
  49. }
  50. function testIsActive() {
  51. assertFalse(delay.isActive());
  52. delay.start();
  53. assertTrue(delay.isActive());
  54. clock.tick(200);
  55. assertFalse(delay.isActive());
  56. }
  57. function testRestart() {
  58. delay.start();
  59. clock.tick(100);
  60. delay.stop();
  61. assertFalse(invoked);
  62. delay.start();
  63. clock.tick(199);
  64. assertFalse(invoked);
  65. clock.tick(1);
  66. assertTrue(invoked);
  67. invoked = false;
  68. delay.start();
  69. clock.tick(200);
  70. assertTrue(invoked);
  71. }
  72. function testStartIfNotActive() {
  73. delay.startIfNotActive();
  74. clock.tick(100);
  75. delay.stop();
  76. assertFalse(invoked);
  77. delay.startIfNotActive();
  78. clock.tick(199);
  79. assertFalse(invoked);
  80. clock.tick(1);
  81. assertTrue(invoked);
  82. invoked = false;
  83. delay.start();
  84. clock.tick(199);
  85. assertFalse(invoked);
  86. delay.startIfNotActive();
  87. clock.tick(1);
  88. assertTrue(invoked);
  89. }
  90. function testOverride() {
  91. delay.start(50);
  92. clock.tick(49);
  93. assertFalse(invoked);
  94. clock.tick(1);
  95. assertTrue(invoked);
  96. }
  97. function testDispose() {
  98. delay.start();
  99. delay.dispose();
  100. assertTrue(delay.isDisposed());
  101. clock.tick(500);
  102. assertFalse(invoked);
  103. }
  104. function testFire() {
  105. delay.start();
  106. clock.tick(50);
  107. delay.fire();
  108. assertTrue(invoked);
  109. assertFalse(delay.isActive());
  110. invoked = false;
  111. clock.tick(200);
  112. assertFalse(
  113. 'Delay fired early with fire call, timeout should have been ' +
  114. 'cleared',
  115. invoked);
  116. }
  117. function testFireIfActive() {
  118. delay.fireIfActive();
  119. assertFalse(invoked);
  120. delay.start();
  121. delay.fireIfActive();
  122. assertTrue(invoked);
  123. invoked = false;
  124. clock.tick(300);
  125. assertFalse(
  126. 'Delay fired early with fireIfActive, timeout should have been ' +
  127. 'cleared',
  128. invoked);
  129. }