| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(dispatcher); | 41 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(dispatcher); |
| 42 } | 42 } |
| 43 | 43 |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 class TestPlatformEventSource : public PlatformEventSource { | 46 class TestPlatformEventSource : public PlatformEventSource { |
| 47 public: | 47 public: |
| 48 TestPlatformEventSource() | 48 TestPlatformEventSource() |
| 49 : stop_stream_(false) { | 49 : stop_stream_(false) { |
| 50 } | 50 } |
| 51 virtual ~TestPlatformEventSource() {} | 51 ~TestPlatformEventSource() override {} |
| 52 | 52 |
| 53 uint32_t Dispatch(const PlatformEvent& event) { return DispatchEvent(event); } | 53 uint32_t Dispatch(const PlatformEvent& event) { return DispatchEvent(event); } |
| 54 | 54 |
| 55 // Dispatches the stream of events, and returns the number of events that are | 55 // Dispatches the stream of events, and returns the number of events that are |
| 56 // dispatched before it is requested to stop. | 56 // dispatched before it is requested to stop. |
| 57 size_t DispatchEventStream(const ScopedVector<PlatformEvent>& events) { | 57 size_t DispatchEventStream(const ScopedVector<PlatformEvent>& events) { |
| 58 stop_stream_ = false; | 58 stop_stream_ = false; |
| 59 for (size_t count = 0; count < events.size(); ++count) { | 59 for (size_t count = 0; count < events.size(); ++count) { |
| 60 DispatchEvent(*events[count]); | 60 DispatchEvent(*events[count]); |
| 61 if (stop_stream_) | 61 if (stop_stream_) |
| 62 return count + 1; | 62 return count + 1; |
| 63 } | 63 } |
| 64 return events.size(); | 64 return events.size(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // PlatformEventSource: | 67 // PlatformEventSource: |
| 68 virtual void StopCurrentEventStream() override { | 68 void StopCurrentEventStream() override { stop_stream_ = true; } |
| 69 stop_stream_ = true; | |
| 70 } | |
| 71 | 69 |
| 72 private: | 70 private: |
| 73 bool stop_stream_; | 71 bool stop_stream_; |
| 74 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); | 72 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); |
| 75 }; | 73 }; |
| 76 | 74 |
| 77 class TestPlatformEventDispatcher : public PlatformEventDispatcher { | 75 class TestPlatformEventDispatcher : public PlatformEventDispatcher { |
| 78 public: | 76 public: |
| 79 TestPlatformEventDispatcher(int id, std::vector<int>* list) | 77 TestPlatformEventDispatcher(int id, std::vector<int>* list) |
| 80 : id_(id), | 78 : id_(id), |
| 81 list_(list), | 79 list_(list), |
| 82 post_dispatch_action_(POST_DISPATCH_NONE), | 80 post_dispatch_action_(POST_DISPATCH_NONE), |
| 83 stop_stream_(false) { | 81 stop_stream_(false) { |
| 84 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 82 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); |
| 85 } | 83 } |
| 86 virtual ~TestPlatformEventDispatcher() { | 84 ~TestPlatformEventDispatcher() override { |
| 87 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 85 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); |
| 88 } | 86 } |
| 89 | 87 |
| 90 void set_post_dispatch_action(uint32_t action) { | 88 void set_post_dispatch_action(uint32_t action) { |
| 91 post_dispatch_action_ = action; | 89 post_dispatch_action_ = action; |
| 92 } | 90 } |
| 93 | 91 |
| 94 protected: | 92 protected: |
| 95 // PlatformEventDispatcher: | 93 // PlatformEventDispatcher: |
| 96 virtual bool CanDispatchEvent(const PlatformEvent& event) override { | 94 bool CanDispatchEvent(const PlatformEvent& event) override { return true; } |
| 97 return true; | |
| 98 } | |
| 99 | 95 |
| 100 virtual uint32_t DispatchEvent(const PlatformEvent& event) override { | 96 uint32_t DispatchEvent(const PlatformEvent& event) override { |
| 101 list_->push_back(id_); | 97 list_->push_back(id_); |
| 102 return post_dispatch_action_; | 98 return post_dispatch_action_; |
| 103 } | 99 } |
| 104 | 100 |
| 105 private: | 101 private: |
| 106 int id_; | 102 int id_; |
| 107 std::vector<int>* list_; | 103 std::vector<int>* list_; |
| 108 uint32_t post_dispatch_action_; | 104 uint32_t post_dispatch_action_; |
| 109 bool stop_stream_; | 105 bool stop_stream_; |
| 110 | 106 |
| 111 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); | 107 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); |
| 112 }; | 108 }; |
| 113 | 109 |
| 114 class TestPlatformEventObserver : public PlatformEventObserver { | 110 class TestPlatformEventObserver : public PlatformEventObserver { |
| 115 public: | 111 public: |
| 116 TestPlatformEventObserver(int id, std::vector<int>* list) | 112 TestPlatformEventObserver(int id, std::vector<int>* list) |
| 117 : id_(id), list_(list) { | 113 : id_(id), list_(list) { |
| 118 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); | 114 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); |
| 119 } | 115 } |
| 120 virtual ~TestPlatformEventObserver() { | 116 ~TestPlatformEventObserver() override { |
| 121 PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); | 117 PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); |
| 122 } | 118 } |
| 123 | 119 |
| 124 protected: | 120 protected: |
| 125 // PlatformEventObserver: | 121 // PlatformEventObserver: |
| 126 virtual void WillProcessEvent(const PlatformEvent& event) override { | 122 void WillProcessEvent(const PlatformEvent& event) override { |
| 127 list_->push_back(id_); | 123 list_->push_back(id_); |
| 128 } | 124 } |
| 129 | 125 |
| 130 virtual void DidProcessEvent(const PlatformEvent& event) override {} | 126 void DidProcessEvent(const PlatformEvent& event) override {} |
| 131 | 127 |
| 132 private: | 128 private: |
| 133 int id_; | 129 int id_; |
| 134 std::vector<int>* list_; | 130 std::vector<int>* list_; |
| 135 | 131 |
| 136 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventObserver); | 132 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventObserver); |
| 137 }; | 133 }; |
| 138 | 134 |
| 139 class PlatformEventTest : public testing::Test { | 135 class PlatformEventTest : public testing::Test { |
| 140 public: | 136 public: |
| 141 PlatformEventTest() {} | 137 PlatformEventTest() {} |
| 142 virtual ~PlatformEventTest() {} | 138 ~PlatformEventTest() override {} |
| 143 | 139 |
| 144 TestPlatformEventSource* source() { return source_.get(); } | 140 TestPlatformEventSource* source() { return source_.get(); } |
| 145 | 141 |
| 146 protected: | 142 protected: |
| 147 // testing::Test: | 143 // testing::Test: |
| 148 virtual void SetUp() override { | 144 void SetUp() override { source_.reset(new TestPlatformEventSource()); } |
| 149 source_.reset(new TestPlatformEventSource()); | |
| 150 } | |
| 151 | 145 |
| 152 private: | 146 private: |
| 153 scoped_ptr<TestPlatformEventSource> source_; | 147 scoped_ptr<TestPlatformEventSource> source_; |
| 154 | 148 |
| 155 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); | 149 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); |
| 156 }; | 150 }; |
| 157 | 151 |
| 158 // Tests that a dispatcher receives an event. | 152 // Tests that a dispatcher receives an event. |
| 159 TEST_F(PlatformEventTest, DispatcherBasic) { | 153 TEST_F(PlatformEventTest, DispatcherBasic) { |
| 160 std::vector<int> list_dispatcher; | 154 std::vector<int> list_dispatcher; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 EXPECT_EQ(15, list[0]); | 321 EXPECT_EQ(15, list[0]); |
| 328 EXPECT_EQ(50, list[1]); | 322 EXPECT_EQ(50, list[1]); |
| 329 EXPECT_EQ(10, list[2]); | 323 EXPECT_EQ(10, list[2]); |
| 330 } | 324 } |
| 331 | 325 |
| 332 // Runs a callback during an event dispatch. | 326 // Runs a callback during an event dispatch. |
| 333 class RunCallbackDuringDispatch : public TestPlatformEventDispatcher { | 327 class RunCallbackDuringDispatch : public TestPlatformEventDispatcher { |
| 334 public: | 328 public: |
| 335 RunCallbackDuringDispatch(int id, std::vector<int>* list) | 329 RunCallbackDuringDispatch(int id, std::vector<int>* list) |
| 336 : TestPlatformEventDispatcher(id, list) {} | 330 : TestPlatformEventDispatcher(id, list) {} |
| 337 virtual ~RunCallbackDuringDispatch() {} | 331 ~RunCallbackDuringDispatch() override {} |
| 338 | 332 |
| 339 void set_callback(const base::Closure& callback) { | 333 void set_callback(const base::Closure& callback) { |
| 340 callback_ = callback; | 334 callback_ = callback; |
| 341 } | 335 } |
| 342 | 336 |
| 343 protected: | 337 protected: |
| 344 // PlatformEventDispatcher: | 338 // PlatformEventDispatcher: |
| 345 virtual uint32_t DispatchEvent(const PlatformEvent& event) override { | 339 uint32_t DispatchEvent(const PlatformEvent& event) override { |
| 346 if (!callback_.is_null()) | 340 if (!callback_.is_null()) |
| 347 callback_.Run(); | 341 callback_.Run(); |
| 348 return TestPlatformEventDispatcher::DispatchEvent(event); | 342 return TestPlatformEventDispatcher::DispatchEvent(event); |
| 349 } | 343 } |
| 350 | 344 |
| 351 private: | 345 private: |
| 352 base::Closure callback_; | 346 base::Closure callback_; |
| 353 | 347 |
| 354 DISALLOW_COPY_AND_ASSIGN(RunCallbackDuringDispatch); | 348 DISALLOW_COPY_AND_ASSIGN(RunCallbackDuringDispatch); |
| 355 }; | 349 }; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 EXPECT_EQ(10, list[0]); | 484 EXPECT_EQ(10, list[0]); |
| 491 EXPECT_EQ(15, list[1]); | 485 EXPECT_EQ(15, list[1]); |
| 492 EXPECT_EQ(20, list[2]); | 486 EXPECT_EQ(20, list[2]); |
| 493 EXPECT_EQ(30, list[3]); | 487 EXPECT_EQ(30, list[3]); |
| 494 } | 488 } |
| 495 | 489 |
| 496 // Provides mechanism for running tests from inside an active message-loop. | 490 // Provides mechanism for running tests from inside an active message-loop. |
| 497 class PlatformEventTestWithMessageLoop : public PlatformEventTest { | 491 class PlatformEventTestWithMessageLoop : public PlatformEventTest { |
| 498 public: | 492 public: |
| 499 PlatformEventTestWithMessageLoop() {} | 493 PlatformEventTestWithMessageLoop() {} |
| 500 virtual ~PlatformEventTestWithMessageLoop() {} | 494 ~PlatformEventTestWithMessageLoop() override {} |
| 501 | 495 |
| 502 void Run() { | 496 void Run() { |
| 503 message_loop_.PostTask( | 497 message_loop_.PostTask( |
| 504 FROM_HERE, | 498 FROM_HERE, |
| 505 base::Bind(&PlatformEventTestWithMessageLoop::RunTest, | 499 base::Bind(&PlatformEventTestWithMessageLoop::RunTest, |
| 506 base::Unretained(this))); | 500 base::Unretained(this))); |
| 507 message_loop_.Run(); | 501 message_loop_.Run(); |
| 508 } | 502 } |
| 509 | 503 |
| 510 protected: | 504 protected: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 523 | 517 |
| 524 #define RUN_TEST_IN_MESSAGE_LOOP(name) \ | 518 #define RUN_TEST_IN_MESSAGE_LOOP(name) \ |
| 525 TEST_F(name, Run) { Run(); } | 519 TEST_F(name, Run) { Run(); } |
| 526 | 520 |
| 527 // Tests that a ScopedEventDispatcher restores the previous dispatcher when | 521 // Tests that a ScopedEventDispatcher restores the previous dispatcher when |
| 528 // destroyed. | 522 // destroyed. |
| 529 class ScopedDispatcherRestoresAfterDestroy | 523 class ScopedDispatcherRestoresAfterDestroy |
| 530 : public PlatformEventTestWithMessageLoop { | 524 : public PlatformEventTestWithMessageLoop { |
| 531 public: | 525 public: |
| 532 // PlatformEventTestWithMessageLoop: | 526 // PlatformEventTestWithMessageLoop: |
| 533 virtual void RunTestImpl() override { | 527 void RunTestImpl() override { |
| 534 std::vector<int> list; | 528 std::vector<int> list; |
| 535 TestPlatformEventDispatcher dispatcher(10, &list); | 529 TestPlatformEventDispatcher dispatcher(10, &list); |
| 536 TestPlatformEventObserver observer(15, &list); | 530 TestPlatformEventObserver observer(15, &list); |
| 537 | 531 |
| 538 TestPlatformEventDispatcher first_overriding(20, &list); | 532 TestPlatformEventDispatcher first_overriding(20, &list); |
| 539 source()->RemovePlatformEventDispatcher(&first_overriding); | 533 source()->RemovePlatformEventDispatcher(&first_overriding); |
| 540 scoped_ptr<ScopedEventDispatcher> first_override_handle = | 534 scoped_ptr<ScopedEventDispatcher> first_override_handle = |
| 541 source()->OverrideDispatcher(&first_overriding); | 535 source()->OverrideDispatcher(&first_overriding); |
| 542 | 536 |
| 543 // Install a second overriding dispatcher. | 537 // Install a second overriding dispatcher. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 562 }; | 556 }; |
| 563 | 557 |
| 564 RUN_TEST_IN_MESSAGE_LOOP(ScopedDispatcherRestoresAfterDestroy) | 558 RUN_TEST_IN_MESSAGE_LOOP(ScopedDispatcherRestoresAfterDestroy) |
| 565 | 559 |
| 566 // This dispatcher destroys the handle to the ScopedEventDispatcher when | 560 // This dispatcher destroys the handle to the ScopedEventDispatcher when |
| 567 // dispatching an event. | 561 // dispatching an event. |
| 568 class DestroyScopedHandleDispatcher : public TestPlatformEventDispatcher { | 562 class DestroyScopedHandleDispatcher : public TestPlatformEventDispatcher { |
| 569 public: | 563 public: |
| 570 DestroyScopedHandleDispatcher(int id, std::vector<int>* list) | 564 DestroyScopedHandleDispatcher(int id, std::vector<int>* list) |
| 571 : TestPlatformEventDispatcher(id, list) {} | 565 : TestPlatformEventDispatcher(id, list) {} |
| 572 virtual ~DestroyScopedHandleDispatcher() {} | 566 ~DestroyScopedHandleDispatcher() override {} |
| 573 | 567 |
| 574 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { | 568 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { |
| 575 handler_ = handler.Pass(); | 569 handler_ = handler.Pass(); |
| 576 } | 570 } |
| 577 | 571 |
| 578 void set_callback(const base::Closure& callback) { | 572 void set_callback(const base::Closure& callback) { |
| 579 callback_ = callback; | 573 callback_ = callback; |
| 580 } | 574 } |
| 581 | 575 |
| 582 private: | 576 private: |
| 583 // PlatformEventDispatcher: | 577 // PlatformEventDispatcher: |
| 584 virtual bool CanDispatchEvent(const PlatformEvent& event) override { | 578 bool CanDispatchEvent(const PlatformEvent& event) override { return true; } |
| 585 return true; | |
| 586 } | |
| 587 | 579 |
| 588 virtual uint32_t DispatchEvent(const PlatformEvent& event) override { | 580 uint32_t DispatchEvent(const PlatformEvent& event) override { |
| 589 handler_.reset(); | 581 handler_.reset(); |
| 590 uint32_t action = TestPlatformEventDispatcher::DispatchEvent(event); | 582 uint32_t action = TestPlatformEventDispatcher::DispatchEvent(event); |
| 591 if (!callback_.is_null()) { | 583 if (!callback_.is_null()) { |
| 592 callback_.Run(); | 584 callback_.Run(); |
| 593 callback_ = base::Closure(); | 585 callback_ = base::Closure(); |
| 594 } | 586 } |
| 595 return action; | 587 return action; |
| 596 } | 588 } |
| 597 | 589 |
| 598 scoped_ptr<ScopedEventDispatcher> handler_; | 590 scoped_ptr<ScopedEventDispatcher> handler_; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 ASSERT_EQ(2u, list->size()); | 625 ASSERT_EQ(2u, list->size()); |
| 634 EXPECT_EQ(15, (*list)[0]); | 626 EXPECT_EQ(15, (*list)[0]); |
| 635 EXPECT_EQ(10, (*list)[1]); | 627 EXPECT_EQ(10, (*list)[1]); |
| 636 list->clear(); | 628 list->clear(); |
| 637 | 629 |
| 638 // Terminate the message-loop. | 630 // Terminate the message-loop. |
| 639 base::MessageLoopForUI::current()->QuitNow(); | 631 base::MessageLoopForUI::current()->QuitNow(); |
| 640 } | 632 } |
| 641 | 633 |
| 642 // PlatformEventTestWithMessageLoop: | 634 // PlatformEventTestWithMessageLoop: |
| 643 virtual void RunTestImpl() override { | 635 void RunTestImpl() override { |
| 644 std::vector<int> list; | 636 std::vector<int> list; |
| 645 TestPlatformEventDispatcher dispatcher(10, &list); | 637 TestPlatformEventDispatcher dispatcher(10, &list); |
| 646 TestPlatformEventObserver observer(15, &list); | 638 TestPlatformEventObserver observer(15, &list); |
| 647 | 639 |
| 648 DestroyScopedHandleDispatcher overriding(20, &list); | 640 DestroyScopedHandleDispatcher overriding(20, &list); |
| 649 source()->RemovePlatformEventDispatcher(&overriding); | 641 source()->RemovePlatformEventDispatcher(&overriding); |
| 650 scoped_ptr<ScopedEventDispatcher> override_handle = | 642 scoped_ptr<ScopedEventDispatcher> override_handle = |
| 651 source()->OverrideDispatcher(&overriding); | 643 source()->OverrideDispatcher(&overriding); |
| 652 | 644 |
| 653 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 645 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 ASSERT_EQ(2u, list->size()); | 725 ASSERT_EQ(2u, list->size()); |
| 734 EXPECT_EQ(15, (*list)[0]); | 726 EXPECT_EQ(15, (*list)[0]); |
| 735 EXPECT_EQ(70, (*list)[1]); | 727 EXPECT_EQ(70, (*list)[1]); |
| 736 list->clear(); | 728 list->clear(); |
| 737 | 729 |
| 738 // Terminate the message-loop. | 730 // Terminate the message-loop. |
| 739 base::MessageLoopForUI::current()->QuitNow(); | 731 base::MessageLoopForUI::current()->QuitNow(); |
| 740 } | 732 } |
| 741 | 733 |
| 742 // PlatformEventTestWithMessageLoop: | 734 // PlatformEventTestWithMessageLoop: |
| 743 virtual void RunTestImpl() override { | 735 void RunTestImpl() override { |
| 744 std::vector<int> list; | 736 std::vector<int> list; |
| 745 TestPlatformEventDispatcher dispatcher(10, &list); | 737 TestPlatformEventDispatcher dispatcher(10, &list); |
| 746 TestPlatformEventObserver observer(15, &list); | 738 TestPlatformEventObserver observer(15, &list); |
| 747 | 739 |
| 748 TestPlatformEventDispatcher overriding(20, &list); | 740 TestPlatformEventDispatcher overriding(20, &list); |
| 749 source()->RemovePlatformEventDispatcher(&overriding); | 741 source()->RemovePlatformEventDispatcher(&overriding); |
| 750 scoped_ptr<ScopedEventDispatcher> override_handle = | 742 scoped_ptr<ScopedEventDispatcher> override_handle = |
| 751 source()->OverrideDispatcher(&overriding); | 743 source()->OverrideDispatcher(&overriding); |
| 752 | 744 |
| 753 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 745 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 778 ASSERT_EQ(2u, list.size()); | 770 ASSERT_EQ(2u, list.size()); |
| 779 EXPECT_EQ(15, list[0]); | 771 EXPECT_EQ(15, list[0]); |
| 780 EXPECT_EQ(10, list[1]); | 772 EXPECT_EQ(10, list[1]); |
| 781 } | 773 } |
| 782 }; | 774 }; |
| 783 | 775 |
| 784 RUN_TEST_IN_MESSAGE_LOOP( | 776 RUN_TEST_IN_MESSAGE_LOOP( |
| 785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 777 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
| 786 | 778 |
| 787 } // namespace ui | 779 } // namespace ui |
| OLD | NEW |