OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/event_dispatcher.h" | 5 #include "ui/events/event_dispatcher.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
9 #include "ui/events/event_dispatcher.h" | 9 #include "ui/events/event_dispatcher.h" |
10 #include "ui/events/event_target.h" | 10 #include "ui/events/event_target.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 const std::vector<int>& handler_list() const { return handler_list_; } | 32 const std::vector<int>& handler_list() const { return handler_list_; } |
33 | 33 |
34 void Reset() { | 34 void Reset() { |
35 handler_list_.clear(); | 35 handler_list_.clear(); |
36 valid_ = true; | 36 valid_ = true; |
37 } | 37 } |
38 | 38 |
39 private: | 39 private: |
40 // Overridden from EventTarget: | 40 // Overridden from EventTarget: |
41 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE { | 41 virtual bool CanAcceptEvent(const ui::Event& event) override { |
42 return true; | 42 return true; |
43 } | 43 } |
44 | 44 |
45 virtual EventTarget* GetParentTarget() OVERRIDE { | 45 virtual EventTarget* GetParentTarget() override { |
46 return parent_; | 46 return parent_; |
47 } | 47 } |
48 | 48 |
49 virtual scoped_ptr<EventTargetIterator> GetChildIterator() const OVERRIDE { | 49 virtual scoped_ptr<EventTargetIterator> GetChildIterator() const override { |
50 return scoped_ptr<EventTargetIterator>(); | 50 return scoped_ptr<EventTargetIterator>(); |
51 } | 51 } |
52 | 52 |
53 virtual EventTargeter* GetEventTargeter() OVERRIDE { | 53 virtual EventTargeter* GetEventTargeter() override { |
54 return NULL; | 54 return NULL; |
55 } | 55 } |
56 | 56 |
57 TestTarget* parent_; | 57 TestTarget* parent_; |
58 std::vector<int> handler_list_; | 58 std::vector<int> handler_list_; |
59 bool valid_; | 59 bool valid_; |
60 | 60 |
61 DISALLOW_COPY_AND_ASSIGN(TestTarget); | 61 DISALLOW_COPY_AND_ASSIGN(TestTarget); |
62 }; | 62 }; |
63 | 63 |
(...skipping 23 matching lines...) Expand all Loading... |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 void set_event_result(EventResult result) { event_result_ = result; } | 90 void set_event_result(EventResult result) { event_result_ = result; } |
91 | 91 |
92 void set_expect_pre_target(bool expect) { expect_pre_target_ = expect; } | 92 void set_expect_pre_target(bool expect) { expect_pre_target_ = expect; } |
93 void set_expect_post_target(bool expect) { expect_post_target_ = expect; } | 93 void set_expect_post_target(bool expect) { expect_post_target_ = expect; } |
94 | 94 |
95 private: | 95 private: |
96 // Overridden from EventHandler: | 96 // Overridden from EventHandler: |
97 virtual void OnEvent(Event* event) OVERRIDE { | 97 virtual void OnEvent(Event* event) override { |
98 ui::EventHandler::OnEvent(event); | 98 ui::EventHandler::OnEvent(event); |
99 ReceivedEvent(event); | 99 ReceivedEvent(event); |
100 SetStatusOnEvent(event); | 100 SetStatusOnEvent(event); |
101 } | 101 } |
102 | 102 |
103 void SetStatusOnEvent(Event* event) { | 103 void SetStatusOnEvent(Event* event) { |
104 if (event_result_ & ui::ER_CONSUMED) | 104 if (event_result_ & ui::ER_CONSUMED) |
105 event->StopPropagation(); | 105 event->StopPropagation(); |
106 if (event_result_ & ui::ER_HANDLED) | 106 if (event_result_ & ui::ER_HANDLED) |
107 event->SetHandled(); | 107 event->SetHandled(); |
(...skipping 26 matching lines...) Expand all Loading... |
134 public: | 134 public: |
135 EventHandlerDestroyDispatcherDelegate(EventDispatcherDelegate* delegate, | 135 EventHandlerDestroyDispatcherDelegate(EventDispatcherDelegate* delegate, |
136 int id) | 136 int id) |
137 : TestEventHandler(id), | 137 : TestEventHandler(id), |
138 dispatcher_delegate_(delegate) { | 138 dispatcher_delegate_(delegate) { |
139 } | 139 } |
140 | 140 |
141 virtual ~EventHandlerDestroyDispatcherDelegate() {} | 141 virtual ~EventHandlerDestroyDispatcherDelegate() {} |
142 | 142 |
143 private: | 143 private: |
144 virtual void ReceivedEvent(Event* event) OVERRIDE { | 144 virtual void ReceivedEvent(Event* event) override { |
145 TestEventHandler::ReceivedEvent(event); | 145 TestEventHandler::ReceivedEvent(event); |
146 delete dispatcher_delegate_; | 146 delete dispatcher_delegate_; |
147 } | 147 } |
148 | 148 |
149 EventDispatcherDelegate* dispatcher_delegate_; | 149 EventDispatcherDelegate* dispatcher_delegate_; |
150 | 150 |
151 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyDispatcherDelegate); | 151 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyDispatcherDelegate); |
152 }; | 152 }; |
153 | 153 |
154 // Invalidates the target when it receives any event. | 154 // Invalidates the target when it receives any event. |
155 class InvalidateTargetEventHandler : public TestEventHandler { | 155 class InvalidateTargetEventHandler : public TestEventHandler { |
156 public: | 156 public: |
157 explicit InvalidateTargetEventHandler(int id) : TestEventHandler(id) {} | 157 explicit InvalidateTargetEventHandler(int id) : TestEventHandler(id) {} |
158 virtual ~InvalidateTargetEventHandler() {} | 158 virtual ~InvalidateTargetEventHandler() {} |
159 | 159 |
160 private: | 160 private: |
161 virtual void ReceivedEvent(Event* event) OVERRIDE { | 161 virtual void ReceivedEvent(Event* event) override { |
162 TestEventHandler::ReceivedEvent(event); | 162 TestEventHandler::ReceivedEvent(event); |
163 TestTarget* target = static_cast<TestTarget*>(event->target()); | 163 TestTarget* target = static_cast<TestTarget*>(event->target()); |
164 target->set_valid(false); | 164 target->set_valid(false); |
165 } | 165 } |
166 | 166 |
167 DISALLOW_COPY_AND_ASSIGN(InvalidateTargetEventHandler); | 167 DISALLOW_COPY_AND_ASSIGN(InvalidateTargetEventHandler); |
168 }; | 168 }; |
169 | 169 |
170 // Destroys a second event handler when this handler gets an event. | 170 // Destroys a second event handler when this handler gets an event. |
171 // Optionally also destroys the dispatcher. | 171 // Optionally also destroys the dispatcher. |
172 class EventHandlerDestroyer : public TestEventHandler { | 172 class EventHandlerDestroyer : public TestEventHandler { |
173 public: | 173 public: |
174 EventHandlerDestroyer(int id, EventHandler* destroy) | 174 EventHandlerDestroyer(int id, EventHandler* destroy) |
175 : TestEventHandler(id), | 175 : TestEventHandler(id), |
176 to_destroy_(destroy), | 176 to_destroy_(destroy), |
177 dispatcher_delegate_(NULL) { | 177 dispatcher_delegate_(NULL) { |
178 } | 178 } |
179 | 179 |
180 virtual ~EventHandlerDestroyer() { | 180 virtual ~EventHandlerDestroyer() { |
181 CHECK(!to_destroy_); | 181 CHECK(!to_destroy_); |
182 } | 182 } |
183 | 183 |
184 void set_dispatcher_delegate(EventDispatcherDelegate* dispatcher_delegate) { | 184 void set_dispatcher_delegate(EventDispatcherDelegate* dispatcher_delegate) { |
185 dispatcher_delegate_ = dispatcher_delegate; | 185 dispatcher_delegate_ = dispatcher_delegate; |
186 } | 186 } |
187 | 187 |
188 private: | 188 private: |
189 virtual void ReceivedEvent(Event* event) OVERRIDE { | 189 virtual void ReceivedEvent(Event* event) override { |
190 TestEventHandler::ReceivedEvent(event); | 190 TestEventHandler::ReceivedEvent(event); |
191 delete to_destroy_; | 191 delete to_destroy_; |
192 to_destroy_ = NULL; | 192 to_destroy_ = NULL; |
193 | 193 |
194 if (dispatcher_delegate_) { | 194 if (dispatcher_delegate_) { |
195 delete dispatcher_delegate_; | 195 delete dispatcher_delegate_; |
196 dispatcher_delegate_ = NULL; | 196 dispatcher_delegate_ = NULL; |
197 } | 197 } |
198 } | 198 } |
199 | 199 |
200 EventHandler* to_destroy_; | 200 EventHandler* to_destroy_; |
201 EventDispatcherDelegate* dispatcher_delegate_; | 201 EventDispatcherDelegate* dispatcher_delegate_; |
202 | 202 |
203 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyer); | 203 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyer); |
204 }; | 204 }; |
205 | 205 |
206 class TestEventDispatcher : public EventDispatcherDelegate { | 206 class TestEventDispatcher : public EventDispatcherDelegate { |
207 public: | 207 public: |
208 TestEventDispatcher() {} | 208 TestEventDispatcher() {} |
209 | 209 |
210 virtual ~TestEventDispatcher() {} | 210 virtual ~TestEventDispatcher() {} |
211 | 211 |
212 EventDispatchDetails ProcessEvent(EventTarget* target, Event* event) { | 212 EventDispatchDetails ProcessEvent(EventTarget* target, Event* event) { |
213 return DispatchEvent(target, event); | 213 return DispatchEvent(target, event); |
214 } | 214 } |
215 | 215 |
216 private: | 216 private: |
217 // Overridden from EventDispatcherDelegate: | 217 // Overridden from EventDispatcherDelegate: |
218 virtual bool CanDispatchToTarget(EventTarget* target) OVERRIDE { | 218 virtual bool CanDispatchToTarget(EventTarget* target) override { |
219 TestTarget* test_target = static_cast<TestTarget*>(target); | 219 TestTarget* test_target = static_cast<TestTarget*>(target); |
220 return test_target->valid(); | 220 return test_target->valid(); |
221 } | 221 } |
222 | 222 |
223 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher); | 223 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher); |
224 }; | 224 }; |
225 | 225 |
226 } // namespace | 226 } // namespace |
227 | 227 |
228 TEST(EventDispatcherTest, EventDispatchOrder) { | 228 TEST(EventDispatcherTest, EventDispatchOrder) { |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 NonCancelableEvent event; | 600 NonCancelableEvent event; |
601 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &event); | 601 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &event); |
602 EXPECT_TRUE(details.dispatcher_destroyed); | 602 EXPECT_TRUE(details.dispatcher_destroyed); |
603 EXPECT_EQ(2U, target.handler_list().size()); | 603 EXPECT_EQ(2U, target.handler_list().size()); |
604 EXPECT_EQ(1, target.handler_list()[0]); | 604 EXPECT_EQ(1, target.handler_list()[0]); |
605 EXPECT_EQ(2, target.handler_list()[1]); | 605 EXPECT_EQ(2, target.handler_list()[1]); |
606 } | 606 } |
607 } | 607 } |
608 | 608 |
609 } // namespace ui | 609 } // namespace ui |
OLD | NEW |