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 774 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 var storageObject = {}; | 785 var storageObject = {}; |
786 storageObject[testAttemptStorageKey] = delaySeconds; | 786 storageObject[testAttemptStorageKey] = delaySeconds; |
787 return storageObject; | 787 return storageObject; |
788 } | 788 } |
789 | 789 |
790 function setupAttemptManagerTest(fixture) { | 790 function setupAttemptManagerTest(fixture) { |
791 Math.random = function() { return testRandomValue; } | 791 Math.random = function() { return testRandomValue; } |
792 | 792 |
793 fixture.makeMockLocalFunctions([ | 793 fixture.makeMockLocalFunctions([ |
794 'attempt', | 794 'attempt', |
795 'planForNextCallback', | |
796 'isRunningCallback' | 795 'isRunningCallback' |
797 ]); | 796 ]); |
798 fixture.makeAndRegisterMockApis([ | 797 fixture.makeAndRegisterMockApis([ |
799 'chrome.alarms.clear', | 798 'chrome.alarms.clear', |
800 'chrome.alarms.create', | 799 'chrome.alarms.create', |
801 'chrome.storage.local.remove', | 800 'chrome.storage.local.remove', |
802 'chrome.storage.local.set', | 801 'chrome.storage.local.set', |
803 'fillFromChromeLocalStorage', | 802 'fillFromChromeLocalStorage', |
804 'instrumented.alarms.get' | 803 'instrumented.alarms.get' |
805 ]); | 804 ]); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 | 900 |
902 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerExponGrowth', function() { | 901 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerExponGrowth', function() { |
903 // Tests that retry time grows exponentially. We don't need to check the case | 902 // Tests that retry time grows exponentially. We don't need to check the case |
904 // of growing more than once, since the object doesn't have state, and the | 903 // of growing more than once, since the object doesn't have state, and the |
905 // test checks all its inputs and outputs of the tested code. | 904 // test checks all its inputs and outputs of the tested code. |
906 | 905 |
907 // Setup. | 906 // Setup. |
908 var test = setupAttemptManagerTest(this); | 907 var test = setupAttemptManagerTest(this); |
909 var testStoredRetryDelay = 433; | 908 var testStoredRetryDelay = 433; |
910 | 909 |
911 // Call planForNext, which prepares next attempt. Current retry time | 910 // Call scheduleRetry, which schedules a retry. |
912 // is less than 1/2 of the maximum delay. | 911 // Current retry time is less than 1/2 of the maximum delay. |
913 // Expectations. | 912 // Expectations. |
914 var expectedRetryDelaySeconds = | 913 var expectedRetryDelaySeconds = |
915 testStoredRetryDelay * 2 * (1 + testRandomValue * 0.2); | 914 testStoredRetryDelay * 2 * (1 + testRandomValue * 0.2); |
916 expectChromeLocalStorageGet( | 915 expectChromeLocalStorageGet( |
917 this, | 916 this, |
918 createTestAttemptStorageEntryRequest(), | 917 createTestAttemptStorageEntryRequest(), |
919 createTestAttemptStorageEntry(testStoredRetryDelay), | 918 createTestAttemptStorageEntry(testStoredRetryDelay), |
920 true); | 919 true); |
921 this.mockApis.expects(once()).chrome_alarms_create( | 920 this.mockApis.expects(once()).chrome_alarms_create( |
922 testAttemptAlarmName, | 921 testAttemptAlarmName, |
923 eqJSON({ | 922 eqJSON({ |
924 delayInMinutes: expectedRetryDelaySeconds / 60, | 923 delayInMinutes: expectedRetryDelaySeconds / 60, |
925 periodInMinutes: testMaximumDelaySeconds / 60})); | 924 periodInMinutes: testMaximumDelaySeconds / 60})); |
926 this.mockApis.expects(once()).chrome_storage_local_set( | 925 this.mockApis.expects(once()).chrome_storage_local_set( |
927 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds))); | 926 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds))); |
928 this.mockLocalFunctions.expects(once()).planForNextCallback(); | |
929 // Invocation. | 927 // Invocation. |
930 test.attempts.planForNext( | 928 var thenCalled = false; |
931 this.mockLocalFunctions.functions().planForNextCallback); | 929 var catchCalled = false; |
| 930 test.attempts.scheduleRetry().then(function(request) { |
| 931 thenCalled = true; |
| 932 }).catch(function(request) { |
| 933 catchCalled = true; |
| 934 }); |
| 935 assertTrue(thenCalled); |
| 936 assertFalse(catchCalled); |
932 }); | 937 }); |
933 | 938 |
934 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() { | 939 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() { |
935 // Tests that retry time stops growing at the maximum value. | 940 // Tests that retry time stops growing at the maximum value. |
936 | 941 |
937 // Setup. | 942 // Setup. |
938 var test = setupAttemptManagerTest(this); | 943 var test = setupAttemptManagerTest(this); |
939 var testStoredRetryDelay = 1500; | 944 var testStoredRetryDelay = 1500; |
940 | 945 |
941 // Call planForNext, which prepares next attempt. Current retry time | 946 // Call scheduleRetry, which schedules a retry. |
942 // is greater than 1/2 of the maximum delay. | 947 // Current retry time is greater than 1/2 of the maximum delay. |
943 // Expectations. | 948 // Expectations. |
944 var expectedRetryDelaySeconds = testMaximumDelaySeconds; | 949 var expectedRetryDelaySeconds = testMaximumDelaySeconds; |
945 expectChromeLocalStorageGet( | 950 expectChromeLocalStorageGet( |
946 this, | 951 this, |
947 createTestAttemptStorageEntryRequest(), | 952 createTestAttemptStorageEntryRequest(), |
948 createTestAttemptStorageEntry(testStoredRetryDelay), | 953 createTestAttemptStorageEntry(testStoredRetryDelay), |
949 true); | 954 true); |
950 this.mockApis.expects(once()).chrome_alarms_create( | 955 this.mockApis.expects(once()).chrome_alarms_create( |
951 testAttemptAlarmName, | 956 testAttemptAlarmName, |
952 eqJSON({ | 957 eqJSON({ |
953 delayInMinutes: expectedRetryDelaySeconds / 60, | 958 delayInMinutes: expectedRetryDelaySeconds / 60, |
954 periodInMinutes: testMaximumDelaySeconds / 60 | 959 periodInMinutes: testMaximumDelaySeconds / 60 |
955 })); | 960 })); |
956 this.mockApis.expects(once()).chrome_storage_local_set( | 961 this.mockApis.expects(once()).chrome_storage_local_set( |
957 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds))); | 962 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds))); |
958 this.mockLocalFunctions.expects(once()).planForNextCallback(); | |
959 // Invocation. | 963 // Invocation. |
960 test.attempts.planForNext( | 964 var thenCalled = false; |
961 this.mockLocalFunctions.functions().planForNextCallback); | 965 var catchCalled = false; |
| 966 test.attempts.scheduleRetry().then(function(request) { |
| 967 thenCalled = true; |
| 968 }).catch(function(request) { |
| 969 catchCalled = true; |
| 970 }); |
| 971 assertTrue(thenCalled); |
| 972 assertFalse(catchCalled); |
962 }); | 973 }); |
963 | 974 |
964 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() { | 975 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() { |
965 // Tests that firing the alarm invokes the attempt. | 976 // Tests that firing the alarm invokes the attempt. |
966 | 977 |
967 // Setup. | 978 // Setup. |
968 var test = setupAttemptManagerTest(this); | 979 var test = setupAttemptManagerTest(this); |
969 var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm'); | 980 var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm'); |
970 assertEquals(1, onAlarmHandlerContainer.length); | 981 assertEquals(1, onAlarmHandlerContainer.length); |
971 | 982 |
972 // Fire the alarm and check that this invokes the attempt callback. | 983 // Fire the alarm and check that this invokes the attempt callback. |
973 // Expectations. | 984 // Expectations. |
974 var alarmsGetSavedArgs = new SaveMockArguments(); | 985 var alarmsGetSavedArgs = new SaveMockArguments(); |
975 this.mockApis.expects(once()). | 986 this.mockApis.expects(once()). |
976 instrumented_alarms_get( | 987 instrumented_alarms_get( |
977 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), | 988 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), |
978 alarmsGetSavedArgs.match(ANYTHING)). | 989 alarmsGetSavedArgs.match(ANYTHING)). |
979 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'})); | 990 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'})); |
980 this.mockLocalFunctions.expects(once()).attempt(); | 991 this.mockLocalFunctions.expects(once()).attempt(); |
981 // Invocation. | 992 // Invocation. |
982 onAlarmHandlerContainer[0]({name: testAttemptAlarmName}); | 993 onAlarmHandlerContainer[0]({name: testAttemptAlarmName}); |
983 }); | 994 }); |
OLD | NEW |