conditionaldelay_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2008 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.ConditionalDelayTest');
  15. goog.setTestOnly('goog.async.ConditionalDelayTest');
  16. goog.require('goog.async.ConditionalDelay');
  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. var returnValue = true;
  23. var onSuccessCalled = false;
  24. var onFailureCalled = false;
  25. function callback() {
  26. invoked = true;
  27. return returnValue;
  28. }
  29. function setUp() {
  30. clock = new goog.testing.MockClock(true);
  31. invoked = false;
  32. returnValue = true;
  33. onSuccessCalled = false;
  34. onFailureCalled = false;
  35. delay = new goog.async.ConditionalDelay(callback);
  36. delay.onSuccess = function() { onSuccessCalled = true; };
  37. delay.onFailure = function() { onFailureCalled = true; };
  38. }
  39. function tearDown() {
  40. clock.dispose();
  41. delay.dispose();
  42. }
  43. function testDelay() {
  44. delay.start(200, 200);
  45. assertFalse(invoked);
  46. clock.tick(100);
  47. assertFalse(invoked);
  48. clock.tick(100);
  49. assertTrue(invoked);
  50. }
  51. function testStop() {
  52. delay.start(200, 500);
  53. assertTrue(delay.isActive());
  54. clock.tick(100);
  55. assertFalse(invoked);
  56. delay.stop();
  57. clock.tick(100);
  58. assertFalse(invoked);
  59. assertFalse(delay.isActive());
  60. }
  61. function testIsActive() {
  62. assertFalse(delay.isActive());
  63. delay.start(200, 200);
  64. assertTrue(delay.isActive());
  65. clock.tick(200);
  66. assertFalse(delay.isActive());
  67. }
  68. function testRestart() {
  69. delay.start(200, 50000);
  70. clock.tick(100);
  71. delay.stop();
  72. assertFalse(invoked);
  73. delay.start(200, 50000);
  74. clock.tick(199);
  75. assertFalse(invoked);
  76. clock.tick(1);
  77. assertTrue(invoked);
  78. invoked = false;
  79. delay.start(200, 200);
  80. clock.tick(200);
  81. assertTrue(invoked);
  82. assertFalse(delay.isActive());
  83. }
  84. function testDispose() {
  85. delay.start(200, 200);
  86. delay.dispose();
  87. assertTrue(delay.isDisposed());
  88. clock.tick(500);
  89. assertFalse(invoked);
  90. }
  91. function testConditionalDelay_Success() {
  92. returnValue = false;
  93. delay.start(100, 300);
  94. clock.tick(99);
  95. assertFalse(invoked);
  96. clock.tick(1);
  97. assertTrue(invoked);
  98. assertTrue(delay.isActive());
  99. assertFalse(delay.isDone());
  100. assertFalse(onSuccessCalled);
  101. assertFalse(onFailureCalled);
  102. returnValue = true;
  103. invoked = false;
  104. clock.tick(100);
  105. assertTrue(invoked);
  106. assertFalse(delay.isActive());
  107. assertTrue(delay.isDone());
  108. assertTrue(onSuccessCalled);
  109. assertFalse(onFailureCalled);
  110. invoked = false;
  111. clock.tick(200);
  112. assertFalse(invoked);
  113. }
  114. function testConditionalDelay_Failure() {
  115. returnValue = false;
  116. delay.start(100, 300);
  117. clock.tick(99);
  118. assertFalse(invoked);
  119. clock.tick(1);
  120. assertTrue(invoked);
  121. assertTrue(delay.isActive());
  122. assertFalse(delay.isDone());
  123. assertFalse(onSuccessCalled);
  124. assertFalse(onFailureCalled);
  125. invoked = false;
  126. clock.tick(100);
  127. assertTrue(invoked);
  128. assertFalse(onSuccessCalled);
  129. assertFalse(onFailureCalled);
  130. invoked = false;
  131. clock.tick(90);
  132. assertFalse(invoked);
  133. clock.tick(10);
  134. assertTrue(invoked);
  135. assertFalse(delay.isActive());
  136. assertFalse(delay.isDone());
  137. assertFalse(onSuccessCalled);
  138. assertTrue(onFailureCalled);
  139. }
  140. function testInfiniteDelay() {
  141. returnValue = false;
  142. delay.start(100, -1);
  143. // Test in a big enough loop.
  144. for (var i = 0; i < 1000; ++i) {
  145. clock.tick(80);
  146. assertTrue(delay.isActive());
  147. assertFalse(delay.isDone());
  148. assertFalse(onSuccessCalled);
  149. assertFalse(onFailureCalled);
  150. }
  151. delay.stop();
  152. assertFalse(delay.isActive());
  153. assertFalse(delay.isDone());
  154. assertFalse(onSuccessCalled);
  155. assertFalse(onFailureCalled);
  156. }
  157. function testCallbackScope() {
  158. var callbackCalled = false;
  159. var scopeObject = {};
  160. function internalCallback() {
  161. assertEquals(this, scopeObject);
  162. callbackCalled = true;
  163. return true;
  164. }
  165. delay = new goog.async.ConditionalDelay(internalCallback, scopeObject);
  166. delay.start(200, 200);
  167. clock.tick(201);
  168. assertTrue(callbackCalled);
  169. }