| 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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 callback('test string', 239). | 200 callback('test string', 239). |
| 201 will(callFunction(function() { | 201 will(callFunction(function() { |
| 202 wrapper.checkInWrappedCallback(); // it should succeed | 202 wrapper.checkInWrappedCallback(); // it should succeed |
| 203 })); | 203 })); |
| 204 | 204 |
| 205 // Invoking tested function. | 205 // Invoking tested function. |
| 206 wrappedCallback('test string', 239); | 206 wrappedCallback('test string', 239); |
| 207 Mock4JS.verifyAllMocks(); | 207 Mock4JS.verifyAllMocks(); |
| 208 | 208 |
| 209 // Step 3. Check that after the callback we are again in non-instrumented | 209 // Step 3. Check that after the callback we are again in non-instrumented |
| 210 // code. | 210 // code. |
| 211 // Expectations. | 211 // Expectations. |
| 212 this.mockGlobals.expects(once()). | 212 this.mockGlobals.expects(once()). |
| 213 buildErrorWithMessageForServer('Not in instrumented callback'). | 213 buildErrorWithMessageForServer('Not in instrumented callback'). |
| 214 will(returnValue(testError)); | 214 will(returnValue(testError)); |
| 215 this.mockGlobals.expects(once()). | 215 this.mockGlobals.expects(once()). |
| 216 reportError(eqJSON(testError)); | 216 reportError(eqJSON(testError)); |
| 217 | 217 |
| 218 // Invocation. | 218 // Invocation. |
| 219 wrapper.checkInWrappedCallback(); | 219 wrapper.checkInWrappedCallback(); |
| 220 | 220 |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 | 498 |
| 499 return { | 499 return { |
| 500 tasks: tasks, | 500 tasks: tasks, |
| 501 pluginFactory: registerWrapperPluginFactorySavedArgs.arguments[0] | 501 pluginFactory: registerWrapperPluginFactorySavedArgs.arguments[0] |
| 502 }; | 502 }; |
| 503 } | 503 } |
| 504 | 504 |
| 505 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager2Sequential', function() { | 505 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager2Sequential', function() { |
| 506 // Tests that 2 tasks get successfully executed consecutively, even if the | 506 // Tests that 2 tasks get successfully executed consecutively, even if the |
| 507 // second one conflicts with the first. | 507 // second one conflicts with the first. |
| 508 | 508 |
| 509 // Setup. | 509 // Setup. |
| 510 var test = setUpTaskManagerTest(this); | 510 var test = setUpTaskManagerTest(this); |
| 511 | 511 |
| 512 // Step 1. Add 1st task that doesn't create pending callbacks. | 512 // Step 1. Add 1st task that doesn't create pending callbacks. |
| 513 // Expectations. | 513 // Expectations. |
| 514 this.mockLocalFunctions.expects(once()).task1(); | 514 this.mockLocalFunctions.expects(once()).task1(); |
| 515 // Invocation. | 515 // Invocation. |
| 516 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); | 516 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); |
| 517 Mock4JS.verifyAllMocks(); | 517 Mock4JS.verifyAllMocks(); |
| 518 | 518 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 | 681 |
| 682 // Step 2. Invoke task's callback and the onSuspend event of the task manager. | 682 // Step 2. Invoke task's callback and the onSuspend event of the task manager. |
| 683 // The 2 callbacks in onSuspendHandlerContainer are from the wrapper and the | 683 // The 2 callbacks in onSuspendHandlerContainer are from the wrapper and the |
| 684 // task manager. | 684 // task manager. |
| 685 task1PluginInstance.prologue(); | 685 task1PluginInstance.prologue(); |
| 686 task1PluginInstance.epilogue(); | 686 task1PluginInstance.epilogue(); |
| 687 onSuspendHandlerContainer[1](); | 687 onSuspendHandlerContainer[1](); |
| 688 }); | 688 }); |
| 689 | 689 |
| 690 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager3Tasks', function() { | 690 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager3Tasks', function() { |
| 691 // Tests that 3 tasks can be executed too. In particular, that if the second | 691 // Tests that 3 tasks can be executed too. In particular, that if the second |
| 692 // task is a single-step task which execution was originally blocked by task1, | 692 // task is a single-step task which execution was originally blocked by task1, |
| 693 // unblocking it causes immediate synchronous execution of both tasks 2 and 3. | 693 // unblocking it causes immediate synchronous execution of both tasks 2 and 3. |
| 694 | 694 |
| 695 // Setup. | 695 // Setup. |
| 696 var test = setUpTaskManagerTest(this); | 696 var test = setUpTaskManagerTest(this); |
| 697 var task1PluginInstance; | 697 var task1PluginInstance; |
| 698 | 698 |
| 699 // Step 1. Add 1st task that creates a pending callback. | 699 // Step 1. Add 1st task that creates a pending callback. |
| 700 // Expectations. | 700 // Expectations. |
| 701 this.mockLocalFunctions.expects(once()).task1(). | 701 this.mockLocalFunctions.expects(once()).task1(). |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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] = undefined; |
| 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 Loading... |
| 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 Loading... |
| 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( | 920 true); |
| 910 storageGetSavedArgs, | |
| 911 1, | |
| 912 createTestAttempStorageEntry(testStoredRetryDelay))); | |
| 913 this.mockApis.expects(once()).chrome_alarms_create( | 921 this.mockApis.expects(once()).chrome_alarms_create( |
| 914 testAttemptAlarmName, | 922 testAttemptAlarmName, |
| 915 eqJSON({ | 923 eqJSON({ |
| 916 delayInMinutes: expectedRetryDelaySeconds / 60, | 924 delayInMinutes: expectedRetryDelaySeconds / 60, |
| 917 periodInMinutes: testMaximumDelaySeconds / 60})); | 925 periodInMinutes: testMaximumDelaySeconds / 60})); |
| 918 this.mockApis.expects(once()).chrome_storage_local_set( | 926 this.mockApis.expects(once()).chrome_storage_local_set( |
| 919 eqJSON(createTestAttempStorageEntry(expectedRetryDelaySeconds))); | 927 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds))); |
| 920 this.mockLocalFunctions.expects(once()).planForNextCallback(); | 928 this.mockLocalFunctions.expects(once()).planForNextCallback(); |
| 921 // Invocation. | 929 // Invocation. |
| 922 test.attempts.planForNext( | 930 test.attempts.planForNext( |
| 923 this.mockLocalFunctions.functions().planForNextCallback); | 931 this.mockLocalFunctions.functions().planForNextCallback); |
| 924 }); | 932 }); |
| 925 | 933 |
| 926 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() { | 934 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() { |
| 927 // Tests that retry time stops growing at the maximum value. | 935 // Tests that retry time stops growing at the maximum value. |
| 928 | 936 |
| 929 // Setup. | 937 // Setup. |
| 930 var test = setupAttemptManagerTest(this); | 938 var test = setupAttemptManagerTest(this); |
| 931 var testStoredRetryDelay = 1500; | 939 var testStoredRetryDelay = 1500; |
| 932 | 940 |
| 933 // Call planForNext, which prepares next attempt. Current retry time | 941 // Call planForNext, which prepares next attempt. Current retry time |
| 934 // is greater than 1/2 of the maximum delay. | 942 // is greater than 1/2 of the maximum delay. |
| 935 // Expectations. | 943 // Expectations. |
| 936 var expectedRetryDelaySeconds = testMaximumDelaySeconds; | 944 var expectedRetryDelaySeconds = testMaximumDelaySeconds; |
| 937 var storageGetSavedArgs = new SaveMockArguments(); | 945 expectChromeLocalStorageGet( |
| 938 this.mockApis.expects(once()). | 946 this, |
| 939 instrumented_storage_local_get( | 947 createTestAttemptStorageEntryRequest(), |
| 940 storageGetSavedArgs.match(eq(testAttemptStorageKey)), | 948 createTestAttemptStorageEntry(testStoredRetryDelay), |
| 941 storageGetSavedArgs.match(ANYTHING)). | 949 true); |
| 942 will(invokeCallback( | |
| 943 storageGetSavedArgs, | |
| 944 1, | |
| 945 createTestAttempStorageEntry(testStoredRetryDelay))); | |
| 946 this.mockApis.expects(once()).chrome_alarms_create( | 950 this.mockApis.expects(once()).chrome_alarms_create( |
| 947 testAttemptAlarmName, | 951 testAttemptAlarmName, |
| 948 eqJSON({ | 952 eqJSON({ |
| 949 delayInMinutes: expectedRetryDelaySeconds / 60, | 953 delayInMinutes: expectedRetryDelaySeconds / 60, |
| 950 periodInMinutes: testMaximumDelaySeconds / 60 | 954 periodInMinutes: testMaximumDelaySeconds / 60 |
| 951 })); | 955 })); |
| 952 this.mockApis.expects(once()).chrome_storage_local_set( | 956 this.mockApis.expects(once()).chrome_storage_local_set( |
| 953 eqJSON(createTestAttempStorageEntry(expectedRetryDelaySeconds))); | 957 eqJSON(createTestAttemptStorageEntry(expectedRetryDelaySeconds))); |
| 954 this.mockLocalFunctions.expects(once()).planForNextCallback(); | 958 this.mockLocalFunctions.expects(once()).planForNextCallback(); |
| 955 // Invocation. | 959 // Invocation. |
| 956 test.attempts.planForNext( | 960 test.attempts.planForNext( |
| 957 this.mockLocalFunctions.functions().planForNextCallback); | 961 this.mockLocalFunctions.functions().planForNextCallback); |
| 958 }); | 962 }); |
| 959 | 963 |
| 960 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() { | 964 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() { |
| 961 // Tests that firing the alarm invokes the attempt. | 965 // Tests that firing the alarm invokes the attempt. |
| 962 | 966 |
| 963 // Setup. | 967 // Setup. |
| 964 var test = setupAttemptManagerTest(this); | 968 var test = setupAttemptManagerTest(this); |
| 965 var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm'); | 969 var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm'); |
| 966 assertEquals(1, onAlarmHandlerContainer.length); | 970 assertEquals(1, onAlarmHandlerContainer.length); |
| 967 | 971 |
| 968 // Fire the alarm and check that this invokes the attempt callback. | 972 // Fire the alarm and check that this invokes the attempt callback. |
| 969 // Expectations. | 973 // Expectations. |
| 970 var alarmsGetSavedArgs = new SaveMockArguments(); | 974 var alarmsGetSavedArgs = new SaveMockArguments(); |
| 971 this.mockApis.expects(once()). | 975 this.mockApis.expects(once()). |
| 972 instrumented_alarms_get( | 976 instrumented_alarms_get( |
| 973 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), | 977 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), |
| 974 alarmsGetSavedArgs.match(ANYTHING)). | 978 alarmsGetSavedArgs.match(ANYTHING)). |
| 975 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'})); | 979 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'})); |
| 976 this.mockLocalFunctions.expects(once()).attempt(); | 980 this.mockLocalFunctions.expects(once()).attempt(); |
| 977 // Invocation. | 981 // Invocation. |
| 978 onAlarmHandlerContainer[0]({name: testAttemptAlarmName}); | 982 onAlarmHandlerContainer[0]({name: testAttemptAlarmName}); |
| 979 }); | 983 }); |
| OLD | NEW |