aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/mockclock_test.html
blob: 39e367f6cd2823e973f7d5a137c6d31d7c923f2f (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<!DOCTYPE html>
<html>
<!--
Copyright 2007 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.testing.MockClock</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.events');
  goog.require('goog.functions');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.recordFunction');
</script>
</head>
<body>
<script>
  var stubs = new goog.testing.PropertyReplacer();

  function tearDown() {
    stubs.reset();
  }

  function testMockClockWasInstalled() {
    var clock = new goog.testing.MockClock();
    var originalTimeout = window.setTimeout;
    clock.install();
    assertNotEquals(window.setTimeout, originalTimeout);
    setTimeout(function() {}, 100);
    assertEquals(1, clock.getTimeoutsMade());
    setInterval(function() {}, 200);
    assertEquals(2, clock.getTimeoutsMade());
    clock.uninstall();
    assertEquals(window.setTimeout, originalTimeout);
    assertNull(clock.replacer_);
  }


  function testSetTimeoutAndTick() {
    var clock = new goog.testing.MockClock(true);
    var m5 = false, m10 = false, m15 = false, m20 = false;
    setTimeout(function() { m5 = true; }, 5);
    setTimeout(function() { m10 = true; }, 10);
    setTimeout(function() { m15 = true; }, 15);
    setTimeout(function() { m20 = true; }, 20);
    assertEquals(4, clock.getTimeoutsMade());

    assertEquals(4, clock.tick(4));
    assertEquals(4, clock.getCurrentTime());

    assertFalse(m5);
    assertFalse(m10);
    assertFalse(m15);
    assertFalse(m20);

    assertEquals(5, clock.tick(1));
    assertEquals(5, clock.getCurrentTime());

    assertTrue('m5 should now be true', m5);
    assertFalse(m10);
    assertFalse(m15);
    assertFalse(m20);

    assertEquals(10, clock.tick(5));
    assertEquals(10, clock.getCurrentTime());

    assertTrue('m5 should be true', m5);
    assertTrue('m10 should now be true', m10);
    assertFalse(m15);
    assertFalse(m20);

    assertEquals(15, clock.tick(5));
    assertEquals(15, clock.getCurrentTime());

    assertTrue('m5 should be true', m5);
    assertTrue('m10 should be true', m10);
    assertTrue('m15 should now be true', m15);
    assertFalse(m20);

    assertEquals(20, clock.tick(5));
    assertEquals(20, clock.getCurrentTime());

    assertTrue('m5 should be true', m5);
    assertTrue('m10 should be true', m10);
    assertTrue('m15 should be true', m15);
    assertTrue('m20 should now be true', m20);

    clock.uninstall();
  }


  function testSetInterval() {
    var clock = new goog.testing.MockClock(true);
    var times = 0
    setInterval(function() { times++; }, 100);

    clock.tick(500);
    assertEquals(5, times);
    clock.tick(100);
    assertEquals(6, times);
    clock.tick(100);
    assertEquals(7, times);
    clock.tick(50);
    assertEquals(7, times);
    clock.tick(50);
    assertEquals(8, times);

    clock.uninstall();
  }


  function testRequestAnimationFrame() {
    goog.global.requestAnimationFrame = function () {
    };
    var clock = new goog.testing.MockClock(true);
    var times = [];
    var recFunc = goog.testing.recordFunction(function(now) {
      times.push(now);
    });
    goog.global.requestAnimationFrame(recFunc);
    clock.tick(50);
    assertEquals(1, recFunc.getCallCount());
    assertEquals(20, times[0]);

    goog.global.requestAnimationFrame(recFunc);
    clock.tick(100);
    assertEquals(2, recFunc.getCallCount());
    assertEquals(70, times[1]);

    clock.uninstall();
  }


  function testClearTimeout() {
    var clock = new goog.testing.MockClock(true);
    var ran = false;
    var c = setTimeout(function() { ran = true; }, 100);
    clock.tick(50);
    assertFalse(ran);
    clearTimeout(c);
    clock.tick(100);
    assertFalse(ran);
    clock.uninstall();
  }


  function testClearInterval() {
    var clock = new goog.testing.MockClock(true);
    var times = 0
    var c = setInterval(function() { times++; }, 100);

    clock.tick(500);
    assertEquals(5, times);
    clock.tick(100);
    assertEquals(6, times);
    clock.tick(100);
    clearInterval(c);
    assertEquals(7, times);
    clock.tick(50);
    assertEquals(7, times);
    clock.tick(50);
    assertEquals(7, times);

    clock.uninstall();
  }


  function testClearInterval2() {
    // Tests that we can clear the interval from inside the function
    var clock = new goog.testing.MockClock(true);
    var times = 0
    var c = setInterval(function() {
      times++;
      if (times == 6) {
        clearInterval(c);
      }
    }, 100);

    clock.tick(500);
    assertEquals(5, times);
    clock.tick(100);
    assertEquals(6, times);
    clock.tick(100);
    assertEquals(6, times);
    clock.tick(50);
    assertEquals(6, times);
    clock.tick(50);
    assertEquals(6, times);

    clock.uninstall();
  }


  function testCancelRequestAnimationFrame() {
    goog.global.requestAnimationFrame = function () {
    };
    goog.global.cancelRequestAnimationFrame = function () {
    };
    var clock = new goog.testing.MockClock(true);
    var ran = false;
    var c = goog.global.requestAnimationFrame(function() { ran = true; });
    clock.tick(10);
    assertFalse(ran);
    goog.global.cancelRequestAnimationFrame(c);
    clock.tick(20);
    assertFalse(ran);
    clock.uninstall();
  }


  function testMockGoogNow() {
    assertNotEquals(0, goog.now());
    var clock = new goog.testing.MockClock(true);
    assertEquals(0, goog.now());
    clock.tick(50);
    assertEquals(50, goog.now());
    clock.uninstall();
    assertNotEquals(50, goog.now());
  }


  function testTimeoutDelay() {
    var clock = new goog.testing.MockClock(true);
    var m5 = false, m10 = false, m20 = false;
    setTimeout(function() { m5 = true; }, 5);
    setTimeout(function() { m10 = true; }, 10);
    setTimeout(function() { m20 = true; }, 20);

    // Fire 3ms early, so m5 fires at t=2
    clock.setTimeoutDelay(-3);
    clock.tick(1);
    assertFalse(m5);
    assertFalse(m10);
    clock.tick(1);
    assertTrue(m5);
    assertFalse(m10);

    // Fire 3ms late, so m10 fires at t=13
    clock.setTimeoutDelay(3);
    assertEquals(12, clock.tick(10));
    assertEquals(12, clock.getCurrentTime());
    assertFalse(m10);
    clock.tick(1);
    assertTrue(m10);
    assertFalse(m20);

    // Fire 10ms early, so m20 fires now, since it's after t=10
    clock.setTimeoutDelay(-10);
    assertFalse(m20);
    assertEquals(14, clock.tick(1));
    assertEquals(14, clock.getCurrentTime());
    assertTrue(m20);

    clock.uninstall();
  }


  function testTimerCallbackCanCreateIntermediateTimer() {
    var clock = new goog.testing.MockClock(true);
    var sequence = [];

    // Create 3 timers: 1, 2, and 3.  Timer 1 should fire at T=1, timer 2 at
    // T=2, and timer 3 at T=3.  The catch: Timer 2 is created by the
    // callback within timer 0.

    // Testing method: Create a simple string sequencing each timer and at
    // what time it fired.

    setTimeout(function() {
      sequence.push('timer1 at T=' + goog.now());
      setTimeout(function() {
        sequence.push('timer2 at T=' + goog.now());
      }, 1);
    }, 1);

    setTimeout(function() {
      sequence.push('timer3 at T=' + goog.now());
    }, 3);

    clock.tick(4);

    assertEquals(
        'Each timer should fire in sequence at the correct time.',
        'timer1 at T=1, timer2 at T=2, timer3 at T=3',
        sequence.join(', '));

    clock.uninstall();
  }


  function testCorrectArgumentsPassedToCallback() {
    var clock = new goog.testing.MockClock(true);
    var timeoutId;
    var timeoutExecuted = false;

    timeoutId = setTimeout(function(arg) {
      assertEquals('"this" must be goog.global',
          goog.global, this);
      assertEquals('The timeout ID must be the first parameter',
          timeoutId, arg);
      assertEquals('Exactly one argument must be passed',
          1, arguments.length);
      timeoutExecuted = true;
    }, 1);

    clock.tick(4);

    assertTrue('The timeout was not executed', timeoutExecuted);

    clock.uninstall();
  }


  function testTickZero() {
    var clock = new goog.testing.MockClock(true);
    var calls = 0;

    setTimeout(function() {
      assertEquals('I need to be first', 0, calls);
      calls++;
    }, 0);

    setTimeout(function() {
      assertEquals('I need to be second', 1, calls);
      calls++;
    }, 0);

    clock.tick(0);
    assertEquals(2, calls);

    setTimeout(function() {
      assertEquals('I need to be third', 2, calls);
      calls++;
    }, 0);

    clock.tick(0);
    assertEquals(3, calls);

    assertEquals('Time should still be zero', 0, goog.now());

    clock.uninstall();
  }


  function testReset() {
    var clock = new goog.testing.MockClock(true);

    setTimeout(function() {
      fail('Timeouts should be cleared after a reset');
    }, 0);

    clock.reset();
    clock.tick(999999);
    clock.uninstall();
  }


  function testQueueInsertionHelper() {
    var queue = [];

    function queueToString() {
      var buffer = [];
      for (var i = 0; i < queue.length; i++) {
        buffer.push(queue[i].runAtMillis);
      }
      return buffer.join(',');
    }

    goog.testing.MockClock.insert_({runAtMillis: 2}, queue);
    assertEquals('Only item',
        '2', queueToString());

    goog.testing.MockClock.insert_({runAtMillis: 4}, queue);
    assertEquals('Biggest item',
        '4,2', queueToString());

    goog.testing.MockClock.insert_({runAtMillis: 5}, queue);
    assertEquals('An even bigger item',
        '5,4,2', queueToString());

    goog.testing.MockClock.insert_({runAtMillis: 1}, queue);
    assertEquals('Smallest item',
        '5,4,2,1', queueToString());

    goog.testing.MockClock.insert_({runAtMillis: 1, dup: true}, queue);
    assertEquals('Duplicate smallest item',
        '5,4,2,1,1', queueToString());
    assertTrue('Duplicate item comes at a smaller index', queue[3].dup);

    goog.testing.MockClock.insert_({runAtMillis: 3}, queue);
    goog.testing.MockClock.insert_({runAtMillis: 3, dup:true}, queue);
    assertEquals('Duplicate a middle item',
        '5,4,3,3,2,1,1', queueToString());
    assertTrue('Duplicate item comes at a smaller index', queue[2].dup);
  }

  function testIsTimeoutSet() {
    var clock = new goog.testing.MockClock(true);
    var timeoutKey = setTimeout(function(){}, 1);
    assertTrue('Timeout ' + timeoutKey + ' should be set',
        clock.isTimeoutSet(timeoutKey));
    var nextTimeoutKey = timeoutKey + 1;
    assertFalse('Timeout ' + nextTimeoutKey + ' should not be set',
        clock.isTimeoutSet(nextTimeoutKey));
    clearTimeout(timeoutKey);
    assertFalse('Timeout ' + timeoutKey + ' should no longer be set',
        clock.isTimeoutSet(timeoutKey));
    var newTimeoutKey = setTimeout(function(){}, 1);
    clock.tick(5);
    assertFalse('Timeout ' + timeoutKey + ' should not be set',
        clock.isTimeoutSet(timeoutKey));
    assertTrue('Timeout ' + newTimeoutKey + ' should be set',
        clock.isTimeoutSet(newTimeoutKey));
    clock.uninstall();
  }

  function testBalksOnTimeoutsGreaterThanMaxInt() {
    // Browsers have trouble with timeout greater than max int, so we
    // want Mock Clock to fail if this happens.
    var clock = new goog.testing.MockClock(true);
    // Functions on window don't seem to be able to throw exceptions in
    // IE6.  Explicitly reading the property makes it work.
    var setTimeout = window.setTimeout;
    assertThrows('Timeouts > MAX_INT should fail',
        function() {
           setTimeout(goog.nullFunction, 2147483648);
        });
    assertThrows('Timeouts much greater than MAX_INT should fail',
        function() {
          setTimeout(goog.nullFunction, 2147483648 * 10);
        });
    clock.uninstall();
  }

  function testCorrectSetTimeoutIsRestored() {
    var safe = goog.functions.error('should not have been called');
    stubs.set(window, 'setTimeout', safe);

    var clock = new goog.testing.MockClock(true);
    assertNotEquals('setTimeout is replaced', safe, window.setTimeout);
    clock.uninstall();
    // NOTE: If this assertion proves to be flaky in IE, the string value of
    // the two functions have to be compared as described in
    // goog.testing.TestCase#finalize.
    assertEquals('setTimeout is restored', safe, window.setTimeout);
  }

  function testMozRequestAnimationFrame() {
    // Setting this function will indirectly tell the mock clock to mock it out.
    stubs.set(window, 'mozRequestAnimationFrame', goog.nullFunction);

    var clock = new goog.testing.MockClock(true);

    var mozBeforePaint = goog.testing.recordFunction();
    goog.events.listen(window, 'MozBeforePaint', mozBeforePaint);

    window.mozRequestAnimationFrame(null);
    assertEquals(0, mozBeforePaint.getCallCount());

    clock.tick(goog.testing.MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT);
    assertEquals(1, mozBeforePaint.getCallCount());
    clock.dispose();
  }

  function testClearBeforeSet() {
    var clock = new goog.testing.MockClock(true);
    var expectedId = 1;
    window.clearTimeout(expectedId);

    var fn = goog.testing.recordFunction();
    var actualId = window.setTimeout(fn, 0);
    assertEquals(
        'In order for this test to work, we have to guess the ids in advance',
        expectedId, actualId);
    clock.tick(1);
    assertEquals(1, fn.getCallCount());
    clock.dispose();
  }

</script>
</body>
</html>