Update V8 to version 4.7.50.
[chromium-blink-merge.git] / ui / events / event_dispatcher_unittest.cc
blobb57d92b4dfdf50c125b8991e39748dc468776cf9
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_dispatcher.h"
10 #include "ui/events/event_target.h"
11 #include "ui/events/event_target_iterator.h"
12 #include "ui/events/event_utils.h"
14 namespace ui {
16 namespace {
18 class TestTarget : public EventTarget {
19 public:
20 TestTarget() : parent_(NULL), valid_(true) {}
21 ~TestTarget() override {}
23 void set_parent(TestTarget* parent) { parent_ = parent; }
25 bool valid() const { return valid_; }
26 void set_valid(bool valid) { valid_ = valid; }
28 void AddHandlerId(int id) {
29 handler_list_.push_back(id);
32 const std::vector<int>& handler_list() const { return handler_list_; }
34 void Reset() {
35 handler_list_.clear();
36 valid_ = true;
39 private:
40 // Overridden from EventTarget:
41 bool CanAcceptEvent(const ui::Event& event) override { return true; }
43 EventTarget* GetParentTarget() override { return parent_; }
45 scoped_ptr<EventTargetIterator> GetChildIterator() const override {
46 return nullptr;
49 EventTargeter* GetEventTargeter() override { return NULL; }
51 TestTarget* parent_;
52 std::vector<int> handler_list_;
53 bool valid_;
55 DISALLOW_COPY_AND_ASSIGN(TestTarget);
58 class TestEventHandler : public EventHandler {
59 public:
60 TestEventHandler(int id)
61 : id_(id),
62 event_result_(ER_UNHANDLED),
63 expect_pre_target_(false),
64 expect_post_target_(false),
65 received_pre_target_(false) {
68 ~TestEventHandler() override {}
70 virtual void ReceivedEvent(Event* event) {
71 static_cast<TestTarget*>(event->target())->AddHandlerId(id_);
72 if (event->phase() == ui::EP_POSTTARGET) {
73 EXPECT_TRUE(expect_post_target_);
74 if (expect_pre_target_)
75 EXPECT_TRUE(received_pre_target_);
76 } else if (event->phase() == ui::EP_PRETARGET) {
77 EXPECT_TRUE(expect_pre_target_);
78 received_pre_target_ = true;
79 } else {
80 NOTREACHED();
84 void set_event_result(EventResult result) { event_result_ = result; }
86 void set_expect_pre_target(bool expect) { expect_pre_target_ = expect; }
87 void set_expect_post_target(bool expect) { expect_post_target_ = expect; }
89 private:
90 // Overridden from EventHandler:
91 void OnEvent(Event* event) override {
92 ui::EventHandler::OnEvent(event);
93 ReceivedEvent(event);
94 SetStatusOnEvent(event);
97 void SetStatusOnEvent(Event* event) {
98 if (event_result_ & ui::ER_CONSUMED)
99 event->StopPropagation();
100 if (event_result_ & ui::ER_HANDLED)
101 event->SetHandled();
104 int id_;
105 EventResult event_result_;
106 bool expect_pre_target_;
107 bool expect_post_target_;
108 bool received_pre_target_;
110 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
113 typedef CancelModeEvent NonCancelableEvent;
115 // Destroys the dispatcher-delegate when it receives any event.
116 class EventHandlerDestroyDispatcherDelegate : public TestEventHandler {
117 public:
118 EventHandlerDestroyDispatcherDelegate(EventDispatcherDelegate* delegate,
119 int id)
120 : TestEventHandler(id),
121 dispatcher_delegate_(delegate) {
124 ~EventHandlerDestroyDispatcherDelegate() override {}
126 private:
127 void ReceivedEvent(Event* event) override {
128 TestEventHandler::ReceivedEvent(event);
129 delete dispatcher_delegate_;
132 EventDispatcherDelegate* dispatcher_delegate_;
134 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyDispatcherDelegate);
137 // Invalidates the target when it receives any event.
138 class InvalidateTargetEventHandler : public TestEventHandler {
139 public:
140 explicit InvalidateTargetEventHandler(int id) : TestEventHandler(id) {}
141 ~InvalidateTargetEventHandler() override {}
143 private:
144 void ReceivedEvent(Event* event) override {
145 TestEventHandler::ReceivedEvent(event);
146 TestTarget* target = static_cast<TestTarget*>(event->target());
147 target->set_valid(false);
150 DISALLOW_COPY_AND_ASSIGN(InvalidateTargetEventHandler);
153 // Destroys a second event handler when this handler gets an event.
154 // Optionally also destroys the dispatcher.
155 class EventHandlerDestroyer : public TestEventHandler {
156 public:
157 EventHandlerDestroyer(int id, EventHandler* destroy)
158 : TestEventHandler(id),
159 to_destroy_(destroy),
160 dispatcher_delegate_(NULL) {
163 ~EventHandlerDestroyer() override { CHECK(!to_destroy_); }
165 void set_dispatcher_delegate(EventDispatcherDelegate* dispatcher_delegate) {
166 dispatcher_delegate_ = dispatcher_delegate;
169 private:
170 void ReceivedEvent(Event* event) override {
171 TestEventHandler::ReceivedEvent(event);
172 delete to_destroy_;
173 to_destroy_ = NULL;
175 if (dispatcher_delegate_) {
176 delete dispatcher_delegate_;
177 dispatcher_delegate_ = NULL;
181 EventHandler* to_destroy_;
182 EventDispatcherDelegate* dispatcher_delegate_;
184 DISALLOW_COPY_AND_ASSIGN(EventHandlerDestroyer);
187 class TestEventDispatcher : public EventDispatcherDelegate {
188 public:
189 TestEventDispatcher() {}
191 ~TestEventDispatcher() override {}
193 EventDispatchDetails ProcessEvent(EventTarget* target, Event* event) {
194 return DispatchEvent(target, event);
197 private:
198 // Overridden from EventDispatcherDelegate:
199 bool CanDispatchToTarget(EventTarget* target) override {
200 TestTarget* test_target = static_cast<TestTarget*>(target);
201 return test_target->valid();
204 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher);
207 } // namespace
209 TEST(EventDispatcherTest, EventDispatchOrder) {
210 TestEventDispatcher dispatcher;
211 TestTarget parent, child;
212 TestEventHandler h1(1), h2(2), h3(3), h4(4);
213 TestEventHandler h5(5), h6(6), h7(7), h8(8);
215 child.set_parent(&parent);
217 parent.AddPreTargetHandler(&h1);
218 parent.AddPreTargetHandler(&h2);
220 child.AddPreTargetHandler(&h3);
221 child.AddPreTargetHandler(&h4);
223 h1.set_expect_pre_target(true);
224 h2.set_expect_pre_target(true);
225 h3.set_expect_pre_target(true);
226 h4.set_expect_pre_target(true);
228 child.AddPostTargetHandler(&h5);
229 child.AddPostTargetHandler(&h6);
231 parent.AddPostTargetHandler(&h7);
232 parent.AddPostTargetHandler(&h8);
234 h5.set_expect_post_target(true);
235 h6.set_expect_post_target(true);
236 h7.set_expect_post_target(true);
237 h8.set_expect_post_target(true);
239 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), gfx::Point(3, 4),
240 ui::EventTimeForNow(), 0, 0);
241 Event::DispatcherApi event_mod(&mouse);
242 dispatcher.ProcessEvent(&child, &mouse);
243 EXPECT_FALSE(mouse.stopped_propagation());
244 EXPECT_FALSE(mouse.handled());
247 int expected[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
248 EXPECT_EQ(
249 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)),
250 child.handler_list());
253 child.Reset();
254 event_mod.set_phase(EP_PREDISPATCH);
255 event_mod.set_result(ER_UNHANDLED);
257 h1.set_event_result(ER_HANDLED);
258 dispatcher.ProcessEvent(&child, &mouse);
259 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase());
260 EXPECT_FALSE(mouse.stopped_propagation());
261 EXPECT_TRUE(mouse.handled());
263 // |h1| marks the event as handled. So only the pre-target handlers should
264 // receive the event.
265 int expected[] = { 1, 2, 3, 4 };
266 EXPECT_EQ(
267 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)),
268 child.handler_list());
271 child.Reset();
272 event_mod.set_phase(EP_PREDISPATCH);
273 event_mod.set_result(ER_UNHANDLED);
275 int nexpected[] = { 1, 2, 3, 4, 5 };
276 h1.set_event_result(ER_UNHANDLED);
277 h5.set_event_result(ER_CONSUMED);
278 dispatcher.ProcessEvent(&child, &mouse);
279 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase());
280 EXPECT_TRUE(mouse.stopped_propagation());
281 EXPECT_TRUE(mouse.handled());
282 EXPECT_EQ(
283 std::vector<int>(nexpected, nexpected + sizeof(nexpected) / sizeof(int)),
284 child.handler_list());
286 child.Reset();
287 event_mod.set_phase(EP_PREDISPATCH);
288 event_mod.set_result(ER_UNHANDLED);
290 int exp[] = { 1 };
291 h1.set_event_result(ER_CONSUMED);
292 dispatcher.ProcessEvent(&child, &mouse);
293 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase());
294 EXPECT_TRUE(mouse.stopped_propagation());
295 EXPECT_TRUE(mouse.handled());
296 EXPECT_EQ(
297 std::vector<int>(exp, exp + sizeof(exp) / sizeof(int)),
298 child.handler_list());
301 // Tests that the event-phases are correct.
302 TEST(EventDispatcherTest, EventDispatchPhase) {
303 TestEventDispatcher dispatcher;
304 TestTarget target;
306 TestEventHandler handler(11);
308 target.AddPreTargetHandler(&handler);
309 target.AddPostTargetHandler(&handler);
310 handler.set_expect_pre_target(true);
311 handler.set_expect_post_target(true);
313 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), gfx::Point(3, 4),
314 ui::EventTimeForNow(), 0, 0);
315 Event::DispatcherApi event_mod(&mouse);
316 dispatcher.ProcessEvent(&target, &mouse);
317 EXPECT_EQ(ER_UNHANDLED, mouse.result());
319 int handlers[] = { 11, 11 };
320 EXPECT_EQ(
321 std::vector<int>(handlers, handlers + sizeof(handlers) / sizeof(int)),
322 target.handler_list());
325 // Tests that if the dispatcher is destroyed in the middle of pre or post-target
326 // dispatching events, it doesn't cause a crash.
327 TEST(EventDispatcherTest, EventDispatcherDestroyedDuringDispatch) {
328 // Test for pre-target first.
330 TestEventDispatcher* dispatcher = new TestEventDispatcher();
331 TestTarget target;
332 EventHandlerDestroyDispatcherDelegate handler(dispatcher, 5);
333 TestEventHandler h1(1), h2(2);
335 target.AddPreTargetHandler(&h1);
336 target.AddPreTargetHandler(&handler);
337 target.AddPreTargetHandler(&h2);
339 h1.set_expect_pre_target(true);
340 handler.set_expect_pre_target(true);
341 // |h2| should not receive any events at all since |handler| will have
342 // destroyed the dispatcher.
343 h2.set_expect_pre_target(false);
345 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), gfx::Point(3, 4),
346 ui::EventTimeForNow(), 0, 0);
347 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &mouse);
348 EXPECT_TRUE(details.dispatcher_destroyed);
349 EXPECT_EQ(ER_CONSUMED, mouse.result());
350 EXPECT_EQ(2U, target.handler_list().size());
351 EXPECT_EQ(1, target.handler_list()[0]);
352 EXPECT_EQ(5, target.handler_list()[1]);
355 // Test for non-cancelable event.
357 TestEventDispatcher* dispatcher = new TestEventDispatcher();
358 TestTarget target;
359 EventHandlerDestroyDispatcherDelegate handler(dispatcher, 5);
360 TestEventHandler h1(1), h2(2);
362 target.AddPreTargetHandler(&h1);
363 target.AddPreTargetHandler(&handler);
364 target.AddPreTargetHandler(&h2);
366 h1.set_expect_pre_target(true);
367 handler.set_expect_pre_target(true);
368 // |h2| should not receive any events at all since |handler| will have
369 // destroyed the dispatcher.
370 h2.set_expect_pre_target(false);
372 NonCancelableEvent event;
373 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &event);
374 EXPECT_TRUE(details.dispatcher_destroyed);
375 EXPECT_EQ(2U, target.handler_list().size());
376 EXPECT_EQ(1, target.handler_list()[0]);
377 EXPECT_EQ(5, target.handler_list()[1]);
380 // Now test for post-target.
382 TestEventDispatcher* dispatcher = new TestEventDispatcher();
383 TestTarget target;
384 EventHandlerDestroyDispatcherDelegate handler(dispatcher, 5);
385 TestEventHandler h1(1), h2(2);
387 target.AddPostTargetHandler(&h1);
388 target.AddPostTargetHandler(&handler);
389 target.AddPostTargetHandler(&h2);
391 h1.set_expect_post_target(true);
392 handler.set_expect_post_target(true);
393 // |h2| should not receive any events at all since |handler| will have
394 // destroyed the dispatcher.
395 h2.set_expect_post_target(false);
397 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), gfx::Point(3, 4),
398 ui::EventTimeForNow(), 0, 0);
399 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &mouse);
400 EXPECT_TRUE(details.dispatcher_destroyed);
401 EXPECT_EQ(ER_CONSUMED, mouse.result());
402 EXPECT_EQ(2U, target.handler_list().size());
403 EXPECT_EQ(1, target.handler_list()[0]);
404 EXPECT_EQ(5, target.handler_list()[1]);
407 // Test for non-cancelable event.
409 TestEventDispatcher* dispatcher = new TestEventDispatcher();
410 TestTarget target;
411 EventHandlerDestroyDispatcherDelegate handler(dispatcher, 5);
412 TestEventHandler h1(1), h2(2);
414 target.AddPostTargetHandler(&h1);
415 target.AddPostTargetHandler(&handler);
416 target.AddPostTargetHandler(&h2);
418 h1.set_expect_post_target(true);
419 handler.set_expect_post_target(true);
420 // |h2| should not receive any events at all since |handler| will have
421 // destroyed the dispatcher.
422 h2.set_expect_post_target(false);
424 NonCancelableEvent event;
425 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &event);
426 EXPECT_TRUE(details.dispatcher_destroyed);
427 EXPECT_EQ(2U, target.handler_list().size());
428 EXPECT_EQ(1, target.handler_list()[0]);
429 EXPECT_EQ(5, target.handler_list()[1]);
433 // Tests that a target becoming invalid in the middle of pre- or post-target
434 // event processing aborts processing.
435 TEST(EventDispatcherTest, EventDispatcherInvalidateTarget) {
436 TestEventDispatcher dispatcher;
437 TestTarget target;
438 TestEventHandler h1(1);
439 InvalidateTargetEventHandler invalidate_handler(2);
440 TestEventHandler h3(3);
442 target.AddPreTargetHandler(&h1);
443 target.AddPreTargetHandler(&invalidate_handler);
444 target.AddPreTargetHandler(&h3);
446 h1.set_expect_pre_target(true);
447 invalidate_handler.set_expect_pre_target(true);
448 // |h3| should not receive events as the target will be invalidated.
449 h3.set_expect_pre_target(false);
451 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), gfx::Point(3, 4),
452 ui::EventTimeForNow(), 0, 0);
453 EventDispatchDetails details = dispatcher.ProcessEvent(&target, &mouse);
454 EXPECT_FALSE(details.dispatcher_destroyed);
455 EXPECT_TRUE(details.target_destroyed);
456 EXPECT_FALSE(target.valid());
457 EXPECT_TRUE(mouse.stopped_propagation());
458 EXPECT_EQ(2U, target.handler_list().size());
459 EXPECT_EQ(1, target.handler_list()[0]);
460 EXPECT_EQ(2, target.handler_list()[1]);
462 // Test for non-cancelable event.
463 target.Reset();
464 NonCancelableEvent event;
465 details = dispatcher.ProcessEvent(&target, &event);
466 EXPECT_FALSE(details.dispatcher_destroyed);
467 EXPECT_TRUE(details.target_destroyed);
468 EXPECT_FALSE(target.valid());
469 EXPECT_EQ(2U, target.handler_list().size());
470 EXPECT_EQ(1, target.handler_list()[0]);
471 EXPECT_EQ(2, target.handler_list()[1]);
474 // Tests that if an event-handler gets destroyed during event-dispatch, it does
475 // not cause a crash.
476 TEST(EventDispatcherTest, EventHandlerDestroyedDuringDispatch) {
478 TestEventDispatcher dispatcher;
479 TestTarget target;
480 TestEventHandler h1(1);
481 TestEventHandler* h3 = new TestEventHandler(3);
482 EventHandlerDestroyer handle_destroyer(2, h3);
484 target.AddPreTargetHandler(&h1);
485 target.AddPreTargetHandler(&handle_destroyer);
486 target.AddPreTargetHandler(h3);
488 h1.set_expect_pre_target(true);
489 handle_destroyer.set_expect_pre_target(true);
490 // |h3| should not receive events since |handle_destroyer| will have
491 // destroyed it.
492 h3->set_expect_pre_target(false);
494 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), gfx::Point(3, 4),
495 ui::EventTimeForNow(), 0, 0);
496 EventDispatchDetails details = dispatcher.ProcessEvent(&target, &mouse);
497 EXPECT_FALSE(details.dispatcher_destroyed);
498 EXPECT_FALSE(details.target_destroyed);
499 EXPECT_FALSE(mouse.stopped_propagation());
500 EXPECT_EQ(2U, target.handler_list().size());
501 EXPECT_EQ(1, target.handler_list()[0]);
502 EXPECT_EQ(2, target.handler_list()[1]);
505 // Test for non-cancelable events.
507 TestEventDispatcher dispatcher;
508 TestTarget target;
509 TestEventHandler h1(1);
510 TestEventHandler* h3 = new TestEventHandler(3);
511 EventHandlerDestroyer handle_destroyer(2, h3);
513 target.AddPreTargetHandler(&h1);
514 target.AddPreTargetHandler(&handle_destroyer);
515 target.AddPreTargetHandler(h3);
517 h1.set_expect_pre_target(true);
518 handle_destroyer.set_expect_pre_target(true);
519 h3->set_expect_pre_target(false);
521 NonCancelableEvent event;
522 EventDispatchDetails details = dispatcher.ProcessEvent(&target, &event);
523 EXPECT_FALSE(details.dispatcher_destroyed);
524 EXPECT_FALSE(details.target_destroyed);
525 EXPECT_EQ(2U, target.handler_list().size());
526 EXPECT_EQ(1, target.handler_list()[0]);
527 EXPECT_EQ(2, target.handler_list()[1]);
531 // Tests that things work correctly if an event-handler destroys both the
532 // dispatcher and a handler.
533 TEST(EventDispatcherTest, EventHandlerAndDispatcherDestroyedDuringDispatch) {
535 TestEventDispatcher* dispatcher = new TestEventDispatcher();
536 TestTarget target;
537 TestEventHandler h1(1);
538 TestEventHandler* h3 = new TestEventHandler(3);
539 EventHandlerDestroyer destroyer(2, h3);
541 target.AddPreTargetHandler(&h1);
542 target.AddPreTargetHandler(&destroyer);
543 target.AddPreTargetHandler(h3);
545 h1.set_expect_pre_target(true);
546 destroyer.set_expect_pre_target(true);
547 destroyer.set_dispatcher_delegate(dispatcher);
548 // |h3| should not receive events since |destroyer| will have destroyed
549 // it.
550 h3->set_expect_pre_target(false);
552 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), gfx::Point(3, 4),
553 ui::EventTimeForNow(), 0, 0);
554 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &mouse);
555 EXPECT_TRUE(details.dispatcher_destroyed);
556 EXPECT_TRUE(mouse.stopped_propagation());
557 EXPECT_EQ(2U, target.handler_list().size());
558 EXPECT_EQ(1, target.handler_list()[0]);
559 EXPECT_EQ(2, target.handler_list()[1]);
562 // Test for non-cancelable events.
564 TestEventDispatcher* dispatcher = new TestEventDispatcher();
565 TestTarget target;
566 TestEventHandler h1(1);
567 TestEventHandler* h3 = new TestEventHandler(3);
568 EventHandlerDestroyer destroyer(2, h3);
570 target.AddPreTargetHandler(&h1);
571 target.AddPreTargetHandler(&destroyer);
572 target.AddPreTargetHandler(h3);
574 h1.set_expect_pre_target(true);
575 destroyer.set_expect_pre_target(true);
576 destroyer.set_dispatcher_delegate(dispatcher);
577 // |h3| should not receive events since |destroyer| will have destroyed
578 // it.
579 h3->set_expect_pre_target(false);
581 NonCancelableEvent event;
582 EventDispatchDetails details = dispatcher->ProcessEvent(&target, &event);
583 EXPECT_TRUE(details.dispatcher_destroyed);
584 EXPECT_EQ(2U, target.handler_list().size());
585 EXPECT_EQ(1, target.handler_list()[0]);
586 EXPECT_EQ(2, target.handler_list()[1]);
590 } // namespace ui