Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / webui / mock_timer_test.html
blob0a8f68f6b5d811cd883fa5201b74f13256657c53
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="mock_timer.js"></script>
5 </head>
6 <body>
7 <script>
9 var mockTimer;
11 /**
12 * Counter class for tallying the number if times a calback is triggered.
13 * @constructor
15 function ClickCounter() {
18 ClickCounter.prototype = {
19 /**
20 * Nubmer of times the callback was triggered.
21 * @type{number}
22 * @private
24 clickCount_: 0,
26 /** Increments click count */
27 tick: function() {
28 this.clickCount_++;
31 /**
32 * Creates a callback function that tracks the number of calls.
33 * @return {!Function}
35 createCallback: function() {
36 var self = this;
37 return function() {
38 self.tick();
42 /**
43 * Nubmer of times the callback was triggered.
44 * @type {number}
46 get value() {
47 return this.clickCount_;
51 function setUp() {
52 mockTimer = new MockTimer();
53 mockTimer.install();
56 function tearDown() {
57 mockTimer.uninstall();
60 function testSetTimeout() {
61 var counter = new ClickCounter();
62 window.setTimeout(counter.createCallback(), 100);
63 assertEquals(0, counter.value);
64 mockTimer.tick(50);
65 assertEquals(0, counter.value);
66 mockTimer.tick(100);
67 assertEquals(1, counter.value);
68 mockTimer.tick(100);
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
77 // the callback.
78 window.clearTimeout(t);
79 mockTimer.tick(200);
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);
89 mockTimer.tick(50);
91 // Verify that each elapsed time interval advances the count by 1.
92 assertEquals(0, counter.value);
93 mockTimer.tick(100);
94 assertEquals(1, counter.value);
95 mockTimer.tick(100);
96 assertEquals(2, counter.value);
97 mockTimer.tick(100);
98 assertEquals(3, counter.value);
100 // Verify that callbacks stop firing after timer is cleared.
101 window.clearInterval(t);
102 mockTimer.tick(100);
103 assertEquals(3, counter.value);
106 function testInterleavedTimers() {
107 var results = '';
108 var createCallback = function(response) {
109 var label = response;
110 return function() {
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);
118 mockTimer.tick(30);
119 assertEquals('ABAABA', results);
120 mockTimer.tick(30);
121 assertEquals('ABAABAABAABA', results);
123 window.clearInterval(t1);
124 window.setTimeout(createCallback('C'), 11);
125 mockTimer.tick(30);
126 assertEquals('ABAABAABAABABCB', results);
128 window.clearInterval(t2);
129 mockTimer.tick(30);
130 assertEquals('ABAABAABAABABCB', results);
133 </script>
134 </body>
135 </html>