Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: chrome/browser/resources/google_now/utility_unittest.gtestjs

Issue 166033010: Convert Google Now's use of chrome.local.storage.get to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase to r252941 Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 var testAttemptAlarmName = 'attempt-scheduler-testAttempts'; 761 var testAttemptAlarmName = 'attempt-scheduler-testAttempts';
762 var testAttemptStorageKey = 'current-delay-testAttempts'; 762 var testAttemptStorageKey = 'current-delay-testAttempts';
763 var testInitialDelaySeconds = 239; 763 var testInitialDelaySeconds = 239;
764 var testMaximumDelaySeconds = 2239; 764 var testMaximumDelaySeconds = 2239;
765 // Value to be returned by mocked Math.random(). We want the value returned by 765 // Value to be returned by mocked Math.random(). We want the value returned by
766 // Math.random() to be predictable to be able to check results against expected 766 // Math.random() to be predictable to be able to check results against expected
767 // values. A fixed seed would be okay, but fixed seeding isn't possible in JS at 767 // values. A fixed seed would be okay, but fixed seeding isn't possible in JS at
768 // the moment. 768 // the moment.
769 var testRandomValue = 0.31415926; 769 var testRandomValue = 0.31415926;
770 770
771 function createTestAttempStorageEntry(delaySeconds) { 771 /**
772 // Creates a test storage object that attempt manager uses to store current 772 * Creates a default storage current delay object.
773 // delay. 773 */
774 function createTestAttemptStorageEntryRequest() {
775 var storageObject = {};
776 storageObject[testAttemptStorageKey] = testMaximumDelaySeconds;
777 return storageObject;
778 }
779
780 /**
781 * Creates a test storage object that attempt manager uses to store current
782 * delay.
783 */
784 function createTestAttemptStorageEntry(delaySeconds) {
774 var storageObject = {}; 785 var storageObject = {};
775 storageObject[testAttemptStorageKey] = delaySeconds; 786 storageObject[testAttemptStorageKey] = delaySeconds;
776 return storageObject; 787 return storageObject;
777 } 788 }
778 789
779 function setupAttemptManagerTest(fixture) { 790 function setupAttemptManagerTest(fixture) {
780 Math.random = function() { return testRandomValue; } 791 Math.random = function() { return testRandomValue; }
781 792
782 fixture.makeMockLocalFunctions([ 793 fixture.makeMockLocalFunctions([
783 'attempt', 794 'attempt',
784 'planForNextCallback', 795 'planForNextCallback',
785 'isRunningCallback' 796 'isRunningCallback'
786 ]); 797 ]);
787 fixture.makeAndRegisterMockApis([ 798 fixture.makeAndRegisterMockApis([
788 'chrome.alarms.clear', 799 'chrome.alarms.clear',
789 'chrome.alarms.create', 800 'chrome.alarms.create',
790 'chrome.storage.local.remove', 801 'chrome.storage.local.remove',
791 'chrome.storage.local.set', 802 'chrome.storage.local.set',
792 'instrumented.alarms.get', 803 'fillFromChromeLocalStorage',
793 'instrumented.storage.local.get' 804 'instrumented.alarms.get'
794 ]); 805 ]);
795 806
796 var testAttempts = buildAttemptManager( 807 var testAttempts = buildAttemptManager(
797 'testAttempts', 808 'testAttempts',
798 fixture.mockLocalFunctions.functions().attempt, 809 fixture.mockLocalFunctions.functions().attempt,
799 testInitialDelaySeconds, 810 testInitialDelaySeconds,
800 testMaximumDelaySeconds); 811 testMaximumDelaySeconds);
801 Mock4JS.verifyAllMocks(); 812 Mock4JS.verifyAllMocks();
802 813
803 return { 814 return {
(...skipping 25 matching lines...) Expand all
829 // Expectations. 840 // Expectations.
830 var expectedRetryDelaySeconds = 841 var expectedRetryDelaySeconds =
831 testInitialDelaySeconds * (1 + testRandomValue * 0.2); 842 testInitialDelaySeconds * (1 + testRandomValue * 0.2);
832 this.mockApis.expects(once()).chrome_alarms_create( 843 this.mockApis.expects(once()).chrome_alarms_create(
833 testAttemptAlarmName, 844 testAttemptAlarmName,
834 eqJSON({ 845 eqJSON({
835 delayInMinutes: expectedRetryDelaySeconds / 60, 846 delayInMinutes: expectedRetryDelaySeconds / 60,
836 periodInMinutes: testMaximumDelaySeconds / 60 847 periodInMinutes: testMaximumDelaySeconds / 60
837 })); 848 }));
838 this.mockApis.expects(once()).chrome_storage_local_set( 849 this.mockApis.expects(once()).chrome_storage_local_set(
839 eqJSON(createTestAttempStorageEntry(expectedRetryDelaySeconds))); 850 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds)));
840 // Invocation. 851 // Invocation.
841 test.attempts.start(); 852 test.attempts.start();
842 Mock4JS.verifyAllMocks(); 853 Mock4JS.verifyAllMocks();
843 854
844 // Step 3. Check that attempt manager is running. 855 // Step 3. Check that attempt manager is running.
845 // Expectations. 856 // Expectations.
846 alarmsGetSavedArgs = new SaveMockArguments(); 857 alarmsGetSavedArgs = new SaveMockArguments();
847 this.mockApis.expects(once()). 858 this.mockApis.expects(once()).
848 instrumented_alarms_get( 859 instrumented_alarms_get(
849 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), 860 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)),
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 906
896 // Setup. 907 // Setup.
897 var test = setupAttemptManagerTest(this); 908 var test = setupAttemptManagerTest(this);
898 var testStoredRetryDelay = 433; 909 var testStoredRetryDelay = 433;
899 910
900 // Call planForNext, which prepares next attempt. Current retry time 911 // Call planForNext, which prepares next attempt. Current retry time
901 // is less than 1/2 of the maximum delay. 912 // is less than 1/2 of the maximum delay.
902 // Expectations. 913 // Expectations.
903 var expectedRetryDelaySeconds = 914 var expectedRetryDelaySeconds =
904 testStoredRetryDelay * 2 * (1 + testRandomValue * 0.2); 915 testStoredRetryDelay * 2 * (1 + testRandomValue * 0.2);
905 var storageGetSavedArgs = new SaveMockArguments(); 916 expectChromeLocalStorageGet(
906 this.mockApis.expects(once()).instrumented_storage_local_get( 917 this,
907 storageGetSavedArgs.match(eq(testAttemptStorageKey)), 918 createTestAttemptStorageEntryRequest(),
908 storageGetSavedArgs.match(ANYTHING)). 919 createTestAttemptStorageEntry(testStoredRetryDelay));
909 will(invokeCallback(
910 storageGetSavedArgs,
911 1,
912 createTestAttempStorageEntry(testStoredRetryDelay)));
913 this.mockApis.expects(once()).chrome_alarms_create( 920 this.mockApis.expects(once()).chrome_alarms_create(
914 testAttemptAlarmName, 921 testAttemptAlarmName,
915 eqJSON({ 922 eqJSON({
916 delayInMinutes: expectedRetryDelaySeconds / 60, 923 delayInMinutes: expectedRetryDelaySeconds / 60,
917 periodInMinutes: testMaximumDelaySeconds / 60})); 924 periodInMinutes: testMaximumDelaySeconds / 60}));
918 this.mockApis.expects(once()).chrome_storage_local_set( 925 this.mockApis.expects(once()).chrome_storage_local_set(
919 eqJSON(createTestAttempStorageEntry(expectedRetryDelaySeconds))); 926 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds)));
920 this.mockLocalFunctions.expects(once()).planForNextCallback(); 927 this.mockLocalFunctions.expects(once()).planForNextCallback();
921 // Invocation. 928 // Invocation.
922 test.attempts.planForNext( 929 test.attempts.planForNext(
923 this.mockLocalFunctions.functions().planForNextCallback); 930 this.mockLocalFunctions.functions().planForNextCallback);
924 }); 931 });
925 932
926 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() { 933 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() {
927 // Tests that retry time stops growing at the maximum value. 934 // Tests that retry time stops growing at the maximum value.
928 935
929 // Setup. 936 // Setup.
930 var test = setupAttemptManagerTest(this); 937 var test = setupAttemptManagerTest(this);
931 var testStoredRetryDelay = 1500; 938 var testStoredRetryDelay = 1500;
932 939
933 // Call planForNext, which prepares next attempt. Current retry time 940 // Call planForNext, which prepares next attempt. Current retry time
934 // is greater than 1/2 of the maximum delay. 941 // is greater than 1/2 of the maximum delay.
935 // Expectations. 942 // Expectations.
936 var expectedRetryDelaySeconds = testMaximumDelaySeconds; 943 var expectedRetryDelaySeconds = testMaximumDelaySeconds;
937 var storageGetSavedArgs = new SaveMockArguments(); 944 expectChromeLocalStorageGet(
938 this.mockApis.expects(once()). 945 this,
939 instrumented_storage_local_get( 946 createTestAttemptStorageEntryRequest(),
940 storageGetSavedArgs.match(eq(testAttemptStorageKey)), 947 createTestAttemptStorageEntry(testStoredRetryDelay)
941 storageGetSavedArgs.match(ANYTHING)). 948 );
942 will(invokeCallback(
943 storageGetSavedArgs,
944 1,
945 createTestAttempStorageEntry(testStoredRetryDelay)));
946 this.mockApis.expects(once()).chrome_alarms_create( 949 this.mockApis.expects(once()).chrome_alarms_create(
947 testAttemptAlarmName, 950 testAttemptAlarmName,
948 eqJSON({ 951 eqJSON({
949 delayInMinutes: expectedRetryDelaySeconds / 60, 952 delayInMinutes: expectedRetryDelaySeconds / 60,
950 periodInMinutes: testMaximumDelaySeconds / 60 953 periodInMinutes: testMaximumDelaySeconds / 60
951 })); 954 }));
952 this.mockApis.expects(once()).chrome_storage_local_set( 955 this.mockApis.expects(once()).chrome_storage_local_set(
953 eqJSON(createTestAttempStorageEntry(expectedRetryDelaySeconds))); 956 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds)));
954 this.mockLocalFunctions.expects(once()).planForNextCallback(); 957 this.mockLocalFunctions.expects(once()).planForNextCallback();
955 // Invocation. 958 // Invocation.
956 test.attempts.planForNext( 959 test.attempts.planForNext(
957 this.mockLocalFunctions.functions().planForNextCallback); 960 this.mockLocalFunctions.functions().planForNextCallback);
958 }); 961 });
959 962
960 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() { 963 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() {
961 // Tests that firing the alarm invokes the attempt. 964 // Tests that firing the alarm invokes the attempt.
962 965
963 // Setup. 966 // Setup.
964 var test = setupAttemptManagerTest(this); 967 var test = setupAttemptManagerTest(this);
965 var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm'); 968 var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm');
966 assertEquals(1, onAlarmHandlerContainer.length); 969 assertEquals(1, onAlarmHandlerContainer.length);
967 970
968 // Fire the alarm and check that this invokes the attempt callback. 971 // Fire the alarm and check that this invokes the attempt callback.
969 // Expectations. 972 // Expectations.
970 var alarmsGetSavedArgs = new SaveMockArguments(); 973 var alarmsGetSavedArgs = new SaveMockArguments();
971 this.mockApis.expects(once()). 974 this.mockApis.expects(once()).
972 instrumented_alarms_get( 975 instrumented_alarms_get(
973 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), 976 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)),
974 alarmsGetSavedArgs.match(ANYTHING)). 977 alarmsGetSavedArgs.match(ANYTHING)).
975 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'})); 978 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'}));
976 this.mockLocalFunctions.expects(once()).attempt(); 979 this.mockLocalFunctions.expects(once()).attempt();
977 // Invocation. 980 // Invocation.
978 onAlarmHandlerContainer[0]({name: testAttemptAlarmName}); 981 onAlarmHandlerContainer[0]({name: testAttemptAlarmName});
979 }); 982 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698