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