OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <vector> | 5 #include <vector> |
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_target_iterator.h" |
9 #include "ui/events/event_targeter.h" | 10 #include "ui/events/event_targeter.h" |
| 11 #include "ui/events/event_utils.h" |
10 #include "ui/events/test/events_test_utils.h" | 12 #include "ui/events/test/events_test_utils.h" |
11 #include "ui/events/test/test_event_handler.h" | 13 #include "ui/events/test/test_event_handler.h" |
12 #include "ui/events/test/test_event_processor.h" | 14 #include "ui/events/test/test_event_processor.h" |
13 #include "ui/events/test/test_event_target.h" | 15 #include "ui/events/test/test_event_target.h" |
| 16 #include "ui/events/test/test_event_targeter.h" |
14 | 17 |
15 typedef std::vector<std::string> HandlerSequenceRecorder; | 18 typedef std::vector<std::string> HandlerSequenceRecorder; |
16 | 19 |
17 namespace ui { | 20 namespace ui { |
18 namespace test { | 21 namespace test { |
19 | 22 |
20 class EventProcessorTest : public testing::Test { | 23 class EventProcessorTest : public testing::Test { |
21 public: | 24 public: |
22 EventProcessorTest() {} | 25 EventProcessorTest() {} |
23 ~EventProcessorTest() override {} | 26 ~EventProcessorTest() override {} |
24 | 27 |
| 28 protected: |
25 // testing::Test: | 29 // testing::Test: |
26 void SetUp() override { | 30 void SetUp() override { |
27 processor_.SetRoot(scoped_ptr<EventTarget>(new TestEventTarget())); | 31 processor_.SetRoot(make_scoped_ptr(new TestEventTarget())); |
28 processor_.Reset(); | 32 processor_.Reset(); |
29 root()->SetEventTargeter(make_scoped_ptr(new EventTargeter())); | 33 root()->SetEventTargeter( |
| 34 make_scoped_ptr(new TestEventTargeter(root(), false))); |
30 } | 35 } |
31 | 36 |
32 TestEventTarget* root() { | 37 TestEventTarget* root() { |
33 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); | 38 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); |
34 } | 39 } |
35 | 40 |
36 TestEventProcessor* processor() { | 41 TestEventProcessor* processor() { |
37 return &processor_; | 42 return &processor_; |
38 } | 43 } |
39 | 44 |
40 void DispatchEvent(Event* event) { | 45 void DispatchEvent(Event* event) { |
41 processor_.OnEventFromSource(event); | 46 processor_.OnEventFromSource(event); |
42 } | 47 } |
43 | 48 |
44 protected: | 49 void SetTarget(TestEventTarget* target) { |
| 50 static_cast<TestEventTargeter*>(root()->GetEventTargeter()) |
| 51 ->set_target(target); |
| 52 } |
| 53 |
| 54 private: |
45 TestEventProcessor processor_; | 55 TestEventProcessor processor_; |
46 | 56 |
47 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); | 57 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); |
48 }; | 58 }; |
49 | 59 |
50 TEST_F(EventProcessorTest, Basic) { | 60 TEST_F(EventProcessorTest, Basic) { |
51 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 61 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 62 child->SetEventTargeter( |
| 63 make_scoped_ptr(new TestEventTargeter(child.get(), false))); |
| 64 SetTarget(child.get()); |
52 root()->AddChild(child.Pass()); | 65 root()->AddChild(child.Pass()); |
53 | 66 |
54 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 67 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
55 EF_NONE, EF_NONE); | 68 EventTimeForNow(), EF_NONE, EF_NONE); |
56 DispatchEvent(&mouse); | 69 DispatchEvent(&mouse); |
57 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 70 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
58 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 71 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
59 | 72 |
| 73 SetTarget(root()); |
60 root()->RemoveChild(root()->child_at(0)); | 74 root()->RemoveChild(root()->child_at(0)); |
61 DispatchEvent(&mouse); | 75 DispatchEvent(&mouse); |
62 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 76 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
63 } | 77 } |
64 | 78 |
65 template<typename T> | |
66 class BoundsEventTargeter : public EventTargeter { | |
67 public: | |
68 virtual ~BoundsEventTargeter() {} | |
69 | |
70 protected: | |
71 virtual bool SubtreeShouldBeExploredForEvent( | |
72 EventTarget* target, const LocatedEvent& event) override { | |
73 T* t = static_cast<T*>(target); | |
74 return (t->bounds().Contains(event.location())); | |
75 } | |
76 }; | |
77 | |
78 class BoundsTestTarget : public TestEventTarget { | |
79 public: | |
80 BoundsTestTarget() {} | |
81 ~BoundsTestTarget() override {} | |
82 | |
83 void set_bounds(gfx::Rect rect) { bounds_ = rect; } | |
84 gfx::Rect bounds() const { return bounds_; } | |
85 | |
86 static void ConvertPointToTarget(BoundsTestTarget* source, | |
87 BoundsTestTarget* target, | |
88 gfx::Point* location) { | |
89 gfx::Vector2d vector; | |
90 if (source->Contains(target)) { | |
91 for (; target && target != source; | |
92 target = static_cast<BoundsTestTarget*>(target->parent())) { | |
93 vector += target->bounds().OffsetFromOrigin(); | |
94 } | |
95 *location -= vector; | |
96 } else if (target->Contains(source)) { | |
97 for (; source && source != target; | |
98 source = static_cast<BoundsTestTarget*>(source->parent())) { | |
99 vector += source->bounds().OffsetFromOrigin(); | |
100 } | |
101 *location += vector; | |
102 } else { | |
103 NOTREACHED(); | |
104 } | |
105 } | |
106 | |
107 private: | |
108 // EventTarget: | |
109 void ConvertEventToTarget(EventTarget* target, | |
110 LocatedEvent* event) override { | |
111 event->ConvertLocationToTarget(this, | |
112 static_cast<BoundsTestTarget*>(target)); | |
113 } | |
114 | |
115 gfx::Rect bounds_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(BoundsTestTarget); | |
118 }; | |
119 | |
120 TEST_F(EventProcessorTest, Bounds) { | |
121 scoped_ptr<BoundsTestTarget> parent(new BoundsTestTarget()); | |
122 scoped_ptr<BoundsTestTarget> child(new BoundsTestTarget()); | |
123 scoped_ptr<BoundsTestTarget> grandchild(new BoundsTestTarget()); | |
124 | |
125 parent->set_bounds(gfx::Rect(0, 0, 30, 30)); | |
126 child->set_bounds(gfx::Rect(5, 5, 20, 20)); | |
127 grandchild->set_bounds(gfx::Rect(5, 5, 5, 5)); | |
128 | |
129 child->AddChild(scoped_ptr<TestEventTarget>(grandchild.Pass())); | |
130 parent->AddChild(scoped_ptr<TestEventTarget>(child.Pass())); | |
131 root()->AddChild(scoped_ptr<TestEventTarget>(parent.Pass())); | |
132 | |
133 ASSERT_EQ(1u, root()->child_count()); | |
134 ASSERT_EQ(1u, root()->child_at(0)->child_count()); | |
135 ASSERT_EQ(1u, root()->child_at(0)->child_at(0)->child_count()); | |
136 | |
137 TestEventTarget* parent_r = root()->child_at(0); | |
138 TestEventTarget* child_r = parent_r->child_at(0); | |
139 TestEventTarget* grandchild_r = child_r->child_at(0); | |
140 | |
141 // Dispatch a mouse event that falls on the parent, but not on the child. When | |
142 // the default event-targeter used, the event will still reach |grandchild|, | |
143 // because the default targeter does not look at the bounds. | |
144 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(1, 1), gfx::Point(1, 1), EF_NONE, | |
145 EF_NONE); | |
146 DispatchEvent(&mouse); | |
147 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | |
148 EXPECT_FALSE(parent_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
149 EXPECT_FALSE(child_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
150 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
151 grandchild_r->ResetReceivedEvents(); | |
152 | |
153 // Now install a targeter on the parent that looks at the bounds and makes | |
154 // sure the event reaches the target only if the location of the event within | |
155 // the bounds of the target. | |
156 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(1, 1), gfx::Point(1, 1), EF_NONE, | |
157 EF_NONE); | |
158 parent_r->SetEventTargeter(scoped_ptr<EventTargeter>( | |
159 new BoundsEventTargeter<BoundsTestTarget>())); | |
160 DispatchEvent(&mouse2); | |
161 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | |
162 EXPECT_TRUE(parent_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
163 EXPECT_FALSE(child_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
164 EXPECT_FALSE(grandchild_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
165 parent_r->ResetReceivedEvents(); | |
166 | |
167 MouseEvent second(ET_MOUSE_MOVED, gfx::Point(12, 12), gfx::Point(12, 12), | |
168 EF_NONE, EF_NONE); | |
169 DispatchEvent(&second); | |
170 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | |
171 EXPECT_FALSE(parent_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
172 EXPECT_FALSE(child_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
173 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_MOUSE_MOVED)); | |
174 } | |
175 | |
176 // ReDispatchEventHandler is used to receive mouse events and forward them | 79 // ReDispatchEventHandler is used to receive mouse events and forward them |
177 // to a specified EventProcessor. Verifies that the event has the correct | 80 // to a specified EventProcessor. Verifies that the event has the correct |
178 // target and phase both before and after the nested event processing. Also | 81 // target and phase both before and after the nested event processing. Also |
179 // verifies that the location of the event remains the same after it has | 82 // verifies that the location of the event remains the same after it has |
180 // been processed by the second EventProcessor. | 83 // been processed by the second EventProcessor. |
181 class ReDispatchEventHandler : public TestEventHandler { | 84 class ReDispatchEventHandler : public TestEventHandler { |
182 public: | 85 public: |
183 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target) | 86 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target) |
184 : processor_(processor), expected_target_(target) {} | 87 : processor_(processor), expected_target_(target) {} |
185 ~ReDispatchEventHandler() override {} | 88 ~ReDispatchEventHandler() override {} |
(...skipping 23 matching lines...) Expand all Loading... |
209 | 112 |
210 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler); | 113 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler); |
211 }; | 114 }; |
212 | 115 |
213 // Verifies that the phase and target information of an event is not mutated | 116 // Verifies that the phase and target information of an event is not mutated |
214 // as a result of sending the event to an event processor while it is still | 117 // as a result of sending the event to an event processor while it is still |
215 // being processed by another event processor. | 118 // being processed by another event processor. |
216 TEST_F(EventProcessorTest, NestedEventProcessing) { | 119 TEST_F(EventProcessorTest, NestedEventProcessing) { |
217 // Add one child to the default event processor used in this test suite. | 120 // Add one child to the default event processor used in this test suite. |
218 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 121 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 122 SetTarget(child.get()); |
219 root()->AddChild(child.Pass()); | 123 root()->AddChild(child.Pass()); |
220 | 124 |
221 // Define a second root target and child. | 125 // Define a second root target and child. |
222 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget()); | 126 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget()); |
223 TestEventTarget* second_root = | 127 TestEventTarget* second_root = |
224 static_cast<TestEventTarget*>(second_root_scoped.get()); | 128 static_cast<TestEventTarget*>(second_root_scoped.get()); |
225 second_root->SetEventTargeter(make_scoped_ptr(new EventTargeter())); | |
226 scoped_ptr<TestEventTarget> second_child(new TestEventTarget()); | 129 scoped_ptr<TestEventTarget> second_child(new TestEventTarget()); |
| 130 second_root->SetEventTargeter( |
| 131 make_scoped_ptr(new TestEventTargeter(second_child.get(), false))); |
227 second_root->AddChild(second_child.Pass()); | 132 second_root->AddChild(second_child.Pass()); |
228 | 133 |
229 // Define a second event processor which owns the second root. | 134 // Define a second event processor which owns the second root. |
230 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor()); | 135 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor()); |
231 second_processor->SetRoot(second_root_scoped.Pass()); | 136 second_processor->SetRoot(second_root_scoped.Pass()); |
232 | 137 |
233 // Indicate that an event which is dispatched to the child target owned by the | 138 // Indicate that an event which is dispatched to the child target owned by the |
234 // first event processor should be handled by |target_handler| instead. | 139 // first event processor should be handled by |target_handler| instead. |
235 scoped_ptr<TestEventHandler> target_handler( | 140 scoped_ptr<TestEventHandler> target_handler( |
236 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); | 141 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); |
237 root()->child_at(0)->set_target_handler(target_handler.get()); | 142 root()->child_at(0)->set_target_handler(target_handler.get()); |
238 | 143 |
239 // Dispatch a mouse event to the tree of event targets owned by the first | 144 // Dispatch a mouse event to the tree of event targets owned by the first |
240 // event processor, checking in ReDispatchEventHandler that the phase and | 145 // event processor, checking in ReDispatchEventHandler that the phase and |
241 // target information of the event is correct. | 146 // target information of the event is correct. |
242 MouseEvent mouse( | 147 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
243 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE); | 148 EventTimeForNow(), EF_NONE, EF_NONE); |
244 DispatchEvent(&mouse); | 149 DispatchEvent(&mouse); |
245 | 150 |
246 // Verify also that |mouse| was seen by the child nodes contained in both | 151 // Verify also that |mouse| was seen by the child nodes contained in both |
247 // event processors and that the event was not handled. | 152 // event processors and that the event was not handled. |
248 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 153 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
249 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 154 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
250 EXPECT_FALSE(mouse.handled()); | 155 EXPECT_FALSE(mouse.handled()); |
251 second_root->child_at(0)->ResetReceivedEvents(); | 156 second_root->child_at(0)->ResetReceivedEvents(); |
252 root()->child_at(0)->ResetReceivedEvents(); | 157 root()->child_at(0)->ResetReceivedEvents(); |
253 | 158 |
254 // Indicate that the child of the second root should handle events, and | 159 // Indicate that the child of the second root should handle events, and |
255 // dispatch another mouse event to verify that it is marked as handled. | 160 // dispatch another mouse event to verify that it is marked as handled. |
256 second_root->child_at(0)->set_mark_events_as_handled(true); | 161 second_root->child_at(0)->set_mark_events_as_handled(true); |
257 MouseEvent mouse2( | 162 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
258 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE); | 163 EventTimeForNow(), EF_NONE, EF_NONE); |
259 DispatchEvent(&mouse2); | 164 DispatchEvent(&mouse2); |
260 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 165 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
261 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 166 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
262 EXPECT_TRUE(mouse2.handled()); | 167 EXPECT_TRUE(mouse2.handled()); |
263 } | 168 } |
264 | 169 |
265 // Verifies that OnEventProcessingFinished() is called when an event | 170 // Verifies that OnEventProcessingFinished() is called when an event |
266 // has been handled. | 171 // has been handled. |
267 TEST_F(EventProcessorTest, OnEventProcessingFinished) { | 172 TEST_F(EventProcessorTest, OnEventProcessingFinished) { |
268 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 173 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
269 child->set_mark_events_as_handled(true); | 174 child->set_mark_events_as_handled(true); |
| 175 SetTarget(child.get()); |
270 root()->AddChild(child.Pass()); | 176 root()->AddChild(child.Pass()); |
271 | 177 |
272 // Dispatch a mouse event. We expect the event to be seen by the target, | 178 // Dispatch a mouse event. We expect the event to be seen by the target, |
273 // handled, and we expect OnEventProcessingFinished() to be invoked once. | 179 // handled, and we expect OnEventProcessingFinished() to be invoked once. |
274 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 180 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
275 EF_NONE, EF_NONE); | 181 EventTimeForNow(), EF_NONE, EF_NONE); |
276 DispatchEvent(&mouse); | 182 DispatchEvent(&mouse); |
277 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 183 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
278 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 184 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
279 EXPECT_TRUE(mouse.handled()); | 185 EXPECT_TRUE(mouse.handled()); |
280 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 186 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
281 } | 187 } |
282 | 188 |
283 // Verifies that OnEventProcessingStarted() has been called when starting to | 189 // Verifies that OnEventProcessingStarted() has been called when starting to |
284 // process an event, and that processing does not take place if | 190 // process an event, and that processing does not take place if |
285 // OnEventProcessingStarted() marks the event as handled. Also verifies that | 191 // OnEventProcessingStarted() marks the event as handled. Also verifies that |
286 // OnEventProcessingFinished() is also called in either case. | 192 // OnEventProcessingFinished() is also called in either case. |
287 TEST_F(EventProcessorTest, OnEventProcessingStarted) { | 193 TEST_F(EventProcessorTest, OnEventProcessingStarted) { |
288 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 194 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 195 SetTarget(child.get()); |
289 root()->AddChild(child.Pass()); | 196 root()->AddChild(child.Pass()); |
290 | 197 |
291 // Dispatch a mouse event. We expect the event to be seen by the target, | 198 // Dispatch a mouse event. We expect the event to be seen by the target, |
292 // OnEventProcessingStarted() should be called once, and | 199 // OnEventProcessingStarted() should be called once, and |
293 // OnEventProcessingFinished() should be called once. The event should | 200 // OnEventProcessingFinished() should be called once. The event should |
294 // remain unhandled. | 201 // remain unhandled. |
295 MouseEvent mouse( | 202 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
296 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE); | 203 EventTimeForNow(), EF_NONE, EF_NONE); |
297 DispatchEvent(&mouse); | 204 DispatchEvent(&mouse); |
298 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 205 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
299 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 206 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
300 EXPECT_FALSE(mouse.handled()); | 207 EXPECT_FALSE(mouse.handled()); |
301 EXPECT_EQ(1, processor()->num_times_processing_started()); | 208 EXPECT_EQ(1, processor()->num_times_processing_started()); |
302 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 209 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
303 processor()->Reset(); | 210 processor()->Reset(); |
304 root()->ResetReceivedEvents(); | 211 root()->ResetReceivedEvents(); |
305 root()->child_at(0)->ResetReceivedEvents(); | 212 root()->child_at(0)->ResetReceivedEvents(); |
306 | 213 |
307 // Dispatch another mouse event, but with OnEventProcessingStarted() marking | 214 // Dispatch another mouse event, but with OnEventProcessingStarted() marking |
308 // the event as handled to prevent processing. We expect the event to not be | 215 // the event as handled to prevent processing. We expect the event to not be |
309 // seen by the target this time, but OnEventProcessingStarted() and | 216 // seen by the target this time, but OnEventProcessingStarted() and |
310 // OnEventProcessingFinished() should both still be called once. | 217 // OnEventProcessingFinished() should both still be called once. |
311 processor()->set_should_processing_occur(false); | 218 processor()->set_should_processing_occur(false); |
312 MouseEvent mouse2( | 219 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
313 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE); | 220 EventTimeForNow(), EF_NONE, EF_NONE); |
314 DispatchEvent(&mouse2); | 221 DispatchEvent(&mouse2); |
315 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 222 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
316 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 223 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
317 EXPECT_TRUE(mouse2.handled()); | 224 EXPECT_TRUE(mouse2.handled()); |
318 EXPECT_EQ(1, processor()->num_times_processing_started()); | 225 EXPECT_EQ(1, processor()->num_times_processing_started()); |
319 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 226 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
320 } | 227 } |
321 | 228 |
322 class IgnoreEventTargeter : public EventTargeter { | |
323 public: | |
324 IgnoreEventTargeter() {} | |
325 ~IgnoreEventTargeter() override {} | |
326 | |
327 private: | |
328 // EventTargeter: | |
329 bool SubtreeShouldBeExploredForEvent( | |
330 EventTarget* target, const LocatedEvent& event) override { | |
331 return false; | |
332 } | |
333 }; | |
334 | |
335 // Verifies that the EventTargeter installed on an EventTarget can dictate | |
336 // whether the target itself can process an event. | |
337 TEST_F(EventProcessorTest, TargeterChecksOwningEventTarget) { | |
338 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | |
339 root()->AddChild(child.Pass()); | |
340 | |
341 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | |
342 EF_NONE, EF_NONE); | |
343 DispatchEvent(&mouse); | |
344 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | |
345 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | |
346 root()->child_at(0)->ResetReceivedEvents(); | |
347 | |
348 // Install an event handler on |child| which always prevents the target from | |
349 // receiving event. | |
350 root()->child_at(0)->SetEventTargeter( | |
351 scoped_ptr<EventTargeter>(new IgnoreEventTargeter())); | |
352 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | |
353 EF_NONE, EF_NONE); | |
354 DispatchEvent(&mouse2); | |
355 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | |
356 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | |
357 } | |
358 | |
359 // An EventTargeter which is used to allow a bubbling behaviour in event | |
360 // dispatch: if an event is not handled after being dispatched to its | |
361 // initial target, the event is dispatched to the next-best target as | |
362 // specified by FindNextBestTarget(). | |
363 class BubblingEventTargeter : public EventTargeter { | |
364 public: | |
365 explicit BubblingEventTargeter(TestEventTarget* initial_target) | |
366 : initial_target_(initial_target) {} | |
367 ~BubblingEventTargeter() override {} | |
368 | |
369 private: | |
370 // EventTargeter: | |
371 EventTarget* FindTargetForEvent(EventTarget* root, | |
372 Event* event) override { | |
373 return initial_target_; | |
374 } | |
375 | |
376 EventTarget* FindNextBestTarget(EventTarget* previous_target, | |
377 Event* event) override { | |
378 return previous_target->GetParentTarget(); | |
379 } | |
380 | |
381 TestEventTarget* initial_target_; | |
382 | |
383 DISALLOW_COPY_AND_ASSIGN(BubblingEventTargeter); | |
384 }; | |
385 | |
386 // Tests that unhandled events are correctly dispatched to the next-best | 229 // Tests that unhandled events are correctly dispatched to the next-best |
387 // target as decided by the BubblingEventTargeter. | 230 // target as decided by the TestEventTargeter. |
388 TEST_F(EventProcessorTest, DispatchToNextBestTarget) { | 231 TEST_F(EventProcessorTest, DispatchToNextBestTarget) { |
389 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 232 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
390 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); | 233 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); |
391 | 234 |
| 235 // Install a TestEventTargeter which permits bubbling. |
392 root()->SetEventTargeter( | 236 root()->SetEventTargeter( |
393 scoped_ptr<EventTargeter>(new BubblingEventTargeter(grandchild.get()))); | 237 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); |
394 child->AddChild(grandchild.Pass()); | 238 child->AddChild(grandchild.Pass()); |
395 root()->AddChild(child.Pass()); | 239 root()->AddChild(child.Pass()); |
396 | 240 |
397 ASSERT_EQ(1u, root()->child_count()); | 241 ASSERT_EQ(1u, root()->child_count()); |
398 ASSERT_EQ(1u, root()->child_at(0)->child_count()); | 242 ASSERT_EQ(1u, root()->child_at(0)->child_count()); |
399 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); | 243 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); |
400 | 244 |
401 TestEventTarget* child_r = root()->child_at(0); | 245 TestEventTarget* child_r = root()->child_at(0); |
402 TestEventTarget* grandchild_r = child_r->child_at(0); | 246 TestEventTarget* grandchild_r = child_r->child_at(0); |
403 | 247 |
404 // When the root has a BubblingEventTargeter installed, events targeted | 248 // When the root has a TestEventTargeter installed which permits bubbling, |
405 // at the grandchild target should be dispatched to all three targets. | 249 // events targeted at the grandchild target should be dispatched to all three |
| 250 // targets. |
406 KeyEvent key_event(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE); | 251 KeyEvent key_event(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE); |
407 DispatchEvent(&key_event); | 252 DispatchEvent(&key_event); |
408 EXPECT_TRUE(root()->DidReceiveEvent(ET_KEY_PRESSED)); | 253 EXPECT_TRUE(root()->DidReceiveEvent(ET_KEY_PRESSED)); |
409 EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED)); | 254 EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED)); |
410 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED)); | 255 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED)); |
411 root()->ResetReceivedEvents(); | 256 root()->ResetReceivedEvents(); |
412 child_r->ResetReceivedEvents(); | 257 child_r->ResetReceivedEvents(); |
413 grandchild_r->ResetReceivedEvents(); | 258 grandchild_r->ResetReceivedEvents(); |
414 | 259 |
415 // Add a pre-target handler on the child of the root that will mark the event | 260 // Add a pre-target handler on the child of the root that will mark the event |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED)); | 294 EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED)); |
450 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED)); | 295 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED)); |
451 root()->ResetReceivedEvents(); | 296 root()->ResetReceivedEvents(); |
452 child_r->ResetReceivedEvents(); | 297 child_r->ResetReceivedEvents(); |
453 grandchild_r->ResetReceivedEvents(); | 298 grandchild_r->ResetReceivedEvents(); |
454 child_r->set_mark_events_as_handled(false); | 299 child_r->set_mark_events_as_handled(false); |
455 } | 300 } |
456 | 301 |
457 // Tests that unhandled events are seen by the correct sequence of | 302 // Tests that unhandled events are seen by the correct sequence of |
458 // targets, pre-target handlers, and post-target handlers when | 303 // targets, pre-target handlers, and post-target handlers when |
459 // a BubblingEventTargeter is installed on the root target. | 304 // a TestEventTargeter is installed on the root target which permits bubbling. |
460 TEST_F(EventProcessorTest, HandlerSequence) { | 305 TEST_F(EventProcessorTest, HandlerSequence) { |
461 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 306 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
462 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); | 307 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); |
463 | 308 |
| 309 // Install a TestEventTargeter which permits bubbling. |
464 root()->SetEventTargeter( | 310 root()->SetEventTargeter( |
465 scoped_ptr<EventTargeter>(new BubblingEventTargeter(grandchild.get()))); | 311 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); |
466 child->AddChild(grandchild.Pass()); | 312 child->AddChild(grandchild.Pass()); |
467 root()->AddChild(child.Pass()); | 313 root()->AddChild(child.Pass()); |
468 | 314 |
469 ASSERT_EQ(1u, root()->child_count()); | 315 ASSERT_EQ(1u, root()->child_count()); |
470 ASSERT_EQ(1u, root()->child_at(0)->child_count()); | 316 ASSERT_EQ(1u, root()->child_at(0)->child_count()); |
471 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); | 317 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); |
472 | 318 |
473 TestEventTarget* child_r = root()->child_at(0); | 319 TestEventTarget* child_r = root()->child_at(0); |
474 TestEventTarget* grandchild_r = child_r->child_at(0); | 320 TestEventTarget* grandchild_r = child_r->child_at(0); |
475 | 321 |
(...skipping 29 matching lines...) Expand all Loading... |
505 post_child.set_handler_name("PostC"); | 351 post_child.set_handler_name("PostC"); |
506 post_child.set_recorder(&recorder); | 352 post_child.set_recorder(&recorder); |
507 child_r->AddPostTargetHandler(&post_child); | 353 child_r->AddPostTargetHandler(&post_child); |
508 | 354 |
509 TestEventHandler post_grandchild; | 355 TestEventHandler post_grandchild; |
510 post_grandchild.set_handler_name("PostG"); | 356 post_grandchild.set_handler_name("PostG"); |
511 post_grandchild.set_recorder(&recorder); | 357 post_grandchild.set_recorder(&recorder); |
512 grandchild_r->AddPostTargetHandler(&post_grandchild); | 358 grandchild_r->AddPostTargetHandler(&post_grandchild); |
513 | 359 |
514 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 360 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
515 EF_NONE, EF_NONE); | 361 EventTimeForNow(), EF_NONE, EF_NONE); |
516 DispatchEvent(&mouse); | 362 DispatchEvent(&mouse); |
517 | 363 |
518 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", | 364 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", |
519 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; | 365 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; |
520 EXPECT_EQ(std::vector<std::string>( | 366 EXPECT_EQ(std::vector<std::string>( |
521 expected, expected + arraysize(expected)), recorder); | 367 expected, expected + arraysize(expected)), recorder); |
522 } | 368 } |
523 | 369 |
524 } // namespace test | 370 } // namespace test |
525 } // namespace ui | 371 } // namespace ui |
OLD | NEW |