OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "ui/events/platform/platform_event_source.h" | 5 #include "ui/events/platform/platform_event_source.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 } | 493 } |
494 | 494 |
495 // Provides mechanism for running tests from inside an active message-loop. | 495 // Provides mechanism for running tests from inside an active message-loop. |
496 class PlatformEventTestWithMessageLoop : public PlatformEventTest { | 496 class PlatformEventTestWithMessageLoop : public PlatformEventTest { |
497 public: | 497 public: |
498 PlatformEventTestWithMessageLoop() {} | 498 PlatformEventTestWithMessageLoop() {} |
499 ~PlatformEventTestWithMessageLoop() override {} | 499 ~PlatformEventTestWithMessageLoop() override {} |
500 | 500 |
501 void Run() { | 501 void Run() { |
502 message_loop_.task_runner()->PostTask( | 502 message_loop_.task_runner()->PostTask( |
503 FROM_HERE, base::Bind(&PlatformEventTestWithMessageLoop::RunTest, | 503 FROM_HERE, base::Bind(&PlatformEventTestWithMessageLoop::RunTestImpl, |
504 base::Unretained(this))); | 504 base::Unretained(this))); |
505 message_loop_.Run(); | 505 base::RunLoop().RunUntilIdle(); |
506 } | 506 } |
507 | 507 |
508 protected: | 508 protected: |
509 void RunTest() { | |
510 RunTestImpl(); | |
511 message_loop_.QuitWhenIdle(); | |
512 } | |
513 | |
514 virtual void RunTestImpl() = 0; | 509 virtual void RunTestImpl() = 0; |
515 | 510 |
516 private: | 511 private: |
517 base::MessageLoopForUI message_loop_; | 512 base::MessageLoopForUI message_loop_; |
518 | 513 |
519 DISALLOW_COPY_AND_ASSIGN(PlatformEventTestWithMessageLoop); | 514 DISALLOW_COPY_AND_ASSIGN(PlatformEventTestWithMessageLoop); |
520 }; | 515 }; |
521 | 516 |
522 #define RUN_TEST_IN_MESSAGE_LOOP(name) \ | 517 #define RUN_TEST_IN_MESSAGE_LOOP(name) \ |
523 TEST_F(name, Run) { Run(); } | 518 TEST_F(name, Run) { Run(); } |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 ASSERT_LT(count, events.size()); | 619 ASSERT_LT(count, events.size()); |
625 events.erase(events.begin(), events.begin() + count); | 620 events.erase(events.begin(), events.begin() + count); |
626 | 621 |
627 count = source()->DispatchEventStream(events); | 622 count = source()->DispatchEventStream(events); |
628 EXPECT_EQ(1u, count); | 623 EXPECT_EQ(1u, count); |
629 ASSERT_EQ(2u, list->size()); | 624 ASSERT_EQ(2u, list->size()); |
630 EXPECT_EQ(15, (*list)[0]); | 625 EXPECT_EQ(15, (*list)[0]); |
631 EXPECT_EQ(10, (*list)[1]); | 626 EXPECT_EQ(10, (*list)[1]); |
632 list->clear(); | 627 list->clear(); |
633 | 628 |
634 // Terminate the message-loop. | 629 // Terminate the run loop. |
635 base::MessageLoopForUI::current()->QuitNow(); | 630 run_loop_.Quit(); |
636 } | 631 } |
637 | 632 |
638 // PlatformEventTestWithMessageLoop: | 633 // PlatformEventTestWithMessageLoop: |
639 void RunTestImpl() override { | 634 void RunTestImpl() override { |
640 std::vector<int> list; | 635 std::vector<int> list; |
641 TestPlatformEventDispatcher dispatcher(10, &list); | 636 TestPlatformEventDispatcher dispatcher(10, &list); |
642 TestPlatformEventObserver observer(15, &list); | 637 TestPlatformEventObserver observer(15, &list); |
643 | 638 |
644 DestroyScopedHandleDispatcher overriding(20, &list); | 639 DestroyScopedHandleDispatcher overriding(20, &list); |
645 source()->RemovePlatformEventDispatcher(&overriding); | 640 source()->RemovePlatformEventDispatcher(&overriding); |
646 std::unique_ptr<ScopedEventDispatcher> override_handle = | 641 std::unique_ptr<ScopedEventDispatcher> override_handle = |
647 source()->OverrideDispatcher(&overriding); | 642 source()->OverrideDispatcher(&overriding); |
648 | 643 |
649 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); | 644 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); |
650 source()->Dispatch(*event); | 645 source()->Dispatch(*event); |
651 ASSERT_EQ(2u, list.size()); | 646 ASSERT_EQ(2u, list.size()); |
652 EXPECT_EQ(15, list[0]); | 647 EXPECT_EQ(15, list[0]); |
653 EXPECT_EQ(20, list[1]); | 648 EXPECT_EQ(20, list[1]); |
654 list.clear(); | 649 list.clear(); |
655 | 650 |
656 overriding.SetScopedHandle(std::move(override_handle)); | 651 overriding.SetScopedHandle(std::move(override_handle)); |
657 base::RunLoop run_loop; | |
658 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 652 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
659 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 653 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
660 loop->task_runner()->PostTask( | 654 loop->task_runner()->PostTask( |
661 FROM_HERE, | 655 FROM_HERE, |
662 base::Bind( | 656 base::Bind( |
663 &DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration:: | 657 &DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration:: |
664 NestedTask, | 658 NestedTask, |
665 base::Unretained(this), base::Unretained(&list), | 659 base::Unretained(this), base::Unretained(&list), |
666 base::Unretained(&overriding))); | 660 base::Unretained(&overriding))); |
667 run_loop.Run(); | 661 run_loop_.Run(); |
668 | 662 |
669 // Dispatching the event should now reach the default dispatcher. | 663 // Dispatching the event should now reach the default dispatcher. |
670 source()->Dispatch(*event); | 664 source()->Dispatch(*event); |
671 ASSERT_EQ(2u, list.size()); | 665 ASSERT_EQ(2u, list.size()); |
672 EXPECT_EQ(15, list[0]); | 666 EXPECT_EQ(15, list[0]); |
673 EXPECT_EQ(10, list[1]); | 667 EXPECT_EQ(10, list[1]); |
674 } | 668 } |
| 669 |
| 670 private: |
| 671 base::RunLoop run_loop_; |
675 }; | 672 }; |
676 | 673 |
677 RUN_TEST_IN_MESSAGE_LOOP( | 674 RUN_TEST_IN_MESSAGE_LOOP( |
678 DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration) | 675 DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration) |
679 | 676 |
680 // Tests that resetting an overridden dispatcher, and installing another | 677 // Tests that resetting an overridden dispatcher, and installing another |
681 // overridden dispatcher before the nested message-loop completely unwinds | 678 // overridden dispatcher before the nested message-loop completely unwinds |
682 // function correctly. | 679 // function correctly. |
683 class ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration | 680 class ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration |
684 : public PlatformEventTestWithMessageLoop { | 681 : public PlatformEventTestWithMessageLoop { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 FROM_HERE, | 720 FROM_HERE, |
724 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), | 721 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), |
725 base::Unretained(source()), *event)); | 722 base::Unretained(source()), *event)); |
726 run_loop.Run(); | 723 run_loop.Run(); |
727 ASSERT_EQ(2u, list->size()); | 724 ASSERT_EQ(2u, list->size()); |
728 EXPECT_EQ(15, (*list)[0]); | 725 EXPECT_EQ(15, (*list)[0]); |
729 EXPECT_EQ(70, (*list)[1]); | 726 EXPECT_EQ(70, (*list)[1]); |
730 list->clear(); | 727 list->clear(); |
731 | 728 |
732 // Terminate the message-loop. | 729 // Terminate the message-loop. |
733 base::MessageLoopForUI::current()->QuitNow(); | 730 run_loop_.Quit(); |
734 } | 731 } |
735 | 732 |
736 // PlatformEventTestWithMessageLoop: | 733 // PlatformEventTestWithMessageLoop: |
737 void RunTestImpl() override { | 734 void RunTestImpl() override { |
738 std::vector<int> list; | 735 std::vector<int> list; |
739 TestPlatformEventDispatcher dispatcher(10, &list); | 736 TestPlatformEventDispatcher dispatcher(10, &list); |
740 TestPlatformEventObserver observer(15, &list); | 737 TestPlatformEventObserver observer(15, &list); |
741 | 738 |
742 TestPlatformEventDispatcher overriding(20, &list); | 739 TestPlatformEventDispatcher overriding(20, &list); |
743 source()->RemovePlatformEventDispatcher(&overriding); | 740 source()->RemovePlatformEventDispatcher(&overriding); |
744 std::unique_ptr<ScopedEventDispatcher> override_handle = | 741 std::unique_ptr<ScopedEventDispatcher> override_handle = |
745 source()->OverrideDispatcher(&overriding); | 742 source()->OverrideDispatcher(&overriding); |
746 | 743 |
747 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); | 744 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); |
748 source()->Dispatch(*event); | 745 source()->Dispatch(*event); |
749 ASSERT_EQ(2u, list.size()); | 746 ASSERT_EQ(2u, list.size()); |
750 EXPECT_EQ(15, list[0]); | 747 EXPECT_EQ(15, list[0]); |
751 EXPECT_EQ(20, list[1]); | 748 EXPECT_EQ(20, list[1]); |
752 list.clear(); | 749 list.clear(); |
753 | 750 |
754 // Start a nested message-loop, and destroy |override_handle| in the nested | 751 // Start a nested message-loop, and destroy |override_handle| in the nested |
755 // loop. That should terminate the nested loop, restore the previous | 752 // loop. That should terminate the nested loop, restore the previous |
756 // dispatchers, and return control to this function. | 753 // dispatchers, and return control to this function. |
757 base::RunLoop run_loop; | |
758 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 754 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
759 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 755 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
760 loop->task_runner()->PostTask( | 756 loop->task_runner()->PostTask( |
761 FROM_HERE, | 757 FROM_HERE, |
762 base::Bind( | 758 base::Bind( |
763 &ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration:: | 759 &ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration:: |
764 NestedTask, | 760 NestedTask, |
765 base::Unretained(this), base::Passed(&override_handle), | 761 base::Unretained(this), base::Passed(&override_handle), |
766 base::Unretained(&list))); | 762 base::Unretained(&list))); |
767 run_loop.Run(); | 763 run_loop_.Run(); |
768 | 764 |
769 // Dispatching the event should now reach the default dispatcher. | 765 // Dispatching the event should now reach the default dispatcher. |
770 source()->Dispatch(*event); | 766 source()->Dispatch(*event); |
771 ASSERT_EQ(2u, list.size()); | 767 ASSERT_EQ(2u, list.size()); |
772 EXPECT_EQ(15, list[0]); | 768 EXPECT_EQ(15, list[0]); |
773 EXPECT_EQ(10, list[1]); | 769 EXPECT_EQ(10, list[1]); |
774 } | 770 } |
| 771 |
| 772 private: |
| 773 base::RunLoop run_loop_; |
775 }; | 774 }; |
776 | 775 |
777 RUN_TEST_IN_MESSAGE_LOOP( | 776 RUN_TEST_IN_MESSAGE_LOOP( |
778 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 777 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
779 | 778 |
780 } // namespace ui | 779 } // namespace ui |
OLD | NEW |