| 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> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/single_thread_task_runner.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/events/platform/platform_event_dispatcher.h" | 20 #include "ui/events/platform/platform_event_dispatcher.h" |
| 20 #include "ui/events/platform/platform_event_observer.h" | 21 #include "ui/events/platform/platform_event_observer.h" |
| 21 #include "ui/events/platform/scoped_event_dispatcher.h" | 22 #include "ui/events/platform/scoped_event_dispatcher.h" |
| 22 | 23 |
| 23 namespace ui { | 24 namespace ui { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 std::unique_ptr<PlatformEvent> CreatePlatformEvent() { | 28 std::unique_ptr<PlatformEvent> CreatePlatformEvent() { |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 EXPECT_EQ(30, list[3]); | 492 EXPECT_EQ(30, list[3]); |
| 492 } | 493 } |
| 493 | 494 |
| 494 // Provides mechanism for running tests from inside an active message-loop. | 495 // Provides mechanism for running tests from inside an active message-loop. |
| 495 class PlatformEventTestWithMessageLoop : public PlatformEventTest { | 496 class PlatformEventTestWithMessageLoop : public PlatformEventTest { |
| 496 public: | 497 public: |
| 497 PlatformEventTestWithMessageLoop() {} | 498 PlatformEventTestWithMessageLoop() {} |
| 498 ~PlatformEventTestWithMessageLoop() override {} | 499 ~PlatformEventTestWithMessageLoop() override {} |
| 499 | 500 |
| 500 void Run() { | 501 void Run() { |
| 501 message_loop_.PostTask( | 502 message_loop_.task_runner()->PostTask( |
| 502 FROM_HERE, | 503 FROM_HERE, base::Bind(&PlatformEventTestWithMessageLoop::RunTest, |
| 503 base::Bind(&PlatformEventTestWithMessageLoop::RunTest, | 504 base::Unretained(this))); |
| 504 base::Unretained(this))); | |
| 505 message_loop_.Run(); | 505 message_loop_.Run(); |
| 506 } | 506 } |
| 507 | 507 |
| 508 protected: | 508 protected: |
| 509 void RunTest() { | 509 void RunTest() { |
| 510 RunTestImpl(); | 510 RunTestImpl(); |
| 511 message_loop_.QuitWhenIdle(); | 511 message_loop_.QuitWhenIdle(); |
| 512 } | 512 } |
| 513 | 513 |
| 514 virtual void RunTestImpl() = 0; | 514 virtual void RunTestImpl() = 0; |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 source()->Dispatch(*event); | 650 source()->Dispatch(*event); |
| 651 ASSERT_EQ(2u, list.size()); | 651 ASSERT_EQ(2u, list.size()); |
| 652 EXPECT_EQ(15, list[0]); | 652 EXPECT_EQ(15, list[0]); |
| 653 EXPECT_EQ(20, list[1]); | 653 EXPECT_EQ(20, list[1]); |
| 654 list.clear(); | 654 list.clear(); |
| 655 | 655 |
| 656 overriding.SetScopedHandle(std::move(override_handle)); | 656 overriding.SetScopedHandle(std::move(override_handle)); |
| 657 base::RunLoop run_loop; | 657 base::RunLoop run_loop; |
| 658 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 658 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 659 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 659 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
| 660 loop->PostTask( | 660 loop->task_runner()->PostTask( |
| 661 FROM_HERE, | 661 FROM_HERE, |
| 662 base::Bind( | 662 base::Bind( |
| 663 &DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration:: | 663 &DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration:: |
| 664 NestedTask, | 664 NestedTask, |
| 665 base::Unretained(this), | 665 base::Unretained(this), base::Unretained(&list), |
| 666 base::Unretained(&list), | |
| 667 base::Unretained(&overriding))); | 666 base::Unretained(&overriding))); |
| 668 run_loop.Run(); | 667 run_loop.Run(); |
| 669 | 668 |
| 670 // Dispatching the event should now reach the default dispatcher. | 669 // Dispatching the event should now reach the default dispatcher. |
| 671 source()->Dispatch(*event); | 670 source()->Dispatch(*event); |
| 672 ASSERT_EQ(2u, list.size()); | 671 ASSERT_EQ(2u, list.size()); |
| 673 EXPECT_EQ(15, list[0]); | 672 EXPECT_EQ(15, list[0]); |
| 674 EXPECT_EQ(10, list[1]); | 673 EXPECT_EQ(10, list[1]); |
| 675 } | 674 } |
| 676 }; | 675 }; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 EXPECT_EQ(15, (*list)[0]); | 712 EXPECT_EQ(15, (*list)[0]); |
| 714 EXPECT_EQ(70, (*list)[1]); | 713 EXPECT_EQ(70, (*list)[1]); |
| 715 list->clear(); | 714 list->clear(); |
| 716 | 715 |
| 717 second_overriding.SetScopedHandle(std::move(second_override_handle)); | 716 second_overriding.SetScopedHandle(std::move(second_override_handle)); |
| 718 second_overriding.set_post_dispatch_action(POST_DISPATCH_NONE); | 717 second_overriding.set_post_dispatch_action(POST_DISPATCH_NONE); |
| 719 base::RunLoop run_loop; | 718 base::RunLoop run_loop; |
| 720 second_overriding.set_callback(run_loop.QuitClosure()); | 719 second_overriding.set_callback(run_loop.QuitClosure()); |
| 721 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 720 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 722 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 721 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
| 723 loop->PostTask( | 722 loop->task_runner()->PostTask( |
| 724 FROM_HERE, | 723 FROM_HERE, |
| 725 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), | 724 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), |
| 726 base::Unretained(source()), | 725 base::Unretained(source()), *event)); |
| 727 *event)); | |
| 728 run_loop.Run(); | 726 run_loop.Run(); |
| 729 ASSERT_EQ(2u, list->size()); | 727 ASSERT_EQ(2u, list->size()); |
| 730 EXPECT_EQ(15, (*list)[0]); | 728 EXPECT_EQ(15, (*list)[0]); |
| 731 EXPECT_EQ(70, (*list)[1]); | 729 EXPECT_EQ(70, (*list)[1]); |
| 732 list->clear(); | 730 list->clear(); |
| 733 | 731 |
| 734 // Terminate the message-loop. | 732 // Terminate the message-loop. |
| 735 base::MessageLoopForUI::current()->QuitNow(); | 733 base::MessageLoopForUI::current()->QuitNow(); |
| 736 } | 734 } |
| 737 | 735 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 752 EXPECT_EQ(15, list[0]); | 750 EXPECT_EQ(15, list[0]); |
| 753 EXPECT_EQ(20, list[1]); | 751 EXPECT_EQ(20, list[1]); |
| 754 list.clear(); | 752 list.clear(); |
| 755 | 753 |
| 756 // Start a nested message-loop, and destroy |override_handle| in the nested | 754 // Start a nested message-loop, and destroy |override_handle| in the nested |
| 757 // loop. That should terminate the nested loop, restore the previous | 755 // loop. That should terminate the nested loop, restore the previous |
| 758 // dispatchers, and return control to this function. | 756 // dispatchers, and return control to this function. |
| 759 base::RunLoop run_loop; | 757 base::RunLoop run_loop; |
| 760 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 758 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 761 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 759 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
| 762 loop->PostTask( | 760 loop->task_runner()->PostTask( |
| 763 FROM_HERE, | 761 FROM_HERE, |
| 764 base::Bind( | 762 base::Bind( |
| 765 &ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration:: | 763 &ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration:: |
| 766 NestedTask, | 764 NestedTask, |
| 767 base::Unretained(this), | 765 base::Unretained(this), base::Passed(&override_handle), |
| 768 base::Passed(&override_handle), | |
| 769 base::Unretained(&list))); | 766 base::Unretained(&list))); |
| 770 run_loop.Run(); | 767 run_loop.Run(); |
| 771 | 768 |
| 772 // Dispatching the event should now reach the default dispatcher. | 769 // Dispatching the event should now reach the default dispatcher. |
| 773 source()->Dispatch(*event); | 770 source()->Dispatch(*event); |
| 774 ASSERT_EQ(2u, list.size()); | 771 ASSERT_EQ(2u, list.size()); |
| 775 EXPECT_EQ(15, list[0]); | 772 EXPECT_EQ(15, list[0]); |
| 776 EXPECT_EQ(10, list[1]); | 773 EXPECT_EQ(10, list[1]); |
| 777 } | 774 } |
| 778 }; | 775 }; |
| 779 | 776 |
| 780 RUN_TEST_IN_MESSAGE_LOOP( | 777 RUN_TEST_IN_MESSAGE_LOOP( |
| 781 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 778 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
| 782 | 779 |
| 783 } // namespace ui | 780 } // namespace ui |
| OLD | NEW |