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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
<!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.Timer</title>
<script src="../base.js"></script>
<script>
goog.require('goog.Timer');
goog.require('goog.events');
goog.require('goog.events.EventTarget');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.MockClock');
</script>
</head>
<body>
<script>
var intervalIds = {};
var intervalIdCounter = 0;
var mockClock;
var maxDuration = 60 * 1000; // 60s
function setUp() {
mockClock = new goog.testing.MockClock(true /* install */);
}
function tearDown() {
mockClock.dispose();
}
// Run a test for 60s and see how many counts we get
function runTest(string, ticks, number) {
var t = new goog.Timer(ticks);
var count = 0;
goog.events.listen(t, 'tick', function(evt) {
count++;
});
t.start();
mockClock.tick(maxDuration);
assertEquals(string, number, count);
t.stop();
goog.events.removeAll(t);
}
function test100msTicks() {
// Desc, interval in ms, expected ticks in 60s
runTest('10 ticks per second for 60 seconds', 100, 600);
}
function test500msTicks() {
runTest('2 ticks per second for 60 seconds', 500, 120);
}
function test1sTicks() {
runTest('1 tick per second for 60 seconds', 1000, 60);
}
function test2sTicks() {
runTest('1 tick every 2 seconds for 60 seconds', 2000, 30);
}
function test5sTicks() {
runTest('1 tick every 5 seconds for 60 seconds', 5000, 12);
}
function test10sTicks() {
runTest('1 tick every 10 seconds for 60 seconds', 10000, 6);
}
function test30sTicks() {
runTest('1 tick every 30 seconds for 60 seconds', 30000, 2);
}
function test60sTicks() {
runTest('1 tick every 60 seconds', 60000, 1);
}
function testCallOnce() {
var c = 0;
var actualTimeoutId = goog.Timer.callOnce(
function() {
if (c > 0) {
assertTrue('callOnce should only be called once', false);
}
c++;
});
assertEquals('callOnce should return the timeout ID',
mockClock.getTimeoutsMade(), actualTimeoutId);
var obj = {c:0};
goog.Timer.callOnce(function() {
if (this.c > 0) {
assertTrue('callOnce should only be called once', false);
}
assertEquals(obj, this);
this.c++;
}, 1, obj);
mockClock.tick(maxDuration);
}
function testCallOnceIgnoresTimeoutsTooLarge() {
var failCallback = goog.partial(fail, 'Timeout should never be called');
assertEquals('Timeouts slightly too large should yield a timer ID of -1',
-1, goog.Timer.callOnce(failCallback, 2147483648));
assertEquals('Infinite timeouts should yield a timer ID of -1',
-1, goog.Timer.callOnce(failCallback, Infinity));
}
</script>
</body>
</html>
|