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

Side by Side 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, 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 | no next file » | no next file with comments »
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 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 this.mockLocalFunctions.expects(once()).callback(); 419 this.mockLocalFunctions.expects(once()).callback();
420 420
421 // Invocation. 421 // Invocation.
422 wrappedCallback(); 422 wrappedCallback();
423 423
424 // Step 4. Firing runtime.onSuspend event. 424 // Step 4. Firing runtime.onSuspend event.
425 assertTrue(onSuspendHandlerContainer.length == 1, 425 assertTrue(onSuspendHandlerContainer.length == 1,
426 'onSuspendHandlerContainer.length must be 1'); 426 'onSuspendHandlerContainer.length must be 1');
427 onSuspendHandlerContainer[0](); 427 onSuspendHandlerContainer[0]();
428 }); 428 });
429
430 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
431 var taskNameB = 'TASK B';
432 var taskNameC = 'TASK C';
433
434 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
435 return newTaskName == taskNameB && scheduledTaskName == taskNameA;
436 }
437
438 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
439 wrapper = undefined;
robliao 2013/08/29 21:15:38 Comment on why this statement is necessary.
vadimt 2013/08/29 21:42:47 Done.
440 fixture.makeAndRegisterMockApis([
441 'wrapper.checkInWrappedCallback',
442 'wrapper.registerWrapperPluginFactory',
443 'wrapper.debugGetStateString'
444 ]);
445 fixture.makeMockLocalFunctions(['task1', 'task2', 'task3']);
446 fixture.makeAndRegisterMockGlobals(['reportError']);
447
448 fixture.mockApis.stubs().wrapper_checkInWrappedCallback();
449 fixture.mockApis.stubs().wrapper_debugGetStateString().
450 will(returnValue('testWrapperDebugState'));
451
452 var registerWrapperPluginFactorySavedArgs = new SaveMockArguments();
453 fixture.mockApis.expects(once()).wrapper_registerWrapperPluginFactory(
454 registerWrapperPluginFactorySavedArgs.match(ANYTHING));
455 var tasks = buildTaskManager(areTasksConflicting);
456 Mock4JS.verifyAllMocks();
457
458 return {
459 tasks: tasks,
460 pluginFactory: registerWrapperPluginFactorySavedArgs.arguments[0]
461 };
462 }
463
464 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.
465 // Tests that 2 tasks get successfully executed consequentially, even if the
466 // second one conflicts with the first.
467
468 // Setup.
469 var test = setUpTaskManagerTest(this);
470
471 // Step 1. Adding 1st task that doesn't create pending callbacks.
472 // Expectations.
473 this.mockLocalFunctions.expects(once()).task1(ANYTHING);
474 // Invocation.
475 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
476 Mock4JS.verifyAllMocks();
477
478 // Step 2. Adding 2nd task.
479 // Expectations.
480 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
481 // Invocation.
482 test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2);
483 });
484
485 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager2', function() {
486 // Tests that adding a task while a conflicting task is being executed, causes
487 // the second one to be ignored.
488
489 // Setup.
490 var test = setUpTaskManagerTest(this);
491 var task1PluginInstance;
492
493 // Step 1. Adding 1st task that creates a pending callback.
494 // Expectations.
495 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
496 will(callFunction(function() {
497 task1PluginInstance = test.pluginFactory();
498 }));
499 // Invocation.
500 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
501 Mock4JS.verifyAllMocks();
502
503 // Step 2. Adding 2nd task.
504 test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2);
505 Mock4JS.verifyAllMocks();
506
507 // Step 3. Entering the callback of task1.
508 task1PluginInstance.prologue();
509 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
510
511 // Step 4. Leaving the callback of task1.
512 task1PluginInstance.epilogue();
513 });
514
515 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager3', function() {
516 // Tests that adding a task while a non-conflicting task is being executed,
517 // causes the second one to be executed after the first one completes.
518
519 // Setup.
520 var test = setUpTaskManagerTest(this);
521 var task1PluginInstance;
522
523 // Step 1. Adding 1st task that creates a pending callback.
524 // Expectations.
525 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
526 will(callFunction(function() {
527 task1PluginInstance = test.pluginFactory();
528 }));
529 // Invocation.
530 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
531 Mock4JS.verifyAllMocks();
532
533 // Step 2. Adding 2nd task.
534 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
535 Mock4JS.verifyAllMocks();
536
537 // Step 3. Entering the callback of task1.
538 task1PluginInstance.prologue();
539 Mock4JS.verifyAllMocks();
540
541 // Step 4. Leaving the callback of task1.
542 // Expectations.
543 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
544 // Invocation.
545 task1PluginInstance.epilogue();
546 });
547
548 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager4', function() {
549 // Tests that task manager correctly detects completion of tasks that create
550 // branching chains of callbacks.
551
552 // Setup.
553 var test = setUpTaskManagerTest(this);
554 var task1PluginInstance1, task1PluginInstance2, task1PluginInstance3;
555
556 // Step 1. Adding 1st task that creates a 2 pending callbacks.
557 // Expectations.
558 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
559 will(callFunction(function() {
560 task1PluginInstance1 = test.pluginFactory();
561 task1PluginInstance2 = test.pluginFactory();
562 }));
563 // Invocation.
564 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
565 Mock4JS.verifyAllMocks();
566
567 // Step 2. Adding 2nd task.
568 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
569 Mock4JS.verifyAllMocks();
570
571 // Step 3. Entering callback 1, create pending callback 3, exit callback 1.
572 // Enter/exit callback 2. Enter callback 3.
573 task1PluginInstance1.prologue();
574 task1PluginInstance3 = test.pluginFactory();
575 task1PluginInstance1.epilogue();
576 task1PluginInstance2.prologue();
577 task1PluginInstance2.epilogue();
578 task1PluginInstance3.prologue();
579 Mock4JS.verifyAllMocks();
580
581 // Step 4. Leaving 3rd callback of task1. Now task1 is complete, and task2
582 // should start.
583 // Expectations.
584 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
585 // Invocation.
586 task1PluginInstance3.epilogue();
587 });
588
589 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager5', function() {
590 // Tests that task manager's onSuspend method reports an error if there are
591 // pending tasks.
592
593 // Setup.
594 var test = setUpTaskManagerTest(this);
595 var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend');
596
597 // Step 1. Adding a task that creates a pending callback.
598 // Expectations.
599 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
600 will(callFunction(function() {
601 test.pluginFactory();
602 }));
603 // Invocation.
604 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
605 Mock4JS.verifyAllMocks();
606
607 // Step 2. Invoke onSuspend event of the task manager.
608 // Setup and expectations. The 2 callbacks in onSuspendHandlerContainer are
609 // from the wrapper and the task manager.
610 assertTrue(onSuspendHandlerContainer.length == 2,
611 'onSuspendHandlerContainer.length must be 2');
612 this.mockGlobals.expects(once()).reportError(eqToString(
613 'Error: ASSERT: Incomplete task when unloading event page,' +
614 ' queue = [{"name":"TASK A"}], testWrapperDebugState'));
615 // Invocation.
616 onSuspendHandlerContainer[1]();
617 });
618
619 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager6', function() {
620 // Tests that task manager's onSuspend method does not reports an error if all
621 // tasks completed.
622
623 // Setup.
624 var test = setUpTaskManagerTest(this);
625 var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend');
626 var task1PluginInstance;
627
628 // Step 1. Adding a task that creates a pending callback.
629 // Expectations.
630 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
631 will(callFunction(function() {
632 task1PluginInstance = test.pluginFactory();
633 }));
634 // Invocation.
635 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
636 Mock4JS.verifyAllMocks();
637
638 // Step 2. Invoke task's callback and the onSuspend event of the task manager.
639 // The 2 callbacks in onSuspendHandlerContainer are from the wrapper and the
640 // task manager.
641 task1PluginInstance.prologue();
642 task1PluginInstance.epilogue();
643 onSuspendHandlerContainer[1]();
644 });
645
646 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager7', function() {
647 // Tests that 3 tasks can be executed too. In particular, that if the second
648 // task is a single-step task which execution was originally blocked by task1,
649 // unblocking it causes immediate synchronous execution of both tasks 2 and 3.
650
651 // Setup.
652 var test = setUpTaskManagerTest(this);
653 var task1PluginInstance;
654
655 // Step 1. Adding 1st task that creates a pending callback.
656 // Expectations.
657 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
658 will(callFunction(function() {
659 task1PluginInstance = test.pluginFactory();
660 }));
661 // Invocation.
662 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
663 Mock4JS.verifyAllMocks();
664
665 // Step 2. Adding 2nd and 3rd tasks.
666 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
667 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task3);
668 Mock4JS.verifyAllMocks();
669
670 // Step 3. Entering the callback of task1.
671 task1PluginInstance.prologue();
672 Mock4JS.verifyAllMocks();
673
674 // Step 4. Leaving the callback of task1.
675 // Expectations.
676 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
677 this.mockLocalFunctions.expects(once()).task3(ANYTHING);
678 // Invocation.
679 task1PluginInstance.epilogue();
680 });
681
682 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager8', function() {
683 // Tests callbacks requested while a task is running, but not from a callback
684 // belonging to a task, are not counted as a part of the task.
685
686 // Setup.
687 var test = setUpTaskManagerTest(this);
688 var task1PluginInstance, nonTaskPluginInstance;
689
690 // Step 1. Adding 1st task that creates a pending callback.
691 // Expectations.
692 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
693 will(callFunction(function() {
694 task1PluginInstance = test.pluginFactory();
695 }));
696 // Invocation.
697 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
698 Mock4JS.verifyAllMocks();
699
700 // Step 2. Code that is not a part of the task creates a pending callback.
701 nonTaskPluginInstance = test.pluginFactory();
702 Mock4JS.verifyAllMocks();
703
704 // Step 3. Entering the callback of task1. After this, task1 should be
705 // finished despite the pending non-task callback.
706 task1PluginInstance.prologue();
707 task1PluginInstance.epilogue();
708 Mock4JS.verifyAllMocks();
709
710 // Step 4. Checking that task1 is finished by submitting task2, which should
711 // be executed immediately.
712 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
713 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
714 });
OLDNEW
« 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