Debian Codegraph @

closure-library/closure/goog/async/debouncer_test.js

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