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 27 matching lines...) Expand all Loading... |
38 } | 38 } |
39 | 39 |
40 void AddDispatcher(PlatformEventDispatcher* dispatcher) { | 40 void AddDispatcher(PlatformEventDispatcher* dispatcher) { |
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) { |
| 50 } |
49 virtual ~TestPlatformEventSource() {} | 51 virtual ~TestPlatformEventSource() {} |
50 | 52 |
51 uint32_t Dispatch(const PlatformEvent& event) { return DispatchEvent(event); } | 53 uint32_t Dispatch(const PlatformEvent& event) { return DispatchEvent(event); } |
52 | 54 |
53 // 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 |
54 // dispatched before it is requested to stop. | 56 // dispatched before it is requested to stop. |
55 size_t DispatchEventStream(const ScopedVector<PlatformEvent>& events) { | 57 size_t DispatchEventStream(const ScopedVector<PlatformEvent>& events) { |
| 58 stop_stream_ = false; |
56 for (size_t count = 0; count < events.size(); ++count) { | 59 for (size_t count = 0; count < events.size(); ++count) { |
57 uint32_t action = DispatchEvent(*events[count]); | 60 DispatchEvent(*events[count]); |
58 if (action & POST_DISPATCH_QUIT_LOOP) | 61 if (stop_stream_) |
59 return count + 1; | 62 return count + 1; |
60 } | 63 } |
61 return events.size(); | 64 return events.size(); |
62 } | 65 } |
63 | 66 |
| 67 // PlatformEventSource: |
| 68 virtual void StopCurrentEventStream() OVERRIDE { |
| 69 stop_stream_ = true; |
| 70 } |
| 71 |
64 private: | 72 private: |
| 73 bool stop_stream_; |
65 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); | 74 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); |
66 }; | 75 }; |
67 | 76 |
68 class TestPlatformEventDispatcher : public PlatformEventDispatcher { | 77 class TestPlatformEventDispatcher : public PlatformEventDispatcher { |
69 public: | 78 public: |
70 TestPlatformEventDispatcher(int id, std::vector<int>* list) | 79 TestPlatformEventDispatcher(int id, std::vector<int>* list) |
71 : id_(id), list_(list), post_dispatch_action_(POST_DISPATCH_NONE) { | 80 : id_(id), |
| 81 list_(list), |
| 82 post_dispatch_action_(POST_DISPATCH_NONE), |
| 83 stop_stream_(false) { |
72 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 84 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); |
73 } | 85 } |
74 virtual ~TestPlatformEventDispatcher() { | 86 virtual ~TestPlatformEventDispatcher() { |
75 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 87 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); |
76 } | 88 } |
77 | 89 |
78 void set_post_dispatch_action(uint32_t action) { | 90 void set_post_dispatch_action(uint32_t action) { |
79 post_dispatch_action_ = action; | 91 post_dispatch_action_ = action; |
80 } | 92 } |
81 | 93 |
82 protected: | 94 protected: |
83 // PlatformEventDispatcher: | 95 // PlatformEventDispatcher: |
84 virtual bool CanDispatchEvent(const PlatformEvent& event) OVERRIDE { | 96 virtual bool CanDispatchEvent(const PlatformEvent& event) OVERRIDE { |
85 return true; | 97 return true; |
86 } | 98 } |
87 | 99 |
88 virtual uint32_t DispatchEvent(const PlatformEvent& event) OVERRIDE { | 100 virtual uint32_t DispatchEvent(const PlatformEvent& event) OVERRIDE { |
89 list_->push_back(id_); | 101 list_->push_back(id_); |
90 return post_dispatch_action_; | 102 return post_dispatch_action_; |
91 } | 103 } |
92 | 104 |
93 private: | 105 private: |
94 int id_; | 106 int id_; |
95 std::vector<int>* list_; | 107 std::vector<int>* list_; |
96 uint32_t post_dispatch_action_; | 108 uint32_t post_dispatch_action_; |
| 109 bool stop_stream_; |
97 | 110 |
98 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); | 111 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); |
99 }; | 112 }; |
100 | 113 |
101 class TestPlatformEventObserver : public PlatformEventObserver { | 114 class TestPlatformEventObserver : public PlatformEventObserver { |
102 public: | 115 public: |
103 TestPlatformEventObserver(int id, std::vector<int>* list) | 116 TestPlatformEventObserver(int id, std::vector<int>* list) |
104 : id_(id), list_(list) { | 117 : id_(id), list_(list) { |
105 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); | 118 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); |
106 } | 119 } |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 class DestroyScopedHandleDispatcher : public TestPlatformEventDispatcher { | 568 class DestroyScopedHandleDispatcher : public TestPlatformEventDispatcher { |
556 public: | 569 public: |
557 DestroyScopedHandleDispatcher(int id, std::vector<int>* list) | 570 DestroyScopedHandleDispatcher(int id, std::vector<int>* list) |
558 : TestPlatformEventDispatcher(id, list) {} | 571 : TestPlatformEventDispatcher(id, list) {} |
559 virtual ~DestroyScopedHandleDispatcher() {} | 572 virtual ~DestroyScopedHandleDispatcher() {} |
560 | 573 |
561 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { | 574 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { |
562 handler_ = handler.Pass(); | 575 handler_ = handler.Pass(); |
563 } | 576 } |
564 | 577 |
| 578 void set_callback(const base::Closure& callback) { |
| 579 callback_ = callback; |
| 580 } |
| 581 |
565 private: | 582 private: |
566 // PlatformEventDispatcher: | 583 // PlatformEventDispatcher: |
567 virtual bool CanDispatchEvent(const PlatformEvent& event) OVERRIDE { | 584 virtual bool CanDispatchEvent(const PlatformEvent& event) OVERRIDE { |
568 return true; | 585 return true; |
569 } | 586 } |
570 | 587 |
571 virtual uint32_t DispatchEvent(const PlatformEvent& event) OVERRIDE { | 588 virtual uint32_t DispatchEvent(const PlatformEvent& event) OVERRIDE { |
572 handler_.reset(); | 589 handler_.reset(); |
573 return TestPlatformEventDispatcher::DispatchEvent(event); | 590 uint32_t action = TestPlatformEventDispatcher::DispatchEvent(event); |
| 591 if (!callback_.is_null()) { |
| 592 callback_.Run(); |
| 593 callback_ = base::Closure(); |
| 594 } |
| 595 return action; |
574 } | 596 } |
575 | 597 |
576 scoped_ptr<ScopedEventDispatcher> handler_; | 598 scoped_ptr<ScopedEventDispatcher> handler_; |
| 599 base::Closure callback_; |
577 | 600 |
578 DISALLOW_COPY_AND_ASSIGN(DestroyScopedHandleDispatcher); | 601 DISALLOW_COPY_AND_ASSIGN(DestroyScopedHandleDispatcher); |
579 }; | 602 }; |
580 | 603 |
581 // Tests that resetting an overridden dispatcher causes the nested message-loop | 604 // Tests that resetting an overridden dispatcher causes the nested message-loop |
582 // iteration to stop and the rest of the events are dispatched in the next | 605 // iteration to stop and the rest of the events are dispatched in the next |
583 // iteration. | 606 // iteration. |
584 class DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration | 607 class DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration |
585 : public PlatformEventTestWithMessageLoop { | 608 : public PlatformEventTestWithMessageLoop { |
586 public: | 609 public: |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 scoped_ptr<ScopedEventDispatcher> second_override_handle = | 712 scoped_ptr<ScopedEventDispatcher> second_override_handle = |
690 source()->OverrideDispatcher(&second_overriding); | 713 source()->OverrideDispatcher(&second_overriding); |
691 | 714 |
692 source()->Dispatch(*event); | 715 source()->Dispatch(*event); |
693 ASSERT_EQ(2u, list->size()); | 716 ASSERT_EQ(2u, list->size()); |
694 EXPECT_EQ(15, (*list)[0]); | 717 EXPECT_EQ(15, (*list)[0]); |
695 EXPECT_EQ(70, (*list)[1]); | 718 EXPECT_EQ(70, (*list)[1]); |
696 list->clear(); | 719 list->clear(); |
697 | 720 |
698 second_overriding.SetScopedHandle(second_override_handle.Pass()); | 721 second_overriding.SetScopedHandle(second_override_handle.Pass()); |
699 second_overriding.set_post_dispatch_action(POST_DISPATCH_QUIT_LOOP); | 722 second_overriding.set_post_dispatch_action(POST_DISPATCH_NONE); |
700 base::RunLoop run_loop; | 723 base::RunLoop run_loop; |
| 724 second_overriding.set_callback(run_loop.QuitClosure()); |
701 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 725 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
702 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 726 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
703 loop->PostTask( | 727 loop->PostTask( |
704 FROM_HERE, | 728 FROM_HERE, |
705 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), | 729 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), |
706 base::Unretained(source()), | 730 base::Unretained(source()), |
707 *event)); | 731 *event)); |
708 run_loop.Run(); | 732 run_loop.Run(); |
709 ASSERT_EQ(2u, list->size()); | 733 ASSERT_EQ(2u, list->size()); |
710 EXPECT_EQ(15, (*list)[0]); | 734 EXPECT_EQ(15, (*list)[0]); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 ASSERT_EQ(2u, list.size()); | 778 ASSERT_EQ(2u, list.size()); |
755 EXPECT_EQ(15, list[0]); | 779 EXPECT_EQ(15, list[0]); |
756 EXPECT_EQ(10, list[1]); | 780 EXPECT_EQ(10, list[1]); |
757 } | 781 } |
758 }; | 782 }; |
759 | 783 |
760 RUN_TEST_IN_MESSAGE_LOOP( | 784 RUN_TEST_IN_MESSAGE_LOOP( |
761 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
762 | 786 |
763 } // namespace ui | 787 } // namespace ui |
OLD | NEW |