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

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: More comments. 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';
431 var taskNameB = 'TASK B';
432 var taskNameC = 'TASK C';
433
434 function areTasksConflicting(newTaskName, scheduledTaskName) {
435 return newTaskName == taskNameB && scheduledTaskName == taskNameA;
436 }
437
438 function setUpTaskManagerTest(fixture) {
439 // We want to mock wrapper using makeAndRegisterMockApis(), which requires
440 // the mocked functions to not exist as a precondition. Resetting 'wrapper' to
441 // 'undefined'.
442 wrapper = undefined;
443
444 fixture.makeAndRegisterMockApis([
445 'wrapper.checkInWrappedCallback',
446 'wrapper.registerWrapperPluginFactory',
447 'wrapper.debugGetStateString'
448 ]);
449 fixture.makeMockLocalFunctions(['task1', 'task2', 'task3']);
450 fixture.makeAndRegisterMockGlobals(['reportError']);
451
452 fixture.mockApis.stubs().wrapper_checkInWrappedCallback();
453 fixture.mockApis.stubs().wrapper_debugGetStateString().
454 will(returnValue('testWrapperDebugState'));
455
456 var registerWrapperPluginFactorySavedArgs = new SaveMockArguments();
457 fixture.mockApis.expects(once()).wrapper_registerWrapperPluginFactory(
458 registerWrapperPluginFactorySavedArgs.match(ANYTHING));
459 var tasks = buildTaskManager(areTasksConflicting);
460 Mock4JS.verifyAllMocks();
461
462 return {
463 tasks: tasks,
464 pluginFactory: registerWrapperPluginFactorySavedArgs.arguments[0]
465 };
466 }
467
468 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager2Sequential', function() {
469 // Tests that 2 tasks get successfully executed consequentially, even if the
470 // second one conflicts with the first.
471
472 // Setup.
473 var test = setUpTaskManagerTest(this);
474
475 // Step 1. Adding 1st task that doesn't create pending callbacks.
476 // Expectations.
477 this.mockLocalFunctions.expects(once()).task1(ANYTHING);
478 // Invocation.
479 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
480 Mock4JS.verifyAllMocks();
481
482 // Step 2. Adding 2nd task.
483 // Expectations.
484 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
485 // Invocation.
486 test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2);
487 });
488
489 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerConflicting', function() {
490 // Tests that adding a task while a conflicting task is being executed, causes
491 // the second one to be ignored.
492
493 // Setup.
494 var test = setUpTaskManagerTest(this);
495 var task1PluginInstance;
496
497 // Step 1. Adding 1st task that creates a pending callback.
498 // Expectations.
499 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
500 will(callFunction(function() {
501 task1PluginInstance = test.pluginFactory();
502 }));
503 // Invocation.
504 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
505 Mock4JS.verifyAllMocks();
506
507 // Step 2. Adding 2nd task.
508 test.tasks.add(taskNameB, this.mockLocalFunctions.functions().task2);
509 Mock4JS.verifyAllMocks();
510
511 // Step 3. Entering the callback of task1.
512 task1PluginInstance.prologue();
robliao 2013/09/03 23:23:31 It is worth a discussion as to what makes this tes
vadimt 2013/09/04 00:29:16 You mean what's the difference from TaskManager2Se
robliao 2013/09/04 00:54:00 That's the idea. A bit of the mechanics should be
vadimt 2013/09/04 01:40:15 Done.
robliao 2013/09/04 17:02:03 Where was this done? On 2013/09/04 01:40:15, vadim
vadimt 2013/09/04 17:21:37 CR tool may not show you the latest uploaded versi
513 Mock4JS.verifyAllMocks();
514
515 // Step 4. Leaving the callback of task1.
516 task1PluginInstance.epilogue();
517 });
518
519 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerNestedTaskEnqueue', function() {
520 // Tests that adding a task while a non-conflicting task is being executed,
521 // causes the second one to be executed after the first one completes.
522
523 // Setup.
524 var test = setUpTaskManagerTest(this);
525 var task1PluginInstance;
526
527 // Step 1. Adding 1st task that creates a pending callback.
528 // Expectations.
529 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
530 will(callFunction(function() {
531 task1PluginInstance = test.pluginFactory();
532 }));
533 // Invocation.
534 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
535 Mock4JS.verifyAllMocks();
536
537 // Step 2. Adding 2nd task.
538 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
robliao 2013/09/03 23:23:31 Why not taskNameB?
vadimt 2013/09/04 00:29:16 taskNameB conflicts with taskNameA, while taskName
robliao 2013/09/04 00:54:00 Future readers will ask this question since the co
vadimt 2013/09/04 01:40:15 Done.
539 Mock4JS.verifyAllMocks();
540
541 // Step 3. Entering the callback of task1.
542 task1PluginInstance.prologue();
543 Mock4JS.verifyAllMocks();
544
545 // Step 4. Leaving the callback of task1.
546 // Expectations.
547 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
548 // Invocation.
549 task1PluginInstance.epilogue();
550 });
551
552 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerBranching', function() {
553 // Tests that task manager correctly detects completion of tasks that create
554 // branching chains of callbacks.
robliao 2013/09/03 23:23:31 Detail the tested branching pattern.
vadimt 2013/09/04 00:29:16 Done.
555
556 // Setup.
557 var test = setUpTaskManagerTest(this);
558 var task1PluginInstance1, task1PluginInstance2, task1PluginInstance3;
559
560 // Step 1. Adding 1st task that creates a 2 pending callbacks.
561 // Expectations.
562 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
563 will(callFunction(function() {
564 task1PluginInstance1 = test.pluginFactory();
565 task1PluginInstance2 = test.pluginFactory();
566 }));
567 // Invocation.
568 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
569 Mock4JS.verifyAllMocks();
570
571 // Step 2. Adding 2nd task.
572 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
robliao 2013/09/03 23:23:31 taskNameB?
vadimt 2013/09/04 00:29:16 Need 'C' because 'B' is conflicting and would be i
robliao 2013/09/04 00:54:00 That's not obvious from a reading of the test case
vadimt 2013/09/04 01:40:15 Done.
573 Mock4JS.verifyAllMocks();
574
575 // Step 3. Entering callback 1, create pending callback 3, exit callback 1.
576 // Enter/exit callback 2. Enter callback 3.
577 task1PluginInstance1.prologue();
578 task1PluginInstance3 = test.pluginFactory();
579 task1PluginInstance1.epilogue();
580 task1PluginInstance2.prologue();
581 task1PluginInstance2.epilogue();
582 task1PluginInstance3.prologue();
583 Mock4JS.verifyAllMocks();
584
585 // Step 4. Leaving 3rd callback of task1. Now task1 is complete, and task2
586 // should start.
587 // Expectations.
588 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
589 // Invocation.
590 task1PluginInstance3.epilogue();
591 });
592
593 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerSuspendError', function() {
594 // Tests that task manager's onSuspend method reports an error if there are
595 // pending tasks.
596
597 // Setup.
598 var test = setUpTaskManagerTest(this);
599 var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend');
600
601 // Step 1. Adding a task that creates a pending callback.
602 // Expectations.
603 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
604 will(callFunction(function() {
605 test.pluginFactory();
606 }));
607 // Invocation.
608 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
609 Mock4JS.verifyAllMocks();
610
611 // Step 2. Invoke onSuspend event of the task manager.
612 // Setup and expectations. The 2 callbacks in onSuspendHandlerContainer are
613 // from the wrapper and the task manager.
614 assertTrue(onSuspendHandlerContainer.length == 2,
615 'onSuspendHandlerContainer.length must be 2');
616 this.mockGlobals.expects(once()).reportError(eqToString(
617 'Error: ASSERT: Incomplete task when unloading event page,' +
618 ' queue = [{"name":"TASK A"}], testWrapperDebugState'));
619 // Invocation.
620 onSuspendHandlerContainer[1]();
621 });
622
623 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerSuspendSuccess', function() {
624 // Tests that task manager's onSuspend method does not reports an error if all
625 // tasks completed.
626
627 // Setup.
628 var test = setUpTaskManagerTest(this);
629 var onSuspendHandlerContainer = getMockHandlerContainer('runtime.onSuspend');
robliao 2013/09/03 23:23:31 Where is getMockHandlerContainer defined? Code sea
vadimt 2013/09/04 00:29:16 In https://codereview.chromium.org/23477006/diff/3
630 var task1PluginInstance;
631
632 // Step 1. Adding a task that creates a pending callback.
633 // Expectations.
634 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
635 will(callFunction(function() {
636 task1PluginInstance = test.pluginFactory();
637 }));
638 // Invocation.
639 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
640 Mock4JS.verifyAllMocks();
641
642 // Step 2. Invoke task's callback and the onSuspend event of the task manager.
643 // The 2 callbacks in onSuspendHandlerContainer are from the wrapper and the
644 // task manager.
645 task1PluginInstance.prologue();
646 task1PluginInstance.epilogue();
647 onSuspendHandlerContainer[1]();
648 });
649
650 TEST_F('GoogleNowUtilityUnitTest', 'TaskManager3Tasks', function() {
651 // Tests that 3 tasks can be executed too. In particular, that if the second
652 // task is a single-step task which execution was originally blocked by task1,
653 // unblocking it causes immediate synchronous execution of both tasks 2 and 3.
654
655 // Setup.
656 var test = setUpTaskManagerTest(this);
657 var task1PluginInstance;
658
659 // Step 1. Adding 1st task that creates a pending callback.
660 // Expectations.
661 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
662 will(callFunction(function() {
663 task1PluginInstance = test.pluginFactory();
664 }));
665 // Invocation.
666 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
667 Mock4JS.verifyAllMocks();
668
669 // Step 2. Adding 2nd and 3rd tasks.
670 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
robliao 2013/09/03 23:23:31 taskNameB?
vadimt 2013/09/04 00:29:16 Se above about conflicts.
671 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task3);
672 Mock4JS.verifyAllMocks();
673
674 // Step 3. Entering the callback of task1.
675 task1PluginInstance.prologue();
676 Mock4JS.verifyAllMocks();
677
678 // Step 4. Leaving the callback of task1.
679 // Expectations.
680 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
681 this.mockLocalFunctions.expects(once()).task3(ANYTHING);
682 // Invocation.
683 task1PluginInstance.epilogue();
684 });
685
686 TEST_F('GoogleNowUtilityUnitTest', 'TaskManagerNestedNonTask', function() {
687 // Tests callbacks requested while a task is running, but not from a callback
688 // belonging to a task, are not counted as a part of the task.
689
690 // Setup.
691 var test = setUpTaskManagerTest(this);
692 var task1PluginInstance, nonTaskPluginInstance;
693
694 // Step 1. Adding 1st task that creates a pending callback.
695 // Expectations.
696 this.mockLocalFunctions.expects(once()).task1(ANYTHING).
697 will(callFunction(function() {
698 task1PluginInstance = test.pluginFactory();
699 }));
700 // Invocation.
701 test.tasks.add(taskNameA, this.mockLocalFunctions.functions().task1);
702 Mock4JS.verifyAllMocks();
703
704 // Step 2. Code that is not a part of the task creates a pending callback.
705 nonTaskPluginInstance = test.pluginFactory();
robliao 2013/09/03 23:23:31 This variable is never read. Necessary?
vadimt 2013/09/04 00:29:16 Thanks!
706 Mock4JS.verifyAllMocks();
707
708 // Step 3. Entering the callback of task1. After this, task1 should be
709 // finished despite the pending non-task callback.
710 task1PluginInstance.prologue();
711 task1PluginInstance.epilogue();
712 Mock4JS.verifyAllMocks();
713
714 // Step 4. Checking that task1 is finished by submitting task2, which should
715 // be executed immediately.
716 this.mockLocalFunctions.expects(once()).task2(ANYTHING);
717 test.tasks.add(taskNameC, this.mockLocalFunctions.functions().task2);
718 });
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