| 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 // TODO(robliao,vadimt): Determine the granularity of testing to perform. | 5 // TODO(robliao,vadimt): Determine the granularity of testing to perform. |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Test fixture for background.js. | 8 * Test fixture for background.js. |
| 9 * @constructor | 9 * @constructor |
| 10 * @extends {testing.Test} | 10 * @extends {testing.Test} |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 'recordEvent', | 460 'recordEvent', |
| 461 'setBackgroundEnable', | 461 'setBackgroundEnable', |
| 462 'startPollingCards' | 462 'startPollingCards' |
| 463 ]); | 463 ]); |
| 464 fixture.makeAndRegisterMockApis([ | 464 fixture.makeAndRegisterMockApis([ |
| 465 'authenticationManager.isSignedIn', | 465 'authenticationManager.isSignedIn', |
| 466 'chrome.location.clearWatch', | 466 'chrome.location.clearWatch', |
| 467 'chrome.storage.local.remove', | 467 'chrome.storage.local.remove', |
| 468 'instrumented.metricsPrivate.getVariationParams', | 468 'instrumented.metricsPrivate.getVariationParams', |
| 469 'instrumented.notifications.getAll', | 469 'instrumented.notifications.getAll', |
| 470 'instrumented.notifications.getPermissionLevel', |
| 470 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', | 471 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', |
| 471 'instrumented.storage.local.get', | 472 'instrumented.storage.local.get', |
| 472 'tasks.add', | 473 'tasks.add', |
| 473 'updateCardsAttempts.isRunning', | 474 'updateCardsAttempts.isRunning', |
| 474 'updateCardsAttempts.stop' | 475 'updateCardsAttempts.stop' |
| 475 ]); | 476 ]); |
| 476 } | 477 } |
| 477 | 478 |
| 478 /** | 479 /** |
| 479 * Sets up the test to expect the state machine calls and send | 480 * Sets up the test to expect the state machine calls and send |
| 480 * the specified state machine state. Currently used to test initialize(). | 481 * the specified state machine state. Currently used to test initialize(). |
| 481 * Note that this CAN NOT be used if any of the methods below are called | 482 * Note that this CAN NOT be used if any of the methods below are called |
| 482 * outside of this context with the same argument matchers. | 483 * outside of this context with the same argument matchers. |
| 483 * expects() calls cannot be chained with the same argument matchers. | 484 * expects() calls cannot be chained with the same argument matchers. |
| 484 * @param {object} fixture Test fixture. | 485 * @param {object} fixture Test fixture. |
| 485 * @param {string} testIdentityToken getAuthToken callback token. | 486 * @param {string} testIdentityToken getAuthToken callback token. |
| 486 * @param {boolean} testGeolocationPref Geolocation Preference callback value. | 487 * @param {boolean} testGeolocationPref Geolocation Preference callback value. |
| 487 * @param {object} testExperimentVariationParams Response of | 488 * @param {object} testExperimentVariationParams Response of |
| 488 * metricsPrivate.getVariationParams | 489 * metricsPrivate.getVariationParams. |
| 490 * @param {string} testExperimentVariationParams Response of |
| 491 * notifications.getPermissionLevel. |
| 489 */ | 492 */ |
| 490 function expectStateMachineCalls( | 493 function expectStateMachineCalls( |
| 491 fixture, | 494 fixture, |
| 492 testIdentityToken, | 495 testIdentityToken, |
| 493 testGeolocationPref, | 496 testGeolocationPref, |
| 494 testExperimentVariationParams) { | 497 testExperimentVariationParams, |
| 498 testNotificationPermissionLevel) { |
| 495 var authenticationManagerIsSignedInSavedArgs = new SaveMockArguments(); | 499 var authenticationManagerIsSignedInSavedArgs = new SaveMockArguments(); |
| 496 fixture.mockApis.expects(once()). | 500 fixture.mockApis.expects(once()). |
| 497 authenticationManager_isSignedIn( | 501 authenticationManager_isSignedIn( |
| 498 authenticationManagerIsSignedInSavedArgs.match(ANYTHING)). | 502 authenticationManagerIsSignedInSavedArgs.match(ANYTHING)). |
| 499 will(invokeCallback( | 503 will(invokeCallback( |
| 500 authenticationManagerIsSignedInSavedArgs, | 504 authenticationManagerIsSignedInSavedArgs, |
| 501 0, | 505 0, |
| 502 testIdentityToken)); | 506 testIdentityToken)); |
| 503 | 507 |
| 504 var getVariationParamsSavedArgs = new SaveMockArguments(); | 508 var getVariationParamsSavedArgs = new SaveMockArguments(); |
| 505 fixture.mockApis.expects(once()). | 509 fixture.mockApis.expects(once()). |
| 506 instrumented_metricsPrivate_getVariationParams( | 510 instrumented_metricsPrivate_getVariationParams( |
| 507 getVariationParamsSavedArgs.match(ANYTHING), | 511 getVariationParamsSavedArgs.match(ANYTHING), |
| 508 getVariationParamsSavedArgs.match(ANYTHING)). | 512 getVariationParamsSavedArgs.match(ANYTHING)). |
| 509 will(invokeCallback( | 513 will(invokeCallback( |
| 510 getVariationParamsSavedArgs, 1, testExperimentVariationParams)); | 514 getVariationParamsSavedArgs, 1, testExperimentVariationParams)); |
| 511 | 515 |
| 512 var googleGeolocationPrefGetSavedArgs = new SaveMockArguments(); | 516 var googleGeolocationPrefGetSavedArgs = new SaveMockArguments(); |
| 513 fixture.mockApis.expects(once()). | 517 fixture.mockApis.expects(once()). |
| 514 instrumented_preferencesPrivate_googleGeolocationAccessEnabled_get( | 518 instrumented_preferencesPrivate_googleGeolocationAccessEnabled_get( |
| 515 googleGeolocationPrefGetSavedArgs.match(eqJSON({})), | 519 googleGeolocationPrefGetSavedArgs.match(eqJSON({})), |
| 516 googleGeolocationPrefGetSavedArgs.match(ANYTHING)). | 520 googleGeolocationPrefGetSavedArgs.match(ANYTHING)). |
| 517 will(invokeCallback( | 521 will(invokeCallback( |
| 518 googleGeolocationPrefGetSavedArgs, 1, {value: testGeolocationPref})); | 522 googleGeolocationPrefGetSavedArgs, 1, {value: testGeolocationPref})); |
| 519 | 523 |
| 524 var notificationGetPermissionLevelSavedArgs = new SaveMockArguments(); |
| 525 fixture.mockApis.expects(once()). |
| 526 instrumented_notifications_getPermissionLevel( |
| 527 notificationGetPermissionLevelSavedArgs.match(ANYTHING)). |
| 528 will(invokeCallback( |
| 529 notificationGetPermissionLevelSavedArgs, |
| 530 0, |
| 531 testNotificationPermissionLevel)) |
| 532 |
| 520 fixture.mockGlobals.expects(once()). | 533 fixture.mockGlobals.expects(once()). |
| 521 setBackgroundEnable(ANYTHING); | 534 setBackgroundEnable(ANYTHING); |
| 522 } | 535 } |
| 523 | 536 |
| 524 /** | 537 /** |
| 525 * Sets up the test to expect the initialization calls that | 538 * Sets up the test to expect the initialization calls that |
| 526 * initialize() invokes. | 539 * initialize() invokes. |
| 527 * Note that this CAN NOT be used if any of the methods below are called | 540 * Note that this CAN NOT be used if any of the methods below are called |
| 528 * outside of this context with the same argument matchers. | 541 * outside of this context with the same argument matchers. |
| 529 * expects() calls cannot be chained with the same argument matchers. | 542 * expects() calls cannot be chained with the same argument matchers. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 549 'Initialize_ToastStateEmpty', | 562 'Initialize_ToastStateEmpty', |
| 550 function() { | 563 function() { |
| 551 // Tests the case when getAuthToken fails most likely because the user is | 564 // Tests the case when getAuthToken fails most likely because the user is |
| 552 // not signed in. In this case, the function should quietly exit after | 565 // not signed in. In this case, the function should quietly exit after |
| 553 // finding out that getAuthToken fails. | 566 // finding out that getAuthToken fails. |
| 554 | 567 |
| 555 // Setup and expectations. | 568 // Setup and expectations. |
| 556 var testIdentityToken = undefined; | 569 var testIdentityToken = undefined; |
| 557 var testGeolocationPref = false; | 570 var testGeolocationPref = false; |
| 558 var testExperimentVariationParams = {}; | 571 var testExperimentVariationParams = {}; |
| 572 var testNotificationPermissionLevel = 'denied'; |
| 559 | 573 |
| 560 mockInitializeDependencies(this); | 574 mockInitializeDependencies(this); |
| 561 | 575 |
| 562 this.mockGlobals.expects(once()).recordEvent( | 576 this.mockGlobals.expects(once()).recordEvent( |
| 563 GoogleNowEvent.EXTENSION_START); | 577 GoogleNowEvent.EXTENSION_START); |
| 564 | 578 |
| 565 this.mockGlobals.expects(once()).recordEvent( | 579 this.mockGlobals.expects(once()).recordEvent( |
| 566 GoogleNowEvent.STOPPED); | 580 GoogleNowEvent.STOPPED); |
| 567 | 581 |
| 568 expectInitialization(this.mockApis); | 582 expectInitialization(this.mockApis); |
| 569 | 583 |
| 570 expectStateMachineCalls( | 584 expectStateMachineCalls( |
| 571 this, | 585 this, |
| 572 testIdentityToken, | 586 testIdentityToken, |
| 573 testGeolocationPref, | 587 testGeolocationPref, |
| 574 testExperimentVariationParams); | 588 testExperimentVariationParams, |
| 589 testNotificationPermissionLevel); |
| 575 | 590 |
| 576 // Invoking the tested function. | 591 // Invoking the tested function. |
| 577 initialize(); | 592 initialize(); |
| 578 }); | 593 }); |
| 579 | 594 |
| 580 TEST_F( | 595 TEST_F( |
| 581 'GoogleNowBackgroundUnitTest', | 596 'GoogleNowBackgroundUnitTest', |
| 582 'DISABLED_Initialize_ToastStateEmpty2', | 597 'DISABLED_Initialize_ToastStateEmpty2', |
| 583 function() { | 598 function() { |
| 584 // Tests the case when getAuthToken succeeds, and the user has never | 599 // Tests the case when getAuthToken succeeds, and the user has never |
| 585 // responded to the toast. | 600 // responded to the toast. |
| 586 // In this case, the function should invoke showWelcomeToast(). | 601 // In this case, the function should invoke showWelcomeToast(). |
| 587 | 602 |
| 588 // Setup and expectations. | 603 // Setup and expectations. |
| 589 var testIdentityToken = 'some identity token'; | 604 var testIdentityToken = 'some identity token'; |
| 590 var testGeolocationPref = false; | 605 var testGeolocationPref = false; |
| 591 var testExperimentVariationParams = {}; | 606 var testExperimentVariationParams = {}; |
| 607 var testNotificationPermissionLevel = 'denied'; |
| 592 | 608 |
| 593 mockInitializeDependencies(this); | 609 mockInitializeDependencies(this); |
| 594 | 610 |
| 595 this.mockGlobals.expects(once()).recordEvent( | 611 this.mockGlobals.expects(once()).recordEvent( |
| 596 GoogleNowEvent.EXTENSION_START); | 612 GoogleNowEvent.EXTENSION_START); |
| 597 | 613 |
| 598 expectInitialization(this.mockApis); | 614 expectInitialization(this.mockApis); |
| 599 | 615 |
| 600 expectStateMachineCalls( | 616 expectStateMachineCalls( |
| 601 this, | 617 this, |
| 602 testIdentityToken, | 618 testIdentityToken, |
| 603 testGeolocationPref, | 619 testGeolocationPref, |
| 604 testExperimentVariationParams); | 620 testExperimentVariationParams, |
| 621 testNotificationPermissionLevel); |
| 605 | 622 |
| 606 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | 623 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
| 607 this.mockApis.expects(exactly(1)). | 624 this.mockApis.expects(exactly(1)). |
| 608 instrumented_notifications_getAll( | 625 instrumented_notifications_getAll( |
| 609 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | 626 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
| 610 will( | 627 will( |
| 611 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | 628 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
| 612 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | 629 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
| 613 | 630 |
| 614 // Invoking the tested function. | 631 // Invoking the tested function. |
| 615 initialize(); | 632 initialize(); |
| 616 }); | 633 }); |
| 617 | 634 |
| 618 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_RunGoogleNow', function() { | 635 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_RunGoogleNow', function() { |
| 619 // Tests if Google Now will invoke startPollingCards when all | 636 // Tests if Google Now will invoke startPollingCards when all |
| 620 // of the required state is fulfilled. | 637 // of the required state is fulfilled. |
| 621 | 638 |
| 622 // Setup and expectations. | 639 // Setup and expectations. |
| 623 var testIdentityToken = 'some identity token'; | 640 var testIdentityToken = 'some identity token'; |
| 624 var testGeolocationPref = true; | 641 var testGeolocationPref = true; |
| 625 var testExperimentVariationParams = {}; | 642 var testExperimentVariationParams = {}; |
| 643 var testNotificationPermissionLevel = 'granted'; |
| 626 | 644 |
| 627 mockInitializeDependencies(this); | 645 mockInitializeDependencies(this); |
| 628 | 646 |
| 629 this.mockGlobals.expects(once()).recordEvent( | 647 this.mockGlobals.expects(once()).recordEvent( |
| 630 GoogleNowEvent.EXTENSION_START); | 648 GoogleNowEvent.EXTENSION_START); |
| 631 | 649 |
| 632 expectInitialization(this.mockApis); | 650 expectInitialization(this.mockApis); |
| 633 | 651 |
| 634 expectStateMachineCalls( | 652 expectStateMachineCalls( |
| 635 this, | 653 this, |
| 636 testIdentityToken, | 654 testIdentityToken, |
| 637 testGeolocationPref, | 655 testGeolocationPref, |
| 638 testExperimentVariationParams); | 656 testExperimentVariationParams, |
| 657 testNotificationPermissionLevel); |
| 639 | 658 |
| 640 this.mockGlobals.expects(once()).startPollingCards(); | 659 this.mockGlobals.expects(once()).startPollingCards(); |
| 641 | 660 |
| 642 // Invoking the tested function. | 661 // Invoking the tested function. |
| 643 initialize(); | 662 initialize(); |
| 644 }); | 663 }); |
| 645 | 664 |
| 646 TEST_F( | 665 TEST_F( |
| 647 'GoogleNowBackgroundUnitTest', | 666 'GoogleNowBackgroundUnitTest', |
| 648 'DISABLED_Initialize_NoGeolocation', | 667 'DISABLED_Initialize_NoGeolocation', |
| 649 function() { | 668 function() { |
| 650 // Tests the case where everything is in place except for the | 669 // Tests the case where everything is in place except for the |
| 651 // Geolocation Preference after the user responded to the toast. | 670 // Geolocation Preference after the user responded to the toast. |
| 652 | 671 |
| 653 // Setup and expectations. | 672 // Setup and expectations. |
| 654 var testIdentityToken = 'some identity token'; | 673 var testIdentityToken = 'some identity token'; |
| 655 var testGeolocationPref = false; | 674 var testGeolocationPref = false; |
| 656 var testExperimentVariationParams = {}; | 675 var testExperimentVariationParams = {}; |
| 676 var testNotificationPermissionLevel = 'denied'; |
| 657 | 677 |
| 658 mockInitializeDependencies(this); | 678 mockInitializeDependencies(this); |
| 659 | 679 |
| 660 this.mockGlobals.expects(once()).recordEvent( | 680 this.mockGlobals.expects(once()).recordEvent( |
| 661 GoogleNowEvent.EXTENSION_START); | 681 GoogleNowEvent.EXTENSION_START); |
| 662 | 682 |
| 663 this.mockGlobals.expects(once()).recordEvent( | 683 this.mockGlobals.expects(once()).recordEvent( |
| 664 GoogleNowEvent.USER_SUPPRESSED); | 684 GoogleNowEvent.USER_SUPPRESSED); |
| 665 | 685 |
| 666 expectInitialization(this.mockApis); | 686 expectInitialization(this.mockApis); |
| 667 | 687 |
| 668 expectStateMachineCalls( | 688 expectStateMachineCalls( |
| 669 this, | 689 this, |
| 670 testIdentityToken, | 690 testIdentityToken, |
| 671 testGeolocationPref, | 691 testGeolocationPref, |
| 672 testExperimentVariationParams); | 692 testExperimentVariationParams, |
| 693 testNotificationPermissionLevel, |
| 694 testNotificationPermissionLevel); |
| 673 | 695 |
| 674 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | 696 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
| 675 this.mockApis.expects(exactly(2)). | 697 this.mockApis.expects(exactly(2)). |
| 676 instrumented_notifications_getAll( | 698 instrumented_notifications_getAll( |
| 677 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | 699 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
| 678 will( | 700 will( |
| 679 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | 701 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
| 680 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | 702 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
| 681 | 703 |
| 682 // Invoking the tested function. | 704 // Invoking the tested function. |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), | 850 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
| 829 chromeTabsCreateSavedArgs.match(ANYTHING)). | 851 chromeTabsCreateSavedArgs.match(ANYTHING)). |
| 830 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); | 852 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
| 831 this.mockApis.expects(once()).chrome_windows_create( | 853 this.mockApis.expects(once()).chrome_windows_create( |
| 832 eqJSON({url: testActionUrl, focused: true})); | 854 eqJSON({url: testActionUrl, focused: true})); |
| 833 | 855 |
| 834 // Invoking the tested function. | 856 // Invoking the tested function. |
| 835 onNotificationClicked( | 857 onNotificationClicked( |
| 836 testNotificationId, this.mockLocalFunctions.functions().selector); | 858 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 837 }); | 859 }); |
| OLD | NEW |