Debian Codegraph @

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

raw
1 // Copyright 2007 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.DelayTest');
15 goog.setTestOnly('goog.async.DelayTest');
16
17 goog.require('goog.async.Delay');
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
25
26 function callback() {
27 invoked = true;
28 }
29
30
31 function setUp() {
32 clock = new goog.testing.MockClock(true);
33 invoked = false;
34 delay = new goog.async.Delay(callback, 200);
35 }
36
37 function tearDown() {
38 clock.dispose();
39 delay.dispose();
40 }
41
42
43 function testDelay() {
44 delay.start();
45 assertFalse(invoked);
46
47 clock.tick(100);
48 assertFalse(invoked);
49
50 clock.tick(100);
51 assertTrue(invoked);
52 }
53
54
55 function testStop() {
56 delay.start();
57
58 clock.tick(100);
59 assertFalse(invoked);
60
61 delay.stop();
62 clock.tick(100);
63 assertFalse(invoked);
64 }
65
66
67 function testIsActive() {
68 assertFalse(delay.isActive());
69 delay.start();
70 assertTrue(delay.isActive());
71 clock.tick(200);
72 assertFalse(delay.isActive());
73 }
74
75
76 function testRestart() {
77 delay.start();
78 clock.tick(100);
79
80 delay.stop();
81 assertFalse(invoked);
82
83 delay.start();
84 clock.tick(199);
85 assertFalse(invoked);
86
87 clock.tick(1);
88 assertTrue(invoked);
89
90 invoked = false;
91 delay.start();
92 clock.tick(200);
93 assertTrue(invoked);
94 }
95
96
97 function testStartIfNotActive() {
98 delay.startIfNotActive();
99 clock.tick(100);
100
101 delay.stop();
102 assertFalse(invoked);
103
104 delay.startIfNotActive();
105 clock.tick(199);
106 assertFalse(invoked);
107
108 clock.tick(1);
109 assertTrue(invoked);
110
111 invoked = false;
112 delay.start();
113 clock.tick(199);
114
115 assertFalse(invoked);
116
117 delay.startIfNotActive();
118 clock.tick(1);
119
120 assertTrue(invoked);
121 }
122
123
124 function testOverride() {
125 delay.start(50);
126 clock.tick(49);
127 assertFalse(invoked);
128
129 clock.tick(1);
130 assertTrue(invoked);
131 }
132
133
134 function testDispose() {
135 delay.start();
136 delay.dispose();
137 assertTrue(delay.isDisposed());
138
139 clock.tick(500);
140 assertFalse(invoked);
141 }
142
143
144 function testFire() {
145 delay.start();
146
147 clock.tick(50);
148 delay.fire();
149 assertTrue(invoked);
150 assertFalse(delay.isActive());
151
152 invoked = false;
153 clock.tick(200);
154 assertFalse(
155 'Delay fired early with fire call, timeout should have been ' +
156 'cleared',
157 invoked);
158 }
159
160 function testFireIfActive() {
161 delay.fireIfActive();
162 assertFalse(invoked);
163
164 delay.start();
165 delay.fireIfActive();
166 assertTrue(invoked);
167 invoked = false;
168 clock.tick(300);
169 assertFalse(
170 'Delay fired early with fireIfActive, timeout should have been ' +
171 'cleared',
172 invoked);
173 }