MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / ui / aura / window_event_dispatcher_unittest.cc
bloba48f0dda02bb2ddc5dc2237dfe46e3df73f1af90
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());
357 root_window()->RemovePreTargetHandler(&handler);
360 TEST_F(WindowEventDispatcherTest, NoDelegateWindowReceivesKeyEvents) {
361 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
362 w1->Show();
363 w1->Focus();
365 ui::test::TestEventHandler handler;
366 w1->AddPreTargetHandler(&handler);
367 ui::KeyEvent key_press(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
368 DispatchEventUsingWindowDispatcher(&key_press);
369 EXPECT_TRUE(key_press.handled());
370 EXPECT_EQ(1, handler.num_key_events());
372 w1->RemovePreTargetHandler(&handler);
375 // Tests that touch-events that are beyond the bounds of the root-window do get
376 // propagated to the event filters correctly with the root as the target.
377 TEST_F(WindowEventDispatcherTest, TouchEventsOutsideBounds) {
378 ui::test::TestEventHandler handler;
379 root_window()->AddPreTargetHandler(&handler);
381 gfx::Point position = root_window()->bounds().origin();
382 position.Offset(-10, -10);
383 ui::TouchEvent press(
384 ui::ET_TOUCH_PRESSED, position, 0, ui::EventTimeForNow());
385 DispatchEventUsingWindowDispatcher(&press);
386 EXPECT_EQ(1, handler.num_touch_events());
388 position = root_window()->bounds().origin();
389 position.Offset(root_window()->bounds().width() + 10,
390 root_window()->bounds().height() + 10);
391 ui::TouchEvent release(
392 ui::ET_TOUCH_RELEASED, position, 0, ui::EventTimeForNow());
393 DispatchEventUsingWindowDispatcher(&release);
394 EXPECT_EQ(2, handler.num_touch_events());
395 root_window()->RemovePreTargetHandler(&handler);
398 // Tests that scroll events are dispatched correctly.
399 TEST_F(WindowEventDispatcherTest, ScrollEventDispatch) {
400 base::TimeDelta now = ui::EventTimeForNow();
401 ui::test::TestEventHandler handler;
402 root_window()->AddPreTargetHandler(&handler);
404 test::TestWindowDelegate delegate;
405 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
406 w1->SetBounds(gfx::Rect(20, 20, 40, 40));
408 // A scroll event on the root-window itself is dispatched.
409 ui::ScrollEvent scroll1(ui::ET_SCROLL,
410 gfx::Point(10, 10),
411 now,
413 0, -10,
414 0, -10,
416 DispatchEventUsingWindowDispatcher(&scroll1);
417 EXPECT_EQ(1, handler.num_scroll_events());
419 // Scroll event on a window should be dispatched properly.
420 ui::ScrollEvent scroll2(ui::ET_SCROLL,
421 gfx::Point(25, 30),
422 now,
424 -10, 0,
425 -10, 0,
427 DispatchEventUsingWindowDispatcher(&scroll2);
428 EXPECT_EQ(2, handler.num_scroll_events());
429 root_window()->RemovePreTargetHandler(&handler);
432 namespace {
434 // FilterFilter that tracks the types of events it's seen.
435 class EventFilterRecorder : public ui::EventHandler {
436 public:
437 typedef std::vector<ui::EventType> Events;
438 typedef std::vector<gfx::Point> EventLocations;
439 typedef std::vector<int> EventFlags;
441 EventFilterRecorder()
442 : wait_until_event_(ui::ET_UNKNOWN),
443 last_touch_may_cause_scrolling_(false) {
446 const Events& events() const { return events_; }
448 const EventLocations& mouse_locations() const { return mouse_locations_; }
449 gfx::Point mouse_location(int i) const { return mouse_locations_[i]; }
450 const EventLocations& touch_locations() const { return touch_locations_; }
451 const EventLocations& gesture_locations() const { return gesture_locations_; }
452 const EventFlags& mouse_event_flags() const { return mouse_event_flags_; }
454 void WaitUntilReceivedEvent(ui::EventType type) {
455 wait_until_event_ = type;
456 run_loop_.reset(new base::RunLoop());
457 run_loop_->Run();
460 Events GetAndResetEvents() {
461 Events events = events_;
462 Reset();
463 return events;
466 void Reset() {
467 events_.clear();
468 mouse_locations_.clear();
469 touch_locations_.clear();
470 gesture_locations_.clear();
471 mouse_event_flags_.clear();
472 last_touch_may_cause_scrolling_ = false;
475 // ui::EventHandler overrides:
476 void OnEvent(ui::Event* event) override {
477 ui::EventHandler::OnEvent(event);
478 events_.push_back(event->type());
479 if (wait_until_event_ == event->type() && run_loop_) {
480 run_loop_->Quit();
481 wait_until_event_ = ui::ET_UNKNOWN;
485 void OnMouseEvent(ui::MouseEvent* event) override {
486 mouse_locations_.push_back(event->location());
487 mouse_event_flags_.push_back(event->flags());
490 void OnTouchEvent(ui::TouchEvent* event) override {
491 touch_locations_.push_back(event->location());
492 last_touch_may_cause_scrolling_ = event->may_cause_scrolling();
495 void OnGestureEvent(ui::GestureEvent* event) override {
496 gesture_locations_.push_back(event->location());
499 bool HasReceivedEvent(ui::EventType type) {
500 return std::find(events_.begin(), events_.end(), type) != events_.end();
503 bool LastTouchMayCauseScrolling() const {
504 return last_touch_may_cause_scrolling_;
507 private:
508 scoped_ptr<base::RunLoop> run_loop_;
509 ui::EventType wait_until_event_;
511 Events events_;
512 EventLocations mouse_locations_;
513 EventLocations touch_locations_;
514 EventLocations gesture_locations_;
515 EventFlags mouse_event_flags_;
516 bool last_touch_may_cause_scrolling_;
518 DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder);
521 // Converts an EventType to a string.
522 std::string EventTypeToString(ui::EventType type) {
523 switch (type) {
524 case ui::ET_TOUCH_RELEASED:
525 return "TOUCH_RELEASED";
527 case ui::ET_TOUCH_CANCELLED:
528 return "TOUCH_CANCELLED";
530 case ui::ET_TOUCH_PRESSED:
531 return "TOUCH_PRESSED";
533 case ui::ET_TOUCH_MOVED:
534 return "TOUCH_MOVED";
536 case ui::ET_MOUSE_PRESSED:
537 return "MOUSE_PRESSED";
539 case ui::ET_MOUSE_DRAGGED:
540 return "MOUSE_DRAGGED";
542 case ui::ET_MOUSE_RELEASED:
543 return "MOUSE_RELEASED";
545 case ui::ET_MOUSE_MOVED:
546 return "MOUSE_MOVED";
548 case ui::ET_MOUSE_ENTERED:
549 return "MOUSE_ENTERED";
551 case ui::ET_MOUSE_EXITED:
552 return "MOUSE_EXITED";
554 case ui::ET_GESTURE_SCROLL_BEGIN:
555 return "GESTURE_SCROLL_BEGIN";
557 case ui::ET_GESTURE_SCROLL_END:
558 return "GESTURE_SCROLL_END";
560 case ui::ET_GESTURE_SCROLL_UPDATE:
561 return "GESTURE_SCROLL_UPDATE";
563 case ui::ET_GESTURE_PINCH_BEGIN:
564 return "GESTURE_PINCH_BEGIN";
566 case ui::ET_GESTURE_PINCH_END:
567 return "GESTURE_PINCH_END";
569 case ui::ET_GESTURE_PINCH_UPDATE:
570 return "GESTURE_PINCH_UPDATE";
572 case ui::ET_GESTURE_TAP:
573 return "GESTURE_TAP";
575 case ui::ET_GESTURE_TAP_DOWN:
576 return "GESTURE_TAP_DOWN";
578 case ui::ET_GESTURE_TAP_CANCEL:
579 return "GESTURE_TAP_CANCEL";
581 case ui::ET_GESTURE_SHOW_PRESS:
582 return "GESTURE_SHOW_PRESS";
584 case ui::ET_GESTURE_BEGIN:
585 return "GESTURE_BEGIN";
587 case ui::ET_GESTURE_END:
588 return "GESTURE_END";
590 default:
591 // We should explicitly require each event type.
592 NOTREACHED() << "Received unexpected event: " << type;
593 break;
595 return "";
598 std::string EventTypesToString(const EventFilterRecorder::Events& events) {
599 std::string result;
600 for (size_t i = 0; i < events.size(); ++i) {
601 if (i != 0)
602 result += " ";
603 result += EventTypeToString(events[i]);
605 return result;
608 } // namespace
610 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
611 #define MAYBE(x) DISABLED_##x
612 #else
613 #define MAYBE(x) x
614 #endif
616 // Verifies a repost mouse event targets the window with capture (if there is
617 // one).
618 // Flaky on 32-bit Windows bots. http://crbug.com/388290
619 TEST_F(WindowEventDispatcherTest, MAYBE(RepostTargetsCaptureWindow)) {
620 // Set capture on |window| generate a mouse event (that is reposted) and not
621 // over |window| and verify |window| gets it (|window| gets it because it has
622 // capture).
623 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
624 EventFilterRecorder recorder;
625 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
626 window->SetBounds(gfx::Rect(20, 20, 40, 30));
627 window->AddPreTargetHandler(&recorder);
628 window->SetCapture();
629 const ui::MouseEvent press_event(
630 ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
631 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
632 host()->dispatcher()->RepostEvent(press_event);
633 RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
634 // Mouse moves/enters may be generated. We only care about a pressed.
635 EXPECT_TRUE(EventTypesToString(recorder.events()).find("MOUSE_PRESSED") !=
636 std::string::npos) << EventTypesToString(recorder.events());
639 TEST_F(WindowEventDispatcherTest, MouseMovesHeld) {
640 EventFilterRecorder recorder;
641 root_window()->AddPreTargetHandler(&recorder);
643 test::TestWindowDelegate delegate;
644 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
645 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
647 ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0),
648 gfx::Point(0, 0), ui::EventTimeForNow(), 0,
650 DispatchEventUsingWindowDispatcher(&mouse_move_event);
651 // Discard MOUSE_ENTER.
652 recorder.Reset();
654 host()->dispatcher()->HoldPointerMoves();
656 // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
657 ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
658 gfx::Point(0, 0), ui::EventTimeForNow(), 0,
660 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
661 EXPECT_TRUE(recorder.events().empty());
663 // Check that we do dispatch the held MOUSE_DRAGGED event before another type
664 // of event.
665 ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
666 gfx::Point(0, 0), ui::EventTimeForNow(), 0,
668 DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
669 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
670 EventTypesToString(recorder.events()));
671 recorder.Reset();
673 // Check that we coalesce held MOUSE_DRAGGED events. Note that here (and
674 // elsewhere in this test) we re-define each event prior to dispatch so that
675 // it has the correct state (phase, handled, target, etc.).
676 mouse_dragged_event =
677 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
678 ui::EventTimeForNow(), 0, 0);
679 ui::MouseEvent mouse_dragged_event2(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
680 gfx::Point(10, 10), ui::EventTimeForNow(),
681 0, 0);
682 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
683 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
684 EXPECT_TRUE(recorder.events().empty());
685 mouse_pressed_event =
686 ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
687 ui::EventTimeForNow(), 0, 0);
688 DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
689 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
690 EventTypesToString(recorder.events()));
691 recorder.Reset();
693 // Check that on ReleasePointerMoves, held events are not dispatched
694 // immediately, but posted instead.
695 mouse_dragged_event =
696 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
697 ui::EventTimeForNow(), 0, 0);
698 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
699 host()->dispatcher()->ReleasePointerMoves();
700 EXPECT_TRUE(recorder.events().empty());
701 RunAllPendingInMessageLoop();
702 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
703 recorder.Reset();
705 // However if another message comes in before the dispatch of the posted
706 // event, check that the posted event is dispatched before this new event.
707 host()->dispatcher()->HoldPointerMoves();
708 mouse_dragged_event =
709 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
710 ui::EventTimeForNow(), 0, 0);
711 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
712 host()->dispatcher()->ReleasePointerMoves();
713 mouse_pressed_event =
714 ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
715 ui::EventTimeForNow(), 0, 0);
716 DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
717 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
718 EventTypesToString(recorder.events()));
719 recorder.Reset();
720 RunAllPendingInMessageLoop();
721 EXPECT_TRUE(recorder.events().empty());
723 // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
724 // them.
725 host()->dispatcher()->HoldPointerMoves();
726 mouse_dragged_event =
727 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
728 ui::EventTimeForNow(), 0, 0);
729 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
730 host()->dispatcher()->ReleasePointerMoves();
731 mouse_dragged_event2 =
732 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
733 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
734 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
735 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
736 recorder.Reset();
737 RunAllPendingInMessageLoop();
738 EXPECT_TRUE(recorder.events().empty());
740 // Check that synthetic mouse move event has a right location when issued
741 // while holding pointer moves.
742 mouse_dragged_event =
743 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
744 ui::EventTimeForNow(), 0, 0);
745 mouse_dragged_event2 =
746 ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
747 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
748 ui::MouseEvent mouse_dragged_event3(ui::ET_MOUSE_DRAGGED, gfx::Point(28, 28),
749 gfx::Point(28, 28), ui::EventTimeForNow(),
750 0, 0);
751 host()->dispatcher()->HoldPointerMoves();
752 DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
753 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
754 window->SetBounds(gfx::Rect(15, 15, 80, 80));
755 DispatchEventUsingWindowDispatcher(&mouse_dragged_event3);
756 RunAllPendingInMessageLoop();
757 EXPECT_TRUE(recorder.events().empty());
758 host()->dispatcher()->ReleasePointerMoves();
759 RunAllPendingInMessageLoop();
760 EXPECT_EQ("MOUSE_MOVED", EventTypesToString(recorder.events()));
761 EXPECT_EQ(gfx::Point(13, 13), recorder.mouse_location(0));
762 recorder.Reset();
763 root_window()->RemovePreTargetHandler(&recorder);
766 TEST_F(WindowEventDispatcherTest, TouchMovesHeld) {
767 EventFilterRecorder recorder;
768 root_window()->AddPreTargetHandler(&recorder);
770 test::TestWindowDelegate delegate;
771 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
772 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
774 // Starting the touch and throwing out the first few events, since the system
775 // is going to generate synthetic mouse events that are not relevant to the
776 // test.
777 ui::TouchEvent touch_pressed_event(
778 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, ui::EventTimeForNow());
779 DispatchEventUsingWindowDispatcher(&touch_pressed_event);
780 recorder.WaitUntilReceivedEvent(ui::ET_GESTURE_SHOW_PRESS);
781 recorder.Reset();
783 host()->dispatcher()->HoldPointerMoves();
785 // Check that we don't immediately dispatch the TOUCH_MOVED event.
786 ui::TouchEvent touch_moved_event(
787 ui::ET_TOUCH_MOVED, gfx::Point(10, 10), 0, ui::EventTimeForNow());
788 ui::TouchEvent touch_moved_event2(
789 ui::ET_TOUCH_MOVED, gfx::Point(11, 10), 0, ui::EventTimeForNow());
790 ui::TouchEvent touch_moved_event3(
791 ui::ET_TOUCH_MOVED, gfx::Point(12, 10), 0, ui::EventTimeForNow());
793 DispatchEventUsingWindowDispatcher(&touch_moved_event);
794 EXPECT_TRUE(recorder.events().empty());
796 // Check that on ReleasePointerMoves, held events are not dispatched
797 // immediately, but posted instead.
798 DispatchEventUsingWindowDispatcher(&touch_moved_event2);
799 host()->dispatcher()->ReleasePointerMoves();
800 EXPECT_TRUE(recorder.events().empty());
802 RunAllPendingInMessageLoop();
803 EXPECT_EQ("TOUCH_MOVED", EventTypesToString(recorder.events()));
804 recorder.Reset();
806 // If another touch event occurs then the held touch should be dispatched
807 // immediately before it.
808 ui::TouchEvent touch_released_event(
809 ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), 0, ui::EventTimeForNow());
810 recorder.Reset();
811 host()->dispatcher()->HoldPointerMoves();
812 DispatchEventUsingWindowDispatcher(&touch_moved_event3);
813 DispatchEventUsingWindowDispatcher(&touch_released_event);
814 EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP GESTURE_END",
815 EventTypesToString(recorder.events()));
816 recorder.Reset();
817 host()->dispatcher()->ReleasePointerMoves();
818 RunAllPendingInMessageLoop();
819 EXPECT_TRUE(recorder.events().empty());
820 root_window()->RemovePreTargetHandler(&recorder);
823 // Tests that mouse move event has a right location
824 // when there isn't the target window
825 TEST_F(WindowEventDispatcherTest, MouseEventWithoutTargetWindow) {
826 EventFilterRecorder recorder_first;
827 EventFilterRecorder recorder_second;
829 test::TestWindowDelegate delegate;
830 scoped_ptr<aura::Window> window_first(CreateTestWindowWithDelegate(
831 &delegate, 1, gfx::Rect(20, 10, 10, 20), root_window()));
832 window_first->Show();
833 window_first->AddPreTargetHandler(&recorder_first);
835 scoped_ptr<aura::Window> window_second(CreateTestWindowWithDelegate(
836 &delegate, 2, gfx::Rect(20, 30, 10, 20), root_window()));
837 window_second->Show();
838 window_second->AddPreTargetHandler(&recorder_second);
840 const gfx::Point event_location(22, 33);
841 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
842 ui::EventTimeForNow(), 0, 0);
843 DispatchEventUsingWindowDispatcher(&mouse);
845 EXPECT_TRUE(recorder_first.events().empty());
846 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED",
847 EventTypesToString(recorder_second.events()));
848 ASSERT_EQ(2u, recorder_second.mouse_locations().size());
849 EXPECT_EQ(gfx::Point(2, 3).ToString(),
850 recorder_second.mouse_locations()[0].ToString());
853 // Tests that a mouse exit is dispatched to the last mouse location when
854 // the window is hiddden.
855 TEST_F(WindowEventDispatcherTest, DispatchMouseExitWhenHidingWindow) {
856 EventFilterRecorder recorder;
858 test::TestWindowDelegate delegate;
859 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
860 &delegate, 1, gfx::Rect(10, 10, 50, 50), root_window()));
861 window->Show();
862 window->AddPreTargetHandler(&recorder);
864 // Dispatch a mouse move event into the window.
865 const gfx::Point event_location(22, 33);
866 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
867 ui::EventTimeForNow(), 0, 0);
868 DispatchEventUsingWindowDispatcher(&mouse);
869 EXPECT_FALSE(recorder.events().empty());
870 recorder.Reset();
872 // Hide the window and verify a mouse exit event's location.
873 window->Hide();
874 EXPECT_FALSE(recorder.events().empty());
875 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder.events()));
876 ASSERT_EQ(1u, recorder.mouse_locations().size());
877 EXPECT_EQ(gfx::Point(12, 23).ToString(),
878 recorder.mouse_locations()[0].ToString());
881 // Verifies that a direct call to ProcessedTouchEvent() does not cause a crash.
882 TEST_F(WindowEventDispatcherTest, CallToProcessedTouchEvent) {
883 test::TestWindowDelegate delegate;
884 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
885 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
887 host()->dispatcher()->ProcessedTouchEvent(0, window.get(), ui::ER_UNHANDLED);
890 // This event handler requests the dispatcher to start holding pointer-move
891 // events when it receives the first scroll-update gesture.
892 class HoldPointerOnScrollHandler : public ui::test::TestEventHandler {
893 public:
894 HoldPointerOnScrollHandler(WindowEventDispatcher* dispatcher,
895 EventFilterRecorder* filter)
896 : dispatcher_(dispatcher),
897 filter_(filter),
898 holding_moves_(false) {}
899 ~HoldPointerOnScrollHandler() override {}
901 private:
902 // ui::test::TestEventHandler:
903 void OnGestureEvent(ui::GestureEvent* gesture) override {
904 if (!holding_moves_ && gesture->type() == ui::ET_GESTURE_SCROLL_UPDATE) {
905 holding_moves_ = true;
906 dispatcher_->HoldPointerMoves();
907 filter_->Reset();
908 } else if (gesture->type() == ui::ET_GESTURE_SCROLL_END) {
909 dispatcher_->ReleasePointerMoves();
910 holding_moves_ = false;
914 WindowEventDispatcher* dispatcher_;
915 EventFilterRecorder* filter_;
916 bool holding_moves_;
918 DISALLOW_COPY_AND_ASSIGN(HoldPointerOnScrollHandler);
921 // Tests that touch-move events don't contribute to an in-progress scroll
922 // gesture if touch-move events are being held by the dispatcher.
923 TEST_F(WindowEventDispatcherTest, TouchMovesHeldOnScroll) {
924 EventFilterRecorder recorder;
925 root_window()->AddPreTargetHandler(&recorder);
926 test::TestWindowDelegate delegate;
927 HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
928 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
929 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
930 window->AddPreTargetHandler(&handler);
932 ui::test::EventGenerator generator(root_window());
933 generator.GestureScrollSequence(
934 gfx::Point(60, 60), gfx::Point(10, 60),
935 base::TimeDelta::FromMilliseconds(100), 25);
937 // |handler| will have reset |filter| and started holding the touch-move
938 // events when scrolling started. At the end of the scroll (i.e. upon
939 // touch-release), the held touch-move event will have been dispatched first,
940 // along with the subsequent events (i.e. touch-release, scroll-end, and
941 // gesture-end).
942 const EventFilterRecorder::Events& events = recorder.events();
943 EXPECT_EQ(
944 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
945 "GESTURE_SCROLL_END GESTURE_END",
946 EventTypesToString(events));
947 ASSERT_EQ(2u, recorder.touch_locations().size());
948 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
949 recorder.touch_locations()[0].ToString());
950 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
951 recorder.touch_locations()[1].ToString());
952 root_window()->RemovePreTargetHandler(&recorder);
955 // Tests that a 'held' touch-event does contribute to gesture event when it is
956 // dispatched.
957 TEST_F(WindowEventDispatcherTest, HeldTouchMoveContributesToGesture) {
958 EventFilterRecorder recorder;
959 root_window()->AddPreTargetHandler(&recorder);
961 const gfx::Point location(20, 20);
962 ui::TouchEvent press(
963 ui::ET_TOUCH_PRESSED, location, 0, ui::EventTimeForNow());
964 DispatchEventUsingWindowDispatcher(&press);
965 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_PRESSED));
966 recorder.Reset();
968 host()->dispatcher()->HoldPointerMoves();
970 ui::TouchEvent move(ui::ET_TOUCH_MOVED,
971 location + gfx::Vector2d(100, 100),
973 ui::EventTimeForNow());
974 DispatchEventUsingWindowDispatcher(&move);
975 EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
976 EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
977 recorder.Reset();
979 host()->dispatcher()->ReleasePointerMoves();
980 EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
981 RunAllPendingInMessageLoop();
982 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
983 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
984 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
986 root_window()->RemovePreTargetHandler(&recorder);
989 // Tests that synthetic mouse events are ignored when mouse
990 // events are disabled.
991 TEST_F(WindowEventDispatcherTest, DispatchSyntheticMouseEvents) {
992 EventFilterRecorder recorder;
993 root_window()->AddPreTargetHandler(&recorder);
995 test::TestWindowDelegate delegate;
996 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
997 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
998 window->Show();
999 window->SetCapture();
1001 test::TestCursorClient cursor_client(root_window());
1003 // Dispatch a non-synthetic mouse event when mouse events are enabled.
1004 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
1005 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
1006 DispatchEventUsingWindowDispatcher(&mouse1);
1007 EXPECT_FALSE(recorder.events().empty());
1008 recorder.Reset();
1010 // Dispatch a synthetic mouse event when mouse events are enabled.
1011 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
1012 gfx::Point(10, 10), ui::EventTimeForNow(),
1013 ui::EF_IS_SYNTHESIZED, 0);
1014 DispatchEventUsingWindowDispatcher(&mouse2);
1015 EXPECT_FALSE(recorder.events().empty());
1016 recorder.Reset();
1018 // Dispatch a synthetic mouse event when mouse events are disabled.
1019 cursor_client.DisableMouseEvents();
1020 DispatchEventUsingWindowDispatcher(&mouse2);
1021 EXPECT_TRUE(recorder.events().empty());
1022 root_window()->RemovePreTargetHandler(&recorder);
1025 // Tests that a mouse-move event is not synthesized when a mouse-button is down.
1026 TEST_F(WindowEventDispatcherTest, DoNotSynthesizeWhileButtonDown) {
1027 EventFilterRecorder recorder;
1028 test::TestWindowDelegate delegate;
1029 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1030 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1031 window->Show();
1033 window->AddPreTargetHandler(&recorder);
1034 // Dispatch a non-synthetic mouse event when mouse events are enabled.
1035 ui::MouseEvent mouse1(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1036 gfx::Point(10, 10), ui::EventTimeForNow(),
1037 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1038 DispatchEventUsingWindowDispatcher(&mouse1);
1039 ASSERT_EQ(1u, recorder.events().size());
1040 EXPECT_EQ(ui::ET_MOUSE_PRESSED, recorder.events()[0]);
1041 window->RemovePreTargetHandler(&recorder);
1042 recorder.Reset();
1044 // Move |window| away from underneath the cursor.
1045 root_window()->AddPreTargetHandler(&recorder);
1046 window->SetBounds(gfx::Rect(30, 30, 100, 100));
1047 EXPECT_TRUE(recorder.events().empty());
1048 RunAllPendingInMessageLoop();
1049 EXPECT_TRUE(recorder.events().empty());
1050 root_window()->RemovePreTargetHandler(&recorder);
1053 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
1054 #define MAYBE(x) DISABLED_##x
1055 #else
1056 #define MAYBE(x) x
1057 #endif
1059 // Tests synthetic mouse events generated when window bounds changes such that
1060 // the cursor previously outside the window becomes inside, or vice versa.
1061 // Do not synthesize events if the window ignores events or is invisible.
1062 // Flaky on 32-bit Windows bots. http://crbug.com/388272
1063 TEST_F(WindowEventDispatcherTest,
1064 MAYBE(SynthesizeMouseEventsOnWindowBoundsChanged)) {
1065 test::TestWindowDelegate delegate;
1066 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1067 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1068 window->Show();
1069 window->SetCapture();
1071 EventFilterRecorder recorder;
1072 window->AddPreTargetHandler(&recorder);
1074 // Dispatch a non-synthetic mouse event to place cursor inside window bounds.
1075 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
1076 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
1077 DispatchEventUsingWindowDispatcher(&mouse);
1078 EXPECT_FALSE(recorder.events().empty());
1079 recorder.Reset();
1081 // Update the window bounds so that cursor is now outside the window.
1082 // This should trigger a synthetic MOVED event.
1083 gfx::Rect bounds1(20, 20, 100, 100);
1084 window->SetBounds(bounds1);
1085 RunAllPendingInMessageLoop();
1086 ASSERT_FALSE(recorder.events().empty());
1087 ASSERT_FALSE(recorder.mouse_event_flags().empty());
1088 EXPECT_EQ(ui::ET_MOUSE_MOVED, recorder.events().back());
1089 EXPECT_EQ(ui::EF_IS_SYNTHESIZED, recorder.mouse_event_flags().back());
1090 recorder.Reset();
1092 // Set window to ignore events.
1093 window->set_ignore_events(true);
1095 // Update the window bounds so that cursor is back inside the window.
1096 // This should not trigger a synthetic event.
1097 gfx::Rect bounds2(5, 5, 100, 100);
1098 window->SetBounds(bounds2);
1099 RunAllPendingInMessageLoop();
1100 EXPECT_TRUE(recorder.events().empty());
1101 recorder.Reset();
1103 // Set window to accept events but invisible.
1104 window->set_ignore_events(false);
1105 window->Hide();
1106 recorder.Reset();
1108 // Update the window bounds so that cursor is outside the window.
1109 // This should not trigger a synthetic event.
1110 window->SetBounds(bounds1);
1111 RunAllPendingInMessageLoop();
1112 EXPECT_TRUE(recorder.events().empty());
1115 // Tests that a mouse exit is dispatched to the last known cursor location
1116 // when the cursor becomes invisible.
1117 TEST_F(WindowEventDispatcherTest, DispatchMouseExitWhenCursorHidden) {
1118 EventFilterRecorder recorder;
1119 root_window()->AddPreTargetHandler(&recorder);
1121 test::TestWindowDelegate delegate;
1122 gfx::Point window_origin(7, 18);
1123 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1124 &delegate, 1234, gfx::Rect(window_origin, gfx::Size(100, 100)),
1125 root_window()));
1126 window->Show();
1128 // Dispatch a mouse move event into the window.
1129 gfx::Point mouse_location(gfx::Point(15, 25));
1130 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location, mouse_location,
1131 ui::EventTimeForNow(), 0, 0);
1132 EXPECT_TRUE(recorder.events().empty());
1133 DispatchEventUsingWindowDispatcher(&mouse1);
1134 EXPECT_FALSE(recorder.events().empty());
1135 recorder.Reset();
1137 // Hide the cursor and verify a mouse exit was dispatched.
1138 host()->OnCursorVisibilityChanged(false);
1139 EXPECT_FALSE(recorder.events().empty());
1140 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder.events()));
1142 // Verify the mouse exit was dispatched at the correct location
1143 // (in the correct coordinate space).
1144 int translated_x = mouse_location.x() - window_origin.x();
1145 int translated_y = mouse_location.y() - window_origin.y();
1146 gfx::Point translated_point(translated_x, translated_y);
1147 EXPECT_EQ(recorder.mouse_location(0).ToString(), translated_point.ToString());
1148 root_window()->RemovePreTargetHandler(&recorder);
1151 // Tests that a synthetic mouse exit is dispatched to the last known cursor
1152 // location after mouse events are disabled on the cursor client.
1153 TEST_F(WindowEventDispatcherTest,
1154 DispatchSyntheticMouseExitAfterMouseEventsDisabled) {
1155 EventFilterRecorder recorder;
1156 root_window()->AddPreTargetHandler(&recorder);
1158 test::TestWindowDelegate delegate;
1159 gfx::Point window_origin(7, 18);
1160 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1161 &delegate, 1234, gfx::Rect(window_origin, gfx::Size(100, 100)),
1162 root_window()));
1163 window->Show();
1165 // Dispatch a mouse move event into the window.
1166 gfx::Point mouse_location(gfx::Point(15, 25));
1167 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location, mouse_location,
1168 ui::EventTimeForNow(), 0, 0);
1169 EXPECT_TRUE(recorder.events().empty());
1170 DispatchEventUsingWindowDispatcher(&mouse1);
1171 EXPECT_FALSE(recorder.events().empty());
1172 recorder.Reset();
1174 test::TestCursorClient cursor_client(root_window());
1175 cursor_client.DisableMouseEvents();
1177 gfx::Point mouse_exit_location(gfx::Point(150, 150));
1178 ui::MouseEvent mouse2(ui::ET_MOUSE_EXITED, gfx::Point(150, 150),
1179 gfx::Point(150, 150), ui::EventTimeForNow(),
1180 ui::EF_IS_SYNTHESIZED, 0);
1181 DispatchEventUsingWindowDispatcher(&mouse2);
1183 EXPECT_FALSE(recorder.events().empty());
1184 // We get the mouse exited event twice in our filter. Once during the
1185 // predispatch phase and during the actual dispatch.
1186 EXPECT_EQ("MOUSE_EXITED MOUSE_EXITED", EventTypesToString(recorder.events()));
1188 // Verify the mouse exit was dispatched at the correct location
1189 // (in the correct coordinate space).
1190 int translated_x = mouse_exit_location.x() - window_origin.x();
1191 int translated_y = mouse_exit_location.y() - window_origin.y();
1192 gfx::Point translated_point(translated_x, translated_y);
1193 EXPECT_EQ(recorder.mouse_location(0).ToString(), translated_point.ToString());
1194 root_window()->RemovePreTargetHandler(&recorder);
1197 class DeletingEventFilter : public ui::EventHandler {
1198 public:
1199 DeletingEventFilter()
1200 : delete_during_pre_handle_(false) {}
1201 ~DeletingEventFilter() override {}
1203 void Reset(bool delete_during_pre_handle) {
1204 delete_during_pre_handle_ = delete_during_pre_handle;
1207 private:
1208 // Overridden from ui::EventHandler:
1209 void OnKeyEvent(ui::KeyEvent* event) override {
1210 if (delete_during_pre_handle_)
1211 delete event->target();
1214 void OnMouseEvent(ui::MouseEvent* event) override {
1215 if (delete_during_pre_handle_)
1216 delete event->target();
1219 bool delete_during_pre_handle_;
1221 DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter);
1224 class DeletingWindowDelegate : public test::TestWindowDelegate {
1225 public:
1226 DeletingWindowDelegate()
1227 : window_(NULL),
1228 delete_during_handle_(false),
1229 got_event_(false) {}
1230 ~DeletingWindowDelegate() override {}
1232 void Reset(Window* window, bool delete_during_handle) {
1233 window_ = window;
1234 delete_during_handle_ = delete_during_handle;
1235 got_event_ = false;
1237 bool got_event() const { return got_event_; }
1239 private:
1240 // Overridden from WindowDelegate:
1241 void OnKeyEvent(ui::KeyEvent* event) override {
1242 if (delete_during_handle_)
1243 delete window_;
1244 got_event_ = true;
1247 void OnMouseEvent(ui::MouseEvent* event) override {
1248 if (delete_during_handle_)
1249 delete window_;
1250 got_event_ = true;
1253 Window* window_;
1254 bool delete_during_handle_;
1255 bool got_event_;
1257 DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate);
1260 TEST_F(WindowEventDispatcherTest, DeleteWindowDuringDispatch) {
1261 // Verifies that we can delete a window during each phase of event handling.
1262 // Deleting the window should not cause a crash, only prevent further
1263 // processing from occurring.
1264 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1265 DeletingWindowDelegate d11;
1266 Window* w11 = CreateNormalWindow(11, w1.get(), &d11);
1267 WindowTracker tracker;
1268 DeletingEventFilter w1_filter;
1269 w1->AddPreTargetHandler(&w1_filter);
1270 client::GetFocusClient(w1.get())->FocusWindow(w11);
1272 ui::test::EventGenerator generator(root_window(), w11);
1274 // First up, no one deletes anything.
1275 tracker.Add(w11);
1276 d11.Reset(w11, false);
1278 generator.PressLeftButton();
1279 EXPECT_TRUE(tracker.Contains(w11));
1280 EXPECT_TRUE(d11.got_event());
1281 generator.ReleaseLeftButton();
1283 // Delegate deletes w11. This will prevent the post-handle step from applying.
1284 w1_filter.Reset(false);
1285 d11.Reset(w11, true);
1286 generator.PressKey(ui::VKEY_A, 0);
1287 EXPECT_FALSE(tracker.Contains(w11));
1288 EXPECT_TRUE(d11.got_event());
1290 // Pre-handle step deletes w11. This will prevent the delegate and the post-
1291 // handle steps from applying.
1292 w11 = CreateNormalWindow(11, w1.get(), &d11);
1293 w1_filter.Reset(true);
1294 d11.Reset(w11, false);
1295 generator.PressLeftButton();
1296 EXPECT_FALSE(tracker.Contains(w11));
1297 EXPECT_FALSE(d11.got_event());
1300 namespace {
1302 // A window delegate that detaches the parent of the target's parent window when
1303 // it receives a tap event.
1304 class DetachesParentOnTapDelegate : public test::TestWindowDelegate {
1305 public:
1306 DetachesParentOnTapDelegate() {}
1307 ~DetachesParentOnTapDelegate() override {}
1309 private:
1310 void OnGestureEvent(ui::GestureEvent* event) override {
1311 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
1312 event->SetHandled();
1313 return;
1316 if (event->type() == ui::ET_GESTURE_TAP) {
1317 Window* parent = static_cast<Window*>(event->target())->parent();
1318 parent->parent()->RemoveChild(parent);
1319 event->SetHandled();
1323 DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate);
1326 } // namespace
1328 // Tests that the gesture recognizer is reset for all child windows when a
1329 // window hides. No expectations, just checks that the test does not crash.
1330 TEST_F(WindowEventDispatcherTest,
1331 GestureRecognizerResetsTargetWhenParentHides) {
1332 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1333 DetachesParentOnTapDelegate delegate;
1334 scoped_ptr<Window> parent(CreateNormalWindow(22, w1.get(), NULL));
1335 Window* child = CreateNormalWindow(11, parent.get(), &delegate);
1336 ui::test::EventGenerator generator(root_window(), child);
1337 generator.GestureTapAt(gfx::Point(40, 40));
1340 namespace {
1342 // A window delegate that processes nested gestures on tap.
1343 class NestedGestureDelegate : public test::TestWindowDelegate {
1344 public:
1345 NestedGestureDelegate(ui::test::EventGenerator* generator,
1346 const gfx::Point tap_location)
1347 : generator_(generator),
1348 tap_location_(tap_location),
1349 gesture_end_count_(0) {}
1350 ~NestedGestureDelegate() override {}
1352 int gesture_end_count() const { return gesture_end_count_; }
1354 private:
1355 void OnGestureEvent(ui::GestureEvent* event) override {
1356 switch (event->type()) {
1357 case ui::ET_GESTURE_TAP_DOWN:
1358 event->SetHandled();
1359 break;
1360 case ui::ET_GESTURE_TAP:
1361 if (generator_)
1362 generator_->GestureTapAt(tap_location_);
1363 event->SetHandled();
1364 break;
1365 case ui::ET_GESTURE_END:
1366 ++gesture_end_count_;
1367 break;
1368 default:
1369 break;
1373 ui::test::EventGenerator* generator_;
1374 const gfx::Point tap_location_;
1375 int gesture_end_count_;
1376 DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate);
1379 } // namespace
1381 // Tests that gesture end is delivered after nested gesture processing.
1382 TEST_F(WindowEventDispatcherTest, GestureEndDeliveredAfterNestedGestures) {
1383 NestedGestureDelegate d1(NULL, gfx::Point());
1384 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &d1));
1385 w1->SetBounds(gfx::Rect(0, 0, 100, 100));
1387 ui::test::EventGenerator nested_generator(root_window(), w1.get());
1388 NestedGestureDelegate d2(&nested_generator, w1->bounds().CenterPoint());
1389 scoped_ptr<Window> w2(CreateNormalWindow(1, root_window(), &d2));
1390 w2->SetBounds(gfx::Rect(100, 0, 100, 100));
1392 // Tap on w2 which triggers nested gestures for w1.
1393 ui::test::EventGenerator generator(root_window(), w2.get());
1394 generator.GestureTapAt(w2->bounds().CenterPoint());
1396 // Both windows should get their gesture end events.
1397 EXPECT_EQ(1, d1.gesture_end_count());
1398 EXPECT_EQ(1, d2.gesture_end_count());
1401 // Tests whether we can repost the Tap down gesture event.
1402 TEST_F(WindowEventDispatcherTest, RepostTapdownGestureTest) {
1403 EventFilterRecorder recorder;
1404 root_window()->AddPreTargetHandler(&recorder);
1406 test::TestWindowDelegate delegate;
1407 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1408 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1410 ui::GestureEventDetails details(ui::ET_GESTURE_TAP_DOWN);
1411 gfx::Point point(10, 10);
1412 ui::GestureEvent event(point.x(),
1413 point.y(),
1415 ui::EventTimeForNow(),
1416 details);
1417 host()->dispatcher()->RepostEvent(event);
1418 RunAllPendingInMessageLoop();
1419 // TODO(rbyers): Currently disabled - crbug.com/170987
1420 EXPECT_FALSE(EventTypesToString(recorder.events()).find("GESTURE_TAP_DOWN") !=
1421 std::string::npos);
1422 recorder.Reset();
1423 root_window()->RemovePreTargetHandler(&recorder);
1426 // This class inherits from the EventFilterRecorder class which provides a
1427 // facility to record events. This class additionally provides a facility to
1428 // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
1429 // events after that.
1430 class RepostGestureEventRecorder : public EventFilterRecorder {
1431 public:
1432 RepostGestureEventRecorder(aura::Window* repost_source,
1433 aura::Window* repost_target)
1434 : repost_source_(repost_source),
1435 repost_target_(repost_target),
1436 reposted_(false),
1437 done_cleanup_(false) {}
1439 ~RepostGestureEventRecorder() override {}
1441 void OnTouchEvent(ui::TouchEvent* event) override {
1442 if (reposted_ && event->type() == ui::ET_TOUCH_PRESSED) {
1443 done_cleanup_ = true;
1444 Reset();
1446 EventFilterRecorder::OnTouchEvent(event);
1449 void OnGestureEvent(ui::GestureEvent* event) override {
1450 EXPECT_EQ(done_cleanup_ ? repost_target_ : repost_source_, event->target());
1451 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
1452 if (!reposted_) {
1453 EXPECT_NE(repost_target_, event->target());
1454 reposted_ = true;
1455 repost_target_->GetHost()->dispatcher()->RepostEvent(*event);
1456 // Ensure that the reposted gesture event above goes to the
1457 // repost_target_;
1458 repost_source_->GetRootWindow()->RemoveChild(repost_source_);
1459 return;
1462 EventFilterRecorder::OnGestureEvent(event);
1465 // Ignore mouse events as they don't fire at all times. This causes
1466 // the GestureRepostEventOrder test to fail randomly.
1467 void OnMouseEvent(ui::MouseEvent* event) override {}
1469 private:
1470 aura::Window* repost_source_;
1471 aura::Window* repost_target_;
1472 // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
1473 bool reposted_;
1474 // set true if we're done cleaning up after hiding repost_source_;
1475 bool done_cleanup_;
1476 DISALLOW_COPY_AND_ASSIGN(RepostGestureEventRecorder);
1479 // Tests whether events which are generated after the reposted gesture event
1480 // are received after that. In this case the scroll sequence events should
1481 // be received after the reposted gesture event.
1482 TEST_F(WindowEventDispatcherTest, GestureRepostEventOrder) {
1483 // Expected events at the end for the repost_target window defined below.
1484 const char kExpectedTargetEvents[] =
1485 // TODO)(rbyers): Gesture event reposting is disabled - crbug.com/279039.
1486 // "GESTURE_BEGIN GESTURE_TAP_DOWN "
1487 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1488 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE TOUCH_MOVED "
1489 "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
1490 "GESTURE_SCROLL_END GESTURE_END";
1491 // We create two windows.
1492 // The first window (repost_source) is the one to which the initial tap
1493 // gesture is sent. It reposts this event to the second window
1494 // (repost_target).
1495 // We then generate the scroll sequence for repost_target and look for two
1496 // ET_GESTURE_TAP_DOWN events in the event list at the end.
1497 test::TestWindowDelegate delegate;
1498 scoped_ptr<aura::Window> repost_target(CreateTestWindowWithDelegate(
1499 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1501 scoped_ptr<aura::Window> repost_source(CreateTestWindowWithDelegate(
1502 &delegate, 1, gfx::Rect(0, 0, 50, 50), root_window()));
1504 RepostGestureEventRecorder repost_event_recorder(repost_source.get(),
1505 repost_target.get());
1506 root_window()->AddPreTargetHandler(&repost_event_recorder);
1508 // Generate a tap down gesture for the repost_source. This will be reposted
1509 // to repost_target.
1510 ui::test::EventGenerator repost_generator(root_window(), repost_source.get());
1511 repost_generator.GestureTapAt(gfx::Point(40, 40));
1512 RunAllPendingInMessageLoop();
1514 ui::test::EventGenerator scroll_generator(root_window(), repost_target.get());
1515 scroll_generator.GestureScrollSequence(
1516 gfx::Point(80, 80),
1517 gfx::Point(100, 100),
1518 base::TimeDelta::FromMilliseconds(100),
1520 RunAllPendingInMessageLoop();
1522 int tap_down_count = 0;
1523 for (size_t i = 0; i < repost_event_recorder.events().size(); ++i) {
1524 if (repost_event_recorder.events()[i] == ui::ET_GESTURE_TAP_DOWN)
1525 ++tap_down_count;
1528 // We expect two tap down events. One from the repost and the other one from
1529 // the scroll sequence posted above.
1530 // TODO(rbyers): Currently disabled - crbug.com/170987
1531 EXPECT_EQ(1, tap_down_count);
1533 EXPECT_EQ(kExpectedTargetEvents,
1534 EventTypesToString(repost_event_recorder.events()));
1535 root_window()->RemovePreTargetHandler(&repost_event_recorder);
1538 class OnMouseExitDeletingEventFilter : public EventFilterRecorder {
1539 public:
1540 OnMouseExitDeletingEventFilter() : window_to_delete_(NULL) {}
1541 ~OnMouseExitDeletingEventFilter() override {}
1543 void set_window_to_delete(Window* window_to_delete) {
1544 window_to_delete_ = window_to_delete;
1547 private:
1548 // Overridden from ui::EventHandler:
1549 void OnMouseEvent(ui::MouseEvent* event) override {
1550 EventFilterRecorder::OnMouseEvent(event);
1551 if (window_to_delete_) {
1552 delete window_to_delete_;
1553 window_to_delete_ = NULL;
1557 Window* window_to_delete_;
1559 DISALLOW_COPY_AND_ASSIGN(OnMouseExitDeletingEventFilter);
1562 // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
1563 // a child, but the child is destroyed because of the synthesized mouse-exit
1564 // event generated on the previous mouse_moved_handler_.
1565 TEST_F(WindowEventDispatcherTest, DeleteWindowDuringMouseMovedDispatch) {
1566 // Create window 1 and set its event filter. Window 1 will take ownership of
1567 // the event filter.
1568 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1569 OnMouseExitDeletingEventFilter w1_filter;
1570 w1->AddPreTargetHandler(&w1_filter);
1571 w1->SetBounds(gfx::Rect(20, 20, 60, 60));
1572 EXPECT_EQ(NULL, host()->dispatcher()->mouse_moved_handler());
1574 ui::test::EventGenerator generator(root_window(), w1.get());
1576 // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
1577 // root window.
1578 generator.MoveMouseTo(51, 51);
1579 EXPECT_EQ(w1.get(), host()->dispatcher()->mouse_moved_handler());
1581 // Create window 2 under the mouse cursor and stack it above window 1.
1582 Window* w2 = CreateNormalWindow(2, root_window(), NULL);
1583 w2->SetBounds(gfx::Rect(30, 30, 40, 40));
1584 root_window()->StackChildAbove(w2, w1.get());
1586 // Set window 2 as the window that is to be deleted when a mouse-exited event
1587 // happens on window 1.
1588 w1_filter.set_window_to_delete(w2);
1590 // Move mosue over window 2. This should generate a mouse-exited event for
1591 // window 1 resulting in deletion of window 2. The original mouse-moved event
1592 // that was targeted to window 2 should be dropped since window 2 is
1593 // destroyed. This test passes if no crash happens.
1594 generator.MoveMouseTo(52, 52);
1595 EXPECT_EQ(NULL, host()->dispatcher()->mouse_moved_handler());
1597 // Check events received by window 1.
1598 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
1599 EventTypesToString(w1_filter.events()));
1602 namespace {
1604 // Used to track if OnWindowDestroying() is invoked and if there is a valid
1605 // RootWindow at such time.
1606 class ValidRootDuringDestructionWindowObserver : public aura::WindowObserver {
1607 public:
1608 ValidRootDuringDestructionWindowObserver(bool* got_destroying,
1609 bool* has_valid_root)
1610 : got_destroying_(got_destroying),
1611 has_valid_root_(has_valid_root) {
1614 // WindowObserver:
1615 void OnWindowDestroying(aura::Window* window) override {
1616 *got_destroying_ = true;
1617 *has_valid_root_ = (window->GetRootWindow() != NULL);
1620 private:
1621 bool* got_destroying_;
1622 bool* has_valid_root_;
1624 DISALLOW_COPY_AND_ASSIGN(ValidRootDuringDestructionWindowObserver);
1627 } // namespace
1629 // Verifies GetRootWindow() from ~Window returns a valid root.
1630 TEST_F(WindowEventDispatcherTest, ValidRootDuringDestruction) {
1631 bool got_destroying = false;
1632 bool has_valid_root = false;
1633 ValidRootDuringDestructionWindowObserver observer(&got_destroying,
1634 &has_valid_root);
1636 scoped_ptr<WindowTreeHost> host(
1637 WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100)));
1638 host->InitHost();
1639 // Owned by WindowEventDispatcher.
1640 Window* w1 = CreateNormalWindow(1, host->window(), NULL);
1641 w1->AddObserver(&observer);
1643 EXPECT_TRUE(got_destroying);
1644 EXPECT_TRUE(has_valid_root);
1647 namespace {
1649 // See description above DontResetHeldEvent for details.
1650 class DontResetHeldEventWindowDelegate : public test::TestWindowDelegate {
1651 public:
1652 explicit DontResetHeldEventWindowDelegate(aura::Window* root)
1653 : root_(root),
1654 mouse_event_count_(0) {}
1655 ~DontResetHeldEventWindowDelegate() override {}
1657 int mouse_event_count() const { return mouse_event_count_; }
1659 // TestWindowDelegate:
1660 void OnMouseEvent(ui::MouseEvent* event) override {
1661 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0 &&
1662 mouse_event_count_++ == 0) {
1663 ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1664 gfx::Point(10, 10), ui::EventTimeForNow(),
1665 ui::EF_SHIFT_DOWN, 0);
1666 root_->GetHost()->dispatcher()->RepostEvent(mouse_event);
1670 private:
1671 Window* root_;
1672 int mouse_event_count_;
1674 DISALLOW_COPY_AND_ASSIGN(DontResetHeldEventWindowDelegate);
1677 } // namespace
1679 // Verifies RootWindow doesn't reset |RootWindow::held_repostable_event_| after
1680 // dispatching. This is done by using DontResetHeldEventWindowDelegate, which
1681 // tracks the number of events with ui::EF_SHIFT_DOWN set (all reposted events
1682 // have EF_SHIFT_DOWN). When the first event is seen RepostEvent() is used to
1683 // schedule another reposted event.
1684 TEST_F(WindowEventDispatcherTest, DontResetHeldEvent) {
1685 DontResetHeldEventWindowDelegate delegate(root_window());
1686 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
1687 w1->SetBounds(gfx::Rect(0, 0, 40, 40));
1688 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1689 gfx::Point(10, 10), ui::EventTimeForNow(),
1690 ui::EF_SHIFT_DOWN, 0);
1691 root_window()->GetHost()->dispatcher()->RepostEvent(pressed);
1692 ui::MouseEvent pressed2(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1693 gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
1694 // Dispatch an event to flush event scheduled by way of RepostEvent().
1695 DispatchEventUsingWindowDispatcher(&pressed2);
1696 // Delegate should have seen reposted event (identified by way of
1697 // EF_SHIFT_DOWN). Dispatch another event to flush the second
1698 // RepostedEvent().
1699 EXPECT_EQ(1, delegate.mouse_event_count());
1700 DispatchEventUsingWindowDispatcher(&pressed2);
1701 EXPECT_EQ(2, delegate.mouse_event_count());
1704 namespace {
1706 // See description above DeleteHostFromHeldMouseEvent for details.
1707 class DeleteHostFromHeldMouseEventDelegate
1708 : public test::TestWindowDelegate {
1709 public:
1710 explicit DeleteHostFromHeldMouseEventDelegate(WindowTreeHost* host)
1711 : host_(host),
1712 got_mouse_event_(false),
1713 got_destroy_(false) {
1715 ~DeleteHostFromHeldMouseEventDelegate() override {}
1717 bool got_mouse_event() const { return got_mouse_event_; }
1718 bool got_destroy() const { return got_destroy_; }
1720 // TestWindowDelegate:
1721 void OnMouseEvent(ui::MouseEvent* event) override {
1722 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0) {
1723 got_mouse_event_ = true;
1724 delete host_;
1727 void OnWindowDestroyed(Window* window) override { got_destroy_ = true; }
1729 private:
1730 WindowTreeHost* host_;
1731 bool got_mouse_event_;
1732 bool got_destroy_;
1734 DISALLOW_COPY_AND_ASSIGN(DeleteHostFromHeldMouseEventDelegate);
1737 } // namespace
1739 // Verifies if a WindowTreeHost is deleted from dispatching a held mouse event
1740 // we don't crash.
1741 TEST_F(WindowEventDispatcherTest, DeleteHostFromHeldMouseEvent) {
1742 // Should be deleted by |delegate|.
1743 WindowTreeHost* h2 = WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100));
1744 h2->InitHost();
1745 DeleteHostFromHeldMouseEventDelegate delegate(h2);
1746 // Owned by |h2|.
1747 Window* w1 = CreateNormalWindow(1, h2->window(), &delegate);
1748 w1->SetBounds(gfx::Rect(0, 0, 40, 40));
1749 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1750 gfx::Point(10, 10), ui::EventTimeForNow(),
1751 ui::EF_SHIFT_DOWN, 0);
1752 h2->dispatcher()->RepostEvent(pressed);
1753 // RunAllPendingInMessageLoop() to make sure the |pressed| is run.
1754 RunAllPendingInMessageLoop();
1755 EXPECT_TRUE(delegate.got_mouse_event());
1756 EXPECT_TRUE(delegate.got_destroy());
1759 TEST_F(WindowEventDispatcherTest, WindowHideCancelsActiveTouches) {
1760 EventFilterRecorder recorder;
1761 root_window()->AddPreTargetHandler(&recorder);
1763 test::TestWindowDelegate delegate;
1764 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1765 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1767 gfx::Point position1 = root_window()->bounds().origin();
1768 ui::TouchEvent press(
1769 ui::ET_TOUCH_PRESSED, position1, 0, ui::EventTimeForNow());
1770 DispatchEventUsingWindowDispatcher(&press);
1772 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN",
1773 EventTypesToString(recorder.GetAndResetEvents()));
1775 window->Hide();
1777 EXPECT_EQ(ui::ET_TOUCH_CANCELLED, recorder.events()[0]);
1778 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_TAP_CANCEL));
1779 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_END));
1780 EXPECT_EQ(3U, recorder.events().size());
1781 root_window()->RemovePreTargetHandler(&recorder);
1784 TEST_F(WindowEventDispatcherTest, WindowHideCancelsActiveGestures) {
1785 EventFilterRecorder recorder;
1786 root_window()->AddPreTargetHandler(&recorder);
1788 test::TestWindowDelegate delegate;
1789 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1790 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1792 gfx::Point position1 = root_window()->bounds().origin();
1793 gfx::Point position2 = root_window()->bounds().CenterPoint();
1794 ui::TouchEvent press(
1795 ui::ET_TOUCH_PRESSED, position1, 0, ui::EventTimeForNow());
1796 DispatchEventUsingWindowDispatcher(&press);
1798 ui::TouchEvent move(
1799 ui::ET_TOUCH_MOVED, position2, 0, ui::EventTimeForNow());
1800 DispatchEventUsingWindowDispatcher(&move);
1802 ui::TouchEvent press2(
1803 ui::ET_TOUCH_PRESSED, position1, 1, ui::EventTimeForNow());
1804 DispatchEventUsingWindowDispatcher(&press2);
1806 // TODO(tdresser): once the unified Gesture Recognizer has stuck, remove the
1807 // special casing here. See crbug.com/332418 for details.
1808 std::string expected =
1809 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1810 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1811 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_PINCH_BEGIN";
1813 std::string expected_ugr =
1814 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1815 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1816 "TOUCH_PRESSED GESTURE_BEGIN";
1818 std::string events_string = EventTypesToString(recorder.GetAndResetEvents());
1819 EXPECT_TRUE((expected == events_string) || (expected_ugr == events_string));
1821 window->Hide();
1823 expected =
1824 "TOUCH_CANCELLED GESTURE_PINCH_END GESTURE_END TOUCH_CANCELLED "
1825 "GESTURE_SCROLL_END GESTURE_END";
1826 expected_ugr =
1827 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END TOUCH_CANCELLED "
1828 "GESTURE_END";
1830 events_string = EventTypesToString(recorder.GetAndResetEvents());
1831 EXPECT_TRUE((expected == events_string) || (expected_ugr == events_string));
1833 root_window()->RemovePreTargetHandler(&recorder);
1836 // Places two windows side by side. Presses down on one window, and starts a
1837 // scroll. Sets capture on the other window and ensures that the "ending" events
1838 // aren't sent to the window which gained capture.
1839 TEST_F(WindowEventDispatcherTest, EndingEventDoesntRetarget) {
1840 EventFilterRecorder recorder1;
1841 EventFilterRecorder recorder2;
1842 scoped_ptr<Window> window1(CreateNormalWindow(1, root_window(), NULL));
1843 window1->SetBounds(gfx::Rect(0, 0, 40, 40));
1845 scoped_ptr<Window> window2(CreateNormalWindow(2, root_window(), NULL));
1846 window2->SetBounds(gfx::Rect(40, 0, 40, 40));
1848 window1->AddPreTargetHandler(&recorder1);
1849 window2->AddPreTargetHandler(&recorder2);
1851 gfx::Point position = window1->bounds().origin();
1852 ui::TouchEvent press(
1853 ui::ET_TOUCH_PRESSED, position, 0, ui::EventTimeForNow());
1854 DispatchEventUsingWindowDispatcher(&press);
1856 gfx::Point position2 = window1->bounds().CenterPoint();
1857 ui::TouchEvent move(
1858 ui::ET_TOUCH_MOVED, position2, 0, ui::EventTimeForNow());
1859 DispatchEventUsingWindowDispatcher(&move);
1861 window2->SetCapture();
1863 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1864 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1865 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END",
1866 EventTypesToString(recorder1.events()));
1868 EXPECT_TRUE(recorder2.events().empty());
1871 namespace {
1873 // This class creates and manages a window which is destroyed as soon as
1874 // capture is lost. This is the case for the drag and drop capture window.
1875 class CaptureWindowTracker : public test::TestWindowDelegate {
1876 public:
1877 CaptureWindowTracker() {}
1878 ~CaptureWindowTracker() override {}
1880 void CreateCaptureWindow(aura::Window* root_window) {
1881 capture_window_.reset(test::CreateTestWindowWithDelegate(
1882 this, -1234, gfx::Rect(20, 20, 20, 20), root_window));
1883 capture_window_->SetCapture();
1886 void reset() {
1887 capture_window_.reset();
1890 void OnCaptureLost() override { capture_window_.reset(); }
1892 void OnWindowDestroyed(Window* window) override {
1893 TestWindowDelegate::OnWindowDestroyed(window);
1894 capture_window_.reset();
1897 aura::Window* capture_window() { return capture_window_.get(); }
1899 private:
1900 scoped_ptr<aura::Window> capture_window_;
1902 DISALLOW_COPY_AND_ASSIGN(CaptureWindowTracker);
1907 // Verifies handling loss of capture by the capture window being hidden.
1908 TEST_F(WindowEventDispatcherTest, CaptureWindowHidden) {
1909 CaptureWindowTracker capture_window_tracker;
1910 capture_window_tracker.CreateCaptureWindow(root_window());
1911 capture_window_tracker.capture_window()->Hide();
1912 EXPECT_EQ(NULL, capture_window_tracker.capture_window());
1915 // Verifies handling loss of capture by the capture window being destroyed.
1916 TEST_F(WindowEventDispatcherTest, CaptureWindowDestroyed) {
1917 CaptureWindowTracker capture_window_tracker;
1918 capture_window_tracker.CreateCaptureWindow(root_window());
1919 capture_window_tracker.reset();
1920 EXPECT_EQ(NULL, capture_window_tracker.capture_window());
1923 class ExitMessageLoopOnMousePress : public ui::test::TestEventHandler {
1924 public:
1925 ExitMessageLoopOnMousePress() {}
1926 ~ExitMessageLoopOnMousePress() override {}
1928 protected:
1929 void OnMouseEvent(ui::MouseEvent* event) override {
1930 ui::test::TestEventHandler::OnMouseEvent(event);
1931 if (event->type() == ui::ET_MOUSE_PRESSED)
1932 base::MessageLoopForUI::current()->Quit();
1935 private:
1936 DISALLOW_COPY_AND_ASSIGN(ExitMessageLoopOnMousePress);
1939 class WindowEventDispatcherTestWithMessageLoop
1940 : public WindowEventDispatcherTest {
1941 public:
1942 WindowEventDispatcherTestWithMessageLoop() {}
1943 ~WindowEventDispatcherTestWithMessageLoop() override {}
1945 void RunTest() {
1946 // Reset any event the window may have received when bringing up the window
1947 // (e.g. mouse-move events if the mouse cursor is over the window).
1948 handler_.Reset();
1950 // Start a nested message-loop, post an event to be dispatched, and then
1951 // terminate the message-loop. When the message-loop unwinds and gets back,
1952 // the reposted event should not have fired.
1953 scoped_ptr<ui::MouseEvent> mouse(new ui::MouseEvent(
1954 ui::ET_MOUSE_PRESSED, gfx::Point(10, 10), gfx::Point(10, 10),
1955 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE));
1956 message_loop()->PostTask(
1957 FROM_HERE,
1958 base::Bind(&WindowEventDispatcherTestWithMessageLoop::RepostEventHelper,
1959 host()->dispatcher(),
1960 base::Passed(&mouse)));
1961 message_loop()->PostTask(FROM_HERE, message_loop()->QuitClosure());
1963 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop());
1964 base::RunLoop loop;
1965 loop.Run();
1966 EXPECT_EQ(0, handler_.num_mouse_events());
1968 // Let the current message-loop run. The event-handler will terminate the
1969 // message-loop when it receives the reposted event.
1972 base::MessageLoop* message_loop() {
1973 return base::MessageLoopForUI::current();
1976 protected:
1977 void SetUp() override {
1978 WindowEventDispatcherTest::SetUp();
1979 window_.reset(CreateNormalWindow(1, root_window(), NULL));
1980 window_->AddPreTargetHandler(&handler_);
1983 void TearDown() override {
1984 window_.reset();
1985 WindowEventDispatcherTest::TearDown();
1988 private:
1989 // Used to avoid a copying |event| when binding to a closure.
1990 static void RepostEventHelper(WindowEventDispatcher* dispatcher,
1991 scoped_ptr<ui::MouseEvent> event) {
1992 dispatcher->RepostEvent(*event);
1995 scoped_ptr<Window> window_;
1996 ExitMessageLoopOnMousePress handler_;
1998 DISALLOW_COPY_AND_ASSIGN(WindowEventDispatcherTestWithMessageLoop);
2001 TEST_F(WindowEventDispatcherTestWithMessageLoop, EventRepostedInNonNestedLoop) {
2002 CHECK(!message_loop()->is_running());
2003 // Perform the test in a callback, so that it runs after the message-loop
2004 // starts.
2005 message_loop()->PostTask(
2006 FROM_HERE, base::Bind(
2007 &WindowEventDispatcherTestWithMessageLoop::RunTest,
2008 base::Unretained(this)));
2009 message_loop()->Run();
2012 class WindowEventDispatcherTestInHighDPI : public WindowEventDispatcherTest {
2013 public:
2014 WindowEventDispatcherTestInHighDPI() {}
2015 ~WindowEventDispatcherTestInHighDPI() override {}
2017 void DispatchEvent(ui::Event* event) {
2018 DispatchEventUsingWindowDispatcher(event);
2021 protected:
2022 void SetUp() override {
2023 WindowEventDispatcherTest::SetUp();
2024 test_screen()->SetDeviceScaleFactor(2.f);
2028 TEST_F(WindowEventDispatcherTestInHighDPI, EventLocationTransform) {
2029 test::TestWindowDelegate delegate;
2030 scoped_ptr<aura::Window> child(test::CreateTestWindowWithDelegate(&delegate,
2031 1234, gfx::Rect(20, 20, 100, 100), root_window()));
2032 child->Show();
2034 ui::test::TestEventHandler handler_child;
2035 ui::test::TestEventHandler handler_root;
2036 root_window()->AddPreTargetHandler(&handler_root);
2037 child->AddPreTargetHandler(&handler_child);
2040 ui::MouseEvent move(ui::ET_MOUSE_MOVED, gfx::Point(30, 30),
2041 gfx::Point(30, 30), ui::EventTimeForNow(), ui::EF_NONE,
2042 ui::EF_NONE);
2043 DispatchEventUsingWindowDispatcher(&move);
2044 EXPECT_EQ(0, handler_child.num_mouse_events());
2045 EXPECT_EQ(1, handler_root.num_mouse_events());
2049 ui::MouseEvent move(ui::ET_MOUSE_MOVED, gfx::Point(50, 50),
2050 gfx::Point(50, 50), ui::EventTimeForNow(), ui::EF_NONE,
2051 ui::EF_NONE);
2052 DispatchEventUsingWindowDispatcher(&move);
2053 // The child receives an ENTER, and a MOVED event.
2054 EXPECT_EQ(2, handler_child.num_mouse_events());
2055 // The root receives both the ENTER and the MOVED events dispatched to
2056 // |child|, as well as an EXIT event.
2057 EXPECT_EQ(3, handler_root.num_mouse_events());
2060 child->RemovePreTargetHandler(&handler_child);
2061 root_window()->RemovePreTargetHandler(&handler_root);
2064 TEST_F(WindowEventDispatcherTestInHighDPI, TouchMovesHeldOnScroll) {
2065 EventFilterRecorder recorder;
2066 root_window()->AddPreTargetHandler(&recorder);
2067 test::TestWindowDelegate delegate;
2068 HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
2069 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2070 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2071 window->AddPreTargetHandler(&handler);
2073 ui::test::EventGenerator generator(root_window());
2074 generator.GestureScrollSequence(
2075 gfx::Point(120, 120), gfx::Point(20, 120),
2076 base::TimeDelta::FromMilliseconds(100), 25);
2078 // |handler| will have reset |filter| and started holding the touch-move
2079 // events when scrolling started. At the end of the scroll (i.e. upon
2080 // touch-release), the held touch-move event will have been dispatched first,
2081 // along with the subsequent events (i.e. touch-release, scroll-end, and
2082 // gesture-end).
2083 const EventFilterRecorder::Events& events = recorder.events();
2084 EXPECT_EQ(
2085 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
2086 "GESTURE_SCROLL_END GESTURE_END",
2087 EventTypesToString(events));
2088 ASSERT_EQ(2u, recorder.touch_locations().size());
2089 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2090 recorder.touch_locations()[0].ToString());
2091 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2092 recorder.touch_locations()[1].ToString());
2093 root_window()->RemovePreTargetHandler(&recorder);
2096 // This handler triggers a nested message loop when it receives a right click
2097 // event, and runs a single callback in the nested message loop.
2098 class TriggerNestedLoopOnRightMousePress : public ui::test::TestEventHandler {
2099 public:
2100 explicit TriggerNestedLoopOnRightMousePress(const base::Closure& callback)
2101 : callback_(callback) {}
2102 ~TriggerNestedLoopOnRightMousePress() override {}
2104 const gfx::Point mouse_move_location() const { return mouse_move_location_; }
2106 private:
2107 void OnMouseEvent(ui::MouseEvent* mouse) override {
2108 TestEventHandler::OnMouseEvent(mouse);
2109 if (mouse->type() == ui::ET_MOUSE_PRESSED &&
2110 mouse->IsOnlyRightMouseButton()) {
2111 base::MessageLoop::ScopedNestableTaskAllower allow(
2112 base::MessageLoopForUI::current());
2113 base::RunLoop run_loop;
2114 scoped_refptr<base::TaskRunner> task_runner =
2115 base::ThreadTaskRunnerHandle::Get();
2116 if (!callback_.is_null())
2117 task_runner->PostTask(FROM_HERE, callback_);
2118 task_runner->PostTask(FROM_HERE, run_loop.QuitClosure());
2119 run_loop.Run();
2120 } else if (mouse->type() == ui::ET_MOUSE_MOVED) {
2121 mouse_move_location_ = mouse->location();
2125 base::Closure callback_;
2126 gfx::Point mouse_move_location_;
2128 DISALLOW_COPY_AND_ASSIGN(TriggerNestedLoopOnRightMousePress);
2131 // Tests that if dispatching a 'held' event triggers a nested message loop, then
2132 // the events that are dispatched from the nested message loop are transformed
2133 // correctly.
2134 TEST_F(WindowEventDispatcherTestInHighDPI,
2135 EventsTransformedInRepostedEventTriggeredNestedLoop) {
2136 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
2137 // Make sure the window is visible.
2138 RunAllPendingInMessageLoop();
2140 ui::MouseEvent mouse_move(ui::ET_MOUSE_MOVED, gfx::Point(80, 80),
2141 gfx::Point(80, 80), ui::EventTimeForNow(),
2142 ui::EF_NONE, ui::EF_NONE);
2143 const base::Closure callback_on_right_click = base::Bind(
2144 base::IgnoreResult(&WindowEventDispatcherTestInHighDPI::DispatchEvent),
2145 base::Unretained(this), base::Unretained(&mouse_move));
2146 TriggerNestedLoopOnRightMousePress handler(callback_on_right_click);
2147 window->AddPreTargetHandler(&handler);
2149 scoped_ptr<ui::MouseEvent> mouse(
2150 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
2151 gfx::Point(10, 10), ui::EventTimeForNow(),
2152 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
2153 host()->dispatcher()->RepostEvent(*mouse);
2154 EXPECT_EQ(0, handler.num_mouse_events());
2156 base::RunLoop run_loop;
2157 run_loop.RunUntilIdle();
2158 // The window should receive the mouse-press and the mouse-move events.
2159 EXPECT_EQ(2, handler.num_mouse_events());
2160 // The mouse-move event location should be transformed because of the DSF
2161 // before it reaches the window.
2162 EXPECT_EQ(gfx::Point(40, 40).ToString(),
2163 handler.mouse_move_location().ToString());
2164 EXPECT_EQ(gfx::Point(40, 40).ToString(),
2165 Env::GetInstance()->last_mouse_location().ToString());
2166 window->RemovePreTargetHandler(&handler);
2169 class SelfDestructDelegate : public test::TestWindowDelegate {
2170 public:
2171 SelfDestructDelegate() {}
2172 ~SelfDestructDelegate() override {}
2174 void OnMouseEvent(ui::MouseEvent* event) override { window_.reset(); }
2176 void set_window(scoped_ptr<aura::Window> window) {
2177 window_ = window.Pass();
2179 bool has_window() const { return !!window_.get(); }
2181 private:
2182 scoped_ptr<aura::Window> window_;
2183 DISALLOW_COPY_AND_ASSIGN(SelfDestructDelegate);
2186 TEST_F(WindowEventDispatcherTest, SynthesizedLocatedEvent) {
2187 ui::test::EventGenerator generator(root_window());
2188 generator.MoveMouseTo(10, 10);
2189 EXPECT_EQ("10,10",
2190 Env::GetInstance()->last_mouse_location().ToString());
2192 // Synthesized event should not update the mouse location.
2193 ui::MouseEvent mouseev(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
2194 ui::EventTimeForNow(), ui::EF_IS_SYNTHESIZED, 0);
2195 generator.Dispatch(&mouseev);
2196 EXPECT_EQ("10,10",
2197 Env::GetInstance()->last_mouse_location().ToString());
2199 generator.MoveMouseTo(0, 0);
2200 EXPECT_EQ("0,0",
2201 Env::GetInstance()->last_mouse_location().ToString());
2203 // Make sure the location gets updated when a syntheiszed enter
2204 // event destroyed the window.
2205 SelfDestructDelegate delegate;
2206 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2207 &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2208 delegate.set_window(window.Pass());
2209 EXPECT_TRUE(delegate.has_window());
2211 generator.MoveMouseTo(100, 100);
2212 EXPECT_FALSE(delegate.has_window());
2213 EXPECT_EQ("100,100",
2214 Env::GetInstance()->last_mouse_location().ToString());
2217 // Tests that the window which has capture can get destroyed as a result of
2218 // ui::ET_MOUSE_CAPTURE_CHANGED event dispatched in
2219 // WindowEventDispatcher::UpdateCapture without causing a "use after free".
2220 TEST_F(WindowEventDispatcherTest, DestroyWindowOnCaptureChanged) {
2221 SelfDestructDelegate delegate;
2222 scoped_ptr<aura::Window> window_first(CreateTestWindowWithDelegate(
2223 &delegate, 1, gfx::Rect(20, 10, 10, 20), root_window()));
2224 Window* window_first_raw = window_first.get();
2225 window_first->Show();
2226 window_first->SetCapture();
2227 delegate.set_window(window_first.Pass());
2228 EXPECT_TRUE(delegate.has_window());
2230 scoped_ptr<aura::Window> window_second(
2231 test::CreateTestWindowWithId(2, root_window()));
2232 window_second->Show();
2234 client::CaptureDelegate* capture_delegate = host()->dispatcher();
2235 capture_delegate->UpdateCapture(window_first_raw, window_second.get());
2236 EXPECT_FALSE(delegate.has_window());
2239 class StaticFocusClient : public client::FocusClient {
2240 public:
2241 explicit StaticFocusClient(Window* focused)
2242 : focused_(focused) {}
2243 ~StaticFocusClient() override {}
2245 private:
2246 // client::FocusClient:
2247 void AddObserver(client::FocusChangeObserver* observer) override {}
2248 void RemoveObserver(client::FocusChangeObserver* observer) override {}
2249 void FocusWindow(Window* window) override {}
2250 void ResetFocusWithinActiveWindow(Window* window) override {}
2251 Window* GetFocusedWindow() override { return focused_; }
2253 Window* focused_;
2255 DISALLOW_COPY_AND_ASSIGN(StaticFocusClient);
2258 // Tests that host-cancel-mode event can be dispatched to a dispatcher safely
2259 // when the focused window does not live in the dispatcher's tree.
2260 TEST_F(WindowEventDispatcherTest, HostCancelModeWithFocusedWindowOutside) {
2261 test::TestWindowDelegate delegate;
2262 scoped_ptr<Window> focused(CreateTestWindowWithDelegate(&delegate, 123,
2263 gfx::Rect(20, 30, 100, 50), NULL));
2264 StaticFocusClient focus_client(focused.get());
2265 client::SetFocusClient(root_window(), &focus_client);
2266 EXPECT_FALSE(root_window()->Contains(focused.get()));
2267 EXPECT_EQ(focused.get(),
2268 client::GetFocusClient(root_window())->GetFocusedWindow());
2269 host()->dispatcher()->DispatchCancelModeEvent();
2270 EXPECT_EQ(focused.get(),
2271 client::GetFocusClient(root_window())->GetFocusedWindow());
2274 // Dispatches a mouse-move event to |target| when it receives a mouse-move
2275 // event.
2276 class DispatchEventHandler : public ui::EventHandler {
2277 public:
2278 explicit DispatchEventHandler(Window* target)
2279 : target_(target),
2280 dispatched_(false) {}
2281 ~DispatchEventHandler() override {}
2283 bool dispatched() const { return dispatched_; }
2284 private:
2285 // ui::EventHandler:
2286 void OnMouseEvent(ui::MouseEvent* mouse) override {
2287 if (mouse->type() == ui::ET_MOUSE_MOVED) {
2288 ui::MouseEvent move(ui::ET_MOUSE_MOVED, target_->bounds().CenterPoint(),
2289 target_->bounds().CenterPoint(),
2290 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
2291 ui::EventDispatchDetails details =
2292 target_->GetHost()->dispatcher()->OnEventFromSource(&move);
2293 ASSERT_FALSE(details.dispatcher_destroyed);
2294 EXPECT_FALSE(details.target_destroyed);
2295 EXPECT_EQ(target_, move.target());
2296 dispatched_ = true;
2298 ui::EventHandler::OnMouseEvent(mouse);
2301 Window* target_;
2302 bool dispatched_;
2304 DISALLOW_COPY_AND_ASSIGN(DispatchEventHandler);
2307 // Moves |window| to |root_window| when it receives a mouse-move event.
2308 class MoveWindowHandler : public ui::EventHandler {
2309 public:
2310 MoveWindowHandler(Window* window, Window* root_window)
2311 : window_to_move_(window),
2312 root_window_to_move_to_(root_window) {}
2313 ~MoveWindowHandler() override {}
2315 private:
2316 // ui::EventHandler:
2317 void OnMouseEvent(ui::MouseEvent* mouse) override {
2318 if (mouse->type() == ui::ET_MOUSE_MOVED) {
2319 root_window_to_move_to_->AddChild(window_to_move_);
2321 ui::EventHandler::OnMouseEvent(mouse);
2324 Window* window_to_move_;
2325 Window* root_window_to_move_to_;
2327 DISALLOW_COPY_AND_ASSIGN(MoveWindowHandler);
2330 // Tests that nested event dispatch works correctly if the target of the older
2331 // event being dispatched is moved to a different dispatcher in response to an
2332 // event in the inner loop.
2333 TEST_F(WindowEventDispatcherTest, NestedEventDispatchTargetMoved) {
2334 scoped_ptr<WindowTreeHost> second_host(
2335 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2336 second_host->InitHost();
2337 Window* second_root = second_host->window();
2339 // Create two windows parented to |root_window()|.
2340 test::TestWindowDelegate delegate;
2341 scoped_ptr<Window> first(CreateTestWindowWithDelegate(&delegate, 123,
2342 gfx::Rect(20, 10, 10, 20), root_window()));
2343 scoped_ptr<Window> second(CreateTestWindowWithDelegate(&delegate, 234,
2344 gfx::Rect(40, 10, 50, 20), root_window()));
2346 // Setup a handler on |first| so that it dispatches an event to |second| when
2347 // |first| receives an event.
2348 DispatchEventHandler dispatch_event(second.get());
2349 first->AddPreTargetHandler(&dispatch_event);
2351 // Setup a handler on |second| so that it moves |first| into |second_root|
2352 // when |second| receives an event.
2353 MoveWindowHandler move_window(first.get(), second_root);
2354 second->AddPreTargetHandler(&move_window);
2356 // Some sanity checks: |first| is inside |root_window()|'s tree.
2357 EXPECT_EQ(root_window(), first->GetRootWindow());
2358 // The two root windows are different.
2359 EXPECT_NE(root_window(), second_root);
2361 // Dispatch an event to |first|.
2362 ui::MouseEvent move(ui::ET_MOUSE_MOVED, first->bounds().CenterPoint(),
2363 first->bounds().CenterPoint(), ui::EventTimeForNow(),
2364 ui::EF_NONE, ui::EF_NONE);
2365 ui::EventDispatchDetails details =
2366 host()->dispatcher()->OnEventFromSource(&move);
2367 ASSERT_FALSE(details.dispatcher_destroyed);
2368 EXPECT_TRUE(details.target_destroyed);
2369 EXPECT_EQ(first.get(), move.target());
2370 EXPECT_TRUE(dispatch_event.dispatched());
2371 EXPECT_EQ(second_root, first->GetRootWindow());
2373 first->RemovePreTargetHandler(&dispatch_event);
2374 second->RemovePreTargetHandler(&move_window);
2377 class AlwaysMouseDownInputStateLookup : public InputStateLookup {
2378 public:
2379 AlwaysMouseDownInputStateLookup() {}
2380 ~AlwaysMouseDownInputStateLookup() override {}
2382 private:
2383 // InputStateLookup:
2384 bool IsMouseButtonDown() const override { return true; }
2386 DISALLOW_COPY_AND_ASSIGN(AlwaysMouseDownInputStateLookup);
2389 TEST_F(WindowEventDispatcherTest,
2390 CursorVisibilityChangedWhileCaptureWindowInAnotherDispatcher) {
2391 test::EventCountDelegate delegate;
2392 scoped_ptr<Window> window(CreateTestWindowWithDelegate(&delegate, 123,
2393 gfx::Rect(20, 10, 10, 20), root_window()));
2394 window->Show();
2396 scoped_ptr<WindowTreeHost> second_host(
2397 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2398 second_host->InitHost();
2399 WindowEventDispatcher* second_dispatcher = second_host->dispatcher();
2401 // Install an InputStateLookup on the Env that always claims that a
2402 // mouse-button is down.
2403 test::EnvTestHelper(Env::GetInstance()).SetInputStateLookup(
2404 scoped_ptr<InputStateLookup>(new AlwaysMouseDownInputStateLookup()));
2406 window->SetCapture();
2408 // Because the mouse button is down, setting the capture on |window| will set
2409 // it as the mouse-move handler for |root_window()|.
2410 EXPECT_EQ(window.get(), host()->dispatcher()->mouse_moved_handler());
2412 // This does not set |window| as the mouse-move handler for the second
2413 // dispatcher.
2414 EXPECT_EQ(NULL, second_dispatcher->mouse_moved_handler());
2416 // However, some capture-client updates the capture in each root-window on a
2417 // capture. Emulate that here. Because of this, the second dispatcher also has
2418 // |window| as the mouse-move handler.
2419 client::CaptureDelegate* second_capture_delegate = second_dispatcher;
2420 second_capture_delegate->UpdateCapture(NULL, window.get());
2421 EXPECT_EQ(window.get(), second_dispatcher->mouse_moved_handler());
2423 // Reset the mouse-event counts for |window|.
2424 delegate.GetMouseMotionCountsAndReset();
2426 // Notify both hosts that the cursor is now hidden. This should send a single
2427 // mouse-exit event to |window|.
2428 host()->OnCursorVisibilityChanged(false);
2429 second_host->OnCursorVisibilityChanged(false);
2430 EXPECT_EQ("0 0 1", delegate.GetMouseMotionCountsAndReset());
2433 TEST_F(WindowEventDispatcherTest,
2434 RedirectedEventToDifferentDispatcherLocation) {
2435 scoped_ptr<WindowTreeHost> second_host(
2436 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2437 second_host->InitHost();
2438 client::SetCaptureClient(second_host->window(),
2439 client::GetCaptureClient(root_window()));
2441 test::EventCountDelegate delegate;
2442 scoped_ptr<Window> window_first(CreateTestWindowWithDelegate(&delegate, 123,
2443 gfx::Rect(20, 10, 10, 20), root_window()));
2444 window_first->Show();
2446 scoped_ptr<Window> window_second(CreateTestWindowWithDelegate(&delegate, 12,
2447 gfx::Rect(10, 10, 20, 30), second_host->window()));
2448 window_second->Show();
2450 window_second->SetCapture();
2451 EXPECT_EQ(window_second.get(),
2452 client::GetCaptureWindow(root_window()));
2454 // Send an event to the first host. Make sure it goes to |window_second| in
2455 // |second_host| instead (since it has capture).
2456 EventFilterRecorder recorder_first;
2457 window_first->AddPreTargetHandler(&recorder_first);
2458 EventFilterRecorder recorder_second;
2459 window_second->AddPreTargetHandler(&recorder_second);
2460 const gfx::Point event_location(25, 15);
2461 ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, event_location, event_location,
2462 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
2463 ui::EF_LEFT_MOUSE_BUTTON);
2464 DispatchEventUsingWindowDispatcher(&mouse);
2465 EXPECT_TRUE(recorder_first.events().empty());
2466 ASSERT_EQ(1u, recorder_second.events().size());
2467 EXPECT_EQ(ui::ET_MOUSE_PRESSED, recorder_second.events()[0]);
2468 EXPECT_EQ(event_location.ToString(),
2469 recorder_second.mouse_locations()[0].ToString());
2472 class AsyncWindowDelegate : public test::TestWindowDelegate {
2473 public:
2474 AsyncWindowDelegate(WindowEventDispatcher* dispatcher)
2475 : dispatcher_(dispatcher), window_(nullptr) {}
2477 void set_window(Window* window) {
2478 window_ = window;
2480 private:
2481 void OnTouchEvent(ui::TouchEvent* event) override {
2482 // Convert touch event back to root window coordinates.
2483 event->ConvertLocationToTarget(window_, window_->GetRootWindow());
2484 event->DisableSynchronousHandling();
2485 dispatcher_->ProcessedTouchEvent(event->unique_event_id(), window_,
2486 ui::ER_UNHANDLED);
2487 event->StopPropagation();
2490 WindowEventDispatcher* dispatcher_;
2491 Window* window_;
2493 DISALLOW_COPY_AND_ASSIGN(AsyncWindowDelegate);
2496 // Tests that gesture events dispatched through the asynchronous flow have
2497 // co-ordinates in the right co-ordinate space.
2498 TEST_F(WindowEventDispatcherTest, GestureEventCoordinates) {
2499 const float kX = 67.3f;
2500 const float kY = 97.8f;
2502 const int kWindowOffset = 50;
2503 EventFilterRecorder recorder;
2504 root_window()->AddPreTargetHandler(&recorder);
2505 AsyncWindowDelegate delegate(host()->dispatcher());
2506 HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
2507 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2508 &delegate,
2510 gfx::Rect(kWindowOffset, kWindowOffset, 100, 100),
2511 root_window()));
2512 window->AddPreTargetHandler(&handler);
2514 delegate.set_window(window.get());
2516 ui::TouchEvent touch_pressed_event(
2517 ui::ET_TOUCH_PRESSED, gfx::PointF(kX, kY), 0, ui::EventTimeForNow());
2519 DispatchEventUsingWindowDispatcher(&touch_pressed_event);
2521 ASSERT_EQ(1u, recorder.touch_locations().size());
2522 EXPECT_EQ(gfx::Point(kX - kWindowOffset, kY - kWindowOffset).ToString(),
2523 recorder.touch_locations()[0].ToString());
2525 ASSERT_EQ(2u, recorder.gesture_locations().size());
2526 EXPECT_EQ(gfx::Point(kX - kWindowOffset, kY - kWindowOffset).ToString(),
2527 recorder.gesture_locations()[0].ToString());
2528 root_window()->RemovePreTargetHandler(&recorder);
2531 // Tests that a scroll-generating touch-event is marked as such.
2532 TEST_F(WindowEventDispatcherTest, TouchMovesMarkedWhenCausingScroll) {
2533 EventFilterRecorder recorder;
2534 root_window()->AddPreTargetHandler(&recorder);
2536 const gfx::Point location(20, 20);
2537 ui::TouchEvent press(
2538 ui::ET_TOUCH_PRESSED, location, 0, ui::EventTimeForNow());
2539 DispatchEventUsingWindowDispatcher(&press);
2540 EXPECT_FALSE(recorder.LastTouchMayCauseScrolling());
2541 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_PRESSED));
2542 recorder.Reset();
2544 ui::TouchEvent move(ui::ET_TOUCH_MOVED,
2545 location + gfx::Vector2d(100, 100),
2547 ui::EventTimeForNow());
2548 DispatchEventUsingWindowDispatcher(&move);
2549 EXPECT_TRUE(recorder.LastTouchMayCauseScrolling());
2550 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
2551 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
2552 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
2553 recorder.Reset();
2555 ui::TouchEvent move2(ui::ET_TOUCH_MOVED,
2556 location + gfx::Vector2d(200, 200),
2558 ui::EventTimeForNow());
2559 DispatchEventUsingWindowDispatcher(&move2);
2560 EXPECT_TRUE(recorder.LastTouchMayCauseScrolling());
2561 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
2562 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
2563 recorder.Reset();
2565 // Delay the release to avoid fling generation.
2566 ui::TouchEvent release(
2567 ui::ET_TOUCH_RELEASED,
2568 location + gfx::Vector2dF(200, 200),
2570 ui::EventTimeForNow() + base::TimeDelta::FromSeconds(1));
2571 DispatchEventUsingWindowDispatcher(&release);
2572 EXPECT_FALSE(recorder.LastTouchMayCauseScrolling());
2573 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_RELEASED));
2574 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_END));
2576 root_window()->RemovePreTargetHandler(&recorder);
2579 // OnCursorMovedToRootLocation() is sometimes called instead of
2580 // WindowTreeHost::MoveCursorTo() when the cursor did not move but the
2581 // cursor's position in root coordinates has changed (e.g. when the displays's
2582 // scale factor changed). Test that hover effects are properly updated.
2583 TEST_F(WindowEventDispatcherTest, OnCursorMovedToRootLocationUpdatesHover) {
2584 WindowEventDispatcher* dispatcher = host()->dispatcher();
2586 scoped_ptr<Window> w(CreateNormalWindow(1, root_window(), nullptr));
2587 w->SetBounds(gfx::Rect(20, 20, 20, 20));
2588 w->Show();
2590 // Move the cursor off of |w|.
2591 dispatcher->OnCursorMovedToRootLocation(gfx::Point(100, 100));
2593 EventFilterRecorder recorder;
2594 w->AddPreTargetHandler(&recorder);
2595 dispatcher->OnCursorMovedToRootLocation(gfx::Point(22, 22));
2596 RunAllPendingInMessageLoop();
2597 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_MOUSE_ENTERED));
2598 recorder.Reset();
2600 // The cursor should not be over |w| after changing the device scale factor to
2601 // 2x. A ET_MOUSE_EXITED event should have been sent to |w|.
2602 test_screen()->SetDeviceScaleFactor(2.f);
2603 dispatcher->OnCursorMovedToRootLocation(gfx::Point(11, 11));
2604 RunAllPendingInMessageLoop();
2605 EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_MOUSE_EXITED));
2607 w->RemovePreTargetHandler(&recorder);
2610 } // namespace aura