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

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

Issue 23751004: Unit tests for attempt manager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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
« no previous file with comments | « no previous file | chrome/test/data/webui/test_api.js » ('j') | chrome/test/data/webui/test_api.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 this.mockGlobals.expects(once()). 172 this.mockGlobals.expects(once()).
173 buildErrorWithMessageForServer('Not in instrumented callback'). 173 buildErrorWithMessageForServer('Not in instrumented callback').
174 will(returnValue(testError)); 174 will(returnValue(testError));
175 this.mockGlobals.expects(once()). 175 this.mockGlobals.expects(once()).
176 reportError(eqJSON(testError)); 176 reportError(eqJSON(testError));
177 177
178 // Invocation. 178 // Invocation.
179 wrapper.checkInWrappedCallback(); 179 wrapper.checkInWrappedCallback();
180 180
181 // Step 4. Check that there won't be errors whe the page unloads. 181 // Step 4. Check that there won't be errors whe the page unloads.
182 assertTrue(onSuspendHandlerContainer.length == 1, 182 assertEquals(1, onSuspendHandlerContainer.length);
183 'onSuspendHandlerContainer.length must be 1');
184 onSuspendHandlerContainer[0](); 183 onSuspendHandlerContainer[0]();
185 }); 184 });
186 185
187 TEST_F('GoogleNowUtilityUnitTest', 'WrapperWrapCallbackPlugin', function() { 186 TEST_F('GoogleNowUtilityUnitTest', 'WrapperWrapCallbackPlugin', function() {
188 // Tests calling plugin's prologue and epilogue. 187 // Tests calling plugin's prologue and epilogue.
189 188
190 // Setup. 189 // Setup.
191 this.makeMockLocalFunctions([ 190 this.makeMockLocalFunctions([
192 'callback', 191 'callback',
193 'pluginFactory', 192 'pluginFactory',
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 // Step 3. Firing runtime.onSuspend event. 373 // Step 3. Firing runtime.onSuspend event.
375 // Expectations. 374 // Expectations.
376 this.mockGlobals.expects(once()). 375 this.mockGlobals.expects(once()).
377 buildErrorWithMessageForServer(stringContains( 376 buildErrorWithMessageForServer(stringContains(
378 'ASSERT: Pending callbacks when unloading event page')). 377 'ASSERT: Pending callbacks when unloading event page')).
379 will(returnValue(testError)); 378 will(returnValue(testError));
380 this.mockGlobals.expects(once()). 379 this.mockGlobals.expects(once()).
381 reportError(eqJSON(testError)); 380 reportError(eqJSON(testError));
382 381
383 // Invocation. 382 // Invocation.
384 assertTrue(onSuspendHandlerContainer.length == 1, 383 assertEquals(1, onSuspendHandlerContainer.length);
385 'onSuspendHandlerContainer.length must be 1');
386 onSuspendHandlerContainer[0](); 384 onSuspendHandlerContainer[0]();
387 }); 385 });
388 386
389 TEST_F('GoogleNowUtilityUnitTest', 387 TEST_F('GoogleNowUtilityUnitTest',
390 'WrapperOnSuspendListenerSuccess', 388 'WrapperOnSuspendListenerSuccess',
391 function() { 389 function() {
392 // Tests that upon unloading event page, we don't get an error if there are no 390 // Tests that upon unloading event page, we don't get an error if there are no
393 // pending required callbacks. 391 // pending required callbacks.
394 392
395 // Setup. 393 // Setup.
(...skipping 21 matching lines...) Expand all
417 Mock4JS.verifyAllMocks(); 415 Mock4JS.verifyAllMocks();
418 416
419 // Step 3. Calling wrapped callback. 417 // Step 3. Calling wrapped callback.
420 // Expectations. 418 // Expectations.
421 this.mockLocalFunctions.expects(once()).callback(); 419 this.mockLocalFunctions.expects(once()).callback();
422 420
423 // Invocation. 421 // Invocation.
424 wrappedCallback(); 422 wrappedCallback();
425 423
426 // Step 4. Firing runtime.onSuspend event. 424 // Step 4. Firing runtime.onSuspend event.
427 assertTrue(onSuspendHandlerContainer.length == 1, 425 assertEquals(1, onSuspendHandlerContainer.length);
428 'onSuspendHandlerContainer.length must be 1');
429 onSuspendHandlerContainer[0](); 426 onSuspendHandlerContainer[0]();
430 }); 427 });
431 428
432 var taskNameA = 'TASK A'; 429 var taskNameA = 'TASK A';
433 var taskNameB = 'TASK B'; 430 var taskNameB = 'TASK B';
434 var taskNameC = 'TASK C'; 431 var taskNameC = 'TASK C';
435 432
436 function areTasksConflicting(newTaskName, scheduledTaskName) { 433 function areTasksConflicting(newTaskName, scheduledTaskName) {
437 // Task B is conflicting with Task A. This means that if Task B is added when 434 // Task B is conflicting with Task A. This means that if Task B is added when
438 // Task A is running, Task B will be ignored (but not vice versa). No other 435 // Task A is running, Task B will be ignored (but not vice versa). No other
439 // pair is conflicting. 436 // pair is conflicting.
440 return newTaskName == taskNameB && scheduledTaskName == taskNameA; 437 return newTaskName == taskNameB && scheduledTaskName == taskNameA;
441 } 438 }
442 439
443 function setUpTaskManagerTest(fixture) { 440 function setUpTaskManagerTest(fixture) {
444 // We want to mock wrapper using makeAndRegisterMockApis(), which requires
445 // the mocked functions to not exist as a precondition. Resetting 'wrapper' to
446 // 'undefined'.
447 wrapper = undefined;
448
449 fixture.makeAndRegisterMockApis([ 441 fixture.makeAndRegisterMockApis([
450 'wrapper.checkInWrappedCallback', 442 'wrapper.checkInWrappedCallback',
451 'wrapper.registerWrapperPluginFactory', 443 'wrapper.registerWrapperPluginFactory',
452 'wrapper.debugGetStateString' 444 'wrapper.debugGetStateString'
453 ]); 445 ]);
454 fixture.makeMockLocalFunctions(['task1', 'task2', 'task3']); 446 fixture.makeMockLocalFunctions(['task1', 'task2', 'task3']);
455 fixture.makeAndRegisterMockGlobals(['reportError']); 447 fixture.makeAndRegisterMockGlobals(['reportError']);
456 448
457 fixture.mockApis.stubs().wrapper_checkInWrappedCallback(); 449 fixture.mockApis.stubs().wrapper_checkInWrappedCallback();
458 fixture.mockApis.stubs().wrapper_debugGetStateString(). 450 fixture.mockApis.stubs().wrapper_debugGetStateString().
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 will(callFunction(function() { 605 will(callFunction(function() {
614 test.pluginFactory(); 606 test.pluginFactory();
615 })); 607 }));
616 // Invocation. 608 // Invocation.
617 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1); 609 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
618 Mock4JS.verifyAllMocks(); 610 Mock4JS.verifyAllMocks();
619 611
620 // Step 2. Invoke onSuspend event of the task manager. 612 // Step 2. Invoke onSuspend event of the task manager.
621 // Setup and expectations. The 2 callbacks in onSuspendHandlerContainer are 613 // Setup and expectations. The 2 callbacks in onSuspendHandlerContainer are
622 // from the wrapper and the task manager. 614 // from the wrapper and the task manager.
623 assertTrue(onSuspendHandlerContainer.length == 2, 615 assertEquals(2, onSuspendHandlerContainer.length);
624 'onSuspendHandlerContainer.length must be 2');
625 this.mockGlobals.expects(once()).reportError(eqToString( 616 this.mockGlobals.expects(once()).reportError(eqToString(
626 'Error: ASSERT: Incomplete task when unloading event page,' + 617 'Error: ASSERT: Incomplete task when unloading event page,' +
627 ' queue = [{"name":"TASK A"}], testWrapperDebugState')); 618 ' queue = [{"name":"TASK A"}], testWrapperDebugState'));
628 // Invocation. 619 // Invocation.
629 onSuspendHandlerContainer[1](); 620 onSuspendHandlerContainer[1]();
630 }); 621 });
631 622
632 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerSuspendSuccess', function() { 623 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerSuspendSuccess', function() {
633 // Tests that task manager's onSuspend method does not reports an error if all 624 // Tests that task manager's onSuspend method does not reports an error if all
634 // tasks completed. 625 // tasks completed.
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 // finished despite the pending non-task callback. 710 // finished despite the pending non-task callback.
720 task1PluginInstance.prologue(); 711 task1PluginInstance.prologue();
721 task1PluginInstance.epilogue(); 712 task1PluginInstance.epilogue();
722 Mock4JS.verifyAllMocks(); 713 Mock4JS.verifyAllMocks();
723 714
724 // Step 4. Checking that task1 is finished by submitting task2, which should 715 // Step 4. Checking that task1 is finished by submitting task2, which should
725 // be executed immediately. 716 // be executed immediately.
726 this.mockLocalFunctions.expects(once()).task2(ANYTHING); 717 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
727 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2); 718 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
728 }); 719 });
720
721 var testAttemptAlarmName = 'attempt-scheduler-testAttempts';
722 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.
723 var testInitialDelaySeconds = 239;
724 var testMaximumDelaySeconds = 2239;
725 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.
726
727 function setupAttemptManagerTest(fixture) {
728 Math.random = function() { return testRandomValue; }
729
730 fixture.makeMockLocalFunctions([
731 'attempt',
732 'planForNextCallback',
733 'isRunningCallback'
734 ]);
735 fixture.makeAndRegisterMockApis([
736 'chrome.alarms.clear',
737 'chrome.alarms.create',
738 'chrome.storage.local.remove',
739 'chrome.storage.local.set',
740 'instrumented.alarms.get',
741 'instrumented.storage.local.get'
742 ]);
743
744 var testAttempts = buildAttemptManager(
745 'testAttempts',
746 fixture.mockLocalFunctions.functions().attempt,
747 testInitialDelaySeconds,
748 testMaximumDelaySeconds);
749 Mock4JS.verifyAllMocks();
750
751 return {
752 attempts: testAttempts
753 };
754 }
755
756 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerStartStop', function() {
757 // Tests starting and stopping an attempt manager.
758
759 // Setup.
760 var test = setupAttemptManagerTest(this);
761
762 // Step 1. Checking that attempt manager is not running.
763 // Expectations.
764 var alarmsGetSavedArgs = new SaveMockArguments();
765 this.mockApis.expects(once()).
766 instrumented_alarms_get(
767 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)),
768 alarmsGetSavedArgs.match(ANYTHING)).
769 will(invokeCallback(alarmsGetSavedArgs, 1, undefined));
770 this.mockLocalFunctions.expects(once()).isRunningCallback(false);
771 // Invocation.
772 test.attempts.isRunning(
773 this.mockLocalFunctions.functions().isRunningCallback);
774 Mock4JS.verifyAllMocks();
775
776 // Step 2. Starting attempt manager with no parameters.
777 // Expectations.
778 var extectedRetryDelaySeconds =
robliao 2013/09/04 20:17:25 Typo: expectedRetryDelaySeconds
vadimt 2013/09/04 22:34:40 Done.
779 testInitialDelaySeconds * (1 + testRandomValue * 0.2);
780 this.mockApis.expects(once()).chrome_alarms_create(
781 testAttemptAlarmName,
782 eqJSON({
783 delayInMinutes: extectedRetryDelaySeconds / 60,
784 periodInMinutes: testMaximumDelaySeconds / 60
785 }));
786 this.mockApis.expects(once()).chrome_storage_local_set(
787 eqJSON({'current-delay-testAttempts': extectedRetryDelaySeconds}));
788 // Invocation.
789 test.attempts.start();
790 Mock4JS.verifyAllMocks();
791
792 // Step 3. Checking that attempt manager is running.
793 // Expectations.
794 alarmsGetSavedArgs = new SaveMockArguments();
795 this.mockApis.expects(once()).
796 instrumented_alarms_get(
797 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)),
798 alarmsGetSavedArgs.match(ANYTHING)).
799 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'}));
800 this.mockLocalFunctions.expects(once()).isRunningCallback(true);
801 // Invocation.
802 test.attempts.isRunning(
803 this.mockLocalFunctions.functions().isRunningCallback);
804 Mock4JS.verifyAllMocks();
805
806 // Step 4. Stopping task manager.
807 // Expectations.
808 this.mockApis.expects(once()).chrome_alarms_clear(testAttemptAlarmName);
809 this.mockApis.expects(once()).chrome_storage_local_remove(
810 testAttemptStorageKey);
811 // Invocation.
812 test.attempts.stop();
813 });
814
815 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerStartWithParam', function() {
816 // Tests starting an attempt manager with a parameter.
817
818 // Setup.
819 var test = setupAttemptManagerTest(this);
820 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.
821
822 // Starting attempt manager with a parameter specifying first delay.
823 // Expectations.
824 this.mockApis.expects(once()).chrome_alarms_create(
825 testAttemptAlarmName,
826 eqJSON({
827 delayInMinutes: testFirstDelaySeconds / 60,
828 periodInMinutes: testMaximumDelaySeconds / 60
829 }));
830 this.mockApis.expects(once()).chrome_storage_local_remove(
831 testAttemptStorageKey);
832 // Invocation.
833 test.attempts.start(testFirstDelaySeconds);
834 });
835
836 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerExponGrowth', function() {
837 // 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.
838
839 // Setup.
840 var test = setupAttemptManagerTest(this);
841 var testStoredRetryDelay = 433;
842
843 // Call planForNext, which prepares next attempt. Current retry time
844 // is less than 1/2 of the maximum delay.
845 // Expectations.
846 var extectedRetryDelaySeconds =
robliao 2013/09/04 20:17:25 Typo (see above)
vadimt 2013/09/04 22:34:40 Done.
847 testStoredRetryDelay * 2 * (1 + testRandomValue * 0.2);
848 var storageGetSavedArgs = new SaveMockArguments();
849 this.mockApis.expects(once()).instrumented_storage_local_get(
850 storageGetSavedArgs.match(eq(testAttemptStorageKey)),
851 storageGetSavedArgs.match(ANYTHING)).
852 will(invokeCallback(
853 storageGetSavedArgs,
854 1,
855 {'current-delay-testAttempts': testStoredRetryDelay}));
856 this.mockApis.expects(once()).chrome_alarms_create(
857 testAttemptAlarmName,
858 eqJSON({
859 delayInMinutes: extectedRetryDelaySeconds / 60,
860 periodInMinutes: testMaximumDelaySeconds / 60}));
861 this.mockApis.expects(once()).chrome_storage_local_set(
862 eqJSON({'current-delay-testAttempts': extectedRetryDelaySeconds}));
863 this.mockLocalFunctions.expects(once()).planForNextCallback();
864 // Invocation.
865 test.attempts.planForNext(
866 this.mockLocalFunctions.functions().planForNextCallback);
867 });
868
869 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerGrowthLimit', function() {
870 // Tests that retry time stops growing at the maximum value.
871
872 // Setup.
873 var test = setupAttemptManagerTest(this);
874 var testStoredRetryDelay = 1500;
875
876 // Call planForNext, which prepares next attempt. Current retry time
877 // is greater than 1/2 of the maximum delay.
878 // Expectations.
879 var extectedRetryDelaySeconds = testMaximumDelaySeconds;
robliao 2013/09/04 20:17:25 Typo (see above)
vadimt 2013/09/04 22:34:40 Done.
880 var storageGetSavedArgs = new SaveMockArguments();
881 this.mockApis.expects(once()).
882 instrumented_storage_local_get(
883 storageGetSavedArgs.match(eq(testAttemptStorageKey)),
884 storageGetSavedArgs.match(ANYTHING)).
885 will(invokeCallback(
886 storageGetSavedArgs,
887 1,
888 {'current-delay-testAttempts': testStoredRetryDelay}));
889 this.mockApis.expects(once()).chrome_alarms_create(
890 testAttemptAlarmName,
891 eqJSON({
892 delayInMinutes: extectedRetryDelaySeconds / 60,
893 periodInMinutes: testMaximumDelaySeconds / 60
894 }));
895 this.mockApis.expects(once()).chrome_storage_local_set(
896 eqJSON({'current-delay-testAttempts': extectedRetryDelaySeconds}));
897 this.mockLocalFunctions.expects(once()).planForNextCallback();
898 // Invocation.
899 test.attempts.planForNext(
900 this.mockLocalFunctions.functions().planForNextCallback);
901 });
902
903 TEST_F('GoogleNowUtilityUnitTest', 'AttemptManagerAlarm', function() {
904 // Tests that firing the alarm invokes the attempt.
905
906 // Setup.
907 var test = setupAttemptManagerTest(this);
908 var onAlarmHandlerContainer = getMockHandlerContainer('alarms.onAlarm');
909 assertEquals(1, onAlarmHandlerContainer.length);
910
911 // Fire the alarm and check that this invokes the attemp callback.
912 // Expectations.
913 var alarmsGetSavedArgs = new SaveMockArguments();
914 this.mockApis.expects(once()).
915 instrumented_alarms_get(
916 alarmsGetSavedArgs.match(eq(testAttemptAlarmName)),
917 alarmsGetSavedArgs.match(ANYTHING)).
918 will(invokeCallback(alarmsGetSavedArgs, 1, {testField: 'TEST VALUE'}));
919 this.mockLocalFunctions.expects(once()).attempt();
920 // Invocation.
921 onAlarmHandlerContainer[0]({name: testAttemptAlarmName});
922 });
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/webui/test_api.js » ('j') | chrome/test/data/webui/test_api.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698