Do not send mouse-move when target is destroyed due to synthesized mouse-exit in...
[chromium-blink-merge.git] / ui / aura / root_window_unittest.cc
blobf10a23877791ee6581c949bd50d55b3e3446280a
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/root_window.h"
7 #include <vector>
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/aura/client/event_client.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/focus_manager.h"
13 #include "ui/aura/test/aura_test_base.h"
14 #include "ui/aura/test/event_generator.h"
15 #include "ui/aura/test/test_cursor_client.h"
16 #include "ui/aura/test/test_event_handler.h"
17 #include "ui/aura/test/test_window_delegate.h"
18 #include "ui/aura/test/test_windows.h"
19 #include "ui/aura/window_tracker.h"
20 #include "ui/base/hit_test.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_handler.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/events/gestures/gesture_configuration.h"
25 #include "ui/events/keycodes/keyboard_codes.h"
26 #include "ui/gfx/point.h"
27 #include "ui/gfx/rect.h"
28 #include "ui/gfx/screen.h"
29 #include "ui/gfx/transform.h"
31 namespace aura {
32 namespace {
34 // A delegate that always returns a non-client component for hit tests.
35 class NonClientDelegate : public test::TestWindowDelegate {
36 public:
37 NonClientDelegate()
38 : non_client_count_(0),
39 mouse_event_count_(0),
40 mouse_event_flags_(0x0) {
42 virtual ~NonClientDelegate() {}
44 int non_client_count() const { return non_client_count_; }
45 gfx::Point non_client_location() const { return non_client_location_; }
46 int mouse_event_count() const { return mouse_event_count_; }
47 gfx::Point mouse_event_location() const { return mouse_event_location_; }
48 int mouse_event_flags() const { return mouse_event_flags_; }
50 virtual int GetNonClientComponent(const gfx::Point& location) const OVERRIDE {
51 NonClientDelegate* self = const_cast<NonClientDelegate*>(this);
52 self->non_client_count_++;
53 self->non_client_location_ = location;
54 return HTTOPLEFT;
56 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
57 mouse_event_count_++;
58 mouse_event_location_ = event->location();
59 mouse_event_flags_ = event->flags();
60 event->SetHandled();
63 private:
64 int non_client_count_;
65 gfx::Point non_client_location_;
66 int mouse_event_count_;
67 gfx::Point mouse_event_location_;
68 int mouse_event_flags_;
70 DISALLOW_COPY_AND_ASSIGN(NonClientDelegate);
73 // A simple event handler that consumes key events.
74 class ConsumeKeyHandler : public test::TestEventHandler {
75 public:
76 ConsumeKeyHandler() {}
77 virtual ~ConsumeKeyHandler() {}
79 // Overridden from ui::EventHandler:
80 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
81 test::TestEventHandler::OnKeyEvent(event);
82 event->StopPropagation();
85 private:
86 DISALLOW_COPY_AND_ASSIGN(ConsumeKeyHandler);
89 bool IsFocusedWindow(aura::Window* window) {
90 return client::GetFocusClient(window)->GetFocusedWindow() == window;
93 } // namespace
95 typedef test::AuraTestBase RootWindowTest;
97 TEST_F(RootWindowTest, OnHostMouseEvent) {
98 // Create two non-overlapping windows so we don't have to worry about which
99 // is on top.
100 scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate());
101 scoped_ptr<NonClientDelegate> delegate2(new NonClientDelegate());
102 const int kWindowWidth = 123;
103 const int kWindowHeight = 45;
104 gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight);
105 gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight);
106 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
107 delegate1.get(), -1234, bounds1, root_window()));
108 scoped_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
109 delegate2.get(), -5678, bounds2, root_window()));
111 // Send a mouse event to window1.
112 gfx::Point point(101, 201);
113 ui::MouseEvent event1(
114 ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON);
115 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&event1);
117 // Event was tested for non-client area for the target window.
118 EXPECT_EQ(1, delegate1->non_client_count());
119 EXPECT_EQ(0, delegate2->non_client_count());
120 // The non-client component test was in local coordinates.
121 EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location());
122 // Mouse event was received by target window.
123 EXPECT_EQ(1, delegate1->mouse_event_count());
124 EXPECT_EQ(0, delegate2->mouse_event_count());
125 // Event was in local coordinates.
126 EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location());
127 // Non-client flag was set.
128 EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT);
131 TEST_F(RootWindowTest, RepostEvent) {
132 // Test RepostEvent in RootWindow. It only works for Mouse Press.
133 EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());
134 gfx::Point point(10, 10);
135 ui::MouseEvent event(
136 ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON);
137 root_window()->RepostEvent(event);
138 RunAllPendingInMessageLoop();
139 EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
142 // Check that we correctly track the state of the mouse buttons in response to
143 // button press and release events.
144 TEST_F(RootWindowTest, MouseButtonState) {
145 EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());
147 gfx::Point location;
148 scoped_ptr<ui::MouseEvent> event;
150 // Press the left button.
151 event.reset(new ui::MouseEvent(
152 ui::ET_MOUSE_PRESSED,
153 location,
154 location,
155 ui::EF_LEFT_MOUSE_BUTTON));
156 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
157 EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
159 // Additionally press the right.
160 event.reset(new ui::MouseEvent(
161 ui::ET_MOUSE_PRESSED,
162 location,
163 location,
164 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON));
165 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
166 EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
168 // Release the left button.
169 event.reset(new ui::MouseEvent(
170 ui::ET_MOUSE_RELEASED,
171 location,
172 location,
173 ui::EF_RIGHT_MOUSE_BUTTON));
174 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
175 EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
177 // Release the right button. We should ignore the Shift-is-down flag.
178 event.reset(new ui::MouseEvent(
179 ui::ET_MOUSE_RELEASED,
180 location,
181 location,
182 ui::EF_SHIFT_DOWN));
183 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
184 EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());
186 // Press the middle button.
187 event.reset(new ui::MouseEvent(
188 ui::ET_MOUSE_PRESSED,
189 location,
190 location,
191 ui::EF_MIDDLE_MOUSE_BUTTON));
192 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
193 EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
196 TEST_F(RootWindowTest, 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, 0);
203 EXPECT_EQ("100,100", root.location().ToString());
204 EXPECT_EQ("100,100", root.root_location().ToString());
206 ui::MouseEvent translated_event(
207 root, static_cast<Window*>(root_window()), w1.get(),
208 ui::ET_MOUSE_ENTERED, root.flags());
209 EXPECT_EQ("50,50", translated_event.location().ToString());
210 EXPECT_EQ("100,100", translated_event.root_location().ToString());
213 namespace {
215 class TestEventClient : public client::EventClient {
216 public:
217 static const int kNonLockWindowId = 100;
218 static const int kLockWindowId = 200;
220 explicit TestEventClient(RootWindow* root_window)
221 : root_window_(root_window),
222 lock_(false) {
223 client::SetEventClient(root_window_, this);
224 Window* lock_window =
225 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
226 lock_window->set_id(kLockWindowId);
227 Window* non_lock_window =
228 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
229 non_lock_window->set_id(kNonLockWindowId);
231 virtual ~TestEventClient() {
232 client::SetEventClient(root_window_, NULL);
235 // Starts/stops locking. Locking prevents windows other than those inside
236 // the lock container from receiving events, getting focus etc.
237 void Lock() {
238 lock_ = true;
240 void Unlock() {
241 lock_ = false;
244 Window* GetLockWindow() {
245 return const_cast<Window*>(
246 static_cast<const TestEventClient*>(this)->GetLockWindow());
248 const Window* GetLockWindow() const {
249 return root_window_->GetChildById(kLockWindowId);
251 Window* GetNonLockWindow() {
252 return root_window_->GetChildById(kNonLockWindowId);
255 private:
256 // Overridden from client::EventClient:
257 virtual bool CanProcessEventsWithinSubtree(
258 const Window* window) const OVERRIDE {
259 return lock_ ?
260 window->Contains(GetLockWindow()) || GetLockWindow()->Contains(window) :
261 true;
264 virtual ui::EventTarget* GetToplevelEventTarget() OVERRIDE {
265 return NULL;
268 RootWindow* root_window_;
269 bool lock_;
271 DISALLOW_COPY_AND_ASSIGN(TestEventClient);
274 } // namespace
276 TEST_F(RootWindowTest, CanProcessEventsWithinSubtree) {
277 TestEventClient client(root_window());
278 test::TestWindowDelegate d;
280 test::TestEventHandler* nonlock_ef = new test::TestEventHandler;
281 test::TestEventHandler* lock_ef = new test::TestEventHandler;
282 client.GetNonLockWindow()->SetEventFilter(nonlock_ef);
283 client.GetLockWindow()->SetEventFilter(lock_ef);
285 Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
286 client.GetNonLockWindow());
287 w1->set_id(1);
288 Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
289 client.GetNonLockWindow());
290 w2->set_id(2);
291 scoped_ptr<Window> w3(
292 test::CreateTestWindowWithDelegate(&d, 3, gfx::Rect(30, 30, 20, 20),
293 client.GetLockWindow()));
295 w1->Focus();
296 EXPECT_TRUE(IsFocusedWindow(w1));
298 client.Lock();
300 // Since we're locked, the attempt to focus w2 will be ignored.
301 w2->Focus();
302 EXPECT_TRUE(IsFocusedWindow(w1));
303 EXPECT_FALSE(IsFocusedWindow(w2));
306 // Attempting to send a key event to w1 (not in the lock container) should
307 // cause focus to be reset.
308 test::EventGenerator generator(root_window());
309 generator.PressKey(ui::VKEY_SPACE, 0);
310 EXPECT_EQ(NULL, client::GetFocusClient(w1)->GetFocusedWindow());
314 // Events sent to a window not in the lock container will not be processed.
315 // i.e. never sent to the non-lock container's event filter.
316 test::EventGenerator generator(root_window(), w1);
317 generator.PressLeftButton();
318 EXPECT_EQ(0, nonlock_ef->num_mouse_events());
320 // Events sent to a window in the lock container will be processed.
321 test::EventGenerator generator3(root_window(), w3.get());
322 generator3.PressLeftButton();
323 EXPECT_EQ(1, lock_ef->num_mouse_events());
326 // Prevent w3 from being deleted by the hierarchy since its delegate is owned
327 // by this scope.
328 w3->parent()->RemoveChild(w3.get());
331 TEST_F(RootWindowTest, IgnoreUnknownKeys) {
332 test::TestEventHandler* filter = new ConsumeKeyHandler;
333 root_window()->SetEventFilter(filter); // passes ownership
335 ui::KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0, false);
336 EXPECT_FALSE(root_window()->AsRootWindowHostDelegate()->OnHostKeyEvent(
337 &unknown_event));
338 EXPECT_EQ(0, filter->num_key_events());
340 ui::KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false);
341 EXPECT_TRUE(root_window()->AsRootWindowHostDelegate()->OnHostKeyEvent(
342 &known_event));
343 EXPECT_EQ(1, filter->num_key_events());
346 // Tests that touch-events that are beyond the bounds of the root-window do get
347 // propagated to the event filters correctly with the root as the target.
348 TEST_F(RootWindowTest, TouchEventsOutsideBounds) {
349 test::TestEventHandler* filter = new test::TestEventHandler;
350 root_window()->SetEventFilter(filter); // passes ownership
352 gfx::Point position = root_window()->bounds().origin();
353 position.Offset(-10, -10);
354 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position, 0, base::TimeDelta());
355 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
356 EXPECT_EQ(1, filter->num_touch_events());
358 position = root_window()->bounds().origin();
359 position.Offset(root_window()->bounds().width() + 10,
360 root_window()->bounds().height() + 10);
361 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, position, 0, base::TimeDelta());
362 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
363 EXPECT_EQ(2, filter->num_touch_events());
366 // Tests that scroll events are dispatched correctly.
367 TEST_F(RootWindowTest, ScrollEventDispatch) {
368 base::TimeDelta now = ui::EventTimeForNow();
369 test::TestEventHandler* filter = new test::TestEventHandler;
370 root_window()->SetEventFilter(filter);
372 test::TestWindowDelegate delegate;
373 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
374 w1->SetBounds(gfx::Rect(20, 20, 40, 40));
376 // A scroll event on the root-window itself is dispatched.
377 ui::ScrollEvent scroll1(ui::ET_SCROLL,
378 gfx::Point(10, 10),
379 now,
381 0, -10,
382 0, -10,
384 root_window()->AsRootWindowHostDelegate()->OnHostScrollEvent(&scroll1);
385 EXPECT_EQ(1, filter->num_scroll_events());
387 // Scroll event on a window should be dispatched properly.
388 ui::ScrollEvent scroll2(ui::ET_SCROLL,
389 gfx::Point(25, 30),
390 now,
392 -10, 0,
393 -10, 0,
395 root_window()->AsRootWindowHostDelegate()->OnHostScrollEvent(&scroll2);
396 EXPECT_EQ(2, filter->num_scroll_events());
399 namespace {
401 // FilterFilter that tracks the types of events it's seen.
402 class EventFilterRecorder : public ui::EventHandler {
403 public:
404 typedef std::vector<ui::EventType> Events;
405 typedef std::vector<gfx::Point> MouseEventLocations;
407 EventFilterRecorder() {}
409 Events& events() { return events_; }
411 MouseEventLocations& mouse_locations() { return mouse_locations_; }
412 gfx::Point mouse_location(int i) const { return mouse_locations_[i]; }
414 // ui::EventHandler overrides:
415 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
416 events_.push_back(event->type());
419 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
420 events_.push_back(event->type());
421 mouse_locations_.push_back(event->location());
424 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE {
425 events_.push_back(event->type());
428 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
429 events_.push_back(event->type());
432 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
433 events_.push_back(event->type());
436 private:
437 Events events_;
438 MouseEventLocations mouse_locations_;
440 DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder);
443 // Converts an EventType to a string.
444 std::string EventTypeToString(ui::EventType type) {
445 switch (type) {
446 case ui::ET_TOUCH_RELEASED:
447 return "TOUCH_RELEASED";
449 case ui::ET_TOUCH_PRESSED:
450 return "TOUCH_PRESSED";
452 case ui::ET_TOUCH_MOVED:
453 return "TOUCH_MOVED";
455 case ui::ET_MOUSE_PRESSED:
456 return "MOUSE_PRESSED";
458 case ui::ET_MOUSE_DRAGGED:
459 return "MOUSE_DRAGGED";
461 case ui::ET_MOUSE_RELEASED:
462 return "MOUSE_RELEASED";
464 case ui::ET_MOUSE_MOVED:
465 return "MOUSE_MOVED";
467 case ui::ET_MOUSE_ENTERED:
468 return "MOUSE_ENTERED";
470 case ui::ET_MOUSE_EXITED:
471 return "MOUSE_EXITED";
473 case ui::ET_GESTURE_SCROLL_BEGIN:
474 return "GESTURE_SCROLL_BEGIN";
476 case ui::ET_GESTURE_SCROLL_END:
477 return "GESTURE_SCROLL_END";
479 case ui::ET_GESTURE_SCROLL_UPDATE:
480 return "GESTURE_SCROLL_UPDATE";
482 case ui::ET_GESTURE_TAP:
483 return "GESTURE_TAP";
485 case ui::ET_GESTURE_TAP_DOWN:
486 return "GESTURE_TAP_DOWN";
488 case ui::ET_GESTURE_BEGIN:
489 return "GESTURE_BEGIN";
491 case ui::ET_GESTURE_END:
492 return "GESTURE_END";
494 default:
495 break;
497 return "";
500 std::string EventTypesToString(const EventFilterRecorder::Events& events) {
501 std::string result;
502 for (size_t i = 0; i < events.size(); ++i) {
503 if (i != 0)
504 result += " ";
505 result += EventTypeToString(events[i]);
507 return result;
510 } // namespace
512 TEST_F(RootWindowTest, MouseMovesHeld) {
513 EventFilterRecorder* filter = new EventFilterRecorder;
514 root_window()->SetEventFilter(filter); // passes ownership
516 test::TestWindowDelegate delegate;
517 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
518 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
520 ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0),
521 gfx::Point(0, 0), 0);
522 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
523 &mouse_move_event);
524 // Discard MOUSE_ENTER.
525 filter->events().clear();
527 root_window()->HoldPointerMoves();
529 // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
530 ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
531 gfx::Point(0, 0), 0);
532 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
533 &mouse_dragged_event);
534 EXPECT_TRUE(filter->events().empty());
536 // Check that we do dispatch the held MOUSE_DRAGGED event before another type
537 // of event.
538 ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
539 gfx::Point(0, 0), 0);
540 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
541 &mouse_pressed_event);
542 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
543 EventTypesToString(filter->events()));
544 filter->events().clear();
546 // Check that we coalesce held MOUSE_DRAGGED events.
547 ui::MouseEvent mouse_dragged_event2(ui::ET_MOUSE_DRAGGED, gfx::Point(1, 1),
548 gfx::Point(1, 1), 0);
549 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
550 &mouse_dragged_event);
551 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
552 &mouse_dragged_event2);
553 EXPECT_TRUE(filter->events().empty());
554 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
555 &mouse_pressed_event);
556 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
557 EventTypesToString(filter->events()));
558 filter->events().clear();
560 // Check that on ReleasePointerMoves, held events are not dispatched
561 // immediately, but posted instead.
562 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
563 &mouse_dragged_event);
564 root_window()->ReleasePointerMoves();
565 EXPECT_TRUE(filter->events().empty());
566 RunAllPendingInMessageLoop();
567 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(filter->events()));
568 filter->events().clear();
570 // However if another message comes in before the dispatch,
571 // the Check that on ReleasePointerMoves, held events are not dispatched
572 // immediately, but posted instead.
573 root_window()->HoldPointerMoves();
574 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
575 &mouse_dragged_event);
576 root_window()->ReleasePointerMoves();
577 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
578 &mouse_pressed_event);
579 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
580 EventTypesToString(filter->events()));
581 filter->events().clear();
582 RunAllPendingInMessageLoop();
583 EXPECT_TRUE(filter->events().empty());
585 // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
586 // them.
587 root_window()->HoldPointerMoves();
588 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
589 &mouse_dragged_event);
590 root_window()->ReleasePointerMoves();
591 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
592 &mouse_dragged_event2);
593 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(filter->events()));
594 filter->events().clear();
595 RunAllPendingInMessageLoop();
596 EXPECT_TRUE(filter->events().empty());
599 TEST_F(RootWindowTest, TouchMovesHeld) {
600 EventFilterRecorder* filter = new EventFilterRecorder;
601 root_window()->SetEventFilter(filter); // passes ownership
603 test::TestWindowDelegate delegate;
604 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
605 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
607 // Starting the touch and throwing out the first few events, since the system
608 // is going to generate synthetic mouse events that are not relevant to the
609 // test.
610 ui::TouchEvent touch_pressed_event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
611 0, base::TimeDelta());
612 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
613 &touch_pressed_event);
614 RunAllPendingInMessageLoop();
615 filter->events().clear();
617 root_window()->HoldPointerMoves();
619 // Check that we don't immediately dispatch the TOUCH_MOVED event.
620 ui::TouchEvent touch_moved_event(ui::ET_TOUCH_MOVED, gfx::Point(0, 0),
621 0, base::TimeDelta());
622 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
623 &touch_moved_event);
624 EXPECT_TRUE(filter->events().empty());
626 // Check that on ReleasePointerMoves, held events are not dispatched
627 // immediately, but posted instead.
628 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
629 &touch_moved_event);
630 root_window()->ReleasePointerMoves();
631 EXPECT_TRUE(filter->events().empty());
632 RunAllPendingInMessageLoop();
633 EXPECT_EQ("TOUCH_MOVED", EventTypesToString(filter->events()));
634 filter->events().clear();
636 // If another touch event occurs then the held touch should be dispatched
637 // immediately before it.
638 ui::TouchEvent touch_released_event(ui::ET_TOUCH_RELEASED, gfx::Point(0, 0),
639 0, base::TimeDelta());
640 filter->events().clear();
641 root_window()->HoldPointerMoves();
642 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
643 &touch_moved_event);
644 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
645 &touch_released_event);
646 EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_END",
647 EventTypesToString(filter->events()));
648 filter->events().clear();
649 root_window()->ReleasePointerMoves();
650 RunAllPendingInMessageLoop();
651 EXPECT_TRUE(filter->events().empty());
654 // Tests that synthetic mouse events are ignored when mouse
655 // events are disabled.
656 TEST_F(RootWindowTest, DispatchSyntheticMouseEvents) {
657 EventFilterRecorder* filter = new EventFilterRecorder;
658 root_window()->SetEventFilter(filter); // passes ownership
660 test::TestWindowDelegate delegate;
661 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
662 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
663 window->Show();
664 window->SetCapture();
666 test::TestCursorClient cursor_client(root_window());
668 // Dispatch a non-synthetic mouse event when mouse events are enabled.
669 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
670 gfx::Point(10, 10), 0);
671 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse1);
672 EXPECT_FALSE(filter->events().empty());
673 filter->events().clear();
675 // Dispatch a synthetic mouse event when mouse events are enabled.
676 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
677 gfx::Point(10, 10), ui::EF_IS_SYNTHESIZED);
678 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse2);
679 EXPECT_FALSE(filter->events().empty());
680 filter->events().clear();
682 // Dispatch a synthetic mouse event when mouse events are disabled.
683 cursor_client.DisableMouseEvents();
684 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse2);
685 EXPECT_TRUE(filter->events().empty());
688 // Tests that a mouse exit is dispatched to the last known cursor location
689 // when the cursor becomes invisible.
690 TEST_F(RootWindowTest, DispatchMouseExitWhenCursorHidden) {
691 EventFilterRecorder* filter = new EventFilterRecorder;
692 root_window()->SetEventFilter(filter); // passes ownership
694 test::TestWindowDelegate delegate;
695 gfx::Point window_origin(7, 18);
696 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
697 &delegate, 1234, gfx::Rect(window_origin,
698 gfx::Size(100, 100)), root_window()));
699 window->Show();
701 // Dispatch a mouse move event into the window.
702 gfx::Point mouse_location(gfx::Point(15, 25));
703 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location,
704 mouse_location, 0);
705 EXPECT_TRUE(filter->events().empty());
706 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse1);
707 EXPECT_FALSE(filter->events().empty());
708 filter->events().clear();
710 // Hide the cursor and verify a mouse exit was dispatched.
711 root_window()->OnCursorVisibilityChanged(false);
712 EXPECT_FALSE(filter->events().empty());
713 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(filter->events()));
715 // Verify the mouse exit was dispatched at the correct location
716 // (in the correct coordinate space).
717 int translated_x = mouse_location.x() - window_origin.x();
718 int translated_y = mouse_location.y() - window_origin.y();
719 gfx::Point translated_point(translated_x, translated_y);
720 EXPECT_EQ(filter->mouse_location(0).ToString(), translated_point.ToString());
723 class DeletingEventFilter : public ui::EventHandler {
724 public:
725 DeletingEventFilter()
726 : delete_during_pre_handle_(false) {}
727 virtual ~DeletingEventFilter() {}
729 void Reset(bool delete_during_pre_handle) {
730 delete_during_pre_handle_ = delete_during_pre_handle;
733 private:
734 // Overridden from ui::EventHandler:
735 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
736 if (delete_during_pre_handle_)
737 delete event->target();
740 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
741 if (delete_during_pre_handle_)
742 delete event->target();
745 bool delete_during_pre_handle_;
747 DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter);
750 class DeletingWindowDelegate : public test::TestWindowDelegate {
751 public:
752 DeletingWindowDelegate()
753 : window_(NULL),
754 delete_during_handle_(false),
755 got_event_(false) {}
756 virtual ~DeletingWindowDelegate() {}
758 void Reset(Window* window, bool delete_during_handle) {
759 window_ = window;
760 delete_during_handle_ = delete_during_handle;
761 got_event_ = false;
763 bool got_event() const { return got_event_; }
765 private:
766 // Overridden from WindowDelegate:
767 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
768 if (delete_during_handle_)
769 delete window_;
770 got_event_ = true;
773 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
774 if (delete_during_handle_)
775 delete window_;
776 got_event_ = true;
779 Window* window_;
780 bool delete_during_handle_;
781 bool got_event_;
783 DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate);
786 TEST_F(RootWindowTest, DeleteWindowDuringDispatch) {
787 // Verifies that we can delete a window during each phase of event handling.
788 // Deleting the window should not cause a crash, only prevent further
789 // processing from occurring.
790 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
791 DeletingWindowDelegate d11;
792 Window* w11 = CreateNormalWindow(11, w1.get(), &d11);
793 WindowTracker tracker;
794 DeletingEventFilter* w1_filter = new DeletingEventFilter;
795 w1->SetEventFilter(w1_filter);
796 client::GetFocusClient(w1.get())->FocusWindow(w11);
798 test::EventGenerator generator(root_window(), w11);
800 // First up, no one deletes anything.
801 tracker.Add(w11);
802 d11.Reset(w11, false);
804 generator.PressLeftButton();
805 EXPECT_TRUE(tracker.Contains(w11));
806 EXPECT_TRUE(d11.got_event());
807 generator.ReleaseLeftButton();
809 // Delegate deletes w11. This will prevent the post-handle step from applying.
810 w1_filter->Reset(false);
811 d11.Reset(w11, true);
812 generator.PressKey(ui::VKEY_A, 0);
813 EXPECT_FALSE(tracker.Contains(w11));
814 EXPECT_TRUE(d11.got_event());
816 // Pre-handle step deletes w11. This will prevent the delegate and the post-
817 // handle steps from applying.
818 w11 = CreateNormalWindow(11, w1.get(), &d11);
819 w1_filter->Reset(true);
820 d11.Reset(w11, false);
821 generator.PressLeftButton();
822 EXPECT_FALSE(tracker.Contains(w11));
823 EXPECT_FALSE(d11.got_event());
826 namespace {
828 // A window delegate that detaches the parent of the target's parent window when
829 // it receives a tap event.
830 class DetachesParentOnTapDelegate : public test::TestWindowDelegate {
831 public:
832 DetachesParentOnTapDelegate() {}
833 virtual ~DetachesParentOnTapDelegate() {}
835 private:
836 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
837 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
838 event->SetHandled();
839 return;
842 if (event->type() == ui::ET_GESTURE_TAP) {
843 Window* parent = static_cast<Window*>(event->target())->parent();
844 parent->parent()->RemoveChild(parent);
845 event->SetHandled();
849 DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate);
852 } // namespace
854 // Tests that the gesture recognizer is reset for all child windows when a
855 // window hides. No expectations, just checks that the test does not crash.
856 TEST_F(RootWindowTest, GestureRecognizerResetsTargetWhenParentHides) {
857 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
858 DetachesParentOnTapDelegate delegate;
859 scoped_ptr<Window> parent(CreateNormalWindow(22, w1.get(), NULL));
860 Window* child = CreateNormalWindow(11, parent.get(), &delegate);
861 test::EventGenerator generator(root_window(), child);
862 generator.GestureTapAt(gfx::Point(40, 40));
865 namespace {
867 // A window delegate that processes nested gestures on tap.
868 class NestedGestureDelegate : public test::TestWindowDelegate {
869 public:
870 NestedGestureDelegate(test::EventGenerator* generator,
871 const gfx::Point tap_location)
872 : generator_(generator),
873 tap_location_(tap_location),
874 gesture_end_count_(0) {}
875 virtual ~NestedGestureDelegate() {}
877 int gesture_end_count() const { return gesture_end_count_; }
879 private:
880 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
881 switch (event->type()) {
882 case ui::ET_GESTURE_TAP_DOWN:
883 event->SetHandled();
884 break;
885 case ui::ET_GESTURE_TAP:
886 if (generator_)
887 generator_->GestureTapAt(tap_location_);
888 event->SetHandled();
889 break;
890 case ui::ET_GESTURE_END:
891 ++gesture_end_count_;
892 break;
893 default:
894 break;
898 test::EventGenerator* generator_;
899 const gfx::Point tap_location_;
900 int gesture_end_count_;
901 DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate);
904 } // namespace
906 // Tests that gesture end is delivered after nested gesture processing.
907 TEST_F(RootWindowTest, GestureEndDeliveredAfterNestedGestures) {
908 NestedGestureDelegate d1(NULL, gfx::Point());
909 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &d1));
910 w1->SetBounds(gfx::Rect(0, 0, 100, 100));
912 test::EventGenerator nested_generator(root_window(), w1.get());
913 NestedGestureDelegate d2(&nested_generator, w1->bounds().CenterPoint());
914 scoped_ptr<Window> w2(CreateNormalWindow(1, root_window(), &d2));
915 w2->SetBounds(gfx::Rect(100, 0, 100, 100));
917 // Tap on w2 which triggers nested gestures for w1.
918 test::EventGenerator generator(root_window(), w2.get());
919 generator.GestureTapAt(w2->bounds().CenterPoint());
921 // Both windows should get their gesture end events.
922 EXPECT_EQ(1, d1.gesture_end_count());
923 EXPECT_EQ(1, d2.gesture_end_count());
926 // Tests whether we can repost the Tap down gesture event.
927 TEST_F(RootWindowTest, RepostTapdownGestureTest) {
928 EventFilterRecorder* filter = new EventFilterRecorder;
929 root_window()->SetEventFilter(filter); // passes ownership
931 test::TestWindowDelegate delegate;
932 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
933 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
935 ui::GestureEventDetails details(ui::ET_GESTURE_TAP_DOWN, 0.0f, 0.0f);
936 gfx::Point point(10, 10);
937 ui::GestureEvent event(
938 ui::ET_GESTURE_TAP_DOWN,
939 point.x(),
940 point.y(),
942 ui::EventTimeForNow(),
943 details,
945 root_window()->RepostEvent(event);
946 RunAllPendingInMessageLoop();
947 EXPECT_TRUE(EventTypesToString(filter->events()).find("GESTURE_TAP_DOWN") !=
948 std::string::npos);
949 filter->events().clear();
952 // This class inherits from the EventFilterRecorder class which provides a
953 // facility to record events. This class additionally provides a facility to
954 // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
955 // events after that.
956 class RepostGestureEventRecorder : public EventFilterRecorder {
957 public:
958 RepostGestureEventRecorder(aura::Window* repost_source,
959 aura::Window* repost_target)
960 : repost_source_(repost_source),
961 repost_target_(repost_target),
962 reposted_(false) {}
964 virtual ~RepostGestureEventRecorder() {}
966 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
967 EXPECT_EQ(reposted_ ? repost_target_ : repost_source_, event->target());
968 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
969 if (!reposted_) {
970 EXPECT_NE(repost_target_, event->target());
971 reposted_ = true;
972 events().clear();
973 repost_target_->GetRootWindow()->RepostEvent(*event);
974 // Ensure that the reposted gesture event above goes to the
975 // repost_target_;
976 repost_source_->GetRootWindow()->RemoveChild(repost_source_);
977 return;
980 EventFilterRecorder::OnGestureEvent(event);
983 // Ignore mouse events as they don't fire at all times. This causes
984 // the GestureRepostEventOrder test to fail randomly.
985 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {}
987 private:
988 aura::Window* repost_source_;
989 aura::Window* repost_target_;
990 // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
991 bool reposted_;
992 DISALLOW_COPY_AND_ASSIGN(RepostGestureEventRecorder);
995 // Tests whether events which are generated after the reposted gesture event
996 // are received after that. In this case the scroll sequence events should
997 // be received after the reposted gesture event.
998 TEST_F(RootWindowTest, GestureRepostEventOrder) {
999 // Expected events at the end for the repost_target window defined below.
1000 const char kExpectedTargetEvents[] = "GESTURE_BEGIN GESTURE_TAP_DOWN "
1001 "TOUCH_RELEASED TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1002 " GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE TOUCH_MOVED "
1003 "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
1004 "GESTURE_SCROLL_END GESTURE_END";
1005 // We create two windows.
1006 // The first window (repost_source) is the one to which the initial tap
1007 // gesture is sent. It reposts this event to the second window
1008 // (repost_target).
1009 // We then generate the scroll sequence for repost_target and look for two
1010 // ET_GESTURE_TAP_DOWN events in the event list at the end.
1011 test::TestWindowDelegate delegate;
1012 scoped_ptr<aura::Window> repost_target(CreateTestWindowWithDelegate(
1013 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1015 scoped_ptr<aura::Window> repost_source(CreateTestWindowWithDelegate(
1016 &delegate, 1, gfx::Rect(0, 0, 50, 50), root_window()));
1018 RepostGestureEventRecorder* repost_event_recorder =
1019 new RepostGestureEventRecorder(repost_source.get(), repost_target.get());
1020 root_window()->SetEventFilter(repost_event_recorder); // passes ownership
1022 // Generate a tap down gesture for the repost_source. This will be reposted
1023 // to repost_target.
1024 test::EventGenerator repost_generator(root_window(), repost_source.get());
1025 repost_generator.GestureTapAt(gfx::Point(40, 40));
1026 RunAllPendingInMessageLoop();
1028 test::EventGenerator scroll_generator(root_window(), repost_target.get());
1029 scroll_generator.GestureScrollSequence(
1030 gfx::Point(80, 80),
1031 gfx::Point(100, 100),
1032 base::TimeDelta::FromMilliseconds(100),
1034 RunAllPendingInMessageLoop();
1036 int tap_down_count = 0;
1037 for (size_t i = 0; i < repost_event_recorder->events().size(); ++i) {
1038 if (repost_event_recorder->events()[i] == ui::ET_GESTURE_TAP_DOWN)
1039 ++tap_down_count;
1042 // We expect two tap down events. One from the repost and the other one from
1043 // the scroll sequence posted above.
1044 EXPECT_EQ(tap_down_count, 2);
1046 EXPECT_EQ(kExpectedTargetEvents,
1047 EventTypesToString(repost_event_recorder->events()));
1050 class OnMouseExitDeletingEventFilter : public EventFilterRecorder {
1051 public:
1052 OnMouseExitDeletingEventFilter() : window_to_delete_(NULL) {}
1053 virtual ~OnMouseExitDeletingEventFilter() {}
1055 void set_window_to_delete(Window* window_to_delete) {
1056 window_to_delete_ = window_to_delete;
1059 private:
1060 // Overridden from ui::EventHandler:
1061 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
1062 EventFilterRecorder::OnMouseEvent(event);
1063 if (window_to_delete_) {
1064 delete window_to_delete_;
1065 window_to_delete_ = NULL;
1069 Window* window_to_delete_;
1071 DISALLOW_COPY_AND_ASSIGN(OnMouseExitDeletingEventFilter);
1074 // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
1075 // a child, but the child is destroyed because of the synthesized mouse-exit
1076 // event generated on the previous mouse_moved_handler_.
1077 TEST_F(RootWindowTest, DeleteWindowDuringMouseMovedDispatch) {
1078 // Create window 1 and set its event filter. Window 1 will take ownership of
1079 // the event filter.
1080 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1081 OnMouseExitDeletingEventFilter* w1_filter =
1082 new OnMouseExitDeletingEventFilter();
1083 w1->SetEventFilter(w1_filter);
1084 w1->SetBounds(gfx::Rect(20, 20, 60, 60));
1085 EXPECT_EQ(NULL, root_window()->mouse_moved_handler());
1087 test::EventGenerator generator(root_window(), w1.get());
1089 // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
1090 // root window.
1091 generator.MoveMouseTo(51, 51);
1092 EXPECT_EQ(w1.get(), root_window()->mouse_moved_handler());
1094 // Create window 2 under the mouse cursor and stack it above window 1.
1095 Window* w2 = CreateNormalWindow(2, root_window(), NULL);
1096 w2->SetBounds(gfx::Rect(30, 30, 40, 40));
1097 root_window()->StackChildAbove(w2, w1.get());
1099 // Set window 2 as the window that is to be deleted when a mouse-exited event
1100 // happens on window 1.
1101 w1_filter->set_window_to_delete(w2);
1103 // Move mosue over window 2. This should generate a mouse-exited event for
1104 // window 1 resulting in deletion of window 2. The original mouse-moved event
1105 // that was targeted to window 2 should be dropped since window 2 is
1106 // destroyed. This test passes if no crash happens.
1107 generator.MoveMouseTo(52, 52);
1108 EXPECT_EQ(NULL, root_window()->mouse_moved_handler());
1110 // Check events received by window 1.
1111 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
1112 EventTypesToString(w1_filter->events()));
1115 } // namespace aura