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

Unified Diff: chrome/browser/resources/google_now/utility_unittest.gtestjs

Issue 23623010: Unit tests for task manager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 86cc6c6a302a32081c608fb3c18981502fa7d478..c7ffebeb7adc55490b46378c5113f51a0488c621 100644
--- a/chrome/browser/resources/google_now/utility_unittest.gtestjs
+++ b/chrome/browser/resources/google_now/utility_unittest.gtestjs
@@ -426,3 +426,289 @@ TEST_F('GoogleNowUtilityUnitTest',
'onSuspendHandlerContainer.length must be 1');
onSuspendHandlerContainer[0]();
});
+
+var taskNameA = 'TASK A';
robliao 2013/08/29 21:15:38 Why are these global? It would be good to isolate
vadimt 2013/08/29 21:42:47 They are used by the tests below. I could include
+var taskNameB = 'TASK B';
+var taskNameC = 'TASK C';
+
+function areTasksConflicting(newTaskName, scheduledTaskName) {
robliao 2013/08/29 21:15:38 Mock this.
vadimt 2013/08/29 21:42:47 No clear what would be the benefit of this. The co
+ return newTaskName == taskNameB && scheduledTaskName == taskNameA;
+}
+
+function setUpTaskManagerTest(fixture) {
robliao 2013/08/29 21:15:38 Add banner comment.
vadimt 2013/08/29 21:42:47 I believe this would be an overkill for a test hel
+ wrapper = undefined;
robliao 2013/08/29 21:15:38 Comment on why this statement is necessary.
vadimt 2013/08/29 21:42:47 Done.
+ fixture.makeAndRegisterMockApis([
+ 'wrapper.checkInWrappedCallback',
+ 'wrapper.registerWrapperPluginFactory',
+ 'wrapper.debugGetStateString'
+ ]);
+ fixture.makeMockLocalFunctions(['task1', 'task2', 'task3']);
+ fixture.makeAndRegisterMockGlobals(['reportError']);
+
+ fixture.mockApis.stubs().wrapper_checkInWrappedCallback();
+ fixture.mockApis.stubs().wrapper_debugGetStateString().
+ will(returnValue('testWrapperDebugState'));
+
+ var registerWrapperPluginFactorySavedArgs = new SaveMockArguments();
+ fixture.mockApis.expects(once()).wrapper_registerWrapperPluginFactory(
+ registerWrapperPluginFactorySavedArgs.match(ANYTHING));
+ var tasks = buildTaskManager(areTasksConflicting);
+ Mock4JS.verifyAllMocks();
+
+ return {
+ tasks: tasks,
+ pluginFactory: registerWrapperPluginFactorySavedArgs.arguments[0]
+ };
+}
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager1', function() {
robliao 2013/08/29 21:15:38 Use a more descriptive name instead of 1-7.
vadimt 2013/08/29 21:42:47 I tried, but this resulted in long boring identifi
robliao 2013/08/30 17:34:09 Short descriptive identifiers are better than 1-7.
vadimt 2013/08/30 20:18:52 Please suggest names (say, for first 3 tests), and
robliao 2013/08/30 22:33:42 1. TaskManager-2Sequential 2. TaskManager-Conflict
vadimt 2013/08/30 22:59:32 Done.
+ // Tests that 2 tasks get successfully executed consequentially, even if the
+ // second one conflicts with the first.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+
+ // Step 1. Adding 1st task that doesn't create pending callbacks.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING);
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // Step 2. Adding 2nd task.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task2(ANYTHING);
+ // Invocation.
+ test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2);
+});
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager2', function() {
+ // Tests that adding a task while a conflicting task is being executed, causes
+ // the second one to be ignored.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+ var task1PluginInstance;
+
+ // Step 1. Adding 1st task that creates a pending callback.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING).
+ will(callFunction(function() {
+ task1PluginInstance = test.pluginFactory();
+ }));
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // Step 2. Adding 2nd task.
+ test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2);
+ Mock4JS.verifyAllMocks();
+
+ // Step 3. Entering the callback of task1.
+ task1PluginInstance.prologue();
+ Mock4JS.verifyAllMocks();
robliao 2013/08/29 21:15:38 Why not just verify all mocks at the end?
vadimt 2013/08/29 21:42:47 Then we are not able to differentiate between: Inv
robliao 2013/08/30 17:34:09 There are no other tests in Chrome that call verif
vadimt 2013/08/30 20:18:52 I believe, we are in a situation when we set the s
+
+ // Step 4. Leaving the callback of task1.
+ task1PluginInstance.epilogue();
+});
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager3', function() {
+ // Tests that adding a task while a non-conflicting task is being executed,
+ // causes the second one to be executed after the first one completes.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+ var task1PluginInstance;
+
+ // Step 1. Adding 1st task that creates a pending callback.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING).
+ will(callFunction(function() {
+ task1PluginInstance = test.pluginFactory();
+ }));
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // Step 2. Adding 2nd task.
+ test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
+ Mock4JS.verifyAllMocks();
+
+ // Step 3. Entering the callback of task1.
+ task1PluginInstance.prologue();
+ Mock4JS.verifyAllMocks();
+
+ // Step 4. Leaving the callback of task1.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task2(ANYTHING);
+ // Invocation.
+ task1PluginInstance.epilogue();
+});
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager4', function() {
+ // Tests that task manager correctly detects completion of tasks that create
+ // branching chains of callbacks.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+ var task1PluginInstance1, task1PluginInstance2, task1PluginInstance3;
+
+ // Step 1. Adding 1st task that creates a 2 pending callbacks.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING).
+ will(callFunction(function() {
+ task1PluginInstance1 = test.pluginFactory();
+ task1PluginInstance2 = test.pluginFactory();
+ }));
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // Step 2. Adding 2nd task.
+ test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
+ Mock4JS.verifyAllMocks();
+
+ // Step 3. Entering callback 1, create pending callback 3, exit callback 1.
+ // Enter/exit callback 2. Enter callback 3.
+ task1PluginInstance1.prologue();
+ task1PluginInstance3 = test.pluginFactory();
+ task1PluginInstance1.epilogue();
+ task1PluginInstance2.prologue();
+ task1PluginInstance2.epilogue();
+ task1PluginInstance3.prologue();
+ Mock4JS.verifyAllMocks();
+
+ // Step 4. Leaving 3rd callback of task1. Now task1 is complete, and task2
+ // should start.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task2(ANYTHING);
+ // Invocation.
+ task1PluginInstance3.epilogue();
+});
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager5', function() {
+ // Tests that task manager's onSuspend method reports an error if there are
+ // pending tasks.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+ var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend');
+
+ // Step 1. Adding a task that creates a pending callback.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING).
+ will(callFunction(function() {
+ test.pluginFactory();
+ }));
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // 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');
+ this.mockGlobals.expects(once()).reportError(eqToString(
+ 'Error: ASSERT: Incomplete task when unloading event page,' +
+ ' queue = [{"name":"TASK A"}], testWrapperDebugState'));
+ // Invocation.
+ onSuspendHandlerContainer[1]();
+});
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager6', function() {
+ // Tests that task manager's onSuspend method does not reports an error if all
+ // tasks completed.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+ var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend');
+ var task1PluginInstance;
+
+ // Step 1. Adding a task that creates a pending callback.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING).
+ will(callFunction(function() {
+ task1PluginInstance = test.pluginFactory();
+ }));
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // Step 2. Invoke task's callback and the onSuspend event of the task manager.
+ // The 2 callbacks in onSuspendHandlerContainer are from the wrapper and the
+ // task manager.
+ task1PluginInstance.prologue();
+ task1PluginInstance.epilogue();
+ onSuspendHandlerContainer[1]();
+});
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager7', function() {
+ // Tests that 3 tasks can be executed too. In particular, that if the second
+ // task is a single-step task which execution was originally blocked by task1,
+ // unblocking it causes immediate synchronous execution of both tasks 2 and 3.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+ var task1PluginInstance;
+
+ // Step 1. Adding 1st task that creates a pending callback.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING).
+ will(callFunction(function() {
+ task1PluginInstance = test.pluginFactory();
+ }));
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // Step 2. Adding 2nd and 3rd tasks.
+ test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
+ test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task3);
+ Mock4JS.verifyAllMocks();
+
+ // Step 3. Entering the callback of task1.
+ task1PluginInstance.prologue();
+ Mock4JS.verifyAllMocks();
+
+ // Step 4. Leaving the callback of task1.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task2(ANYTHING);
+ this.mockLocalFunctions.expects(once()).task3(ANYTHING);
+ // Invocation.
+ task1PluginInstance.epilogue();
+});
+
+TEST_F('GoogleNowUtilityUnitTest', 'TaskManager8', function() {
+ // Tests callbacks requested while a task is running, but not from a callback
+ // belonging to a task, are not counted as a part of the task.
+
+ // Setup.
+ var test = setUpTaskManagerTest(this);
+ var task1PluginInstance, nonTaskPluginInstance;
+
+ // Step 1. Adding 1st task that creates a pending callback.
+ // Expectations.
+ this.mockLocalFunctions.expects(once()).task1(ANYTHING).
+ will(callFunction(function() {
+ task1PluginInstance = test.pluginFactory();
+ }));
+ // Invocation.
+ test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
+ Mock4JS.verifyAllMocks();
+
+ // Step 2. Code that is not a part of the task creates a pending callback.
+ nonTaskPluginInstance = test.pluginFactory();
+ Mock4JS.verifyAllMocks();
+
+ // Step 3. Entering the callback of task1. After this, task1 should be
+ // finished despite the pending non-task callback.
+ task1PluginInstance.prologue();
+ task1PluginInstance.epilogue();
+ Mock4JS.verifyAllMocks();
+
+ // Step 4. Checking that task1 is finished by submitting task2, which should
+ // be executed immediately.
+ this.mockLocalFunctions.expects(once()).task2(ANYTHING);
+ test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
+});
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698