Remove support for specifying version on command line.
[chromium-blink-merge.git] / ui / aura / root_window_unittest.cc
blob645e2f99eec274923d69f836ae3aa0a25290e2b9
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 "base/bind.h"
10 #include "base/run_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/aura/client/event_client.h"
13 #include "ui/aura/client/focus_client.h"
14 #include "ui/aura/env.h"
15 #include "ui/aura/test/aura_test_base.h"
16 #include "ui/aura/test/event_generator.h"
17 #include "ui/aura/test/test_cursor_client.h"
18 #include "ui/aura/test/test_event_handler.h"
19 #include "ui/aura/test/test_window_delegate.h"
20 #include "ui/aura/test/test_windows.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_tracker.h"
23 #include "ui/base/hit_test.h"
24 #include "ui/events/event.h"
25 #include "ui/events/event_handler.h"
26 #include "ui/events/event_utils.h"
27 #include "ui/events/gestures/gesture_configuration.h"
28 #include "ui/events/keycodes/keyboard_codes.h"
29 #include "ui/gfx/point.h"
30 #include "ui/gfx/rect.h"
31 #include "ui/gfx/screen.h"
32 #include "ui/gfx/transform.h"
34 namespace aura {
35 namespace {
37 // A delegate that always returns a non-client component for hit tests.
38 class NonClientDelegate : public test::TestWindowDelegate {
39 public:
40 NonClientDelegate()
41 : non_client_count_(0),
42 mouse_event_count_(0),
43 mouse_event_flags_(0x0) {
45 virtual ~NonClientDelegate() {}
47 int non_client_count() const { return non_client_count_; }
48 gfx::Point non_client_location() const { return non_client_location_; }
49 int mouse_event_count() const { return mouse_event_count_; }
50 gfx::Point mouse_event_location() const { return mouse_event_location_; }
51 int mouse_event_flags() const { return mouse_event_flags_; }
53 virtual int GetNonClientComponent(const gfx::Point& location) const OVERRIDE {
54 NonClientDelegate* self = const_cast<NonClientDelegate*>(this);
55 self->non_client_count_++;
56 self->non_client_location_ = location;
57 return HTTOPLEFT;
59 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
60 mouse_event_count_++;
61 mouse_event_location_ = event->location();
62 mouse_event_flags_ = event->flags();
63 event->SetHandled();
66 private:
67 int non_client_count_;
68 gfx::Point non_client_location_;
69 int mouse_event_count_;
70 gfx::Point mouse_event_location_;
71 int mouse_event_flags_;
73 DISALLOW_COPY_AND_ASSIGN(NonClientDelegate);
76 // A simple event handler that consumes key events.
77 class ConsumeKeyHandler : public test::TestEventHandler {
78 public:
79 ConsumeKeyHandler() {}
80 virtual ~ConsumeKeyHandler() {}
82 // Overridden from ui::EventHandler:
83 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
84 test::TestEventHandler::OnKeyEvent(event);
85 event->StopPropagation();
88 private:
89 DISALLOW_COPY_AND_ASSIGN(ConsumeKeyHandler);
92 bool IsFocusedWindow(aura::Window* window) {
93 return client::GetFocusClient(window)->GetFocusedWindow() == window;
96 } // namespace
98 typedef test::AuraTestBase RootWindowTest;
100 TEST_F(RootWindowTest, OnHostMouseEvent) {
101 // Create two non-overlapping windows so we don't have to worry about which
102 // is on top.
103 scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate());
104 scoped_ptr<NonClientDelegate> delegate2(new NonClientDelegate());
105 const int kWindowWidth = 123;
106 const int kWindowHeight = 45;
107 gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight);
108 gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight);
109 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
110 delegate1.get(), -1234, bounds1, root_window()));
111 scoped_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
112 delegate2.get(), -5678, bounds2, root_window()));
114 // Send a mouse event to window1.
115 gfx::Point point(101, 201);
116 ui::MouseEvent event1(
117 ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON,
118 ui::EF_LEFT_MOUSE_BUTTON);
119 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(&event1);
121 // Event was tested for non-client area for the target window.
122 EXPECT_EQ(1, delegate1->non_client_count());
123 EXPECT_EQ(0, delegate2->non_client_count());
124 // The non-client component test was in local coordinates.
125 EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location());
126 // Mouse event was received by target window.
127 EXPECT_EQ(1, delegate1->mouse_event_count());
128 EXPECT_EQ(0, delegate2->mouse_event_count());
129 // Event was in local coordinates.
130 EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location());
131 // Non-client flag was set.
132 EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT);
135 TEST_F(RootWindowTest, RepostEvent) {
136 // Test RepostEvent in RootWindow. It only works for Mouse Press.
137 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
138 gfx::Point point(10, 10);
139 ui::MouseEvent event(
140 ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON,
141 ui::EF_LEFT_MOUSE_BUTTON);
142 dispatcher()->RepostEvent(event);
143 RunAllPendingInMessageLoop();
144 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
147 // Check that we correctly track the state of the mouse buttons in response to
148 // button press and release events.
149 TEST_F(RootWindowTest, MouseButtonState) {
150 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
152 gfx::Point location;
153 scoped_ptr<ui::MouseEvent> event;
155 // Press the left button.
156 event.reset(new ui::MouseEvent(
157 ui::ET_MOUSE_PRESSED,
158 location,
159 location,
160 ui::EF_LEFT_MOUSE_BUTTON,
161 ui::EF_LEFT_MOUSE_BUTTON));
162 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(event.get());
163 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
165 // Additionally press the right.
166 event.reset(new ui::MouseEvent(
167 ui::ET_MOUSE_PRESSED,
168 location,
169 location,
170 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON,
171 ui::EF_RIGHT_MOUSE_BUTTON));
172 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(event.get());
173 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
175 // Release the left button.
176 event.reset(new ui::MouseEvent(
177 ui::ET_MOUSE_RELEASED,
178 location,
179 location,
180 ui::EF_RIGHT_MOUSE_BUTTON,
181 ui::EF_LEFT_MOUSE_BUTTON));
182 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(event.get());
183 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
185 // Release the right button. We should ignore the Shift-is-down flag.
186 event.reset(new ui::MouseEvent(
187 ui::ET_MOUSE_RELEASED,
188 location,
189 location,
190 ui::EF_SHIFT_DOWN,
191 ui::EF_RIGHT_MOUSE_BUTTON));
192 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(event.get());
193 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
195 // Press the middle button.
196 event.reset(new ui::MouseEvent(
197 ui::ET_MOUSE_PRESSED,
198 location,
199 location,
200 ui::EF_MIDDLE_MOUSE_BUTTON,
201 ui::EF_MIDDLE_MOUSE_BUTTON));
202 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(event.get());
203 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
206 TEST_F(RootWindowTest, TranslatedEvent) {
207 scoped_ptr<Window> w1(test::CreateTestWindowWithDelegate(NULL, 1,
208 gfx::Rect(50, 50, 100, 100), root_window()));
210 gfx::Point origin(100, 100);
211 ui::MouseEvent root(ui::ET_MOUSE_PRESSED, origin, origin, 0, 0);
213 EXPECT_EQ("100,100", root.location().ToString());
214 EXPECT_EQ("100,100", root.root_location().ToString());
216 ui::MouseEvent translated_event(
217 root, static_cast<Window*>(root_window()), w1.get(),
218 ui::ET_MOUSE_ENTERED, root.flags());
219 EXPECT_EQ("50,50", translated_event.location().ToString());
220 EXPECT_EQ("100,100", translated_event.root_location().ToString());
223 namespace {
225 class TestEventClient : public client::EventClient {
226 public:
227 static const int kNonLockWindowId = 100;
228 static const int kLockWindowId = 200;
230 explicit TestEventClient(Window* root_window)
231 : root_window_(root_window),
232 lock_(false) {
233 client::SetEventClient(root_window_, this);
234 Window* lock_window =
235 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
236 lock_window->set_id(kLockWindowId);
237 Window* non_lock_window =
238 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
239 non_lock_window->set_id(kNonLockWindowId);
241 virtual ~TestEventClient() {
242 client::SetEventClient(root_window_, NULL);
245 // Starts/stops locking. Locking prevents windows other than those inside
246 // the lock container from receiving events, getting focus etc.
247 void Lock() {
248 lock_ = true;
250 void Unlock() {
251 lock_ = false;
254 Window* GetLockWindow() {
255 return const_cast<Window*>(
256 static_cast<const TestEventClient*>(this)->GetLockWindow());
258 const Window* GetLockWindow() const {
259 return root_window_->GetChildById(kLockWindowId);
261 Window* GetNonLockWindow() {
262 return root_window_->GetChildById(kNonLockWindowId);
265 private:
266 // Overridden from client::EventClient:
267 virtual bool CanProcessEventsWithinSubtree(
268 const Window* window) const OVERRIDE {
269 return lock_ ?
270 window->Contains(GetLockWindow()) || GetLockWindow()->Contains(window) :
271 true;
274 virtual ui::EventTarget* GetToplevelEventTarget() OVERRIDE {
275 return NULL;
278 Window* root_window_;
279 bool lock_;
281 DISALLOW_COPY_AND_ASSIGN(TestEventClient);
284 } // namespace
286 TEST_F(RootWindowTest, CanProcessEventsWithinSubtree) {
287 TestEventClient client(root_window());
288 test::TestWindowDelegate d;
290 test::TestEventHandler* nonlock_ef = new test::TestEventHandler;
291 test::TestEventHandler* lock_ef = new test::TestEventHandler;
292 client.GetNonLockWindow()->SetEventFilter(nonlock_ef);
293 client.GetLockWindow()->SetEventFilter(lock_ef);
295 Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
296 client.GetNonLockWindow());
297 w1->set_id(1);
298 Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
299 client.GetNonLockWindow());
300 w2->set_id(2);
301 scoped_ptr<Window> w3(
302 test::CreateTestWindowWithDelegate(&d, 3, gfx::Rect(30, 30, 20, 20),
303 client.GetLockWindow()));
305 w1->Focus();
306 EXPECT_TRUE(IsFocusedWindow(w1));
308 client.Lock();
310 // Since we're locked, the attempt to focus w2 will be ignored.
311 w2->Focus();
312 EXPECT_TRUE(IsFocusedWindow(w1));
313 EXPECT_FALSE(IsFocusedWindow(w2));
316 // Attempting to send a key event to w1 (not in the lock container) should
317 // cause focus to be reset.
318 test::EventGenerator generator(root_window());
319 generator.PressKey(ui::VKEY_SPACE, 0);
320 EXPECT_EQ(NULL, client::GetFocusClient(w1)->GetFocusedWindow());
324 // Events sent to a window not in the lock container will not be processed.
325 // i.e. never sent to the non-lock container's event filter.
326 test::EventGenerator generator(root_window(), w1);
327 generator.PressLeftButton();
328 EXPECT_EQ(0, nonlock_ef->num_mouse_events());
330 // Events sent to a window in the lock container will be processed.
331 test::EventGenerator generator3(root_window(), w3.get());
332 generator3.PressLeftButton();
333 EXPECT_EQ(1, lock_ef->num_mouse_events());
336 // Prevent w3 from being deleted by the hierarchy since its delegate is owned
337 // by this scope.
338 w3->parent()->RemoveChild(w3.get());
341 TEST_F(RootWindowTest, IgnoreUnknownKeys) {
342 test::TestEventHandler* filter = new ConsumeKeyHandler;
343 root_window()->SetEventFilter(filter); // passes ownership
345 ui::KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0, false);
346 EXPECT_FALSE(dispatcher()->AsWindowTreeHostDelegate()->OnHostKeyEvent(
347 &unknown_event));
348 EXPECT_EQ(0, filter->num_key_events());
350 ui::KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false);
351 EXPECT_TRUE(dispatcher()->AsWindowTreeHostDelegate()->OnHostKeyEvent(
352 &known_event));
353 EXPECT_EQ(1, filter->num_key_events());
356 TEST_F(RootWindowTest, NoDelegateWindowReceivesKeyEvents) {
357 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
358 w1->Show();
359 w1->Focus();
361 test::TestEventHandler handler;
362 w1->AddPreTargetHandler(&handler);
363 ui::KeyEvent key_press(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false);
364 EXPECT_TRUE(dispatcher()->AsWindowTreeHostDelegate()->OnHostKeyEvent(
365 &key_press));
366 EXPECT_EQ(1, handler.num_key_events());
368 w1->RemovePreTargetHandler(&handler);
371 // Tests that touch-events that are beyond the bounds of the root-window do get
372 // propagated to the event filters correctly with the root as the target.
373 TEST_F(RootWindowTest, TouchEventsOutsideBounds) {
374 test::TestEventHandler* filter = new test::TestEventHandler;
375 root_window()->SetEventFilter(filter); // passes ownership
377 gfx::Point position = root_window()->bounds().origin();
378 position.Offset(-10, -10);
379 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position, 0, base::TimeDelta());
380 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&press);
381 EXPECT_EQ(1, filter->num_touch_events());
383 position = root_window()->bounds().origin();
384 position.Offset(root_window()->bounds().width() + 10,
385 root_window()->bounds().height() + 10);
386 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, position, 0, base::TimeDelta());
387 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&release);
388 EXPECT_EQ(2, filter->num_touch_events());
391 // Tests that scroll events are dispatched correctly.
392 TEST_F(RootWindowTest, ScrollEventDispatch) {
393 base::TimeDelta now = ui::EventTimeForNow();
394 test::TestEventHandler* filter = new test::TestEventHandler;
395 root_window()->SetEventFilter(filter);
397 test::TestWindowDelegate delegate;
398 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
399 w1->SetBounds(gfx::Rect(20, 20, 40, 40));
401 // A scroll event on the root-window itself is dispatched.
402 ui::ScrollEvent scroll1(ui::ET_SCROLL,
403 gfx::Point(10, 10),
404 now,
406 0, -10,
407 0, -10,
409 dispatcher()->AsWindowTreeHostDelegate()->OnHostScrollEvent(&scroll1);
410 EXPECT_EQ(1, filter->num_scroll_events());
412 // Scroll event on a window should be dispatched properly.
413 ui::ScrollEvent scroll2(ui::ET_SCROLL,
414 gfx::Point(25, 30),
415 now,
417 -10, 0,
418 -10, 0,
420 dispatcher()->AsWindowTreeHostDelegate()->OnHostScrollEvent(&scroll2);
421 EXPECT_EQ(2, filter->num_scroll_events());
424 namespace {
426 // FilterFilter that tracks the types of events it's seen.
427 class EventFilterRecorder : public ui::EventHandler {
428 public:
429 typedef std::vector<ui::EventType> Events;
430 typedef std::vector<gfx::Point> MouseEventLocations;
432 EventFilterRecorder() {}
434 Events& events() { return events_; }
436 MouseEventLocations& mouse_locations() { return mouse_locations_; }
437 gfx::Point mouse_location(int i) const { return mouse_locations_[i]; }
439 Events GetAndResetEvents() {
440 Events events = events_;
441 events_.clear();
442 return events;
445 // ui::EventHandler overrides:
446 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
447 events_.push_back(event->type());
450 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
451 events_.push_back(event->type());
452 mouse_locations_.push_back(event->location());
455 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE {
456 events_.push_back(event->type());
459 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
460 events_.push_back(event->type());
463 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
464 events_.push_back(event->type());
467 private:
468 Events events_;
469 MouseEventLocations mouse_locations_;
471 DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder);
474 // Converts an EventType to a string.
475 std::string EventTypeToString(ui::EventType type) {
476 switch (type) {
477 case ui::ET_TOUCH_RELEASED:
478 return "TOUCH_RELEASED";
480 case ui::ET_TOUCH_CANCELLED:
481 return "TOUCH_CANCELLED";
483 case ui::ET_TOUCH_PRESSED:
484 return "TOUCH_PRESSED";
486 case ui::ET_TOUCH_MOVED:
487 return "TOUCH_MOVED";
489 case ui::ET_MOUSE_PRESSED:
490 return "MOUSE_PRESSED";
492 case ui::ET_MOUSE_DRAGGED:
493 return "MOUSE_DRAGGED";
495 case ui::ET_MOUSE_RELEASED:
496 return "MOUSE_RELEASED";
498 case ui::ET_MOUSE_MOVED:
499 return "MOUSE_MOVED";
501 case ui::ET_MOUSE_ENTERED:
502 return "MOUSE_ENTERED";
504 case ui::ET_MOUSE_EXITED:
505 return "MOUSE_EXITED";
507 case ui::ET_GESTURE_SCROLL_BEGIN:
508 return "GESTURE_SCROLL_BEGIN";
510 case ui::ET_GESTURE_SCROLL_END:
511 return "GESTURE_SCROLL_END";
513 case ui::ET_GESTURE_SCROLL_UPDATE:
514 return "GESTURE_SCROLL_UPDATE";
516 case ui::ET_GESTURE_PINCH_BEGIN:
517 return "GESTURE_PINCH_BEGIN";
519 case ui::ET_GESTURE_PINCH_END:
520 return "GESTURE_PINCH_END";
522 case ui::ET_GESTURE_PINCH_UPDATE:
523 return "GESTURE_PINCH_UPDATE";
525 case ui::ET_GESTURE_TAP:
526 return "GESTURE_TAP";
528 case ui::ET_GESTURE_TAP_DOWN:
529 return "GESTURE_TAP_DOWN";
531 case ui::ET_GESTURE_TAP_CANCEL:
532 return "GESTURE_TAP_CANCEL";
534 case ui::ET_GESTURE_SHOW_PRESS:
535 return "GESTURE_SHOW_PRESS";
537 case ui::ET_GESTURE_BEGIN:
538 return "GESTURE_BEGIN";
540 case ui::ET_GESTURE_END:
541 return "GESTURE_END";
543 default:
544 // We should explicitly require each event type.
545 NOTREACHED();
546 break;
548 return "";
551 std::string EventTypesToString(const EventFilterRecorder::Events& events) {
552 std::string result;
553 for (size_t i = 0; i < events.size(); ++i) {
554 if (i != 0)
555 result += " ";
556 result += EventTypeToString(events[i]);
558 return result;
561 } // namespace
563 // Verifies a repost mouse event targets the window with capture (if there is
564 // one).
565 TEST_F(RootWindowTest, RepostTargetsCaptureWindow) {
566 // Set capture on |window| generate a mouse event (that is reposted) and not
567 // over |window| and verify |window| gets it (|window| gets it because it has
568 // capture).
569 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
570 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
571 window->SetBounds(gfx::Rect(20, 20, 40, 30));
572 EventFilterRecorder* recorder = new EventFilterRecorder;
573 window->SetEventFilter(recorder); // Takes ownership.
574 window->SetCapture();
575 const ui::MouseEvent press_event(
576 ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
577 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
578 dispatcher()->RepostEvent(press_event);
579 RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
580 // Mouse moves/enters may be generated. We only care about a pressed.
581 EXPECT_TRUE(EventTypesToString(recorder->events()).find("MOUSE_PRESSED") !=
582 std::string::npos) << EventTypesToString(recorder->events());
585 TEST_F(RootWindowTest, MouseMovesHeld) {
586 EventFilterRecorder* filter = new EventFilterRecorder;
587 root_window()->SetEventFilter(filter); // passes ownership
589 test::TestWindowDelegate delegate;
590 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
591 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
593 ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0),
594 gfx::Point(0, 0), 0, 0);
595 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
596 &mouse_move_event);
597 // Discard MOUSE_ENTER.
598 filter->events().clear();
600 dispatcher()->HoldPointerMoves();
602 // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
603 ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
604 gfx::Point(0, 0), 0, 0);
605 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
606 &mouse_dragged_event);
607 EXPECT_TRUE(filter->events().empty());
609 // Check that we do dispatch the held MOUSE_DRAGGED event before another type
610 // of event.
611 ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
612 gfx::Point(0, 0), 0, 0);
613 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
614 &mouse_pressed_event);
615 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
616 EventTypesToString(filter->events()));
617 filter->events().clear();
619 // Check that we coalesce held MOUSE_DRAGGED events.
620 ui::MouseEvent mouse_dragged_event2(ui::ET_MOUSE_DRAGGED, gfx::Point(1, 1),
621 gfx::Point(1, 1), 0, 0);
622 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
623 &mouse_dragged_event);
624 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
625 &mouse_dragged_event2);
626 EXPECT_TRUE(filter->events().empty());
627 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
628 &mouse_pressed_event);
629 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
630 EventTypesToString(filter->events()));
631 filter->events().clear();
633 // Check that on ReleasePointerMoves, held events are not dispatched
634 // immediately, but posted instead.
635 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
636 &mouse_dragged_event);
637 dispatcher()->ReleasePointerMoves();
638 EXPECT_TRUE(filter->events().empty());
639 RunAllPendingInMessageLoop();
640 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(filter->events()));
641 filter->events().clear();
643 // However if another message comes in before the dispatch of the posted
644 // event, check that the posted event is dispatched before this new event.
645 dispatcher()->HoldPointerMoves();
646 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
647 &mouse_dragged_event);
648 dispatcher()->ReleasePointerMoves();
649 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
650 &mouse_pressed_event);
651 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
652 EventTypesToString(filter->events()));
653 filter->events().clear();
654 RunAllPendingInMessageLoop();
655 EXPECT_TRUE(filter->events().empty());
657 // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
658 // them.
659 dispatcher()->HoldPointerMoves();
660 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
661 &mouse_dragged_event);
662 dispatcher()->ReleasePointerMoves();
663 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(
664 &mouse_dragged_event2);
665 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(filter->events()));
666 filter->events().clear();
667 RunAllPendingInMessageLoop();
668 EXPECT_TRUE(filter->events().empty());
671 TEST_F(RootWindowTest, TouchMovesHeld) {
672 EventFilterRecorder* filter = new EventFilterRecorder;
673 root_window()->SetEventFilter(filter); // passes ownership
675 test::TestWindowDelegate delegate;
676 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
677 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
679 // Starting the touch and throwing out the first few events, since the system
680 // is going to generate synthetic mouse events that are not relevant to the
681 // test.
682 ui::TouchEvent touch_pressed_event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
683 0, base::TimeDelta());
684 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(
685 &touch_pressed_event);
686 RunAllPendingInMessageLoop();
687 filter->events().clear();
689 dispatcher()->HoldPointerMoves();
691 // Check that we don't immediately dispatch the TOUCH_MOVED event.
692 ui::TouchEvent touch_moved_event(ui::ET_TOUCH_MOVED, gfx::Point(0, 0),
693 0, base::TimeDelta());
694 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(
695 &touch_moved_event);
696 EXPECT_TRUE(filter->events().empty());
698 // Check that on ReleasePointerMoves, held events are not dispatched
699 // immediately, but posted instead.
700 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(
701 &touch_moved_event);
702 dispatcher()->ReleasePointerMoves();
703 EXPECT_TRUE(filter->events().empty());
704 RunAllPendingInMessageLoop();
705 EXPECT_EQ("TOUCH_MOVED", EventTypesToString(filter->events()));
706 filter->events().clear();
708 // If another touch event occurs then the held touch should be dispatched
709 // immediately before it.
710 ui::TouchEvent touch_released_event(ui::ET_TOUCH_RELEASED, gfx::Point(0, 0),
711 0, base::TimeDelta());
712 filter->events().clear();
713 dispatcher()->HoldPointerMoves();
714 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(
715 &touch_moved_event);
716 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(
717 &touch_released_event);
718 EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP_CANCEL GESTURE_END",
719 EventTypesToString(filter->events()));
720 filter->events().clear();
721 dispatcher()->ReleasePointerMoves();
722 RunAllPendingInMessageLoop();
723 EXPECT_TRUE(filter->events().empty());
726 // Tests that synthetic mouse events are ignored when mouse
727 // events are disabled.
728 TEST_F(RootWindowTest, DispatchSyntheticMouseEvents) {
729 EventFilterRecorder* filter = new EventFilterRecorder;
730 root_window()->SetEventFilter(filter); // passes ownership
732 test::TestWindowDelegate delegate;
733 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
734 &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
735 window->Show();
736 window->SetCapture();
738 test::TestCursorClient cursor_client(root_window());
740 // Dispatch a non-synthetic mouse event when mouse events are enabled.
741 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
742 gfx::Point(10, 10), 0, 0);
743 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(&mouse1);
744 EXPECT_FALSE(filter->events().empty());
745 filter->events().clear();
747 // Dispatch a synthetic mouse event when mouse events are enabled.
748 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
749 gfx::Point(10, 10), ui::EF_IS_SYNTHESIZED, 0);
750 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(&mouse2);
751 EXPECT_FALSE(filter->events().empty());
752 filter->events().clear();
754 // Dispatch a synthetic mouse event when mouse events are disabled.
755 cursor_client.DisableMouseEvents();
756 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(&mouse2);
757 EXPECT_TRUE(filter->events().empty());
760 // Tests that a mouse exit is dispatched to the last known cursor location
761 // when the cursor becomes invisible.
762 TEST_F(RootWindowTest, DispatchMouseExitWhenCursorHidden) {
763 EventFilterRecorder* filter = new EventFilterRecorder;
764 root_window()->SetEventFilter(filter); // passes ownership
766 test::TestWindowDelegate delegate;
767 gfx::Point window_origin(7, 18);
768 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
769 &delegate, 1234, gfx::Rect(window_origin,
770 gfx::Size(100, 100)), root_window()));
771 window->Show();
773 // Dispatch a mouse move event into the window.
774 gfx::Point mouse_location(gfx::Point(15, 25));
775 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location,
776 mouse_location, 0, 0);
777 EXPECT_TRUE(filter->events().empty());
778 dispatcher()->AsWindowTreeHostDelegate()->OnHostMouseEvent(&mouse1);
779 EXPECT_FALSE(filter->events().empty());
780 filter->events().clear();
782 // Hide the cursor and verify a mouse exit was dispatched.
783 dispatcher()->OnCursorVisibilityChanged(false);
784 EXPECT_FALSE(filter->events().empty());
785 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(filter->events()));
787 // Verify the mouse exit was dispatched at the correct location
788 // (in the correct coordinate space).
789 int translated_x = mouse_location.x() - window_origin.x();
790 int translated_y = mouse_location.y() - window_origin.y();
791 gfx::Point translated_point(translated_x, translated_y);
792 EXPECT_EQ(filter->mouse_location(0).ToString(), translated_point.ToString());
795 class DeletingEventFilter : public ui::EventHandler {
796 public:
797 DeletingEventFilter()
798 : delete_during_pre_handle_(false) {}
799 virtual ~DeletingEventFilter() {}
801 void Reset(bool delete_during_pre_handle) {
802 delete_during_pre_handle_ = delete_during_pre_handle;
805 private:
806 // Overridden from ui::EventHandler:
807 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
808 if (delete_during_pre_handle_)
809 delete event->target();
812 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
813 if (delete_during_pre_handle_)
814 delete event->target();
817 bool delete_during_pre_handle_;
819 DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter);
822 class DeletingWindowDelegate : public test::TestWindowDelegate {
823 public:
824 DeletingWindowDelegate()
825 : window_(NULL),
826 delete_during_handle_(false),
827 got_event_(false) {}
828 virtual ~DeletingWindowDelegate() {}
830 void Reset(Window* window, bool delete_during_handle) {
831 window_ = window;
832 delete_during_handle_ = delete_during_handle;
833 got_event_ = false;
835 bool got_event() const { return got_event_; }
837 private:
838 // Overridden from WindowDelegate:
839 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
840 if (delete_during_handle_)
841 delete window_;
842 got_event_ = true;
845 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
846 if (delete_during_handle_)
847 delete window_;
848 got_event_ = true;
851 Window* window_;
852 bool delete_during_handle_;
853 bool got_event_;
855 DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate);
858 TEST_F(RootWindowTest, DeleteWindowDuringDispatch) {
859 // Verifies that we can delete a window during each phase of event handling.
860 // Deleting the window should not cause a crash, only prevent further
861 // processing from occurring.
862 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
863 DeletingWindowDelegate d11;
864 Window* w11 = CreateNormalWindow(11, w1.get(), &d11);
865 WindowTracker tracker;
866 DeletingEventFilter* w1_filter = new DeletingEventFilter;
867 w1->SetEventFilter(w1_filter);
868 client::GetFocusClient(w1.get())->FocusWindow(w11);
870 test::EventGenerator generator(root_window(), w11);
872 // First up, no one deletes anything.
873 tracker.Add(w11);
874 d11.Reset(w11, false);
876 generator.PressLeftButton();
877 EXPECT_TRUE(tracker.Contains(w11));
878 EXPECT_TRUE(d11.got_event());
879 generator.ReleaseLeftButton();
881 // Delegate deletes w11. This will prevent the post-handle step from applying.
882 w1_filter->Reset(false);
883 d11.Reset(w11, true);
884 generator.PressKey(ui::VKEY_A, 0);
885 EXPECT_FALSE(tracker.Contains(w11));
886 EXPECT_TRUE(d11.got_event());
888 // Pre-handle step deletes w11. This will prevent the delegate and the post-
889 // handle steps from applying.
890 w11 = CreateNormalWindow(11, w1.get(), &d11);
891 w1_filter->Reset(true);
892 d11.Reset(w11, false);
893 generator.PressLeftButton();
894 EXPECT_FALSE(tracker.Contains(w11));
895 EXPECT_FALSE(d11.got_event());
898 namespace {
900 // A window delegate that detaches the parent of the target's parent window when
901 // it receives a tap event.
902 class DetachesParentOnTapDelegate : public test::TestWindowDelegate {
903 public:
904 DetachesParentOnTapDelegate() {}
905 virtual ~DetachesParentOnTapDelegate() {}
907 private:
908 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
909 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
910 event->SetHandled();
911 return;
914 if (event->type() == ui::ET_GESTURE_TAP) {
915 Window* parent = static_cast<Window*>(event->target())->parent();
916 parent->parent()->RemoveChild(parent);
917 event->SetHandled();
921 DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate);
924 } // namespace
926 // Tests that the gesture recognizer is reset for all child windows when a
927 // window hides. No expectations, just checks that the test does not crash.
928 TEST_F(RootWindowTest, GestureRecognizerResetsTargetWhenParentHides) {
929 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
930 DetachesParentOnTapDelegate delegate;
931 scoped_ptr<Window> parent(CreateNormalWindow(22, w1.get(), NULL));
932 Window* child = CreateNormalWindow(11, parent.get(), &delegate);
933 test::EventGenerator generator(root_window(), child);
934 generator.GestureTapAt(gfx::Point(40, 40));
937 namespace {
939 // A window delegate that processes nested gestures on tap.
940 class NestedGestureDelegate : public test::TestWindowDelegate {
941 public:
942 NestedGestureDelegate(test::EventGenerator* generator,
943 const gfx::Point tap_location)
944 : generator_(generator),
945 tap_location_(tap_location),
946 gesture_end_count_(0) {}
947 virtual ~NestedGestureDelegate() {}
949 int gesture_end_count() const { return gesture_end_count_; }
951 private:
952 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
953 switch (event->type()) {
954 case ui::ET_GESTURE_TAP_DOWN:
955 event->SetHandled();
956 break;
957 case ui::ET_GESTURE_TAP:
958 if (generator_)
959 generator_->GestureTapAt(tap_location_);
960 event->SetHandled();
961 break;
962 case ui::ET_GESTURE_END:
963 ++gesture_end_count_;
964 break;
965 default:
966 break;
970 test::EventGenerator* generator_;
971 const gfx::Point tap_location_;
972 int gesture_end_count_;
973 DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate);
976 } // namespace
978 // Tests that gesture end is delivered after nested gesture processing.
979 TEST_F(RootWindowTest, GestureEndDeliveredAfterNestedGestures) {
980 NestedGestureDelegate d1(NULL, gfx::Point());
981 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &d1));
982 w1->SetBounds(gfx::Rect(0, 0, 100, 100));
984 test::EventGenerator nested_generator(root_window(), w1.get());
985 NestedGestureDelegate d2(&nested_generator, w1->bounds().CenterPoint());
986 scoped_ptr<Window> w2(CreateNormalWindow(1, root_window(), &d2));
987 w2->SetBounds(gfx::Rect(100, 0, 100, 100));
989 // Tap on w2 which triggers nested gestures for w1.
990 test::EventGenerator generator(root_window(), w2.get());
991 generator.GestureTapAt(w2->bounds().CenterPoint());
993 // Both windows should get their gesture end events.
994 EXPECT_EQ(1, d1.gesture_end_count());
995 EXPECT_EQ(1, d2.gesture_end_count());
998 // Tests whether we can repost the Tap down gesture event.
999 TEST_F(RootWindowTest, RepostTapdownGestureTest) {
1000 EventFilterRecorder* filter = new EventFilterRecorder;
1001 root_window()->SetEventFilter(filter); // passes ownership
1003 test::TestWindowDelegate delegate;
1004 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1005 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1007 ui::GestureEventDetails details(ui::ET_GESTURE_TAP_DOWN, 0.0f, 0.0f);
1008 gfx::Point point(10, 10);
1009 ui::GestureEvent event(
1010 ui::ET_GESTURE_TAP_DOWN,
1011 point.x(),
1012 point.y(),
1014 ui::EventTimeForNow(),
1015 details,
1017 dispatcher()->RepostEvent(event);
1018 RunAllPendingInMessageLoop();
1019 // TODO(rbyers): Currently disabled - crbug.com/170987
1020 EXPECT_FALSE(EventTypesToString(filter->events()).find("GESTURE_TAP_DOWN") !=
1021 std::string::npos);
1022 filter->events().clear();
1025 // This class inherits from the EventFilterRecorder class which provides a
1026 // facility to record events. This class additionally provides a facility to
1027 // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
1028 // events after that.
1029 class RepostGestureEventRecorder : public EventFilterRecorder {
1030 public:
1031 RepostGestureEventRecorder(aura::Window* repost_source,
1032 aura::Window* repost_target)
1033 : repost_source_(repost_source),
1034 repost_target_(repost_target),
1035 reposted_(false),
1036 done_cleanup_(false) {}
1038 virtual ~RepostGestureEventRecorder() {}
1040 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
1041 if (reposted_ && event->type() == ui::ET_TOUCH_PRESSED) {
1042 done_cleanup_ = true;
1043 events().clear();
1045 EventFilterRecorder::OnTouchEvent(event);
1048 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
1049 EXPECT_EQ(done_cleanup_ ? repost_target_ : repost_source_, event->target());
1050 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
1051 if (!reposted_) {
1052 EXPECT_NE(repost_target_, event->target());
1053 reposted_ = true;
1054 repost_target_->GetDispatcher()->RepostEvent(*event);
1055 // Ensure that the reposted gesture event above goes to the
1056 // repost_target_;
1057 repost_source_->GetRootWindow()->RemoveChild(repost_source_);
1058 return;
1061 EventFilterRecorder::OnGestureEvent(event);
1064 // Ignore mouse events as they don't fire at all times. This causes
1065 // the GestureRepostEventOrder test to fail randomly.
1066 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {}
1068 private:
1069 aura::Window* repost_source_;
1070 aura::Window* repost_target_;
1071 // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
1072 bool reposted_;
1073 // set true if we're done cleaning up after hiding repost_source_;
1074 bool done_cleanup_;
1075 DISALLOW_COPY_AND_ASSIGN(RepostGestureEventRecorder);
1078 // Tests whether events which are generated after the reposted gesture event
1079 // are received after that. In this case the scroll sequence events should
1080 // be received after the reposted gesture event.
1081 TEST_F(RootWindowTest, GestureRepostEventOrder) {
1082 // Expected events at the end for the repost_target window defined below.
1083 const char kExpectedTargetEvents[] =
1084 // TODO)(rbyers): Gesture event reposting is disabled - crbug.com/279039.
1085 // "GESTURE_BEGIN GESTURE_TAP_DOWN "
1086 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1087 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE TOUCH_MOVED "
1088 "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
1089 "GESTURE_SCROLL_END GESTURE_END";
1090 // We create two windows.
1091 // The first window (repost_source) is the one to which the initial tap
1092 // gesture is sent. It reposts this event to the second window
1093 // (repost_target).
1094 // We then generate the scroll sequence for repost_target and look for two
1095 // ET_GESTURE_TAP_DOWN events in the event list at the end.
1096 test::TestWindowDelegate delegate;
1097 scoped_ptr<aura::Window> repost_target(CreateTestWindowWithDelegate(
1098 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1100 scoped_ptr<aura::Window> repost_source(CreateTestWindowWithDelegate(
1101 &delegate, 1, gfx::Rect(0, 0, 50, 50), root_window()));
1103 RepostGestureEventRecorder* repost_event_recorder =
1104 new RepostGestureEventRecorder(repost_source.get(), repost_target.get());
1105 root_window()->SetEventFilter(repost_event_recorder); // passes ownership
1107 // Generate a tap down gesture for the repost_source. This will be reposted
1108 // to repost_target.
1109 test::EventGenerator repost_generator(root_window(), repost_source.get());
1110 repost_generator.GestureTapAt(gfx::Point(40, 40));
1111 RunAllPendingInMessageLoop();
1113 test::EventGenerator scroll_generator(root_window(), repost_target.get());
1114 scroll_generator.GestureScrollSequence(
1115 gfx::Point(80, 80),
1116 gfx::Point(100, 100),
1117 base::TimeDelta::FromMilliseconds(100),
1119 RunAllPendingInMessageLoop();
1121 int tap_down_count = 0;
1122 for (size_t i = 0; i < repost_event_recorder->events().size(); ++i) {
1123 if (repost_event_recorder->events()[i] == ui::ET_GESTURE_TAP_DOWN)
1124 ++tap_down_count;
1127 // We expect two tap down events. One from the repost and the other one from
1128 // the scroll sequence posted above.
1129 // TODO(rbyers): Currently disabled - crbug.com/170987
1130 EXPECT_EQ(1, tap_down_count);
1132 EXPECT_EQ(kExpectedTargetEvents,
1133 EventTypesToString(repost_event_recorder->events()));
1136 class OnMouseExitDeletingEventFilter : public EventFilterRecorder {
1137 public:
1138 OnMouseExitDeletingEventFilter() : window_to_delete_(NULL) {}
1139 virtual ~OnMouseExitDeletingEventFilter() {}
1141 void set_window_to_delete(Window* window_to_delete) {
1142 window_to_delete_ = window_to_delete;
1145 private:
1146 // Overridden from ui::EventHandler:
1147 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
1148 EventFilterRecorder::OnMouseEvent(event);
1149 if (window_to_delete_) {
1150 delete window_to_delete_;
1151 window_to_delete_ = NULL;
1155 Window* window_to_delete_;
1157 DISALLOW_COPY_AND_ASSIGN(OnMouseExitDeletingEventFilter);
1160 // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
1161 // a child, but the child is destroyed because of the synthesized mouse-exit
1162 // event generated on the previous mouse_moved_handler_.
1163 TEST_F(RootWindowTest, DeleteWindowDuringMouseMovedDispatch) {
1164 // Create window 1 and set its event filter. Window 1 will take ownership of
1165 // the event filter.
1166 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
1167 OnMouseExitDeletingEventFilter* w1_filter =
1168 new OnMouseExitDeletingEventFilter();
1169 w1->SetEventFilter(w1_filter);
1170 w1->SetBounds(gfx::Rect(20, 20, 60, 60));
1171 EXPECT_EQ(NULL, dispatcher()->mouse_moved_handler());
1173 test::EventGenerator generator(root_window(), w1.get());
1175 // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
1176 // root window.
1177 generator.MoveMouseTo(51, 51);
1178 EXPECT_EQ(w1.get(), dispatcher()->mouse_moved_handler());
1180 // Create window 2 under the mouse cursor and stack it above window 1.
1181 Window* w2 = CreateNormalWindow(2, root_window(), NULL);
1182 w2->SetBounds(gfx::Rect(30, 30, 40, 40));
1183 root_window()->StackChildAbove(w2, w1.get());
1185 // Set window 2 as the window that is to be deleted when a mouse-exited event
1186 // happens on window 1.
1187 w1_filter->set_window_to_delete(w2);
1189 // Move mosue over window 2. This should generate a mouse-exited event for
1190 // window 1 resulting in deletion of window 2. The original mouse-moved event
1191 // that was targeted to window 2 should be dropped since window 2 is
1192 // destroyed. This test passes if no crash happens.
1193 generator.MoveMouseTo(52, 52);
1194 EXPECT_EQ(NULL, dispatcher()->mouse_moved_handler());
1196 // Check events received by window 1.
1197 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
1198 EventTypesToString(w1_filter->events()));
1201 namespace {
1203 // Used to track if OnWindowDestroying() is invoked and if there is a valid
1204 // RootWindow at such time.
1205 class ValidRootDuringDestructionWindowObserver : public aura::WindowObserver {
1206 public:
1207 ValidRootDuringDestructionWindowObserver(bool* got_destroying,
1208 bool* has_valid_root)
1209 : got_destroying_(got_destroying),
1210 has_valid_root_(has_valid_root) {
1213 // WindowObserver:
1214 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
1215 *got_destroying_ = true;
1216 *has_valid_root_ = (window->GetRootWindow() != NULL);
1219 private:
1220 bool* got_destroying_;
1221 bool* has_valid_root_;
1223 DISALLOW_COPY_AND_ASSIGN(ValidRootDuringDestructionWindowObserver);
1226 } // namespace
1228 #if defined(USE_OZONE)
1229 // Creating multiple WindowTreeHostOzone instances is broken.
1230 #define MAYBE_ValidRootDuringDestruction DISABLED_ValidRootDuringDestruction
1231 #else
1232 #define MAYBE_ValidRootDuringDestruction ValidRootDuringDestruction
1233 #endif
1235 // Verifies GetRootWindow() from ~Window returns a valid root.
1236 TEST_F(RootWindowTest, MAYBE_ValidRootDuringDestruction) {
1237 bool got_destroying = false;
1238 bool has_valid_root = false;
1239 ValidRootDuringDestructionWindowObserver observer(&got_destroying,
1240 &has_valid_root);
1242 scoped_ptr<RootWindow> root_window(
1243 new RootWindow(RootWindow::CreateParams(gfx::Rect(0, 0, 100, 100))));
1244 root_window->Init();
1245 // Owned by RootWindow.
1246 Window* w1 = CreateNormalWindow(1, root_window->window(), NULL);
1247 w1->AddObserver(&observer);
1249 EXPECT_TRUE(got_destroying);
1250 EXPECT_TRUE(has_valid_root);
1253 namespace {
1255 // See description above DontResetHeldEvent for details.
1256 class DontResetHeldEventWindowDelegate : public test::TestWindowDelegate {
1257 public:
1258 explicit DontResetHeldEventWindowDelegate(aura::Window* root)
1259 : root_(root),
1260 mouse_event_count_(0) {}
1261 virtual ~DontResetHeldEventWindowDelegate() {}
1263 int mouse_event_count() const { return mouse_event_count_; }
1265 // TestWindowDelegate:
1266 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
1267 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0 &&
1268 mouse_event_count_++ == 0) {
1269 ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED,
1270 gfx::Point(10, 10), gfx::Point(10, 10),
1271 ui::EF_SHIFT_DOWN, 0);
1272 root_->GetDispatcher()->RepostEvent(mouse_event);
1276 private:
1277 Window* root_;
1278 int mouse_event_count_;
1280 DISALLOW_COPY_AND_ASSIGN(DontResetHeldEventWindowDelegate);
1283 } // namespace
1285 // Verifies RootWindow doesn't reset |RootWindow::held_repostable_event_| after
1286 // dispatching. This is done by using DontResetHeldEventWindowDelegate, which
1287 // tracks the number of events with ui::EF_SHIFT_DOWN set (all reposted events
1288 // have EF_SHIFT_DOWN). When the first event is seen RepostEvent() is used to
1289 // schedule another reposted event.
1290 TEST_F(RootWindowTest, DontResetHeldEvent) {
1291 DontResetHeldEventWindowDelegate delegate(root_window());
1292 scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
1293 WindowTreeHostDelegate* root_window_delegate =
1294 static_cast<WindowTreeHostDelegate*>(root_window()->GetDispatcher());
1295 w1->SetBounds(gfx::Rect(0, 0, 40, 40));
1296 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED,
1297 gfx::Point(10, 10), gfx::Point(10, 10),
1298 ui::EF_SHIFT_DOWN, 0);
1299 root_window()->GetDispatcher()->RepostEvent(pressed);
1300 ui::MouseEvent pressed2(ui::ET_MOUSE_PRESSED,
1301 gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
1302 // Invoke OnHostMouseEvent() to flush event scheduled by way of RepostEvent().
1303 root_window_delegate->OnHostMouseEvent(&pressed2);
1304 // Delegate should have seen reposted event (identified by way of
1305 // EF_SHIFT_DOWN). Invoke OnHostMouseEvent() to flush the second
1306 // RepostedEvent().
1307 EXPECT_EQ(1, delegate.mouse_event_count());
1308 root_window_delegate->OnHostMouseEvent(&pressed2);
1309 EXPECT_EQ(2, delegate.mouse_event_count());
1312 namespace {
1314 // See description above DeleteRootFromHeldMouseEvent for details.
1315 class DeleteRootFromHeldMouseEventDelegate : public test::TestWindowDelegate {
1316 public:
1317 explicit DeleteRootFromHeldMouseEventDelegate(aura::RootWindow* root)
1318 : root_(root),
1319 got_mouse_event_(false),
1320 got_destroy_(false) {
1322 virtual ~DeleteRootFromHeldMouseEventDelegate() {}
1324 bool got_mouse_event() const { return got_mouse_event_; }
1325 bool got_destroy() const { return got_destroy_; }
1327 // TestWindowDelegate:
1328 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
1329 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0) {
1330 got_mouse_event_ = true;
1331 delete root_;
1334 virtual void OnWindowDestroyed() OVERRIDE {
1335 got_destroy_ = true;
1338 private:
1339 RootWindow* root_;
1340 bool got_mouse_event_;
1341 bool got_destroy_;
1343 DISALLOW_COPY_AND_ASSIGN(DeleteRootFromHeldMouseEventDelegate);
1346 } // namespace
1348 #if defined(USE_OZONE)
1349 // Creating multiple WindowTreeHostOzone instances is broken.
1350 #define MAYBE_DeleteRootFromHeldMouseEvent DISABLED_DeleteRootFromHeldMouseEvent
1351 #else
1352 #define MAYBE_DeleteRootFromHeldMouseEvent DeleteRootFromHeldMouseEvent
1353 #endif
1355 // Verifies if a RootWindow is deleted from dispatching a held mouse event we
1356 // don't crash.
1357 TEST_F(RootWindowTest, MAYBE_DeleteRootFromHeldMouseEvent) {
1358 // Should be deleted by |delegate|.
1359 RootWindow* r2 =
1360 new RootWindow(RootWindow::CreateParams(gfx::Rect(0, 0, 100, 100)));
1361 r2->Init();
1362 DeleteRootFromHeldMouseEventDelegate delegate(r2);
1363 // Owned by |r2|.
1364 Window* w1 = CreateNormalWindow(1, r2->window(), &delegate);
1365 w1->SetBounds(gfx::Rect(0, 0, 40, 40));
1366 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED,
1367 gfx::Point(10, 10), gfx::Point(10, 10),
1368 ui::EF_SHIFT_DOWN, 0);
1369 r2->RepostEvent(pressed);
1370 // RunAllPendingInMessageLoop() to make sure the |pressed| is run.
1371 RunAllPendingInMessageLoop();
1372 EXPECT_TRUE(delegate.got_mouse_event());
1373 EXPECT_TRUE(delegate.got_destroy());
1376 TEST_F(RootWindowTest, WindowHideCancelsActiveTouches) {
1377 EventFilterRecorder* filter = new EventFilterRecorder;
1378 root_window()->SetEventFilter(filter); // passes ownership
1380 test::TestWindowDelegate delegate;
1381 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1382 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1384 gfx::Point position1 = root_window()->bounds().origin();
1385 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position1, 0, base::TimeDelta());
1386 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&press);
1388 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN",
1389 EventTypesToString(filter->GetAndResetEvents()));
1391 window->Hide();
1393 EXPECT_EQ("TOUCH_CANCELLED GESTURE_TAP_CANCEL GESTURE_END",
1394 EventTypesToString(filter->events()));
1397 TEST_F(RootWindowTest, WindowHideCancelsActiveGestures) {
1398 EventFilterRecorder* filter = new EventFilterRecorder;
1399 root_window()->SetEventFilter(filter); // passes ownership
1401 test::TestWindowDelegate delegate;
1402 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1403 &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1405 gfx::Point position1 = root_window()->bounds().origin();
1406 gfx::Point position2 = root_window()->bounds().CenterPoint();
1407 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position1, 0, base::TimeDelta());
1408 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&press);
1410 ui::TouchEvent move(ui::ET_TOUCH_MOVED, position2, 0, base::TimeDelta());
1411 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&move);
1413 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, position1, 1, base::TimeDelta());
1414 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&press2);
1416 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1417 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1418 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_PINCH_BEGIN",
1419 EventTypesToString(filter->GetAndResetEvents()));
1421 window->Hide();
1423 EXPECT_EQ("TOUCH_CANCELLED GESTURE_PINCH_END GESTURE_END TOUCH_CANCELLED "
1424 "GESTURE_SCROLL_END GESTURE_END",
1425 EventTypesToString(filter->events()));
1428 // Places two windows side by side. Presses down on one window, and starts a
1429 // scroll. Sets capture on the other window and ensures that the "ending" events
1430 // aren't sent to the window which gained capture.
1431 TEST_F(RootWindowTest, EndingEventDoesntRetarget) {
1432 scoped_ptr<Window> window1(CreateNormalWindow(1, root_window(), NULL));
1433 window1->SetBounds(gfx::Rect(0, 0, 40, 40));
1435 scoped_ptr<Window> window2(CreateNormalWindow(2, root_window(), NULL));
1436 window2->SetBounds(gfx::Rect(40, 0, 40, 40));
1438 EventFilterRecorder* filter1 = new EventFilterRecorder();
1439 window1->SetEventFilter(filter1); // passes ownership
1440 EventFilterRecorder* filter2 = new EventFilterRecorder();
1441 window2->SetEventFilter(filter2); // passes ownership
1443 gfx::Point position = window1->bounds().origin();
1444 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position, 0, base::TimeDelta());
1445 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&press);
1447 gfx::Point position2 = window1->bounds().CenterPoint();
1448 ui::TouchEvent move(ui::ET_TOUCH_MOVED, position2, 0, base::TimeDelta());
1449 dispatcher()->AsWindowTreeHostDelegate()->OnHostTouchEvent(&move);
1451 window2->SetCapture();
1453 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1454 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1455 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END",
1456 EventTypesToString(filter1->events()));
1458 EXPECT_TRUE(filter2->events().empty());
1461 class ExitMessageLoopOnMousePress : public test::TestEventHandler {
1462 public:
1463 ExitMessageLoopOnMousePress() {}
1464 virtual ~ExitMessageLoopOnMousePress() {}
1466 protected:
1467 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
1468 test::TestEventHandler::OnMouseEvent(event);
1469 if (event->type() == ui::ET_MOUSE_PRESSED)
1470 base::MessageLoopForUI::current()->Quit();
1473 private:
1474 DISALLOW_COPY_AND_ASSIGN(ExitMessageLoopOnMousePress);
1477 class RootWindowTestWithMessageLoop : public RootWindowTest {
1478 public:
1479 RootWindowTestWithMessageLoop() {}
1480 virtual ~RootWindowTestWithMessageLoop() {}
1482 void RunTest() {
1483 // Reset any event the window may have received when bringing up the window
1484 // (e.g. mouse-move events if the mouse cursor is over the window).
1485 handler_.Reset();
1487 // Start a nested message-loop, post an event to be dispatched, and then
1488 // terminate the message-loop. When the message-loop unwinds and gets back,
1489 // the reposted event should not have fired.
1490 ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
1491 gfx::Point(10, 10), ui::EF_NONE, ui::EF_NONE);
1492 message_loop()->PostTask(FROM_HERE,
1493 base::Bind(&RootWindow::RepostEvent,
1494 base::Unretained(dispatcher()),
1495 mouse));
1496 message_loop()->PostTask(FROM_HERE,
1497 message_loop()->QuitClosure());
1499 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop());
1500 base::RunLoop loop;
1501 loop.Run();
1502 EXPECT_EQ(0, handler_.num_mouse_events());
1504 // Let the current message-loop run. The event-handler will terminate the
1505 // message-loop when it receives the reposted event.
1508 base::MessageLoop* message_loop() {
1509 return base::MessageLoopForUI::current();
1512 protected:
1513 virtual void SetUp() OVERRIDE {
1514 RootWindowTest::SetUp();
1515 window_.reset(CreateNormalWindow(1, root_window(), NULL));
1516 window_->AddPreTargetHandler(&handler_);
1519 virtual void TearDown() OVERRIDE {
1520 window_.reset();
1521 RootWindowTest::TearDown();
1524 private:
1525 scoped_ptr<Window> window_;
1526 ExitMessageLoopOnMousePress handler_;
1528 DISALLOW_COPY_AND_ASSIGN(RootWindowTestWithMessageLoop);
1531 TEST_F(RootWindowTestWithMessageLoop, EventRepostedInNonNestedLoop) {
1532 CHECK(!message_loop()->is_running());
1533 // Perform the test in a callback, so that it runs after the message-loop
1534 // starts.
1535 message_loop()->PostTask(FROM_HERE,
1536 base::Bind(&RootWindowTestWithMessageLoop::RunTest,
1537 base::Unretained(this)));
1538 message_loop()->Run();
1541 } // namespace aura