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