Chromium Code Reviews| Index: chrome/browser/resources/google_now/utility_unittest.gtestjs |
| diff --git a/chrome/browser/resources/google_now/utility_unittest.gtestjs b/chrome/browser/resources/google_now/utility_unittest.gtestjs |
| index b99c7b3559266f0ab4ee106bf9c176ec7fcfa93b..d9d04b846faf4d54aacd69e1cb5f8978df983c83 100644 |
| --- a/chrome/browser/resources/google_now/utility_unittest.gtestjs |
| +++ b/chrome/browser/resources/google_now/utility_unittest.gtestjs |
| @@ -179,8 +179,7 @@ TEST_F('GoogleNowUtilityUnitTest', 'WrapperWrapCallbackEvent', function() { |
| wrapper.checkInWrappedCallback(); |
| // Step 4. Check that there won't be errors whe the page unloads. |
| - assertTrue(onSuspendHandlerContainer.length == 1, |
| - 'onSuspendHandlerContainer.length must be 1'); |
| + assertEquals(1, onSuspendHandlerContainer.length); |
| onSuspendHandlerContainer[0](); |
| }); |
| @@ -381,8 +380,7 @@ TEST_F('GoogleNowUtilityUnitTest', 'WrapperOnSuspendListenerFail', function() { |
| reportError(eqJSON(testError)); |
| // Invocation. |
| - assertTrue(onSuspendHandlerContainer.length == 1, |
| - 'onSuspendHandlerContainer.length must be 1'); |
| + assertEquals(1, onSuspendHandlerContainer.length); |
| onSuspendHandlerContainer[0](); |
| }); |
| @@ -424,8 +422,7 @@ TEST_F('GoogleNowUtilityUnitTest', |
| wrappedCallback(); |
| // Step 4. Firing runtime.onSuspend event. |
| - assertTrue(onSuspendHandlerContainer.length == 1, |
| - 'onSuspendHandlerContainer.length must be 1'); |
| + assertEquals(1, onSuspendHandlerContainer.length); |
| onSuspendHandlerContainer[0](); |
| }); |
| @@ -441,11 +438,6 @@ function areTasksConflicting(newTaskName, scheduledTaskName) { |
| } |
| function setUpTaskManagerTest(fixture) { |
| - // We want to mock wrapper using makeAndRegisterMockApis(), which requires |
| - // the mocked functions to not exist as a precondition. Resetting 'wrapper' to |
| - // 'undefined'. |
| - wrapper = undefined; |
| - |
| fixture.makeAndRegisterMockApis([ |
| 'wrapper.checkInWrappedCallback', |
| 'wrapper.registerWrapperPluginFactory', |
| @@ -620,8 +612,7 @@ TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerSuspendError', function() { |
| // Step 2. Invoke onSuspend event of the task manager. |
| // Setup and expectations. The 2 callbacks in onSuspendHandlerContainer are |
| // from the wrapper and the task manager. |
| - assertTrue(onSuspendHandlerContainer.length == 2, |
| - 'onSuspendHandlerContainer.length must be 2'); |
| + assertEquals(2, onSuspendHandlerContainer.length); |
| this.mockGlobals.expects(once()).reportError(eqToString( |
| 'Error: ASSERT: Incomplete task when unloading event page,' + |
| ' queue = [{"name":"TASK A"}], testWrapperDebugState')); |
| @@ -726,3 +717,206 @@ TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerNestedNonTask', function() { |
| this.mockLocalFunctions.expects(once()).task2(ANYTHING); |
| test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2); |
| }); |
| + |
| +var testAttemptAlarmName = 'attempt-scheduler-testAttempts'; |
| +var testAttemptStorageKey = 'current-delay-testAttempts'; |
|
robliao
2013/09/04 20:17:25
This literal (5 uses) is used more than the variab
vadimt
2013/09/04 22:34:40
Done.
|
| +var testInitialDelaySeconds = 239; |
| +var testMaximumDelaySeconds = 2239; |
| +var testRandomValue = 0.31415926; |
|
robliao
2013/09/04 20:17:25
Comment why this is called random value and why we
vadimt
2013/09/04 22:34:40
Done.
|
| + |
| +function setupAttemptManagerTest(fixture) { |
| + Math.random = function() { return testRandomValue; } |
| + |
| + fixture.makeMockLocalFunctions([ |
| + 'attempt', |
| + 'planForNextCallback', |
| + 'isRunningCallback' |
| + ]); |
| + fixture.makeAndRegisterMockApis([ |
| + 'chrome.alarms.clear', |
| + 'chrome.alarms.create', |
| + 'chrome.storage.local.remove', |
| + 'chrome.storage.local.set', |
| + 'instrumented.alarms.get', |
| + 'instrumented.storage.local.get' |
| + ]); |
| + |
| + var testAttempts = buildAttemptManager( |
| + 'testAttempts', |
| + fixture.mockLocalFunctions.functions().attempt, |
| + testInitialDelaySeconds, |
| + testMaximumDelaySeconds); |
| + Mock4JS.verifyAllMocks(); |
| + |
| + return { |
| + attempts: testAttempts |
| + }; |
| +} |
| + |
| +TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerStartStop', function() { |
| + // Tests starting and stopping an attempt manager. |
| + |
| + // Setup. |
| + var test = setupAttemptManagerTest(this); |
| + |
| + // Step 1. Checking that attempt manager is not running. |
| + // Expectations. |
| + var alarmsGetSavedArgs = new SaveMockArguments(); |
| + this.mockApis.expects(once()). |
| + instrumented_alarms_get( |
| + alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), |
| + alarmsGetSavedArgs.match(ANYTHING)). |
| + will(invokeCallback(alarmsGetSavedArgs, 1, undefined)); |
| + this.mockLocalFunctions.expects(once()).isRunningCallback(false); |
| + // Invocation. |
| + test.attempts.isRunning( |
| + this.mockLocalFunctions.functions().isRunningCallback); |
| + Mock4JS.verifyAllMocks(); |
| + |
| + // Step 2. Starting attempt manager with no parameters. |
| + // Expectations. |
| + var extectedRetryDelaySeconds = |
|
robliao
2013/09/04 20:17:25
Typo: expectedRetryDelaySeconds
vadimt
2013/09/04 22:34:40
Done.
|
| + testInitialDelaySeconds * (1 + testRandomValue * 0.2); |
| + this.mockApis.expects(once()).chrome_alarms_create( |
| + testAttemptAlarmName, |
| + eqJSON({ |
| + delayInMinutes: extectedRetryDelaySeconds / 60, |
| + periodInMinutes: testMaximumDelaySeconds / 60 |
| + })); |
| + this.mockApis.expects(once()).chrome_storage_local_set( |
| + eqJSON({'current-delay-testAttempts': extectedRetryDelaySeconds})); |
| + // Invocation. |
| + test.attempts.start(); |
| + Mock4JS.verifyAllMocks(); |
| + |
| + // Step 3. Checking that attempt manager is running. |
| + // Expectations. |
| + alarmsGetSavedArgs = new SaveMockArguments(); |
| + this.mockApis.expects(once()). |
| + instrumented_alarms_get( |
| + alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), |
| + alarmsGetSavedArgs.match(ANYTHING)). |
| + will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'})); |
| + this.mockLocalFunctions.expects(once()).isRunningCallback(true); |
| + // Invocation. |
| + test.attempts.isRunning( |
| + this.mockLocalFunctions.functions().isRunningCallback); |
| + Mock4JS.verifyAllMocks(); |
| + |
| + // Step 4. Stopping task manager. |
| + // Expectations. |
| + this.mockApis.expects(once()).chrome_alarms_clear(testAttemptAlarmName); |
| + this.mockApis.expects(once()).chrome_storage_local_remove( |
| + testAttemptStorageKey); |
| + // Invocation. |
| + test.attempts.stop(); |
| +}); |
| + |
| +TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerStartWithParam', function() { |
| + // Tests starting an attempt manager with a parameter. |
| + |
| + // Setup. |
| + var test = setupAttemptManagerTest(this); |
| + var testFirstDelaySeconds = 1039; |
|
robliao
2013/09/04 20:17:25
Why not use testInitialDelaySeconds?
vadimt
2013/09/04 22:34:40
To verify that the implementation doesn't use test
robliao
2013/09/05 00:09:39
Change the test name to AttemptManagerStartWithDel
vadimt
2013/09/05 00:30:00
Done.
|
| + |
| + // Starting attempt manager with a parameter specifying first delay. |
| + // Expectations. |
| + this.mockApis.expects(once()).chrome_alarms_create( |
| + testAttemptAlarmName, |
| + eqJSON({ |
| + delayInMinutes: testFirstDelaySeconds / 60, |
| + periodInMinutes: testMaximumDelaySeconds / 60 |
| + })); |
| + this.mockApis.expects(once()).chrome_storage_local_remove( |
| + testAttemptStorageKey); |
| + // Invocation. |
| + test.attempts.start(testFirstDelaySeconds); |
| +}); |
| + |
| +TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerExponGrowth', function() { |
| + // Tests that retry time grows exponentially. |
|
robliao
2013/09/04 20:17:25
It may be useful to have a test that grows this mo
vadimt
2013/09/04 22:34:40
Given that the object doesn't have state, and we c
robliao
2013/09/05 00:09:39
Makes sense. Consider updating the comment with th
vadimt
2013/09/05 00:30:00
Done.
|
| + |
| + // Setup. |
| + var test = setupAttemptManagerTest(this); |
| + var testStoredRetryDelay = 433; |
| + |
| + // Call planForNext, which prepares next attempt. Current retry time |
| + // is less than 1/2 of the maximum delay. |
| + // Expectations. |
| + var extectedRetryDelaySeconds = |
|
robliao
2013/09/04 20:17:25
Typo (see above)
vadimt
2013/09/04 22:34:40
Done.
|
| + testStoredRetryDelay * 2 * (1 + testRandomValue * 0.2); |
| + var storageGetSavedArgs = new SaveMockArguments(); |
| + this.mockApis.expects(once()).instrumented_storage_local_get( |
| + storageGetSavedArgs.match(eq(testAttemptStorageKey)), |
| + storageGetSavedArgs.match(ANYTHING)). |
| + will(invokeCallback( |
| + storageGetSavedArgs, |
| + 1, |
| + {'current-delay-testAttempts': testStoredRetryDelay})); |
| + this.mockApis.expects(once()).chrome_alarms_create( |
| + testAttemptAlarmName, |
| + eqJSON({ |
| + delayInMinutes: extectedRetryDelaySeconds / 60, |
| + periodInMinutes: testMaximumDelaySeconds / 60})); |
| + this.mockApis.expects(once()).chrome_storage_local_set( |
| + eqJSON({'current-delay-testAttempts': extectedRetryDelaySeconds})); |
| + this.mockLocalFunctions.expects(once()).planForNextCallback(); |
| + // Invocation. |
| + test.attempts.planForNext( |
| + this.mockLocalFunctions.functions().planForNextCallback); |
| +}); |
| + |
| +TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() { |
| + // Tests that retry time stops growing at the maximum value. |
| + |
| + // Setup. |
| + var test = setupAttemptManagerTest(this); |
| + var testStoredRetryDelay = 1500; |
| + |
| + // Call planForNext, which prepares next attempt. Current retry time |
| + // is greater than 1/2 of the maximum delay. |
| + // Expectations. |
| + var extectedRetryDelaySeconds = testMaximumDelaySeconds; |
|
robliao
2013/09/04 20:17:25
Typo (see above)
vadimt
2013/09/04 22:34:40
Done.
|
| + var storageGetSavedArgs = new SaveMockArguments(); |
| + this.mockApis.expects(once()). |
| + instrumented_storage_local_get( |
| + storageGetSavedArgs.match(eq(testAttemptStorageKey)), |
| + storageGetSavedArgs.match(ANYTHING)). |
| + will(invokeCallback( |
| + storageGetSavedArgs, |
| + 1, |
| + {'current-delay-testAttempts': testStoredRetryDelay})); |
| + this.mockApis.expects(once()).chrome_alarms_create( |
| + testAttemptAlarmName, |
| + eqJSON({ |
| + delayInMinutes: extectedRetryDelaySeconds / 60, |
| + periodInMinutes: testMaximumDelaySeconds / 60 |
| + })); |
| + this.mockApis.expects(once()).chrome_storage_local_set( |
| + eqJSON({'current-delay-testAttempts': extectedRetryDelaySeconds})); |
| + this.mockLocalFunctions.expects(once()).planForNextCallback(); |
| + // Invocation. |
| + test.attempts.planForNext( |
| + this.mockLocalFunctions.functions().planForNextCallback); |
| +}); |
| + |
| +TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() { |
| + // Tests that firing the alarm invokes the attempt. |
| + |
| + // Setup. |
| + var test = setupAttemptManagerTest(this); |
| + var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm'); |
| + assertEquals(1, onAlarmHandlerContainer.length); |
| + |
| + // Fire the alarm and check that this invokes the attemp callback. |
| + // Expectations. |
| + var alarmsGetSavedArgs = new SaveMockArguments(); |
| + this.mockApis.expects(once()). |
| + instrumented_alarms_get( |
| + alarmsGetSavedArgs.match(eq(testAttemptAlarmName)), |
| + alarmsGetSavedArgs.match(ANYTHING)). |
| + will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'})); |
| + this.mockLocalFunctions.expects(once()).attempt(); |
| + // Invocation. |
| + onAlarmHandlerContainer[0]({name: testAttemptAlarmName}); |
| +}); |