1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.async.Throttle</title>
<script src="../base.js"></script>
<script>
goog.require('goog.async.Throttle');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
function testThrottle() {
var clock = new goog.testing.MockClock(true);
var callBackCount = 0;
var callBackFunction = function() {
callBackCount++;
}
var throttle = new goog.async.Throttle(callBackFunction, 100);
assertEquals(0, callBackCount);
throttle.fire();
assertEquals(1, callBackCount);
throttle.fire();
assertEquals(1, callBackCount);
throttle.fire();
throttle.fire();
assertEquals(1, callBackCount);
clock.tick(101);
assertEquals(2, callBackCount);
clock.tick(101);
assertEquals(2, callBackCount);
throttle.fire();
assertEquals(3, callBackCount);
throttle.fire();
assertEquals(3, callBackCount);
throttle.stop();
clock.tick(101);
assertEquals(3, callBackCount);
throttle.fire();
assertEquals(4, callBackCount);
clock.tick(101);
assertEquals(4, callBackCount);
throttle.fire();
throttle.fire();
assertEquals(5, callBackCount);
throttle.pause();
throttle.resume();
assertEquals(5, callBackCount);
throttle.pause();
clock.tick(101);
assertEquals(5, callBackCount);
throttle.resume();
assertEquals(6, callBackCount);
clock.tick(101);
assertEquals(6, callBackCount);
throttle.pause();
throttle.fire();
assertEquals(6, callBackCount);
clock.tick(101);
assertEquals(6, callBackCount);
throttle.resume();
assertEquals(7, callBackCount);
throttle.pause();
throttle.pause();
clock.tick(101);
throttle.fire();
throttle.resume();
assertEquals(7, callBackCount);
throttle.resume();
assertEquals(8, callBackCount);
throttle.pause();
throttle.pause();
throttle.fire();
throttle.resume();
clock.tick(101);
assertEquals(8, callBackCount);
throttle.resume();
assertEquals(9, callBackCount);
clock.uninstall();
}
</script>
</body>
</html>
|