OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Test fixture for utility.js. | 6 * Test fixture for utility.js. |
7 * @constructor | 7 * @constructor |
8 * @extends {testing.Test} | 8 * @extends {testing.Test} |
9 */ | 9 */ |
10 function GoogleNowUtilityUnitTest () { | 10 function GoogleNowUtilityUnitTest () { |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 this.mockLocalFunctions.expects(once()).callback(); | 421 this.mockLocalFunctions.expects(once()).callback(); |
422 | 422 |
423 // Invocation. | 423 // Invocation. |
424 wrappedCallback(); | 424 wrappedCallback(); |
425 | 425 |
426 // Step 4. Firing runtime.onSuspend event. | 426 // Step 4. Firing runtime.onSuspend event. |
427 assertTrue(onSuspendHandlerContainer.length == 1, | 427 assertTrue(onSuspendHandlerContainer.length == 1, |
428 'onSuspendHandlerContainer.length must be 1'); | 428 'onSuspendHandlerContainer.length must be 1'); |
429 onSuspendHandlerContainer[0](); | 429 onSuspendHandlerContainer[0](); |
430 }); | 430 }); |
431 | |
432 var taskNameA = 'TASK A'; | |
433 var taskNameB = 'TASK B'; | |
434 var taskNameC = 'TASK C'; | |
435 | |
436 function areTasksConflicting(newTaskName, scheduledTaskName) { | |
437 // Task B is conflicting with Task A. This means that if Task B is added when | |
438 // Task A is running, Task B will be ignored (but not vice versa). No other | |
439 // pair is conflicting. | |
440 return newTaskName == taskNameB && scheduledTaskName == taskNameA; | |
441 } | |
442 | |
443 function setUpTaskManagerTest(fixture) { | |
444 // We want to mock wrapper using makeAndRegisterMockApis(), which requires | |
445 // the mocked functions to not exist as a precondition. Resetting 'wrapper' to | |
446 // 'undefined'. | |
447 wrapper = undefined; | |
448 | |
449 fixture.makeAndRegisterMockApis([ | |
450 'wrapper.checkInWrappedCallback', | |
451 'wrapper.registerWrapperPluginFactory', | |
452 'wrapper.debugGetStateString' | |
453 ]); | |
454 fixture.makeMockLocalFunctions(['task1', 'task2', 'task3']); | |
455 fixture.makeAndRegisterMockGlobals(['reportError']); | |
456 | |
457 fixture.mockApis.stubs().wrapper_checkInWrappedCallback(); | |
458 fixture.mockApis.stubs().wrapper_debugGetStateString(). | |
459 will(returnValue('testWrapperDebugState')); | |
460 | |
461 var registerWrapperPluginFactorySavedArgs = new SaveMockArguments(); | |
462 fixture.mockApis.expects(once()).wrapper_registerWrapperPluginFactory( | |
463 registerWrapperPluginFactorySavedArgs.match(ANYTHING)); | |
464 var tasks = buildTaskManager(areTasksConflicting); | |
465 Mock4JS.verifyAllMocks(); | |
466 | |
467 return { | |
468 tasks: tasks, | |
469 pluginFactory: registerWrapperPluginFactorySavedArgs.arguments[0] | |
470 }; | |
471 } | |
472 | |
473 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager2Sequential', function() { | |
474 // Tests that 2 tasks get successfully executed consequentially, even if the | |
skare_
2013/09/05 19:50:34
consequentially -> consecutively?
vadimt
2013/09/05 21:32:13
Done.
| |
475 // second one conflicts with the first. | |
476 | |
477 // Setup. | |
478 var test = setUpTaskManagerTest(this); | |
479 | |
480 // Step 1. Adding 1st task that doesn't create pending callbacks. | |
skare_
2013/09/05 19:50:34
tiny nit, optional: 1st -> first, 2nd -> second be
vadimt
2013/09/05 21:32:13
Yeah, but since I use numbers in task names, I'd p
| |
481 // Expectations. | |
482 this.mockLocalFunctions.expects(once()).task1(ANYTHING); | |
483 // Invocation. | |
484 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
485 Mock4JS.verifyAllMocks(); | |
486 | |
487 // Step 2. Adding 2nd task. | |
488 // Expectations. | |
489 this.mockLocalFunctions.expects(once()).task2(ANYTHING); | |
490 // Invocation. | |
491 test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2); | |
492 }); | |
493 | |
494 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerConflicting', function() { | |
495 // Tests that adding a task while a conflicting task is being executed, causes | |
skare_
2013/09/05 19:50:34
tiny nit: no comma after "executed"
vadimt
2013/09/05 21:32:13
Done.
| |
496 // the second one to be ignored. | |
497 | |
498 // Setup. | |
499 var test = setUpTaskManagerTest(this); | |
500 var task1PluginInstance; | |
501 | |
502 // Step 1. Adding 1st task that creates a pending callback. | |
503 // Expectations. | |
504 this.mockLocalFunctions.expects(once()).task1(ANYTHING). | |
505 will(callFunction(function() { | |
506 task1PluginInstance = test.pluginFactory(); | |
507 })); | |
508 // Invocation. | |
509 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
510 Mock4JS.verifyAllMocks(); | |
511 | |
512 // Step 2. Adding 2nd task. Since it conflicts with currently running task1 | |
513 // (see areTasksConflicting), it should be ignored. | |
514 test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2); | |
515 Mock4JS.verifyAllMocks(); | |
516 | |
517 // Step 3. Entering the callback of task1. | |
518 task1PluginInstance.prologue(); | |
519 Mock4JS.verifyAllMocks(); | |
520 | |
521 // Step 4. Leaving the callback of task1. | |
522 task1PluginInstance.epilogue(); | |
523 }); | |
524 | |
525 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerNestedTaskEnqueue', function() { | |
526 // Tests that adding a task while a non-conflicting task is being executed, | |
skare_
2013/09/05 19:50:34
no , after executed
vadimt
2013/09/05 21:32:13
Done.
| |
527 // causes the second one to be executed after the first one completes. | |
528 | |
529 // Setup. | |
530 var test = setUpTaskManagerTest(this); | |
531 var task1PluginInstance; | |
532 | |
533 // Step 1. Adding 1st task that creates a pending callback. | |
534 // Expectations. | |
535 this.mockLocalFunctions.expects(once()).task1(ANYTHING). | |
536 will(callFunction(function() { | |
537 task1PluginInstance = test.pluginFactory(); | |
538 })); | |
539 // Invocation. | |
540 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
541 Mock4JS.verifyAllMocks(); | |
542 | |
543 // Step 2. Adding 2nd task. Since it doesn't conflict with currently running | |
544 // task1 (see areTasksConflicting), it should not be ignored. | |
545 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2); | |
546 Mock4JS.verifyAllMocks(); | |
547 | |
548 // Step 3. Entering the callback of task1. | |
549 task1PluginInstance.prologue(); | |
550 Mock4JS.verifyAllMocks(); | |
551 | |
552 // Step 4. Leaving the callback of task1. | |
553 // Expectations. | |
554 this.mockLocalFunctions.expects(once()).task2(ANYTHING); | |
555 // Invocation. | |
556 task1PluginInstance.epilogue(); | |
557 }); | |
558 | |
559 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerBranching', function() { | |
560 // Tests that task manager correctly detects completion of tasks that create | |
561 // branching chains of callbacks (in this test, task1 creates pending | |
562 // callbacks 1 and 2, and callback 1 creates pending callback 3). | |
563 | |
564 // Setup. | |
565 var test = setUpTaskManagerTest(this); | |
566 var task1PluginInstance1, task1PluginInstance2, task1PluginInstance3; | |
567 | |
568 // Step 1. Adding 1st task that creates a 2 pending callbacks. | |
skare_
2013/09/05 19:50:34
(through file) unify verb forms/moods. Adding -> a
vadimt
2013/09/05 21:32:13
Done.
| |
569 // Expectations. | |
570 this.mockLocalFunctions.expects(once()).task1(ANYTHING). | |
571 will(callFunction(function() { | |
572 task1PluginInstance1 = test.pluginFactory(); | |
573 task1PluginInstance2 = test.pluginFactory(); | |
574 })); | |
575 // Invocation. | |
576 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
577 Mock4JS.verifyAllMocks(); | |
578 | |
579 // Step 2. Adding 2nd task, which is not conflicting (see areTasksConflicting) | |
580 // with task1. | |
581 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2); | |
582 Mock4JS.verifyAllMocks(); | |
583 | |
584 // Step 3. Entering callback 1, create pending callback 3, exit callback 1. | |
585 // Enter/exit callback 2. Enter callback 3. | |
586 task1PluginInstance1.prologue(); | |
587 task1PluginInstance3 = test.pluginFactory(); | |
588 task1PluginInstance1.epilogue(); | |
589 task1PluginInstance2.prologue(); | |
590 task1PluginInstance2.epilogue(); | |
591 task1PluginInstance3.prologue(); | |
592 Mock4JS.verifyAllMocks(); | |
593 | |
594 // Step 4. Leaving 3rd callback of task1. Now task1 is complete, and task2 | |
595 // should start. | |
596 // Expectations. | |
597 this.mockLocalFunctions.expects(once()).task2(ANYTHING); | |
598 // Invocation. | |
599 task1PluginInstance3.epilogue(); | |
600 }); | |
601 | |
602 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerSuspendError', function() { | |
603 // Tests that task manager's onSuspend method reports an error if there are | |
604 // pending tasks. | |
605 | |
606 // Setup. | |
607 var test = setUpTaskManagerTest(this); | |
608 var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend'); | |
609 | |
610 // Step 1. Adding a task that creates a pending callback. | |
611 // Expectations. | |
612 this.mockLocalFunctions.expects(once()).task1(ANYTHING). | |
613 will(callFunction(function() { | |
614 test.pluginFactory(); | |
615 })); | |
616 // Invocation. | |
617 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
618 Mock4JS.verifyAllMocks(); | |
619 | |
620 // Step 2. Invoke onSuspend event of the task manager. | |
621 // Setup and expectations. The 2 callbacks in onSuspendHandlerContainer are | |
622 // from the wrapper and the task manager. | |
623 assertTrue(onSuspendHandlerContainer.length == 2, | |
624 'onSuspendHandlerContainer.length must be 2'); | |
625 this.mockGlobals.expects(once()).reportError(eqToString( | |
626 'Error: ASSERT: Incomplete task when unloading event page,' + | |
627 ' queue = [{"name":"TASK A"}], testWrapperDebugState')); | |
628 // Invocation. | |
629 onSuspendHandlerContainer[1](); | |
630 }); | |
631 | |
632 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerSuspendSuccess', function() { | |
633 // Tests that task manager's onSuspend method does not reports an error if all | |
skare_
2013/09/05 19:50:34
report an error
vadimt
2013/09/05 21:32:13
Done.
| |
634 // tasks completed. | |
635 | |
636 // Setup. | |
637 var test = setUpTaskManagerTest(this); | |
638 var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend'); | |
639 var task1PluginInstance; | |
640 | |
641 // Step 1. Adding a task that creates a pending callback. | |
642 // Expectations. | |
643 this.mockLocalFunctions.expects(once()).task1(ANYTHING). | |
644 will(callFunction(function() { | |
645 task1PluginInstance = test.pluginFactory(); | |
646 })); | |
647 // Invocation. | |
648 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
649 Mock4JS.verifyAllMocks(); | |
650 | |
651 // Step 2. Invoke task's callback and the onSuspend event of the task manager. | |
652 // The 2 callbacks in onSuspendHandlerContainer are from the wrapper and the | |
653 // task manager. | |
654 task1PluginInstance.prologue(); | |
655 task1PluginInstance.epilogue(); | |
656 onSuspendHandlerContainer[1](); | |
657 }); | |
658 | |
659 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager3Tasks', function() { | |
660 // Tests that 3 tasks can be executed too. In particular, that if the second | |
661 // task is a single-step task which execution was originally blocked by task1, | |
662 // unblocking it causes immediate synchronous execution of both tasks 2 and 3. | |
663 | |
664 // Setup. | |
665 var test = setUpTaskManagerTest(this); | |
666 var task1PluginInstance; | |
667 | |
668 // Step 1. Adding 1st task that creates a pending callback. | |
669 // Expectations. | |
670 this.mockLocalFunctions.expects(once()).task1(ANYTHING). | |
671 will(callFunction(function() { | |
672 task1PluginInstance = test.pluginFactory(); | |
673 })); | |
674 // Invocation. | |
675 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
676 Mock4JS.verifyAllMocks(); | |
677 | |
678 // Step 2. Adding 2nd and 3rd tasks, both non-conflicting (see | |
679 // areTasksConflicting) with task1. | |
680 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2); | |
681 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task3); | |
682 Mock4JS.verifyAllMocks(); | |
683 | |
684 // Step 3. Entering the callback of task1. | |
685 task1PluginInstance.prologue(); | |
686 Mock4JS.verifyAllMocks(); | |
687 | |
688 // Step 4. Leaving the callback of task1. | |
689 // Expectations. | |
690 this.mockLocalFunctions.expects(once()).task2(ANYTHING); | |
691 this.mockLocalFunctions.expects(once()).task3(ANYTHING); | |
692 // Invocation. | |
693 task1PluginInstance.epilogue(); | |
694 }); | |
695 | |
696 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerNestedNonTask', function() { | |
697 // Tests callbacks requested while a task is running, but not from a callback | |
698 // belonging to a task, are not counted as a part of the task. | |
699 | |
700 // Setup. | |
701 var test = setUpTaskManagerTest(this); | |
702 var task1PluginInstance; | |
703 | |
704 // Step 1. Adding 1st task that creates a pending callback. | |
705 // Expectations. | |
706 this.mockLocalFunctions.expects(once()).task1(ANYTHING). | |
707 will(callFunction(function() { | |
708 task1PluginInstance = test.pluginFactory(); | |
709 })); | |
710 // Invocation. | |
711 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | |
712 Mock4JS.verifyAllMocks(); | |
713 | |
714 // Step 2. Code that is not a part of the task creates a pending callback. | |
715 test.pluginFactory(); | |
716 Mock4JS.verifyAllMocks(); | |
717 | |
718 // Step 3. Entering the callback of task1. After this, task1 should be | |
719 // finished despite the pending non-task callback. | |
720 task1PluginInstance.prologue(); | |
721 task1PluginInstance.epilogue(); | |
722 Mock4JS.verifyAllMocks(); | |
723 | |
724 // Step 4. Checking that task1 is finished by submitting task2, which should | |
725 // be executed immediately. | |
726 this.mockLocalFunctions.expects(once()).task2(ANYTHING); | |
727 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2); | |
728 }); | |
OLD | NEW |