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
3 // found in the LICENSE file.
5 #include "ui/events/event_dispatcher.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/events/event.h"
9 #include "ui/events/event_utils.h"
15 class TestTarget
: public EventTarget
{
17 TestTarget() : parent_(NULL
), valid_(true) {}
18 virtual ~TestTarget() {}
20 void set_parent(TestTarget
* parent
) { parent_
= parent
; }
22 bool valid() const { return valid_
; }
23 void set_valid(bool valid
) { valid_
= valid
; }
25 void AddHandlerId(int id
) {
26 handler_list_
.push_back(id
);
29 const std::vector
<int>& handler_list() const { return handler_list_
; }
32 handler_list_
.clear();
37 // Overridden from EventTarget:
38 virtual bool CanAcceptEvent(const ui::Event
& event
) OVERRIDE
{
42 virtual EventTarget
* GetParentTarget() OVERRIDE
{
47 std::vector
<int> handler_list_
;
50 DISALLOW_COPY_AND_ASSIGN(TestTarget
);
53 class TestEventHandler
: public EventHandler
{
55 TestEventHandler(int id
)
57 event_result_(ER_UNHANDLED
),
58 expect_pre_target_(false),
59 expect_post_target_(false),
60 received_pre_target_(false) {
63 virtual ~TestEventHandler() {}
65 virtual void ReceivedEvent(Event
* event
) {
66 static_cast<TestTarget
*>(event
->target())->AddHandlerId(id_
);
67 if (event
->phase() == ui::EP_POSTTARGET
) {
68 EXPECT_TRUE(expect_post_target_
);
69 if (expect_pre_target_
)
70 EXPECT_TRUE(received_pre_target_
);
71 } else if (event
->phase() == ui::EP_PRETARGET
) {
72 EXPECT_TRUE(expect_pre_target_
);
73 received_pre_target_
= true;
79 void set_event_result(EventResult result
) { event_result_
= result
; }
81 void set_expect_pre_target(bool expect
) { expect_pre_target_
= expect
; }
82 void set_expect_post_target(bool expect
) { expect_post_target_
= expect
; }
85 // Overridden from EventHandler:
86 virtual void OnEvent(Event
* event
) OVERRIDE
{
87 ui::EventHandler::OnEvent(event
);
89 SetStatusOnEvent(event
);
92 void SetStatusOnEvent(Event
* event
) {
93 if (event_result_
& ui::ER_CONSUMED
)
94 event
->StopPropagation();
95 if (event_result_
& ui::ER_HANDLED
)
100 EventResult event_result_
;
101 bool expect_pre_target_
;
102 bool expect_post_target_
;
103 bool received_pre_target_
;
105 DISALLOW_COPY_AND_ASSIGN(TestEventHandler
);
108 class NonCancelableEvent
: public Event
{
111 : Event(ui::ET_CANCEL_MODE
, ui::EventTimeForNow(), 0) {
112 set_cancelable(false);
115 virtual ~NonCancelableEvent() {}
118 DISALLOW_COPY_AND_ASSIGN(NonCancelableEvent
);
121 // Destroys the dispatcher-delegate when it receives any event.
122 class EventHandlerDestroyDispatcherDelegate
: public TestEventHandler
{
124 EventHandlerDestroyDispatcherDelegate(EventDispatcherDelegate
* delegate
,
126 : TestEventHandler(id
),
127 dispatcher_delegate_(delegate
) {
130 virtual ~EventHandlerDestroyDispatcherDelegate() {}
133 virtual void ReceivedEvent(Event
* event
) OVERRIDE
{
134 TestEventHandler::ReceivedEvent(event
);
135 delete dispatcher_delegate_
;
138 EventDispatcherDelegate
* dispatcher_delegate_
;
140 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyDispatcherDelegate
);
143 // Invalidates the target when it receives any event.
144 class InvalidateTargetEventHandler
: public TestEventHandler
{
146 explicit InvalidateTargetEventHandler(int id
) : TestEventHandler(id
) {}
147 virtual ~InvalidateTargetEventHandler() {}
150 virtual void ReceivedEvent(Event
* event
) OVERRIDE
{
151 TestEventHandler::ReceivedEvent(event
);
152 TestTarget
* target
= static_cast<TestTarget
*>(event
->target());
153 target
->set_valid(false);
156 DISALLOW_COPY_AND_ASSIGN(InvalidateTargetEventHandler
);
159 // Destroys a second event handler when this handler gets an event.
160 // Optionally also destroys the dispatcher.
161 class EventHandlerDestroyer
: public TestEventHandler
{
163 EventHandlerDestroyer(int id
, EventHandler
* destroy
)
164 : TestEventHandler(id
),
165 to_destroy_(destroy
),
166 dispatcher_delegate_(NULL
) {
169 virtual ~EventHandlerDestroyer() {
173 void set_dispatcher_delegate(EventDispatcherDelegate
* dispatcher_delegate
) {
174 dispatcher_delegate_
= dispatcher_delegate
;
178 virtual void ReceivedEvent(Event
* event
) OVERRIDE
{
179 TestEventHandler::ReceivedEvent(event
);
183 if (dispatcher_delegate_
) {
184 delete dispatcher_delegate_
;
185 dispatcher_delegate_
= NULL
;
189 EventHandler
* to_destroy_
;
190 EventDispatcherDelegate
* dispatcher_delegate_
;
192 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyer
);
195 class TestEventDispatcher
: public EventDispatcherDelegate
{
197 TestEventDispatcher() {}
199 virtual ~TestEventDispatcher() {}
201 void ProcessEvent(EventTarget
* target
, Event
* event
) {
202 DispatchEvent(target
, event
);
206 // Overridden from EventDispatcherDelegate:
207 virtual bool CanDispatchToTarget(EventTarget
* target
) OVERRIDE
{
208 TestTarget
* test_target
= static_cast<TestTarget
*>(target
);
209 return test_target
->valid();
212 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher
);
217 TEST(EventDispatcherTest
, EventDispatchOrder
) {
218 TestEventDispatcher dispatcher
;
219 TestTarget parent
, child
;
220 TestEventHandler
h1(1), h2(2), h3(3), h4(4);
221 TestEventHandler
h5(5), h6(6), h7(7), h8(8);
223 child
.set_parent(&parent
);
225 parent
.AddPreTargetHandler(&h1
);
226 parent
.AddPreTargetHandler(&h2
);
228 child
.AddPreTargetHandler(&h3
);
229 child
.AddPreTargetHandler(&h4
);
231 h1
.set_expect_pre_target(true);
232 h2
.set_expect_pre_target(true);
233 h3
.set_expect_pre_target(true);
234 h4
.set_expect_pre_target(true);
236 child
.AddPostTargetHandler(&h5
);
237 child
.AddPostTargetHandler(&h6
);
239 parent
.AddPostTargetHandler(&h7
);
240 parent
.AddPostTargetHandler(&h8
);
242 h5
.set_expect_post_target(true);
243 h6
.set_expect_post_target(true);
244 h7
.set_expect_post_target(true);
245 h8
.set_expect_post_target(true);
247 MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(3, 4),
248 gfx::Point(3, 4), 0);
249 Event::DispatcherApi
event_mod(&mouse
);
250 dispatcher
.ProcessEvent(&child
, &mouse
);
251 EXPECT_FALSE(mouse
.stopped_propagation());
252 EXPECT_FALSE(mouse
.handled());
255 int expected
[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
257 std::vector
<int>(expected
, expected
+ sizeof(expected
) / sizeof(int)),
258 child
.handler_list());
262 event_mod
.set_phase(EP_PREDISPATCH
);
263 event_mod
.set_result(ER_UNHANDLED
);
265 h1
.set_event_result(ER_HANDLED
);
266 dispatcher
.ProcessEvent(&child
, &mouse
);
267 EXPECT_EQ(EP_POSTDISPATCH
, mouse
.phase());
268 EXPECT_FALSE(mouse
.stopped_propagation());
269 EXPECT_TRUE(mouse
.handled());
271 // |h1| marks the event as handled. So only the pre-target handlers should
272 // receive the event.
273 int expected
[] = { 1, 2, 3, 4 };
275 std::vector
<int>(expected
, expected
+ sizeof(expected
) / sizeof(int)),
276 child
.handler_list());
280 event_mod
.set_phase(EP_PREDISPATCH
);
281 event_mod
.set_result(ER_UNHANDLED
);
283 int nexpected
[] = { 1, 2, 3, 4, 5 };
284 h1
.set_event_result(ER_UNHANDLED
);
285 h5
.set_event_result(ER_CONSUMED
);
286 dispatcher
.ProcessEvent(&child
, &mouse
);
287 EXPECT_EQ(EP_POSTDISPATCH
, mouse
.phase());
288 EXPECT_TRUE(mouse
.stopped_propagation());
289 EXPECT_TRUE(mouse
.handled());
291 std::vector
<int>(nexpected
, nexpected
+ sizeof(nexpected
) / sizeof(int)),
292 child
.handler_list());
295 event_mod
.set_phase(EP_PREDISPATCH
);
296 event_mod
.set_result(ER_UNHANDLED
);
299 h1
.set_event_result(ER_CONSUMED
);
300 dispatcher
.ProcessEvent(&child
, &mouse
);
301 EXPECT_EQ(EP_POSTDISPATCH
, mouse
.phase());
302 EXPECT_TRUE(mouse
.stopped_propagation());
303 EXPECT_TRUE(mouse
.handled());
305 std::vector
<int>(exp
, exp
+ sizeof(exp
) / sizeof(int)),
306 child
.handler_list());
309 // Tests that the event-phases are correct.
310 TEST(EventDispatcherTest
, EventDispatchPhase
) {
311 TestEventDispatcher dispatcher
;
314 TestEventHandler
handler(11);
316 target
.AddPreTargetHandler(&handler
);
317 target
.AddPostTargetHandler(&handler
);
318 handler
.set_expect_pre_target(true);
319 handler
.set_expect_post_target(true);
321 MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(3, 4),
322 gfx::Point(3, 4), 0);
323 Event::DispatcherApi
event_mod(&mouse
);
324 dispatcher
.ProcessEvent(&target
, &mouse
);
325 EXPECT_EQ(ER_UNHANDLED
, mouse
.result());
327 int handlers
[] = { 11, 11 };
329 std::vector
<int>(handlers
, handlers
+ sizeof(handlers
) / sizeof(int)),
330 target
.handler_list());
333 // Tests that if the dispatcher is destroyed in the middle of pre or post-target
334 // dispatching events, it doesn't cause a crash.
335 TEST(EventDispatcherTest
, EventDispatcherDestroyedDuringDispatch
) {
336 // Test for pre-target first.
338 TestEventDispatcher
* dispatcher
= new TestEventDispatcher();
340 EventHandlerDestroyDispatcherDelegate
handler(dispatcher
, 5);
341 TestEventHandler
h1(1), h2(2);
343 target
.AddPreTargetHandler(&h1
);
344 target
.AddPreTargetHandler(&handler
);
345 target
.AddPreTargetHandler(&h2
);
347 h1
.set_expect_pre_target(true);
348 handler
.set_expect_pre_target(true);
349 // |h2| should not receive any events at all since |handler| will have
350 // destroyed the dispatcher.
351 h2
.set_expect_pre_target(false);
353 MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(3, 4),
354 gfx::Point(3, 4), 0);
355 Event::DispatcherApi
event_mod(&mouse
);
356 dispatcher
->ProcessEvent(&target
, &mouse
);
357 EXPECT_EQ(ER_CONSUMED
, mouse
.result());
358 EXPECT_EQ(2U, target
.handler_list().size());
359 EXPECT_EQ(1, target
.handler_list()[0]);
360 EXPECT_EQ(5, target
.handler_list()[1]);
363 // Test for non-cancelable event.
365 TestEventDispatcher
* dispatcher
= new TestEventDispatcher();
367 EventHandlerDestroyDispatcherDelegate
handler(dispatcher
, 5);
368 TestEventHandler
h1(1), h2(2);
370 target
.AddPreTargetHandler(&h1
);
371 target
.AddPreTargetHandler(&handler
);
372 target
.AddPreTargetHandler(&h2
);
374 h1
.set_expect_pre_target(true);
375 handler
.set_expect_pre_target(true);
376 // |h2| should not receive any events at all since |handler| will have
377 // destroyed the dispatcher.
378 h2
.set_expect_pre_target(false);
380 NonCancelableEvent event
;
381 Event::DispatcherApi
event_mod(&event
);
382 dispatcher
->ProcessEvent(&target
, &event
);
383 EXPECT_EQ(2U, target
.handler_list().size());
384 EXPECT_EQ(1, target
.handler_list()[0]);
385 EXPECT_EQ(5, target
.handler_list()[1]);
388 // Now test for post-target.
390 TestEventDispatcher
* dispatcher
= new TestEventDispatcher();
392 EventHandlerDestroyDispatcherDelegate
handler(dispatcher
, 5);
393 TestEventHandler
h1(1), h2(2);
395 target
.AddPostTargetHandler(&h1
);
396 target
.AddPostTargetHandler(&handler
);
397 target
.AddPostTargetHandler(&h2
);
399 h1
.set_expect_post_target(true);
400 handler
.set_expect_post_target(true);
401 // |h2| should not receive any events at all since |handler| will have
402 // destroyed the dispatcher.
403 h2
.set_expect_post_target(false);
405 MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(3, 4),
406 gfx::Point(3, 4), 0);
407 Event::DispatcherApi
event_mod(&mouse
);
408 dispatcher
->ProcessEvent(&target
, &mouse
);
409 EXPECT_EQ(ER_CONSUMED
, mouse
.result());
410 EXPECT_EQ(2U, target
.handler_list().size());
411 EXPECT_EQ(1, target
.handler_list()[0]);
412 EXPECT_EQ(5, target
.handler_list()[1]);
415 // Test for non-cancelable event.
417 TestEventDispatcher
* dispatcher
= new TestEventDispatcher();
419 EventHandlerDestroyDispatcherDelegate
handler(dispatcher
, 5);
420 TestEventHandler
h1(1), h2(2);
422 target
.AddPostTargetHandler(&h1
);
423 target
.AddPostTargetHandler(&handler
);
424 target
.AddPostTargetHandler(&h2
);
426 h1
.set_expect_post_target(true);
427 handler
.set_expect_post_target(true);
428 // |h2| should not receive any events at all since |handler| will have
429 // destroyed the dispatcher.
430 h2
.set_expect_post_target(false);
432 NonCancelableEvent event
;
433 Event::DispatcherApi
event_mod(&event
);
434 dispatcher
->ProcessEvent(&target
, &event
);
435 EXPECT_EQ(2U, target
.handler_list().size());
436 EXPECT_EQ(1, target
.handler_list()[0]);
437 EXPECT_EQ(5, target
.handler_list()[1]);
441 // Tests that a target becoming invalid in the middle of pre- or post-target
442 // event processing aborts processing.
443 TEST(EventDispatcherTest
, EventDispatcherInvalidateTarget
) {
444 TestEventDispatcher dispatcher
;
446 TestEventHandler
h1(1);
447 InvalidateTargetEventHandler
invalidate_handler(2);
448 TestEventHandler
h3(3);
450 target
.AddPreTargetHandler(&h1
);
451 target
.AddPreTargetHandler(&invalidate_handler
);
452 target
.AddPreTargetHandler(&h3
);
454 h1
.set_expect_pre_target(true);
455 invalidate_handler
.set_expect_pre_target(true);
456 // |h3| should not receive events as the target will be invalidated.
457 h3
.set_expect_pre_target(false);
459 MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(3, 4), gfx::Point(3, 4), 0);
460 dispatcher
.ProcessEvent(&target
, &mouse
);
461 EXPECT_FALSE(target
.valid());
462 EXPECT_TRUE(mouse
.stopped_propagation());
463 EXPECT_EQ(2U, target
.handler_list().size());
464 EXPECT_EQ(1, target
.handler_list()[0]);
465 EXPECT_EQ(2, target
.handler_list()[1]);
467 // Test for non-cancelable event.
469 NonCancelableEvent event
;
470 dispatcher
.ProcessEvent(&target
, &event
);
471 EXPECT_FALSE(target
.valid());
472 EXPECT_TRUE(mouse
.stopped_propagation());
473 EXPECT_EQ(2U, target
.handler_list().size());
474 EXPECT_EQ(1, target
.handler_list()[0]);
475 EXPECT_EQ(2, target
.handler_list()[1]);
478 // Tests that if an event-handler gets destroyed during event-dispatch, it does
479 // not cause a crash.
480 TEST(EventDispatcherTest
, EventHandlerDestroyedDuringDispatch
) {
482 TestEventDispatcher dispatcher
;
484 TestEventHandler
h1(1);
485 TestEventHandler
* h3
= new TestEventHandler(3);
486 EventHandlerDestroyer
handle_destroyer(2, h3
);
488 target
.AddPreTargetHandler(&h1
);
489 target
.AddPreTargetHandler(&handle_destroyer
);
490 target
.AddPreTargetHandler(h3
);
492 h1
.set_expect_pre_target(true);
493 handle_destroyer
.set_expect_pre_target(true);
494 // |h3| should not receive events since |handle_destroyer| will have
496 h3
->set_expect_pre_target(false);
498 MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(3, 4), gfx::Point(3, 4), 0);
499 dispatcher
.ProcessEvent(&target
, &mouse
);
500 EXPECT_FALSE(mouse
.stopped_propagation());
501 EXPECT_EQ(2U, target
.handler_list().size());
502 EXPECT_EQ(1, target
.handler_list()[0]);
503 EXPECT_EQ(2, target
.handler_list()[1]);
506 // Test for non-cancelable events.
508 TestEventDispatcher dispatcher
;
510 TestEventHandler
h1(1);
511 TestEventHandler
* h3
= new TestEventHandler(3);
512 EventHandlerDestroyer
handle_destroyer(2, h3
);
514 target
.AddPreTargetHandler(&h1
);
515 target
.AddPreTargetHandler(&handle_destroyer
);
516 target
.AddPreTargetHandler(h3
);
518 h1
.set_expect_pre_target(true);
519 handle_destroyer
.set_expect_pre_target(true);
520 h3
->set_expect_pre_target(false);
522 NonCancelableEvent event
;
523 dispatcher
.ProcessEvent(&target
, &event
);
524 EXPECT_EQ(2U, target
.handler_list().size());
525 EXPECT_EQ(1, target
.handler_list()[0]);
526 EXPECT_EQ(2, target
.handler_list()[1]);
530 // Tests that things work correctly if an event-handler destroys both the
531 // dispatcher and a handler.
532 TEST(EventDispatcherTest
, EventHandlerAndDispatcherDestroyedDuringDispatch
) {
534 TestEventDispatcher
* dispatcher
= new TestEventDispatcher();
536 TestEventHandler
h1(1);
537 TestEventHandler
* h3
= new TestEventHandler(3);
538 EventHandlerDestroyer
destroyer(2, h3
);
540 target
.AddPreTargetHandler(&h1
);
541 target
.AddPreTargetHandler(&destroyer
);
542 target
.AddPreTargetHandler(h3
);
544 h1
.set_expect_pre_target(true);
545 destroyer
.set_expect_pre_target(true);
546 destroyer
.set_dispatcher_delegate(dispatcher
);
547 // |h3| should not receive events since |destroyer| will have destroyed
549 h3
->set_expect_pre_target(false);
551 MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(3, 4), gfx::Point(3, 4), 0);
552 dispatcher
->ProcessEvent(&target
, &mouse
);
553 EXPECT_TRUE(mouse
.stopped_propagation());
554 EXPECT_EQ(2U, target
.handler_list().size());
555 EXPECT_EQ(1, target
.handler_list()[0]);
556 EXPECT_EQ(2, target
.handler_list()[1]);
559 // Test for non-cancelable events.
561 TestEventDispatcher
* dispatcher
= new TestEventDispatcher();
563 TestEventHandler
h1(1);
564 TestEventHandler
* h3
= new TestEventHandler(3);
565 EventHandlerDestroyer
destroyer(2, h3
);
567 target
.AddPreTargetHandler(&h1
);
568 target
.AddPreTargetHandler(&destroyer
);
569 target
.AddPreTargetHandler(h3
);
571 h1
.set_expect_pre_target(true);
572 destroyer
.set_expect_pre_target(true);
573 destroyer
.set_dispatcher_delegate(dispatcher
);
574 // |h3| should not receive events since |destroyer| will have destroyed
576 h3
->set_expect_pre_target(false);
578 NonCancelableEvent event
;
579 dispatcher
->ProcessEvent(&target
, &event
);
580 EXPECT_EQ(2U, target
.handler_list().size());
581 EXPECT_EQ(1, target
.handler_list()[0]);
582 EXPECT_EQ(2, target
.handler_list()[1]);