| 1 |
// Copyright 2007 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.testing.MockClockTest'); |
| 16 |
goog.setTestOnly('goog.testing.MockClockTest'); |
| 17 |
|
| 18 |
goog.require('goog.Promise'); |
| 19 |
goog.require('goog.Timer'); |
| 20 |
goog.require('goog.events'); |
| 21 |
goog.require('goog.functions'); |
| 22 |
goog.require('goog.testing.MockClock'); |
| 23 |
goog.require('goog.testing.PropertyReplacer'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.testing.recordFunction'); |
| 26 |
|
| 27 |
var stubs = new goog.testing.PropertyReplacer(); |
| 28 |
|
| 29 |
function tearDown() { |
| 30 |
stubs.reset(); |
| 31 |
} |
| 32 |
|
| 33 |
function testMockClockWasInstalled() { |
| 34 |
var clock = new goog.testing.MockClock(); |
| 35 |
var originalTimeout = window.setTimeout; |
| 36 |
clock.install(); |
| 37 |
assertNotEquals(window.setTimeout, originalTimeout); |
| 38 |
setTimeout(function() {}, 100); |
| 39 |
assertEquals(1, clock.getTimeoutsMade()); |
| 40 |
setInterval(function() {}, 200); |
| 41 |
assertEquals(2, clock.getTimeoutsMade()); |
| 42 |
clock.uninstall(); |
| 43 |
assertEquals(window.setTimeout, originalTimeout); |
| 44 |
assertNull(clock.replacer_); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
function testSetTimeoutAndTick() { |
| 49 |
var clock = new goog.testing.MockClock(true); |
| 50 |
var m5 = false, m10 = false, m15 = false, m20 = false; |
| 51 |
setTimeout(function() { m5 = true; }, 5); |
| 52 |
setTimeout(function() { m10 = true; }, 10); |
| 53 |
setTimeout(function() { m15 = true; }, 15); |
| 54 |
setTimeout(function() { m20 = true; }, 20); |
| 55 |
assertEquals(4, clock.getTimeoutsMade()); |
| 56 |
|
| 57 |
assertEquals(4, clock.tick(4)); |
| 58 |
assertEquals(4, clock.getCurrentTime()); |
| 59 |
assertEquals(0, clock.getCallbacksTriggered()); |
| 60 |
|
| 61 |
assertFalse(m5); |
| 62 |
assertFalse(m10); |
| 63 |
assertFalse(m15); |
| 64 |
assertFalse(m20); |
| 65 |
|
| 66 |
assertEquals(5, clock.tick(1)); |
| 67 |
assertEquals(5, clock.getCurrentTime()); |
| 68 |
assertEquals(1, clock.getCallbacksTriggered()); |
| 69 |
|
| 70 |
assertTrue('m5 should now be true', m5); |
| 71 |
assertFalse(m10); |
| 72 |
assertFalse(m15); |
| 73 |
assertFalse(m20); |
| 74 |
|
| 75 |
assertEquals(10, clock.tick(5)); |
| 76 |
assertEquals(10, clock.getCurrentTime()); |
| 77 |
assertEquals(2, clock.getCallbacksTriggered()); |
| 78 |
|
| 79 |
assertTrue('m5 should be true', m5); |
| 80 |
assertTrue('m10 should now be true', m10); |
| 81 |
assertFalse(m15); |
| 82 |
assertFalse(m20); |
| 83 |
|
| 84 |
assertEquals(15, clock.tick(5)); |
| 85 |
assertEquals(15, clock.getCurrentTime()); |
| 86 |
assertEquals(3, clock.getCallbacksTriggered()); |
| 87 |
|
| 88 |
assertTrue('m5 should be true', m5); |
| 89 |
assertTrue('m10 should be true', m10); |
| 90 |
assertTrue('m15 should now be true', m15); |
| 91 |
assertFalse(m20); |
| 92 |
|
| 93 |
assertEquals(20, clock.tick(5)); |
| 94 |
assertEquals(20, clock.getCurrentTime()); |
| 95 |
assertEquals(4, clock.getCallbacksTriggered()); |
| 96 |
|
| 97 |
assertTrue('m5 should be true', m5); |
| 98 |
assertTrue('m10 should be true', m10); |
| 99 |
assertTrue('m15 should be true', m15); |
| 100 |
assertTrue('m20 should now be true', m20); |
| 101 |
|
| 102 |
clock.uninstall(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
function testSetImmediateAndTick() { |
| 107 |
var clock = new goog.testing.MockClock(true); |
| 108 |
var tick0 = false; |
| 109 |
var tick1 = false; |
| 110 |
setImmediate(function() { tick0 = true; }); |
| 111 |
setImmediate(function() { tick1 = true; }); |
| 112 |
assertEquals(2, clock.getTimeoutsMade()); |
| 113 |
assertEquals(0, clock.getCallbacksTriggered()); |
| 114 |
|
| 115 |
clock.tick(0); |
| 116 |
assertEquals(2, clock.getCallbacksTriggered()); |
| 117 |
assertTrue(tick0); |
| 118 |
assertTrue(tick1); |
| 119 |
|
| 120 |
clock.uninstall(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
function testSetInterval() { |
| 125 |
var clock = new goog.testing.MockClock(true); |
| 126 |
var times = 0; |
| 127 |
setInterval(function() { times++; }, 100); |
| 128 |
|
| 129 |
clock.tick(500); |
| 130 |
assertEquals(5, clock.getCallbacksTriggered()); |
| 131 |
assertEquals(5, times); |
| 132 |
clock.tick(100); |
| 133 |
assertEquals(6, clock.getCallbacksTriggered()); |
| 134 |
assertEquals(6, times); |
| 135 |
clock.tick(100); |
| 136 |
assertEquals(7, clock.getCallbacksTriggered()); |
| 137 |
assertEquals(7, times); |
| 138 |
clock.tick(50); |
| 139 |
assertEquals(7, clock.getCallbacksTriggered()); |
| 140 |
assertEquals(7, times); |
| 141 |
clock.tick(50); |
| 142 |
assertEquals(8, clock.getCallbacksTriggered()); |
| 143 |
assertEquals(8, times); |
| 144 |
|
| 145 |
clock.uninstall(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
function testRequestAnimationFrame() { |
| 150 |
goog.global.requestAnimationFrame = function() {}; |
| 151 |
var clock = new goog.testing.MockClock(true); |
| 152 |
var times = []; |
| 153 |
var recFunc = goog.testing.recordFunction(function(now) { times.push(now); }); |
| 154 |
goog.global.requestAnimationFrame(recFunc); |
| 155 |
clock.tick(50); |
| 156 |
assertEquals(1, clock.getCallbacksTriggered()); |
| 157 |
assertEquals(1, recFunc.getCallCount()); |
| 158 |
assertEquals(20, times[0]); |
| 159 |
|
| 160 |
goog.global.requestAnimationFrame(recFunc); |
| 161 |
clock.tick(100); |
| 162 |
assertEquals(2, clock.getCallbacksTriggered()); |
| 163 |
assertEquals(2, recFunc.getCallCount()); |
| 164 |
assertEquals(70, times[1]); |
| 165 |
|
| 166 |
clock.uninstall(); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
function testClearTimeout() { |
| 171 |
var clock = new goog.testing.MockClock(true); |
| 172 |
var ran = false; |
| 173 |
var c = setTimeout(function() { ran = true; }, 100); |
| 174 |
clock.tick(50); |
| 175 |
assertFalse(ran); |
| 176 |
clearTimeout(c); |
| 177 |
clock.tick(100); |
| 178 |
assertFalse(ran); |
| 179 |
clock.uninstall(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
function testClearInterval() { |
| 184 |
var clock = new goog.testing.MockClock(true); |
| 185 |
var times = 0; |
| 186 |
var c = setInterval(function() { times++; }, 100); |
| 187 |
|
| 188 |
clock.tick(500); |
| 189 |
assertEquals(5, times); |
| 190 |
clock.tick(100); |
| 191 |
assertEquals(6, times); |
| 192 |
clock.tick(100); |
| 193 |
clearInterval(c); |
| 194 |
assertEquals(7, times); |
| 195 |
clock.tick(50); |
| 196 |
assertEquals(7, times); |
| 197 |
clock.tick(50); |
| 198 |
assertEquals(7, times); |
| 199 |
|
| 200 |
clock.uninstall(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
function testClearInterval2() { |
| 205 |
// Tests that we can clear the interval from inside the function |
| 206 |
var clock = new goog.testing.MockClock(true); |
| 207 |
var times = 0; |
| 208 |
var c = setInterval(function() { |
| 209 |
times++; |
| 210 |
if (times == 6) { |
| 211 |
clearInterval(c); |
| 212 |
} |
| 213 |
}, 100); |
| 214 |
|
| 215 |
clock.tick(500); |
| 216 |
assertEquals(5, times); |
| 217 |
clock.tick(100); |
| 218 |
assertEquals(6, times); |
| 219 |
clock.tick(100); |
| 220 |
assertEquals(6, times); |
| 221 |
clock.tick(50); |
| 222 |
assertEquals(6, times); |
| 223 |
clock.tick(50); |
| 224 |
assertEquals(6, times); |
| 225 |
|
| 226 |
clock.uninstall(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
function testCancelRequestAnimationFrame() { |
| 231 |
goog.global.requestAnimationFrame = function() {}; |
| 232 |
goog.global.cancelRequestAnimationFrame = function() {}; |
| 233 |
var clock = new goog.testing.MockClock(true); |
| 234 |
var ran = false; |
| 235 |
var c = goog.global.requestAnimationFrame(function() { ran = true; }); |
| 236 |
clock.tick(10); |
| 237 |
assertFalse(ran); |
| 238 |
goog.global.cancelRequestAnimationFrame(c); |
| 239 |
clock.tick(20); |
| 240 |
assertFalse(ran); |
| 241 |
clock.uninstall(); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
function testMockGoogNow() { |
| 246 |
assertNotEquals(0, goog.now()); |
| 247 |
var clock = new goog.testing.MockClock(true); |
| 248 |
assertEquals(0, goog.now()); |
| 249 |
clock.tick(50); |
| 250 |
assertEquals(50, goog.now()); |
| 251 |
clock.uninstall(); |
| 252 |
assertNotEquals(50, goog.now()); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
function testTimeoutDelay() { |
| 257 |
var clock = new goog.testing.MockClock(true); |
| 258 |
var m5 = false, m10 = false, m20 = false; |
| 259 |
setTimeout(function() { m5 = true; }, 5); |
| 260 |
setTimeout(function() { m10 = true; }, 10); |
| 261 |
setTimeout(function() { m20 = true; }, 20); |
| 262 |
|
| 263 |
// Fire 3ms early, so m5 fires at t=2 |
| 264 |
clock.setTimeoutDelay(-3); |
| 265 |
clock.tick(1); |
| 266 |
assertFalse(m5); |
| 267 |
assertFalse(m10); |
| 268 |
clock.tick(1); |
| 269 |
assertTrue(m5); |
| 270 |
assertFalse(m10); |
| 271 |
|
| 272 |
// Fire 3ms late, so m10 fires at t=13 |
| 273 |
clock.setTimeoutDelay(3); |
| 274 |
assertEquals(12, clock.tick(10)); |
| 275 |
assertEquals(12, clock.getCurrentTime()); |
| 276 |
assertFalse(m10); |
| 277 |
clock.tick(1); |
| 278 |
assertTrue(m10); |
| 279 |
assertFalse(m20); |
| 280 |
|
| 281 |
// Fire 10ms early, so m20 fires now, since it's after t=10 |
| 282 |
clock.setTimeoutDelay(-10); |
| 283 |
assertFalse(m20); |
| 284 |
assertEquals(14, clock.tick(1)); |
| 285 |
assertEquals(14, clock.getCurrentTime()); |
| 286 |
assertTrue(m20); |
| 287 |
|
| 288 |
clock.uninstall(); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
function testTimerCallbackCanCreateIntermediateTimer() { |
| 293 |
var clock = new goog.testing.MockClock(true); |
| 294 |
var sequence = []; |
| 295 |
|
| 296 |
// Create 3 timers: 1, 2, and 3. Timer 1 should fire at T=1, timer 2 at |
| 297 |
// T=2, and timer 3 at T=3. The catch: Timer 2 is created by the |
| 298 |
// callback within timer 0. |
| 299 |
|
| 300 |
// Testing method: Create a simple string sequencing each timer and at |
| 301 |
// what time it fired. |
| 302 |
|
| 303 |
setTimeout(function() { |
| 304 |
sequence.push('timer1 at T=' + goog.now()); |
| 305 |
setTimeout(function() { sequence.push('timer2 at T=' + goog.now()); }, 1); |
| 306 |
}, 1); |
| 307 |
|
| 308 |
setTimeout(function() { sequence.push('timer3 at T=' + goog.now()); }, 3); |
| 309 |
|
| 310 |
clock.tick(4); |
| 311 |
|
| 312 |
assertEquals( |
| 313 |
'Each timer should fire in sequence at the correct time.', |
| 314 |
'timer1 at T=1, timer2 at T=2, timer3 at T=3', sequence.join(', ')); |
| 315 |
|
| 316 |
clock.uninstall(); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
function testCorrectArgumentsPassedToCallback() { |
| 321 |
var clock = new goog.testing.MockClock(true); |
| 322 |
var timeoutId; |
| 323 |
var timeoutExecuted = false; |
| 324 |
|
| 325 |
timeoutId = setTimeout(function(arg) { |
| 326 |
assertEquals('"this" must be goog.global', goog.global, this); |
| 327 |
assertEquals('The timeout ID must be the first parameter', timeoutId, arg); |
| 328 |
assertEquals('Exactly one argument must be passed', 1, arguments.length); |
| 329 |
timeoutExecuted = true; |
| 330 |
}, 1); |
| 331 |
|
| 332 |
clock.tick(4); |
| 333 |
|
| 334 |
assertTrue('The timeout was not executed', timeoutExecuted); |
| 335 |
|
| 336 |
clock.uninstall(); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
function testTickZero() { |
| 341 |
var clock = new goog.testing.MockClock(true); |
| 342 |
var calls = 0; |
| 343 |
|
| 344 |
setTimeout(function() { |
| 345 |
assertEquals('I need to be first', 0, calls); |
| 346 |
calls++; |
| 347 |
}, 0); |
| 348 |
|
| 349 |
setTimeout(function() { |
| 350 |
assertEquals('I need to be second', 1, calls); |
| 351 |
calls++; |
| 352 |
}, 0); |
| 353 |
|
| 354 |
clock.tick(0); |
| 355 |
assertEquals(2, calls); |
| 356 |
|
| 357 |
setTimeout(function() { |
| 358 |
assertEquals('I need to be third', 2, calls); |
| 359 |
calls++; |
| 360 |
}, 0); |
| 361 |
|
| 362 |
clock.tick(0); |
| 363 |
assertEquals(3, calls); |
| 364 |
|
| 365 |
assertEquals('Time should still be zero', 0, goog.now()); |
| 366 |
|
| 367 |
clock.uninstall(); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
function testReset() { |
| 372 |
var clock = new goog.testing.MockClock(true); |
| 373 |
|
| 374 |
var id = setTimeout(function() { |
| 375 |
fail('Timeouts should be cleared after a reset'); |
| 376 |
}, 0); |
| 377 |
|
| 378 |
clock.reset(); |
| 379 |
clock.tick(999999); |
| 380 |
|
| 381 |
var calls = 0; |
| 382 |
setTimeout(function() { calls++; }, 10); |
| 383 |
clearTimeout(id); |
| 384 |
clock.tick(100); |
| 385 |
assertEquals( |
| 386 |
'New timeout should still run after clearing from before reset', 1, |
| 387 |
calls); |
| 388 |
|
| 389 |
clock.uninstall(); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
function testNewClockWithOldTimeoutId() { |
| 394 |
var clock = new goog.testing.MockClock(true); |
| 395 |
|
| 396 |
var id = setTimeout(function() { |
| 397 |
fail('Timeouts should be cleared after uninstall'); |
| 398 |
}, 0); |
| 399 |
|
| 400 |
clock.uninstall(); |
| 401 |
clock = new goog.testing.MockClock(true); |
| 402 |
|
| 403 |
var calls = 0; |
| 404 |
setTimeout(function() { calls++; }, 10); |
| 405 |
clearTimeout(id); |
| 406 |
clock.tick(100); |
| 407 |
assertEquals( |
| 408 |
'Timeout should still run after cancelling from old clock', 1, calls); |
| 409 |
clock.uninstall(); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
function testQueueInsertionHelper() { |
| 414 |
var queue = []; |
| 415 |
|
| 416 |
function queueToString() { |
| 417 |
var buffer = []; |
| 418 |
for (var i = 0; i < queue.length; i++) { |
| 419 |
buffer.push(queue[i].runAtMillis); |
| 420 |
} |
| 421 |
return buffer.join(','); |
| 422 |
} |
| 423 |
|
| 424 |
goog.testing.MockClock.insert_({runAtMillis: 2}, queue); |
| 425 |
assertEquals('Only item', '2', queueToString()); |
| 426 |
|
| 427 |
goog.testing.MockClock.insert_({runAtMillis: 4}, queue); |
| 428 |
assertEquals('Biggest item', '4,2', queueToString()); |
| 429 |
|
| 430 |
goog.testing.MockClock.insert_({runAtMillis: 5}, queue); |
| 431 |
assertEquals('An even bigger item', '5,4,2', queueToString()); |
| 432 |
|
| 433 |
goog.testing.MockClock.insert_({runAtMillis: 1}, queue); |
| 434 |
assertEquals('Smallest item', '5,4,2,1', queueToString()); |
| 435 |
|
| 436 |
goog.testing.MockClock.insert_({runAtMillis: 1, dup: true}, queue); |
| 437 |
assertEquals('Duplicate smallest item', '5,4,2,1,1', queueToString()); |
| 438 |
assertTrue('Duplicate item comes at a smaller index', queue[3].dup); |
| 439 |
|
| 440 |
goog.testing.MockClock.insert_({runAtMillis: 3}, queue); |
| 441 |
goog.testing.MockClock.insert_({runAtMillis: 3, dup: true}, queue); |
| 442 |
assertEquals('Duplicate a middle item', '5,4,3,3,2,1,1', queueToString()); |
| 443 |
assertTrue('Duplicate item comes at a smaller index', queue[2].dup); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
function testIsTimeoutSet() { |
| 448 |
var clock = new goog.testing.MockClock(true); |
| 449 |
var timeoutKey = setTimeout(function() {}, 1); |
| 450 |
assertTrue( |
| 451 |
'Timeout ' + timeoutKey + ' should be set', |
| 452 |
clock.isTimeoutSet(timeoutKey)); |
| 453 |
var nextTimeoutKey = timeoutKey + 1; |
| 454 |
assertFalse( |
| 455 |
'Timeout ' + nextTimeoutKey + ' should not be set', |
| 456 |
clock.isTimeoutSet(nextTimeoutKey)); |
| 457 |
clearTimeout(timeoutKey); |
| 458 |
assertFalse( |
| 459 |
'Timeout ' + timeoutKey + ' should no longer be set', |
| 460 |
clock.isTimeoutSet(timeoutKey)); |
| 461 |
var newTimeoutKey = setTimeout(function() {}, 1); |
| 462 |
clock.tick(5); |
| 463 |
assertFalse( |
| 464 |
'Timeout ' + timeoutKey + ' should not be set', |
| 465 |
clock.isTimeoutSet(timeoutKey)); |
| 466 |
assertTrue( |
| 467 |
'Timeout ' + newTimeoutKey + ' should be set', |
| 468 |
clock.isTimeoutSet(newTimeoutKey)); |
| 469 |
clock.uninstall(); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
function testBalksOnTimeoutsGreaterThanMaxInt() { |
| 474 |
// Browsers have trouble with timeout greater than max int, so we |
| 475 |
// want Mock Clock to fail if this happens. |
| 476 |
var clock = new goog.testing.MockClock(true); |
| 477 |
// Functions on window don't seem to be able to throw exceptions in |
| 478 |
// IE6. Explicitly reading the property makes it work. |
| 479 |
var setTimeout = window.setTimeout; |
| 480 |
assertThrows('Timeouts > MAX_INT should fail', function() { |
| 481 |
setTimeout(goog.nullFunction, 2147483648); |
| 482 |
}); |
| 483 |
assertThrows('Timeouts much greater than MAX_INT should fail', function() { |
| 484 |
setTimeout(goog.nullFunction, 2147483648 * 10); |
| 485 |
}); |
| 486 |
clock.uninstall(); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
function testCorrectSetTimeoutIsRestored() { |
| 491 |
var safe = goog.functions.error('should not have been called'); |
| 492 |
stubs.set(window, 'setTimeout', safe); |
| 493 |
|
| 494 |
var clock = new goog.testing.MockClock(true); |
| 495 |
assertNotEquals('setTimeout is replaced', safe, window.setTimeout); |
| 496 |
clock.uninstall(); |
| 497 |
// NOTE: If this assertion proves to be flaky in IE, the string value of |
| 498 |
// the two functions have to be compared as described in |
| 499 |
// goog.testing.TestCase#finalize. |
| 500 |
assertEquals('setTimeout is restored', safe, window.setTimeout); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
function testMozRequestAnimationFrame() { |
| 505 |
// Setting this function will indirectly tell the mock clock to mock it out. |
| 506 |
stubs.set(window, 'mozRequestAnimationFrame', goog.nullFunction); |
| 507 |
|
| 508 |
var clock = new goog.testing.MockClock(true); |
| 509 |
|
| 510 |
var mozBeforePaint = goog.testing.recordFunction(); |
| 511 |
goog.events.listen(window, 'MozBeforePaint', mozBeforePaint); |
| 512 |
|
| 513 |
window.mozRequestAnimationFrame(null); |
| 514 |
assertEquals(0, mozBeforePaint.getCallCount()); |
| 515 |
|
| 516 |
clock.tick(goog.testing.MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT); |
| 517 |
assertEquals(1, mozBeforePaint.getCallCount()); |
| 518 |
clock.dispose(); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
function testClearBeforeSet() { |
| 523 |
var clock = new goog.testing.MockClock(true); |
| 524 |
var expectedId = goog.testing.MockClock.nextId; |
| 525 |
window.clearTimeout(expectedId); |
| 526 |
|
| 527 |
var fn = goog.testing.recordFunction(); |
| 528 |
var actualId = window.setTimeout(fn, 0); |
| 529 |
assertEquals( |
| 530 |
'In order for this test to work, we have to guess the ids in advance', |
| 531 |
expectedId, actualId); |
| 532 |
clock.tick(1); |
| 533 |
assertEquals(1, fn.getCallCount()); |
| 534 |
clock.dispose(); |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
function testNonFunctionArguments() { |
| 539 |
var clock = new goog.testing.MockClock(true); |
| 540 |
|
| 541 |
// Unlike normal setTimeout and friends, we only accept functions (not |
| 542 |
// strings, not undefined, etc). Make sure that if we get a non-function, we |
| 543 |
// fail early rather than on the next .tick() operation. |
| 544 |
|
| 545 |
assertThrows('setTimeout with a non-function value should fail', function() { |
| 546 |
window.setTimeout(undefined, 0); |
| 547 |
}); |
| 548 |
clock.tick(1); |
| 549 |
|
| 550 |
assertThrows('setTimeout with a string should fail', function() { |
| 551 |
window.setTimeout('throw new Error("setTimeout string eval!");', 0); |
| 552 |
}); |
| 553 |
clock.tick(1); |
| 554 |
|
| 555 |
clock.dispose(); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
function testUnspecifiedTimeout() { |
| 560 |
var clock = new goog.testing.MockClock(true); |
| 561 |
var m0a = false, m0b = false, m10 = false; |
| 562 |
setTimeout(function() { m0a = true; }); |
| 563 |
setTimeout(function() { m10 = true; }, 10); |
| 564 |
assertEquals(2, clock.getTimeoutsMade()); |
| 565 |
|
| 566 |
assertFalse(m0a); |
| 567 |
assertFalse(m0b); |
| 568 |
assertFalse(m10); |
| 569 |
|
| 570 |
assertEquals(0, clock.tick(0)); |
| 571 |
assertEquals(0, clock.getCurrentTime()); |
| 572 |
|
| 573 |
assertTrue(m0a); |
| 574 |
assertFalse(m0b); |
| 575 |
assertFalse(m10); |
| 576 |
|
| 577 |
setTimeout(function() { m0b = true; }); |
| 578 |
assertEquals(3, clock.getTimeoutsMade()); |
| 579 |
|
| 580 |
assertEquals(0, clock.tick(0)); |
| 581 |
assertEquals(0, clock.getCurrentTime()); |
| 582 |
|
| 583 |
assertTrue(m0a); |
| 584 |
assertTrue(m0b); |
| 585 |
assertFalse(m10); |
| 586 |
|
| 587 |
assertEquals(10, clock.tick(10)); |
| 588 |
assertEquals(10, clock.getCurrentTime()); |
| 589 |
|
| 590 |
assertTrue(m0a); |
| 591 |
assertTrue(m0b); |
| 592 |
assertTrue(m10); |
| 593 |
|
| 594 |
clock.uninstall(); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
function testUnspecifiedInterval() { |
| 599 |
var clock = new goog.testing.MockClock(true); |
| 600 |
var times = 0; |
| 601 |
var handle = setInterval(function() { |
| 602 |
if (++times >= 5) { |
| 603 |
clearInterval(handle); |
| 604 |
} |
| 605 |
}); |
| 606 |
|
| 607 |
clock.tick(0); |
| 608 |
assertEquals(5, times); |
| 609 |
|
| 610 |
clock.uninstall(); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
function testTickPromise() { |
| 615 |
var clock = new goog.testing.MockClock(true); |
| 616 |
|
| 617 |
var p = goog.Promise.resolve('foo'); |
| 618 |
assertEquals('foo', clock.tickPromise(p)); |
| 619 |
|
| 620 |
var rejected = goog.Promise.reject(new Error('failed')); |
| 621 |
var e = assertThrows(function() { clock.tickPromise(rejected); }); |
| 622 |
assertEquals('failed', e.message); |
| 623 |
|
| 624 |
var delayed = goog.Timer.promise(500, 'delayed'); |
| 625 |
e = assertThrows(function() { clock.tickPromise(delayed); }); |
| 626 |
assertEquals( |
| 627 |
'Promise was expected to be resolved after mock clock tick.', e.message); |
| 628 |
assertEquals('delayed', clock.tickPromise(delayed, 500)); |
| 629 |
|
| 630 |
clock.dispose(); |
| 631 |
} |