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