Debian Codegraph @

closure-library/closure/goog/timer/timer_test.js

raw
1 // Copyright 2006 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.TimerTest');
16 goog.setTestOnly('goog.TimerTest');
17
18 goog.require('goog.Promise');
19 goog.require('goog.Timer');
20 goog.require('goog.events');
21 goog.require('goog.testing.MockClock');
22 goog.require('goog.testing.jsunit');
23
24 var intervalIds = {};
25 var intervalIdCounter = 0;
26 var mockClock;
27 var maxDuration = 60 * 1000; // 60s
28
29 function setUp() {
30 mockClock = new goog.testing.MockClock(true /* install */);
31 }
32
33 function tearDown() {
34 mockClock.dispose();
35 }
36
37 // Run a test for 60s and see how many counts we get
38 function runTest(string, ticks, number) {
39 var t = new goog.Timer(ticks);
40 var count = 0;
41 goog.events.listen(t, 'tick', function(evt) { count++; });
42 t.start();
43 mockClock.tick(maxDuration);
44 assertEquals(string, number, count);
45 t.stop();
46 goog.events.removeAll(t);
47 }
48
49
50 function test100msTicks() {
51 // Desc, interval in ms, expected ticks in 60s
52 runTest('10 ticks per second for 60 seconds', 100, 600);
53 }
54
55 function test500msTicks() {
56 runTest('2 ticks per second for 60 seconds', 500, 120);
57 }
58
59 function test1sTicks() {
60 runTest('1 tick per second for 60 seconds', 1000, 60);
61 }
62
63 function test2sTicks() {
64 runTest('1 tick every 2 seconds for 60 seconds', 2000, 30);
65 }
66
67 function test5sTicks() {
68 runTest('1 tick every 5 seconds for 60 seconds', 5000, 12);
69 }
70
71 function test10sTicks() {
72 runTest('1 tick every 10 seconds for 60 seconds', 10000, 6);
73 }
74
75 function test30sTicks() {
76 runTest('1 tick every 30 seconds for 60 seconds', 30000, 2);
77 }
78
79 function test60sTicks() {
80 runTest('1 tick every 60 seconds', 60000, 1);
81 }
82
83 function testCallOnce() {
84 var c = 0;
85 var expectedTimeoutId = goog.testing.MockClock.nextId;
86 var actualTimeoutId = goog.Timer.callOnce(function() {
87 if (c > 0) {
88 assertTrue('callOnce should only be called once', false);
89 }
90 c++;
91 });
92 assertEquals(
93 'callOnce should return the timeout ID', expectedTimeoutId,
94 actualTimeoutId);
95
96 var obj = {c: 0};
97 goog.Timer.callOnce(function() {
98 if (this.c > 0) {
99 assertTrue('callOnce should only be called once', false);
100 }
101 assertEquals(obj, this);
102 this.c++;
103 }, 1, obj);
104 mockClock.tick(maxDuration);
105 }
106
107 function testCallOnceIgnoresTimeoutsTooLarge() {
108 var failCallback = goog.partial(fail, 'Timeout should never be called');
109 assertEquals(
110 'Timeouts slightly too large should yield a timer ID of -1', -1,
111 goog.Timer.callOnce(failCallback, 2147483648));
112 assertEquals(
113 'Infinite timeouts should yield a timer ID of -1', -1,
114 goog.Timer.callOnce(failCallback, Infinity));
115 }
116
117 function testPromise() {
118 var c = 0;
119 goog.Timer.promise(1, 'A').then(function(result) {
120 c++;
121 assertEquals('promise should return resolved value', 'A', result);
122 });
123 mockClock.tick(10);
124 assertEquals('promise must be yielded once and only once', 1, c);
125 }
126
127 function testPromise_cancel() {
128 var c = 0;
129 goog.Timer.promise(1, 'A')
130 .then(
131 function(result) { fail('promise must not be resolved'); },
132 function(reason) {
133 c++;
134 assertTrue(
135 'promise must fail due to cancel signal',
136 reason instanceof goog.Promise.CancellationError);
137 })
138 .cancel();
139 mockClock.tick(10);
140 assertEquals('promise must be canceled once and only once', 1, c);
141 }
142
143 function testPromise_timeoutTooLarge() {
144 var c = 0;
145 goog.Timer.promise(2147483648, 'A')
146 .then(
147 function(result) { fail('promise must not be resolved'); },
148 function(reason) {
149 c++;
150 assertTrue('promise must be rejected', reason instanceof Error);
151 });
152 mockClock.tick(10);
153 assertEquals('promise must be rejected once and only once', 1, c);
154 }