throttle_test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2006 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.ThrottleTest');
  15. goog.setTestOnly('goog.async.ThrottleTest');
  16. goog.require('goog.async.Throttle');
  17. goog.require('goog.testing.MockClock');
  18. goog.require('goog.testing.jsunit');
  19. function testThrottle() {
  20. var clock = new goog.testing.MockClock(true);
  21. var callBackCount = 0;
  22. var callBackFunction = function() { callBackCount++; };
  23. var throttle = new goog.async.Throttle(callBackFunction, 100);
  24. assertEquals(0, callBackCount);
  25. throttle.fire();
  26. assertEquals(1, callBackCount);
  27. throttle.fire();
  28. assertEquals(1, callBackCount);
  29. throttle.fire();
  30. throttle.fire();
  31. assertEquals(1, callBackCount);
  32. clock.tick(101);
  33. assertEquals(2, callBackCount);
  34. clock.tick(101);
  35. assertEquals(2, callBackCount);
  36. throttle.fire();
  37. assertEquals(3, callBackCount);
  38. throttle.fire();
  39. assertEquals(3, callBackCount);
  40. throttle.stop();
  41. clock.tick(101);
  42. assertEquals(3, callBackCount);
  43. throttle.fire();
  44. assertEquals(4, callBackCount);
  45. clock.tick(101);
  46. assertEquals(4, callBackCount);
  47. throttle.fire();
  48. throttle.fire();
  49. assertEquals(5, callBackCount);
  50. throttle.pause();
  51. throttle.resume();
  52. assertEquals(5, callBackCount);
  53. throttle.pause();
  54. clock.tick(101);
  55. assertEquals(5, callBackCount);
  56. throttle.resume();
  57. assertEquals(6, callBackCount);
  58. clock.tick(101);
  59. assertEquals(6, callBackCount);
  60. throttle.pause();
  61. throttle.fire();
  62. assertEquals(6, callBackCount);
  63. clock.tick(101);
  64. assertEquals(6, callBackCount);
  65. throttle.resume();
  66. assertEquals(7, callBackCount);
  67. throttle.pause();
  68. throttle.pause();
  69. clock.tick(101);
  70. throttle.fire();
  71. throttle.resume();
  72. assertEquals(7, callBackCount);
  73. throttle.resume();
  74. assertEquals(8, callBackCount);
  75. throttle.pause();
  76. throttle.pause();
  77. throttle.fire();
  78. throttle.resume();
  79. clock.tick(101);
  80. assertEquals(8, callBackCount);
  81. throttle.resume();
  82. assertEquals(9, callBackCount);
  83. clock.uninstall();
  84. }
  85. function testThrottleScopeBinding() {
  86. var interval = 500;
  87. var mockClock = new goog.testing.MockClock(true);
  88. var x = {'y': 0};
  89. new goog.async.Throttle(function() { ++this['y']; }, interval, x).fire();
  90. assertEquals(1, x['y']);
  91. mockClock.uninstall();
  92. }
  93. function testThrottleArgumentBinding() {
  94. var interval = 500;
  95. var mockClock = new goog.testing.MockClock(true);
  96. var calls = 0;
  97. var throttle = new goog.async.Throttle(function(a, b, c) {
  98. ++calls;
  99. assertEquals(3, a);
  100. assertEquals('string', b);
  101. assertEquals(false, c);
  102. }, interval);
  103. throttle.fire(3, 'string', false);
  104. assertEquals(1, calls);
  105. // fire should always pass the last arguments passed to it into the decorated
  106. // function, even if called multiple times.
  107. throttle.fire();
  108. mockClock.tick(interval / 2);
  109. throttle.fire(8, null, true);
  110. throttle.fire(3, 'string', false);
  111. mockClock.tick(interval);
  112. assertEquals(2, calls);
  113. mockClock.uninstall();
  114. }
  115. function testThrottleArgumentAndScopeBinding() {
  116. var interval = 500;
  117. var mockClock = new goog.testing.MockClock(true);
  118. var x = {'calls': 0};
  119. var throttle = new goog.async.Throttle(function(a, b, c) {
  120. ++this['calls'];
  121. assertEquals(3, a);
  122. assertEquals('string', b);
  123. assertEquals(false, c);
  124. }, interval, x);
  125. throttle.fire(3, 'string', false);
  126. assertEquals(1, x['calls']);
  127. // fire should always pass the last arguments passed to it into the decorated
  128. // function, even if called multiple times.
  129. throttle.fire();
  130. mockClock.tick(interval / 2);
  131. throttle.fire(8, null, true);
  132. throttle.fire(3, 'string', false);
  133. mockClock.tick(interval);
  134. assertEquals(2, x['calls']);
  135. mockClock.uninstall();
  136. }