debouncer_test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2015 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.DebouncerTest');
  15. goog.setTestOnly('goog.async.DebouncerTest');
  16. goog.require('goog.array');
  17. goog.require('goog.async.Debouncer');
  18. goog.require('goog.testing.MockClock');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.testing.recordFunction');
  21. function testDebouncerCommandSequences() {
  22. // Encoded sequences of commands to perform mapped to expected # of calls.
  23. // f: fire
  24. // w: wait (for the debouncing timer to elapse)
  25. // p: pause
  26. // r: resume
  27. // s: stop
  28. var expectedCommandSequenceCalls = {
  29. 'f': 0,
  30. 'ff': 0,
  31. 'fw': 1,
  32. 'ffw': 1,
  33. 'fpr': 0,
  34. 'fsf': 0,
  35. 'fsw': 0,
  36. 'fprw': 1,
  37. 'fpwr': 1,
  38. 'fsfw': 1,
  39. 'fswf': 0,
  40. 'fprfw': 1,
  41. 'fprsw': 0,
  42. 'fpswr': 0,
  43. 'fpwfr': 0,
  44. 'fpwsr': 0,
  45. 'fswfw': 1,
  46. 'fpswrw': 0,
  47. 'fpwfrw': 1,
  48. 'fpwsfr': 0,
  49. 'fpwsrw': 0,
  50. 'fspwrw': 0,
  51. 'fpwsfrw': 1,
  52. 'ffwfwfffw': 3
  53. };
  54. var interval = 500;
  55. var mockClock = new goog.testing.MockClock(true);
  56. for (var commandSequence in expectedCommandSequenceCalls) {
  57. var recordFn = goog.testing.recordFunction();
  58. var debouncer = new goog.async.Debouncer(recordFn, interval);
  59. for (var i = 0; i < commandSequence.length; ++i) {
  60. switch (commandSequence[i]) {
  61. case 'f':
  62. debouncer.fire();
  63. break;
  64. case 'w':
  65. mockClock.tick(interval);
  66. break;
  67. case 'p':
  68. debouncer.pause();
  69. break;
  70. case 'r':
  71. debouncer.resume();
  72. break;
  73. case 's':
  74. debouncer.stop();
  75. break;
  76. }
  77. }
  78. var expectedCalls = expectedCommandSequenceCalls[commandSequence];
  79. assertEquals(
  80. 'Expected ' + expectedCalls + ' calls for command sequence "' +
  81. commandSequence + '" (' +
  82. goog.array
  83. .map(
  84. commandSequence,
  85. function(command) {
  86. switch (command) {
  87. case 'f':
  88. return 'fire';
  89. case 'w':
  90. return 'wait';
  91. case 'p':
  92. return 'pause';
  93. case 'r':
  94. return 'resume';
  95. case 's':
  96. return 'stop';
  97. }
  98. })
  99. .join(' -> ') +
  100. ')',
  101. expectedCalls, recordFn.getCallCount());
  102. debouncer.dispose();
  103. }
  104. mockClock.uninstall();
  105. }
  106. function testDebouncerScopeBinding() {
  107. var interval = 500;
  108. var mockClock = new goog.testing.MockClock(true);
  109. var x = {'y': 0};
  110. var debouncer =
  111. new goog.async.Debouncer(function() { ++this['y']; }, interval, x);
  112. debouncer.fire();
  113. assertEquals(0, x['y']);
  114. mockClock.tick(interval);
  115. assertEquals(1, x['y']);
  116. mockClock.uninstall();
  117. }
  118. function testDebouncerArgumentBinding() {
  119. var interval = 500;
  120. var mockClock = new goog.testing.MockClock(true);
  121. var calls = 0;
  122. var debouncer = new goog.async.Debouncer(function(a, b, c) {
  123. ++calls;
  124. assertEquals(3, a);
  125. assertEquals('string', b);
  126. assertEquals(false, c);
  127. }, interval);
  128. debouncer.fire(3, 'string', false);
  129. mockClock.tick(interval);
  130. assertEquals(1, calls);
  131. // fire should always pass the last arguments passed to it into the decorated
  132. // function, even if called multiple times.
  133. debouncer.fire();
  134. mockClock.tick(interval / 2);
  135. debouncer.fire(8, null, true);
  136. debouncer.fire(3, 'string', false);
  137. mockClock.tick(interval);
  138. assertEquals(2, calls);
  139. mockClock.uninstall();
  140. }
  141. function testDebouncerArgumentAndScopeBinding() {
  142. var interval = 500;
  143. var mockClock = new goog.testing.MockClock(true);
  144. var x = {'calls': 0};
  145. var debouncer = new goog.async.Debouncer(function(a, b, c) {
  146. ++this['calls'];
  147. assertEquals(3, a);
  148. assertEquals('string', b);
  149. assertEquals(false, c);
  150. }, interval, x);
  151. debouncer.fire(3, 'string', false);
  152. mockClock.tick(interval);
  153. assertEquals(1, x['calls']);
  154. // fire should always pass the last arguments passed to it into the decorated
  155. // function, even if called multiple times.
  156. debouncer.fire();
  157. mockClock.tick(interval / 2);
  158. debouncer.fire(8, null, true);
  159. debouncer.fire(3, 'string', false);
  160. mockClock.tick(interval);
  161. assertEquals(2, x['calls']);
  162. mockClock.uninstall();
  163. }