Debian Codegraph @

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

raw
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
17 goog.require('goog.async.ConditionalDelay');
18 goog.require('goog.testing.MockClock');
19 goog.require('goog.testing.jsunit');
20
21 var invoked = false;
22 var delay = null;
23 var clock = null;
24 var returnValue = true;
25 var onSuccessCalled = false;
26 var onFailureCalled = false;
27
28
29 function callback() {
30 invoked = true;
31 return returnValue;
32 }
33
34
35 function setUp() {
36 clock = new goog.testing.MockClock(true);
37 invoked = false;
38 returnValue = true;
39 onSuccessCalled = false;
40 onFailureCalled = false;
41 delay = new goog.async.ConditionalDelay(callback);
42 delay.onSuccess = function() { onSuccessCalled = true; };
43 delay.onFailure = function() { onFailureCalled = true; };
44 }
45
46
47 function tearDown() {
48 clock.dispose();
49 delay.dispose();
50 }
51
52
53 function testDelay() {
54 delay.start(200, 200);
55 assertFalse(invoked);
56
57 clock.tick(100);
58 assertFalse(invoked);
59
60 clock.tick(100);
61 assertTrue(invoked);
62 }
63
64
65 function testStop() {
66 delay.start(200, 500);
67 assertTrue(delay.isActive());
68
69 clock.tick(100);
70 assertFalse(invoked);
71
72 delay.stop();
73 clock.tick(100);
74 assertFalse(invoked);
75
76 assertFalse(delay.isActive());
77 }
78
79
80 function testIsActive() {
81 assertFalse(delay.isActive());
82 delay.start(200, 200);
83 assertTrue(delay.isActive());
84 clock.tick(200);
85 assertFalse(delay.isActive());
86 }
87
88
89 function testRestart() {
90 delay.start(200, 50000);
91 clock.tick(100);
92
93 delay.stop();
94 assertFalse(invoked);
95
96 delay.start(200, 50000);
97 clock.tick(199);
98 assertFalse(invoked);
99
100 clock.tick(1);
101 assertTrue(invoked);
102
103 invoked = false;
104 delay.start(200, 200);
105 clock.tick(200);
106 assertTrue(invoked);
107
108 assertFalse(delay.isActive());
109 }
110
111
112 function testDispose() {
113 delay.start(200, 200);
114 delay.dispose();
115 assertTrue(delay.isDisposed());
116
117 clock.tick(500);
118 assertFalse(invoked);
119 }
120
121
122 function testConditionalDelay_Success() {
123 returnValue = false;
124 delay.start(100, 300);
125
126 clock.tick(99);
127 assertFalse(invoked);
128 clock.tick(1);
129 assertTrue(invoked);
130
131 assertTrue(delay.isActive());
132 assertFalse(delay.isDone());
133 assertFalse(onSuccessCalled);
134 assertFalse(onFailureCalled);
135
136 returnValue = true;
137
138 invoked = false;
139 clock.tick(100);
140 assertTrue(invoked);
141
142 assertFalse(delay.isActive());
143 assertTrue(delay.isDone());
144 assertTrue(onSuccessCalled);
145 assertFalse(onFailureCalled);
146
147 invoked = false;
148 clock.tick(200);
149 assertFalse(invoked);
150 }
151
152
153 function testConditionalDelay_Failure() {
154 returnValue = false;
155 delay.start(100, 300);
156
157 clock.tick(99);
158 assertFalse(invoked);
159 clock.tick(1);
160 assertTrue(invoked);
161
162 assertTrue(delay.isActive());
163 assertFalse(delay.isDone());
164 assertFalse(onSuccessCalled);
165 assertFalse(onFailureCalled);
166
167 invoked = false;
168 clock.tick(100);
169 assertTrue(invoked);
170 assertFalse(onSuccessCalled);
171 assertFalse(onFailureCalled);
172
173 invoked = false;
174 clock.tick(90);
175 assertFalse(invoked);
176 clock.tick(10);
177 assertTrue(invoked);
178
179 assertFalse(delay.isActive());
180 assertFalse(delay.isDone());
181 assertFalse(onSuccessCalled);
182 assertTrue(onFailureCalled);
183 }
184
185
186 function testInfiniteDelay() {
187 returnValue = false;
188 delay.start(100, -1);
189
190 // Test in a big enough loop.
191 for (var i = 0; i < 1000; ++i) {
192 clock.tick(80);
193 assertTrue(delay.isActive());
194 assertFalse(delay.isDone());
195 assertFalse(onSuccessCalled);
196 assertFalse(onFailureCalled);
197 }
198
199 delay.stop();
200 assertFalse(delay.isActive());
201 assertFalse(delay.isDone());
202 assertFalse(onSuccessCalled);
203 assertFalse(onFailureCalled);
204 }
205
206 function testCallbackScope() {
207 var callbackCalled = false;
208 var scopeObject = {};
209 function internalCallback() {
210 assertEquals(this, scopeObject);
211 callbackCalled = true;
212 return true;
213 }
214 delay = new goog.async.ConditionalDelay(internalCallback, scopeObject);
215 delay.start(200, 200);
216 clock.tick(201);
217 assertTrue(callbackCalled);
218 }