|
390 | 390 | var counter = 0;
|
391 | 391 | var incr = function(){ counter++; };
|
392 | 392 | var throttledIncr = _.throttle(incr, 100);
|
393 |
| - var origNowFunc = _.now; |
| 393 | + var originalNowFunc = Date.now; |
| 394 | + var originalGetTimeFunc = Date.prototype.getTime; |
394 | 395 |
|
395 | 396 | throttledIncr();
|
396 |
| - assert.strictEqual(counter, 1); |
397 |
| - _.now = function() { |
398 |
| - return new Date(2013, 0, 1, 1, 1, 1); |
399 |
| - }; |
| 397 | + assert.strictEqual(counter, 1, 'incr was called immediately'); |
| 398 | + |
| 399 | + Date.prototype.getTime = function() { |
| 400 | + return +(new Date(2013, 0, 1, 1, 1, 1)); |
| 401 | + } |
| 402 | + Date.now = function() { |
| 403 | + return +(new Date(2013, 0, 1, 1, 1, 1)); |
| 404 | + } |
400 | 405 |
|
401 | 406 | _.delay(function() {
|
402 | 407 | throttledIncr();
|
403 |
| - assert.strictEqual(counter, 2); |
| 408 | + assert.strictEqual(counter, 2, 'incr was throttled successfully, with tampered system time'); |
404 | 409 | done();
|
405 |
| - _.now = origNowFunc; |
| 410 | + Date.now = originalNowFunc; |
| 411 | + Date.prototype.getTime = originalGetTimeFunc; |
406 | 412 | }, 200);
|
407 | 413 | });
|
408 | 414 |
|
| 415 | + QUnit.test('throttle continues to function after system time is not accessible (or in invalid format)', function(assert) { |
| 416 | + assert.expect(3); |
| 417 | + var done = assert.async(); |
| 418 | + var counter = 0; |
| 419 | + var incr = function(){ counter++; }; |
| 420 | + var throttledIncr = _.throttle(incr, 100); |
| 421 | + var originalNowFunc = Date.now; |
| 422 | + var originalGetTimeFunc = Date.prototype.getTime; |
| 423 | + var originalValueOfFunc = Date.prototype.valueOf; |
| 424 | + |
| 425 | + throttledIncr(); |
| 426 | + assert.strictEqual(counter, 1, 'incr was called immediately'); |
| 427 | + |
| 428 | + Date.prototype.valueOf = function() { |
| 429 | + return null; |
| 430 | + } |
| 431 | + Date.prototype.getTime = function() { |
| 432 | + return null; |
| 433 | + } |
| 434 | + Date.now = function() { |
| 435 | + return null; |
| 436 | + } |
| 437 | + |
| 438 | + _.delay(function() { |
| 439 | + throttledIncr(); |
| 440 | + assert.strictEqual(counter, 2, 'incr was throttled successfully, with tampered system time'); |
| 441 | + Date.now = originalNowFunc; |
| 442 | + Date.prototype.getTime = originalGetTimeFunc; |
| 443 | + Date.prototype.valueOf = originalValueOfFunc; |
| 444 | + }, 200); |
| 445 | + |
| 446 | + _.delay(function() { |
| 447 | + throttledIncr(); |
| 448 | + assert.strictEqual(counter, 3, 'incr was throttled successfully, after system time method restoration'); |
| 449 | + done(); |
| 450 | + }, 400); |
| 451 | + }); |
| 452 | + |
409 | 453 | QUnit.test('throttle re-entrant', function(assert) {
|
410 | 454 | assert.expect(2);
|
411 | 455 | var done = assert.async();
|
|
542 | 586 | assert.expect(2);
|
543 | 587 | var done = assert.async();
|
544 | 588 | var counter = 0;
|
545 |
| - var origNowFunc = _.now; |
546 | 589 | var debouncedIncr = _.debounce(function(){
|
547 | 590 | counter++;
|
548 | 591 | }, 100, true);
|
| 592 | + var originalNowFunc = Date.now; |
| 593 | + var originalGetTimeFunc = Date.prototype.getTime; |
549 | 594 |
|
550 | 595 | debouncedIncr();
|
551 | 596 | assert.strictEqual(counter, 1, 'incr was called immediately');
|
552 | 597 |
|
553 |
| - _.now = function() { |
554 |
| - return new Date(2013, 0, 1, 1, 1, 1); |
555 |
| - }; |
| 598 | + Date.prototype.getTime = function() { |
| 599 | + return +(new Date(2013, 0, 1, 1, 1, 1)); |
| 600 | + } |
| 601 | + Date.now = function() { |
| 602 | + return +(new Date(2013, 0, 1, 1, 1, 1)); |
| 603 | + } |
556 | 604 |
|
557 | 605 | _.delay(function() {
|
558 | 606 | debouncedIncr();
|
559 |
| - assert.strictEqual(counter, 2, 'incr was debounced successfully'); |
| 607 | + assert.strictEqual(counter, 2, 'incr was debounced successfully, with tampered system time'); |
560 | 608 | done();
|
561 |
| - _.now = origNowFunc; |
| 609 | + Date.now = originalNowFunc; |
| 610 | + Date.prototype.getTime = originalGetTimeFunc; |
562 | 611 | }, 200);
|
563 | 612 | });
|
564 | 613 |
|
| 614 | + QUnit.test('debounce after system time is is not accessible (or in invalid format)', function(assert) { |
| 615 | + assert.expect(3); |
| 616 | + var done = assert.async(); |
| 617 | + var counter = 0; |
| 618 | + var debouncedIncr = _.debounce(function(){ |
| 619 | + counter++; |
| 620 | + }, 100, true); |
| 621 | + var originalNowFunc = Date.now; |
| 622 | + var originalGetTimeFunc = Date.prototype.getTime; |
| 623 | + var originalValueOfFunc = Date.prototype.valueOf; |
| 624 | + |
| 625 | + debouncedIncr(); |
| 626 | + assert.strictEqual(counter, 1, 'incr was called immediately'); |
| 627 | + |
| 628 | + Date.prototype.valueOf = function() { |
| 629 | + return null; |
| 630 | + }; |
| 631 | + Date.prototype.getTime = function() { |
| 632 | + return null; |
| 633 | + }; |
| 634 | + Date.now = function() { |
| 635 | + return null; |
| 636 | + }; |
| 637 | + |
| 638 | + _.delay(function() { |
| 639 | + debouncedIncr(); |
| 640 | + assert.strictEqual(counter, 2, 'incr was debounced successfully, with tampered system time'); |
| 641 | + Date.now = originalNowFunc; |
| 642 | + Date.prototype.getTime = originalGetTimeFunc; |
| 643 | + Date.prototype.valueOf = originalValueOfFunc; |
| 644 | + }, 200); |
| 645 | + |
| 646 | + _.delay(function() { |
| 647 | + debouncedIncr(); |
| 648 | + assert.strictEqual(counter, 3, 'incr was debounced successfully, after system time method restoration'); |
| 649 | + done(); |
| 650 | + }, 400); |
| 651 | + }); |
| 652 | + |
565 | 653 | QUnit.test('debounce re-entrant', function(assert) {
|
566 | 654 | assert.expect(2);
|
567 | 655 | var done = assert.async();
|
|
0 commit comments