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" |
10 #include "ui/events/event_utils.h" | 11 #include "ui/events/event_utils.h" |
11 #include "ui/events/test/events_test_utils.h" | 12 #include "ui/events/test/events_test_utils.h" |
12 #include "ui/events/test/test_event_handler.h" | 13 #include "ui/events/test/test_event_handler.h" |
13 #include "ui/events/test/test_event_processor.h" | 14 #include "ui/events/test/test_event_processor.h" |
14 #include "ui/events/test/test_event_target.h" | 15 #include "ui/events/test/test_event_target.h" |
15 | 16 |
16 typedef std::vector<std::string> HandlerSequenceRecorder; | 17 typedef std::vector<std::string> HandlerSequenceRecorder; |
17 | 18 |
18 namespace ui { | 19 namespace ui { |
19 namespace test { | 20 namespace test { |
20 | 21 |
22 class RecursiveEventTargeter : public EventTargeter { | |
tdanderson
2015/05/22 18:29:56
I'm not sure if this class is necessary. WindowTar
varkha
2015/05/22 22:18:34
Done. I was considering it but at the time it seem
| |
23 public: | |
24 RecursiveEventTargeter() {} | |
25 ~RecursiveEventTargeter() override {} | |
26 | |
27 // EventTargeter: | |
28 EventTarget* FindTargetForEvent(EventTarget* root, Event* event) override { | |
29 if (!event->IsLocatedEvent()) | |
30 return root; | |
31 LocatedEvent* located_event = static_cast<LocatedEvent*>(event); | |
32 | |
33 scoped_ptr<EventTargetIterator> iter = root->GetChildIterator(); | |
34 if (iter) { | |
35 EventTarget* target = root; | |
36 for (EventTarget* child = iter->GetNextTarget(); child; | |
37 child = iter->GetNextTarget()) { | |
38 EventTargeter* targeter = child->GetEventTargeter(); | |
39 if (!targeter) | |
40 targeter = this; | |
41 target->ConvertEventToTarget(child, located_event); | |
42 target = child; | |
43 EventTarget* child_target = | |
44 targeter->FindTargetForEvent(child, located_event); | |
45 if (child_target) | |
46 return child_target; | |
47 } | |
48 target->ConvertEventToTarget(root, located_event); | |
49 } | |
50 return root->CanAcceptEvent(*event) ? root : NULL; | |
51 } | |
52 | |
53 EventTarget* FindNextBestTarget(EventTarget* previous_target, | |
54 Event* event) override { | |
55 return nullptr; | |
56 } | |
57 | |
58 private: | |
59 DISALLOW_COPY_AND_ASSIGN(RecursiveEventTargeter); | |
60 }; | |
61 | |
21 class EventProcessorTest : public testing::Test { | 62 class EventProcessorTest : public testing::Test { |
22 public: | 63 public: |
23 EventProcessorTest() {} | 64 EventProcessorTest() {} |
24 ~EventProcessorTest() override {} | 65 ~EventProcessorTest() override {} |
25 | 66 |
26 // testing::Test: | 67 // testing::Test: |
27 void SetUp() override { | 68 void SetUp() override { |
28 processor_.SetRoot(scoped_ptr<EventTarget>(new TestEventTarget())); | 69 processor_.SetRoot(scoped_ptr<EventTarget>(new TestEventTarget())); |
29 processor_.Reset(); | 70 processor_.Reset(); |
30 root()->SetEventTargeter(make_scoped_ptr(new EventTargeter())); | 71 root()->SetEventTargeter(make_scoped_ptr(new RecursiveEventTargeter())); |
31 } | 72 } |
32 | 73 |
33 TestEventTarget* root() { | 74 TestEventTarget* root() { |
34 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); | 75 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); |
35 } | 76 } |
36 | 77 |
37 TestEventProcessor* processor() { | 78 TestEventProcessor* processor() { |
38 return &processor_; | 79 return &processor_; |
39 } | 80 } |
40 | 81 |
41 void DispatchEvent(Event* event) { | 82 void DispatchEvent(Event* event) { |
42 processor_.OnEventFromSource(event); | 83 processor_.OnEventFromSource(event); |
43 } | 84 } |
44 | 85 |
45 protected: | 86 protected: |
46 TestEventProcessor processor_; | 87 TestEventProcessor processor_; |
47 | 88 |
48 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); | 89 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); |
49 }; | 90 }; |
50 | 91 |
51 TEST_F(EventProcessorTest, Basic) { | 92 TEST_F(EventProcessorTest, Basic) { |
52 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 93 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
94 child->SetEventTargeter(make_scoped_ptr(new RecursiveEventTargeter())); | |
53 root()->AddChild(child.Pass()); | 95 root()->AddChild(child.Pass()); |
54 | 96 |
55 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 97 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
56 EventTimeForNow(), EF_NONE, EF_NONE); | 98 EventTimeForNow(), EF_NONE, EF_NONE); |
57 DispatchEvent(&mouse); | 99 DispatchEvent(&mouse); |
58 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 100 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
59 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 101 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
60 | 102 |
61 root()->RemoveChild(root()->child_at(0)); | 103 root()->RemoveChild(root()->child_at(0)); |
62 DispatchEvent(&mouse); | 104 DispatchEvent(&mouse); |
63 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 105 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
64 } | 106 } |
65 | 107 |
66 template<typename T> | |
67 class BoundsEventTargeter : public EventTargeter { | |
68 public: | |
69 ~BoundsEventTargeter() override {} | |
70 | |
71 protected: | |
72 bool SubtreeShouldBeExploredForEvent( | |
73 EventTarget* target, const LocatedEvent& event) override { | |
74 T* t = static_cast<T*>(target); | |
75 return (t->bounds().Contains(event.location())); | |
76 } | |
77 }; | |
78 | |
79 class BoundsTestTarget : public TestEventTarget { | |
80 public: | |
81 BoundsTestTarget() {} | |
82 ~BoundsTestTarget() override {} | |
83 | |
84 void set_bounds(gfx::Rect rect) { bounds_ = rect; } | |
85 gfx::Rect bounds() const { return bounds_; } | |
86 | |
87 static void ConvertPointToTarget(BoundsTestTarget* source, | |
88 BoundsTestTarget* target, | |
89 gfx::Point* location) { | |
90 gfx::Vector2d vector; | |
91 if (source->Contains(target)) { | |
92 for (; target && target != source; | |
93 target = static_cast<BoundsTestTarget*>(target->parent())) { | |
94 vector += target->bounds().OffsetFromOrigin(); | |
95 } | |
96 *location -= vector; | |
97 } else if (target->Contains(source)) { | |
98 for (; source && source != target; | |
99 source = static_cast<BoundsTestTarget*>(source->parent())) { | |
100 vector += source->bounds().OffsetFromOrigin(); | |
101 } | |
102 *location += vector; | |
103 } else { | |
104 NOTREACHED(); | |
105 } | |
106 } | |
107 | |
108 private: | |
109 // EventTarget: | |
110 void ConvertEventToTarget(EventTarget* target, 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), | |
145 EventTimeForNow(), EF_NONE, 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), | |
157 EventTimeForNow(), EF_NONE, 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 EventTimeForNow(), 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 | 108 // ReDispatchEventHandler is used to receive mouse events and forward them |
177 // to a specified EventProcessor. Verifies that the event has the correct | 109 // to a specified EventProcessor. Verifies that the event has the correct |
178 // target and phase both before and after the nested event processing. Also | 110 // 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 | 111 // verifies that the location of the event remains the same after it has |
180 // been processed by the second EventProcessor. | 112 // been processed by the second EventProcessor. |
181 class ReDispatchEventHandler : public TestEventHandler { | 113 class ReDispatchEventHandler : public TestEventHandler { |
182 public: | 114 public: |
183 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target) | 115 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target) |
184 : processor_(processor), expected_target_(target) {} | 116 : processor_(processor), expected_target_(target) {} |
185 ~ReDispatchEventHandler() override {} | 117 ~ReDispatchEventHandler() override {} |
(...skipping 29 matching lines...) Expand all Loading... | |
215 // being processed by another event processor. | 147 // being processed by another event processor. |
216 TEST_F(EventProcessorTest, NestedEventProcessing) { | 148 TEST_F(EventProcessorTest, NestedEventProcessing) { |
217 // Add one child to the default event processor used in this test suite. | 149 // Add one child to the default event processor used in this test suite. |
218 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 150 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
219 root()->AddChild(child.Pass()); | 151 root()->AddChild(child.Pass()); |
220 | 152 |
221 // Define a second root target and child. | 153 // Define a second root target and child. |
222 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget()); | 154 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget()); |
223 TestEventTarget* second_root = | 155 TestEventTarget* second_root = |
224 static_cast<TestEventTarget*>(second_root_scoped.get()); | 156 static_cast<TestEventTarget*>(second_root_scoped.get()); |
225 second_root->SetEventTargeter(make_scoped_ptr(new EventTargeter())); | 157 second_root->SetEventTargeter(make_scoped_ptr(new RecursiveEventTargeter())); |
226 scoped_ptr<TestEventTarget> second_child(new TestEventTarget()); | 158 scoped_ptr<TestEventTarget> second_child(new TestEventTarget()); |
227 second_root->AddChild(second_child.Pass()); | 159 second_root->AddChild(second_child.Pass()); |
228 | 160 |
229 // Define a second event processor which owns the second root. | 161 // Define a second event processor which owns the second root. |
230 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor()); | 162 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor()); |
231 second_processor->SetRoot(second_root_scoped.Pass()); | 163 second_processor->SetRoot(second_root_scoped.Pass()); |
232 | 164 |
233 // Indicate that an event which is dispatched to the child target owned by the | 165 // 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. | 166 // first event processor should be handled by |target_handler| instead. |
235 scoped_ptr<TestEventHandler> target_handler( | 167 scoped_ptr<TestEventHandler> target_handler( |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 244 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
313 EventTimeForNow(), EF_NONE, EF_NONE); | 245 EventTimeForNow(), EF_NONE, EF_NONE); |
314 DispatchEvent(&mouse2); | 246 DispatchEvent(&mouse2); |
315 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 247 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
316 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 248 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
317 EXPECT_TRUE(mouse2.handled()); | 249 EXPECT_TRUE(mouse2.handled()); |
318 EXPECT_EQ(1, processor()->num_times_processing_started()); | 250 EXPECT_EQ(1, processor()->num_times_processing_started()); |
319 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 251 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
320 } | 252 } |
321 | 253 |
322 class IgnoreEventTargeter : public EventTargeter { | |
323 public: | |
324 IgnoreEventTargeter() {} | |
325 ~IgnoreEventTargeter() override {} | |
326 | |
327 private: | |
328 // EventTargeter: | |
329 bool SubtreeShouldBeExploredForEvent(EventTarget* target, | |
330 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 EventTimeForNow(), 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 EventTimeForNow(), 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 | 254 // 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 | 255 // 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 | 256 // initial target, the event is dispatched to the next-best target as |
362 // specified by FindNextBestTarget(). | 257 // specified by FindNextBestTarget(). |
363 class BubblingEventTargeter : public EventTargeter { | 258 class BubblingEventTargeter : public EventTargeter { |
364 public: | 259 public: |
365 explicit BubblingEventTargeter(TestEventTarget* initial_target) | 260 explicit BubblingEventTargeter(TestEventTarget* initial_target) |
366 : initial_target_(initial_target) {} | 261 : initial_target_(initial_target) {} |
367 ~BubblingEventTargeter() override {} | 262 ~BubblingEventTargeter() override {} |
368 | 263 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 DispatchEvent(&mouse); | 410 DispatchEvent(&mouse); |
516 | 411 |
517 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", | 412 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", |
518 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; | 413 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; |
519 EXPECT_EQ(std::vector<std::string>( | 414 EXPECT_EQ(std::vector<std::string>( |
520 expected, expected + arraysize(expected)), recorder); | 415 expected, expected + arraysize(expected)), recorder); |
521 } | 416 } |
522 | 417 |
523 } // namespace test | 418 } // namespace test |
524 } // namespace ui | 419 } // namespace ui |
OLD | NEW |