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 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), | 470 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
471 chromeTabsCreateSavedArgs.match(ANYTHING)). | 471 chromeTabsCreateSavedArgs.match(ANYTHING)). |
472 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); | 472 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
473 this.mockApis.expects(once()).chrome_windows_create( | 473 this.mockApis.expects(once()).chrome_windows_create( |
474 eqJSON({url: testActionUrl, focused: true})); | 474 eqJSON({url: testActionUrl, focused: true})); |
475 | 475 |
476 // Invoking the tested function. | 476 // Invoking the tested function. |
477 onNotificationClicked( | 477 onNotificationClicked( |
478 testNotificationId, this.mockLocalFunctions.functions().selector); | 478 testNotificationId, this.mockLocalFunctions.functions().selector); |
479 }); | 479 }); |
| 480 |
| 481 TEST_F( |
| 482 'GoogleNowBackgroundUnitTest', |
| 483 'ShowNotificationCards', |
| 484 function() { |
| 485 // Tests showNotificationCards function. Checks that the function properly |
| 486 // deletes the card that didn't get an update, updates existing card and |
| 487 // creates a new card that previously didn't exist. |
| 488 |
| 489 // Setup and expectations. |
| 490 var existingNotifications = { |
| 491 'SHOULD BE DELETED': 'SOMETHING', |
| 492 'SHOULD BE KEPT': 'SOMETHING' |
| 493 }; |
| 494 |
| 495 var notificationGroups = { |
| 496 TEST_FIELD: 'TEST VALUE' |
| 497 }; |
| 498 |
| 499 var cards = { |
| 500 'SHOULD BE KEPT': [1], |
| 501 'NEW CARD': [2] |
| 502 }; |
| 503 |
| 504 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION'; |
| 505 |
| 506 var expectedUpdatedNotifications = { |
| 507 'SHOULD BE KEPT': 'KEPT CARD NOTIFICATION DATA', |
| 508 'NEW CARD': 'NEW CARD NOTIFICATION DATA' |
| 509 }; |
| 510 |
| 511 this.makeAndRegisterMockApis([ |
| 512 'cardSet.update', |
| 513 'chrome.storage.local.set', |
| 514 'instrumented.notifications.getAll' |
| 515 ]); |
| 516 this.makeMockLocalFunctions([ |
| 517 'onSuccess' |
| 518 ]); |
| 519 |
| 520 var notificationsGetAllSavedArgs = new SaveMockArguments(); |
| 521 this.mockApis.expects(once()). |
| 522 instrumented_notifications_getAll( |
| 523 notificationsGetAllSavedArgs.match(ANYTHING)). |
| 524 will(invokeCallback( |
| 525 notificationsGetAllSavedArgs, 0, existingNotifications)); |
| 526 |
| 527 this.mockApis.expects(once()). |
| 528 cardSet_update( |
| 529 'SHOULD BE KEPT', |
| 530 [1], |
| 531 eqJSON(notificationGroups), |
| 532 fakeOnCardShownFunction). |
| 533 will(returnValue('KEPT CARD NOTIFICATION DATA')); |
| 534 this.mockApis.expects(once()). |
| 535 cardSet_update( |
| 536 'NEW CARD', |
| 537 [2], |
| 538 eqJSON(notificationGroups), |
| 539 fakeOnCardShownFunction). |
| 540 will(returnValue('NEW CARD NOTIFICATION DATA')); |
| 541 this.mockApis.expects(once()). |
| 542 cardSet_update( |
| 543 'SHOULD BE DELETED', |
| 544 [], |
| 545 eqJSON(notificationGroups), |
| 546 fakeOnCardShownFunction). |
| 547 will(returnValue(undefined)); |
| 548 |
| 549 this.mockApis.expects(once()). |
| 550 chrome_storage_local_set( |
| 551 eqJSON({notificationsData: expectedUpdatedNotifications})); |
| 552 |
| 553 this.mockLocalFunctions.expects(once()). |
| 554 onSuccess(); |
| 555 |
| 556 // Invoking the tested function. |
| 557 showNotificationCards( |
| 558 notificationGroups, |
| 559 cards, |
| 560 this.mockLocalFunctions.functions().onSuccess, |
| 561 fakeOnCardShownFunction); |
| 562 }); |
| 563 |
| 564 TEST_F( |
| 565 'GoogleNowBackgroundUnitTest', |
| 566 'CombineGroup', |
| 567 function() { |
| 568 // Tests combineGroup function. Verifies that both notifications with and |
| 569 // without show time are handled correctly and that cards are correctly |
| 570 // added to existing cards with same ID or start a new combined card. |
| 571 |
| 572 // Setup and expectations. |
| 573 var combinedCards = { |
| 574 'EXISTING CARD': [1] |
| 575 }; |
| 576 |
| 577 var receivedNotificationNoShowTime = { |
| 578 chromeNotificationId: 'EXISTING CARD', |
| 579 trigger: {hideTimeSec: 1} |
| 580 }; |
| 581 var receivedNotificationWithShowTime = { |
| 582 chromeNotificationId: 'NEW CARD', |
| 583 trigger: {showTimeSec: 2, hideTimeSec: 3} |
| 584 } |
| 585 |
| 586 var storedGroup = { |
| 587 cardsTimestamp: 10000, |
| 588 cards: [ |
| 589 receivedNotificationNoShowTime, |
| 590 receivedNotificationWithShowTime |
| 591 ] |
| 592 }; |
| 593 |
| 594 // Invoking the tested function. |
| 595 combineGroup(combinedCards, storedGroup); |
| 596 |
| 597 // Check the output value. |
| 598 var expectedCombinedCards = { |
| 599 'EXISTING CARD': [ |
| 600 1, |
| 601 { |
| 602 receivedNotification: receivedNotificationNoShowTime, |
| 603 hideTime: 11000 |
| 604 } |
| 605 ], |
| 606 'NEW CARD': [ |
| 607 { |
| 608 receivedNotification: receivedNotificationWithShowTime, |
| 609 showTime: 12000, |
| 610 hideTime: 13000 |
| 611 } |
| 612 ] |
| 613 }; |
| 614 |
| 615 assertEquals( |
| 616 JSON.stringify(expectedCombinedCards), |
| 617 JSON.stringify(combinedCards)); |
| 618 }); |
| 619 |
| 620 TEST_F( |
| 621 'GoogleNowBackgroundUnitTest', |
| 622 'CombineAndShowNotificationCards', |
| 623 function() { |
| 624 // Tests combineAndShowNotificationCards function. |
| 625 // The test passes 2 groups to combineAndShowNotificationCards, checks |
| 626 // that it calls combineGroup() for each of these groups and calls |
| 627 // showNotificationCards() with the results of these combineGroup() calls. |
| 628 |
| 629 // Setup and expectations. |
| 630 var testGroups = { |
| 631 'TEST GROUP 1': {testField: 'TEST VALUE 1'}, |
| 632 'TEST GROUP 2': {testField: 'TEST VALUE 2'} |
| 633 }; |
| 634 |
| 635 var fakeOnSuccessFunction = 'FAKE ON SUCCESS FUNCTION'; |
| 636 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION'; |
| 637 |
| 638 this.makeAndRegisterMockGlobals( |
| 639 ['combineGroup', 'showNotificationCards']); |
| 640 |
| 641 var combineGroupSavedArgs = new SaveMockArguments(); |
| 642 this.mockGlobals.expects(once()). |
| 643 combineGroup( |
| 644 combineGroupSavedArgs.match(eqJSON({})), |
| 645 combineGroupSavedArgs.match(eqJSON({testField: 'TEST VALUE 1'}))). |
| 646 will(callFunction(function() { |
| 647 combineGroupSavedArgs.arguments[0].card1 = { |
| 648 testValue: 'TEST CARD VALUE 1' |
| 649 }; |
| 650 })); |
| 651 this.mockGlobals.expects(once()). |
| 652 combineGroup( |
| 653 combineGroupSavedArgs.match( |
| 654 eqJSON({card1: {testValue: 'TEST CARD VALUE 1'}})), |
| 655 combineGroupSavedArgs.match( |
| 656 eqJSON({testField: 'TEST VALUE 2'}))). |
| 657 will(callFunction(function() { |
| 658 combineGroupSavedArgs.arguments[0].card2 = { |
| 659 testValue: 'TEST CARD VALUE 2' |
| 660 }; |
| 661 })); |
| 662 this.mockGlobals.expects(once()). |
| 663 showNotificationCards( |
| 664 eqJSON(testGroups), |
| 665 eqJSON({ |
| 666 card1: {testValue: 'TEST CARD VALUE 1'}, |
| 667 card2: {testValue: 'TEST CARD VALUE 2'} |
| 668 }), |
| 669 fakeOnSuccessFunction, |
| 670 fakeOnCardShownFunction); |
| 671 |
| 672 // Invoking the tested function. |
| 673 combineAndShowNotificationCards( |
| 674 testGroups, fakeOnSuccessFunction, fakeOnCardShownFunction); |
| 675 }); |
| 676 |
| 677 TEST_F( |
| 678 'GoogleNowBackgroundUnitTest', |
| 679 'ProcessServerResponse', |
| 680 function() { |
| 681 // Tests processServerResponse function. |
| 682 |
| 683 // Setup and expectations. |
| 684 Date.now = function() { return 3000000; }; |
| 685 |
| 686 // GROUP1 was requested and contains cards c4 and c5. For c5, there is a |
| 687 // non-expired dismissal, so it will be ignored. |
| 688 // GROUP2 was not requested, but is contained in server response to |
| 689 // indicate that the group still exists. Stored group GROUP2 won't change. |
| 690 // GROUP3 is stored, but is not present in server's response, which means |
| 691 // it doesn't exist anymore. This group will be deleted. |
| 692 // GROUP4 doesn't contain cards, but it was requested. This is treated as |
| 693 // if it had an empty array of cards. Cards in the stored group will be |
| 694 // replaced with an empty array. |
| 695 // GROUP5 doesn't have next poll time, and it will be stored without next |
| 696 // poll time. |
| 697 var serverResponse = { |
| 698 groups: { |
| 699 GROUP1: {requested: true, nextPollSeconds: 46}, |
| 700 GROUP2: {requested: false}, |
| 701 GROUP4: {requested: true, nextPollSeconds: 45}, |
| 702 GROUP5: {requested: true} |
| 703 }, |
| 704 notifications: [ |
| 705 {notificationId: 'c4', groupName: 'GROUP1'}, |
| 706 {notificationId: 'c5', groupName: 'GROUP1'} |
| 707 ] |
| 708 }; |
| 709 |
| 710 var recentDismissals = { |
| 711 c4: 1800000, // expired dismissal |
| 712 c5: 1800001 // non-expired dismissal |
| 713 }; |
| 714 |
| 715 var storedGroups = { |
| 716 GROUP2: { |
| 717 cards: [{notificationId: 'c2'}], |
| 718 cardsTimestamp: 239, |
| 719 nextPollTime: 10000 |
| 720 }, |
| 721 GROUP3: { |
| 722 cards: [{notificationId: 'c3'}], |
| 723 cardsTimestamp: 240, |
| 724 nextPollTime: 10001 |
| 725 }, |
| 726 GROUP4: { |
| 727 cards: [{notificationId: 'c6'}], |
| 728 cardsTimestamp: 241, |
| 729 nextPollTime: 10002 |
| 730 } |
| 731 }; |
| 732 |
| 733 var expectedUpdatedGroups = { |
| 734 GROUP1: { |
| 735 cards: [{notificationId: 'c4', groupName: 'GROUP1'}], |
| 736 cardsTimestamp: 3000000, |
| 737 nextPollTime: 3046000 |
| 738 }, |
| 739 GROUP2: { |
| 740 cards: [{notificationId: 'c2'}], |
| 741 cardsTimestamp: 239, |
| 742 nextPollTime: 10000 |
| 743 }, |
| 744 GROUP4: { |
| 745 cards: [], |
| 746 cardsTimestamp: 3000000, |
| 747 nextPollTime: 3045000 |
| 748 }, |
| 749 GROUP5: { |
| 750 cards: [], |
| 751 cardsTimestamp: 3000000 |
| 752 } |
| 753 }; |
| 754 |
| 755 var expectedUpdatedRecentDismissals = { |
| 756 c5: 1800001 |
| 757 }; |
| 758 |
| 759 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION'; |
| 760 |
| 761 this.makeAndRegisterMockGlobals([ |
| 762 'scheduleNextPoll', |
| 763 'combineAndShowNotificationCards', |
| 764 'recordEvent' |
| 765 ]); |
| 766 |
| 767 this.makeAndRegisterMockApis([ |
| 768 'chrome.storage.local.set', |
| 769 'instrumented.storage.local.get' |
| 770 ]); |
| 771 |
| 772 var storageGetSavedArgs = new SaveMockArguments(); |
| 773 this.mockApis.expects(once()). |
| 774 instrumented_storage_local_get( |
| 775 storageGetSavedArgs.match( |
| 776 eq(['notificationGroups', 'recentDismissals'])), |
| 777 storageGetSavedArgs.match(ANYTHING)). |
| 778 will(invokeCallback( |
| 779 storageGetSavedArgs, |
| 780 1, |
| 781 { |
| 782 notificationGroups: storedGroups, |
| 783 recentDismissals: recentDismissals |
| 784 })); |
| 785 |
| 786 this.mockGlobals.expects(once()). |
| 787 scheduleNextPoll(eqJSON(expectedUpdatedGroups), true); |
| 788 |
| 789 var combineAndShowNotificationCardsSavedArgs = new SaveMockArguments(); |
| 790 this.mockGlobals.expects(once()). |
| 791 combineAndShowNotificationCards( |
| 792 combineAndShowNotificationCardsSavedArgs.match( |
| 793 eqJSON(expectedUpdatedGroups)), |
| 794 combineAndShowNotificationCardsSavedArgs.match( |
| 795 ANYTHING), |
| 796 combineAndShowNotificationCardsSavedArgs.match( |
| 797 eq(fakeOnCardShownFunction))). |
| 798 will(invokeCallback(combineAndShowNotificationCardsSavedArgs, 1)); |
| 799 |
| 800 this.mockApis.expects(once()). |
| 801 chrome_storage_local_set( |
| 802 eqJSON({ |
| 803 notificationGroups: expectedUpdatedGroups, |
| 804 recentDismissals: expectedUpdatedRecentDismissals})); |
| 805 |
| 806 this.mockGlobals.expects(once()). |
| 807 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); |
| 808 |
| 809 // Invoking the tested function. |
| 810 processServerResponse(serverResponse, fakeOnCardShownFunction); |
| 811 }); |
| 812 |
| 813 TEST_F( |
| 814 'GoogleNowBackgroundUnitTest', |
| 815 'ProcessServerResponseGoogleNowDisabled', |
| 816 function() { |
| 817 // Tests processServerResponse function for the case when the response |
| 818 // indicates that Google Now is disabled. |
| 819 |
| 820 // Setup and expectations. |
| 821 var serverResponse = { |
| 822 googleNowDisabled: true, |
| 823 groups: { |
| 824 GROUP1: {nextPollTimeSeconds: 200} |
| 825 } |
| 826 }; |
| 827 |
| 828 var storedGroups = { |
| 829 GROUP2: { |
| 830 cards: [{notificationId: 'c2'}], |
| 831 cardsTimestamp: 239, |
| 832 nextPollTime: 10000 |
| 833 }, |
| 834 GROUP3: { |
| 835 cards: [{notificationId: 'c3'}], |
| 836 cardsTimestamp: 240, |
| 837 nextPollTime: 10001 |
| 838 } |
| 839 }; |
| 840 |
| 841 var expectedUpdatedGroups = { |
| 842 }; |
| 843 |
| 844 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION'; |
| 845 |
| 846 this.makeAndRegisterMockGlobals([ |
| 847 'scheduleNextPoll', |
| 848 'combineAndShowNotificationCards', |
| 849 'recordEvent', |
| 850 'onStateChange' |
| 851 ]); |
| 852 |
| 853 this.makeAndRegisterMockApis([ |
| 854 'chrome.storage.local.set', |
| 855 'instrumented.storage.local.get' |
| 856 ]); |
| 857 |
| 858 this.mockApis.expects(once()). |
| 859 chrome_storage_local_set( |
| 860 eqJSON({googleNowEnabled: false})); |
| 861 |
| 862 this.mockGlobals.expects(once()).onStateChange(); |
| 863 |
| 864 var storageGetSavedArgs = new SaveMockArguments(); |
| 865 this.mockApis.expects(once()). |
| 866 instrumented_storage_local_get( |
| 867 storageGetSavedArgs.match( |
| 868 eq(['notificationGroups', 'recentDismissals'])), |
| 869 storageGetSavedArgs.match(ANYTHING)). |
| 870 will(invokeCallback( |
| 871 storageGetSavedArgs, 1, {notificationGroups: storedGroups})); |
| 872 |
| 873 this.mockGlobals.expects(once()). |
| 874 scheduleNextPoll(eqJSON(expectedUpdatedGroups), false); |
| 875 |
| 876 var combineAndShowNotificationCardsSavedArgs = new SaveMockArguments(); |
| 877 this.mockGlobals.expects(once()). |
| 878 combineAndShowNotificationCards( |
| 879 combineAndShowNotificationCardsSavedArgs.match( |
| 880 eqJSON(expectedUpdatedGroups)), |
| 881 combineAndShowNotificationCardsSavedArgs.match( |
| 882 ANYTHING), |
| 883 combineAndShowNotificationCardsSavedArgs.match( |
| 884 eq(fakeOnCardShownFunction))). |
| 885 will(invokeCallback(combineAndShowNotificationCardsSavedArgs, 1)); |
| 886 |
| 887 this.mockApis.expects(once()). |
| 888 chrome_storage_local_set( |
| 889 eqJSON({ |
| 890 notificationGroups: expectedUpdatedGroups, |
| 891 recentDismissals: {}})); |
| 892 |
| 893 this.mockGlobals.expects(once()). |
| 894 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); |
| 895 |
| 896 // Invoking the tested function. |
| 897 processServerResponse(serverResponse, fakeOnCardShownFunction); |
| 898 }); |
OLD | NEW |