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 { |
69 stop_stream_ = true; | 69 stop_stream_ = true; |
70 } | 70 } |
71 | 71 |
72 private: | 72 private: |
73 bool stop_stream_; | 73 bool stop_stream_; |
74 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); | 74 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); |
75 }; | 75 }; |
76 | 76 |
77 class TestPlatformEventDispatcher : public PlatformEventDispatcher { | 77 class TestPlatformEventDispatcher : public PlatformEventDispatcher { |
78 public: | 78 public: |
79 TestPlatformEventDispatcher(int id, std::vector<int>* list) | 79 TestPlatformEventDispatcher(int id, std::vector<int>* list) |
80 : id_(id), | 80 : id_(id), |
81 list_(list), | 81 list_(list), |
82 post_dispatch_action_(POST_DISPATCH_NONE), | 82 post_dispatch_action_(POST_DISPATCH_NONE), |
83 stop_stream_(false) { | 83 stop_stream_(false) { |
84 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 84 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); |
85 } | 85 } |
86 virtual ~TestPlatformEventDispatcher() { | 86 ~TestPlatformEventDispatcher() override { |
87 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 87 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); |
88 } | 88 } |
89 | 89 |
90 void set_post_dispatch_action(uint32_t action) { | 90 void set_post_dispatch_action(uint32_t action) { |
91 post_dispatch_action_ = action; | 91 post_dispatch_action_ = action; |
92 } | 92 } |
93 | 93 |
94 protected: | 94 protected: |
95 // PlatformEventDispatcher: | 95 // PlatformEventDispatcher: |
96 virtual bool CanDispatchEvent(const PlatformEvent& event) override { | 96 bool CanDispatchEvent(const PlatformEvent& event) override { |
97 return true; | 97 return true; |
98 } | 98 } |
99 | 99 |
100 virtual uint32_t DispatchEvent(const PlatformEvent& event) override { | 100 uint32_t DispatchEvent(const PlatformEvent& event) override { |
101 list_->push_back(id_); | 101 list_->push_back(id_); |
102 return post_dispatch_action_; | 102 return post_dispatch_action_; |
103 } | 103 } |
104 | 104 |
105 private: | 105 private: |
106 int id_; | 106 int id_; |
107 std::vector<int>* list_; | 107 std::vector<int>* list_; |
108 uint32_t post_dispatch_action_; | 108 uint32_t post_dispatch_action_; |
109 bool stop_stream_; | 109 bool stop_stream_; |
110 | 110 |
111 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); | 111 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); |
112 }; | 112 }; |
113 | 113 |
114 class TestPlatformEventObserver : public PlatformEventObserver { | 114 class TestPlatformEventObserver : public PlatformEventObserver { |
115 public: | 115 public: |
116 TestPlatformEventObserver(int id, std::vector<int>* list) | 116 TestPlatformEventObserver(int id, std::vector<int>* list) |
117 : id_(id), list_(list) { | 117 : id_(id), list_(list) { |
118 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); | 118 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); |
119 } | 119 } |
120 virtual ~TestPlatformEventObserver() { | 120 ~TestPlatformEventObserver() override { |
121 PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); | 121 PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); |
122 } | 122 } |
123 | 123 |
124 protected: | 124 protected: |
125 // PlatformEventObserver: | 125 // PlatformEventObserver: |
126 virtual void WillProcessEvent(const PlatformEvent& event) override { | 126 void WillProcessEvent(const PlatformEvent& event) override { |
127 list_->push_back(id_); | 127 list_->push_back(id_); |
128 } | 128 } |
129 | 129 |
130 virtual void DidProcessEvent(const PlatformEvent& event) override {} | 130 void DidProcessEvent(const PlatformEvent& event) override {} |
131 | 131 |
132 private: | 132 private: |
133 int id_; | 133 int id_; |
134 std::vector<int>* list_; | 134 std::vector<int>* list_; |
135 | 135 |
136 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventObserver); | 136 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventObserver); |
137 }; | 137 }; |
138 | 138 |
139 class PlatformEventTest : public testing::Test { | 139 class PlatformEventTest : public testing::Test { |
140 public: | 140 public: |
141 PlatformEventTest() {} | 141 PlatformEventTest() {} |
142 virtual ~PlatformEventTest() {} | 142 ~PlatformEventTest() override {} |
143 | 143 |
144 TestPlatformEventSource* source() { return source_.get(); } | 144 TestPlatformEventSource* source() { return source_.get(); } |
145 | 145 |
146 protected: | 146 protected: |
147 // testing::Test: | 147 // testing::Test: |
148 virtual void SetUp() override { | 148 void SetUp() override { |
149 source_.reset(new TestPlatformEventSource()); | 149 source_.reset(new TestPlatformEventSource()); |
150 } | 150 } |
151 | 151 |
152 private: | 152 private: |
153 scoped_ptr<TestPlatformEventSource> source_; | 153 scoped_ptr<TestPlatformEventSource> source_; |
154 | 154 |
155 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); | 155 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); |
156 }; | 156 }; |
157 | 157 |
158 // Tests that a dispatcher receives an event. | 158 // Tests that a dispatcher receives an event. |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 EXPECT_EQ(15, list[0]); | 327 EXPECT_EQ(15, list[0]); |
328 EXPECT_EQ(50, list[1]); | 328 EXPECT_EQ(50, list[1]); |
329 EXPECT_EQ(10, list[2]); | 329 EXPECT_EQ(10, list[2]); |
330 } | 330 } |
331 | 331 |
332 // Runs a callback during an event dispatch. | 332 // Runs a callback during an event dispatch. |
333 class RunCallbackDuringDispatch : public TestPlatformEventDispatcher { | 333 class RunCallbackDuringDispatch : public TestPlatformEventDispatcher { |
334 public: | 334 public: |
335 RunCallbackDuringDispatch(int id, std::vector<int>* list) | 335 RunCallbackDuringDispatch(int id, std::vector<int>* list) |
336 : TestPlatformEventDispatcher(id, list) {} | 336 : TestPlatformEventDispatcher(id, list) {} |
337 virtual ~RunCallbackDuringDispatch() {} | 337 ~RunCallbackDuringDispatch() override {} |
338 | 338 |
339 void set_callback(const base::Closure& callback) { | 339 void set_callback(const base::Closure& callback) { |
340 callback_ = callback; | 340 callback_ = callback; |
341 } | 341 } |
342 | 342 |
343 protected: | 343 protected: |
344 // PlatformEventDispatcher: | 344 // PlatformEventDispatcher: |
345 virtual uint32_t DispatchEvent(const PlatformEvent& event) override { | 345 uint32_t DispatchEvent(const PlatformEvent& event) override { |
346 if (!callback_.is_null()) | 346 if (!callback_.is_null()) |
347 callback_.Run(); | 347 callback_.Run(); |
348 return TestPlatformEventDispatcher::DispatchEvent(event); | 348 return TestPlatformEventDispatcher::DispatchEvent(event); |
349 } | 349 } |
350 | 350 |
351 private: | 351 private: |
352 base::Closure callback_; | 352 base::Closure callback_; |
353 | 353 |
354 DISALLOW_COPY_AND_ASSIGN(RunCallbackDuringDispatch); | 354 DISALLOW_COPY_AND_ASSIGN(RunCallbackDuringDispatch); |
355 }; | 355 }; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 EXPECT_EQ(10, list[0]); | 490 EXPECT_EQ(10, list[0]); |
491 EXPECT_EQ(15, list[1]); | 491 EXPECT_EQ(15, list[1]); |
492 EXPECT_EQ(20, list[2]); | 492 EXPECT_EQ(20, list[2]); |
493 EXPECT_EQ(30, list[3]); | 493 EXPECT_EQ(30, list[3]); |
494 } | 494 } |
495 | 495 |
496 // Provides mechanism for running tests from inside an active message-loop. | 496 // Provides mechanism for running tests from inside an active message-loop. |
497 class PlatformEventTestWithMessageLoop : public PlatformEventTest { | 497 class PlatformEventTestWithMessageLoop : public PlatformEventTest { |
498 public: | 498 public: |
499 PlatformEventTestWithMessageLoop() {} | 499 PlatformEventTestWithMessageLoop() {} |
500 virtual ~PlatformEventTestWithMessageLoop() {} | 500 ~PlatformEventTestWithMessageLoop() override {} |
501 | 501 |
502 void Run() { | 502 void Run() { |
503 message_loop_.PostTask( | 503 message_loop_.PostTask( |
504 FROM_HERE, | 504 FROM_HERE, |
505 base::Bind(&PlatformEventTestWithMessageLoop::RunTest, | 505 base::Bind(&PlatformEventTestWithMessageLoop::RunTest, |
506 base::Unretained(this))); | 506 base::Unretained(this))); |
507 message_loop_.Run(); | 507 message_loop_.Run(); |
508 } | 508 } |
509 | 509 |
510 protected: | 510 protected: |
(...skipping 12 matching lines...) Expand all Loading... |
523 | 523 |
524 #define RUN_TEST_IN_MESSAGE_LOOP(name) \ | 524 #define RUN_TEST_IN_MESSAGE_LOOP(name) \ |
525 TEST_F(name, Run) { Run(); } | 525 TEST_F(name, Run) { Run(); } |
526 | 526 |
527 // Tests that a ScopedEventDispatcher restores the previous dispatcher when | 527 // Tests that a ScopedEventDispatcher restores the previous dispatcher when |
528 // destroyed. | 528 // destroyed. |
529 class ScopedDispatcherRestoresAfterDestroy | 529 class ScopedDispatcherRestoresAfterDestroy |
530 : public PlatformEventTestWithMessageLoop { | 530 : public PlatformEventTestWithMessageLoop { |
531 public: | 531 public: |
532 // PlatformEventTestWithMessageLoop: | 532 // PlatformEventTestWithMessageLoop: |
533 virtual void RunTestImpl() override { | 533 void RunTestImpl() override { |
534 std::vector<int> list; | 534 std::vector<int> list; |
535 TestPlatformEventDispatcher dispatcher(10, &list); | 535 TestPlatformEventDispatcher dispatcher(10, &list); |
536 TestPlatformEventObserver observer(15, &list); | 536 TestPlatformEventObserver observer(15, &list); |
537 | 537 |
538 TestPlatformEventDispatcher first_overriding(20, &list); | 538 TestPlatformEventDispatcher first_overriding(20, &list); |
539 source()->RemovePlatformEventDispatcher(&first_overriding); | 539 source()->RemovePlatformEventDispatcher(&first_overriding); |
540 scoped_ptr<ScopedEventDispatcher> first_override_handle = | 540 scoped_ptr<ScopedEventDispatcher> first_override_handle = |
541 source()->OverrideDispatcher(&first_overriding); | 541 source()->OverrideDispatcher(&first_overriding); |
542 | 542 |
543 // Install a second overriding dispatcher. | 543 // Install a second overriding dispatcher. |
(...skipping 18 matching lines...) Expand all Loading... |
562 }; | 562 }; |
563 | 563 |
564 RUN_TEST_IN_MESSAGE_LOOP(ScopedDispatcherRestoresAfterDestroy) | 564 RUN_TEST_IN_MESSAGE_LOOP(ScopedDispatcherRestoresAfterDestroy) |
565 | 565 |
566 // This dispatcher destroys the handle to the ScopedEventDispatcher when | 566 // This dispatcher destroys the handle to the ScopedEventDispatcher when |
567 // dispatching an event. | 567 // dispatching an event. |
568 class DestroyScopedHandleDispatcher : public TestPlatformEventDispatcher { | 568 class DestroyScopedHandleDispatcher : public TestPlatformEventDispatcher { |
569 public: | 569 public: |
570 DestroyScopedHandleDispatcher(int id, std::vector<int>* list) | 570 DestroyScopedHandleDispatcher(int id, std::vector<int>* list) |
571 : TestPlatformEventDispatcher(id, list) {} | 571 : TestPlatformEventDispatcher(id, list) {} |
572 virtual ~DestroyScopedHandleDispatcher() {} | 572 ~DestroyScopedHandleDispatcher() override {} |
573 | 573 |
574 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { | 574 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { |
575 handler_ = handler.Pass(); | 575 handler_ = handler.Pass(); |
576 } | 576 } |
577 | 577 |
578 void set_callback(const base::Closure& callback) { | 578 void set_callback(const base::Closure& callback) { |
579 callback_ = callback; | 579 callback_ = callback; |
580 } | 580 } |
581 | 581 |
582 private: | 582 private: |
583 // PlatformEventDispatcher: | 583 // PlatformEventDispatcher: |
584 virtual bool CanDispatchEvent(const PlatformEvent& event) override { | 584 bool CanDispatchEvent(const PlatformEvent& event) override { |
585 return true; | 585 return true; |
586 } | 586 } |
587 | 587 |
588 virtual uint32_t DispatchEvent(const PlatformEvent& event) override { | 588 uint32_t DispatchEvent(const PlatformEvent& event) override { |
589 handler_.reset(); | 589 handler_.reset(); |
590 uint32_t action = TestPlatformEventDispatcher::DispatchEvent(event); | 590 uint32_t action = TestPlatformEventDispatcher::DispatchEvent(event); |
591 if (!callback_.is_null()) { | 591 if (!callback_.is_null()) { |
592 callback_.Run(); | 592 callback_.Run(); |
593 callback_ = base::Closure(); | 593 callback_ = base::Closure(); |
594 } | 594 } |
595 return action; | 595 return action; |
596 } | 596 } |
597 | 597 |
598 scoped_ptr<ScopedEventDispatcher> handler_; | 598 scoped_ptr<ScopedEventDispatcher> handler_; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 ASSERT_EQ(2u, list->size()); | 633 ASSERT_EQ(2u, list->size()); |
634 EXPECT_EQ(15, (*list)[0]); | 634 EXPECT_EQ(15, (*list)[0]); |
635 EXPECT_EQ(10, (*list)[1]); | 635 EXPECT_EQ(10, (*list)[1]); |
636 list->clear(); | 636 list->clear(); |
637 | 637 |
638 // Terminate the message-loop. | 638 // Terminate the message-loop. |
639 base::MessageLoopForUI::current()->QuitNow(); | 639 base::MessageLoopForUI::current()->QuitNow(); |
640 } | 640 } |
641 | 641 |
642 // PlatformEventTestWithMessageLoop: | 642 // PlatformEventTestWithMessageLoop: |
643 virtual void RunTestImpl() override { | 643 void RunTestImpl() override { |
644 std::vector<int> list; | 644 std::vector<int> list; |
645 TestPlatformEventDispatcher dispatcher(10, &list); | 645 TestPlatformEventDispatcher dispatcher(10, &list); |
646 TestPlatformEventObserver observer(15, &list); | 646 TestPlatformEventObserver observer(15, &list); |
647 | 647 |
648 DestroyScopedHandleDispatcher overriding(20, &list); | 648 DestroyScopedHandleDispatcher overriding(20, &list); |
649 source()->RemovePlatformEventDispatcher(&overriding); | 649 source()->RemovePlatformEventDispatcher(&overriding); |
650 scoped_ptr<ScopedEventDispatcher> override_handle = | 650 scoped_ptr<ScopedEventDispatcher> override_handle = |
651 source()->OverrideDispatcher(&overriding); | 651 source()->OverrideDispatcher(&overriding); |
652 | 652 |
653 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 653 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 ASSERT_EQ(2u, list->size()); | 733 ASSERT_EQ(2u, list->size()); |
734 EXPECT_EQ(15, (*list)[0]); | 734 EXPECT_EQ(15, (*list)[0]); |
735 EXPECT_EQ(70, (*list)[1]); | 735 EXPECT_EQ(70, (*list)[1]); |
736 list->clear(); | 736 list->clear(); |
737 | 737 |
738 // Terminate the message-loop. | 738 // Terminate the message-loop. |
739 base::MessageLoopForUI::current()->QuitNow(); | 739 base::MessageLoopForUI::current()->QuitNow(); |
740 } | 740 } |
741 | 741 |
742 // PlatformEventTestWithMessageLoop: | 742 // PlatformEventTestWithMessageLoop: |
743 virtual void RunTestImpl() override { | 743 void RunTestImpl() override { |
744 std::vector<int> list; | 744 std::vector<int> list; |
745 TestPlatformEventDispatcher dispatcher(10, &list); | 745 TestPlatformEventDispatcher dispatcher(10, &list); |
746 TestPlatformEventObserver observer(15, &list); | 746 TestPlatformEventObserver observer(15, &list); |
747 | 747 |
748 TestPlatformEventDispatcher overriding(20, &list); | 748 TestPlatformEventDispatcher overriding(20, &list); |
749 source()->RemovePlatformEventDispatcher(&overriding); | 749 source()->RemovePlatformEventDispatcher(&overriding); |
750 scoped_ptr<ScopedEventDispatcher> override_handle = | 750 scoped_ptr<ScopedEventDispatcher> override_handle = |
751 source()->OverrideDispatcher(&overriding); | 751 source()->OverrideDispatcher(&overriding); |
752 | 752 |
753 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 753 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
(...skipping 24 matching lines...) Expand all Loading... |
778 ASSERT_EQ(2u, list.size()); | 778 ASSERT_EQ(2u, list.size()); |
779 EXPECT_EQ(15, list[0]); | 779 EXPECT_EQ(15, list[0]); |
780 EXPECT_EQ(10, list[1]); | 780 EXPECT_EQ(10, list[1]); |
781 } | 781 } |
782 }; | 782 }; |
783 | 783 |
784 RUN_TEST_IN_MESSAGE_LOOP( | 784 RUN_TEST_IN_MESSAGE_LOOP( |
785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
786 | 786 |
787 } // namespace ui | 787 } // namespace ui |
OLD | NEW |