Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / aura / window_event_dispatcher_unittest.cc
blobf83cb7f15fce23096c6e991c13003ca57ab8c00f
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/aura/window_event_dispatcher.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/aura/client/capture_client.h"
14 #include "ui/aura/client/event_client.h"
15 #include "ui/aura/client/focus_client.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/test/aura_test_base.h"
18 #include "ui/aura/test/env_test_helper.h"
19 #include "ui/aura/test/test_cursor_client.h"
20 #include "ui/aura/test/test_screen.h"
21 #include "ui/aura/test/test_window_delegate.h"
22 #include "ui/aura/test/test_windows.h"
23 #include "ui/aura/window.h"
24 #include "ui/aura/window_tracker.h"
25 #include "ui/base/hit_test.h"
26 #include "ui/events/event.h"
27 #include "ui/events/event_handler.h"
28 #include "ui/events/event_utils.h"
29 #include "ui/events/gesture_detection/gesture_configuration.h"
30 #include "ui/events/keycodes/keyboard_codes.h"
31 #include "ui/events/test/event_generator.h"
32 #include "ui/events/test/test_event_handler.h"
33 #include "ui/gfx/geometry/point.h"
34 #include "ui/gfx/geometry/rect.h"
35 #include "ui/gfx/screen.h"
36 #include "ui/gfx/transform.h"
38 namespace aura {
39 namespace {
41 // A delegate that always returns a non-client component for hit tests.
42 class NonClientDelegate : public test::TestWindowDelegate {
43 public:
44 NonClientDelegate()
45 : non_client_count_(0),
46 mouse_event_count_(0),
47 mouse_event_flags_(0x0) {
49 ~NonClientDelegate() override {}
51 int non_client_count() const { return non_client_count_; }
52 gfx::Point non_client_location() const { return non_client_location_; }
53 int mouse_event_count() const { return mouse_event_count_; }
54 gfx::Point mouse_event_location() const { return mouse_event_location_; }
55 int mouse_event_flags() const { return mouse_event_flags_; }
57 int GetNonClientComponent(const gfx::Point& location) const override {
58 NonClientDelegate* self = const_cast<NonClientDelegate*>(this);
59 self->non_client_count_++;
60 self->non_client_location_ = location;
61 return HTTOPLEFT;
63 void OnMouseEvent(ui::MouseEvent* event) override {
64 mouse_event_count_++;
65 mouse_event_location_ = event->location();
66 mouse_event_flags_ = event->flags();
67 event->SetHandled();
70 private:
71 int non_client_count_;
72 gfx::Point non_client_location_;
73 int mouse_event_count_;
74 gfx::Point mouse_event_location_;
75 int mouse_event_flags_;
77 DISALLOW_COPY_AND_ASSIGN(NonClientDelegate);
80 // A simple event handler that consumes key events.
81 class ConsumeKeyHandler : public ui::test::TestEventHandler {
82 public:
83 ConsumeKeyHandler() {}
84 ~ConsumeKeyHandler() override {}
86 // Overridden from ui::EventHandler:
87 void OnKeyEvent(ui::KeyEvent* event) override {
88 ui::test::TestEventHandler::OnKeyEvent(event);
89 event->StopPropagation();
92 private:
93 DISALLOW_COPY_AND_ASSIGN(ConsumeKeyHandler);
96 bool IsFocusedWindow(aura::Window* window) {
97 return client::GetFocusClient(window)->GetFocusedWindow() == window;
100 } // namespace
102 typedef test::AuraTestBase WindowEventDispatcherTest;
104 TEST_F(WindowEventDispatcherTest, OnHostMouseEvent) {
105 // Create two non-overlapping windows so we don't have to worry about which
106 // is on top.
107 scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate());
108 scoped_ptr<NonClientDelegate> delegate2(new NonClientDelegate());
109 const int kWindowWidth = 123;
110 const int kWindowHeight = 45;
111 gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight);
112 gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight);
113 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
114 delegate1.get(), -1234, bounds1, root_window()));
115 scoped_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
116 delegate2.get(), -5678, bounds2, root_window()));
118 // Send a mouse event to window1.
119 gfx::Point point(101, 201);
120 ui::MouseEvent event1(ui::ET_MOUSE_PRESSED, point, point,
121 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
122 ui::EF_LEFT_MOUSE_BUTTON);
123 DispatchEventUsingWindowDispatcher(&event1);
125 // Event was tested for non-client area for the target window.
126 EXPECT_EQ(1, delegate1->non_client_count());
127 EXPECT_EQ(0, delegate2->non_client_count());
128 // The non-client component test was in local coordinates.
129 EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location());
130 // Mouse event was received by target window.
131 EXPECT_EQ(1, delegate1->mouse_event_count());
132 EXPECT_EQ(0, delegate2->mouse_event_count());
133 // Event was in local coordinates.
134 EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location());
135 // Non-client flag was set.
136 EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT);
139 TEST_F(WindowEventDispatcherTest, RepostEvent) {
140 // Test RepostEvent in RootWindow. It only works for Mouse Press.
141 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
142 gfx::Point point(10, 10);
143 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, point, point,
144 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
145 ui::EF_LEFT_MOUSE_BUTTON);
146 host()->dispatcher()->RepostEvent(event);
147 RunAllPendingInMessageLoop();
148 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
151 // Check that we correctly track the state of the mouse buttons in response to
152 // button press and release events.
153 TEST_F(WindowEventDispatcherTest, MouseButtonState) {
154 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
156 gfx::Point location;
157 scoped_ptr<ui::MouseEvent> event;
159 // Press the left button.
160 event.reset(new ui::MouseEvent(
161 ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
162 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
163 DispatchEventUsingWindowDispatcher(event.get());
164 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
166 // Additionally press the right.
167 event.reset(new ui::MouseEvent(
168 ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
169 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON,
170 ui::EF_RIGHT_MOUSE_BUTTON));
171 DispatchEventUsingWindowDispatcher(event.get());
172 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
174 // Release the left button.
175 event.reset(new ui::MouseEvent(
176 ui::ET_MOUSE_RELEASED, location, location, ui::EventTimeForNow(),
177 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
178 DispatchEventUsingWindowDispatcher(event.get());
179 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
181 // Release the right button. We should ignore the Shift-is-down flag.
182 event.reset(new ui::MouseEvent(ui::ET_MOUSE_RELEASED, location, location,
183 ui::EventTimeForNow(), ui::EF_SHIFT_DOWN,
184 ui::EF_RIGHT_MOUSE_BUTTON));
185 DispatchEventUsingWindowDispatcher(event.get());
186 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
188 // Press the middle button.
189 event.reset(new ui::MouseEvent(
190 ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
191 ui::EF_MIDDLE_MOUSE_BUTTON, ui::EF_MIDDLE_MOUSE_BUTTON));
192 DispatchEventUsingWindowDispatcher(event.get());
193 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
196 TEST_F(WindowEventDispatcherTest, TranslatedEvent) {
197 scoped_ptr<Window> w1(test::CreateTestWindowWithDelegate(NULL, 1,
198 gfx::Rect(50, 50, 100, 100), root_window()));
200 gfx::Point origin(100, 100);
201 ui::MouseEvent root(ui::ET_MOUSE_PRESSED, origin, origin,
202 ui::EventTimeForNow(), 0, 0);
204 EXPECT_EQ("100,100", root.location().ToString());
205 EXPECT_EQ("100,100", root.root_location().ToString());
207 ui::MouseEvent translated_event(
208 root, static_cast<Window*>(root_window()), w1.get(),
209 ui::ET_MOUSE_ENTERED, root.flags());
210 EXPECT_EQ("50,50", translated_event.location().ToString());
211 EXPECT_EQ("100,100", translated_event.root_location().ToString());
214 namespace {
216 class TestEventClient : public client::EventClient {
217 public:
218 static const int kNonLockWindowId = 100;
219 static const int kLockWindowId = 200;
221 explicit TestEventClient(Window* root_window)
222 : root_window_(root_window),
223 lock_(false) {
224 client::SetEventClient(root_window_, this);
225 Window* lock_window =
226 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
227 lock_window->set_id(kLockWindowId);
228 Window* non_lock_window =
229 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
230 non_lock_window->set_id(kNonLockWindowId);
232 ~TestEventClient() override { client::SetEventClient(root_window_, NULL); }
234 // Starts/stops locking. Locking prevents windows other than those inside
235 // the lock container from receiving events, getting focus etc.
236 void Lock() {
237 lock_ = true;
239 void Unlock() {
240 lock_ = false;
243 Window* GetLockWindow() {
244 return const_cast<Window*>(
245 static_cast<const TestEventClient*>(this)->GetLockWindow());
247 const Window* GetLockWindow() const {
248 return root_window_->GetChildById(kLockWindowId);
250 Window* GetNonLockWindow() {
251 return root_window_->GetChildById(kNonLockWindowId);
254 private:
255 // Overridden from client::EventClient:
256 bool CanProcessEventsWithinSubtree(const Window* window) const override {
257 return lock_ ?
258 window->Contains(GetLockWindow()) || GetLockWindow()->Contains(window) :
259 true;
262 ui::EventTarget* GetToplevelEventTarget() override { return NULL; }
264 Window* root_window_;
265 bool lock_;
267 DISALLOW_COPY_AND_ASSIGN(TestEventClient);
270 } // namespace
272 TEST_F(WindowEventDispatcherTest, CanProcessEventsWithinSubtree) {
273 TestEventClient client(root_window());
274 test::TestWindowDelegate d;
276 ui::test::TestEventHandler nonlock_ef;
277 ui::test::TestEventHandler lock_ef;
278 client.GetNonLockWindow()->AddPreTargetHandler(&nonlock_ef);
279 client.GetLockWindow()->AddPreTargetHandler(&lock_ef);
281 Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
282 client.GetNonLockWindow());
283 w1->set_id(1);
284 Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
285 client.GetNonLockWindow());
286 w2->set_id(2);
287 scoped_ptr<Window> w3(
288 test::CreateTestWindowWithDelegate(&d, 3, gfx::Rect(30, 30, 20, 20),
289 client.GetLockWindow()));
291 w1->Focus();
292 EXPECT_TRUE(IsFocusedWindow(w1));
294 client.Lock();
296 // Since we're locked, the attempt to focus w2 will be ignored.
297 w2->Focus();
298 EXPECT_TRUE(IsFocusedWindow(w1));
299 EXPECT_FALSE(IsFocusedWindow(w2));
302 // Attempting to send a key event to w1 (not in the lock container) should
303 // cause focus to be reset.
304 ui::test::EventGenerator generator(root_window());
305 generator.PressKey(ui::VKEY_SPACE, 0);
306 EXPECT_EQ(NULL, client::GetFocusClient(w1)->GetFocusedWindow());
307 EXPECT_FALSE(IsFocusedWindow(w1));
311 // Events sent to a window not in the lock container will not be processed.
312 // i.e. never sent to the non-lock container's event filter.
313 ui::test::EventGenerator generator(root_window(), w1);
314 generator.ClickLeftButton();
315 EXPECT_EQ(0, nonlock_ef.num_mouse_events());
317 // Events sent to a window in the lock container will be processed.
318 ui::test::EventGenerator generator3(root_window(), w3.get());
319 generator3.PressLeftButton();
320 EXPECT_EQ(1, lock_ef.num_mouse_events());
323 // Prevent w3 from being deleted by the hierarchy since its delegate is owned
324 // by this scope.
325 w3->parent()->RemoveChild(w3.get());
328 TEST_F(WindowEventDispatcherTest, DontIgnoreUnknownKeys) {
329 ConsumeKeyHandler handler;
330 root_window()->AddPreTargetHandler(&handler);
332 ui::KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, ui::EF_NONE);
333 DispatchEventUsingWindowDispatcher(&unknown_event);
334 EXPECT_TRUE(unknown_event.handled());
335 EXPECT_EQ(1, handler.num_key_events());
337 handler.Reset();
338 ui::KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
339 DispatchEventUsingWindowDispatcher(&known_event);
340 EXPECT_TRUE(known_event.handled());
341 EXPECT_EQ(1, handler.num_key_events());
343 handler.Reset();
344 ui::KeyEvent ime_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN,
345 ui::EF_IME_FABRICATED_KEY);
346 DispatchEventUsingWindowDispatcher(&ime_event);
347 EXPECT_TRUE(ime_event.handled());
348 EXPECT_EQ(1, handler.num_key_events());
350 handler.Reset();
351 ui::KeyEvent unknown_key_with_char_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN,
352 ui::EF_NONE);
353 unknown_key_with_char_event.set_character(0x00e4 /* "ä" */);
354 DispatchEventUsingWindowDispatcher(&unknown_key_with_char_event);
355 EXPECT_TRUE(unknown_key_with_char_event.handled());
356 EXPECT_EQ(1, handler.num_key_events());
359 TEST_F(WindowEventDispatcherTest, NoDelegateWindowReceivesKeyEvents) {
360 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
361 w1->Show();
362 w1->Focus();
364 ui::test::TestEventHandler handler;
365 w1->AddPreTargetHandler(&handler);
366 ui::KeyEvent key_press(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
367 DispatchEventUsingWindowDispatcher(&key_press);
368 EXPECT_TRUE(key_press.handled());
369 EXPECT_EQ(1, handler.num_key_events());
371 w1->RemovePreTargetHandler(&handler);
374 // Tests that touch-events that are beyond the bounds of the root-window do get
375 // propagated to the event filters correctly with the root as the target.
376 TEST_F(WindowEventDispatcherTest, TouchEventsOutsideBounds) {
377 ui::test::TestEventHandler handler;
378 root_window()->AddPreTargetHandler(&handler);
380 gfx::Point position = root_window()->bounds().origin();
381 position.Offset(-10, -10);
382 ui::TouchEvent press(
383 ui::ET_TOUCH_PRESSED, position, 0, ui::EventTimeForNow());
384 DispatchEventUsingWindowDispatcher(&press);
385 EXPECT_EQ(1, handler.num_touch_events());
387 position = root_window()->bounds().origin();
388 position.Offset(root_window()->bounds().width() + 10,
389 root_window()->bounds().height() + 10);
390 ui::TouchEvent release(
391 ui::ET_TOUCH_RELEASED, position, 0, ui::EventTimeForNow());
392 DispatchEventUsingWindowDispatcher(&release);
393 EXPECT_EQ(2, handler.num_touch_events());
396 // Tests that scroll events are dispatched correctly.
397 TEST_F(WindowEventDispatcherTest, ScrollEventDispatch) {
398 base::TimeDelta now = ui::EventTimeForNow();
399 ui::test::TestEventHandler handler;
400 root_window()->AddPreTargetHandler(&handler);
402 test::TestWindowDelegate delegate;
403 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
404 w1->SetBounds(gfx::Rect(20, 20, 40, 40));
406 // A scroll event on the root-window itself is dispatched.
407 ui::ScrollEvent scroll1(ui::ET_SCROLL,
408 gfx::Point(10, 10),
409 now,
411 0, -10,
412 0, -10,
414 DispatchEventUsingWindowDispatcher(&scroll1);
415 EXPECT_EQ(1, handler.num_scroll_events());
417 // Scroll event on a window should be dispatched properly.
418 ui::ScrollEvent scroll2(ui::ET_SCROLL,
419 gfx::Point(25, 30),
420 now,
422 -10, 0,
423 -10, 0,
425 DispatchEventUsingWindowDispatcher(&scroll2);
426 EXPECT_EQ(2, handler.num_scroll_events());
427 root_window()->RemovePreTargetHandler(&handler);
430 namespace {
432 // FilterFilter that tracks the types of events it's seen.
433 class EventFilterRecorder : public ui::EventHandler {
434 public:
435 typedef std::vector<ui::EventType> Events;
436 typedef std::vector<gfx::Point> EventLocations;
437 typedef std::vector<int> EventFlags;
439 EventFilterRecorder()
440 : wait_until_event_(ui::ET_UNKNOWN),
441 last_touch_may_cause_scrolling_(false) {
444 const Events& events() const { return events_; }
446 const EventLocations& mouse_locations() const { return mouse_locations_; }
447 gfx::Point mouse_location(int i) const { return mouse_locations_[i]; }
448 const EventLocations& touch_locations() const { return touch_locations_; }
449 const EventLocations& gesture_locations() const { return gesture_locations_; }
450 const EventFlags& mouse_event_flags() const { return mouse_event_flags_; }
452 void WaitUntilReceivedEvent(ui::EventType type) {
453 wait_until_event_ = type;
454 run_loop_.reset(new base::RunLoop());
455 run_loop_->Run();
458 Events GetAndResetEvents() {
459 Events events = events_;
460 Reset();
461 return events;
464 void Reset() {
465 events_.clear();
466 mouse_locations_.clear();
467 touch_locations_.clear();
468 gesture_locations_.clear();
469 mouse_event_flags_.clear();
470 last_touch_may_cause_scrolling_ = false;
473 // ui::EventHandler overrides:
474 void OnEvent(ui::Event* event) override {
475 ui::EventHandler::OnEvent(event);
476 events_.push_back(event->type());
477 if (wait_until_event_ == event->type() && run_loop_) {
478 run_loop_->Quit();
479 wait_until_event_ = ui::ET_UNKNOWN;
483 void OnMouseEvent(ui::MouseEvent* event) override {
484 mouse_locations_.push_back(event->location());
485 mouse_event_flags_.push_back(event->flags());
488 void OnTouchEvent(ui::TouchEvent* event) override {
489 touch_locations_.push_back(event->location());
490 last_touch_may_cause_scrolling_ = event->may_cause_scrolling();
493 void OnGestureEvent(ui::GestureEvent* event) override {
494 gesture_locations_.push_back(event->location());
497 bool HasReceivedEvent(ui::EventType type) {
498 return std::find(events_.begin(), events_.end(), type) != events_.end();
501 bool LastTouchMayCauseScrolling() const {
502 return last_touch_may_cause_scrolling_;
505 private:
506 scoped_ptr<base::RunLoop> run_loop_;
507 ui::EventType wait_until_event_;
509 Events events_;
510 EventLocations mouse_locations_;
511 EventLocations touch_locations_;
512 EventLocations gesture_locations_;
513 EventFlags mouse_event_flags_;
514 bool last_touch_may_cause_scrolling_;
516 DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder);
519 // Converts an EventType to a string.
520 std::string EventTypeToString(ui::EventType type) {
521 switch (type) {
522 case ui::ET_TOUCH_RELEASED:
523 return "TOUCH_RELEASED";
525 case ui::ET_TOUCH_CANCELLED:
526 return "TOUCH_CANCELLED";
528 case ui::ET_TOUCH_PRESSED:
529 return "TOUCH_PRESSED";
531 case ui::ET_TOUCH_MOVED:
532 return "TOUCH_MOVED";
534 case ui::ET_MOUSE_PRESSED:
535 return "MOUSE_PRESSED";
537 case ui::ET_MOUSE_DRAGGED:
538 return "MOUSE_DRAGGED";
540 case ui::ET_MOUSE_RELEASED:
541 return "MOUSE_RELEASED";
543 case ui::ET_MOUSE_MOVED:
544 return "MOUSE_MOVED";
546 case ui::ET_MOUSE_ENTERED:
547 return "MOUSE_ENTERED";
549 case ui::ET_MOUSE_EXITED:
550 return "MOUSE_EXITED";
552 case ui::ET_GESTURE_SCROLL_BEGIN:
553 return "GESTURE_SCROLL_BEGIN";
555 case ui::ET_GESTURE_SCROLL_END:
556 return "GESTURE_SCROLL_END";
558 case ui::ET_GESTURE_SCROLL_UPDATE:
559 return "GESTURE_SCROLL_UPDATE";
561 case ui::ET_GESTURE_PINCH_BEGIN:
562 return "GESTURE_PINCH_BEGIN";
564 case ui::ET_GESTURE_PINCH_END:
565 return "GESTURE_PINCH_END";
567 case ui::ET_GESTURE_PINCH_UPDATE:
568 return "GESTURE_PINCH_UPDATE";
570 case ui::ET_GESTURE_TAP:
571 return "GESTURE_TAP";
573 case ui::ET_GESTURE_TAP_DOWN:
574 return "GESTURE_TAP_DOWN";
576 case ui::ET_GESTURE_TAP_CANCEL:
577 return "GESTURE_TAP_CANCEL";
579 case ui::ET_GESTURE_SHOW_PRESS:
580 return "GESTURE_SHOW_PRESS";
582 case ui::ET_GESTURE_BEGIN:
583 return "GESTURE_BEGIN";
585 case ui::ET_GESTURE_END:
586 return "GESTURE_END";
588 default:
589 // We should explicitly require each event type.
590 NOTREACHED() << "Received unexpected event: " << type;
591 break;
593 return "";
596 std::string EventTypesToString(const EventFilterRecorder::Events& events) {
597 std::string result;
598 for (size_t i = 0; i < events.size(); ++i) {
599 if (i != 0)
600 result += " ";
601 result += EventTypeToString(events[i]);
603 return result;
606 } // namespace
608 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
609 #define MAYBE(x) DISABLED_##x
610 #else
611 #define MAYBE(x) x
612 #endif
614 // Verifies a repost mouse event targets the window with capture (if there is
615 // one).
616 // Flaky on 32-bit Windows bots. http://crbug.com/388290
617 TEST_F(WindowEventDispatcherTest, MAYBE(RepostTargetsCaptureWindow)) {
618 // Set capture on |window| generate a mouse event (that is reposted) and not
619 // over |window| and verify |window| gets it (|window| gets it because it has
620 // capture).
621 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
622 EventFilterRecorder recorder;
623 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
624 window->SetBounds(gfx::Rect(20, 20, 40, 30));
625 window->AddPreTargetHandler(&recorder);
626 window->SetCapture();
627 const ui::MouseEvent press_event(
628 ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
629 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
630 host()->dispatcher()->RepostEvent(press_event);
631 RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
632 // Mouse moves/enters may be generated. We only care about a pressed.
633 EXPECT_TRUE(EventTypesToString(recorder.events()).find("MOUSE_PRESSED") !=
634 std::string::npos) << EventTypesToString(recorder.events());
637 TEST_F(WindowEventDispatcherTest, MouseMovesHeld) {
638 EventFilterRecorder recorder;
639 root_window()->AddPreTargetHandler(&recorder);
641 test::TestWindowDelegate delegate;
642 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
643 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
645 ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0),
646 gfx::Point(0, 0), ui::EventTimeForNow(), 0,
648 DispatchEventUsingWindowDispatcher(&mouse_move_event);
649 // Discard MOUSE_ENTER.
650 recorder.Reset();
652 host()->dispatcher()->HoldPointerMoves();
654 // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
655 ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
656 gfx::Point(0, 0), ui::EventTimeForNow(), 0,
658 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
659 EXPECT_TRUE(recorder.events().empty());
661 // Check that we do dispatch the held MOUSE_DRAGGED event before another type
662 // of event.
663 ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
664 gfx::Point(0, 0), ui::EventTimeForNow(), 0,
666 DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
667 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
668 EventTypesToString(recorder.events()));
669 recorder.Reset();
671 // Check that we coalesce held MOUSE_DRAGGED events. Note that here (and
672 // elsewhere in this test) we re-define each event prior to dispatch so that
673 // it has the correct state (phase, handled, target, etc.).
674 mouse_dragged_event =
675 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
676 ui::EventTimeForNow(), 0, 0);
677 ui::MouseEvent mouse_dragged_event2(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
678 gfx::Point(10, 10), ui::EventTimeForNow(),
679 0, 0);
680 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
681 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
682 EXPECT_TRUE(recorder.events().empty());
683 mouse_pressed_event =
684 ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
685 ui::EventTimeForNow(), 0, 0);
686 DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
687 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
688 EventTypesToString(recorder.events()));
689 recorder.Reset();
691 // Check that on ReleasePointerMoves, held events are not dispatched
692 // immediately, but posted instead.
693 mouse_dragged_event =
694 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
695 ui::EventTimeForNow(), 0, 0);
696 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
697 host()->dispatcher()->ReleasePointerMoves();
698 EXPECT_TRUE(recorder.events().empty());
699 RunAllPendingInMessageLoop();
700 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
701 recorder.Reset();
703 // However if another message comes in before the dispatch of the posted
704 // event, check that the posted event is dispatched before this new event.
705 host()->dispatcher()->HoldPointerMoves();
706 mouse_dragged_event =
707 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
708 ui::EventTimeForNow(), 0, 0);
709 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
710 host()->dispatcher()->ReleasePointerMoves();
711 mouse_pressed_event =
712 ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
713 ui::EventTimeForNow(), 0, 0);
714 DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
715 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
716 EventTypesToString(recorder.events()));
717 recorder.Reset();
718 RunAllPendingInMessageLoop();
719 EXPECT_TRUE(recorder.events().empty());
721 // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
722 // them.
723 host()->dispatcher()->HoldPointerMoves();
724 mouse_dragged_event =
725 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
726 ui::EventTimeForNow(), 0, 0);
727 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
728 host()->dispatcher()->ReleasePointerMoves();
729 mouse_dragged_event2 =
730 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
731 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
732 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
733 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
734 recorder.Reset();
735 RunAllPendingInMessageLoop();
736 EXPECT_TRUE(recorder.events().empty());
738 // Check that synthetic mouse move event has a right location when issued
739 // while holding pointer moves.
740 mouse_dragged_event =
741 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
742 ui::EventTimeForNow(), 0, 0);
743 mouse_dragged_event2 =
744 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
745 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
746 ui::MouseEvent mouse_dragged_event3(ui::ET_MOUSE_DRAGGED, gfx::Point(28, 28),
747 gfx::Point(28, 28), ui::EventTimeForNow(),
748 0, 0);
749 host()->dispatcher()->HoldPointerMoves();
750 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
751 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
752 window->SetBounds(gfx::Rect(15, 15, 80, 80));
753 DispatchEventUsingWindowDispatcher(&mouse_dragged_event3);
754 RunAllPendingInMessageLoop();
755 EXPECT_TRUE(recorder.events().empty());
756 host()->dispatcher()->ReleasePointerMoves();
757 RunAllPendingInMessageLoop();
758 EXPECT_EQ("MOUSE_MOVED", EventTypesToString(recorder.events()));
759 EXPECT_EQ(gfx::Point(13, 13), recorder.mouse_location(0));
760 recorder.Reset();
761 root_window()->RemovePreTargetHandler(&recorder);
764 TEST_F(WindowEventDispatcherTest, TouchMovesHeld) {
765 EventFilterRecorder recorder;
766 root_window()->AddPreTargetHandler(&recorder);
768 test::TestWindowDelegate delegate;
769 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
770 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
772 // Starting the touch and throwing out the first few events, since the system
773 // is going to generate synthetic mouse events that are not relevant to the
774 // test.
775 ui::TouchEvent touch_pressed_event(
776 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, ui::EventTimeForNow());
777 DispatchEventUsingWindowDispatcher(&touch_pressed_event);
778 recorder.WaitUntilReceivedEvent(ui::ET_GESTURE_SHOW_PRESS);
779 recorder.Reset();
781 host()->dispatcher()->HoldPointerMoves();
783 // Check that we don't immediately dispatch the TOUCH_MOVED event.
784 ui::TouchEvent touch_moved_event(
785 ui::ET_TOUCH_MOVED, gfx::Point(10, 10), 0, ui::EventTimeForNow());
786 ui::TouchEvent touch_moved_event2(
787 ui::ET_TOUCH_MOVED, gfx::Point(11, 10), 0, ui::EventTimeForNow());
788 ui::TouchEvent touch_moved_event3(
789 ui::ET_TOUCH_MOVED, gfx::Point(12, 10), 0, ui::EventTimeForNow());
791 DispatchEventUsingWindowDispatcher(&touch_moved_event);
792 EXPECT_TRUE(recorder.events().empty());
794 // Check that on ReleasePointerMoves, held events are not dispatched
795 // immediately, but posted instead.
796 DispatchEventUsingWindowDispatcher(&touch_moved_event2);
797 host()->dispatcher()->ReleasePointerMoves();
798 EXPECT_TRUE(recorder.events().empty());
800 RunAllPendingInMessageLoop();
801 EXPECT_EQ("TOUCH_MOVED", EventTypesToString(recorder.events()));
802 recorder.Reset();
804 // If another touch event occurs then the held touch should be dispatched
805 // immediately before it.
806 ui::TouchEvent touch_released_event(
807 ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), 0, ui::EventTimeForNow());
808 recorder.Reset();
809 host()->dispatcher()->HoldPointerMoves();
810 DispatchEventUsingWindowDispatcher(&touch_moved_event3);
811 DispatchEventUsingWindowDispatcher(&touch_released_event);
812 EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP GESTURE_END",
813 EventTypesToString(recorder.events()));
814 recorder.Reset();
815 host()->dispatcher()->ReleasePointerMoves();
816 RunAllPendingInMessageLoop();
817 EXPECT_TRUE(recorder.events().empty());
820 // Tests that mouse move event has a right location
821 // when there isn't the target window
822 TEST_F(WindowEventDispatcherTest, MouseEventWithoutTargetWindow) {
823 EventFilterRecorder recorder_first;
824 EventFilterRecorder recorder_second;
826 test::TestWindowDelegate delegate;
827 scoped_ptr<aura::Window> window_first(CreateTestWindowWithDelegate(
828 &delegate, 1, gfx::Rect(20, 10, 10, 20), root_window()));
829 window_first->Show();
830 window_first->AddPreTargetHandler(&recorder_first);
832 scoped_ptr<aura::Window> window_second(CreateTestWindowWithDelegate(
833 &delegate, 2, gfx::Rect(20, 30, 10, 20), root_window()));
834 window_second->Show();
835 window_second->AddPreTargetHandler(&recorder_second);
837 const gfx::Point event_location(22, 33);
838 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
839 ui::EventTimeForNow(), 0, 0);
840 DispatchEventUsingWindowDispatcher(&mouse);
842 EXPECT_TRUE(recorder_first.events().empty());
843 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED",
844 EventTypesToString(recorder_second.events()));
845 ASSERT_EQ(2u, recorder_second.mouse_locations().size());
846 EXPECT_EQ(gfx::Point(2, 3).ToString(),
847 recorder_second.mouse_locations()[0].ToString());
850 // Verifies that a direct call to ProcessedTouchEvent() does not cause a crash.
851 TEST_F(WindowEventDispatcherTest, CallToProcessedTouchEvent) {
852 test::TestWindowDelegate delegate;
853 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
854 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
856 host()->dispatcher()->ProcessedTouchEvent(0, window.get(), ui::ER_UNHANDLED);
859 // This event handler requests the dispatcher to start holding pointer-move
860 // events when it receives the first scroll-update gesture.
861 class HoldPointerOnScrollHandler : public ui::test::TestEventHandler {
862 public:
863 HoldPointerOnScrollHandler(WindowEventDispatcher* dispatcher,
864 EventFilterRecorder* filter)
865 : dispatcher_(dispatcher),
866 filter_(filter),
867 holding_moves_(false) {}
868 ~HoldPointerOnScrollHandler() override {}
870 private:
871 // ui::test::TestEventHandler:
872 void OnGestureEvent(ui::GestureEvent* gesture) override {
873 if (!holding_moves_ && gesture->type() == ui::ET_GESTURE_SCROLL_UPDATE) {
874 holding_moves_ = true;
875 dispatcher_->HoldPointerMoves();
876 filter_->Reset();
877 } else if (gesture->type() == ui::ET_GESTURE_SCROLL_END) {
878 dispatcher_->ReleasePointerMoves();
879 holding_moves_ = false;
883 WindowEventDispatcher* dispatcher_;
884 EventFilterRecorder* filter_;
885 bool holding_moves_;
887 DISALLOW_COPY_AND_ASSIGN(HoldPointerOnScrollHandler);
890 // Tests that touch-move events don't contribute to an in-progress scroll
891 // gesture if touch-move events are being held by the dispatcher.
892 TEST_F(WindowEventDispatcherTest, TouchMovesHeldOnScroll) {
893 EventFilterRecorder recorder;
894 root_window()->AddPreTargetHandler(&recorder);
895 test::TestWindowDelegate delegate;
896 HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
897 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
898 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
899 window->AddPreTargetHandler(&handler);
901 ui::test::EventGenerator generator(root_window());
902 generator.GestureScrollSequence(
903 gfx::Point(60, 60), gfx::Point(10, 60),
904 base::TimeDelta::FromMilliseconds(100), 25);
906 // |handler| will have reset |filter| and started holding the touch-move
907 // events when scrolling started. At the end of the scroll (i.e. upon
908 // touch-release), the held touch-move event will have been dispatched first,
909 // along with the subsequent events (i.e. touch-release, scroll-end, and
910 // gesture-end).
911 const EventFilterRecorder::Events& events = recorder.events();
912 EXPECT_EQ(
913 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
914 "GESTURE_SCROLL_END GESTURE_END",
915 EventTypesToString(events));
916 ASSERT_EQ(2u, recorder.touch_locations().size());
917 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
918 recorder.touch_locations()[0].ToString());
919 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
920 recorder.touch_locations()[1].ToString());
923 // Tests that a 'held' touch-event does contribute to gesture event when it is
924 // dispatched.
925 TEST_F(WindowEventDispatcherTest, HeldTouchMoveContributesToGesture) {
926 EventFilterRecorder recorder;
927 root_window()->AddPreTargetHandler(&recorder);
929 const gfx::Point location(20, 20);
930 ui::TouchEvent press(
931 ui::ET_TOUCH_PRESSED, location, 0, ui::EventTimeForNow());
932 DispatchEventUsingWindowDispatcher(&press);
933 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_PRESSED));
934 recorder.Reset();
936 host()->dispatcher()->HoldPointerMoves();
938 ui::TouchEvent move(ui::ET_TOUCH_MOVED,
939 location + gfx::Vector2d(100, 100),
941 ui::EventTimeForNow());
942 DispatchEventUsingWindowDispatcher(&move);
943 EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
944 EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
945 recorder.Reset();
947 host()->dispatcher()->ReleasePointerMoves();
948 EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
949 RunAllPendingInMessageLoop();
950 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
951 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
952 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
954 root_window()->RemovePreTargetHandler(&recorder);
957 // Tests that synthetic mouse events are ignored when mouse
958 // events are disabled.
959 TEST_F(WindowEventDispatcherTest, DispatchSyntheticMouseEvents) {
960 EventFilterRecorder recorder;
961 root_window()->AddPreTargetHandler(&recorder);
963 test::TestWindowDelegate delegate;
964 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
965 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
966 window->Show();
967 window->SetCapture();
969 test::TestCursorClient cursor_client(root_window());
971 // Dispatch a non-synthetic mouse event when mouse events are enabled.
972 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
973 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
974 DispatchEventUsingWindowDispatcher(&mouse1);
975 EXPECT_FALSE(recorder.events().empty());
976 recorder.Reset();
978 // Dispatch a synthetic mouse event when mouse events are enabled.
979 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
980 gfx::Point(10, 10), ui::EventTimeForNow(),
981 ui::EF_IS_SYNTHESIZED, 0);
982 DispatchEventUsingWindowDispatcher(&mouse2);
983 EXPECT_FALSE(recorder.events().empty());
984 recorder.Reset();
986 // Dispatch a synthetic mouse event when mouse events are disabled.
987 cursor_client.DisableMouseEvents();
988 DispatchEventUsingWindowDispatcher(&mouse2);
989 EXPECT_TRUE(recorder.events().empty());
990 root_window()->RemovePreTargetHandler(&recorder);
993 // Tests that a mouse-move event is not synthesized when a mouse-button is down.
994 TEST_F(WindowEventDispatcherTest, DoNotSynthesizeWhileButtonDown) {
995 EventFilterRecorder recorder;
996 test::TestWindowDelegate delegate;
997 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
998 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
999 window->Show();
1001 window->AddPreTargetHandler(&recorder);
1002 // Dispatch a non-synthetic mouse event when mouse events are enabled.
1003 ui::MouseEvent mouse1(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1004 gfx::Point(10, 10), ui::EventTimeForNow(),
1005 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1006 DispatchEventUsingWindowDispatcher(&mouse1);
1007 ASSERT_EQ(1u, recorder.events().size());
1008 EXPECT_EQ(ui::ET_MOUSE_PRESSED, recorder.events()[0]);
1009 window->RemovePreTargetHandler(&recorder);
1010 recorder.Reset();
1012 // Move |window| away from underneath the cursor.
1013 root_window()->AddPreTargetHandler(&recorder);
1014 window->SetBounds(gfx::Rect(30, 30, 100, 100));
1015 EXPECT_TRUE(recorder.events().empty());
1016 RunAllPendingInMessageLoop();
1017 EXPECT_TRUE(recorder.events().empty());
1018 root_window()->RemovePreTargetHandler(&recorder);
1021 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
1022 #define MAYBE(x) DISABLED_##x
1023 #else
1024 #define MAYBE(x) x
1025 #endif
1027 // Tests synthetic mouse events generated when window bounds changes such that
1028 // the cursor previously outside the window becomes inside, or vice versa.
1029 // Do not synthesize events if the window ignores events or is invisible.
1030 // Flaky on 32-bit Windows bots. http://crbug.com/388272
1031 TEST_F(WindowEventDispatcherTest,
1032 MAYBE(SynthesizeMouseEventsOnWindowBoundsChanged)) {
1033 test::TestWindowDelegate delegate;
1034 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1035 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1036 window->Show();
1037 window->SetCapture();
1039 EventFilterRecorder recorder;
1040 window->AddPreTargetHandler(&recorder);
1042 // Dispatch a non-synthetic mouse event to place cursor inside window bounds.
1043 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
1044 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
1045 DispatchEventUsingWindowDispatcher(&mouse);
1046 EXPECT_FALSE(recorder.events().empty());
1047 recorder.Reset();
1049 // Update the window bounds so that cursor is now outside the window.
1050 // This should trigger a synthetic MOVED event.
1051 gfx::Rect bounds1(20, 20, 100, 100);
1052 window->SetBounds(bounds1);
1053 RunAllPendingInMessageLoop();
1054 ASSERT_FALSE(recorder.events().empty());
1055 ASSERT_FALSE(recorder.mouse_event_flags().empty());
1056 EXPECT_EQ(ui::ET_MOUSE_MOVED, recorder.events().back());
1057 EXPECT_EQ(ui::EF_IS_SYNTHESIZED, recorder.mouse_event_flags().back());
1058 recorder.Reset();
1060 // Set window to ignore events.
1061 window->set_ignore_events(true);
1063 // Update the window bounds so that cursor is back inside the window.
1064 // This should not trigger a synthetic event.
1065 gfx::Rect bounds2(5, 5, 100, 100);
1066 window->SetBounds(bounds2);
1067 RunAllPendingInMessageLoop();
1068 EXPECT_TRUE(recorder.events().empty());
1069 recorder.Reset();
1071 // Set window to accept events but invisible.
1072 window->set_ignore_events(false);
1073 window->Hide();
1074 recorder.Reset();
1076 // Update the window bounds so that cursor is outside the window.
1077 // This should not trigger a synthetic event.
1078 window->SetBounds(bounds1);
1079 RunAllPendingInMessageLoop();
1080 EXPECT_TRUE(recorder.events().empty());
1083 // Tests that a mouse exit is dispatched to the last known cursor location
1084 // when the cursor becomes invisible.
1085 TEST_F(WindowEventDispatcherTest, DispatchMouseExitWhenCursorHidden) {
1086 EventFilterRecorder recorder;
1087 root_window()->AddPreTargetHandler(&recorder);
1089 test::TestWindowDelegate delegate;
1090 gfx::Point window_origin(7, 18);
1091 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1092 &delegate, 1234, gfx::Rect(window_origin, gfx::Size(100, 100)),
1093 root_window()));
1094 window->Show();
1096 // Dispatch a mouse move event into the window.
1097 gfx::Point mouse_location(gfx::Point(15, 25));
1098 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location, mouse_location,
1099 ui::EventTimeForNow(), 0, 0);
1100 EXPECT_TRUE(recorder.events().empty());
1101 DispatchEventUsingWindowDispatcher(&mouse1);
1102 EXPECT_FALSE(recorder.events().empty());
1103 recorder.Reset();
1105 // Hide the cursor and verify a mouse exit was dispatched.
1106 host()->OnCursorVisibilityChanged(false);
1107 EXPECT_FALSE(recorder.events().empty());
1108 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder.events()));
1110 // Verify the mouse exit was dispatched at the correct location
1111 // (in the correct coordinate space).
1112 int translated_x = mouse_location.x() - window_origin.x();
1113 int translated_y = mouse_location.y() - window_origin.y();
1114 gfx::Point translated_point(translated_x, translated_y);
1115 EXPECT_EQ(recorder.mouse_location(0).ToString(), translated_point.ToString());
1116 root_window()->RemovePreTargetHandler(&recorder);
1119 // Tests that a synthetic mouse exit is dispatched to the last known cursor
1120 // location after mouse events are disabled on the cursor client.
1121 TEST_F(WindowEventDispatcherTest,
1122 DispatchSyntheticMouseExitAfterMouseEventsDisabled) {
1123 EventFilterRecorder recorder;
1124 root_window()->AddPreTargetHandler(&recorder);
1126 test::TestWindowDelegate delegate;
1127 gfx::Point window_origin(7, 18);
1128 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1129 &delegate, 1234, gfx::Rect(window_origin, gfx::Size(100, 100)),
1130 root_window()));
1131 window->Show();
1133 // Dispatch a mouse move event into the window.
1134 gfx::Point mouse_location(gfx::Point(15, 25));
1135 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location, mouse_location,
1136 ui::EventTimeForNow(), 0, 0);
1137 EXPECT_TRUE(recorder.events().empty());
1138 DispatchEventUsingWindowDispatcher(&mouse1);
1139 EXPECT_FALSE(recorder.events().empty());
1140 recorder.Reset();
1142 test::TestCursorClient cursor_client(root_window());
1143 cursor_client.DisableMouseEvents();
1145 gfx::Point mouse_exit_location(gfx::Point(150, 150));
1146 ui::MouseEvent mouse2(ui::ET_MOUSE_EXITED, gfx::Point(150, 150),
1147 gfx::Point(150, 150), ui::EventTimeForNow(),
1148 ui::EF_IS_SYNTHESIZED, 0);
1149 DispatchEventUsingWindowDispatcher(&mouse2);
1151 EXPECT_FALSE(recorder.events().empty());
1152 // We get the mouse exited event twice in our filter. Once during the
1153 // predispatch phase and during the actual dispatch.
1154 EXPECT_EQ("MOUSE_EXITED MOUSE_EXITED", EventTypesToString(recorder.events()));
1156 // Verify the mouse exit was dispatched at the correct location
1157 // (in the correct coordinate space).
1158 int translated_x = mouse_exit_location.x() - window_origin.x();
1159 int translated_y = mouse_exit_location.y() - window_origin.y();
1160 gfx::Point translated_point(translated_x, translated_y);
1161 EXPECT_EQ(recorder.mouse_location(0).ToString(), translated_point.ToString());
1162 root_window()->RemovePreTargetHandler(&recorder);
1165 class DeletingEventFilter : public ui::EventHandler {
1166 public:
1167 DeletingEventFilter()
1168 : delete_during_pre_handle_(false) {}
1169 ~DeletingEventFilter() override {}
1171 void Reset(bool delete_during_pre_handle) {
1172 delete_during_pre_handle_ = delete_during_pre_handle;
1175 private:
1176 // Overridden from ui::EventHandler:
1177 void OnKeyEvent(ui::KeyEvent* event) override {
1178 if (delete_during_pre_handle_)
1179 delete event->target();
1182 void OnMouseEvent(ui::MouseEvent* event) override {
1183 if (delete_during_pre_handle_)
1184 delete event->target();
1187 bool delete_during_pre_handle_;
1189 DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter);
1192 class DeletingWindowDelegate : public test::TestWindowDelegate {
1193 public:
1194 DeletingWindowDelegate()
1195 : window_(NULL),
1196 delete_during_handle_(false),
1197 got_event_(false) {}
1198 ~DeletingWindowDelegate() override {}
1200 void Reset(Window* window, bool delete_during_handle) {
1201 window_ = window;
1202 delete_during_handle_ = delete_during_handle;
1203 got_event_ = false;
1205 bool got_event() const { return got_event_; }
1207 private:
1208 // Overridden from WindowDelegate:
1209 void OnKeyEvent(ui::KeyEvent* event) override {
1210 if (delete_during_handle_)
1211 delete window_;
1212 got_event_ = true;
1215 void OnMouseEvent(ui::MouseEvent* event) override {
1216 if (delete_during_handle_)
1217 delete window_;
1218 got_event_ = true;
1221 Window* window_;
1222 bool delete_during_handle_;
1223 bool got_event_;
1225 DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate);
1228 TEST_F(WindowEventDispatcherTest, DeleteWindowDuringDispatch) {
1229 // Verifies that we can delete a window during each phase of event handling.
1230 // Deleting the window should not cause a crash, only prevent further
1231 // processing from occurring.
1232 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1233 DeletingWindowDelegate d11;
1234 Window* w11 = CreateNormalWindow(11, w1.get(), &d11);
1235 WindowTracker tracker;
1236 DeletingEventFilter w1_filter;
1237 w1->AddPreTargetHandler(&w1_filter);
1238 client::GetFocusClient(w1.get())->FocusWindow(w11);
1240 ui::test::EventGenerator generator(root_window(), w11);
1242 // First up, no one deletes anything.
1243 tracker.Add(w11);
1244 d11.Reset(w11, false);
1246 generator.PressLeftButton();
1247 EXPECT_TRUE(tracker.Contains(w11));
1248 EXPECT_TRUE(d11.got_event());
1249 generator.ReleaseLeftButton();
1251 // Delegate deletes w11. This will prevent the post-handle step from applying.
1252 w1_filter.Reset(false);
1253 d11.Reset(w11, true);
1254 generator.PressKey(ui::VKEY_A, 0);
1255 EXPECT_FALSE(tracker.Contains(w11));
1256 EXPECT_TRUE(d11.got_event());
1258 // Pre-handle step deletes w11. This will prevent the delegate and the post-
1259 // handle steps from applying.
1260 w11 = CreateNormalWindow(11, w1.get(), &d11);
1261 w1_filter.Reset(true);
1262 d11.Reset(w11, false);
1263 generator.PressLeftButton();
1264 EXPECT_FALSE(tracker.Contains(w11));
1265 EXPECT_FALSE(d11.got_event());
1268 namespace {
1270 // A window delegate that detaches the parent of the target's parent window when
1271 // it receives a tap event.
1272 class DetachesParentOnTapDelegate : public test::TestWindowDelegate {
1273 public:
1274 DetachesParentOnTapDelegate() {}
1275 ~DetachesParentOnTapDelegate() override {}
1277 private:
1278 void OnGestureEvent(ui::GestureEvent* event) override {
1279 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
1280 event->SetHandled();
1281 return;
1284 if (event->type() == ui::ET_GESTURE_TAP) {
1285 Window* parent = static_cast<Window*>(event->target())->parent();
1286 parent->parent()->RemoveChild(parent);
1287 event->SetHandled();
1291 DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate);
1294 } // namespace
1296 // Tests that the gesture recognizer is reset for all child windows when a
1297 // window hides. No expectations, just checks that the test does not crash.
1298 TEST_F(WindowEventDispatcherTest,
1299 GestureRecognizerResetsTargetWhenParentHides) {
1300 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1301 DetachesParentOnTapDelegate delegate;
1302 scoped_ptr<Window> parent(CreateNormalWindow(22, w1.get(), NULL));
1303 Window* child = CreateNormalWindow(11, parent.get(), &delegate);
1304 ui::test::EventGenerator generator(root_window(), child);
1305 generator.GestureTapAt(gfx::Point(40, 40));
1308 namespace {
1310 // A window delegate that processes nested gestures on tap.
1311 class NestedGestureDelegate : public test::TestWindowDelegate {
1312 public:
1313 NestedGestureDelegate(ui::test::EventGenerator* generator,
1314 const gfx::Point tap_location)
1315 : generator_(generator),
1316 tap_location_(tap_location),
1317 gesture_end_count_(0) {}
1318 ~NestedGestureDelegate() override {}
1320 int gesture_end_count() const { return gesture_end_count_; }
1322 private:
1323 void OnGestureEvent(ui::GestureEvent* event) override {
1324 switch (event->type()) {
1325 case ui::ET_GESTURE_TAP_DOWN:
1326 event->SetHandled();
1327 break;
1328 case ui::ET_GESTURE_TAP:
1329 if (generator_)
1330 generator_->GestureTapAt(tap_location_);
1331 event->SetHandled();
1332 break;
1333 case ui::ET_GESTURE_END:
1334 ++gesture_end_count_;
1335 break;
1336 default:
1337 break;
1341 ui::test::EventGenerator* generator_;
1342 const gfx::Point tap_location_;
1343 int gesture_end_count_;
1344 DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate);
1347 } // namespace
1349 // Tests that gesture end is delivered after nested gesture processing.
1350 TEST_F(WindowEventDispatcherTest, GestureEndDeliveredAfterNestedGestures) {
1351 NestedGestureDelegate d1(NULL, gfx::Point());
1352 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &d1));
1353 w1->SetBounds(gfx::Rect(0, 0, 100, 100));
1355 ui::test::EventGenerator nested_generator(root_window(), w1.get());
1356 NestedGestureDelegate d2(&nested_generator, w1->bounds().CenterPoint());
1357 scoped_ptr<Window> w2(CreateNormalWindow(1, root_window(), &d2));
1358 w2->SetBounds(gfx::Rect(100, 0, 100, 100));
1360 // Tap on w2 which triggers nested gestures for w1.
1361 ui::test::EventGenerator generator(root_window(), w2.get());
1362 generator.GestureTapAt(w2->bounds().CenterPoint());
1364 // Both windows should get their gesture end events.
1365 EXPECT_EQ(1, d1.gesture_end_count());
1366 EXPECT_EQ(1, d2.gesture_end_count());
1369 // Tests whether we can repost the Tap down gesture event.
1370 TEST_F(WindowEventDispatcherTest, RepostTapdownGestureTest) {
1371 EventFilterRecorder recorder;
1372 root_window()->AddPreTargetHandler(&recorder);
1374 test::TestWindowDelegate delegate;
1375 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1376 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1378 ui::GestureEventDetails details(ui::ET_GESTURE_TAP_DOWN);
1379 gfx::Point point(10, 10);
1380 ui::GestureEvent event(point.x(),
1381 point.y(),
1383 ui::EventTimeForNow(),
1384 details);
1385 host()->dispatcher()->RepostEvent(event);
1386 RunAllPendingInMessageLoop();
1387 // TODO(rbyers): Currently disabled - crbug.com/170987
1388 EXPECT_FALSE(EventTypesToString(recorder.events()).find("GESTURE_TAP_DOWN") !=
1389 std::string::npos);
1390 recorder.Reset();
1391 root_window()->RemovePreTargetHandler(&recorder);
1394 // This class inherits from the EventFilterRecorder class which provides a
1395 // facility to record events. This class additionally provides a facility to
1396 // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
1397 // events after that.
1398 class RepostGestureEventRecorder : public EventFilterRecorder {
1399 public:
1400 RepostGestureEventRecorder(aura::Window* repost_source,
1401 aura::Window* repost_target)
1402 : repost_source_(repost_source),
1403 repost_target_(repost_target),
1404 reposted_(false),
1405 done_cleanup_(false) {}
1407 ~RepostGestureEventRecorder() override {}
1409 void OnTouchEvent(ui::TouchEvent* event) override {
1410 if (reposted_ && event->type() == ui::ET_TOUCH_PRESSED) {
1411 done_cleanup_ = true;
1412 Reset();
1414 EventFilterRecorder::OnTouchEvent(event);
1417 void OnGestureEvent(ui::GestureEvent* event) override {
1418 EXPECT_EQ(done_cleanup_ ? repost_target_ : repost_source_, event->target());
1419 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
1420 if (!reposted_) {
1421 EXPECT_NE(repost_target_, event->target());
1422 reposted_ = true;
1423 repost_target_->GetHost()->dispatcher()->RepostEvent(*event);
1424 // Ensure that the reposted gesture event above goes to the
1425 // repost_target_;
1426 repost_source_->GetRootWindow()->RemoveChild(repost_source_);
1427 return;
1430 EventFilterRecorder::OnGestureEvent(event);
1433 // Ignore mouse events as they don't fire at all times. This causes
1434 // the GestureRepostEventOrder test to fail randomly.
1435 void OnMouseEvent(ui::MouseEvent* event) override {}
1437 private:
1438 aura::Window* repost_source_;
1439 aura::Window* repost_target_;
1440 // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
1441 bool reposted_;
1442 // set true if we're done cleaning up after hiding repost_source_;
1443 bool done_cleanup_;
1444 DISALLOW_COPY_AND_ASSIGN(RepostGestureEventRecorder);
1447 // Tests whether events which are generated after the reposted gesture event
1448 // are received after that. In this case the scroll sequence events should
1449 // be received after the reposted gesture event.
1450 TEST_F(WindowEventDispatcherTest, GestureRepostEventOrder) {
1451 // Expected events at the end for the repost_target window defined below.
1452 const char kExpectedTargetEvents[] =
1453 // TODO)(rbyers): Gesture event reposting is disabled - crbug.com/279039.
1454 // "GESTURE_BEGIN GESTURE_TAP_DOWN "
1455 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1456 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE TOUCH_MOVED "
1457 "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
1458 "GESTURE_SCROLL_END GESTURE_END";
1459 // We create two windows.
1460 // The first window (repost_source) is the one to which the initial tap
1461 // gesture is sent. It reposts this event to the second window
1462 // (repost_target).
1463 // We then generate the scroll sequence for repost_target and look for two
1464 // ET_GESTURE_TAP_DOWN events in the event list at the end.
1465 test::TestWindowDelegate delegate;
1466 scoped_ptr<aura::Window> repost_target(CreateTestWindowWithDelegate(
1467 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1469 scoped_ptr<aura::Window> repost_source(CreateTestWindowWithDelegate(
1470 &delegate, 1, gfx::Rect(0, 0, 50, 50), root_window()));
1472 RepostGestureEventRecorder repost_event_recorder(repost_source.get(),
1473 repost_target.get());
1474 root_window()->AddPreTargetHandler(&repost_event_recorder);
1476 // Generate a tap down gesture for the repost_source. This will be reposted
1477 // to repost_target.
1478 ui::test::EventGenerator repost_generator(root_window(), repost_source.get());
1479 repost_generator.GestureTapAt(gfx::Point(40, 40));
1480 RunAllPendingInMessageLoop();
1482 ui::test::EventGenerator scroll_generator(root_window(), repost_target.get());
1483 scroll_generator.GestureScrollSequence(
1484 gfx::Point(80, 80),
1485 gfx::Point(100, 100),
1486 base::TimeDelta::FromMilliseconds(100),
1488 RunAllPendingInMessageLoop();
1490 int tap_down_count = 0;
1491 for (size_t i = 0; i < repost_event_recorder.events().size(); ++i) {
1492 if (repost_event_recorder.events()[i] == ui::ET_GESTURE_TAP_DOWN)
1493 ++tap_down_count;
1496 // We expect two tap down events. One from the repost and the other one from
1497 // the scroll sequence posted above.
1498 // TODO(rbyers): Currently disabled - crbug.com/170987
1499 EXPECT_EQ(1, tap_down_count);
1501 EXPECT_EQ(kExpectedTargetEvents,
1502 EventTypesToString(repost_event_recorder.events()));
1503 root_window()->RemovePreTargetHandler(&repost_event_recorder);
1506 class OnMouseExitDeletingEventFilter : public EventFilterRecorder {
1507 public:
1508 OnMouseExitDeletingEventFilter() : window_to_delete_(NULL) {}
1509 ~OnMouseExitDeletingEventFilter() override {}
1511 void set_window_to_delete(Window* window_to_delete) {
1512 window_to_delete_ = window_to_delete;
1515 private:
1516 // Overridden from ui::EventHandler:
1517 void OnMouseEvent(ui::MouseEvent* event) override {
1518 EventFilterRecorder::OnMouseEvent(event);
1519 if (window_to_delete_) {
1520 delete window_to_delete_;
1521 window_to_delete_ = NULL;
1525 Window* window_to_delete_;
1527 DISALLOW_COPY_AND_ASSIGN(OnMouseExitDeletingEventFilter);
1530 // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
1531 // a child, but the child is destroyed because of the synthesized mouse-exit
1532 // event generated on the previous mouse_moved_handler_.
1533 TEST_F(WindowEventDispatcherTest, DeleteWindowDuringMouseMovedDispatch) {
1534 // Create window 1 and set its event filter. Window 1 will take ownership of
1535 // the event filter.
1536 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1537 OnMouseExitDeletingEventFilter w1_filter;
1538 w1->AddPreTargetHandler(&w1_filter);
1539 w1->SetBounds(gfx::Rect(20, 20, 60, 60));
1540 EXPECT_EQ(NULL, host()->dispatcher()->mouse_moved_handler());
1542 ui::test::EventGenerator generator(root_window(), w1.get());
1544 // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
1545 // root window.
1546 generator.MoveMouseTo(51, 51);
1547 EXPECT_EQ(w1.get(), host()->dispatcher()->mouse_moved_handler());
1549 // Create window 2 under the mouse cursor and stack it above window 1.
1550 Window* w2 = CreateNormalWindow(2, root_window(), NULL);
1551 w2->SetBounds(gfx::Rect(30, 30, 40, 40));
1552 root_window()->StackChildAbove(w2, w1.get());
1554 // Set window 2 as the window that is to be deleted when a mouse-exited event
1555 // happens on window 1.
1556 w1_filter.set_window_to_delete(w2);
1558 // Move mosue over window 2. This should generate a mouse-exited event for
1559 // window 1 resulting in deletion of window 2. The original mouse-moved event
1560 // that was targeted to window 2 should be dropped since window 2 is
1561 // destroyed. This test passes if no crash happens.
1562 generator.MoveMouseTo(52, 52);
1563 EXPECT_EQ(NULL, host()->dispatcher()->mouse_moved_handler());
1565 // Check events received by window 1.
1566 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
1567 EventTypesToString(w1_filter.events()));
1570 namespace {
1572 // Used to track if OnWindowDestroying() is invoked and if there is a valid
1573 // RootWindow at such time.
1574 class ValidRootDuringDestructionWindowObserver : public aura::WindowObserver {
1575 public:
1576 ValidRootDuringDestructionWindowObserver(bool* got_destroying,
1577 bool* has_valid_root)
1578 : got_destroying_(got_destroying),
1579 has_valid_root_(has_valid_root) {
1582 // WindowObserver:
1583 void OnWindowDestroying(aura::Window* window) override {
1584 *got_destroying_ = true;
1585 *has_valid_root_ = (window->GetRootWindow() != NULL);
1588 private:
1589 bool* got_destroying_;
1590 bool* has_valid_root_;
1592 DISALLOW_COPY_AND_ASSIGN(ValidRootDuringDestructionWindowObserver);
1595 } // namespace
1597 // Verifies GetRootWindow() from ~Window returns a valid root.
1598 TEST_F(WindowEventDispatcherTest, ValidRootDuringDestruction) {
1599 bool got_destroying = false;
1600 bool has_valid_root = false;
1601 ValidRootDuringDestructionWindowObserver observer(&got_destroying,
1602 &has_valid_root);
1604 scoped_ptr<WindowTreeHost> host(
1605 WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100)));
1606 host->InitHost();
1607 // Owned by WindowEventDispatcher.
1608 Window* w1 = CreateNormalWindow(1, host->window(), NULL);
1609 w1->AddObserver(&observer);
1611 EXPECT_TRUE(got_destroying);
1612 EXPECT_TRUE(has_valid_root);
1615 namespace {
1617 // See description above DontResetHeldEvent for details.
1618 class DontResetHeldEventWindowDelegate : public test::TestWindowDelegate {
1619 public:
1620 explicit DontResetHeldEventWindowDelegate(aura::Window* root)
1621 : root_(root),
1622 mouse_event_count_(0) {}
1623 ~DontResetHeldEventWindowDelegate() override {}
1625 int mouse_event_count() const { return mouse_event_count_; }
1627 // TestWindowDelegate:
1628 void OnMouseEvent(ui::MouseEvent* event) override {
1629 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0 &&
1630 mouse_event_count_++ == 0) {
1631 ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1632 gfx::Point(10, 10), ui::EventTimeForNow(),
1633 ui::EF_SHIFT_DOWN, 0);
1634 root_->GetHost()->dispatcher()->RepostEvent(mouse_event);
1638 private:
1639 Window* root_;
1640 int mouse_event_count_;
1642 DISALLOW_COPY_AND_ASSIGN(DontResetHeldEventWindowDelegate);
1645 } // namespace
1647 // Verifies RootWindow doesn't reset |RootWindow::held_repostable_event_| after
1648 // dispatching. This is done by using DontResetHeldEventWindowDelegate, which
1649 // tracks the number of events with ui::EF_SHIFT_DOWN set (all reposted events
1650 // have EF_SHIFT_DOWN). When the first event is seen RepostEvent() is used to
1651 // schedule another reposted event.
1652 TEST_F(WindowEventDispatcherTest, DontResetHeldEvent) {
1653 DontResetHeldEventWindowDelegate delegate(root_window());
1654 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
1655 w1->SetBounds(gfx::Rect(0, 0, 40, 40));
1656 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1657 gfx::Point(10, 10), ui::EventTimeForNow(),
1658 ui::EF_SHIFT_DOWN, 0);
1659 root_window()->GetHost()->dispatcher()->RepostEvent(pressed);
1660 ui::MouseEvent pressed2(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1661 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
1662 // Dispatch an event to flush event scheduled by way of RepostEvent().
1663 DispatchEventUsingWindowDispatcher(&pressed2);
1664 // Delegate should have seen reposted event (identified by way of
1665 // EF_SHIFT_DOWN). Dispatch another event to flush the second
1666 // RepostedEvent().
1667 EXPECT_EQ(1, delegate.mouse_event_count());
1668 DispatchEventUsingWindowDispatcher(&pressed2);
1669 EXPECT_EQ(2, delegate.mouse_event_count());
1672 namespace {
1674 // See description above DeleteHostFromHeldMouseEvent for details.
1675 class DeleteHostFromHeldMouseEventDelegate
1676 : public test::TestWindowDelegate {
1677 public:
1678 explicit DeleteHostFromHeldMouseEventDelegate(WindowTreeHost* host)
1679 : host_(host),
1680 got_mouse_event_(false),
1681 got_destroy_(false) {
1683 ~DeleteHostFromHeldMouseEventDelegate() override {}
1685 bool got_mouse_event() const { return got_mouse_event_; }
1686 bool got_destroy() const { return got_destroy_; }
1688 // TestWindowDelegate:
1689 void OnMouseEvent(ui::MouseEvent* event) override {
1690 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0) {
1691 got_mouse_event_ = true;
1692 delete host_;
1695 void OnWindowDestroyed(Window* window) override { got_destroy_ = true; }
1697 private:
1698 WindowTreeHost* host_;
1699 bool got_mouse_event_;
1700 bool got_destroy_;
1702 DISALLOW_COPY_AND_ASSIGN(DeleteHostFromHeldMouseEventDelegate);
1705 } // namespace
1707 // Verifies if a WindowTreeHost is deleted from dispatching a held mouse event
1708 // we don't crash.
1709 TEST_F(WindowEventDispatcherTest, DeleteHostFromHeldMouseEvent) {
1710 // Should be deleted by |delegate|.
1711 WindowTreeHost* h2 = WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100));
1712 h2->InitHost();
1713 DeleteHostFromHeldMouseEventDelegate delegate(h2);
1714 // Owned by |h2|.
1715 Window* w1 = CreateNormalWindow(1, h2->window(), &delegate);
1716 w1->SetBounds(gfx::Rect(0, 0, 40, 40));
1717 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1718 gfx::Point(10, 10), ui::EventTimeForNow(),
1719 ui::EF_SHIFT_DOWN, 0);
1720 h2->dispatcher()->RepostEvent(pressed);
1721 // RunAllPendingInMessageLoop() to make sure the |pressed| is run.
1722 RunAllPendingInMessageLoop();
1723 EXPECT_TRUE(delegate.got_mouse_event());
1724 EXPECT_TRUE(delegate.got_destroy());
1727 TEST_F(WindowEventDispatcherTest, WindowHideCancelsActiveTouches) {
1728 EventFilterRecorder recorder;
1729 root_window()->AddPreTargetHandler(&recorder);
1731 test::TestWindowDelegate delegate;
1732 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1733 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1735 gfx::Point position1 = root_window()->bounds().origin();
1736 ui::TouchEvent press(
1737 ui::ET_TOUCH_PRESSED, position1, 0, ui::EventTimeForNow());
1738 DispatchEventUsingWindowDispatcher(&press);
1740 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN",
1741 EventTypesToString(recorder.GetAndResetEvents()));
1743 window->Hide();
1745 EXPECT_EQ(ui::ET_TOUCH_CANCELLED, recorder.events()[0]);
1746 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_TAP_CANCEL));
1747 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_END));
1748 EXPECT_EQ(3U, recorder.events().size());
1749 root_window()->RemovePreTargetHandler(&recorder);
1752 TEST_F(WindowEventDispatcherTest, WindowHideCancelsActiveGestures) {
1753 EventFilterRecorder recorder;
1754 root_window()->AddPreTargetHandler(&recorder);
1756 test::TestWindowDelegate delegate;
1757 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1758 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1760 gfx::Point position1 = root_window()->bounds().origin();
1761 gfx::Point position2 = root_window()->bounds().CenterPoint();
1762 ui::TouchEvent press(
1763 ui::ET_TOUCH_PRESSED, position1, 0, ui::EventTimeForNow());
1764 DispatchEventUsingWindowDispatcher(&press);
1766 ui::TouchEvent move(
1767 ui::ET_TOUCH_MOVED, position2, 0, ui::EventTimeForNow());
1768 DispatchEventUsingWindowDispatcher(&move);
1770 ui::TouchEvent press2(
1771 ui::ET_TOUCH_PRESSED, position1, 1, ui::EventTimeForNow());
1772 DispatchEventUsingWindowDispatcher(&press2);
1774 // TODO(tdresser): once the unified Gesture Recognizer has stuck, remove the
1775 // special casing here. See crbug.com/332418 for details.
1776 std::string expected =
1777 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1778 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1779 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_PINCH_BEGIN";
1781 std::string expected_ugr =
1782 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1783 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1784 "TOUCH_PRESSED GESTURE_BEGIN";
1786 std::string events_string = EventTypesToString(recorder.GetAndResetEvents());
1787 EXPECT_TRUE((expected == events_string) || (expected_ugr == events_string));
1789 window->Hide();
1791 expected =
1792 "TOUCH_CANCELLED GESTURE_PINCH_END GESTURE_END TOUCH_CANCELLED "
1793 "GESTURE_SCROLL_END GESTURE_END";
1794 expected_ugr =
1795 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END TOUCH_CANCELLED "
1796 "GESTURE_END";
1798 events_string = EventTypesToString(recorder.GetAndResetEvents());
1799 EXPECT_TRUE((expected == events_string) || (expected_ugr == events_string));
1801 root_window()->RemovePreTargetHandler(&recorder);
1804 // Places two windows side by side. Presses down on one window, and starts a
1805 // scroll. Sets capture on the other window and ensures that the "ending" events
1806 // aren't sent to the window which gained capture.
1807 TEST_F(WindowEventDispatcherTest, EndingEventDoesntRetarget) {
1808 EventFilterRecorder recorder1;
1809 EventFilterRecorder recorder2;
1810 scoped_ptr<Window> window1(CreateNormalWindow(1, root_window(), NULL));
1811 window1->SetBounds(gfx::Rect(0, 0, 40, 40));
1813 scoped_ptr<Window> window2(CreateNormalWindow(2, root_window(), NULL));
1814 window2->SetBounds(gfx::Rect(40, 0, 40, 40));
1816 window1->AddPreTargetHandler(&recorder1);
1817 window2->AddPreTargetHandler(&recorder2);
1819 gfx::Point position = window1->bounds().origin();
1820 ui::TouchEvent press(
1821 ui::ET_TOUCH_PRESSED, position, 0, ui::EventTimeForNow());
1822 DispatchEventUsingWindowDispatcher(&press);
1824 gfx::Point position2 = window1->bounds().CenterPoint();
1825 ui::TouchEvent move(
1826 ui::ET_TOUCH_MOVED, position2, 0, ui::EventTimeForNow());
1827 DispatchEventUsingWindowDispatcher(&move);
1829 window2->SetCapture();
1831 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1832 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1833 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END",
1834 EventTypesToString(recorder1.events()));
1836 EXPECT_TRUE(recorder2.events().empty());
1839 namespace {
1841 // This class creates and manages a window which is destroyed as soon as
1842 // capture is lost. This is the case for the drag and drop capture window.
1843 class CaptureWindowTracker : public test::TestWindowDelegate {
1844 public:
1845 CaptureWindowTracker() {}
1846 ~CaptureWindowTracker() override {}
1848 void CreateCaptureWindow(aura::Window* root_window) {
1849 capture_window_.reset(test::CreateTestWindowWithDelegate(
1850 this, -1234, gfx::Rect(20, 20, 20, 20), root_window));
1851 capture_window_->SetCapture();
1854 void reset() {
1855 capture_window_.reset();
1858 void OnCaptureLost() override { capture_window_.reset(); }
1860 void OnWindowDestroyed(Window* window) override {
1861 TestWindowDelegate::OnWindowDestroyed(window);
1862 capture_window_.reset();
1865 aura::Window* capture_window() { return capture_window_.get(); }
1867 private:
1868 scoped_ptr<aura::Window> capture_window_;
1870 DISALLOW_COPY_AND_ASSIGN(CaptureWindowTracker);
1875 // Verifies handling loss of capture by the capture window being hidden.
1876 TEST_F(WindowEventDispatcherTest, CaptureWindowHidden) {
1877 CaptureWindowTracker capture_window_tracker;
1878 capture_window_tracker.CreateCaptureWindow(root_window());
1879 capture_window_tracker.capture_window()->Hide();
1880 EXPECT_EQ(NULL, capture_window_tracker.capture_window());
1883 // Verifies handling loss of capture by the capture window being destroyed.
1884 TEST_F(WindowEventDispatcherTest, CaptureWindowDestroyed) {
1885 CaptureWindowTracker capture_window_tracker;
1886 capture_window_tracker.CreateCaptureWindow(root_window());
1887 capture_window_tracker.reset();
1888 EXPECT_EQ(NULL, capture_window_tracker.capture_window());
1891 class ExitMessageLoopOnMousePress : public ui::test::TestEventHandler {
1892 public:
1893 ExitMessageLoopOnMousePress() {}
1894 ~ExitMessageLoopOnMousePress() override {}
1896 protected:
1897 void OnMouseEvent(ui::MouseEvent* event) override {
1898 ui::test::TestEventHandler::OnMouseEvent(event);
1899 if (event->type() == ui::ET_MOUSE_PRESSED)
1900 base::MessageLoopForUI::current()->Quit();
1903 private:
1904 DISALLOW_COPY_AND_ASSIGN(ExitMessageLoopOnMousePress);
1907 class WindowEventDispatcherTestWithMessageLoop
1908 : public WindowEventDispatcherTest {
1909 public:
1910 WindowEventDispatcherTestWithMessageLoop() {}
1911 ~WindowEventDispatcherTestWithMessageLoop() override {}
1913 void RunTest() {
1914 // Reset any event the window may have received when bringing up the window
1915 // (e.g. mouse-move events if the mouse cursor is over the window).
1916 handler_.Reset();
1918 // Start a nested message-loop, post an event to be dispatched, and then
1919 // terminate the message-loop. When the message-loop unwinds and gets back,
1920 // the reposted event should not have fired.
1921 scoped_ptr<ui::MouseEvent> mouse(new ui::MouseEvent(
1922 ui::ET_MOUSE_PRESSED, gfx::Point(10, 10), gfx::Point(10, 10),
1923 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE));
1924 message_loop()->PostTask(
1925 FROM_HERE,
1926 base::Bind(&WindowEventDispatcherTestWithMessageLoop::RepostEventHelper,
1927 host()->dispatcher(),
1928 base::Passed(&mouse)));
1929 message_loop()->PostTask(FROM_HERE, message_loop()->QuitClosure());
1931 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop());
1932 base::RunLoop loop;
1933 loop.Run();
1934 EXPECT_EQ(0, handler_.num_mouse_events());
1936 // Let the current message-loop run. The event-handler will terminate the
1937 // message-loop when it receives the reposted event.
1940 base::MessageLoop* message_loop() {
1941 return base::MessageLoopForUI::current();
1944 protected:
1945 void SetUp() override {
1946 WindowEventDispatcherTest::SetUp();
1947 window_.reset(CreateNormalWindow(1, root_window(), NULL));
1948 window_->AddPreTargetHandler(&handler_);
1951 void TearDown() override {
1952 window_.reset();
1953 WindowEventDispatcherTest::TearDown();
1956 private:
1957 // Used to avoid a copying |event| when binding to a closure.
1958 static void RepostEventHelper(WindowEventDispatcher* dispatcher,
1959 scoped_ptr<ui::MouseEvent> event) {
1960 dispatcher->RepostEvent(*event);
1963 scoped_ptr<Window> window_;
1964 ExitMessageLoopOnMousePress handler_;
1966 DISALLOW_COPY_AND_ASSIGN(WindowEventDispatcherTestWithMessageLoop);
1969 TEST_F(WindowEventDispatcherTestWithMessageLoop, EventRepostedInNonNestedLoop) {
1970 CHECK(!message_loop()->is_running());
1971 // Perform the test in a callback, so that it runs after the message-loop
1972 // starts.
1973 message_loop()->PostTask(
1974 FROM_HERE, base::Bind(
1975 &WindowEventDispatcherTestWithMessageLoop::RunTest,
1976 base::Unretained(this)));
1977 message_loop()->Run();
1980 class WindowEventDispatcherTestInHighDPI : public WindowEventDispatcherTest {
1981 public:
1982 WindowEventDispatcherTestInHighDPI() {}
1983 ~WindowEventDispatcherTestInHighDPI() override {}
1985 void DispatchEvent(ui::Event* event) {
1986 DispatchEventUsingWindowDispatcher(event);
1989 protected:
1990 void SetUp() override {
1991 WindowEventDispatcherTest::SetUp();
1992 test_screen()->SetDeviceScaleFactor(2.f);
1996 TEST_F(WindowEventDispatcherTestInHighDPI, EventLocationTransform) {
1997 test::TestWindowDelegate delegate;
1998 scoped_ptr<aura::Window> child(test::CreateTestWindowWithDelegate(&delegate,
1999 1234, gfx::Rect(20, 20, 100, 100), root_window()));
2000 child->Show();
2002 ui::test::TestEventHandler handler_child;
2003 ui::test::TestEventHandler handler_root;
2004 root_window()->AddPreTargetHandler(&handler_root);
2005 child->AddPreTargetHandler(&handler_child);
2008 ui::MouseEvent move(ui::ET_MOUSE_MOVED, gfx::Point(30, 30),
2009 gfx::Point(30, 30), ui::EventTimeForNow(), ui::EF_NONE,
2010 ui::EF_NONE);
2011 DispatchEventUsingWindowDispatcher(&move);
2012 EXPECT_EQ(0, handler_child.num_mouse_events());
2013 EXPECT_EQ(1, handler_root.num_mouse_events());
2017 ui::MouseEvent move(ui::ET_MOUSE_MOVED, gfx::Point(50, 50),
2018 gfx::Point(50, 50), ui::EventTimeForNow(), ui::EF_NONE,
2019 ui::EF_NONE);
2020 DispatchEventUsingWindowDispatcher(&move);
2021 // The child receives an ENTER, and a MOVED event.
2022 EXPECT_EQ(2, handler_child.num_mouse_events());
2023 // The root receives both the ENTER and the MOVED events dispatched to
2024 // |child|, as well as an EXIT event.
2025 EXPECT_EQ(3, handler_root.num_mouse_events());
2028 child->RemovePreTargetHandler(&handler_child);
2029 root_window()->RemovePreTargetHandler(&handler_root);
2032 TEST_F(WindowEventDispatcherTestInHighDPI, TouchMovesHeldOnScroll) {
2033 EventFilterRecorder recorder;
2034 root_window()->AddPreTargetHandler(&recorder);
2035 test::TestWindowDelegate delegate;
2036 HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
2037 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2038 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2039 window->AddPreTargetHandler(&handler);
2041 ui::test::EventGenerator generator(root_window());
2042 generator.GestureScrollSequence(
2043 gfx::Point(120, 120), gfx::Point(20, 120),
2044 base::TimeDelta::FromMilliseconds(100), 25);
2046 // |handler| will have reset |filter| and started holding the touch-move
2047 // events when scrolling started. At the end of the scroll (i.e. upon
2048 // touch-release), the held touch-move event will have been dispatched first,
2049 // along with the subsequent events (i.e. touch-release, scroll-end, and
2050 // gesture-end).
2051 const EventFilterRecorder::Events& events = recorder.events();
2052 EXPECT_EQ(
2053 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
2054 "GESTURE_SCROLL_END GESTURE_END",
2055 EventTypesToString(events));
2056 ASSERT_EQ(2u, recorder.touch_locations().size());
2057 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2058 recorder.touch_locations()[0].ToString());
2059 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2060 recorder.touch_locations()[1].ToString());
2063 // This handler triggers a nested message loop when it receives a right click
2064 // event, and runs a single callback in the nested message loop.
2065 class TriggerNestedLoopOnRightMousePress : public ui::test::TestEventHandler {
2066 public:
2067 explicit TriggerNestedLoopOnRightMousePress(const base::Closure& callback)
2068 : callback_(callback) {}
2069 ~TriggerNestedLoopOnRightMousePress() override {}
2071 const gfx::Point mouse_move_location() const { return mouse_move_location_; }
2073 private:
2074 void OnMouseEvent(ui::MouseEvent* mouse) override {
2075 TestEventHandler::OnMouseEvent(mouse);
2076 if (mouse->type() == ui::ET_MOUSE_PRESSED &&
2077 mouse->IsOnlyRightMouseButton()) {
2078 base::MessageLoop::ScopedNestableTaskAllower allow(
2079 base::MessageLoopForUI::current());
2080 base::RunLoop run_loop;
2081 scoped_refptr<base::TaskRunner> task_runner =
2082 base::ThreadTaskRunnerHandle::Get();
2083 if (!callback_.is_null())
2084 task_runner->PostTask(FROM_HERE, callback_);
2085 task_runner->PostTask(FROM_HERE, run_loop.QuitClosure());
2086 run_loop.Run();
2087 } else if (mouse->type() == ui::ET_MOUSE_MOVED) {
2088 mouse_move_location_ = mouse->location();
2092 base::Closure callback_;
2093 gfx::Point mouse_move_location_;
2095 DISALLOW_COPY_AND_ASSIGN(TriggerNestedLoopOnRightMousePress);
2098 // Tests that if dispatching a 'held' event triggers a nested message loop, then
2099 // the events that are dispatched from the nested message loop are transformed
2100 // correctly.
2101 TEST_F(WindowEventDispatcherTestInHighDPI,
2102 EventsTransformedInRepostedEventTriggeredNestedLoop) {
2103 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
2104 // Make sure the window is visible.
2105 RunAllPendingInMessageLoop();
2107 ui::MouseEvent mouse_move(ui::ET_MOUSE_MOVED, gfx::Point(80, 80),
2108 gfx::Point(80, 80), ui::EventTimeForNow(),
2109 ui::EF_NONE, ui::EF_NONE);
2110 const base::Closure callback_on_right_click = base::Bind(
2111 base::IgnoreResult(&WindowEventDispatcherTestInHighDPI::DispatchEvent),
2112 base::Unretained(this), base::Unretained(&mouse_move));
2113 TriggerNestedLoopOnRightMousePress handler(callback_on_right_click);
2114 window->AddPreTargetHandler(&handler);
2116 scoped_ptr<ui::MouseEvent> mouse(
2117 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
2118 gfx::Point(10, 10), ui::EventTimeForNow(),
2119 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
2120 host()->dispatcher()->RepostEvent(*mouse);
2121 EXPECT_EQ(0, handler.num_mouse_events());
2123 base::RunLoop run_loop;
2124 run_loop.RunUntilIdle();
2125 // The window should receive the mouse-press and the mouse-move events.
2126 EXPECT_EQ(2, handler.num_mouse_events());
2127 // The mouse-move event location should be transformed because of the DSF
2128 // before it reaches the window.
2129 EXPECT_EQ(gfx::Point(40, 40).ToString(),
2130 handler.mouse_move_location().ToString());
2131 EXPECT_EQ(gfx::Point(40, 40).ToString(),
2132 Env::GetInstance()->last_mouse_location().ToString());
2133 window->RemovePreTargetHandler(&handler);
2136 class SelfDestructDelegate : public test::TestWindowDelegate {
2137 public:
2138 SelfDestructDelegate() {}
2139 ~SelfDestructDelegate() override {}
2141 void OnMouseEvent(ui::MouseEvent* event) override { window_.reset(); }
2143 void set_window(scoped_ptr<aura::Window> window) {
2144 window_ = window.Pass();
2146 bool has_window() const { return !!window_.get(); }
2148 private:
2149 scoped_ptr<aura::Window> window_;
2150 DISALLOW_COPY_AND_ASSIGN(SelfDestructDelegate);
2153 TEST_F(WindowEventDispatcherTest, SynthesizedLocatedEvent) {
2154 ui::test::EventGenerator generator(root_window());
2155 generator.MoveMouseTo(10, 10);
2156 EXPECT_EQ("10,10",
2157 Env::GetInstance()->last_mouse_location().ToString());
2159 // Synthesized event should not update the mouse location.
2160 ui::MouseEvent mouseev(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
2161 ui::EventTimeForNow(), ui::EF_IS_SYNTHESIZED, 0);
2162 generator.Dispatch(&mouseev);
2163 EXPECT_EQ("10,10",
2164 Env::GetInstance()->last_mouse_location().ToString());
2166 generator.MoveMouseTo(0, 0);
2167 EXPECT_EQ("0,0",
2168 Env::GetInstance()->last_mouse_location().ToString());
2170 // Make sure the location gets updated when a syntheiszed enter
2171 // event destroyed the window.
2172 SelfDestructDelegate delegate;
2173 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2174 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2175 delegate.set_window(window.Pass());
2176 EXPECT_TRUE(delegate.has_window());
2178 generator.MoveMouseTo(100, 100);
2179 EXPECT_FALSE(delegate.has_window());
2180 EXPECT_EQ("100,100",
2181 Env::GetInstance()->last_mouse_location().ToString());
2184 // Tests that the window which has capture can get destroyed as a result of
2185 // ui::ET_MOUSE_CAPTURE_CHANGED event dispatched in
2186 // WindowEventDispatcher::UpdateCapture without causing a "use after free".
2187 TEST_F(WindowEventDispatcherTest, DestroyWindowOnCaptureChanged) {
2188 SelfDestructDelegate delegate;
2189 scoped_ptr<aura::Window> window_first(CreateTestWindowWithDelegate(
2190 &delegate, 1, gfx::Rect(20, 10, 10, 20), root_window()));
2191 Window* window_first_raw = window_first.get();
2192 window_first->Show();
2193 window_first->SetCapture();
2194 delegate.set_window(window_first.Pass());
2195 EXPECT_TRUE(delegate.has_window());
2197 scoped_ptr<aura::Window> window_second(
2198 test::CreateTestWindowWithId(2, root_window()));
2199 window_second->Show();
2201 client::CaptureDelegate* capture_delegate = host()->dispatcher();
2202 capture_delegate->UpdateCapture(window_first_raw, window_second.get());
2203 EXPECT_FALSE(delegate.has_window());
2206 class StaticFocusClient : public client::FocusClient {
2207 public:
2208 explicit StaticFocusClient(Window* focused)
2209 : focused_(focused) {}
2210 ~StaticFocusClient() override {}
2212 private:
2213 // client::FocusClient:
2214 void AddObserver(client::FocusChangeObserver* observer) override {}
2215 void RemoveObserver(client::FocusChangeObserver* observer) override {}
2216 void FocusWindow(Window* window) override {}
2217 void ResetFocusWithinActiveWindow(Window* window) override {}
2218 Window* GetFocusedWindow() override { return focused_; }
2220 Window* focused_;
2222 DISALLOW_COPY_AND_ASSIGN(StaticFocusClient);
2225 // Tests that host-cancel-mode event can be dispatched to a dispatcher safely
2226 // when the focused window does not live in the dispatcher's tree.
2227 TEST_F(WindowEventDispatcherTest, HostCancelModeWithFocusedWindowOutside) {
2228 test::TestWindowDelegate delegate;
2229 scoped_ptr<Window> focused(CreateTestWindowWithDelegate(&delegate, 123,
2230 gfx::Rect(20, 30, 100, 50), NULL));
2231 StaticFocusClient focus_client(focused.get());
2232 client::SetFocusClient(root_window(), &focus_client);
2233 EXPECT_FALSE(root_window()->Contains(focused.get()));
2234 EXPECT_EQ(focused.get(),
2235 client::GetFocusClient(root_window())->GetFocusedWindow());
2236 host()->dispatcher()->DispatchCancelModeEvent();
2237 EXPECT_EQ(focused.get(),
2238 client::GetFocusClient(root_window())->GetFocusedWindow());
2241 // Dispatches a mouse-move event to |target| when it receives a mouse-move
2242 // event.
2243 class DispatchEventHandler : public ui::EventHandler {
2244 public:
2245 explicit DispatchEventHandler(Window* target)
2246 : target_(target),
2247 dispatched_(false) {}
2248 ~DispatchEventHandler() override {}
2250 bool dispatched() const { return dispatched_; }
2251 private:
2252 // ui::EventHandler:
2253 void OnMouseEvent(ui::MouseEvent* mouse) override {
2254 if (mouse->type() == ui::ET_MOUSE_MOVED) {
2255 ui::MouseEvent move(ui::ET_MOUSE_MOVED, target_->bounds().CenterPoint(),
2256 target_->bounds().CenterPoint(),
2257 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
2258 ui::EventDispatchDetails details =
2259 target_->GetHost()->dispatcher()->OnEventFromSource(&move);
2260 ASSERT_FALSE(details.dispatcher_destroyed);
2261 EXPECT_FALSE(details.target_destroyed);
2262 EXPECT_EQ(target_, move.target());
2263 dispatched_ = true;
2265 ui::EventHandler::OnMouseEvent(mouse);
2268 Window* target_;
2269 bool dispatched_;
2271 DISALLOW_COPY_AND_ASSIGN(DispatchEventHandler);
2274 // Moves |window| to |root_window| when it receives a mouse-move event.
2275 class MoveWindowHandler : public ui::EventHandler {
2276 public:
2277 MoveWindowHandler(Window* window, Window* root_window)
2278 : window_to_move_(window),
2279 root_window_to_move_to_(root_window) {}
2280 ~MoveWindowHandler() override {}
2282 private:
2283 // ui::EventHandler:
2284 void OnMouseEvent(ui::MouseEvent* mouse) override {
2285 if (mouse->type() == ui::ET_MOUSE_MOVED) {
2286 root_window_to_move_to_->AddChild(window_to_move_);
2288 ui::EventHandler::OnMouseEvent(mouse);
2291 Window* window_to_move_;
2292 Window* root_window_to_move_to_;
2294 DISALLOW_COPY_AND_ASSIGN(MoveWindowHandler);
2297 // Tests that nested event dispatch works correctly if the target of the older
2298 // event being dispatched is moved to a different dispatcher in response to an
2299 // event in the inner loop.
2300 TEST_F(WindowEventDispatcherTest, NestedEventDispatchTargetMoved) {
2301 scoped_ptr<WindowTreeHost> second_host(
2302 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2303 second_host->InitHost();
2304 Window* second_root = second_host->window();
2306 // Create two windows parented to |root_window()|.
2307 test::TestWindowDelegate delegate;
2308 scoped_ptr<Window> first(CreateTestWindowWithDelegate(&delegate, 123,
2309 gfx::Rect(20, 10, 10, 20), root_window()));
2310 scoped_ptr<Window> second(CreateTestWindowWithDelegate(&delegate, 234,
2311 gfx::Rect(40, 10, 50, 20), root_window()));
2313 // Setup a handler on |first| so that it dispatches an event to |second| when
2314 // |first| receives an event.
2315 DispatchEventHandler dispatch_event(second.get());
2316 first->AddPreTargetHandler(&dispatch_event);
2318 // Setup a handler on |second| so that it moves |first| into |second_root|
2319 // when |second| receives an event.
2320 MoveWindowHandler move_window(first.get(), second_root);
2321 second->AddPreTargetHandler(&move_window);
2323 // Some sanity checks: |first| is inside |root_window()|'s tree.
2324 EXPECT_EQ(root_window(), first->GetRootWindow());
2325 // The two root windows are different.
2326 EXPECT_NE(root_window(), second_root);
2328 // Dispatch an event to |first|.
2329 ui::MouseEvent move(ui::ET_MOUSE_MOVED, first->bounds().CenterPoint(),
2330 first->bounds().CenterPoint(), ui::EventTimeForNow(),
2331 ui::EF_NONE, ui::EF_NONE);
2332 ui::EventDispatchDetails details =
2333 host()->dispatcher()->OnEventFromSource(&move);
2334 ASSERT_FALSE(details.dispatcher_destroyed);
2335 EXPECT_TRUE(details.target_destroyed);
2336 EXPECT_EQ(first.get(), move.target());
2337 EXPECT_TRUE(dispatch_event.dispatched());
2338 EXPECT_EQ(second_root, first->GetRootWindow());
2340 first->RemovePreTargetHandler(&dispatch_event);
2341 second->RemovePreTargetHandler(&move_window);
2344 class AlwaysMouseDownInputStateLookup : public InputStateLookup {
2345 public:
2346 AlwaysMouseDownInputStateLookup() {}
2347 ~AlwaysMouseDownInputStateLookup() override {}
2349 private:
2350 // InputStateLookup:
2351 bool IsMouseButtonDown() const override { return true; }
2353 DISALLOW_COPY_AND_ASSIGN(AlwaysMouseDownInputStateLookup);
2356 TEST_F(WindowEventDispatcherTest,
2357 CursorVisibilityChangedWhileCaptureWindowInAnotherDispatcher) {
2358 test::EventCountDelegate delegate;
2359 scoped_ptr<Window> window(CreateTestWindowWithDelegate(&delegate, 123,
2360 gfx::Rect(20, 10, 10, 20), root_window()));
2361 window->Show();
2363 scoped_ptr<WindowTreeHost> second_host(
2364 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2365 second_host->InitHost();
2366 WindowEventDispatcher* second_dispatcher = second_host->dispatcher();
2368 // Install an InputStateLookup on the Env that always claims that a
2369 // mouse-button is down.
2370 test::EnvTestHelper(Env::GetInstance()).SetInputStateLookup(
2371 scoped_ptr<InputStateLookup>(new AlwaysMouseDownInputStateLookup()));
2373 window->SetCapture();
2375 // Because the mouse button is down, setting the capture on |window| will set
2376 // it as the mouse-move handler for |root_window()|.
2377 EXPECT_EQ(window.get(), host()->dispatcher()->mouse_moved_handler());
2379 // This does not set |window| as the mouse-move handler for the second
2380 // dispatcher.
2381 EXPECT_EQ(NULL, second_dispatcher->mouse_moved_handler());
2383 // However, some capture-client updates the capture in each root-window on a
2384 // capture. Emulate that here. Because of this, the second dispatcher also has
2385 // |window| as the mouse-move handler.
2386 client::CaptureDelegate* second_capture_delegate = second_dispatcher;
2387 second_capture_delegate->UpdateCapture(NULL, window.get());
2388 EXPECT_EQ(window.get(), second_dispatcher->mouse_moved_handler());
2390 // Reset the mouse-event counts for |window|.
2391 delegate.GetMouseMotionCountsAndReset();
2393 // Notify both hosts that the cursor is now hidden. This should send a single
2394 // mouse-exit event to |window|.
2395 host()->OnCursorVisibilityChanged(false);
2396 second_host->OnCursorVisibilityChanged(false);
2397 EXPECT_EQ("0 0 1", delegate.GetMouseMotionCountsAndReset());
2400 TEST_F(WindowEventDispatcherTest,
2401 RedirectedEventToDifferentDispatcherLocation) {
2402 scoped_ptr<WindowTreeHost> second_host(
2403 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2404 second_host->InitHost();
2405 client::SetCaptureClient(second_host->window(),
2406 client::GetCaptureClient(root_window()));
2408 test::EventCountDelegate delegate;
2409 scoped_ptr<Window> window_first(CreateTestWindowWithDelegate(&delegate, 123,
2410 gfx::Rect(20, 10, 10, 20), root_window()));
2411 window_first->Show();
2413 scoped_ptr<Window> window_second(CreateTestWindowWithDelegate(&delegate, 12,
2414 gfx::Rect(10, 10, 20, 30), second_host->window()));
2415 window_second->Show();
2417 window_second->SetCapture();
2418 EXPECT_EQ(window_second.get(),
2419 client::GetCaptureWindow(root_window()));
2421 // Send an event to the first host. Make sure it goes to |window_second| in
2422 // |second_host| instead (since it has capture).
2423 EventFilterRecorder recorder_first;
2424 window_first->AddPreTargetHandler(&recorder_first);
2425 EventFilterRecorder recorder_second;
2426 window_second->AddPreTargetHandler(&recorder_second);
2427 const gfx::Point event_location(25, 15);
2428 ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, event_location, event_location,
2429 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
2430 ui::EF_LEFT_MOUSE_BUTTON);
2431 DispatchEventUsingWindowDispatcher(&mouse);
2432 EXPECT_TRUE(recorder_first.events().empty());
2433 ASSERT_EQ(1u, recorder_second.events().size());
2434 EXPECT_EQ(ui::ET_MOUSE_PRESSED, recorder_second.events()[0]);
2435 EXPECT_EQ(event_location.ToString(),
2436 recorder_second.mouse_locations()[0].ToString());
2439 class AsyncWindowDelegate : public test::TestWindowDelegate {
2440 public:
2441 AsyncWindowDelegate(WindowEventDispatcher* dispatcher)
2442 : dispatcher_(dispatcher) {}
2444 void set_window(Window* window) {
2445 window_ = window;
2447 private:
2448 void OnTouchEvent(ui::TouchEvent* event) override {
2449 // Convert touch event back to root window coordinates.
2450 event->ConvertLocationToTarget(window_, window_->GetRootWindow());
2451 event->DisableSynchronousHandling();
2452 dispatcher_->ProcessedTouchEvent(event->unique_event_id(), window_,
2453 ui::ER_UNHANDLED);
2454 event->StopPropagation();
2457 WindowEventDispatcher* dispatcher_;
2458 Window* window_;
2460 DISALLOW_COPY_AND_ASSIGN(AsyncWindowDelegate);
2463 // Tests that gesture events dispatched through the asynchronous flow have
2464 // co-ordinates in the right co-ordinate space.
2465 TEST_F(WindowEventDispatcherTest, GestureEventCoordinates) {
2466 const float kX = 67.3f;
2467 const float kY = 97.8f;
2469 const int kWindowOffset = 50;
2470 EventFilterRecorder recorder;
2471 root_window()->AddPreTargetHandler(&recorder);
2472 AsyncWindowDelegate delegate(host()->dispatcher());
2473 HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
2474 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2475 &delegate,
2477 gfx::Rect(kWindowOffset, kWindowOffset, 100, 100),
2478 root_window()));
2479 window->AddPreTargetHandler(&handler);
2481 delegate.set_window(window.get());
2483 ui::TouchEvent touch_pressed_event(
2484 ui::ET_TOUCH_PRESSED, gfx::PointF(kX, kY), 0, ui::EventTimeForNow());
2486 DispatchEventUsingWindowDispatcher(&touch_pressed_event);
2488 ASSERT_EQ(1u, recorder.touch_locations().size());
2489 EXPECT_EQ(gfx::Point(kX - kWindowOffset, kY - kWindowOffset).ToString(),
2490 recorder.touch_locations()[0].ToString());
2492 ASSERT_EQ(2u, recorder.gesture_locations().size());
2493 EXPECT_EQ(gfx::Point(kX - kWindowOffset, kY - kWindowOffset).ToString(),
2494 recorder.gesture_locations()[0].ToString());
2497 // Tests that a scroll-generating touch-event is marked as such.
2498 TEST_F(WindowEventDispatcherTest, TouchMovesMarkedWhenCausingScroll) {
2499 EventFilterRecorder recorder;
2500 root_window()->AddPreTargetHandler(&recorder);
2502 const gfx::Point location(20, 20);
2503 ui::TouchEvent press(
2504 ui::ET_TOUCH_PRESSED, location, 0, ui::EventTimeForNow());
2505 DispatchEventUsingWindowDispatcher(&press);
2506 EXPECT_FALSE(recorder.LastTouchMayCauseScrolling());
2507 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_PRESSED));
2508 recorder.Reset();
2510 ui::TouchEvent move(ui::ET_TOUCH_MOVED,
2511 location + gfx::Vector2d(100, 100),
2513 ui::EventTimeForNow());
2514 DispatchEventUsingWindowDispatcher(&move);
2515 EXPECT_TRUE(recorder.LastTouchMayCauseScrolling());
2516 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
2517 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
2518 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
2519 recorder.Reset();
2521 ui::TouchEvent move2(ui::ET_TOUCH_MOVED,
2522 location + gfx::Vector2d(200, 200),
2524 ui::EventTimeForNow());
2525 DispatchEventUsingWindowDispatcher(&move2);
2526 EXPECT_TRUE(recorder.LastTouchMayCauseScrolling());
2527 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
2528 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
2529 recorder.Reset();
2531 // Delay the release to avoid fling generation.
2532 ui::TouchEvent release(
2533 ui::ET_TOUCH_RELEASED,
2534 location + gfx::Vector2dF(200, 200),
2536 ui::EventTimeForNow() + base::TimeDelta::FromSeconds(1));
2537 DispatchEventUsingWindowDispatcher(&release);
2538 EXPECT_FALSE(recorder.LastTouchMayCauseScrolling());
2539 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_RELEASED));
2540 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_END));
2542 root_window()->RemovePreTargetHandler(&recorder);
2545 // OnCursorMovedToRootLocation() is sometimes called instead of
2546 // WindowTreeHost::MoveCursorTo() when the cursor did not move but the
2547 // cursor's position in root coordinates has changed (e.g. when the displays's
2548 // scale factor changed). Test that hover effects are properly updated.
2549 TEST_F(WindowEventDispatcherTest, OnCursorMovedToRootLocationUpdatesHover) {
2550 WindowEventDispatcher* dispatcher = host()->dispatcher();
2552 scoped_ptr<Window> w(CreateNormalWindow(1, root_window(), nullptr));
2553 w->SetBounds(gfx::Rect(20, 20, 20, 20));
2554 w->Show();
2556 // Move the cursor off of |w|.
2557 dispatcher->OnCursorMovedToRootLocation(gfx::Point(100, 100));
2559 EventFilterRecorder recorder;
2560 w->AddPreTargetHandler(&recorder);
2561 dispatcher->OnCursorMovedToRootLocation(gfx::Point(22, 22));
2562 RunAllPendingInMessageLoop();
2563 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_MOUSE_ENTERED));
2564 recorder.Reset();
2566 // The cursor should not be over |w| after changing the device scale factor to
2567 // 2x. A ET_MOUSE_EXITED event should have been sent to |w|.
2568 test_screen()->SetDeviceScaleFactor(2.f);
2569 dispatcher->OnCursorMovedToRootLocation(gfx::Point(11, 11));
2570 RunAllPendingInMessageLoop();
2571 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_MOUSE_EXITED));
2573 w->RemovePreTargetHandler(&recorder);
2576 } // namespace aura