1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/aura/window_event_dispatcher.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/aura/client/capture_client.h"
14 #include "ui/aura/client/event_client.h"
15 #include "ui/aura/client/focus_client.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/test/aura_test_base.h"
18 #include "ui/aura/test/env_test_helper.h"
19 #include "ui/aura/test/test_cursor_client.h"
20 #include "ui/aura/test/test_screen.h"
21 #include "ui/aura/test/test_window_delegate.h"
22 #include "ui/aura/test/test_windows.h"
23 #include "ui/aura/window.h"
24 #include "ui/aura/window_tracker.h"
25 #include "ui/base/hit_test.h"
26 #include "ui/events/event.h"
27 #include "ui/events/event_handler.h"
28 #include "ui/events/event_utils.h"
29 #include "ui/events/gesture_detection/gesture_configuration.h"
30 #include "ui/events/keycodes/keyboard_codes.h"
31 #include "ui/events/test/event_generator.h"
32 #include "ui/events/test/test_event_handler.h"
33 #include "ui/gfx/geometry/point.h"
34 #include "ui/gfx/geometry/rect.h"
35 #include "ui/gfx/screen.h"
36 #include "ui/gfx/transform.h"
41 // A delegate that always returns a non-client component for hit tests.
42 class NonClientDelegate
: public test::TestWindowDelegate
{
45 : non_client_count_(0),
46 mouse_event_count_(0),
47 mouse_event_flags_(0x0) {
49 ~NonClientDelegate() override
{}
51 int non_client_count() const { return non_client_count_
; }
52 gfx::Point
non_client_location() const { return non_client_location_
; }
53 int mouse_event_count() const { return mouse_event_count_
; }
54 gfx::Point
mouse_event_location() const { return mouse_event_location_
; }
55 int mouse_event_flags() const { return mouse_event_flags_
; }
57 int GetNonClientComponent(const gfx::Point
& location
) const override
{
58 NonClientDelegate
* self
= const_cast<NonClientDelegate
*>(this);
59 self
->non_client_count_
++;
60 self
->non_client_location_
= location
;
63 void OnMouseEvent(ui::MouseEvent
* event
) override
{
65 mouse_event_location_
= event
->location();
66 mouse_event_flags_
= event
->flags();
71 int non_client_count_
;
72 gfx::Point non_client_location_
;
73 int mouse_event_count_
;
74 gfx::Point mouse_event_location_
;
75 int mouse_event_flags_
;
77 DISALLOW_COPY_AND_ASSIGN(NonClientDelegate
);
80 // A simple event handler that consumes key events.
81 class ConsumeKeyHandler
: public ui::test::TestEventHandler
{
83 ConsumeKeyHandler() {}
84 ~ConsumeKeyHandler() override
{}
86 // Overridden from ui::EventHandler:
87 void OnKeyEvent(ui::KeyEvent
* event
) override
{
88 ui::test::TestEventHandler::OnKeyEvent(event
);
89 event
->StopPropagation();
93 DISALLOW_COPY_AND_ASSIGN(ConsumeKeyHandler
);
96 bool IsFocusedWindow(aura::Window
* window
) {
97 return client::GetFocusClient(window
)->GetFocusedWindow() == window
;
102 typedef test::AuraTestBase WindowEventDispatcherTest
;
104 TEST_F(WindowEventDispatcherTest
, OnHostMouseEvent
) {
105 // Create two non-overlapping windows so we don't have to worry about which
107 scoped_ptr
<NonClientDelegate
> delegate1(new NonClientDelegate());
108 scoped_ptr
<NonClientDelegate
> delegate2(new NonClientDelegate());
109 const int kWindowWidth
= 123;
110 const int kWindowHeight
= 45;
111 gfx::Rect
bounds1(100, 200, kWindowWidth
, kWindowHeight
);
112 gfx::Rect
bounds2(300, 400, kWindowWidth
, kWindowHeight
);
113 scoped_ptr
<aura::Window
> window1(CreateTestWindowWithDelegate(
114 delegate1
.get(), -1234, bounds1
, root_window()));
115 scoped_ptr
<aura::Window
> window2(CreateTestWindowWithDelegate(
116 delegate2
.get(), -5678, bounds2
, root_window()));
118 // Send a mouse event to window1.
119 gfx::Point
point(101, 201);
120 ui::MouseEvent
event1(
121 ui::ET_MOUSE_PRESSED
, point
, point
, ui::EF_LEFT_MOUSE_BUTTON
,
122 ui::EF_LEFT_MOUSE_BUTTON
);
123 DispatchEventUsingWindowDispatcher(&event1
);
125 // Event was tested for non-client area for the target window.
126 EXPECT_EQ(1, delegate1
->non_client_count());
127 EXPECT_EQ(0, delegate2
->non_client_count());
128 // The non-client component test was in local coordinates.
129 EXPECT_EQ(gfx::Point(1, 1), delegate1
->non_client_location());
130 // Mouse event was received by target window.
131 EXPECT_EQ(1, delegate1
->mouse_event_count());
132 EXPECT_EQ(0, delegate2
->mouse_event_count());
133 // Event was in local coordinates.
134 EXPECT_EQ(gfx::Point(1, 1), delegate1
->mouse_event_location());
135 // Non-client flag was set.
136 EXPECT_TRUE(delegate1
->mouse_event_flags() & ui::EF_IS_NON_CLIENT
);
139 TEST_F(WindowEventDispatcherTest
, RepostEvent
) {
140 // Test RepostEvent in RootWindow. It only works for Mouse Press.
141 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
142 gfx::Point
point(10, 10);
143 ui::MouseEvent
event(
144 ui::ET_MOUSE_PRESSED
, point
, point
, ui::EF_LEFT_MOUSE_BUTTON
,
145 ui::EF_LEFT_MOUSE_BUTTON
);
146 host()->dispatcher()->RepostEvent(event
);
147 RunAllPendingInMessageLoop();
148 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
151 // Check that we correctly track the state of the mouse buttons in response to
152 // button press and release events.
153 TEST_F(WindowEventDispatcherTest
, MouseButtonState
) {
154 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
157 scoped_ptr
<ui::MouseEvent
> event
;
159 // Press the left button.
160 event
.reset(new ui::MouseEvent(
161 ui::ET_MOUSE_PRESSED
,
164 ui::EF_LEFT_MOUSE_BUTTON
,
165 ui::EF_LEFT_MOUSE_BUTTON
));
166 DispatchEventUsingWindowDispatcher(event
.get());
167 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
169 // Additionally press the right.
170 event
.reset(new ui::MouseEvent(
171 ui::ET_MOUSE_PRESSED
,
174 ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_RIGHT_MOUSE_BUTTON
,
175 ui::EF_RIGHT_MOUSE_BUTTON
));
176 DispatchEventUsingWindowDispatcher(event
.get());
177 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
179 // Release the left button.
180 event
.reset(new ui::MouseEvent(
181 ui::ET_MOUSE_RELEASED
,
184 ui::EF_RIGHT_MOUSE_BUTTON
,
185 ui::EF_LEFT_MOUSE_BUTTON
));
186 DispatchEventUsingWindowDispatcher(event
.get());
187 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
189 // Release the right button. We should ignore the Shift-is-down flag.
190 event
.reset(new ui::MouseEvent(
191 ui::ET_MOUSE_RELEASED
,
195 ui::EF_RIGHT_MOUSE_BUTTON
));
196 DispatchEventUsingWindowDispatcher(event
.get());
197 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
199 // Press the middle button.
200 event
.reset(new ui::MouseEvent(
201 ui::ET_MOUSE_PRESSED
,
204 ui::EF_MIDDLE_MOUSE_BUTTON
,
205 ui::EF_MIDDLE_MOUSE_BUTTON
));
206 DispatchEventUsingWindowDispatcher(event
.get());
207 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
210 TEST_F(WindowEventDispatcherTest
, TranslatedEvent
) {
211 scoped_ptr
<Window
> w1(test::CreateTestWindowWithDelegate(NULL
, 1,
212 gfx::Rect(50, 50, 100, 100), root_window()));
214 gfx::Point
origin(100, 100);
215 ui::MouseEvent
root(ui::ET_MOUSE_PRESSED
, origin
, origin
, 0, 0);
217 EXPECT_EQ("100,100", root
.location().ToString());
218 EXPECT_EQ("100,100", root
.root_location().ToString());
220 ui::MouseEvent
translated_event(
221 root
, static_cast<Window
*>(root_window()), w1
.get(),
222 ui::ET_MOUSE_ENTERED
, root
.flags());
223 EXPECT_EQ("50,50", translated_event
.location().ToString());
224 EXPECT_EQ("100,100", translated_event
.root_location().ToString());
229 class TestEventClient
: public client::EventClient
{
231 static const int kNonLockWindowId
= 100;
232 static const int kLockWindowId
= 200;
234 explicit TestEventClient(Window
* root_window
)
235 : root_window_(root_window
),
237 client::SetEventClient(root_window_
, this);
238 Window
* lock_window
=
239 test::CreateTestWindowWithBounds(root_window_
->bounds(), root_window_
);
240 lock_window
->set_id(kLockWindowId
);
241 Window
* non_lock_window
=
242 test::CreateTestWindowWithBounds(root_window_
->bounds(), root_window_
);
243 non_lock_window
->set_id(kNonLockWindowId
);
245 ~TestEventClient() override
{ client::SetEventClient(root_window_
, NULL
); }
247 // Starts/stops locking. Locking prevents windows other than those inside
248 // the lock container from receiving events, getting focus etc.
256 Window
* GetLockWindow() {
257 return const_cast<Window
*>(
258 static_cast<const TestEventClient
*>(this)->GetLockWindow());
260 const Window
* GetLockWindow() const {
261 return root_window_
->GetChildById(kLockWindowId
);
263 Window
* GetNonLockWindow() {
264 return root_window_
->GetChildById(kNonLockWindowId
);
268 // Overridden from client::EventClient:
269 bool CanProcessEventsWithinSubtree(const Window
* window
) const override
{
271 window
->Contains(GetLockWindow()) || GetLockWindow()->Contains(window
) :
275 ui::EventTarget
* GetToplevelEventTarget() override
{ return NULL
; }
277 Window
* root_window_
;
280 DISALLOW_COPY_AND_ASSIGN(TestEventClient
);
285 TEST_F(WindowEventDispatcherTest
, CanProcessEventsWithinSubtree
) {
286 TestEventClient
client(root_window());
287 test::TestWindowDelegate d
;
289 ui::test::TestEventHandler nonlock_ef
;
290 ui::test::TestEventHandler lock_ef
;
291 client
.GetNonLockWindow()->AddPreTargetHandler(&nonlock_ef
);
292 client
.GetLockWindow()->AddPreTargetHandler(&lock_ef
);
294 Window
* w1
= test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
295 client
.GetNonLockWindow());
297 Window
* w2
= test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
298 client
.GetNonLockWindow());
300 scoped_ptr
<Window
> w3(
301 test::CreateTestWindowWithDelegate(&d
, 3, gfx::Rect(30, 30, 20, 20),
302 client
.GetLockWindow()));
305 EXPECT_TRUE(IsFocusedWindow(w1
));
309 // Since we're locked, the attempt to focus w2 will be ignored.
311 EXPECT_TRUE(IsFocusedWindow(w1
));
312 EXPECT_FALSE(IsFocusedWindow(w2
));
315 // Attempting to send a key event to w1 (not in the lock container) should
316 // cause focus to be reset.
317 ui::test::EventGenerator
generator(root_window());
318 generator
.PressKey(ui::VKEY_SPACE
, 0);
319 EXPECT_EQ(NULL
, client::GetFocusClient(w1
)->GetFocusedWindow());
320 EXPECT_FALSE(IsFocusedWindow(w1
));
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 ui::test::EventGenerator
generator(root_window(), w1
);
327 generator
.ClickLeftButton();
328 EXPECT_EQ(0, nonlock_ef
.num_mouse_events());
330 // Events sent to a window in the lock container will be processed.
331 ui::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
338 w3
->parent()->RemoveChild(w3
.get());
341 TEST_F(WindowEventDispatcherTest
, DontIgnoreUnknownKeys
) {
342 ConsumeKeyHandler handler
;
343 root_window()->AddPreTargetHandler(&handler
);
345 ui::KeyEvent
unknown_event(ui::ET_KEY_PRESSED
, ui::VKEY_UNKNOWN
, ui::EF_NONE
);
346 DispatchEventUsingWindowDispatcher(&unknown_event
);
347 EXPECT_TRUE(unknown_event
.handled());
348 EXPECT_EQ(1, handler
.num_key_events());
351 ui::KeyEvent
known_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
352 DispatchEventUsingWindowDispatcher(&known_event
);
353 EXPECT_TRUE(known_event
.handled());
354 EXPECT_EQ(1, handler
.num_key_events());
357 ui::KeyEvent
ime_event(ui::ET_KEY_PRESSED
, ui::VKEY_UNKNOWN
,
358 ui::EF_IME_FABRICATED_KEY
);
359 DispatchEventUsingWindowDispatcher(&ime_event
);
360 EXPECT_TRUE(ime_event
.handled());
361 EXPECT_EQ(1, handler
.num_key_events());
364 ui::KeyEvent
unknown_key_with_char_event(ui::ET_KEY_PRESSED
, ui::VKEY_UNKNOWN
,
366 unknown_key_with_char_event
.set_character(0x00e4 /* "ä" */);
367 DispatchEventUsingWindowDispatcher(&unknown_key_with_char_event
);
368 EXPECT_TRUE(unknown_key_with_char_event
.handled());
369 EXPECT_EQ(1, handler
.num_key_events());
372 TEST_F(WindowEventDispatcherTest
, NoDelegateWindowReceivesKeyEvents
) {
373 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
377 ui::test::TestEventHandler handler
;
378 w1
->AddPreTargetHandler(&handler
);
379 ui::KeyEvent
key_press(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
380 DispatchEventUsingWindowDispatcher(&key_press
);
381 EXPECT_TRUE(key_press
.handled());
382 EXPECT_EQ(1, handler
.num_key_events());
384 w1
->RemovePreTargetHandler(&handler
);
387 // Tests that touch-events that are beyond the bounds of the root-window do get
388 // propagated to the event filters correctly with the root as the target.
389 TEST_F(WindowEventDispatcherTest
, TouchEventsOutsideBounds
) {
390 ui::test::TestEventHandler handler
;
391 root_window()->AddPreTargetHandler(&handler
);
393 gfx::Point position
= root_window()->bounds().origin();
394 position
.Offset(-10, -10);
395 ui::TouchEvent
press(
396 ui::ET_TOUCH_PRESSED
, position
, 0, ui::EventTimeForNow());
397 DispatchEventUsingWindowDispatcher(&press
);
398 EXPECT_EQ(1, handler
.num_touch_events());
400 position
= root_window()->bounds().origin();
401 position
.Offset(root_window()->bounds().width() + 10,
402 root_window()->bounds().height() + 10);
403 ui::TouchEvent
release(
404 ui::ET_TOUCH_RELEASED
, position
, 0, ui::EventTimeForNow());
405 DispatchEventUsingWindowDispatcher(&release
);
406 EXPECT_EQ(2, handler
.num_touch_events());
409 // Tests that scroll events are dispatched correctly.
410 TEST_F(WindowEventDispatcherTest
, ScrollEventDispatch
) {
411 base::TimeDelta now
= ui::EventTimeForNow();
412 ui::test::TestEventHandler handler
;
413 root_window()->AddPreTargetHandler(&handler
);
415 test::TestWindowDelegate delegate
;
416 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &delegate
));
417 w1
->SetBounds(gfx::Rect(20, 20, 40, 40));
419 // A scroll event on the root-window itself is dispatched.
420 ui::ScrollEvent
scroll1(ui::ET_SCROLL
,
427 DispatchEventUsingWindowDispatcher(&scroll1
);
428 EXPECT_EQ(1, handler
.num_scroll_events());
430 // Scroll event on a window should be dispatched properly.
431 ui::ScrollEvent
scroll2(ui::ET_SCROLL
,
438 DispatchEventUsingWindowDispatcher(&scroll2
);
439 EXPECT_EQ(2, handler
.num_scroll_events());
440 root_window()->RemovePreTargetHandler(&handler
);
445 // FilterFilter that tracks the types of events it's seen.
446 class EventFilterRecorder
: public ui::EventHandler
{
448 typedef std::vector
<ui::EventType
> Events
;
449 typedef std::vector
<gfx::Point
> EventLocations
;
450 typedef std::vector
<int> EventFlags
;
452 EventFilterRecorder()
453 : wait_until_event_(ui::ET_UNKNOWN
),
454 last_touch_may_cause_scrolling_(false) {
457 const Events
& events() const { return events_
; }
459 const EventLocations
& mouse_locations() const { return mouse_locations_
; }
460 gfx::Point
mouse_location(int i
) const { return mouse_locations_
[i
]; }
461 const EventLocations
& touch_locations() const { return touch_locations_
; }
462 const EventLocations
& gesture_locations() const { return gesture_locations_
; }
463 const EventFlags
& mouse_event_flags() const { return mouse_event_flags_
; }
465 void WaitUntilReceivedEvent(ui::EventType type
) {
466 wait_until_event_
= type
;
467 run_loop_
.reset(new base::RunLoop());
471 Events
GetAndResetEvents() {
472 Events events
= events_
;
479 mouse_locations_
.clear();
480 touch_locations_
.clear();
481 gesture_locations_
.clear();
482 mouse_event_flags_
.clear();
483 last_touch_may_cause_scrolling_
= false;
486 // ui::EventHandler overrides:
487 void OnEvent(ui::Event
* event
) override
{
488 ui::EventHandler::OnEvent(event
);
489 events_
.push_back(event
->type());
490 if (wait_until_event_
== event
->type() && run_loop_
) {
492 wait_until_event_
= ui::ET_UNKNOWN
;
496 void OnMouseEvent(ui::MouseEvent
* event
) override
{
497 mouse_locations_
.push_back(event
->location());
498 mouse_event_flags_
.push_back(event
->flags());
501 void OnTouchEvent(ui::TouchEvent
* event
) override
{
502 touch_locations_
.push_back(event
->location());
503 last_touch_may_cause_scrolling_
= event
->may_cause_scrolling();
506 void OnGestureEvent(ui::GestureEvent
* event
) override
{
507 gesture_locations_
.push_back(event
->location());
510 bool HasReceivedEvent(ui::EventType type
) {
511 return std::find(events_
.begin(), events_
.end(), type
) != events_
.end();
514 bool LastTouchMayCauseScrolling() const {
515 return last_touch_may_cause_scrolling_
;
519 scoped_ptr
<base::RunLoop
> run_loop_
;
520 ui::EventType wait_until_event_
;
523 EventLocations mouse_locations_
;
524 EventLocations touch_locations_
;
525 EventLocations gesture_locations_
;
526 EventFlags mouse_event_flags_
;
527 bool last_touch_may_cause_scrolling_
;
529 DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder
);
532 // Converts an EventType to a string.
533 std::string
EventTypeToString(ui::EventType type
) {
535 case ui::ET_TOUCH_RELEASED
:
536 return "TOUCH_RELEASED";
538 case ui::ET_TOUCH_CANCELLED
:
539 return "TOUCH_CANCELLED";
541 case ui::ET_TOUCH_PRESSED
:
542 return "TOUCH_PRESSED";
544 case ui::ET_TOUCH_MOVED
:
545 return "TOUCH_MOVED";
547 case ui::ET_MOUSE_PRESSED
:
548 return "MOUSE_PRESSED";
550 case ui::ET_MOUSE_DRAGGED
:
551 return "MOUSE_DRAGGED";
553 case ui::ET_MOUSE_RELEASED
:
554 return "MOUSE_RELEASED";
556 case ui::ET_MOUSE_MOVED
:
557 return "MOUSE_MOVED";
559 case ui::ET_MOUSE_ENTERED
:
560 return "MOUSE_ENTERED";
562 case ui::ET_MOUSE_EXITED
:
563 return "MOUSE_EXITED";
565 case ui::ET_GESTURE_SCROLL_BEGIN
:
566 return "GESTURE_SCROLL_BEGIN";
568 case ui::ET_GESTURE_SCROLL_END
:
569 return "GESTURE_SCROLL_END";
571 case ui::ET_GESTURE_SCROLL_UPDATE
:
572 return "GESTURE_SCROLL_UPDATE";
574 case ui::ET_GESTURE_PINCH_BEGIN
:
575 return "GESTURE_PINCH_BEGIN";
577 case ui::ET_GESTURE_PINCH_END
:
578 return "GESTURE_PINCH_END";
580 case ui::ET_GESTURE_PINCH_UPDATE
:
581 return "GESTURE_PINCH_UPDATE";
583 case ui::ET_GESTURE_TAP
:
584 return "GESTURE_TAP";
586 case ui::ET_GESTURE_TAP_DOWN
:
587 return "GESTURE_TAP_DOWN";
589 case ui::ET_GESTURE_TAP_CANCEL
:
590 return "GESTURE_TAP_CANCEL";
592 case ui::ET_GESTURE_SHOW_PRESS
:
593 return "GESTURE_SHOW_PRESS";
595 case ui::ET_GESTURE_BEGIN
:
596 return "GESTURE_BEGIN";
598 case ui::ET_GESTURE_END
:
599 return "GESTURE_END";
602 // We should explicitly require each event type.
603 NOTREACHED() << "Received unexpected event: " << type
;
609 std::string
EventTypesToString(const EventFilterRecorder::Events
& events
) {
611 for (size_t i
= 0; i
< events
.size(); ++i
) {
614 result
+= EventTypeToString(events
[i
]);
621 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
622 #define MAYBE(x) DISABLED_##x
627 // Verifies a repost mouse event targets the window with capture (if there is
629 // Flaky on 32-bit Windows bots. http://crbug.com/388290
630 TEST_F(WindowEventDispatcherTest
, MAYBE(RepostTargetsCaptureWindow
)) {
631 // Set capture on |window| generate a mouse event (that is reposted) and not
632 // over |window| and verify |window| gets it (|window| gets it because it has
634 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
635 EventFilterRecorder recorder
;
636 scoped_ptr
<Window
> window(CreateNormalWindow(1, root_window(), NULL
));
637 window
->SetBounds(gfx::Rect(20, 20, 40, 30));
638 window
->AddPreTargetHandler(&recorder
);
639 window
->SetCapture();
640 const ui::MouseEvent
press_event(
641 ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
642 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
643 host()->dispatcher()->RepostEvent(press_event
);
644 RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
645 // Mouse moves/enters may be generated. We only care about a pressed.
646 EXPECT_TRUE(EventTypesToString(recorder
.events()).find("MOUSE_PRESSED") !=
647 std::string::npos
) << EventTypesToString(recorder
.events());
650 TEST_F(WindowEventDispatcherTest
, MouseMovesHeld
) {
651 EventFilterRecorder recorder
;
652 root_window()->AddPreTargetHandler(&recorder
);
654 test::TestWindowDelegate delegate
;
655 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
656 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
658 ui::MouseEvent
mouse_move_event(ui::ET_MOUSE_MOVED
, gfx::Point(0, 0),
659 gfx::Point(0, 0), 0, 0);
660 DispatchEventUsingWindowDispatcher(&mouse_move_event
);
661 // Discard MOUSE_ENTER.
664 host()->dispatcher()->HoldPointerMoves();
666 // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
667 ui::MouseEvent
mouse_dragged_event(ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0),
668 gfx::Point(0, 0), 0, 0);
669 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
670 EXPECT_TRUE(recorder
.events().empty());
672 // Check that we do dispatch the held MOUSE_DRAGGED event before another type
674 ui::MouseEvent
mouse_pressed_event(ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0),
675 gfx::Point(0, 0), 0, 0);
676 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
677 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
678 EventTypesToString(recorder
.events()));
681 // Check that we coalesce held MOUSE_DRAGGED events. Note that here (and
682 // elsewhere in this test) we re-define each event prior to dispatch so that
683 // it has the correct state (phase, handled, target, etc.).
684 mouse_dragged_event
= ui::MouseEvent(
685 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
686 ui::MouseEvent
mouse_dragged_event2(ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10),
687 gfx::Point(10, 10), 0, 0);
688 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
689 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
690 EXPECT_TRUE(recorder
.events().empty());
691 mouse_pressed_event
= ui::MouseEvent(
692 ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
693 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
694 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
695 EventTypesToString(recorder
.events()));
698 // Check that on ReleasePointerMoves, held events are not dispatched
699 // immediately, but posted instead.
700 mouse_dragged_event
= ui::MouseEvent(
701 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
702 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
703 host()->dispatcher()->ReleasePointerMoves();
704 EXPECT_TRUE(recorder
.events().empty());
705 RunAllPendingInMessageLoop();
706 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder
.events()));
709 // However if another message comes in before the dispatch of the posted
710 // event, check that the posted event is dispatched before this new event.
711 host()->dispatcher()->HoldPointerMoves();
712 mouse_dragged_event
= ui::MouseEvent(
713 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
714 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
715 host()->dispatcher()->ReleasePointerMoves();
716 mouse_pressed_event
= ui::MouseEvent(
717 ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
718 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
719 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
720 EventTypesToString(recorder
.events()));
722 RunAllPendingInMessageLoop();
723 EXPECT_TRUE(recorder
.events().empty());
725 // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
727 host()->dispatcher()->HoldPointerMoves();
728 mouse_dragged_event
= ui::MouseEvent(
729 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
730 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
731 host()->dispatcher()->ReleasePointerMoves();
732 mouse_dragged_event2
= ui::MouseEvent(
733 ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
734 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
735 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder
.events()));
737 RunAllPendingInMessageLoop();
738 EXPECT_TRUE(recorder
.events().empty());
740 // Check that synthetic mouse move event has a right location when issued
741 // while holding pointer moves.
742 mouse_dragged_event
= ui::MouseEvent(
743 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
744 mouse_dragged_event2
= ui::MouseEvent(
745 ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
746 ui::MouseEvent
mouse_dragged_event3(ui::ET_MOUSE_DRAGGED
, gfx::Point(28, 28),
747 gfx::Point(28, 28), 0, 0);
748 host()->dispatcher()->HoldPointerMoves();
749 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
750 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
751 window
->SetBounds(gfx::Rect(15, 15, 80, 80));
752 DispatchEventUsingWindowDispatcher(&mouse_dragged_event3
);
753 RunAllPendingInMessageLoop();
754 EXPECT_TRUE(recorder
.events().empty());
755 host()->dispatcher()->ReleasePointerMoves();
756 RunAllPendingInMessageLoop();
757 EXPECT_EQ("MOUSE_MOVED", EventTypesToString(recorder
.events()));
758 EXPECT_EQ(gfx::Point(13, 13), recorder
.mouse_location(0));
760 root_window()->RemovePreTargetHandler(&recorder
);
763 TEST_F(WindowEventDispatcherTest
, TouchMovesHeld
) {
764 EventFilterRecorder recorder
;
765 root_window()->AddPreTargetHandler(&recorder
);
767 test::TestWindowDelegate delegate
;
768 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
769 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
771 // Starting the touch and throwing out the first few events, since the system
772 // is going to generate synthetic mouse events that are not relevant to the
774 ui::TouchEvent
touch_pressed_event(
775 ui::ET_TOUCH_PRESSED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
776 DispatchEventUsingWindowDispatcher(&touch_pressed_event
);
777 recorder
.WaitUntilReceivedEvent(ui::ET_GESTURE_SHOW_PRESS
);
780 host()->dispatcher()->HoldPointerMoves();
782 // Check that we don't immediately dispatch the TOUCH_MOVED event.
783 ui::TouchEvent
touch_moved_event(
784 ui::ET_TOUCH_MOVED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
785 ui::TouchEvent
touch_moved_event2(
786 ui::ET_TOUCH_MOVED
, gfx::Point(11, 10), 0, ui::EventTimeForNow());
787 ui::TouchEvent
touch_moved_event3(
788 ui::ET_TOUCH_MOVED
, gfx::Point(12, 10), 0, ui::EventTimeForNow());
790 DispatchEventUsingWindowDispatcher(&touch_moved_event
);
791 EXPECT_TRUE(recorder
.events().empty());
793 // Check that on ReleasePointerMoves, held events are not dispatched
794 // immediately, but posted instead.
795 DispatchEventUsingWindowDispatcher(&touch_moved_event2
);
796 host()->dispatcher()->ReleasePointerMoves();
797 EXPECT_TRUE(recorder
.events().empty());
799 RunAllPendingInMessageLoop();
800 EXPECT_EQ("TOUCH_MOVED", EventTypesToString(recorder
.events()));
803 // If another touch event occurs then the held touch should be dispatched
804 // immediately before it.
805 ui::TouchEvent
touch_released_event(
806 ui::ET_TOUCH_RELEASED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
808 host()->dispatcher()->HoldPointerMoves();
809 DispatchEventUsingWindowDispatcher(&touch_moved_event3
);
810 DispatchEventUsingWindowDispatcher(&touch_released_event
);
811 EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP GESTURE_END",
812 EventTypesToString(recorder
.events()));
814 host()->dispatcher()->ReleasePointerMoves();
815 RunAllPendingInMessageLoop();
816 EXPECT_TRUE(recorder
.events().empty());
819 // Tests that mouse move event has a right location
820 // when there isn't the target window
821 TEST_F(WindowEventDispatcherTest
, MouseEventWithoutTargetWindow
) {
822 EventFilterRecorder recorder_first
;
823 EventFilterRecorder recorder_second
;
825 test::TestWindowDelegate delegate
;
826 scoped_ptr
<aura::Window
> window_first(CreateTestWindowWithDelegate(
827 &delegate
, 1, gfx::Rect(20, 10, 10, 20), root_window()));
828 window_first
->Show();
829 window_first
->AddPreTargetHandler(&recorder_first
);
831 scoped_ptr
<aura::Window
> window_second(CreateTestWindowWithDelegate(
832 &delegate
, 2, gfx::Rect(20, 30, 10, 20), root_window()));
833 window_second
->Show();
834 window_second
->AddPreTargetHandler(&recorder_second
);
836 const gfx::Point
event_location(22, 33);
837 ui::MouseEvent
mouse(ui::ET_MOUSE_MOVED
, event_location
, event_location
, 0,
839 DispatchEventUsingWindowDispatcher(&mouse
);
841 EXPECT_TRUE(recorder_first
.events().empty());
842 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED",
843 EventTypesToString(recorder_second
.events()));
844 ASSERT_EQ(2u, recorder_second
.mouse_locations().size());
845 EXPECT_EQ(gfx::Point(2, 3).ToString(),
846 recorder_second
.mouse_locations()[0].ToString());
849 // Verifies that a direct call to ProcessedTouchEvent() with a
850 // TOUCH_PRESSED event does not cause a crash.
851 TEST_F(WindowEventDispatcherTest
, CallToProcessedTouchEvent
) {
852 test::TestWindowDelegate delegate
;
853 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
854 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
856 ui::TouchEvent
touch(
857 ui::ET_TOUCH_PRESSED
, gfx::Point(10, 10), 1, ui::EventTimeForNow());
858 host()->dispatcher()->ProcessedTouchEvent(
859 &touch
, window
.get(), ui::ER_UNHANDLED
);
862 // This event handler requests the dispatcher to start holding pointer-move
863 // events when it receives the first scroll-update gesture.
864 class HoldPointerOnScrollHandler
: public ui::test::TestEventHandler
{
866 HoldPointerOnScrollHandler(WindowEventDispatcher
* dispatcher
,
867 EventFilterRecorder
* filter
)
868 : dispatcher_(dispatcher
),
870 holding_moves_(false) {}
871 ~HoldPointerOnScrollHandler() override
{}
874 // ui::test::TestEventHandler:
875 void OnGestureEvent(ui::GestureEvent
* gesture
) override
{
876 if (!holding_moves_
&& gesture
->type() == ui::ET_GESTURE_SCROLL_UPDATE
) {
877 holding_moves_
= true;
878 dispatcher_
->HoldPointerMoves();
880 } else if (gesture
->type() == ui::ET_GESTURE_SCROLL_END
) {
881 dispatcher_
->ReleasePointerMoves();
882 holding_moves_
= false;
886 WindowEventDispatcher
* dispatcher_
;
887 EventFilterRecorder
* filter_
;
890 DISALLOW_COPY_AND_ASSIGN(HoldPointerOnScrollHandler
);
893 // Tests that touch-move events don't contribute to an in-progress scroll
894 // gesture if touch-move events are being held by the dispatcher.
895 TEST_F(WindowEventDispatcherTest
, TouchMovesHeldOnScroll
) {
896 EventFilterRecorder recorder
;
897 root_window()->AddPreTargetHandler(&recorder
);
898 test::TestWindowDelegate delegate
;
899 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
900 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
901 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
902 window
->AddPreTargetHandler(&handler
);
904 ui::test::EventGenerator
generator(root_window());
905 generator
.GestureScrollSequence(
906 gfx::Point(60, 60), gfx::Point(10, 60),
907 base::TimeDelta::FromMilliseconds(100), 25);
909 // |handler| will have reset |filter| and started holding the touch-move
910 // events when scrolling started. At the end of the scroll (i.e. upon
911 // touch-release), the held touch-move event will have been dispatched first,
912 // along with the subsequent events (i.e. touch-release, scroll-end, and
914 const EventFilterRecorder::Events
& events
= recorder
.events();
916 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
917 "GESTURE_SCROLL_END GESTURE_END",
918 EventTypesToString(events
));
919 ASSERT_EQ(2u, recorder
.touch_locations().size());
920 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
921 recorder
.touch_locations()[0].ToString());
922 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
923 recorder
.touch_locations()[1].ToString());
926 // Tests that a 'held' touch-event does contribute to gesture event when it is
928 TEST_F(WindowEventDispatcherTest
, HeldTouchMoveContributesToGesture
) {
929 EventFilterRecorder recorder
;
930 root_window()->AddPreTargetHandler(&recorder
);
932 const gfx::Point
location(20, 20);
933 ui::TouchEvent
press(
934 ui::ET_TOUCH_PRESSED
, location
, 0, ui::EventTimeForNow());
935 DispatchEventUsingWindowDispatcher(&press
);
936 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_PRESSED
));
939 host()->dispatcher()->HoldPointerMoves();
941 ui::TouchEvent
move(ui::ET_TOUCH_MOVED
,
942 location
+ gfx::Vector2d(100, 100),
944 ui::EventTimeForNow());
945 DispatchEventUsingWindowDispatcher(&move
);
946 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
947 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
950 host()->dispatcher()->ReleasePointerMoves();
951 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
952 RunAllPendingInMessageLoop();
953 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
954 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
955 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE
));
957 root_window()->RemovePreTargetHandler(&recorder
);
960 // Tests that synthetic mouse events are ignored when mouse
961 // events are disabled.
962 TEST_F(WindowEventDispatcherTest
, DispatchSyntheticMouseEvents
) {
963 EventFilterRecorder recorder
;
964 root_window()->AddPreTargetHandler(&recorder
);
966 test::TestWindowDelegate delegate
;
967 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
968 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
970 window
->SetCapture();
972 test::TestCursorClient
cursor_client(root_window());
974 // Dispatch a non-synthetic mouse event when mouse events are enabled.
975 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
976 gfx::Point(10, 10), 0, 0);
977 DispatchEventUsingWindowDispatcher(&mouse1
);
978 EXPECT_FALSE(recorder
.events().empty());
981 // Dispatch a synthetic mouse event when mouse events are enabled.
982 ui::MouseEvent
mouse2(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
983 gfx::Point(10, 10), ui::EF_IS_SYNTHESIZED
, 0);
984 DispatchEventUsingWindowDispatcher(&mouse2
);
985 EXPECT_FALSE(recorder
.events().empty());
988 // Dispatch a synthetic mouse event when mouse events are disabled.
989 cursor_client
.DisableMouseEvents();
990 DispatchEventUsingWindowDispatcher(&mouse2
);
991 EXPECT_TRUE(recorder
.events().empty());
992 root_window()->RemovePreTargetHandler(&recorder
);
995 // Tests that a mouse-move event is not synthesized when a mouse-button is down.
996 TEST_F(WindowEventDispatcherTest
, DoNotSynthesizeWhileButtonDown
) {
997 EventFilterRecorder recorder
;
998 test::TestWindowDelegate delegate
;
999 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1000 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1003 window
->AddPreTargetHandler(&recorder
);
1004 // Dispatch a non-synthetic mouse event when mouse events are enabled.
1005 ui::MouseEvent
mouse1(ui::ET_MOUSE_PRESSED
, gfx::Point(10, 10),
1006 gfx::Point(10, 10), ui::EF_LEFT_MOUSE_BUTTON
,
1007 ui::EF_LEFT_MOUSE_BUTTON
);
1008 DispatchEventUsingWindowDispatcher(&mouse1
);
1009 ASSERT_EQ(1u, recorder
.events().size());
1010 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, recorder
.events()[0]);
1011 window
->RemovePreTargetHandler(&recorder
);
1014 // Move |window| away from underneath the cursor.
1015 root_window()->AddPreTargetHandler(&recorder
);
1016 window
->SetBounds(gfx::Rect(30, 30, 100, 100));
1017 EXPECT_TRUE(recorder
.events().empty());
1018 RunAllPendingInMessageLoop();
1019 EXPECT_TRUE(recorder
.events().empty());
1020 root_window()->RemovePreTargetHandler(&recorder
);
1023 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
1024 #define MAYBE(x) DISABLED_##x
1029 // Tests synthetic mouse events generated when window bounds changes such that
1030 // the cursor previously outside the window becomes inside, or vice versa.
1031 // Do not synthesize events if the window ignores events or is invisible.
1032 // Flaky on 32-bit Windows bots. http://crbug.com/388272
1033 TEST_F(WindowEventDispatcherTest
,
1034 MAYBE(SynthesizeMouseEventsOnWindowBoundsChanged
)) {
1035 test::TestWindowDelegate delegate
;
1036 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1037 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1039 window
->SetCapture();
1041 EventFilterRecorder recorder
;
1042 window
->AddPreTargetHandler(&recorder
);
1044 // Dispatch a non-synthetic mouse event to place cursor inside window bounds.
1045 ui::MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
1046 gfx::Point(10, 10), 0, 0);
1047 DispatchEventUsingWindowDispatcher(&mouse
);
1048 EXPECT_FALSE(recorder
.events().empty());
1051 // Update the window bounds so that cursor is now outside the window.
1052 // This should trigger a synthetic MOVED event.
1053 gfx::Rect
bounds1(20, 20, 100, 100);
1054 window
->SetBounds(bounds1
);
1055 RunAllPendingInMessageLoop();
1056 ASSERT_FALSE(recorder
.events().empty());
1057 ASSERT_FALSE(recorder
.mouse_event_flags().empty());
1058 EXPECT_EQ(ui::ET_MOUSE_MOVED
, recorder
.events().back());
1059 EXPECT_EQ(ui::EF_IS_SYNTHESIZED
, recorder
.mouse_event_flags().back());
1062 // Set window to ignore events.
1063 window
->set_ignore_events(true);
1065 // Update the window bounds so that cursor is back inside the window.
1066 // This should not trigger a synthetic event.
1067 gfx::Rect
bounds2(5, 5, 100, 100);
1068 window
->SetBounds(bounds2
);
1069 RunAllPendingInMessageLoop();
1070 EXPECT_TRUE(recorder
.events().empty());
1073 // Set window to accept events but invisible.
1074 window
->set_ignore_events(false);
1078 // Update the window bounds so that cursor is outside the window.
1079 // This should not trigger a synthetic event.
1080 window
->SetBounds(bounds1
);
1081 RunAllPendingInMessageLoop();
1082 EXPECT_TRUE(recorder
.events().empty());
1085 // Tests that a mouse exit is dispatched to the last known cursor location
1086 // when the cursor becomes invisible.
1087 TEST_F(WindowEventDispatcherTest
, DispatchMouseExitWhenCursorHidden
) {
1088 EventFilterRecorder recorder
;
1089 root_window()->AddPreTargetHandler(&recorder
);
1091 test::TestWindowDelegate delegate
;
1092 gfx::Point
window_origin(7, 18);
1093 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1094 &delegate
, 1234, gfx::Rect(window_origin
, gfx::Size(100, 100)),
1098 // Dispatch a mouse move event into the window.
1099 gfx::Point
mouse_location(gfx::Point(15, 25));
1100 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, mouse_location
,
1101 mouse_location
, 0, 0);
1102 EXPECT_TRUE(recorder
.events().empty());
1103 DispatchEventUsingWindowDispatcher(&mouse1
);
1104 EXPECT_FALSE(recorder
.events().empty());
1107 // Hide the cursor and verify a mouse exit was dispatched.
1108 host()->OnCursorVisibilityChanged(false);
1109 EXPECT_FALSE(recorder
.events().empty());
1110 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder
.events()));
1112 // Verify the mouse exit was dispatched at the correct location
1113 // (in the correct coordinate space).
1114 int translated_x
= mouse_location
.x() - window_origin
.x();
1115 int translated_y
= mouse_location
.y() - window_origin
.y();
1116 gfx::Point
translated_point(translated_x
, translated_y
);
1117 EXPECT_EQ(recorder
.mouse_location(0).ToString(), translated_point
.ToString());
1118 root_window()->RemovePreTargetHandler(&recorder
);
1121 // Tests that a synthetic mouse exit is dispatched to the last known cursor
1122 // location after mouse events are disabled on the cursor client.
1123 TEST_F(WindowEventDispatcherTest
,
1124 DispatchSyntheticMouseExitAfterMouseEventsDisabled
) {
1125 EventFilterRecorder recorder
;
1126 root_window()->AddPreTargetHandler(&recorder
);
1128 test::TestWindowDelegate delegate
;
1129 gfx::Point
window_origin(7, 18);
1130 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1131 &delegate
, 1234, gfx::Rect(window_origin
, gfx::Size(100, 100)),
1135 // Dispatch a mouse move event into the window.
1136 gfx::Point
mouse_location(gfx::Point(15, 25));
1137 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, mouse_location
,
1138 mouse_location
, 0, 0);
1139 EXPECT_TRUE(recorder
.events().empty());
1140 DispatchEventUsingWindowDispatcher(&mouse1
);
1141 EXPECT_FALSE(recorder
.events().empty());
1144 test::TestCursorClient
cursor_client(root_window());
1145 cursor_client
.DisableMouseEvents();
1147 gfx::Point
mouse_exit_location(gfx::Point(150, 150));
1148 ui::MouseEvent
mouse2(ui::ET_MOUSE_EXITED
, gfx::Point(150, 150),
1149 gfx::Point(150, 150), ui::EF_IS_SYNTHESIZED
, 0);
1150 DispatchEventUsingWindowDispatcher(&mouse2
);
1152 EXPECT_FALSE(recorder
.events().empty());
1153 // We get the mouse exited event twice in our filter. Once during the
1154 // predispatch phase and during the actual dispatch.
1155 EXPECT_EQ("MOUSE_EXITED MOUSE_EXITED", EventTypesToString(recorder
.events()));
1157 // Verify the mouse exit was dispatched at the correct location
1158 // (in the correct coordinate space).
1159 int translated_x
= mouse_exit_location
.x() - window_origin
.x();
1160 int translated_y
= mouse_exit_location
.y() - window_origin
.y();
1161 gfx::Point
translated_point(translated_x
, translated_y
);
1162 EXPECT_EQ(recorder
.mouse_location(0).ToString(), translated_point
.ToString());
1163 root_window()->RemovePreTargetHandler(&recorder
);
1166 class DeletingEventFilter
: public ui::EventHandler
{
1168 DeletingEventFilter()
1169 : delete_during_pre_handle_(false) {}
1170 ~DeletingEventFilter() override
{}
1172 void Reset(bool delete_during_pre_handle
) {
1173 delete_during_pre_handle_
= delete_during_pre_handle
;
1177 // Overridden from ui::EventHandler:
1178 void OnKeyEvent(ui::KeyEvent
* event
) override
{
1179 if (delete_during_pre_handle_
)
1180 delete event
->target();
1183 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1184 if (delete_during_pre_handle_
)
1185 delete event
->target();
1188 bool delete_during_pre_handle_
;
1190 DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter
);
1193 class DeletingWindowDelegate
: public test::TestWindowDelegate
{
1195 DeletingWindowDelegate()
1197 delete_during_handle_(false),
1198 got_event_(false) {}
1199 ~DeletingWindowDelegate() override
{}
1201 void Reset(Window
* window
, bool delete_during_handle
) {
1203 delete_during_handle_
= delete_during_handle
;
1206 bool got_event() const { return got_event_
; }
1209 // Overridden from WindowDelegate:
1210 void OnKeyEvent(ui::KeyEvent
* event
) override
{
1211 if (delete_during_handle_
)
1216 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1217 if (delete_during_handle_
)
1223 bool delete_during_handle_
;
1226 DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate
);
1229 TEST_F(WindowEventDispatcherTest
, DeleteWindowDuringDispatch
) {
1230 // Verifies that we can delete a window during each phase of event handling.
1231 // Deleting the window should not cause a crash, only prevent further
1232 // processing from occurring.
1233 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1234 DeletingWindowDelegate d11
;
1235 Window
* w11
= CreateNormalWindow(11, w1
.get(), &d11
);
1236 WindowTracker tracker
;
1237 DeletingEventFilter w1_filter
;
1238 w1
->AddPreTargetHandler(&w1_filter
);
1239 client::GetFocusClient(w1
.get())->FocusWindow(w11
);
1241 ui::test::EventGenerator
generator(root_window(), w11
);
1243 // First up, no one deletes anything.
1245 d11
.Reset(w11
, false);
1247 generator
.PressLeftButton();
1248 EXPECT_TRUE(tracker
.Contains(w11
));
1249 EXPECT_TRUE(d11
.got_event());
1250 generator
.ReleaseLeftButton();
1252 // Delegate deletes w11. This will prevent the post-handle step from applying.
1253 w1_filter
.Reset(false);
1254 d11
.Reset(w11
, true);
1255 generator
.PressKey(ui::VKEY_A
, 0);
1256 EXPECT_FALSE(tracker
.Contains(w11
));
1257 EXPECT_TRUE(d11
.got_event());
1259 // Pre-handle step deletes w11. This will prevent the delegate and the post-
1260 // handle steps from applying.
1261 w11
= CreateNormalWindow(11, w1
.get(), &d11
);
1262 w1_filter
.Reset(true);
1263 d11
.Reset(w11
, false);
1264 generator
.PressLeftButton();
1265 EXPECT_FALSE(tracker
.Contains(w11
));
1266 EXPECT_FALSE(d11
.got_event());
1271 // A window delegate that detaches the parent of the target's parent window when
1272 // it receives a tap event.
1273 class DetachesParentOnTapDelegate
: public test::TestWindowDelegate
{
1275 DetachesParentOnTapDelegate() {}
1276 ~DetachesParentOnTapDelegate() override
{}
1279 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1280 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
1281 event
->SetHandled();
1285 if (event
->type() == ui::ET_GESTURE_TAP
) {
1286 Window
* parent
= static_cast<Window
*>(event
->target())->parent();
1287 parent
->parent()->RemoveChild(parent
);
1288 event
->SetHandled();
1292 DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate
);
1297 // Tests that the gesture recognizer is reset for all child windows when a
1298 // window hides. No expectations, just checks that the test does not crash.
1299 TEST_F(WindowEventDispatcherTest
,
1300 GestureRecognizerResetsTargetWhenParentHides
) {
1301 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1302 DetachesParentOnTapDelegate delegate
;
1303 scoped_ptr
<Window
> parent(CreateNormalWindow(22, w1
.get(), NULL
));
1304 Window
* child
= CreateNormalWindow(11, parent
.get(), &delegate
);
1305 ui::test::EventGenerator
generator(root_window(), child
);
1306 generator
.GestureTapAt(gfx::Point(40, 40));
1311 // A window delegate that processes nested gestures on tap.
1312 class NestedGestureDelegate
: public test::TestWindowDelegate
{
1314 NestedGestureDelegate(ui::test::EventGenerator
* generator
,
1315 const gfx::Point tap_location
)
1316 : generator_(generator
),
1317 tap_location_(tap_location
),
1318 gesture_end_count_(0) {}
1319 ~NestedGestureDelegate() override
{}
1321 int gesture_end_count() const { return gesture_end_count_
; }
1324 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1325 switch (event
->type()) {
1326 case ui::ET_GESTURE_TAP_DOWN
:
1327 event
->SetHandled();
1329 case ui::ET_GESTURE_TAP
:
1331 generator_
->GestureTapAt(tap_location_
);
1332 event
->SetHandled();
1334 case ui::ET_GESTURE_END
:
1335 ++gesture_end_count_
;
1342 ui::test::EventGenerator
* generator_
;
1343 const gfx::Point tap_location_
;
1344 int gesture_end_count_
;
1345 DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate
);
1350 // Tests that gesture end is delivered after nested gesture processing.
1351 TEST_F(WindowEventDispatcherTest
, GestureEndDeliveredAfterNestedGestures
) {
1352 NestedGestureDelegate
d1(NULL
, gfx::Point());
1353 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &d1
));
1354 w1
->SetBounds(gfx::Rect(0, 0, 100, 100));
1356 ui::test::EventGenerator
nested_generator(root_window(), w1
.get());
1357 NestedGestureDelegate
d2(&nested_generator
, w1
->bounds().CenterPoint());
1358 scoped_ptr
<Window
> w2(CreateNormalWindow(1, root_window(), &d2
));
1359 w2
->SetBounds(gfx::Rect(100, 0, 100, 100));
1361 // Tap on w2 which triggers nested gestures for w1.
1362 ui::test::EventGenerator
generator(root_window(), w2
.get());
1363 generator
.GestureTapAt(w2
->bounds().CenterPoint());
1365 // Both windows should get their gesture end events.
1366 EXPECT_EQ(1, d1
.gesture_end_count());
1367 EXPECT_EQ(1, d2
.gesture_end_count());
1370 // Tests whether we can repost the Tap down gesture event.
1371 TEST_F(WindowEventDispatcherTest
, RepostTapdownGestureTest
) {
1372 EventFilterRecorder recorder
;
1373 root_window()->AddPreTargetHandler(&recorder
);
1375 test::TestWindowDelegate delegate
;
1376 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1377 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1379 ui::GestureEventDetails
details(ui::ET_GESTURE_TAP_DOWN
);
1380 gfx::Point
point(10, 10);
1381 ui::GestureEvent
event(point
.x(),
1384 ui::EventTimeForNow(),
1386 host()->dispatcher()->RepostEvent(event
);
1387 RunAllPendingInMessageLoop();
1388 // TODO(rbyers): Currently disabled - crbug.com/170987
1389 EXPECT_FALSE(EventTypesToString(recorder
.events()).find("GESTURE_TAP_DOWN") !=
1392 root_window()->RemovePreTargetHandler(&recorder
);
1395 // This class inherits from the EventFilterRecorder class which provides a
1396 // facility to record events. This class additionally provides a facility to
1397 // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
1398 // events after that.
1399 class RepostGestureEventRecorder
: public EventFilterRecorder
{
1401 RepostGestureEventRecorder(aura::Window
* repost_source
,
1402 aura::Window
* repost_target
)
1403 : repost_source_(repost_source
),
1404 repost_target_(repost_target
),
1406 done_cleanup_(false) {}
1408 ~RepostGestureEventRecorder() override
{}
1410 void OnTouchEvent(ui::TouchEvent
* event
) override
{
1411 if (reposted_
&& event
->type() == ui::ET_TOUCH_PRESSED
) {
1412 done_cleanup_
= true;
1415 EventFilterRecorder::OnTouchEvent(event
);
1418 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1419 EXPECT_EQ(done_cleanup_
? repost_target_
: repost_source_
, event
->target());
1420 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
1422 EXPECT_NE(repost_target_
, event
->target());
1424 repost_target_
->GetHost()->dispatcher()->RepostEvent(*event
);
1425 // Ensure that the reposted gesture event above goes to the
1427 repost_source_
->GetRootWindow()->RemoveChild(repost_source_
);
1431 EventFilterRecorder::OnGestureEvent(event
);
1434 // Ignore mouse events as they don't fire at all times. This causes
1435 // the GestureRepostEventOrder test to fail randomly.
1436 void OnMouseEvent(ui::MouseEvent
* event
) override
{}
1439 aura::Window
* repost_source_
;
1440 aura::Window
* repost_target_
;
1441 // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
1443 // set true if we're done cleaning up after hiding repost_source_;
1445 DISALLOW_COPY_AND_ASSIGN(RepostGestureEventRecorder
);
1448 // Tests whether events which are generated after the reposted gesture event
1449 // are received after that. In this case the scroll sequence events should
1450 // be received after the reposted gesture event.
1451 TEST_F(WindowEventDispatcherTest
, GestureRepostEventOrder
) {
1452 // Expected events at the end for the repost_target window defined below.
1453 const char kExpectedTargetEvents
[] =
1454 // TODO)(rbyers): Gesture event reposting is disabled - crbug.com/279039.
1455 // "GESTURE_BEGIN GESTURE_TAP_DOWN "
1456 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1457 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE TOUCH_MOVED "
1458 "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
1459 "GESTURE_SCROLL_END GESTURE_END";
1460 // We create two windows.
1461 // The first window (repost_source) is the one to which the initial tap
1462 // gesture is sent. It reposts this event to the second window
1464 // We then generate the scroll sequence for repost_target and look for two
1465 // ET_GESTURE_TAP_DOWN events in the event list at the end.
1466 test::TestWindowDelegate delegate
;
1467 scoped_ptr
<aura::Window
> repost_target(CreateTestWindowWithDelegate(
1468 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1470 scoped_ptr
<aura::Window
> repost_source(CreateTestWindowWithDelegate(
1471 &delegate
, 1, gfx::Rect(0, 0, 50, 50), root_window()));
1473 RepostGestureEventRecorder
repost_event_recorder(repost_source
.get(),
1474 repost_target
.get());
1475 root_window()->AddPreTargetHandler(&repost_event_recorder
);
1477 // Generate a tap down gesture for the repost_source. This will be reposted
1478 // to repost_target.
1479 ui::test::EventGenerator
repost_generator(root_window(), repost_source
.get());
1480 repost_generator
.GestureTapAt(gfx::Point(40, 40));
1481 RunAllPendingInMessageLoop();
1483 ui::test::EventGenerator
scroll_generator(root_window(), repost_target
.get());
1484 scroll_generator
.GestureScrollSequence(
1486 gfx::Point(100, 100),
1487 base::TimeDelta::FromMilliseconds(100),
1489 RunAllPendingInMessageLoop();
1491 int tap_down_count
= 0;
1492 for (size_t i
= 0; i
< repost_event_recorder
.events().size(); ++i
) {
1493 if (repost_event_recorder
.events()[i
] == ui::ET_GESTURE_TAP_DOWN
)
1497 // We expect two tap down events. One from the repost and the other one from
1498 // the scroll sequence posted above.
1499 // TODO(rbyers): Currently disabled - crbug.com/170987
1500 EXPECT_EQ(1, tap_down_count
);
1502 EXPECT_EQ(kExpectedTargetEvents
,
1503 EventTypesToString(repost_event_recorder
.events()));
1504 root_window()->RemovePreTargetHandler(&repost_event_recorder
);
1507 class OnMouseExitDeletingEventFilter
: public EventFilterRecorder
{
1509 OnMouseExitDeletingEventFilter() : window_to_delete_(NULL
) {}
1510 ~OnMouseExitDeletingEventFilter() override
{}
1512 void set_window_to_delete(Window
* window_to_delete
) {
1513 window_to_delete_
= window_to_delete
;
1517 // Overridden from ui::EventHandler:
1518 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1519 EventFilterRecorder::OnMouseEvent(event
);
1520 if (window_to_delete_
) {
1521 delete window_to_delete_
;
1522 window_to_delete_
= NULL
;
1526 Window
* window_to_delete_
;
1528 DISALLOW_COPY_AND_ASSIGN(OnMouseExitDeletingEventFilter
);
1531 // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
1532 // a child, but the child is destroyed because of the synthesized mouse-exit
1533 // event generated on the previous mouse_moved_handler_.
1534 TEST_F(WindowEventDispatcherTest
, DeleteWindowDuringMouseMovedDispatch
) {
1535 // Create window 1 and set its event filter. Window 1 will take ownership of
1536 // the event filter.
1537 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1538 OnMouseExitDeletingEventFilter w1_filter
;
1539 w1
->AddPreTargetHandler(&w1_filter
);
1540 w1
->SetBounds(gfx::Rect(20, 20, 60, 60));
1541 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_moved_handler());
1543 ui::test::EventGenerator
generator(root_window(), w1
.get());
1545 // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
1547 generator
.MoveMouseTo(51, 51);
1548 EXPECT_EQ(w1
.get(), host()->dispatcher()->mouse_moved_handler());
1550 // Create window 2 under the mouse cursor and stack it above window 1.
1551 Window
* w2
= CreateNormalWindow(2, root_window(), NULL
);
1552 w2
->SetBounds(gfx::Rect(30, 30, 40, 40));
1553 root_window()->StackChildAbove(w2
, w1
.get());
1555 // Set window 2 as the window that is to be deleted when a mouse-exited event
1556 // happens on window 1.
1557 w1_filter
.set_window_to_delete(w2
);
1559 // Move mosue over window 2. This should generate a mouse-exited event for
1560 // window 1 resulting in deletion of window 2. The original mouse-moved event
1561 // that was targeted to window 2 should be dropped since window 2 is
1562 // destroyed. This test passes if no crash happens.
1563 generator
.MoveMouseTo(52, 52);
1564 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_moved_handler());
1566 // Check events received by window 1.
1567 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
1568 EventTypesToString(w1_filter
.events()));
1573 // Used to track if OnWindowDestroying() is invoked and if there is a valid
1574 // RootWindow at such time.
1575 class ValidRootDuringDestructionWindowObserver
: public aura::WindowObserver
{
1577 ValidRootDuringDestructionWindowObserver(bool* got_destroying
,
1578 bool* has_valid_root
)
1579 : got_destroying_(got_destroying
),
1580 has_valid_root_(has_valid_root
) {
1584 void OnWindowDestroying(aura::Window
* window
) override
{
1585 *got_destroying_
= true;
1586 *has_valid_root_
= (window
->GetRootWindow() != NULL
);
1590 bool* got_destroying_
;
1591 bool* has_valid_root_
;
1593 DISALLOW_COPY_AND_ASSIGN(ValidRootDuringDestructionWindowObserver
);
1598 // Verifies GetRootWindow() from ~Window returns a valid root.
1599 TEST_F(WindowEventDispatcherTest
, ValidRootDuringDestruction
) {
1600 bool got_destroying
= false;
1601 bool has_valid_root
= false;
1602 ValidRootDuringDestructionWindowObserver
observer(&got_destroying
,
1605 scoped_ptr
<WindowTreeHost
> host(
1606 WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100)));
1608 // Owned by WindowEventDispatcher.
1609 Window
* w1
= CreateNormalWindow(1, host
->window(), NULL
);
1610 w1
->AddObserver(&observer
);
1612 EXPECT_TRUE(got_destroying
);
1613 EXPECT_TRUE(has_valid_root
);
1618 // See description above DontResetHeldEvent for details.
1619 class DontResetHeldEventWindowDelegate
: public test::TestWindowDelegate
{
1621 explicit DontResetHeldEventWindowDelegate(aura::Window
* root
)
1623 mouse_event_count_(0) {}
1624 ~DontResetHeldEventWindowDelegate() override
{}
1626 int mouse_event_count() const { return mouse_event_count_
; }
1628 // TestWindowDelegate:
1629 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1630 if ((event
->flags() & ui::EF_SHIFT_DOWN
) != 0 &&
1631 mouse_event_count_
++ == 0) {
1632 ui::MouseEvent
mouse_event(ui::ET_MOUSE_PRESSED
,
1633 gfx::Point(10, 10), gfx::Point(10, 10),
1634 ui::EF_SHIFT_DOWN
, 0);
1635 root_
->GetHost()->dispatcher()->RepostEvent(mouse_event
);
1641 int mouse_event_count_
;
1643 DISALLOW_COPY_AND_ASSIGN(DontResetHeldEventWindowDelegate
);
1648 // Verifies RootWindow doesn't reset |RootWindow::held_repostable_event_| after
1649 // dispatching. This is done by using DontResetHeldEventWindowDelegate, which
1650 // tracks the number of events with ui::EF_SHIFT_DOWN set (all reposted events
1651 // have EF_SHIFT_DOWN). When the first event is seen RepostEvent() is used to
1652 // schedule another reposted event.
1653 TEST_F(WindowEventDispatcherTest
, DontResetHeldEvent
) {
1654 DontResetHeldEventWindowDelegate
delegate(root_window());
1655 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &delegate
));
1656 w1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1657 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
1658 gfx::Point(10, 10), gfx::Point(10, 10),
1659 ui::EF_SHIFT_DOWN
, 0);
1660 root_window()->GetHost()->dispatcher()->RepostEvent(pressed
);
1661 ui::MouseEvent
pressed2(ui::ET_MOUSE_PRESSED
,
1662 gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
1663 // Dispatch an event to flush event scheduled by way of RepostEvent().
1664 DispatchEventUsingWindowDispatcher(&pressed2
);
1665 // Delegate should have seen reposted event (identified by way of
1666 // EF_SHIFT_DOWN). Dispatch another event to flush the second
1668 EXPECT_EQ(1, delegate
.mouse_event_count());
1669 DispatchEventUsingWindowDispatcher(&pressed2
);
1670 EXPECT_EQ(2, delegate
.mouse_event_count());
1675 // See description above DeleteHostFromHeldMouseEvent for details.
1676 class DeleteHostFromHeldMouseEventDelegate
1677 : public test::TestWindowDelegate
{
1679 explicit DeleteHostFromHeldMouseEventDelegate(WindowTreeHost
* host
)
1681 got_mouse_event_(false),
1682 got_destroy_(false) {
1684 ~DeleteHostFromHeldMouseEventDelegate() override
{}
1686 bool got_mouse_event() const { return got_mouse_event_
; }
1687 bool got_destroy() const { return got_destroy_
; }
1689 // TestWindowDelegate:
1690 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1691 if ((event
->flags() & ui::EF_SHIFT_DOWN
) != 0) {
1692 got_mouse_event_
= true;
1696 void OnWindowDestroyed(Window
* window
) override
{ got_destroy_
= true; }
1699 WindowTreeHost
* host_
;
1700 bool got_mouse_event_
;
1703 DISALLOW_COPY_AND_ASSIGN(DeleteHostFromHeldMouseEventDelegate
);
1708 // Verifies if a WindowTreeHost is deleted from dispatching a held mouse event
1710 TEST_F(WindowEventDispatcherTest
, DeleteHostFromHeldMouseEvent
) {
1711 // Should be deleted by |delegate|.
1712 WindowTreeHost
* h2
= WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100));
1714 DeleteHostFromHeldMouseEventDelegate
delegate(h2
);
1716 Window
* w1
= CreateNormalWindow(1, h2
->window(), &delegate
);
1717 w1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1718 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
1719 gfx::Point(10, 10), gfx::Point(10, 10),
1720 ui::EF_SHIFT_DOWN
, 0);
1721 h2
->dispatcher()->RepostEvent(pressed
);
1722 // RunAllPendingInMessageLoop() to make sure the |pressed| is run.
1723 RunAllPendingInMessageLoop();
1724 EXPECT_TRUE(delegate
.got_mouse_event());
1725 EXPECT_TRUE(delegate
.got_destroy());
1728 TEST_F(WindowEventDispatcherTest
, WindowHideCancelsActiveTouches
) {
1729 EventFilterRecorder recorder
;
1730 root_window()->AddPreTargetHandler(&recorder
);
1732 test::TestWindowDelegate delegate
;
1733 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1734 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1736 gfx::Point position1
= root_window()->bounds().origin();
1737 ui::TouchEvent
press(
1738 ui::ET_TOUCH_PRESSED
, position1
, 0, ui::EventTimeForNow());
1739 DispatchEventUsingWindowDispatcher(&press
);
1741 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN",
1742 EventTypesToString(recorder
.GetAndResetEvents()));
1746 EXPECT_EQ(ui::ET_TOUCH_CANCELLED
, recorder
.events()[0]);
1747 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_TAP_CANCEL
));
1748 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_END
));
1749 EXPECT_EQ(3U, recorder
.events().size());
1750 root_window()->RemovePreTargetHandler(&recorder
);
1753 TEST_F(WindowEventDispatcherTest
, WindowHideCancelsActiveGestures
) {
1754 EventFilterRecorder recorder
;
1755 root_window()->AddPreTargetHandler(&recorder
);
1757 test::TestWindowDelegate delegate
;
1758 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1759 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1761 gfx::Point position1
= root_window()->bounds().origin();
1762 gfx::Point position2
= root_window()->bounds().CenterPoint();
1763 ui::TouchEvent
press(
1764 ui::ET_TOUCH_PRESSED
, position1
, 0, ui::EventTimeForNow());
1765 DispatchEventUsingWindowDispatcher(&press
);
1767 ui::TouchEvent
move(
1768 ui::ET_TOUCH_MOVED
, position2
, 0, ui::EventTimeForNow());
1769 DispatchEventUsingWindowDispatcher(&move
);
1771 ui::TouchEvent
press2(
1772 ui::ET_TOUCH_PRESSED
, position1
, 1, ui::EventTimeForNow());
1773 DispatchEventUsingWindowDispatcher(&press2
);
1775 // TODO(tdresser): once the unified Gesture Recognizer has stuck, remove the
1776 // special casing here. See crbug.com/332418 for details.
1777 std::string expected
=
1778 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1779 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1780 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_PINCH_BEGIN";
1782 std::string expected_ugr
=
1783 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1784 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1785 "TOUCH_PRESSED GESTURE_BEGIN";
1787 std::string events_string
= EventTypesToString(recorder
.GetAndResetEvents());
1788 EXPECT_TRUE((expected
== events_string
) || (expected_ugr
== events_string
));
1793 "TOUCH_CANCELLED GESTURE_PINCH_END GESTURE_END TOUCH_CANCELLED "
1794 "GESTURE_SCROLL_END GESTURE_END";
1796 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END TOUCH_CANCELLED "
1799 events_string
= EventTypesToString(recorder
.GetAndResetEvents());
1800 EXPECT_TRUE((expected
== events_string
) || (expected_ugr
== events_string
));
1802 root_window()->RemovePreTargetHandler(&recorder
);
1805 // Places two windows side by side. Presses down on one window, and starts a
1806 // scroll. Sets capture on the other window and ensures that the "ending" events
1807 // aren't sent to the window which gained capture.
1808 TEST_F(WindowEventDispatcherTest
, EndingEventDoesntRetarget
) {
1809 EventFilterRecorder recorder1
;
1810 EventFilterRecorder recorder2
;
1811 scoped_ptr
<Window
> window1(CreateNormalWindow(1, root_window(), NULL
));
1812 window1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1814 scoped_ptr
<Window
> window2(CreateNormalWindow(2, root_window(), NULL
));
1815 window2
->SetBounds(gfx::Rect(40, 0, 40, 40));
1817 window1
->AddPreTargetHandler(&recorder1
);
1818 window2
->AddPreTargetHandler(&recorder2
);
1820 gfx::Point position
= window1
->bounds().origin();
1821 ui::TouchEvent
press(
1822 ui::ET_TOUCH_PRESSED
, position
, 0, ui::EventTimeForNow());
1823 DispatchEventUsingWindowDispatcher(&press
);
1825 gfx::Point position2
= window1
->bounds().CenterPoint();
1826 ui::TouchEvent
move(
1827 ui::ET_TOUCH_MOVED
, position2
, 0, ui::EventTimeForNow());
1828 DispatchEventUsingWindowDispatcher(&move
);
1830 window2
->SetCapture();
1832 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1833 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1834 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END",
1835 EventTypesToString(recorder1
.events()));
1837 EXPECT_TRUE(recorder2
.events().empty());
1842 // This class creates and manages a window which is destroyed as soon as
1843 // capture is lost. This is the case for the drag and drop capture window.
1844 class CaptureWindowTracker
: public test::TestWindowDelegate
{
1846 CaptureWindowTracker() {}
1847 ~CaptureWindowTracker() override
{}
1849 void CreateCaptureWindow(aura::Window
* root_window
) {
1850 capture_window_
.reset(test::CreateTestWindowWithDelegate(
1851 this, -1234, gfx::Rect(20, 20, 20, 20), root_window
));
1852 capture_window_
->SetCapture();
1856 capture_window_
.reset();
1859 void OnCaptureLost() override
{ capture_window_
.reset(); }
1861 void OnWindowDestroyed(Window
* window
) override
{
1862 TestWindowDelegate::OnWindowDestroyed(window
);
1863 capture_window_
.reset();
1866 aura::Window
* capture_window() { return capture_window_
.get(); }
1869 scoped_ptr
<aura::Window
> capture_window_
;
1871 DISALLOW_COPY_AND_ASSIGN(CaptureWindowTracker
);
1876 // Verifies handling loss of capture by the capture window being hidden.
1877 TEST_F(WindowEventDispatcherTest
, CaptureWindowHidden
) {
1878 CaptureWindowTracker capture_window_tracker
;
1879 capture_window_tracker
.CreateCaptureWindow(root_window());
1880 capture_window_tracker
.capture_window()->Hide();
1881 EXPECT_EQ(NULL
, capture_window_tracker
.capture_window());
1884 // Verifies handling loss of capture by the capture window being destroyed.
1885 TEST_F(WindowEventDispatcherTest
, CaptureWindowDestroyed
) {
1886 CaptureWindowTracker capture_window_tracker
;
1887 capture_window_tracker
.CreateCaptureWindow(root_window());
1888 capture_window_tracker
.reset();
1889 EXPECT_EQ(NULL
, capture_window_tracker
.capture_window());
1892 class ExitMessageLoopOnMousePress
: public ui::test::TestEventHandler
{
1894 ExitMessageLoopOnMousePress() {}
1895 ~ExitMessageLoopOnMousePress() override
{}
1898 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1899 ui::test::TestEventHandler::OnMouseEvent(event
);
1900 if (event
->type() == ui::ET_MOUSE_PRESSED
)
1901 base::MessageLoopForUI::current()->Quit();
1905 DISALLOW_COPY_AND_ASSIGN(ExitMessageLoopOnMousePress
);
1908 class WindowEventDispatcherTestWithMessageLoop
1909 : public WindowEventDispatcherTest
{
1911 WindowEventDispatcherTestWithMessageLoop() {}
1912 ~WindowEventDispatcherTestWithMessageLoop() override
{}
1915 // Reset any event the window may have received when bringing up the window
1916 // (e.g. mouse-move events if the mouse cursor is over the window).
1919 // Start a nested message-loop, post an event to be dispatched, and then
1920 // terminate the message-loop. When the message-loop unwinds and gets back,
1921 // the reposted event should not have fired.
1922 scoped_ptr
<ui::MouseEvent
> mouse(new ui::MouseEvent(ui::ET_MOUSE_PRESSED
,
1927 message_loop()->PostTask(
1929 base::Bind(&WindowEventDispatcherTestWithMessageLoop::RepostEventHelper
,
1930 host()->dispatcher(),
1931 base::Passed(&mouse
)));
1932 message_loop()->PostTask(FROM_HERE
, message_loop()->QuitClosure());
1934 base::MessageLoop::ScopedNestableTaskAllower
allow(message_loop());
1937 EXPECT_EQ(0, handler_
.num_mouse_events());
1939 // Let the current message-loop run. The event-handler will terminate the
1940 // message-loop when it receives the reposted event.
1943 base::MessageLoop
* message_loop() {
1944 return base::MessageLoopForUI::current();
1948 void SetUp() override
{
1949 WindowEventDispatcherTest::SetUp();
1950 window_
.reset(CreateNormalWindow(1, root_window(), NULL
));
1951 window_
->AddPreTargetHandler(&handler_
);
1954 void TearDown() override
{
1956 WindowEventDispatcherTest::TearDown();
1960 // Used to avoid a copying |event| when binding to a closure.
1961 static void RepostEventHelper(WindowEventDispatcher
* dispatcher
,
1962 scoped_ptr
<ui::MouseEvent
> event
) {
1963 dispatcher
->RepostEvent(*event
);
1966 scoped_ptr
<Window
> window_
;
1967 ExitMessageLoopOnMousePress handler_
;
1969 DISALLOW_COPY_AND_ASSIGN(WindowEventDispatcherTestWithMessageLoop
);
1972 TEST_F(WindowEventDispatcherTestWithMessageLoop
, EventRepostedInNonNestedLoop
) {
1973 CHECK(!message_loop()->is_running());
1974 // Perform the test in a callback, so that it runs after the message-loop
1976 message_loop()->PostTask(
1977 FROM_HERE
, base::Bind(
1978 &WindowEventDispatcherTestWithMessageLoop::RunTest
,
1979 base::Unretained(this)));
1980 message_loop()->Run();
1983 class WindowEventDispatcherTestInHighDPI
: public WindowEventDispatcherTest
{
1985 WindowEventDispatcherTestInHighDPI() {}
1986 ~WindowEventDispatcherTestInHighDPI() override
{}
1988 void DispatchEvent(ui::Event
* event
) {
1989 DispatchEventUsingWindowDispatcher(event
);
1993 void SetUp() override
{
1994 WindowEventDispatcherTest::SetUp();
1995 test_screen()->SetDeviceScaleFactor(2.f
);
1999 TEST_F(WindowEventDispatcherTestInHighDPI
, EventLocationTransform
) {
2000 test::TestWindowDelegate delegate
;
2001 scoped_ptr
<aura::Window
> child(test::CreateTestWindowWithDelegate(&delegate
,
2002 1234, gfx::Rect(20, 20, 100, 100), root_window()));
2005 ui::test::TestEventHandler handler_child
;
2006 ui::test::TestEventHandler handler_root
;
2007 root_window()->AddPreTargetHandler(&handler_root
);
2008 child
->AddPreTargetHandler(&handler_child
);
2011 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
2012 gfx::Point(30, 30), gfx::Point(30, 30),
2013 ui::EF_NONE
, ui::EF_NONE
);
2014 DispatchEventUsingWindowDispatcher(&move
);
2015 EXPECT_EQ(0, handler_child
.num_mouse_events());
2016 EXPECT_EQ(1, handler_root
.num_mouse_events());
2020 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
2021 gfx::Point(50, 50), gfx::Point(50, 50),
2022 ui::EF_NONE
, ui::EF_NONE
);
2023 DispatchEventUsingWindowDispatcher(&move
);
2024 // The child receives an ENTER, and a MOVED event.
2025 EXPECT_EQ(2, handler_child
.num_mouse_events());
2026 // The root receives both the ENTER and the MOVED events dispatched to
2027 // |child|, as well as an EXIT event.
2028 EXPECT_EQ(3, handler_root
.num_mouse_events());
2031 child
->RemovePreTargetHandler(&handler_child
);
2032 root_window()->RemovePreTargetHandler(&handler_root
);
2035 TEST_F(WindowEventDispatcherTestInHighDPI
, TouchMovesHeldOnScroll
) {
2036 EventFilterRecorder recorder
;
2037 root_window()->AddPreTargetHandler(&recorder
);
2038 test::TestWindowDelegate delegate
;
2039 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
2040 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2041 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2042 window
->AddPreTargetHandler(&handler
);
2044 ui::test::EventGenerator
generator(root_window());
2045 generator
.GestureScrollSequence(
2046 gfx::Point(120, 120), gfx::Point(20, 120),
2047 base::TimeDelta::FromMilliseconds(100), 25);
2049 // |handler| will have reset |filter| and started holding the touch-move
2050 // events when scrolling started. At the end of the scroll (i.e. upon
2051 // touch-release), the held touch-move event will have been dispatched first,
2052 // along with the subsequent events (i.e. touch-release, scroll-end, and
2054 const EventFilterRecorder::Events
& events
= recorder
.events();
2056 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
2057 "GESTURE_SCROLL_END GESTURE_END",
2058 EventTypesToString(events
));
2059 ASSERT_EQ(2u, recorder
.touch_locations().size());
2060 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2061 recorder
.touch_locations()[0].ToString());
2062 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2063 recorder
.touch_locations()[1].ToString());
2066 // This handler triggers a nested message loop when it receives a right click
2067 // event, and runs a single callback in the nested message loop.
2068 class TriggerNestedLoopOnRightMousePress
: public ui::test::TestEventHandler
{
2070 explicit TriggerNestedLoopOnRightMousePress(const base::Closure
& callback
)
2071 : callback_(callback
) {}
2072 ~TriggerNestedLoopOnRightMousePress() override
{}
2074 const gfx::Point
mouse_move_location() const { return mouse_move_location_
; }
2077 void OnMouseEvent(ui::MouseEvent
* mouse
) override
{
2078 TestEventHandler::OnMouseEvent(mouse
);
2079 if (mouse
->type() == ui::ET_MOUSE_PRESSED
&&
2080 mouse
->IsOnlyRightMouseButton()) {
2081 base::MessageLoop::ScopedNestableTaskAllower
allow(
2082 base::MessageLoopForUI::current());
2083 base::RunLoop run_loop
;
2084 scoped_refptr
<base::TaskRunner
> task_runner
=
2085 base::ThreadTaskRunnerHandle::Get();
2086 if (!callback_
.is_null())
2087 task_runner
->PostTask(FROM_HERE
, callback_
);
2088 task_runner
->PostTask(FROM_HERE
, run_loop
.QuitClosure());
2090 } else if (mouse
->type() == ui::ET_MOUSE_MOVED
) {
2091 mouse_move_location_
= mouse
->location();
2095 base::Closure callback_
;
2096 gfx::Point mouse_move_location_
;
2098 DISALLOW_COPY_AND_ASSIGN(TriggerNestedLoopOnRightMousePress
);
2101 // Tests that if dispatching a 'held' event triggers a nested message loop, then
2102 // the events that are dispatched from the nested message loop are transformed
2104 TEST_F(WindowEventDispatcherTestInHighDPI
,
2105 EventsTransformedInRepostedEventTriggeredNestedLoop
) {
2106 scoped_ptr
<Window
> window(CreateNormalWindow(1, root_window(), NULL
));
2107 // Make sure the window is visible.
2108 RunAllPendingInMessageLoop();
2110 ui::MouseEvent
mouse_move(ui::ET_MOUSE_MOVED
, gfx::Point(80, 80),
2111 gfx::Point(80, 80), ui::EF_NONE
, ui::EF_NONE
);
2112 const base::Closure callback_on_right_click
= base::Bind(
2113 base::IgnoreResult(&WindowEventDispatcherTestInHighDPI::DispatchEvent
),
2114 base::Unretained(this), base::Unretained(&mouse_move
));
2115 TriggerNestedLoopOnRightMousePress
handler(callback_on_right_click
);
2116 window
->AddPreTargetHandler(&handler
);
2118 scoped_ptr
<ui::MouseEvent
> mouse(new ui::MouseEvent(
2119 ui::ET_MOUSE_PRESSED
, gfx::Point(10, 10), gfx::Point(10, 10),
2120 ui::EF_RIGHT_MOUSE_BUTTON
, ui::EF_RIGHT_MOUSE_BUTTON
));
2121 host()->dispatcher()->RepostEvent(*mouse
);
2122 EXPECT_EQ(0, handler
.num_mouse_events());
2124 base::RunLoop run_loop
;
2125 run_loop
.RunUntilIdle();
2126 // The window should receive the mouse-press and the mouse-move events.
2127 EXPECT_EQ(2, handler
.num_mouse_events());
2128 // The mouse-move event location should be transformed because of the DSF
2129 // before it reaches the window.
2130 EXPECT_EQ(gfx::Point(40, 40).ToString(),
2131 handler
.mouse_move_location().ToString());
2132 window
->RemovePreTargetHandler(&handler
);
2135 class SelfDestructDelegate
: public test::TestWindowDelegate
{
2137 SelfDestructDelegate() {}
2138 ~SelfDestructDelegate() override
{}
2140 void OnMouseEvent(ui::MouseEvent
* event
) override
{ window_
.reset(); }
2142 void set_window(scoped_ptr
<aura::Window
> window
) {
2143 window_
= window
.Pass();
2145 bool has_window() const { return !!window_
.get(); }
2148 scoped_ptr
<aura::Window
> window_
;
2149 DISALLOW_COPY_AND_ASSIGN(SelfDestructDelegate
);
2152 TEST_F(WindowEventDispatcherTest
, SynthesizedLocatedEvent
) {
2153 ui::test::EventGenerator
generator(root_window());
2154 generator
.MoveMouseTo(10, 10);
2156 Env::GetInstance()->last_mouse_location().ToString());
2158 // Synthesized event should not update the mouse location.
2159 ui::MouseEvent
mouseev(ui::ET_MOUSE_MOVED
, gfx::Point(), gfx::Point(),
2160 ui::EF_IS_SYNTHESIZED
, 0);
2161 generator
.Dispatch(&mouseev
);
2163 Env::GetInstance()->last_mouse_location().ToString());
2165 generator
.MoveMouseTo(0, 0);
2167 Env::GetInstance()->last_mouse_location().ToString());
2169 // Make sure the location gets updated when a syntheiszed enter
2170 // event destroyed the window.
2171 SelfDestructDelegate delegate
;
2172 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2173 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2174 delegate
.set_window(window
.Pass());
2175 EXPECT_TRUE(delegate
.has_window());
2177 generator
.MoveMouseTo(100, 100);
2178 EXPECT_FALSE(delegate
.has_window());
2179 EXPECT_EQ("100,100",
2180 Env::GetInstance()->last_mouse_location().ToString());
2183 // Tests that the window which has capture can get destroyed as a result of
2184 // ui::ET_MOUSE_CAPTURE_CHANGED event dispatched in
2185 // WindowEventDispatcher::UpdateCapture without causing a "use after free".
2186 TEST_F(WindowEventDispatcherTest
, DestroyWindowOnCaptureChanged
) {
2187 SelfDestructDelegate delegate
;
2188 scoped_ptr
<aura::Window
> window_first(CreateTestWindowWithDelegate(
2189 &delegate
, 1, gfx::Rect(20, 10, 10, 20), root_window()));
2190 Window
* window_first_raw
= window_first
.get();
2191 window_first
->Show();
2192 window_first
->SetCapture();
2193 delegate
.set_window(window_first
.Pass());
2194 EXPECT_TRUE(delegate
.has_window());
2196 scoped_ptr
<aura::Window
> window_second(
2197 test::CreateTestWindowWithId(2, root_window()));
2198 window_second
->Show();
2200 client::CaptureDelegate
* capture_delegate
= host()->dispatcher();
2201 capture_delegate
->UpdateCapture(window_first_raw
, window_second
.get());
2202 EXPECT_FALSE(delegate
.has_window());
2205 class StaticFocusClient
: public client::FocusClient
{
2207 explicit StaticFocusClient(Window
* focused
)
2208 : focused_(focused
) {}
2209 ~StaticFocusClient() override
{}
2212 // client::FocusClient:
2213 void AddObserver(client::FocusChangeObserver
* observer
) override
{}
2214 void RemoveObserver(client::FocusChangeObserver
* observer
) override
{}
2215 void FocusWindow(Window
* window
) override
{}
2216 void ResetFocusWithinActiveWindow(Window
* window
) override
{}
2217 Window
* GetFocusedWindow() override
{ return focused_
; }
2221 DISALLOW_COPY_AND_ASSIGN(StaticFocusClient
);
2224 // Tests that host-cancel-mode event can be dispatched to a dispatcher safely
2225 // when the focused window does not live in the dispatcher's tree.
2226 TEST_F(WindowEventDispatcherTest
, HostCancelModeWithFocusedWindowOutside
) {
2227 test::TestWindowDelegate delegate
;
2228 scoped_ptr
<Window
> focused(CreateTestWindowWithDelegate(&delegate
, 123,
2229 gfx::Rect(20, 30, 100, 50), NULL
));
2230 StaticFocusClient
focus_client(focused
.get());
2231 client::SetFocusClient(root_window(), &focus_client
);
2232 EXPECT_FALSE(root_window()->Contains(focused
.get()));
2233 EXPECT_EQ(focused
.get(),
2234 client::GetFocusClient(root_window())->GetFocusedWindow());
2235 host()->dispatcher()->DispatchCancelModeEvent();
2236 EXPECT_EQ(focused
.get(),
2237 client::GetFocusClient(root_window())->GetFocusedWindow());
2240 // Dispatches a mouse-move event to |target| when it receives a mouse-move
2242 class DispatchEventHandler
: public ui::EventHandler
{
2244 explicit DispatchEventHandler(Window
* target
)
2246 dispatched_(false) {}
2247 ~DispatchEventHandler() override
{}
2249 bool dispatched() const { return dispatched_
; }
2251 // ui::EventHandler:
2252 void OnMouseEvent(ui::MouseEvent
* mouse
) override
{
2253 if (mouse
->type() == ui::ET_MOUSE_MOVED
) {
2254 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, target_
->bounds().CenterPoint(),
2255 target_
->bounds().CenterPoint(), ui::EF_NONE
, ui::EF_NONE
);
2256 ui::EventDispatchDetails details
=
2257 target_
->GetHost()->dispatcher()->OnEventFromSource(&move
);
2258 ASSERT_FALSE(details
.dispatcher_destroyed
);
2259 EXPECT_FALSE(details
.target_destroyed
);
2260 EXPECT_EQ(target_
, move
.target());
2263 ui::EventHandler::OnMouseEvent(mouse
);
2269 DISALLOW_COPY_AND_ASSIGN(DispatchEventHandler
);
2272 // Moves |window| to |root_window| when it receives a mouse-move event.
2273 class MoveWindowHandler
: public ui::EventHandler
{
2275 MoveWindowHandler(Window
* window
, Window
* root_window
)
2276 : window_to_move_(window
),
2277 root_window_to_move_to_(root_window
) {}
2278 ~MoveWindowHandler() override
{}
2281 // ui::EventHandler:
2282 void OnMouseEvent(ui::MouseEvent
* mouse
) override
{
2283 if (mouse
->type() == ui::ET_MOUSE_MOVED
) {
2284 root_window_to_move_to_
->AddChild(window_to_move_
);
2286 ui::EventHandler::OnMouseEvent(mouse
);
2289 Window
* window_to_move_
;
2290 Window
* root_window_to_move_to_
;
2292 DISALLOW_COPY_AND_ASSIGN(MoveWindowHandler
);
2295 // Tests that nested event dispatch works correctly if the target of the older
2296 // event being dispatched is moved to a different dispatcher in response to an
2297 // event in the inner loop.
2298 TEST_F(WindowEventDispatcherTest
, NestedEventDispatchTargetMoved
) {
2299 scoped_ptr
<WindowTreeHost
> second_host(
2300 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2301 second_host
->InitHost();
2302 Window
* second_root
= second_host
->window();
2304 // Create two windows parented to |root_window()|.
2305 test::TestWindowDelegate delegate
;
2306 scoped_ptr
<Window
> first(CreateTestWindowWithDelegate(&delegate
, 123,
2307 gfx::Rect(20, 10, 10, 20), root_window()));
2308 scoped_ptr
<Window
> second(CreateTestWindowWithDelegate(&delegate
, 234,
2309 gfx::Rect(40, 10, 50, 20), root_window()));
2311 // Setup a handler on |first| so that it dispatches an event to |second| when
2312 // |first| receives an event.
2313 DispatchEventHandler
dispatch_event(second
.get());
2314 first
->AddPreTargetHandler(&dispatch_event
);
2316 // Setup a handler on |second| so that it moves |first| into |second_root|
2317 // when |second| receives an event.
2318 MoveWindowHandler
move_window(first
.get(), second_root
);
2319 second
->AddPreTargetHandler(&move_window
);
2321 // Some sanity checks: |first| is inside |root_window()|'s tree.
2322 EXPECT_EQ(root_window(), first
->GetRootWindow());
2323 // The two root windows are different.
2324 EXPECT_NE(root_window(), second_root
);
2326 // Dispatch an event to |first|.
2327 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, first
->bounds().CenterPoint(),
2328 first
->bounds().CenterPoint(), ui::EF_NONE
, ui::EF_NONE
);
2329 ui::EventDispatchDetails details
=
2330 host()->dispatcher()->OnEventFromSource(&move
);
2331 ASSERT_FALSE(details
.dispatcher_destroyed
);
2332 EXPECT_TRUE(details
.target_destroyed
);
2333 EXPECT_EQ(first
.get(), move
.target());
2334 EXPECT_TRUE(dispatch_event
.dispatched());
2335 EXPECT_EQ(second_root
, first
->GetRootWindow());
2337 first
->RemovePreTargetHandler(&dispatch_event
);
2338 second
->RemovePreTargetHandler(&move_window
);
2341 class AlwaysMouseDownInputStateLookup
: public InputStateLookup
{
2343 AlwaysMouseDownInputStateLookup() {}
2344 ~AlwaysMouseDownInputStateLookup() override
{}
2347 // InputStateLookup:
2348 bool IsMouseButtonDown() const override
{ return true; }
2350 DISALLOW_COPY_AND_ASSIGN(AlwaysMouseDownInputStateLookup
);
2353 TEST_F(WindowEventDispatcherTest
,
2354 CursorVisibilityChangedWhileCaptureWindowInAnotherDispatcher
) {
2355 test::EventCountDelegate delegate
;
2356 scoped_ptr
<Window
> window(CreateTestWindowWithDelegate(&delegate
, 123,
2357 gfx::Rect(20, 10, 10, 20), root_window()));
2360 scoped_ptr
<WindowTreeHost
> second_host(
2361 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2362 second_host
->InitHost();
2363 WindowEventDispatcher
* second_dispatcher
= second_host
->dispatcher();
2365 // Install an InputStateLookup on the Env that always claims that a
2366 // mouse-button is down.
2367 test::EnvTestHelper(Env::GetInstance()).SetInputStateLookup(
2368 scoped_ptr
<InputStateLookup
>(new AlwaysMouseDownInputStateLookup()));
2370 window
->SetCapture();
2372 // Because the mouse button is down, setting the capture on |window| will set
2373 // it as the mouse-move handler for |root_window()|.
2374 EXPECT_EQ(window
.get(), host()->dispatcher()->mouse_moved_handler());
2376 // This does not set |window| as the mouse-move handler for the second
2378 EXPECT_EQ(NULL
, second_dispatcher
->mouse_moved_handler());
2380 // However, some capture-client updates the capture in each root-window on a
2381 // capture. Emulate that here. Because of this, the second dispatcher also has
2382 // |window| as the mouse-move handler.
2383 client::CaptureDelegate
* second_capture_delegate
= second_dispatcher
;
2384 second_capture_delegate
->UpdateCapture(NULL
, window
.get());
2385 EXPECT_EQ(window
.get(), second_dispatcher
->mouse_moved_handler());
2387 // Reset the mouse-event counts for |window|.
2388 delegate
.GetMouseMotionCountsAndReset();
2390 // Notify both hosts that the cursor is now hidden. This should send a single
2391 // mouse-exit event to |window|.
2392 host()->OnCursorVisibilityChanged(false);
2393 second_host
->OnCursorVisibilityChanged(false);
2394 EXPECT_EQ("0 0 1", delegate
.GetMouseMotionCountsAndReset());
2397 TEST_F(WindowEventDispatcherTest
,
2398 RedirectedEventToDifferentDispatcherLocation
) {
2399 scoped_ptr
<WindowTreeHost
> second_host(
2400 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2401 second_host
->InitHost();
2402 client::SetCaptureClient(second_host
->window(),
2403 client::GetCaptureClient(root_window()));
2405 test::EventCountDelegate delegate
;
2406 scoped_ptr
<Window
> window_first(CreateTestWindowWithDelegate(&delegate
, 123,
2407 gfx::Rect(20, 10, 10, 20), root_window()));
2408 window_first
->Show();
2410 scoped_ptr
<Window
> window_second(CreateTestWindowWithDelegate(&delegate
, 12,
2411 gfx::Rect(10, 10, 20, 30), second_host
->window()));
2412 window_second
->Show();
2414 window_second
->SetCapture();
2415 EXPECT_EQ(window_second
.get(),
2416 client::GetCaptureWindow(root_window()));
2418 // Send an event to the first host. Make sure it goes to |window_second| in
2419 // |second_host| instead (since it has capture).
2420 EventFilterRecorder recorder_first
;
2421 window_first
->AddPreTargetHandler(&recorder_first
);
2422 EventFilterRecorder recorder_second
;
2423 window_second
->AddPreTargetHandler(&recorder_second
);
2424 const gfx::Point
event_location(25, 15);
2425 ui::MouseEvent
mouse(ui::ET_MOUSE_PRESSED
, event_location
,
2426 event_location
, ui::EF_LEFT_MOUSE_BUTTON
,
2427 ui::EF_LEFT_MOUSE_BUTTON
);
2428 DispatchEventUsingWindowDispatcher(&mouse
);
2429 EXPECT_TRUE(recorder_first
.events().empty());
2430 ASSERT_EQ(1u, recorder_second
.events().size());
2431 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, recorder_second
.events()[0]);
2432 EXPECT_EQ(event_location
.ToString(),
2433 recorder_second
.mouse_locations()[0].ToString());
2436 class AsyncWindowDelegate
: public test::TestWindowDelegate
{
2438 AsyncWindowDelegate(WindowEventDispatcher
* dispatcher
)
2439 : dispatcher_(dispatcher
) {}
2441 void set_window(Window
* window
) {
2445 void OnTouchEvent(ui::TouchEvent
* event
) override
{
2446 // Convert touch event back to root window coordinates.
2447 event
->ConvertLocationToTarget(window_
, window_
->GetRootWindow());
2448 event
->DisableSynchronousHandling();
2449 dispatcher_
->ProcessedTouchEvent(event
, window_
, ui::ER_UNHANDLED
);
2450 event
->StopPropagation();
2453 WindowEventDispatcher
* dispatcher_
;
2456 DISALLOW_COPY_AND_ASSIGN(AsyncWindowDelegate
);
2459 // Tests that gesture events dispatched through the asynchronous flow have
2460 // co-ordinates in the right co-ordinate space.
2461 TEST_F(WindowEventDispatcherTest
, GestureEventCoordinates
) {
2462 const float kX
= 67.3f
;
2463 const float kY
= 97.8f
;
2465 const int kWindowOffset
= 50;
2466 EventFilterRecorder recorder
;
2467 root_window()->AddPreTargetHandler(&recorder
);
2468 AsyncWindowDelegate
delegate(host()->dispatcher());
2469 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
2470 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2473 gfx::Rect(kWindowOffset
, kWindowOffset
, 100, 100),
2475 window
->AddPreTargetHandler(&handler
);
2477 delegate
.set_window(window
.get());
2479 ui::TouchEvent
touch_pressed_event(
2480 ui::ET_TOUCH_PRESSED
, gfx::PointF(kX
, kY
), 0, ui::EventTimeForNow());
2482 DispatchEventUsingWindowDispatcher(&touch_pressed_event
);
2484 ASSERT_EQ(1u, recorder
.touch_locations().size());
2485 EXPECT_EQ(gfx::Point(kX
- kWindowOffset
, kY
- kWindowOffset
).ToString(),
2486 recorder
.touch_locations()[0].ToString());
2488 ASSERT_EQ(2u, recorder
.gesture_locations().size());
2489 EXPECT_EQ(gfx::Point(kX
- kWindowOffset
, kY
- kWindowOffset
).ToString(),
2490 recorder
.gesture_locations()[0].ToString());
2493 // Tests that a scroll-generating touch-event is marked as such.
2494 TEST_F(WindowEventDispatcherTest
, TouchMovesMarkedWhenCausingScroll
) {
2495 EventFilterRecorder recorder
;
2496 root_window()->AddPreTargetHandler(&recorder
);
2498 const gfx::Point
location(20, 20);
2499 ui::TouchEvent
press(
2500 ui::ET_TOUCH_PRESSED
, location
, 0, ui::EventTimeForNow());
2501 DispatchEventUsingWindowDispatcher(&press
);
2502 EXPECT_FALSE(recorder
.LastTouchMayCauseScrolling());
2503 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_PRESSED
));
2506 ui::TouchEvent
move(ui::ET_TOUCH_MOVED
,
2507 location
+ gfx::Vector2d(100, 100),
2509 ui::EventTimeForNow());
2510 DispatchEventUsingWindowDispatcher(&move
);
2511 EXPECT_TRUE(recorder
.LastTouchMayCauseScrolling());
2512 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
2513 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
2514 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE
));
2517 ui::TouchEvent
move2(ui::ET_TOUCH_MOVED
,
2518 location
+ gfx::Vector2d(200, 200),
2520 ui::EventTimeForNow());
2521 DispatchEventUsingWindowDispatcher(&move2
);
2522 EXPECT_TRUE(recorder
.LastTouchMayCauseScrolling());
2523 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
2524 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE
));
2527 // Delay the release to avoid fling generation.
2528 ui::TouchEvent
release(
2529 ui::ET_TOUCH_RELEASED
,
2530 location
+ gfx::Vector2dF(200, 200),
2532 ui::EventTimeForNow() + base::TimeDelta::FromSeconds(1));
2533 DispatchEventUsingWindowDispatcher(&release
);
2534 EXPECT_FALSE(recorder
.LastTouchMayCauseScrolling());
2535 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_RELEASED
));
2536 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_END
));
2538 root_window()->RemovePreTargetHandler(&recorder
);