4 <script src=
"mock_timer.js"></script>
12 * Counter class for tallying the number if times a calback is triggered.
15 function ClickCounter() {
18 ClickCounter
.prototype = {
20 * Nubmer of times the callback was triggered.
26 /** Increments click count */
32 * Creates a callback function that tracks the number of calls.
35 createCallback: function() {
43 * Nubmer of times the callback was triggered.
47 return this.clickCount_
;
52 mockTimer
= new MockTimer();
57 mockTimer
.uninstall();
60 function testSetTimeout() {
61 var counter
= new ClickCounter();
62 window
.setTimeout(counter
.createCallback(), 100);
63 assertEquals(0, counter
.value
);
65 assertEquals(0, counter
.value
);
67 assertEquals(1, counter
.value
);
69 assertEquals(1, counter
.value
);
72 function testClearTimeout() {
73 var counter
= new ClickCounter();
74 var t
= window
.setTimeout(counter
.createCallback(), 100);
76 // Verify that clearing a timeout before the elapsed time does not trigger
78 window
.clearTimeout(t
);
80 assertEquals(0, counter
.value
);
83 function testSetAndClearInterval() {
84 var counter
= new ClickCounter();
85 var t
= window
.setInterval(counter
.createCallback(), 100);
87 // Verify that callback doesn't fire before elapsed interval.
88 assertEquals(0, counter
.value
);
91 // Verify that each elapsed time interval advances the count by 1.
92 assertEquals(0, counter
.value
);
94 assertEquals(1, counter
.value
);
96 assertEquals(2, counter
.value
);
98 assertEquals(3, counter
.value
);
100 // Verify that callbacks stop firing after timer is cleared.
101 window
.clearInterval(t
);
103 assertEquals(3, counter
.value
);
106 function testInterleavedTimers() {
108 var createCallback = function(response
) {
109 var label
= response
;
111 results
= results
+ label
;
115 // Verify callbacks are properly interleaved.
116 var t1
= window
.setInterval(createCallback('A'), 7);
117 var t2
= window
.setInterval(createCallback('B'), 13);
119 assertEquals('ABAABA', results
);
121 assertEquals('ABAABAABAABA', results
);
123 window
.clearInterval(t1
);
124 window
.setTimeout(createCallback('C'), 11);
126 assertEquals('ABAABAABAABABCB', results
);
128 window
.clearInterval(t2
);
130 assertEquals('ABAABAABAABABCB', results
);