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_targeter.h" | 9 #include "ui/events/event_targeter.h" |
10 #include "ui/events/event_utils.h" | 10 #include "ui/events/event_utils.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 EventTimeForNow(), EF_NONE, EF_NONE); | 56 EventTimeForNow(), EF_NONE, EF_NONE); |
57 DispatchEvent(&mouse); | 57 DispatchEvent(&mouse); |
58 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 58 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
59 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 59 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
60 | 60 |
61 root()->RemoveChild(root()->child_at(0)); | 61 root()->RemoveChild(root()->child_at(0)); |
62 DispatchEvent(&mouse); | 62 DispatchEvent(&mouse); |
63 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 63 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
64 } | 64 } |
65 | 65 |
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 | 66 // ReDispatchEventHandler is used to receive mouse events and forward them |
177 // to a specified EventProcessor. Verifies that the event has the correct | 67 // to a specified EventProcessor. Verifies that the event has the correct |
178 // target and phase both before and after the nested event processing. Also | 68 // 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 | 69 // verifies that the location of the event remains the same after it has |
180 // been processed by the second EventProcessor. | 70 // been processed by the second EventProcessor. |
181 class ReDispatchEventHandler : public TestEventHandler { | 71 class ReDispatchEventHandler : public TestEventHandler { |
182 public: | 72 public: |
183 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target) | 73 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target) |
184 : processor_(processor), expected_target_(target) {} | 74 : processor_(processor), expected_target_(target) {} |
185 ~ReDispatchEventHandler() override {} | 75 ~ReDispatchEventHandler() override {} |
(...skipping 126 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), | 202 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
313 EventTimeForNow(), EF_NONE, EF_NONE); | 203 EventTimeForNow(), EF_NONE, EF_NONE); |
314 DispatchEvent(&mouse2); | 204 DispatchEvent(&mouse2); |
315 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 205 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
316 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 206 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
317 EXPECT_TRUE(mouse2.handled()); | 207 EXPECT_TRUE(mouse2.handled()); |
318 EXPECT_EQ(1, processor()->num_times_processing_started()); | 208 EXPECT_EQ(1, processor()->num_times_processing_started()); |
319 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 209 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
320 } | 210 } |
321 | 211 |
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 | 212 // 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 | 213 // 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 | 214 // initial target, the event is dispatched to the next-best target as |
362 // specified by FindNextBestTarget(). | 215 // specified by FindNextBestTarget(). |
363 class BubblingEventTargeter : public EventTargeter { | 216 class BubblingEventTargeter : public EventTargeter { |
364 public: | 217 public: |
365 explicit BubblingEventTargeter(TestEventTarget* initial_target) | 218 explicit BubblingEventTargeter(TestEventTarget* initial_target) |
366 : initial_target_(initial_target) {} | 219 : initial_target_(initial_target) {} |
367 ~BubblingEventTargeter() override {} | 220 ~BubblingEventTargeter() override {} |
368 | 221 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 DispatchEvent(&mouse); | 368 DispatchEvent(&mouse); |
516 | 369 |
517 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", | 370 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", |
518 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; | 371 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; |
519 EXPECT_EQ(std::vector<std::string>( | 372 EXPECT_EQ(std::vector<std::string>( |
520 expected, expected + arraysize(expected)), recorder); | 373 expected, expected + arraysize(expected)), recorder); |
521 } | 374 } |
522 | 375 |
523 } // namespace test | 376 } // namespace test |
524 } // namespace ui | 377 } // namespace ui |
OLD | NEW |