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
3 // found in the LICENSE file.
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/events/event.h"
9 #include "ui/events/event_targeter.h"
10 #include "ui/events/test/events_test_utils.h"
11 #include "ui/events/test/test_event_handler.h"
12 #include "ui/events/test/test_event_processor.h"
13 #include "ui/events/test/test_event_target.h"
15 typedef std::vector
<std::string
> HandlerSequenceRecorder
;
20 class EventProcessorTest
: public testing::Test
{
22 EventProcessorTest() {}
23 ~EventProcessorTest() override
{}
26 void SetUp() override
{
27 processor_
.SetRoot(scoped_ptr
<EventTarget
>(new TestEventTarget()));
29 root()->SetEventTargeter(make_scoped_ptr(new EventTargeter()));
32 TestEventTarget
* root() {
33 return static_cast<TestEventTarget
*>(processor_
.GetRootTarget());
36 TestEventProcessor
* processor() {
40 void DispatchEvent(Event
* event
) {
41 processor_
.OnEventFromSource(event
);
45 TestEventProcessor processor_
;
47 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest
);
50 TEST_F(EventProcessorTest
, Basic
) {
51 scoped_ptr
<TestEventTarget
> child(new TestEventTarget());
52 root()->AddChild(child
.Pass());
54 MouseEvent
mouse(ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10),
56 DispatchEvent(&mouse
);
57 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
58 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
60 root()->RemoveChild(root()->child_at(0));
61 DispatchEvent(&mouse
);
62 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
66 class BoundsEventTargeter
: public EventTargeter
{
68 virtual ~BoundsEventTargeter() {}
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()));
78 class BoundsTestTarget
: public TestEventTarget
{
81 ~BoundsTestTarget() override
{}
83 void set_bounds(gfx::Rect rect
) { bounds_
= rect
; }
84 gfx::Rect
bounds() const { return bounds_
; }
86 static void ConvertPointToTarget(BoundsTestTarget
* source
,
87 BoundsTestTarget
* target
,
88 gfx::Point
* location
) {
90 if (source
->Contains(target
)) {
91 for (; target
&& target
!= source
;
92 target
= static_cast<BoundsTestTarget
*>(target
->parent())) {
93 vector
+= target
->bounds().OffsetFromOrigin();
96 } else if (target
->Contains(source
)) {
97 for (; source
&& source
!= target
;
98 source
= static_cast<BoundsTestTarget
*>(source
->parent())) {
99 vector
+= source
->bounds().OffsetFromOrigin();
109 void ConvertEventToTarget(EventTarget
* target
, LocatedEvent
* event
) override
{
110 event
->ConvertLocationToTarget(this,
111 static_cast<BoundsTestTarget
*>(target
));
116 DISALLOW_COPY_AND_ASSIGN(BoundsTestTarget
);
119 TEST_F(EventProcessorTest
, Bounds
) {
120 scoped_ptr
<BoundsTestTarget
> parent(new BoundsTestTarget());
121 scoped_ptr
<BoundsTestTarget
> child(new BoundsTestTarget());
122 scoped_ptr
<BoundsTestTarget
> grandchild(new BoundsTestTarget());
124 parent
->set_bounds(gfx::Rect(0, 0, 30, 30));
125 child
->set_bounds(gfx::Rect(5, 5, 20, 20));
126 grandchild
->set_bounds(gfx::Rect(5, 5, 5, 5));
128 child
->AddChild(scoped_ptr
<TestEventTarget
>(grandchild
.Pass()));
129 parent
->AddChild(scoped_ptr
<TestEventTarget
>(child
.Pass()));
130 root()->AddChild(scoped_ptr
<TestEventTarget
>(parent
.Pass()));
132 ASSERT_EQ(1u, root()->child_count());
133 ASSERT_EQ(1u, root()->child_at(0)->child_count());
134 ASSERT_EQ(1u, root()->child_at(0)->child_at(0)->child_count());
136 TestEventTarget
* parent_r
= root()->child_at(0);
137 TestEventTarget
* child_r
= parent_r
->child_at(0);
138 TestEventTarget
* grandchild_r
= child_r
->child_at(0);
140 // Dispatch a mouse event that falls on the parent, but not on the child. When
141 // the default event-targeter used, the event will still reach |grandchild|,
142 // because the default targeter does not look at the bounds.
143 MouseEvent
mouse(ET_MOUSE_MOVED
, gfx::Point(1, 1), gfx::Point(1, 1), EF_NONE
,
145 DispatchEvent(&mouse
);
146 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
147 EXPECT_FALSE(parent_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
148 EXPECT_FALSE(child_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
149 EXPECT_TRUE(grandchild_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
150 grandchild_r
->ResetReceivedEvents();
152 // Now install a targeter on the parent that looks at the bounds and makes
153 // sure the event reaches the target only if the location of the event within
154 // the bounds of the target.
155 MouseEvent
mouse2(ET_MOUSE_MOVED
, gfx::Point(1, 1), gfx::Point(1, 1), EF_NONE
,
157 parent_r
->SetEventTargeter(scoped_ptr
<EventTargeter
>(
158 new BoundsEventTargeter
<BoundsTestTarget
>()));
159 DispatchEvent(&mouse2
);
160 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
161 EXPECT_TRUE(parent_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
162 EXPECT_FALSE(child_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
163 EXPECT_FALSE(grandchild_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
164 parent_r
->ResetReceivedEvents();
166 MouseEvent
second(ET_MOUSE_MOVED
, gfx::Point(12, 12), gfx::Point(12, 12),
168 DispatchEvent(&second
);
169 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
170 EXPECT_FALSE(parent_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
171 EXPECT_FALSE(child_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
172 EXPECT_TRUE(grandchild_r
->DidReceiveEvent(ET_MOUSE_MOVED
));
175 // ReDispatchEventHandler is used to receive mouse events and forward them
176 // to a specified EventProcessor. Verifies that the event has the correct
177 // target and phase both before and after the nested event processing. Also
178 // verifies that the location of the event remains the same after it has
179 // been processed by the second EventProcessor.
180 class ReDispatchEventHandler
: public TestEventHandler
{
182 ReDispatchEventHandler(EventProcessor
* processor
, EventTarget
* target
)
183 : processor_(processor
), expected_target_(target
) {}
184 ~ReDispatchEventHandler() override
{}
187 void OnMouseEvent(MouseEvent
* event
) override
{
188 TestEventHandler::OnMouseEvent(event
);
190 EXPECT_EQ(expected_target_
, event
->target());
191 EXPECT_EQ(EP_TARGET
, event
->phase());
193 gfx::Point
location(event
->location());
194 EventDispatchDetails details
= processor_
->OnEventFromSource(event
);
195 EXPECT_FALSE(details
.dispatcher_destroyed
);
196 EXPECT_FALSE(details
.target_destroyed
);
198 // The nested event-processing should not have mutated the target,
199 // phase, or location of |event|.
200 EXPECT_EQ(expected_target_
, event
->target());
201 EXPECT_EQ(EP_TARGET
, event
->phase());
202 EXPECT_EQ(location
, event
->location());
206 EventProcessor
* processor_
;
207 EventTarget
* expected_target_
;
209 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler
);
212 // Verifies that the phase and target information of an event is not mutated
213 // as a result of sending the event to an event processor while it is still
214 // being processed by another event processor.
215 TEST_F(EventProcessorTest
, NestedEventProcessing
) {
216 // Add one child to the default event processor used in this test suite.
217 scoped_ptr
<TestEventTarget
> child(new TestEventTarget());
218 root()->AddChild(child
.Pass());
220 // Define a second root target and child.
221 scoped_ptr
<EventTarget
> second_root_scoped(new TestEventTarget());
222 TestEventTarget
* second_root
=
223 static_cast<TestEventTarget
*>(second_root_scoped
.get());
224 second_root
->SetEventTargeter(make_scoped_ptr(new EventTargeter()));
225 scoped_ptr
<TestEventTarget
> second_child(new TestEventTarget());
226 second_root
->AddChild(second_child
.Pass());
228 // Define a second event processor which owns the second root.
229 scoped_ptr
<TestEventProcessor
> second_processor(new TestEventProcessor());
230 second_processor
->SetRoot(second_root_scoped
.Pass());
232 // Indicate that an event which is dispatched to the child target owned by the
233 // first event processor should be handled by |target_handler| instead.
234 scoped_ptr
<TestEventHandler
> target_handler(
235 new ReDispatchEventHandler(second_processor
.get(), root()->child_at(0)));
236 root()->child_at(0)->set_target_handler(target_handler
.get());
238 // Dispatch a mouse event to the tree of event targets owned by the first
239 // event processor, checking in ReDispatchEventHandler that the phase and
240 // target information of the event is correct.
242 ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE
, EF_NONE
);
243 DispatchEvent(&mouse
);
245 // Verify also that |mouse| was seen by the child nodes contained in both
246 // event processors and that the event was not handled.
247 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
248 EXPECT_TRUE(second_root
->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
249 EXPECT_FALSE(mouse
.handled());
250 second_root
->child_at(0)->ResetReceivedEvents();
251 root()->child_at(0)->ResetReceivedEvents();
253 // Indicate that the child of the second root should handle events, and
254 // dispatch another mouse event to verify that it is marked as handled.
255 second_root
->child_at(0)->set_mark_events_as_handled(true);
257 ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE
, EF_NONE
);
258 DispatchEvent(&mouse2
);
259 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
260 EXPECT_TRUE(second_root
->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
261 EXPECT_TRUE(mouse2
.handled());
264 // Verifies that OnEventProcessingFinished() is called when an event
266 TEST_F(EventProcessorTest
, OnEventProcessingFinished
) {
267 scoped_ptr
<TestEventTarget
> child(new TestEventTarget());
268 child
->set_mark_events_as_handled(true);
269 root()->AddChild(child
.Pass());
271 // Dispatch a mouse event. We expect the event to be seen by the target,
272 // handled, and we expect OnEventProcessingFinished() to be invoked once.
273 MouseEvent
mouse(ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10),
275 DispatchEvent(&mouse
);
276 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
277 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
278 EXPECT_TRUE(mouse
.handled());
279 EXPECT_EQ(1, processor()->num_times_processing_finished());
282 // Verifies that OnEventProcessingStarted() has been called when starting to
283 // process an event, and that processing does not take place if
284 // OnEventProcessingStarted() marks the event as handled. Also verifies that
285 // OnEventProcessingFinished() is also called in either case.
286 TEST_F(EventProcessorTest
, OnEventProcessingStarted
) {
287 scoped_ptr
<TestEventTarget
> child(new TestEventTarget());
288 root()->AddChild(child
.Pass());
290 // Dispatch a mouse event. We expect the event to be seen by the target,
291 // OnEventProcessingStarted() should be called once, and
292 // OnEventProcessingFinished() should be called once. The event should
295 ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE
, EF_NONE
);
296 DispatchEvent(&mouse
);
297 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
298 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
299 EXPECT_FALSE(mouse
.handled());
300 EXPECT_EQ(1, processor()->num_times_processing_started());
301 EXPECT_EQ(1, processor()->num_times_processing_finished());
302 processor()->Reset();
303 root()->ResetReceivedEvents();
304 root()->child_at(0)->ResetReceivedEvents();
306 // Dispatch another mouse event, but with OnEventProcessingStarted() marking
307 // the event as handled to prevent processing. We expect the event to not be
308 // seen by the target this time, but OnEventProcessingStarted() and
309 // OnEventProcessingFinished() should both still be called once.
310 processor()->set_should_processing_occur(false);
312 ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE
, EF_NONE
);
313 DispatchEvent(&mouse2
);
314 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
315 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
316 EXPECT_TRUE(mouse2
.handled());
317 EXPECT_EQ(1, processor()->num_times_processing_started());
318 EXPECT_EQ(1, processor()->num_times_processing_finished());
321 class IgnoreEventTargeter
: public EventTargeter
{
323 IgnoreEventTargeter() {}
324 ~IgnoreEventTargeter() override
{}
328 bool SubtreeShouldBeExploredForEvent(EventTarget
* target
,
329 const LocatedEvent
& event
) override
{
334 // Verifies that the EventTargeter installed on an EventTarget can dictate
335 // whether the target itself can process an event.
336 TEST_F(EventProcessorTest
, TargeterChecksOwningEventTarget
) {
337 scoped_ptr
<TestEventTarget
> child(new TestEventTarget());
338 root()->AddChild(child
.Pass());
340 MouseEvent
mouse(ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10),
342 DispatchEvent(&mouse
);
343 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
344 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
345 root()->child_at(0)->ResetReceivedEvents();
347 // Install an event handler on |child| which always prevents the target from
349 root()->child_at(0)->SetEventTargeter(
350 scoped_ptr
<EventTargeter
>(new IgnoreEventTargeter()));
351 MouseEvent
mouse2(ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10),
353 DispatchEvent(&mouse2
);
354 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED
));
355 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED
));
358 // An EventTargeter which is used to allow a bubbling behaviour in event
359 // dispatch: if an event is not handled after being dispatched to its
360 // initial target, the event is dispatched to the next-best target as
361 // specified by FindNextBestTarget().
362 class BubblingEventTargeter
: public EventTargeter
{
364 explicit BubblingEventTargeter(TestEventTarget
* initial_target
)
365 : initial_target_(initial_target
) {}
366 ~BubblingEventTargeter() override
{}
370 EventTarget
* FindTargetForEvent(EventTarget
* root
, Event
* event
) override
{
371 return initial_target_
;
374 EventTarget
* FindNextBestTarget(EventTarget
* previous_target
,
375 Event
* event
) override
{
376 return previous_target
->GetParentTarget();
379 TestEventTarget
* initial_target_
;
381 DISALLOW_COPY_AND_ASSIGN(BubblingEventTargeter
);
384 // Tests that unhandled events are correctly dispatched to the next-best
385 // target as decided by the BubblingEventTargeter.
386 TEST_F(EventProcessorTest
, DispatchToNextBestTarget
) {
387 scoped_ptr
<TestEventTarget
> child(new TestEventTarget());
388 scoped_ptr
<TestEventTarget
> grandchild(new TestEventTarget());
390 root()->SetEventTargeter(
391 scoped_ptr
<EventTargeter
>(new BubblingEventTargeter(grandchild
.get())));
392 child
->AddChild(grandchild
.Pass());
393 root()->AddChild(child
.Pass());
395 ASSERT_EQ(1u, root()->child_count());
396 ASSERT_EQ(1u, root()->child_at(0)->child_count());
397 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count());
399 TestEventTarget
* child_r
= root()->child_at(0);
400 TestEventTarget
* grandchild_r
= child_r
->child_at(0);
402 // When the root has a BubblingEventTargeter installed, events targeted
403 // at the grandchild target should be dispatched to all three targets.
404 KeyEvent
key_event(ET_KEY_PRESSED
, VKEY_ESCAPE
, EF_NONE
);
405 DispatchEvent(&key_event
);
406 EXPECT_TRUE(root()->DidReceiveEvent(ET_KEY_PRESSED
));
407 EXPECT_TRUE(child_r
->DidReceiveEvent(ET_KEY_PRESSED
));
408 EXPECT_TRUE(grandchild_r
->DidReceiveEvent(ET_KEY_PRESSED
));
409 root()->ResetReceivedEvents();
410 child_r
->ResetReceivedEvents();
411 grandchild_r
->ResetReceivedEvents();
413 // Add a pre-target handler on the child of the root that will mark the event
414 // as handled. No targets in the hierarchy should receive the event.
415 TestEventHandler handler
;
416 child_r
->AddPreTargetHandler(&handler
);
417 key_event
= KeyEvent(ET_KEY_PRESSED
, VKEY_ESCAPE
, EF_NONE
);
418 DispatchEvent(&key_event
);
419 EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED
));
420 EXPECT_FALSE(child_r
->DidReceiveEvent(ET_KEY_PRESSED
));
421 EXPECT_FALSE(grandchild_r
->DidReceiveEvent(ET_KEY_PRESSED
));
422 EXPECT_EQ(1, handler
.num_key_events());
425 // Add a post-target handler on the child of the root that will mark the event
426 // as handled. Only the grandchild (the initial target) should receive the
428 child_r
->RemovePreTargetHandler(&handler
);
429 child_r
->AddPostTargetHandler(&handler
);
430 key_event
= KeyEvent(ET_KEY_PRESSED
, VKEY_ESCAPE
, EF_NONE
);
431 DispatchEvent(&key_event
);
432 EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED
));
433 EXPECT_FALSE(child_r
->DidReceiveEvent(ET_KEY_PRESSED
));
434 EXPECT_TRUE(grandchild_r
->DidReceiveEvent(ET_KEY_PRESSED
));
435 EXPECT_EQ(1, handler
.num_key_events());
437 grandchild_r
->ResetReceivedEvents();
438 child_r
->RemovePostTargetHandler(&handler
);
440 // Mark the event as handled when it reaches the EP_TARGET phase of
441 // dispatch at the child of the root. The child and grandchild
442 // targets should both receive the event, but the root should not.
443 child_r
->set_mark_events_as_handled(true);
444 key_event
= KeyEvent(ET_KEY_PRESSED
, VKEY_ESCAPE
, EF_NONE
);
445 DispatchEvent(&key_event
);
446 EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED
));
447 EXPECT_TRUE(child_r
->DidReceiveEvent(ET_KEY_PRESSED
));
448 EXPECT_TRUE(grandchild_r
->DidReceiveEvent(ET_KEY_PRESSED
));
449 root()->ResetReceivedEvents();
450 child_r
->ResetReceivedEvents();
451 grandchild_r
->ResetReceivedEvents();
452 child_r
->set_mark_events_as_handled(false);
455 // Tests that unhandled events are seen by the correct sequence of
456 // targets, pre-target handlers, and post-target handlers when
457 // a BubblingEventTargeter is installed on the root target.
458 TEST_F(EventProcessorTest
, HandlerSequence
) {
459 scoped_ptr
<TestEventTarget
> child(new TestEventTarget());
460 scoped_ptr
<TestEventTarget
> grandchild(new TestEventTarget());
462 root()->SetEventTargeter(
463 scoped_ptr
<EventTargeter
>(new BubblingEventTargeter(grandchild
.get())));
464 child
->AddChild(grandchild
.Pass());
465 root()->AddChild(child
.Pass());
467 ASSERT_EQ(1u, root()->child_count());
468 ASSERT_EQ(1u, root()->child_at(0)->child_count());
469 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count());
471 TestEventTarget
* child_r
= root()->child_at(0);
472 TestEventTarget
* grandchild_r
= child_r
->child_at(0);
474 HandlerSequenceRecorder recorder
;
475 root()->set_target_name("R");
476 root()->set_recorder(&recorder
);
477 child_r
->set_target_name("C");
478 child_r
->set_recorder(&recorder
);
479 grandchild_r
->set_target_name("G");
480 grandchild_r
->set_recorder(&recorder
);
482 TestEventHandler pre_root
;
483 pre_root
.set_handler_name("PreR");
484 pre_root
.set_recorder(&recorder
);
485 root()->AddPreTargetHandler(&pre_root
);
487 TestEventHandler pre_child
;
488 pre_child
.set_handler_name("PreC");
489 pre_child
.set_recorder(&recorder
);
490 child_r
->AddPreTargetHandler(&pre_child
);
492 TestEventHandler pre_grandchild
;
493 pre_grandchild
.set_handler_name("PreG");
494 pre_grandchild
.set_recorder(&recorder
);
495 grandchild_r
->AddPreTargetHandler(&pre_grandchild
);
497 TestEventHandler post_root
;
498 post_root
.set_handler_name("PostR");
499 post_root
.set_recorder(&recorder
);
500 root()->AddPostTargetHandler(&post_root
);
502 TestEventHandler post_child
;
503 post_child
.set_handler_name("PostC");
504 post_child
.set_recorder(&recorder
);
505 child_r
->AddPostTargetHandler(&post_child
);
507 TestEventHandler post_grandchild
;
508 post_grandchild
.set_handler_name("PostG");
509 post_grandchild
.set_recorder(&recorder
);
510 grandchild_r
->AddPostTargetHandler(&post_grandchild
);
512 MouseEvent
mouse(ET_MOUSE_MOVED
, gfx::Point(10, 10), gfx::Point(10, 10),
514 DispatchEvent(&mouse
);
516 std::string expected
[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC",
517 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" };
518 EXPECT_EQ(std::vector
<std::string
>(
519 expected
, expected
+ arraysize(expected
)), recorder
);