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 "testing/gtest/include/gtest/gtest.h"
12 #include "ui/aura/client/capture_client.h"
13 #include "ui/aura/client/event_client.h"
14 #include "ui/aura/client/focus_client.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/test/aura_test_base.h"
17 #include "ui/aura/test/env_test_helper.h"
18 #include "ui/aura/test/test_cursor_client.h"
19 #include "ui/aura/test/test_screen.h"
20 #include "ui/aura/test/test_window_delegate.h"
21 #include "ui/aura/test/test_windows.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_tracker.h"
24 #include "ui/base/hit_test.h"
25 #include "ui/events/event.h"
26 #include "ui/events/event_handler.h"
27 #include "ui/events/event_utils.h"
28 #include "ui/events/gesture_detection/gesture_configuration.h"
29 #include "ui/events/keycodes/keyboard_codes.h"
30 #include "ui/events/test/event_generator.h"
31 #include "ui/events/test/test_event_handler.h"
32 #include "ui/gfx/geometry/point.h"
33 #include "ui/gfx/geometry/rect.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/transform.h"
40 // A delegate that always returns a non-client component for hit tests.
41 class NonClientDelegate
: public test::TestWindowDelegate
{
44 : non_client_count_(0),
45 mouse_event_count_(0),
46 mouse_event_flags_(0x0) {
48 ~NonClientDelegate() override
{}
50 int non_client_count() const { return non_client_count_
; }
51 gfx::Point
non_client_location() const { return non_client_location_
; }
52 int mouse_event_count() const { return mouse_event_count_
; }
53 gfx::Point
mouse_event_location() const { return mouse_event_location_
; }
54 int mouse_event_flags() const { return mouse_event_flags_
; }
56 int GetNonClientComponent(const gfx::Point
& location
) const override
{
57 NonClientDelegate
* self
= const_cast<NonClientDelegate
*>(this);
58 self
->non_client_count_
++;
59 self
->non_client_location_
= location
;
62 void OnMouseEvent(ui::MouseEvent
* event
) override
{
64 mouse_event_location_
= event
->location();
65 mouse_event_flags_
= event
->flags();
70 int non_client_count_
;
71 gfx::Point non_client_location_
;
72 int mouse_event_count_
;
73 gfx::Point mouse_event_location_
;
74 int mouse_event_flags_
;
76 DISALLOW_COPY_AND_ASSIGN(NonClientDelegate
);
79 // A simple event handler that consumes key events.
80 class ConsumeKeyHandler
: public ui::test::TestEventHandler
{
82 ConsumeKeyHandler() {}
83 ~ConsumeKeyHandler() override
{}
85 // Overridden from ui::EventHandler:
86 void OnKeyEvent(ui::KeyEvent
* event
) override
{
87 ui::test::TestEventHandler::OnKeyEvent(event
);
88 event
->StopPropagation();
92 DISALLOW_COPY_AND_ASSIGN(ConsumeKeyHandler
);
95 bool IsFocusedWindow(aura::Window
* window
) {
96 return client::GetFocusClient(window
)->GetFocusedWindow() == window
;
101 typedef test::AuraTestBase WindowEventDispatcherTest
;
103 TEST_F(WindowEventDispatcherTest
, OnHostMouseEvent
) {
104 // Create two non-overlapping windows so we don't have to worry about which
106 scoped_ptr
<NonClientDelegate
> delegate1(new NonClientDelegate());
107 scoped_ptr
<NonClientDelegate
> delegate2(new NonClientDelegate());
108 const int kWindowWidth
= 123;
109 const int kWindowHeight
= 45;
110 gfx::Rect
bounds1(100, 200, kWindowWidth
, kWindowHeight
);
111 gfx::Rect
bounds2(300, 400, kWindowWidth
, kWindowHeight
);
112 scoped_ptr
<aura::Window
> window1(CreateTestWindowWithDelegate(
113 delegate1
.get(), -1234, bounds1
, root_window()));
114 scoped_ptr
<aura::Window
> window2(CreateTestWindowWithDelegate(
115 delegate2
.get(), -5678, bounds2
, root_window()));
117 // Send a mouse event to window1.
118 gfx::Point
point(101, 201);
119 ui::MouseEvent
event1(
120 ui::ET_MOUSE_PRESSED
, point
, point
, ui::EF_LEFT_MOUSE_BUTTON
,
121 ui::EF_LEFT_MOUSE_BUTTON
);
122 DispatchEventUsingWindowDispatcher(&event1
);
124 // Event was tested for non-client area for the target window.
125 EXPECT_EQ(1, delegate1
->non_client_count());
126 EXPECT_EQ(0, delegate2
->non_client_count());
127 // The non-client component test was in local coordinates.
128 EXPECT_EQ(gfx::Point(1, 1), delegate1
->non_client_location());
129 // Mouse event was received by target window.
130 EXPECT_EQ(1, delegate1
->mouse_event_count());
131 EXPECT_EQ(0, delegate2
->mouse_event_count());
132 // Event was in local coordinates.
133 EXPECT_EQ(gfx::Point(1, 1), delegate1
->mouse_event_location());
134 // Non-client flag was set.
135 EXPECT_TRUE(delegate1
->mouse_event_flags() & ui::EF_IS_NON_CLIENT
);
138 TEST_F(WindowEventDispatcherTest
, RepostEvent
) {
139 // Test RepostEvent in RootWindow. It only works for Mouse Press.
140 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
141 gfx::Point
point(10, 10);
142 ui::MouseEvent
event(
143 ui::ET_MOUSE_PRESSED
, point
, point
, ui::EF_LEFT_MOUSE_BUTTON
,
144 ui::EF_LEFT_MOUSE_BUTTON
);
145 host()->dispatcher()->RepostEvent(event
);
146 RunAllPendingInMessageLoop();
147 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
150 // Check that we correctly track the state of the mouse buttons in response to
151 // button press and release events.
152 TEST_F(WindowEventDispatcherTest
, MouseButtonState
) {
153 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
156 scoped_ptr
<ui::MouseEvent
> event
;
158 // Press the left button.
159 event
.reset(new ui::MouseEvent(
160 ui::ET_MOUSE_PRESSED
,
163 ui::EF_LEFT_MOUSE_BUTTON
,
164 ui::EF_LEFT_MOUSE_BUTTON
));
165 DispatchEventUsingWindowDispatcher(event
.get());
166 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
168 // Additionally press the right.
169 event
.reset(new ui::MouseEvent(
170 ui::ET_MOUSE_PRESSED
,
173 ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_RIGHT_MOUSE_BUTTON
,
174 ui::EF_RIGHT_MOUSE_BUTTON
));
175 DispatchEventUsingWindowDispatcher(event
.get());
176 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
178 // Release the left button.
179 event
.reset(new ui::MouseEvent(
180 ui::ET_MOUSE_RELEASED
,
183 ui::EF_RIGHT_MOUSE_BUTTON
,
184 ui::EF_LEFT_MOUSE_BUTTON
));
185 DispatchEventUsingWindowDispatcher(event
.get());
186 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
188 // Release the right button. We should ignore the Shift-is-down flag.
189 event
.reset(new ui::MouseEvent(
190 ui::ET_MOUSE_RELEASED
,
194 ui::EF_RIGHT_MOUSE_BUTTON
));
195 DispatchEventUsingWindowDispatcher(event
.get());
196 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
198 // Press the middle button.
199 event
.reset(new ui::MouseEvent(
200 ui::ET_MOUSE_PRESSED
,
203 ui::EF_MIDDLE_MOUSE_BUTTON
,
204 ui::EF_MIDDLE_MOUSE_BUTTON
));
205 DispatchEventUsingWindowDispatcher(event
.get());
206 EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
209 TEST_F(WindowEventDispatcherTest
, TranslatedEvent
) {
210 scoped_ptr
<Window
> w1(test::CreateTestWindowWithDelegate(NULL
, 1,
211 gfx::Rect(50, 50, 100, 100), root_window()));
213 gfx::Point
origin(100, 100);
214 ui::MouseEvent
root(ui::ET_MOUSE_PRESSED
, origin
, origin
, 0, 0);
216 EXPECT_EQ("100,100", root
.location().ToString());
217 EXPECT_EQ("100,100", root
.root_location().ToString());
219 ui::MouseEvent
translated_event(
220 root
, static_cast<Window
*>(root_window()), w1
.get(),
221 ui::ET_MOUSE_ENTERED
, root
.flags());
222 EXPECT_EQ("50,50", translated_event
.location().ToString());
223 EXPECT_EQ("100,100", translated_event
.root_location().ToString());
228 class TestEventClient
: public client::EventClient
{
230 static const int kNonLockWindowId
= 100;
231 static const int kLockWindowId
= 200;
233 explicit TestEventClient(Window
* root_window
)
234 : root_window_(root_window
),
236 client::SetEventClient(root_window_
, this);
237 Window
* lock_window
=
238 test::CreateTestWindowWithBounds(root_window_
->bounds(), root_window_
);
239 lock_window
->set_id(kLockWindowId
);
240 Window
* non_lock_window
=
241 test::CreateTestWindowWithBounds(root_window_
->bounds(), root_window_
);
242 non_lock_window
->set_id(kNonLockWindowId
);
244 ~TestEventClient() override
{ client::SetEventClient(root_window_
, NULL
); }
246 // Starts/stops locking. Locking prevents windows other than those inside
247 // the lock container from receiving events, getting focus etc.
255 Window
* GetLockWindow() {
256 return const_cast<Window
*>(
257 static_cast<const TestEventClient
*>(this)->GetLockWindow());
259 const Window
* GetLockWindow() const {
260 return root_window_
->GetChildById(kLockWindowId
);
262 Window
* GetNonLockWindow() {
263 return root_window_
->GetChildById(kNonLockWindowId
);
267 // Overridden from client::EventClient:
268 bool CanProcessEventsWithinSubtree(const Window
* window
) const override
{
270 window
->Contains(GetLockWindow()) || GetLockWindow()->Contains(window
) :
274 ui::EventTarget
* GetToplevelEventTarget() override
{ return NULL
; }
276 Window
* root_window_
;
279 DISALLOW_COPY_AND_ASSIGN(TestEventClient
);
284 TEST_F(WindowEventDispatcherTest
, CanProcessEventsWithinSubtree
) {
285 TestEventClient
client(root_window());
286 test::TestWindowDelegate d
;
288 ui::test::TestEventHandler nonlock_ef
;
289 ui::test::TestEventHandler lock_ef
;
290 client
.GetNonLockWindow()->AddPreTargetHandler(&nonlock_ef
);
291 client
.GetLockWindow()->AddPreTargetHandler(&lock_ef
);
293 Window
* w1
= test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
294 client
.GetNonLockWindow());
296 Window
* w2
= test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
297 client
.GetNonLockWindow());
299 scoped_ptr
<Window
> w3(
300 test::CreateTestWindowWithDelegate(&d
, 3, gfx::Rect(30, 30, 20, 20),
301 client
.GetLockWindow()));
304 EXPECT_TRUE(IsFocusedWindow(w1
));
308 // Since we're locked, the attempt to focus w2 will be ignored.
310 EXPECT_TRUE(IsFocusedWindow(w1
));
311 EXPECT_FALSE(IsFocusedWindow(w2
));
314 // Attempting to send a key event to w1 (not in the lock container) should
315 // cause focus to be reset.
316 ui::test::EventGenerator
generator(root_window());
317 generator
.PressKey(ui::VKEY_SPACE
, 0);
318 EXPECT_EQ(NULL
, client::GetFocusClient(w1
)->GetFocusedWindow());
319 EXPECT_FALSE(IsFocusedWindow(w1
));
323 // Events sent to a window not in the lock container will not be processed.
324 // i.e. never sent to the non-lock container's event filter.
325 ui::test::EventGenerator
generator(root_window(), w1
);
326 generator
.ClickLeftButton();
327 EXPECT_EQ(0, nonlock_ef
.num_mouse_events());
329 // Events sent to a window in the lock container will be processed.
330 ui::test::EventGenerator
generator3(root_window(), w3
.get());
331 generator3
.PressLeftButton();
332 EXPECT_EQ(1, lock_ef
.num_mouse_events());
335 // Prevent w3 from being deleted by the hierarchy since its delegate is owned
337 w3
->parent()->RemoveChild(w3
.get());
340 TEST_F(WindowEventDispatcherTest
, DontIgnoreUnknownKeys
) {
341 ConsumeKeyHandler handler
;
342 root_window()->AddPreTargetHandler(&handler
);
344 ui::KeyEvent
unknown_event(ui::ET_KEY_PRESSED
, ui::VKEY_UNKNOWN
, ui::EF_NONE
);
345 DispatchEventUsingWindowDispatcher(&unknown_event
);
346 EXPECT_TRUE(unknown_event
.handled());
347 EXPECT_EQ(1, handler
.num_key_events());
350 ui::KeyEvent
known_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
351 DispatchEventUsingWindowDispatcher(&known_event
);
352 EXPECT_TRUE(known_event
.handled());
353 EXPECT_EQ(1, handler
.num_key_events());
356 ui::KeyEvent
ime_event(ui::ET_KEY_PRESSED
, ui::VKEY_UNKNOWN
,
357 ui::EF_IME_FABRICATED_KEY
);
358 DispatchEventUsingWindowDispatcher(&ime_event
);
359 EXPECT_TRUE(ime_event
.handled());
360 EXPECT_EQ(1, handler
.num_key_events());
363 ui::KeyEvent
unknown_key_with_char_event(ui::ET_KEY_PRESSED
, ui::VKEY_UNKNOWN
,
365 unknown_key_with_char_event
.set_character(0x00e4 /* "ä" */);
366 DispatchEventUsingWindowDispatcher(&unknown_key_with_char_event
);
367 EXPECT_TRUE(unknown_key_with_char_event
.handled());
368 EXPECT_EQ(1, handler
.num_key_events());
371 TEST_F(WindowEventDispatcherTest
, NoDelegateWindowReceivesKeyEvents
) {
372 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
376 ui::test::TestEventHandler handler
;
377 w1
->AddPreTargetHandler(&handler
);
378 ui::KeyEvent
key_press(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
379 DispatchEventUsingWindowDispatcher(&key_press
);
380 EXPECT_TRUE(key_press
.handled());
381 EXPECT_EQ(1, handler
.num_key_events());
383 w1
->RemovePreTargetHandler(&handler
);
386 // Tests that touch-events that are beyond the bounds of the root-window do get
387 // propagated to the event filters correctly with the root as the target.
388 TEST_F(WindowEventDispatcherTest
, TouchEventsOutsideBounds
) {
389 ui::test::TestEventHandler handler
;
390 root_window()->AddPreTargetHandler(&handler
);
392 gfx::Point position
= root_window()->bounds().origin();
393 position
.Offset(-10, -10);
394 ui::TouchEvent
press(
395 ui::ET_TOUCH_PRESSED
, position
, 0, ui::EventTimeForNow());
396 DispatchEventUsingWindowDispatcher(&press
);
397 EXPECT_EQ(1, handler
.num_touch_events());
399 position
= root_window()->bounds().origin();
400 position
.Offset(root_window()->bounds().width() + 10,
401 root_window()->bounds().height() + 10);
402 ui::TouchEvent
release(
403 ui::ET_TOUCH_RELEASED
, position
, 0, ui::EventTimeForNow());
404 DispatchEventUsingWindowDispatcher(&release
);
405 EXPECT_EQ(2, handler
.num_touch_events());
408 // Tests that scroll events are dispatched correctly.
409 TEST_F(WindowEventDispatcherTest
, ScrollEventDispatch
) {
410 base::TimeDelta now
= ui::EventTimeForNow();
411 ui::test::TestEventHandler handler
;
412 root_window()->AddPreTargetHandler(&handler
);
414 test::TestWindowDelegate delegate
;
415 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &delegate
));
416 w1
->SetBounds(gfx::Rect(20, 20, 40, 40));
418 // A scroll event on the root-window itself is dispatched.
419 ui::ScrollEvent
scroll1(ui::ET_SCROLL
,
426 DispatchEventUsingWindowDispatcher(&scroll1
);
427 EXPECT_EQ(1, handler
.num_scroll_events());
429 // Scroll event on a window should be dispatched properly.
430 ui::ScrollEvent
scroll2(ui::ET_SCROLL
,
437 DispatchEventUsingWindowDispatcher(&scroll2
);
438 EXPECT_EQ(2, handler
.num_scroll_events());
439 root_window()->RemovePreTargetHandler(&handler
);
444 // FilterFilter that tracks the types of events it's seen.
445 class EventFilterRecorder
: public ui::EventHandler
{
447 typedef std::vector
<ui::EventType
> Events
;
448 typedef std::vector
<gfx::Point
> EventLocations
;
449 typedef std::vector
<int> EventFlags
;
451 EventFilterRecorder()
452 : wait_until_event_(ui::ET_UNKNOWN
),
453 last_touch_may_cause_scrolling_(false) {
456 const Events
& events() const { return events_
; }
458 const EventLocations
& mouse_locations() const { return mouse_locations_
; }
459 gfx::Point
mouse_location(int i
) const { return mouse_locations_
[i
]; }
460 const EventLocations
& touch_locations() const { return touch_locations_
; }
461 const EventLocations
& gesture_locations() const { return gesture_locations_
; }
462 const EventFlags
& mouse_event_flags() const { return mouse_event_flags_
; }
464 void WaitUntilReceivedEvent(ui::EventType type
) {
465 wait_until_event_
= type
;
466 run_loop_
.reset(new base::RunLoop());
470 Events
GetAndResetEvents() {
471 Events events
= events_
;
478 mouse_locations_
.clear();
479 touch_locations_
.clear();
480 gesture_locations_
.clear();
481 mouse_event_flags_
.clear();
482 last_touch_may_cause_scrolling_
= false;
485 // ui::EventHandler overrides:
486 void OnEvent(ui::Event
* event
) override
{
487 ui::EventHandler::OnEvent(event
);
488 events_
.push_back(event
->type());
489 if (wait_until_event_
== event
->type() && run_loop_
) {
491 wait_until_event_
= ui::ET_UNKNOWN
;
495 void OnMouseEvent(ui::MouseEvent
* event
) override
{
496 mouse_locations_
.push_back(event
->location());
497 mouse_event_flags_
.push_back(event
->flags());
500 void OnTouchEvent(ui::TouchEvent
* event
) override
{
501 touch_locations_
.push_back(event
->location());
502 last_touch_may_cause_scrolling_
= event
->may_cause_scrolling();
505 void OnGestureEvent(ui::GestureEvent
* event
) override
{
506 gesture_locations_
.push_back(event
->location());
509 bool HasReceivedEvent(ui::EventType type
) {
510 return std::find(events_
.begin(), events_
.end(), type
) != events_
.end();
513 bool LastTouchMayCauseScrolling() const {
514 return last_touch_may_cause_scrolling_
;
518 scoped_ptr
<base::RunLoop
> run_loop_
;
519 ui::EventType wait_until_event_
;
522 EventLocations mouse_locations_
;
523 EventLocations touch_locations_
;
524 EventLocations gesture_locations_
;
525 EventFlags mouse_event_flags_
;
526 bool last_touch_may_cause_scrolling_
;
528 DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder
);
531 // Converts an EventType to a string.
532 std::string
EventTypeToString(ui::EventType type
) {
534 case ui::ET_TOUCH_RELEASED
:
535 return "TOUCH_RELEASED";
537 case ui::ET_TOUCH_CANCELLED
:
538 return "TOUCH_CANCELLED";
540 case ui::ET_TOUCH_PRESSED
:
541 return "TOUCH_PRESSED";
543 case ui::ET_TOUCH_MOVED
:
544 return "TOUCH_MOVED";
546 case ui::ET_MOUSE_PRESSED
:
547 return "MOUSE_PRESSED";
549 case ui::ET_MOUSE_DRAGGED
:
550 return "MOUSE_DRAGGED";
552 case ui::ET_MOUSE_RELEASED
:
553 return "MOUSE_RELEASED";
555 case ui::ET_MOUSE_MOVED
:
556 return "MOUSE_MOVED";
558 case ui::ET_MOUSE_ENTERED
:
559 return "MOUSE_ENTERED";
561 case ui::ET_MOUSE_EXITED
:
562 return "MOUSE_EXITED";
564 case ui::ET_GESTURE_SCROLL_BEGIN
:
565 return "GESTURE_SCROLL_BEGIN";
567 case ui::ET_GESTURE_SCROLL_END
:
568 return "GESTURE_SCROLL_END";
570 case ui::ET_GESTURE_SCROLL_UPDATE
:
571 return "GESTURE_SCROLL_UPDATE";
573 case ui::ET_GESTURE_PINCH_BEGIN
:
574 return "GESTURE_PINCH_BEGIN";
576 case ui::ET_GESTURE_PINCH_END
:
577 return "GESTURE_PINCH_END";
579 case ui::ET_GESTURE_PINCH_UPDATE
:
580 return "GESTURE_PINCH_UPDATE";
582 case ui::ET_GESTURE_TAP
:
583 return "GESTURE_TAP";
585 case ui::ET_GESTURE_TAP_DOWN
:
586 return "GESTURE_TAP_DOWN";
588 case ui::ET_GESTURE_TAP_CANCEL
:
589 return "GESTURE_TAP_CANCEL";
591 case ui::ET_GESTURE_SHOW_PRESS
:
592 return "GESTURE_SHOW_PRESS";
594 case ui::ET_GESTURE_BEGIN
:
595 return "GESTURE_BEGIN";
597 case ui::ET_GESTURE_END
:
598 return "GESTURE_END";
601 // We should explicitly require each event type.
602 NOTREACHED() << "Received unexpected event: " << type
;
608 std::string
EventTypesToString(const EventFilterRecorder::Events
& events
) {
610 for (size_t i
= 0; i
< events
.size(); ++i
) {
613 result
+= EventTypeToString(events
[i
]);
620 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
621 #define MAYBE(x) DISABLED_##x
626 // Verifies a repost mouse event targets the window with capture (if there is
628 // Flaky on 32-bit Windows bots. http://crbug.com/388290
629 TEST_F(WindowEventDispatcherTest
, MAYBE(RepostTargetsCaptureWindow
)) {
630 // Set capture on |window| generate a mouse event (that is reposted) and not
631 // over |window| and verify |window| gets it (|window| gets it because it has
633 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
634 EventFilterRecorder recorder
;
635 scoped_ptr
<Window
> window(CreateNormalWindow(1, root_window(), NULL
));
636 window
->SetBounds(gfx::Rect(20, 20, 40, 30));
637 window
->AddPreTargetHandler(&recorder
);
638 window
->SetCapture();
639 const ui::MouseEvent
press_event(
640 ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
641 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
642 host()->dispatcher()->RepostEvent(press_event
);
643 RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
644 // Mouse moves/enters may be generated. We only care about a pressed.
645 EXPECT_TRUE(EventTypesToString(recorder
.events()).find("MOUSE_PRESSED") !=
646 std::string::npos
) << EventTypesToString(recorder
.events());
649 TEST_F(WindowEventDispatcherTest
, MouseMovesHeld
) {
650 EventFilterRecorder recorder
;
651 root_window()->AddPreTargetHandler(&recorder
);
653 test::TestWindowDelegate delegate
;
654 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
655 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
657 ui::MouseEvent
mouse_move_event(ui::ET_MOUSE_MOVED
, gfx::Point(0, 0),
658 gfx::Point(0, 0), 0, 0);
659 DispatchEventUsingWindowDispatcher(&mouse_move_event
);
660 // Discard MOUSE_ENTER.
663 host()->dispatcher()->HoldPointerMoves();
665 // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
666 ui::MouseEvent
mouse_dragged_event(ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0),
667 gfx::Point(0, 0), 0, 0);
668 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
669 EXPECT_TRUE(recorder
.events().empty());
671 // Check that we do dispatch the held MOUSE_DRAGGED event before another type
673 ui::MouseEvent
mouse_pressed_event(ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0),
674 gfx::Point(0, 0), 0, 0);
675 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
676 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
677 EventTypesToString(recorder
.events()));
680 // Check that we coalesce held MOUSE_DRAGGED events. Note that here (and
681 // elsewhere in this test) we re-define each event prior to dispatch so that
682 // it has the correct state (phase, handled, target, etc.).
683 mouse_dragged_event
= ui::MouseEvent(
684 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
685 ui::MouseEvent
mouse_dragged_event2(ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10),
686 gfx::Point(10, 10), 0, 0);
687 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
688 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
689 EXPECT_TRUE(recorder
.events().empty());
690 mouse_pressed_event
= ui::MouseEvent(
691 ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
692 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
693 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
694 EventTypesToString(recorder
.events()));
697 // Check that on ReleasePointerMoves, held events are not dispatched
698 // immediately, but posted instead.
699 mouse_dragged_event
= ui::MouseEvent(
700 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
701 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
702 host()->dispatcher()->ReleasePointerMoves();
703 EXPECT_TRUE(recorder
.events().empty());
704 RunAllPendingInMessageLoop();
705 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder
.events()));
708 // However if another message comes in before the dispatch of the posted
709 // event, check that the posted event is dispatched before this new event.
710 host()->dispatcher()->HoldPointerMoves();
711 mouse_dragged_event
= ui::MouseEvent(
712 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
713 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
714 host()->dispatcher()->ReleasePointerMoves();
715 mouse_pressed_event
= ui::MouseEvent(
716 ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
717 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
718 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
719 EventTypesToString(recorder
.events()));
721 RunAllPendingInMessageLoop();
722 EXPECT_TRUE(recorder
.events().empty());
724 // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
726 host()->dispatcher()->HoldPointerMoves();
727 mouse_dragged_event
= ui::MouseEvent(
728 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
729 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
730 host()->dispatcher()->ReleasePointerMoves();
731 mouse_dragged_event2
= ui::MouseEvent(
732 ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
733 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
734 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder
.events()));
736 RunAllPendingInMessageLoop();
737 EXPECT_TRUE(recorder
.events().empty());
739 // Check that synthetic mouse move event has a right location when issued
740 // while holding pointer moves.
741 mouse_dragged_event
= ui::MouseEvent(
742 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
743 mouse_dragged_event2
= ui::MouseEvent(
744 ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
745 ui::MouseEvent
mouse_dragged_event3(ui::ET_MOUSE_DRAGGED
, gfx::Point(28, 28),
746 gfx::Point(28, 28), 0, 0);
747 host()->dispatcher()->HoldPointerMoves();
748 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
749 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
750 window
->SetBounds(gfx::Rect(15, 15, 80, 80));
751 DispatchEventUsingWindowDispatcher(&mouse_dragged_event3
);
752 RunAllPendingInMessageLoop();
753 EXPECT_TRUE(recorder
.events().empty());
754 host()->dispatcher()->ReleasePointerMoves();
755 RunAllPendingInMessageLoop();
756 EXPECT_EQ("MOUSE_MOVED", EventTypesToString(recorder
.events()));
757 EXPECT_EQ(gfx::Point(13, 13), recorder
.mouse_location(0));
759 root_window()->RemovePreTargetHandler(&recorder
);
762 TEST_F(WindowEventDispatcherTest
, TouchMovesHeld
) {
763 EventFilterRecorder recorder
;
764 root_window()->AddPreTargetHandler(&recorder
);
766 test::TestWindowDelegate delegate
;
767 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
768 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
770 // Starting the touch and throwing out the first few events, since the system
771 // is going to generate synthetic mouse events that are not relevant to the
773 ui::TouchEvent
touch_pressed_event(
774 ui::ET_TOUCH_PRESSED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
775 DispatchEventUsingWindowDispatcher(&touch_pressed_event
);
776 recorder
.WaitUntilReceivedEvent(ui::ET_GESTURE_SHOW_PRESS
);
779 host()->dispatcher()->HoldPointerMoves();
781 // Check that we don't immediately dispatch the TOUCH_MOVED event.
782 ui::TouchEvent
touch_moved_event(
783 ui::ET_TOUCH_MOVED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
784 ui::TouchEvent
touch_moved_event2(
785 ui::ET_TOUCH_MOVED
, gfx::Point(11, 10), 0, ui::EventTimeForNow());
786 ui::TouchEvent
touch_moved_event3(
787 ui::ET_TOUCH_MOVED
, gfx::Point(12, 10), 0, ui::EventTimeForNow());
789 DispatchEventUsingWindowDispatcher(&touch_moved_event
);
790 EXPECT_TRUE(recorder
.events().empty());
792 // Check that on ReleasePointerMoves, held events are not dispatched
793 // immediately, but posted instead.
794 DispatchEventUsingWindowDispatcher(&touch_moved_event2
);
795 host()->dispatcher()->ReleasePointerMoves();
796 EXPECT_TRUE(recorder
.events().empty());
798 RunAllPendingInMessageLoop();
799 EXPECT_EQ("TOUCH_MOVED", EventTypesToString(recorder
.events()));
802 // If another touch event occurs then the held touch should be dispatched
803 // immediately before it.
804 ui::TouchEvent
touch_released_event(
805 ui::ET_TOUCH_RELEASED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
807 host()->dispatcher()->HoldPointerMoves();
808 DispatchEventUsingWindowDispatcher(&touch_moved_event3
);
809 DispatchEventUsingWindowDispatcher(&touch_released_event
);
810 EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP GESTURE_END",
811 EventTypesToString(recorder
.events()));
813 host()->dispatcher()->ReleasePointerMoves();
814 RunAllPendingInMessageLoop();
815 EXPECT_TRUE(recorder
.events().empty());
818 // Tests that mouse move event has a right location
819 // when there isn't the target window
820 TEST_F(WindowEventDispatcherTest
, MouseEventWithoutTargetWindow
) {
821 EventFilterRecorder recorder_first
;
822 EventFilterRecorder recorder_second
;
824 test::TestWindowDelegate delegate
;
825 scoped_ptr
<aura::Window
> window_first(CreateTestWindowWithDelegate(
826 &delegate
, 1, gfx::Rect(20, 10, 10, 20), root_window()));
827 window_first
->Show();
828 window_first
->AddPreTargetHandler(&recorder_first
);
830 scoped_ptr
<aura::Window
> window_second(CreateTestWindowWithDelegate(
831 &delegate
, 2, gfx::Rect(20, 30, 10, 20), root_window()));
832 window_second
->Show();
833 window_second
->AddPreTargetHandler(&recorder_second
);
835 const gfx::Point
event_location(22, 33);
836 ui::MouseEvent
mouse(ui::ET_MOUSE_MOVED
, event_location
, event_location
, 0,
838 DispatchEventUsingWindowDispatcher(&mouse
);
840 EXPECT_TRUE(recorder_first
.events().empty());
841 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED",
842 EventTypesToString(recorder_second
.events()));
843 ASSERT_EQ(2u, recorder_second
.mouse_locations().size());
844 EXPECT_EQ(gfx::Point(2, 3).ToString(),
845 recorder_second
.mouse_locations()[0].ToString());
848 // Verifies that a direct call to ProcessedTouchEvent() with a
849 // TOUCH_PRESSED event does not cause a crash.
850 TEST_F(WindowEventDispatcherTest
, CallToProcessedTouchEvent
) {
851 test::TestWindowDelegate delegate
;
852 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
853 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
855 ui::TouchEvent
touch(
856 ui::ET_TOUCH_PRESSED
, gfx::Point(10, 10), 1, ui::EventTimeForNow());
857 host()->dispatcher()->ProcessedTouchEvent(
858 &touch
, window
.get(), ui::ER_UNHANDLED
);
861 // This event handler requests the dispatcher to start holding pointer-move
862 // events when it receives the first scroll-update gesture.
863 class HoldPointerOnScrollHandler
: public ui::test::TestEventHandler
{
865 HoldPointerOnScrollHandler(WindowEventDispatcher
* dispatcher
,
866 EventFilterRecorder
* filter
)
867 : dispatcher_(dispatcher
),
869 holding_moves_(false) {}
870 ~HoldPointerOnScrollHandler() override
{}
873 // ui::test::TestEventHandler:
874 void OnGestureEvent(ui::GestureEvent
* gesture
) override
{
875 if (!holding_moves_
&& gesture
->type() == ui::ET_GESTURE_SCROLL_UPDATE
) {
876 holding_moves_
= true;
877 dispatcher_
->HoldPointerMoves();
879 } else if (gesture
->type() == ui::ET_GESTURE_SCROLL_END
) {
880 dispatcher_
->ReleasePointerMoves();
881 holding_moves_
= false;
885 WindowEventDispatcher
* dispatcher_
;
886 EventFilterRecorder
* filter_
;
889 DISALLOW_COPY_AND_ASSIGN(HoldPointerOnScrollHandler
);
892 // Tests that touch-move events don't contribute to an in-progress scroll
893 // gesture if touch-move events are being held by the dispatcher.
894 TEST_F(WindowEventDispatcherTest
, TouchMovesHeldOnScroll
) {
895 EventFilterRecorder recorder
;
896 root_window()->AddPreTargetHandler(&recorder
);
897 test::TestWindowDelegate delegate
;
898 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
899 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
900 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
901 window
->AddPreTargetHandler(&handler
);
903 ui::test::EventGenerator
generator(root_window());
904 generator
.GestureScrollSequence(
905 gfx::Point(60, 60), gfx::Point(10, 60),
906 base::TimeDelta::FromMilliseconds(100), 25);
908 // |handler| will have reset |filter| and started holding the touch-move
909 // events when scrolling started. At the end of the scroll (i.e. upon
910 // touch-release), the held touch-move event will have been dispatched first,
911 // along with the subsequent events (i.e. touch-release, scroll-end, and
913 const EventFilterRecorder::Events
& events
= recorder
.events();
915 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
916 "GESTURE_SCROLL_END GESTURE_END",
917 EventTypesToString(events
));
918 ASSERT_EQ(2u, recorder
.touch_locations().size());
919 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
920 recorder
.touch_locations()[0].ToString());
921 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
922 recorder
.touch_locations()[1].ToString());
925 // Tests that a 'held' touch-event does contribute to gesture event when it is
927 TEST_F(WindowEventDispatcherTest
, HeldTouchMoveContributesToGesture
) {
928 EventFilterRecorder recorder
;
929 root_window()->AddPreTargetHandler(&recorder
);
931 const gfx::Point
location(20, 20);
932 ui::TouchEvent
press(
933 ui::ET_TOUCH_PRESSED
, location
, 0, ui::EventTimeForNow());
934 DispatchEventUsingWindowDispatcher(&press
);
935 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_PRESSED
));
938 host()->dispatcher()->HoldPointerMoves();
940 ui::TouchEvent
move(ui::ET_TOUCH_MOVED
,
941 location
+ gfx::Vector2d(100, 100),
943 ui::EventTimeForNow());
944 DispatchEventUsingWindowDispatcher(&move
);
945 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
946 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
949 host()->dispatcher()->ReleasePointerMoves();
950 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
951 RunAllPendingInMessageLoop();
952 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
953 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
954 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE
));
956 root_window()->RemovePreTargetHandler(&recorder
);
959 // Tests that synthetic mouse events are ignored when mouse
960 // events are disabled.
961 TEST_F(WindowEventDispatcherTest
, DispatchSyntheticMouseEvents
) {
962 EventFilterRecorder recorder
;
963 root_window()->AddPreTargetHandler(&recorder
);
965 test::TestWindowDelegate delegate
;
966 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
967 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
969 window
->SetCapture();
971 test::TestCursorClient
cursor_client(root_window());
973 // Dispatch a non-synthetic mouse event when mouse events are enabled.
974 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
975 gfx::Point(10, 10), 0, 0);
976 DispatchEventUsingWindowDispatcher(&mouse1
);
977 EXPECT_FALSE(recorder
.events().empty());
980 // Dispatch a synthetic mouse event when mouse events are enabled.
981 ui::MouseEvent
mouse2(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
982 gfx::Point(10, 10), ui::EF_IS_SYNTHESIZED
, 0);
983 DispatchEventUsingWindowDispatcher(&mouse2
);
984 EXPECT_FALSE(recorder
.events().empty());
987 // Dispatch a synthetic mouse event when mouse events are disabled.
988 cursor_client
.DisableMouseEvents();
989 DispatchEventUsingWindowDispatcher(&mouse2
);
990 EXPECT_TRUE(recorder
.events().empty());
991 root_window()->RemovePreTargetHandler(&recorder
);
994 // Tests that a mouse-move event is not synthesized when a mouse-button is down.
995 TEST_F(WindowEventDispatcherTest
, DoNotSynthesizeWhileButtonDown
) {
996 EventFilterRecorder recorder
;
997 test::TestWindowDelegate delegate
;
998 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
999 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1002 window
->AddPreTargetHandler(&recorder
);
1003 // Dispatch a non-synthetic mouse event when mouse events are enabled.
1004 ui::MouseEvent
mouse1(ui::ET_MOUSE_PRESSED
, gfx::Point(10, 10),
1005 gfx::Point(10, 10), ui::EF_LEFT_MOUSE_BUTTON
,
1006 ui::EF_LEFT_MOUSE_BUTTON
);
1007 DispatchEventUsingWindowDispatcher(&mouse1
);
1008 ASSERT_EQ(1u, recorder
.events().size());
1009 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, recorder
.events()[0]);
1010 window
->RemovePreTargetHandler(&recorder
);
1013 // Move |window| away from underneath the cursor.
1014 root_window()->AddPreTargetHandler(&recorder
);
1015 window
->SetBounds(gfx::Rect(30, 30, 100, 100));
1016 EXPECT_TRUE(recorder
.events().empty());
1017 RunAllPendingInMessageLoop();
1018 EXPECT_TRUE(recorder
.events().empty());
1019 root_window()->RemovePreTargetHandler(&recorder
);
1022 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
1023 #define MAYBE(x) DISABLED_##x
1028 // Tests synthetic mouse events generated when window bounds changes such that
1029 // the cursor previously outside the window becomes inside, or vice versa.
1030 // Do not synthesize events if the window ignores events or is invisible.
1031 // Flaky on 32-bit Windows bots. http://crbug.com/388272
1032 TEST_F(WindowEventDispatcherTest
,
1033 MAYBE(SynthesizeMouseEventsOnWindowBoundsChanged
)) {
1034 test::TestWindowDelegate delegate
;
1035 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1036 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1038 window
->SetCapture();
1040 EventFilterRecorder recorder
;
1041 window
->AddPreTargetHandler(&recorder
);
1043 // Dispatch a non-synthetic mouse event to place cursor inside window bounds.
1044 ui::MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
1045 gfx::Point(10, 10), 0, 0);
1046 DispatchEventUsingWindowDispatcher(&mouse
);
1047 EXPECT_FALSE(recorder
.events().empty());
1050 // Update the window bounds so that cursor is now outside the window.
1051 // This should trigger a synthetic MOVED event.
1052 gfx::Rect
bounds1(20, 20, 100, 100);
1053 window
->SetBounds(bounds1
);
1054 RunAllPendingInMessageLoop();
1055 ASSERT_FALSE(recorder
.events().empty());
1056 ASSERT_FALSE(recorder
.mouse_event_flags().empty());
1057 EXPECT_EQ(ui::ET_MOUSE_MOVED
, recorder
.events().back());
1058 EXPECT_EQ(ui::EF_IS_SYNTHESIZED
, recorder
.mouse_event_flags().back());
1061 // Set window to ignore events.
1062 window
->set_ignore_events(true);
1064 // Update the window bounds so that cursor is back inside the window.
1065 // This should not trigger a synthetic event.
1066 gfx::Rect
bounds2(5, 5, 100, 100);
1067 window
->SetBounds(bounds2
);
1068 RunAllPendingInMessageLoop();
1069 EXPECT_TRUE(recorder
.events().empty());
1072 // Set window to accept events but invisible.
1073 window
->set_ignore_events(false);
1077 // Update the window bounds so that cursor is outside the window.
1078 // This should not trigger a synthetic event.
1079 window
->SetBounds(bounds1
);
1080 RunAllPendingInMessageLoop();
1081 EXPECT_TRUE(recorder
.events().empty());
1084 // Tests that a mouse exit is dispatched to the last known cursor location
1085 // when the cursor becomes invisible.
1086 TEST_F(WindowEventDispatcherTest
, DispatchMouseExitWhenCursorHidden
) {
1087 EventFilterRecorder recorder
;
1088 root_window()->AddPreTargetHandler(&recorder
);
1090 test::TestWindowDelegate delegate
;
1091 gfx::Point
window_origin(7, 18);
1092 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1093 &delegate
, 1234, gfx::Rect(window_origin
, gfx::Size(100, 100)),
1097 // Dispatch a mouse move event into the window.
1098 gfx::Point
mouse_location(gfx::Point(15, 25));
1099 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, mouse_location
,
1100 mouse_location
, 0, 0);
1101 EXPECT_TRUE(recorder
.events().empty());
1102 DispatchEventUsingWindowDispatcher(&mouse1
);
1103 EXPECT_FALSE(recorder
.events().empty());
1106 // Hide the cursor and verify a mouse exit was dispatched.
1107 host()->OnCursorVisibilityChanged(false);
1108 EXPECT_FALSE(recorder
.events().empty());
1109 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder
.events()));
1111 // Verify the mouse exit was dispatched at the correct location
1112 // (in the correct coordinate space).
1113 int translated_x
= mouse_location
.x() - window_origin
.x();
1114 int translated_y
= mouse_location
.y() - window_origin
.y();
1115 gfx::Point
translated_point(translated_x
, translated_y
);
1116 EXPECT_EQ(recorder
.mouse_location(0).ToString(), translated_point
.ToString());
1117 root_window()->RemovePreTargetHandler(&recorder
);
1120 // Tests that a synthetic mouse exit is dispatched to the last known cursor
1121 // location after mouse events are disabled on the cursor client.
1122 TEST_F(WindowEventDispatcherTest
,
1123 DispatchSyntheticMouseExitAfterMouseEventsDisabled
) {
1124 EventFilterRecorder recorder
;
1125 root_window()->AddPreTargetHandler(&recorder
);
1127 test::TestWindowDelegate delegate
;
1128 gfx::Point
window_origin(7, 18);
1129 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1130 &delegate
, 1234, gfx::Rect(window_origin
, gfx::Size(100, 100)),
1134 // Dispatch a mouse move event into the window.
1135 gfx::Point
mouse_location(gfx::Point(15, 25));
1136 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, mouse_location
,
1137 mouse_location
, 0, 0);
1138 EXPECT_TRUE(recorder
.events().empty());
1139 DispatchEventUsingWindowDispatcher(&mouse1
);
1140 EXPECT_FALSE(recorder
.events().empty());
1143 test::TestCursorClient
cursor_client(root_window());
1144 cursor_client
.DisableMouseEvents();
1146 gfx::Point
mouse_exit_location(gfx::Point(150, 150));
1147 ui::MouseEvent
mouse2(ui::ET_MOUSE_EXITED
, gfx::Point(150, 150),
1148 gfx::Point(150, 150), ui::EF_IS_SYNTHESIZED
, 0);
1149 DispatchEventUsingWindowDispatcher(&mouse2
);
1151 EXPECT_FALSE(recorder
.events().empty());
1152 // We get the mouse exited event twice in our filter. Once during the
1153 // predispatch phase and during the actual dispatch.
1154 EXPECT_EQ("MOUSE_EXITED MOUSE_EXITED", EventTypesToString(recorder
.events()));
1156 // Verify the mouse exit was dispatched at the correct location
1157 // (in the correct coordinate space).
1158 int translated_x
= mouse_exit_location
.x() - window_origin
.x();
1159 int translated_y
= mouse_exit_location
.y() - window_origin
.y();
1160 gfx::Point
translated_point(translated_x
, translated_y
);
1161 EXPECT_EQ(recorder
.mouse_location(0).ToString(), translated_point
.ToString());
1162 root_window()->RemovePreTargetHandler(&recorder
);
1165 class DeletingEventFilter
: public ui::EventHandler
{
1167 DeletingEventFilter()
1168 : delete_during_pre_handle_(false) {}
1169 ~DeletingEventFilter() override
{}
1171 void Reset(bool delete_during_pre_handle
) {
1172 delete_during_pre_handle_
= delete_during_pre_handle
;
1176 // Overridden from ui::EventHandler:
1177 void OnKeyEvent(ui::KeyEvent
* event
) override
{
1178 if (delete_during_pre_handle_
)
1179 delete event
->target();
1182 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1183 if (delete_during_pre_handle_
)
1184 delete event
->target();
1187 bool delete_during_pre_handle_
;
1189 DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter
);
1192 class DeletingWindowDelegate
: public test::TestWindowDelegate
{
1194 DeletingWindowDelegate()
1196 delete_during_handle_(false),
1197 got_event_(false) {}
1198 ~DeletingWindowDelegate() override
{}
1200 void Reset(Window
* window
, bool delete_during_handle
) {
1202 delete_during_handle_
= delete_during_handle
;
1205 bool got_event() const { return got_event_
; }
1208 // Overridden from WindowDelegate:
1209 void OnKeyEvent(ui::KeyEvent
* event
) override
{
1210 if (delete_during_handle_
)
1215 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1216 if (delete_during_handle_
)
1222 bool delete_during_handle_
;
1225 DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate
);
1228 TEST_F(WindowEventDispatcherTest
, DeleteWindowDuringDispatch
) {
1229 // Verifies that we can delete a window during each phase of event handling.
1230 // Deleting the window should not cause a crash, only prevent further
1231 // processing from occurring.
1232 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1233 DeletingWindowDelegate d11
;
1234 Window
* w11
= CreateNormalWindow(11, w1
.get(), &d11
);
1235 WindowTracker tracker
;
1236 DeletingEventFilter w1_filter
;
1237 w1
->AddPreTargetHandler(&w1_filter
);
1238 client::GetFocusClient(w1
.get())->FocusWindow(w11
);
1240 ui::test::EventGenerator
generator(root_window(), w11
);
1242 // First up, no one deletes anything.
1244 d11
.Reset(w11
, false);
1246 generator
.PressLeftButton();
1247 EXPECT_TRUE(tracker
.Contains(w11
));
1248 EXPECT_TRUE(d11
.got_event());
1249 generator
.ReleaseLeftButton();
1251 // Delegate deletes w11. This will prevent the post-handle step from applying.
1252 w1_filter
.Reset(false);
1253 d11
.Reset(w11
, true);
1254 generator
.PressKey(ui::VKEY_A
, 0);
1255 EXPECT_FALSE(tracker
.Contains(w11
));
1256 EXPECT_TRUE(d11
.got_event());
1258 // Pre-handle step deletes w11. This will prevent the delegate and the post-
1259 // handle steps from applying.
1260 w11
= CreateNormalWindow(11, w1
.get(), &d11
);
1261 w1_filter
.Reset(true);
1262 d11
.Reset(w11
, false);
1263 generator
.PressLeftButton();
1264 EXPECT_FALSE(tracker
.Contains(w11
));
1265 EXPECT_FALSE(d11
.got_event());
1270 // A window delegate that detaches the parent of the target's parent window when
1271 // it receives a tap event.
1272 class DetachesParentOnTapDelegate
: public test::TestWindowDelegate
{
1274 DetachesParentOnTapDelegate() {}
1275 ~DetachesParentOnTapDelegate() override
{}
1278 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1279 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
1280 event
->SetHandled();
1284 if (event
->type() == ui::ET_GESTURE_TAP
) {
1285 Window
* parent
= static_cast<Window
*>(event
->target())->parent();
1286 parent
->parent()->RemoveChild(parent
);
1287 event
->SetHandled();
1291 DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate
);
1296 // Tests that the gesture recognizer is reset for all child windows when a
1297 // window hides. No expectations, just checks that the test does not crash.
1298 TEST_F(WindowEventDispatcherTest
,
1299 GestureRecognizerResetsTargetWhenParentHides
) {
1300 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1301 DetachesParentOnTapDelegate delegate
;
1302 scoped_ptr
<Window
> parent(CreateNormalWindow(22, w1
.get(), NULL
));
1303 Window
* child
= CreateNormalWindow(11, parent
.get(), &delegate
);
1304 ui::test::EventGenerator
generator(root_window(), child
);
1305 generator
.GestureTapAt(gfx::Point(40, 40));
1310 // A window delegate that processes nested gestures on tap.
1311 class NestedGestureDelegate
: public test::TestWindowDelegate
{
1313 NestedGestureDelegate(ui::test::EventGenerator
* generator
,
1314 const gfx::Point tap_location
)
1315 : generator_(generator
),
1316 tap_location_(tap_location
),
1317 gesture_end_count_(0) {}
1318 ~NestedGestureDelegate() override
{}
1320 int gesture_end_count() const { return gesture_end_count_
; }
1323 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1324 switch (event
->type()) {
1325 case ui::ET_GESTURE_TAP_DOWN
:
1326 event
->SetHandled();
1328 case ui::ET_GESTURE_TAP
:
1330 generator_
->GestureTapAt(tap_location_
);
1331 event
->SetHandled();
1333 case ui::ET_GESTURE_END
:
1334 ++gesture_end_count_
;
1341 ui::test::EventGenerator
* generator_
;
1342 const gfx::Point tap_location_
;
1343 int gesture_end_count_
;
1344 DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate
);
1349 // Tests that gesture end is delivered after nested gesture processing.
1350 TEST_F(WindowEventDispatcherTest
, GestureEndDeliveredAfterNestedGestures
) {
1351 NestedGestureDelegate
d1(NULL
, gfx::Point());
1352 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &d1
));
1353 w1
->SetBounds(gfx::Rect(0, 0, 100, 100));
1355 ui::test::EventGenerator
nested_generator(root_window(), w1
.get());
1356 NestedGestureDelegate
d2(&nested_generator
, w1
->bounds().CenterPoint());
1357 scoped_ptr
<Window
> w2(CreateNormalWindow(1, root_window(), &d2
));
1358 w2
->SetBounds(gfx::Rect(100, 0, 100, 100));
1360 // Tap on w2 which triggers nested gestures for w1.
1361 ui::test::EventGenerator
generator(root_window(), w2
.get());
1362 generator
.GestureTapAt(w2
->bounds().CenterPoint());
1364 // Both windows should get their gesture end events.
1365 EXPECT_EQ(1, d1
.gesture_end_count());
1366 EXPECT_EQ(1, d2
.gesture_end_count());
1369 // Tests whether we can repost the Tap down gesture event.
1370 TEST_F(WindowEventDispatcherTest
, RepostTapdownGestureTest
) {
1371 EventFilterRecorder recorder
;
1372 root_window()->AddPreTargetHandler(&recorder
);
1374 test::TestWindowDelegate delegate
;
1375 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1376 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1378 ui::GestureEventDetails
details(ui::ET_GESTURE_TAP_DOWN
);
1379 gfx::Point
point(10, 10);
1380 ui::GestureEvent
event(point
.x(),
1383 ui::EventTimeForNow(),
1385 host()->dispatcher()->RepostEvent(event
);
1386 RunAllPendingInMessageLoop();
1387 // TODO(rbyers): Currently disabled - crbug.com/170987
1388 EXPECT_FALSE(EventTypesToString(recorder
.events()).find("GESTURE_TAP_DOWN") !=
1391 root_window()->RemovePreTargetHandler(&recorder
);
1394 // This class inherits from the EventFilterRecorder class which provides a
1395 // facility to record events. This class additionally provides a facility to
1396 // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
1397 // events after that.
1398 class RepostGestureEventRecorder
: public EventFilterRecorder
{
1400 RepostGestureEventRecorder(aura::Window
* repost_source
,
1401 aura::Window
* repost_target
)
1402 : repost_source_(repost_source
),
1403 repost_target_(repost_target
),
1405 done_cleanup_(false) {}
1407 ~RepostGestureEventRecorder() override
{}
1409 void OnTouchEvent(ui::TouchEvent
* event
) override
{
1410 if (reposted_
&& event
->type() == ui::ET_TOUCH_PRESSED
) {
1411 done_cleanup_
= true;
1414 EventFilterRecorder::OnTouchEvent(event
);
1417 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1418 EXPECT_EQ(done_cleanup_
? repost_target_
: repost_source_
, event
->target());
1419 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
1421 EXPECT_NE(repost_target_
, event
->target());
1423 repost_target_
->GetHost()->dispatcher()->RepostEvent(*event
);
1424 // Ensure that the reposted gesture event above goes to the
1426 repost_source_
->GetRootWindow()->RemoveChild(repost_source_
);
1430 EventFilterRecorder::OnGestureEvent(event
);
1433 // Ignore mouse events as they don't fire at all times. This causes
1434 // the GestureRepostEventOrder test to fail randomly.
1435 void OnMouseEvent(ui::MouseEvent
* event
) override
{}
1438 aura::Window
* repost_source_
;
1439 aura::Window
* repost_target_
;
1440 // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
1442 // set true if we're done cleaning up after hiding repost_source_;
1444 DISALLOW_COPY_AND_ASSIGN(RepostGestureEventRecorder
);
1447 // Tests whether events which are generated after the reposted gesture event
1448 // are received after that. In this case the scroll sequence events should
1449 // be received after the reposted gesture event.
1450 TEST_F(WindowEventDispatcherTest
, GestureRepostEventOrder
) {
1451 // Expected events at the end for the repost_target window defined below.
1452 const char kExpectedTargetEvents
[] =
1453 // TODO)(rbyers): Gesture event reposting is disabled - crbug.com/279039.
1454 // "GESTURE_BEGIN GESTURE_TAP_DOWN "
1455 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1456 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE TOUCH_MOVED "
1457 "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
1458 "GESTURE_SCROLL_END GESTURE_END";
1459 // We create two windows.
1460 // The first window (repost_source) is the one to which the initial tap
1461 // gesture is sent. It reposts this event to the second window
1463 // We then generate the scroll sequence for repost_target and look for two
1464 // ET_GESTURE_TAP_DOWN events in the event list at the end.
1465 test::TestWindowDelegate delegate
;
1466 scoped_ptr
<aura::Window
> repost_target(CreateTestWindowWithDelegate(
1467 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1469 scoped_ptr
<aura::Window
> repost_source(CreateTestWindowWithDelegate(
1470 &delegate
, 1, gfx::Rect(0, 0, 50, 50), root_window()));
1472 RepostGestureEventRecorder
repost_event_recorder(repost_source
.get(),
1473 repost_target
.get());
1474 root_window()->AddPreTargetHandler(&repost_event_recorder
);
1476 // Generate a tap down gesture for the repost_source. This will be reposted
1477 // to repost_target.
1478 ui::test::EventGenerator
repost_generator(root_window(), repost_source
.get());
1479 repost_generator
.GestureTapAt(gfx::Point(40, 40));
1480 RunAllPendingInMessageLoop();
1482 ui::test::EventGenerator
scroll_generator(root_window(), repost_target
.get());
1483 scroll_generator
.GestureScrollSequence(
1485 gfx::Point(100, 100),
1486 base::TimeDelta::FromMilliseconds(100),
1488 RunAllPendingInMessageLoop();
1490 int tap_down_count
= 0;
1491 for (size_t i
= 0; i
< repost_event_recorder
.events().size(); ++i
) {
1492 if (repost_event_recorder
.events()[i
] == ui::ET_GESTURE_TAP_DOWN
)
1496 // We expect two tap down events. One from the repost and the other one from
1497 // the scroll sequence posted above.
1498 // TODO(rbyers): Currently disabled - crbug.com/170987
1499 EXPECT_EQ(1, tap_down_count
);
1501 EXPECT_EQ(kExpectedTargetEvents
,
1502 EventTypesToString(repost_event_recorder
.events()));
1503 root_window()->RemovePreTargetHandler(&repost_event_recorder
);
1506 class OnMouseExitDeletingEventFilter
: public EventFilterRecorder
{
1508 OnMouseExitDeletingEventFilter() : window_to_delete_(NULL
) {}
1509 ~OnMouseExitDeletingEventFilter() override
{}
1511 void set_window_to_delete(Window
* window_to_delete
) {
1512 window_to_delete_
= window_to_delete
;
1516 // Overridden from ui::EventHandler:
1517 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1518 EventFilterRecorder::OnMouseEvent(event
);
1519 if (window_to_delete_
) {
1520 delete window_to_delete_
;
1521 window_to_delete_
= NULL
;
1525 Window
* window_to_delete_
;
1527 DISALLOW_COPY_AND_ASSIGN(OnMouseExitDeletingEventFilter
);
1530 // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
1531 // a child, but the child is destroyed because of the synthesized mouse-exit
1532 // event generated on the previous mouse_moved_handler_.
1533 TEST_F(WindowEventDispatcherTest
, DeleteWindowDuringMouseMovedDispatch
) {
1534 // Create window 1 and set its event filter. Window 1 will take ownership of
1535 // the event filter.
1536 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1537 OnMouseExitDeletingEventFilter w1_filter
;
1538 w1
->AddPreTargetHandler(&w1_filter
);
1539 w1
->SetBounds(gfx::Rect(20, 20, 60, 60));
1540 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_moved_handler());
1542 ui::test::EventGenerator
generator(root_window(), w1
.get());
1544 // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
1546 generator
.MoveMouseTo(51, 51);
1547 EXPECT_EQ(w1
.get(), host()->dispatcher()->mouse_moved_handler());
1549 // Create window 2 under the mouse cursor and stack it above window 1.
1550 Window
* w2
= CreateNormalWindow(2, root_window(), NULL
);
1551 w2
->SetBounds(gfx::Rect(30, 30, 40, 40));
1552 root_window()->StackChildAbove(w2
, w1
.get());
1554 // Set window 2 as the window that is to be deleted when a mouse-exited event
1555 // happens on window 1.
1556 w1_filter
.set_window_to_delete(w2
);
1558 // Move mosue over window 2. This should generate a mouse-exited event for
1559 // window 1 resulting in deletion of window 2. The original mouse-moved event
1560 // that was targeted to window 2 should be dropped since window 2 is
1561 // destroyed. This test passes if no crash happens.
1562 generator
.MoveMouseTo(52, 52);
1563 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_moved_handler());
1565 // Check events received by window 1.
1566 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
1567 EventTypesToString(w1_filter
.events()));
1572 // Used to track if OnWindowDestroying() is invoked and if there is a valid
1573 // RootWindow at such time.
1574 class ValidRootDuringDestructionWindowObserver
: public aura::WindowObserver
{
1576 ValidRootDuringDestructionWindowObserver(bool* got_destroying
,
1577 bool* has_valid_root
)
1578 : got_destroying_(got_destroying
),
1579 has_valid_root_(has_valid_root
) {
1583 void OnWindowDestroying(aura::Window
* window
) override
{
1584 *got_destroying_
= true;
1585 *has_valid_root_
= (window
->GetRootWindow() != NULL
);
1589 bool* got_destroying_
;
1590 bool* has_valid_root_
;
1592 DISALLOW_COPY_AND_ASSIGN(ValidRootDuringDestructionWindowObserver
);
1597 // Verifies GetRootWindow() from ~Window returns a valid root.
1598 TEST_F(WindowEventDispatcherTest
, ValidRootDuringDestruction
) {
1599 bool got_destroying
= false;
1600 bool has_valid_root
= false;
1601 ValidRootDuringDestructionWindowObserver
observer(&got_destroying
,
1604 scoped_ptr
<WindowTreeHost
> host(
1605 WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100)));
1607 // Owned by WindowEventDispatcher.
1608 Window
* w1
= CreateNormalWindow(1, host
->window(), NULL
);
1609 w1
->AddObserver(&observer
);
1611 EXPECT_TRUE(got_destroying
);
1612 EXPECT_TRUE(has_valid_root
);
1617 // See description above DontResetHeldEvent for details.
1618 class DontResetHeldEventWindowDelegate
: public test::TestWindowDelegate
{
1620 explicit DontResetHeldEventWindowDelegate(aura::Window
* root
)
1622 mouse_event_count_(0) {}
1623 ~DontResetHeldEventWindowDelegate() override
{}
1625 int mouse_event_count() const { return mouse_event_count_
; }
1627 // TestWindowDelegate:
1628 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1629 if ((event
->flags() & ui::EF_SHIFT_DOWN
) != 0 &&
1630 mouse_event_count_
++ == 0) {
1631 ui::MouseEvent
mouse_event(ui::ET_MOUSE_PRESSED
,
1632 gfx::Point(10, 10), gfx::Point(10, 10),
1633 ui::EF_SHIFT_DOWN
, 0);
1634 root_
->GetHost()->dispatcher()->RepostEvent(mouse_event
);
1640 int mouse_event_count_
;
1642 DISALLOW_COPY_AND_ASSIGN(DontResetHeldEventWindowDelegate
);
1647 // Verifies RootWindow doesn't reset |RootWindow::held_repostable_event_| after
1648 // dispatching. This is done by using DontResetHeldEventWindowDelegate, which
1649 // tracks the number of events with ui::EF_SHIFT_DOWN set (all reposted events
1650 // have EF_SHIFT_DOWN). When the first event is seen RepostEvent() is used to
1651 // schedule another reposted event.
1652 TEST_F(WindowEventDispatcherTest
, DontResetHeldEvent
) {
1653 DontResetHeldEventWindowDelegate
delegate(root_window());
1654 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &delegate
));
1655 w1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1656 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
1657 gfx::Point(10, 10), gfx::Point(10, 10),
1658 ui::EF_SHIFT_DOWN
, 0);
1659 root_window()->GetHost()->dispatcher()->RepostEvent(pressed
);
1660 ui::MouseEvent
pressed2(ui::ET_MOUSE_PRESSED
,
1661 gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
1662 // Dispatch an event to flush event scheduled by way of RepostEvent().
1663 DispatchEventUsingWindowDispatcher(&pressed2
);
1664 // Delegate should have seen reposted event (identified by way of
1665 // EF_SHIFT_DOWN). Dispatch another event to flush the second
1667 EXPECT_EQ(1, delegate
.mouse_event_count());
1668 DispatchEventUsingWindowDispatcher(&pressed2
);
1669 EXPECT_EQ(2, delegate
.mouse_event_count());
1674 // See description above DeleteHostFromHeldMouseEvent for details.
1675 class DeleteHostFromHeldMouseEventDelegate
1676 : public test::TestWindowDelegate
{
1678 explicit DeleteHostFromHeldMouseEventDelegate(WindowTreeHost
* host
)
1680 got_mouse_event_(false),
1681 got_destroy_(false) {
1683 ~DeleteHostFromHeldMouseEventDelegate() override
{}
1685 bool got_mouse_event() const { return got_mouse_event_
; }
1686 bool got_destroy() const { return got_destroy_
; }
1688 // TestWindowDelegate:
1689 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1690 if ((event
->flags() & ui::EF_SHIFT_DOWN
) != 0) {
1691 got_mouse_event_
= true;
1695 void OnWindowDestroyed(Window
* window
) override
{ got_destroy_
= true; }
1698 WindowTreeHost
* host_
;
1699 bool got_mouse_event_
;
1702 DISALLOW_COPY_AND_ASSIGN(DeleteHostFromHeldMouseEventDelegate
);
1707 // Verifies if a WindowTreeHost is deleted from dispatching a held mouse event
1709 TEST_F(WindowEventDispatcherTest
, DeleteHostFromHeldMouseEvent
) {
1710 // Should be deleted by |delegate|.
1711 WindowTreeHost
* h2
= WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100));
1713 DeleteHostFromHeldMouseEventDelegate
delegate(h2
);
1715 Window
* w1
= CreateNormalWindow(1, h2
->window(), &delegate
);
1716 w1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1717 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
1718 gfx::Point(10, 10), gfx::Point(10, 10),
1719 ui::EF_SHIFT_DOWN
, 0);
1720 h2
->dispatcher()->RepostEvent(pressed
);
1721 // RunAllPendingInMessageLoop() to make sure the |pressed| is run.
1722 RunAllPendingInMessageLoop();
1723 EXPECT_TRUE(delegate
.got_mouse_event());
1724 EXPECT_TRUE(delegate
.got_destroy());
1727 TEST_F(WindowEventDispatcherTest
, WindowHideCancelsActiveTouches
) {
1728 EventFilterRecorder recorder
;
1729 root_window()->AddPreTargetHandler(&recorder
);
1731 test::TestWindowDelegate delegate
;
1732 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1733 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1735 gfx::Point position1
= root_window()->bounds().origin();
1736 ui::TouchEvent
press(
1737 ui::ET_TOUCH_PRESSED
, position1
, 0, ui::EventTimeForNow());
1738 DispatchEventUsingWindowDispatcher(&press
);
1740 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN",
1741 EventTypesToString(recorder
.GetAndResetEvents()));
1745 EXPECT_EQ(ui::ET_TOUCH_CANCELLED
, recorder
.events()[0]);
1746 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_TAP_CANCEL
));
1747 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_END
));
1748 EXPECT_EQ(3U, recorder
.events().size());
1749 root_window()->RemovePreTargetHandler(&recorder
);
1752 TEST_F(WindowEventDispatcherTest
, WindowHideCancelsActiveGestures
) {
1753 EventFilterRecorder recorder
;
1754 root_window()->AddPreTargetHandler(&recorder
);
1756 test::TestWindowDelegate delegate
;
1757 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1758 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1760 gfx::Point position1
= root_window()->bounds().origin();
1761 gfx::Point position2
= root_window()->bounds().CenterPoint();
1762 ui::TouchEvent
press(
1763 ui::ET_TOUCH_PRESSED
, position1
, 0, ui::EventTimeForNow());
1764 DispatchEventUsingWindowDispatcher(&press
);
1766 ui::TouchEvent
move(
1767 ui::ET_TOUCH_MOVED
, position2
, 0, ui::EventTimeForNow());
1768 DispatchEventUsingWindowDispatcher(&move
);
1770 ui::TouchEvent
press2(
1771 ui::ET_TOUCH_PRESSED
, position1
, 1, ui::EventTimeForNow());
1772 DispatchEventUsingWindowDispatcher(&press2
);
1774 // TODO(tdresser): once the unified Gesture Recognizer has stuck, remove the
1775 // special casing here. See crbug.com/332418 for details.
1776 std::string expected
=
1777 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1778 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1779 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_PINCH_BEGIN";
1781 std::string expected_ugr
=
1782 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1783 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1784 "TOUCH_PRESSED GESTURE_BEGIN";
1786 std::string events_string
= EventTypesToString(recorder
.GetAndResetEvents());
1787 EXPECT_TRUE((expected
== events_string
) || (expected_ugr
== events_string
));
1792 "TOUCH_CANCELLED GESTURE_PINCH_END GESTURE_END TOUCH_CANCELLED "
1793 "GESTURE_SCROLL_END GESTURE_END";
1795 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END TOUCH_CANCELLED "
1798 events_string
= EventTypesToString(recorder
.GetAndResetEvents());
1799 EXPECT_TRUE((expected
== events_string
) || (expected_ugr
== events_string
));
1801 root_window()->RemovePreTargetHandler(&recorder
);
1804 // Places two windows side by side. Presses down on one window, and starts a
1805 // scroll. Sets capture on the other window and ensures that the "ending" events
1806 // aren't sent to the window which gained capture.
1807 TEST_F(WindowEventDispatcherTest
, EndingEventDoesntRetarget
) {
1808 EventFilterRecorder recorder1
;
1809 EventFilterRecorder recorder2
;
1810 scoped_ptr
<Window
> window1(CreateNormalWindow(1, root_window(), NULL
));
1811 window1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1813 scoped_ptr
<Window
> window2(CreateNormalWindow(2, root_window(), NULL
));
1814 window2
->SetBounds(gfx::Rect(40, 0, 40, 40));
1816 window1
->AddPreTargetHandler(&recorder1
);
1817 window2
->AddPreTargetHandler(&recorder2
);
1819 gfx::Point position
= window1
->bounds().origin();
1820 ui::TouchEvent
press(
1821 ui::ET_TOUCH_PRESSED
, position
, 0, ui::EventTimeForNow());
1822 DispatchEventUsingWindowDispatcher(&press
);
1824 gfx::Point position2
= window1
->bounds().CenterPoint();
1825 ui::TouchEvent
move(
1826 ui::ET_TOUCH_MOVED
, position2
, 0, ui::EventTimeForNow());
1827 DispatchEventUsingWindowDispatcher(&move
);
1829 window2
->SetCapture();
1831 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1832 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1833 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END",
1834 EventTypesToString(recorder1
.events()));
1836 EXPECT_TRUE(recorder2
.events().empty());
1841 // This class creates and manages a window which is destroyed as soon as
1842 // capture is lost. This is the case for the drag and drop capture window.
1843 class CaptureWindowTracker
: public test::TestWindowDelegate
{
1845 CaptureWindowTracker() {}
1846 ~CaptureWindowTracker() override
{}
1848 void CreateCaptureWindow(aura::Window
* root_window
) {
1849 capture_window_
.reset(test::CreateTestWindowWithDelegate(
1850 this, -1234, gfx::Rect(20, 20, 20, 20), root_window
));
1851 capture_window_
->SetCapture();
1855 capture_window_
.reset();
1858 void OnCaptureLost() override
{ capture_window_
.reset(); }
1860 void OnWindowDestroyed(Window
* window
) override
{
1861 TestWindowDelegate::OnWindowDestroyed(window
);
1862 capture_window_
.reset();
1865 aura::Window
* capture_window() { return capture_window_
.get(); }
1868 scoped_ptr
<aura::Window
> capture_window_
;
1870 DISALLOW_COPY_AND_ASSIGN(CaptureWindowTracker
);
1875 // Verifies handling loss of capture by the capture window being hidden.
1876 TEST_F(WindowEventDispatcherTest
, CaptureWindowHidden
) {
1877 CaptureWindowTracker capture_window_tracker
;
1878 capture_window_tracker
.CreateCaptureWindow(root_window());
1879 capture_window_tracker
.capture_window()->Hide();
1880 EXPECT_EQ(NULL
, capture_window_tracker
.capture_window());
1883 // Verifies handling loss of capture by the capture window being destroyed.
1884 TEST_F(WindowEventDispatcherTest
, CaptureWindowDestroyed
) {
1885 CaptureWindowTracker capture_window_tracker
;
1886 capture_window_tracker
.CreateCaptureWindow(root_window());
1887 capture_window_tracker
.reset();
1888 EXPECT_EQ(NULL
, capture_window_tracker
.capture_window());
1891 class ExitMessageLoopOnMousePress
: public ui::test::TestEventHandler
{
1893 ExitMessageLoopOnMousePress() {}
1894 ~ExitMessageLoopOnMousePress() override
{}
1897 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1898 ui::test::TestEventHandler::OnMouseEvent(event
);
1899 if (event
->type() == ui::ET_MOUSE_PRESSED
)
1900 base::MessageLoopForUI::current()->Quit();
1904 DISALLOW_COPY_AND_ASSIGN(ExitMessageLoopOnMousePress
);
1907 class WindowEventDispatcherTestWithMessageLoop
1908 : public WindowEventDispatcherTest
{
1910 WindowEventDispatcherTestWithMessageLoop() {}
1911 ~WindowEventDispatcherTestWithMessageLoop() override
{}
1914 // Reset any event the window may have received when bringing up the window
1915 // (e.g. mouse-move events if the mouse cursor is over the window).
1918 // Start a nested message-loop, post an event to be dispatched, and then
1919 // terminate the message-loop. When the message-loop unwinds and gets back,
1920 // the reposted event should not have fired.
1921 scoped_ptr
<ui::MouseEvent
> mouse(new ui::MouseEvent(ui::ET_MOUSE_PRESSED
,
1926 message_loop()->PostTask(
1928 base::Bind(&WindowEventDispatcherTestWithMessageLoop::RepostEventHelper
,
1929 host()->dispatcher(),
1930 base::Passed(&mouse
)));
1931 message_loop()->PostTask(FROM_HERE
, message_loop()->QuitClosure());
1933 base::MessageLoop::ScopedNestableTaskAllower
allow(message_loop());
1936 EXPECT_EQ(0, handler_
.num_mouse_events());
1938 // Let the current message-loop run. The event-handler will terminate the
1939 // message-loop when it receives the reposted event.
1942 base::MessageLoop
* message_loop() {
1943 return base::MessageLoopForUI::current();
1947 void SetUp() override
{
1948 WindowEventDispatcherTest::SetUp();
1949 window_
.reset(CreateNormalWindow(1, root_window(), NULL
));
1950 window_
->AddPreTargetHandler(&handler_
);
1953 void TearDown() override
{
1955 WindowEventDispatcherTest::TearDown();
1959 // Used to avoid a copying |event| when binding to a closure.
1960 static void RepostEventHelper(WindowEventDispatcher
* dispatcher
,
1961 scoped_ptr
<ui::MouseEvent
> event
) {
1962 dispatcher
->RepostEvent(*event
);
1965 scoped_ptr
<Window
> window_
;
1966 ExitMessageLoopOnMousePress handler_
;
1968 DISALLOW_COPY_AND_ASSIGN(WindowEventDispatcherTestWithMessageLoop
);
1971 TEST_F(WindowEventDispatcherTestWithMessageLoop
, EventRepostedInNonNestedLoop
) {
1972 CHECK(!message_loop()->is_running());
1973 // Perform the test in a callback, so that it runs after the message-loop
1975 message_loop()->PostTask(
1976 FROM_HERE
, base::Bind(
1977 &WindowEventDispatcherTestWithMessageLoop::RunTest
,
1978 base::Unretained(this)));
1979 message_loop()->Run();
1982 class WindowEventDispatcherTestInHighDPI
: public WindowEventDispatcherTest
{
1984 WindowEventDispatcherTestInHighDPI() {}
1985 ~WindowEventDispatcherTestInHighDPI() override
{}
1988 void SetUp() override
{
1989 WindowEventDispatcherTest::SetUp();
1990 test_screen()->SetDeviceScaleFactor(2.f
);
1994 TEST_F(WindowEventDispatcherTestInHighDPI
, EventLocationTransform
) {
1995 test::TestWindowDelegate delegate
;
1996 scoped_ptr
<aura::Window
> child(test::CreateTestWindowWithDelegate(&delegate
,
1997 1234, gfx::Rect(20, 20, 100, 100), root_window()));
2000 ui::test::TestEventHandler handler_child
;
2001 ui::test::TestEventHandler handler_root
;
2002 root_window()->AddPreTargetHandler(&handler_root
);
2003 child
->AddPreTargetHandler(&handler_child
);
2006 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
2007 gfx::Point(30, 30), gfx::Point(30, 30),
2008 ui::EF_NONE
, ui::EF_NONE
);
2009 DispatchEventUsingWindowDispatcher(&move
);
2010 EXPECT_EQ(0, handler_child
.num_mouse_events());
2011 EXPECT_EQ(1, handler_root
.num_mouse_events());
2015 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
2016 gfx::Point(50, 50), gfx::Point(50, 50),
2017 ui::EF_NONE
, ui::EF_NONE
);
2018 DispatchEventUsingWindowDispatcher(&move
);
2019 // The child receives an ENTER, and a MOVED event.
2020 EXPECT_EQ(2, handler_child
.num_mouse_events());
2021 // The root receives both the ENTER and the MOVED events dispatched to
2022 // |child|, as well as an EXIT event.
2023 EXPECT_EQ(3, handler_root
.num_mouse_events());
2026 child
->RemovePreTargetHandler(&handler_child
);
2027 root_window()->RemovePreTargetHandler(&handler_root
);
2030 TEST_F(WindowEventDispatcherTestInHighDPI
, TouchMovesHeldOnScroll
) {
2031 EventFilterRecorder recorder
;
2032 root_window()->AddPreTargetHandler(&recorder
);
2033 test::TestWindowDelegate delegate
;
2034 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
2035 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2036 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2037 window
->AddPreTargetHandler(&handler
);
2039 ui::test::EventGenerator
generator(root_window());
2040 generator
.GestureScrollSequence(
2041 gfx::Point(120, 120), gfx::Point(20, 120),
2042 base::TimeDelta::FromMilliseconds(100), 25);
2044 // |handler| will have reset |filter| and started holding the touch-move
2045 // events when scrolling started. At the end of the scroll (i.e. upon
2046 // touch-release), the held touch-move event will have been dispatched first,
2047 // along with the subsequent events (i.e. touch-release, scroll-end, and
2049 const EventFilterRecorder::Events
& events
= recorder
.events();
2051 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
2052 "GESTURE_SCROLL_END GESTURE_END",
2053 EventTypesToString(events
));
2054 ASSERT_EQ(2u, recorder
.touch_locations().size());
2055 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2056 recorder
.touch_locations()[0].ToString());
2057 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2058 recorder
.touch_locations()[1].ToString());
2061 class SelfDestructDelegate
: public test::TestWindowDelegate
{
2063 SelfDestructDelegate() {}
2064 ~SelfDestructDelegate() override
{}
2066 void OnMouseEvent(ui::MouseEvent
* event
) override
{ window_
.reset(); }
2068 void set_window(scoped_ptr
<aura::Window
> window
) {
2069 window_
= window
.Pass();
2071 bool has_window() const { return !!window_
.get(); }
2074 scoped_ptr
<aura::Window
> window_
;
2075 DISALLOW_COPY_AND_ASSIGN(SelfDestructDelegate
);
2078 TEST_F(WindowEventDispatcherTest
, SynthesizedLocatedEvent
) {
2079 ui::test::EventGenerator
generator(root_window());
2080 generator
.MoveMouseTo(10, 10);
2082 Env::GetInstance()->last_mouse_location().ToString());
2084 // Synthesized event should not update the mouse location.
2085 ui::MouseEvent
mouseev(ui::ET_MOUSE_MOVED
, gfx::Point(), gfx::Point(),
2086 ui::EF_IS_SYNTHESIZED
, 0);
2087 generator
.Dispatch(&mouseev
);
2089 Env::GetInstance()->last_mouse_location().ToString());
2091 generator
.MoveMouseTo(0, 0);
2093 Env::GetInstance()->last_mouse_location().ToString());
2095 // Make sure the location gets updated when a syntheiszed enter
2096 // event destroyed the window.
2097 SelfDestructDelegate delegate
;
2098 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2099 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2100 delegate
.set_window(window
.Pass());
2101 EXPECT_TRUE(delegate
.has_window());
2103 generator
.MoveMouseTo(100, 100);
2104 EXPECT_FALSE(delegate
.has_window());
2105 EXPECT_EQ("100,100",
2106 Env::GetInstance()->last_mouse_location().ToString());
2109 // Tests that the window which has capture can get destroyed as a result of
2110 // ui::ET_MOUSE_CAPTURE_CHANGED event dispatched in
2111 // WindowEventDispatcher::UpdateCapture without causing a "use after free".
2112 TEST_F(WindowEventDispatcherTest
, DestroyWindowOnCaptureChanged
) {
2113 SelfDestructDelegate delegate
;
2114 scoped_ptr
<aura::Window
> window_first(CreateTestWindowWithDelegate(
2115 &delegate
, 1, gfx::Rect(20, 10, 10, 20), root_window()));
2116 Window
* window_first_raw
= window_first
.get();
2117 window_first
->Show();
2118 window_first
->SetCapture();
2119 delegate
.set_window(window_first
.Pass());
2120 EXPECT_TRUE(delegate
.has_window());
2122 scoped_ptr
<aura::Window
> window_second(
2123 test::CreateTestWindowWithId(2, root_window()));
2124 window_second
->Show();
2126 client::CaptureDelegate
* capture_delegate
= host()->dispatcher();
2127 capture_delegate
->UpdateCapture(window_first_raw
, window_second
.get());
2128 EXPECT_FALSE(delegate
.has_window());
2131 class StaticFocusClient
: public client::FocusClient
{
2133 explicit StaticFocusClient(Window
* focused
)
2134 : focused_(focused
) {}
2135 ~StaticFocusClient() override
{}
2138 // client::FocusClient:
2139 void AddObserver(client::FocusChangeObserver
* observer
) override
{}
2140 void RemoveObserver(client::FocusChangeObserver
* observer
) override
{}
2141 void FocusWindow(Window
* window
) override
{}
2142 void ResetFocusWithinActiveWindow(Window
* window
) override
{}
2143 Window
* GetFocusedWindow() override
{ return focused_
; }
2147 DISALLOW_COPY_AND_ASSIGN(StaticFocusClient
);
2150 // Tests that host-cancel-mode event can be dispatched to a dispatcher safely
2151 // when the focused window does not live in the dispatcher's tree.
2152 TEST_F(WindowEventDispatcherTest
, HostCancelModeWithFocusedWindowOutside
) {
2153 test::TestWindowDelegate delegate
;
2154 scoped_ptr
<Window
> focused(CreateTestWindowWithDelegate(&delegate
, 123,
2155 gfx::Rect(20, 30, 100, 50), NULL
));
2156 StaticFocusClient
focus_client(focused
.get());
2157 client::SetFocusClient(root_window(), &focus_client
);
2158 EXPECT_FALSE(root_window()->Contains(focused
.get()));
2159 EXPECT_EQ(focused
.get(),
2160 client::GetFocusClient(root_window())->GetFocusedWindow());
2161 host()->dispatcher()->DispatchCancelModeEvent();
2162 EXPECT_EQ(focused
.get(),
2163 client::GetFocusClient(root_window())->GetFocusedWindow());
2166 // Dispatches a mouse-move event to |target| when it receives a mouse-move
2168 class DispatchEventHandler
: public ui::EventHandler
{
2170 explicit DispatchEventHandler(Window
* target
)
2172 dispatched_(false) {}
2173 ~DispatchEventHandler() override
{}
2175 bool dispatched() const { return dispatched_
; }
2177 // ui::EventHandler:
2178 void OnMouseEvent(ui::MouseEvent
* mouse
) override
{
2179 if (mouse
->type() == ui::ET_MOUSE_MOVED
) {
2180 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, target_
->bounds().CenterPoint(),
2181 target_
->bounds().CenterPoint(), ui::EF_NONE
, ui::EF_NONE
);
2182 ui::EventDispatchDetails details
=
2183 target_
->GetHost()->dispatcher()->OnEventFromSource(&move
);
2184 ASSERT_FALSE(details
.dispatcher_destroyed
);
2185 EXPECT_FALSE(details
.target_destroyed
);
2186 EXPECT_EQ(target_
, move
.target());
2189 ui::EventHandler::OnMouseEvent(mouse
);
2195 DISALLOW_COPY_AND_ASSIGN(DispatchEventHandler
);
2198 // Moves |window| to |root_window| when it receives a mouse-move event.
2199 class MoveWindowHandler
: public ui::EventHandler
{
2201 MoveWindowHandler(Window
* window
, Window
* root_window
)
2202 : window_to_move_(window
),
2203 root_window_to_move_to_(root_window
) {}
2204 ~MoveWindowHandler() override
{}
2207 // ui::EventHandler:
2208 void OnMouseEvent(ui::MouseEvent
* mouse
) override
{
2209 if (mouse
->type() == ui::ET_MOUSE_MOVED
) {
2210 root_window_to_move_to_
->AddChild(window_to_move_
);
2212 ui::EventHandler::OnMouseEvent(mouse
);
2215 Window
* window_to_move_
;
2216 Window
* root_window_to_move_to_
;
2218 DISALLOW_COPY_AND_ASSIGN(MoveWindowHandler
);
2221 // Tests that nested event dispatch works correctly if the target of the older
2222 // event being dispatched is moved to a different dispatcher in response to an
2223 // event in the inner loop.
2224 TEST_F(WindowEventDispatcherTest
, NestedEventDispatchTargetMoved
) {
2225 scoped_ptr
<WindowTreeHost
> second_host(
2226 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2227 second_host
->InitHost();
2228 Window
* second_root
= second_host
->window();
2230 // Create two windows parented to |root_window()|.
2231 test::TestWindowDelegate delegate
;
2232 scoped_ptr
<Window
> first(CreateTestWindowWithDelegate(&delegate
, 123,
2233 gfx::Rect(20, 10, 10, 20), root_window()));
2234 scoped_ptr
<Window
> second(CreateTestWindowWithDelegate(&delegate
, 234,
2235 gfx::Rect(40, 10, 50, 20), root_window()));
2237 // Setup a handler on |first| so that it dispatches an event to |second| when
2238 // |first| receives an event.
2239 DispatchEventHandler
dispatch_event(second
.get());
2240 first
->AddPreTargetHandler(&dispatch_event
);
2242 // Setup a handler on |second| so that it moves |first| into |second_root|
2243 // when |second| receives an event.
2244 MoveWindowHandler
move_window(first
.get(), second_root
);
2245 second
->AddPreTargetHandler(&move_window
);
2247 // Some sanity checks: |first| is inside |root_window()|'s tree.
2248 EXPECT_EQ(root_window(), first
->GetRootWindow());
2249 // The two root windows are different.
2250 EXPECT_NE(root_window(), second_root
);
2252 // Dispatch an event to |first|.
2253 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, first
->bounds().CenterPoint(),
2254 first
->bounds().CenterPoint(), ui::EF_NONE
, ui::EF_NONE
);
2255 ui::EventDispatchDetails details
=
2256 host()->dispatcher()->OnEventFromSource(&move
);
2257 ASSERT_FALSE(details
.dispatcher_destroyed
);
2258 EXPECT_TRUE(details
.target_destroyed
);
2259 EXPECT_EQ(first
.get(), move
.target());
2260 EXPECT_TRUE(dispatch_event
.dispatched());
2261 EXPECT_EQ(second_root
, first
->GetRootWindow());
2263 first
->RemovePreTargetHandler(&dispatch_event
);
2264 second
->RemovePreTargetHandler(&move_window
);
2267 class AlwaysMouseDownInputStateLookup
: public InputStateLookup
{
2269 AlwaysMouseDownInputStateLookup() {}
2270 ~AlwaysMouseDownInputStateLookup() override
{}
2273 // InputStateLookup:
2274 bool IsMouseButtonDown() const override
{ return true; }
2276 DISALLOW_COPY_AND_ASSIGN(AlwaysMouseDownInputStateLookup
);
2279 TEST_F(WindowEventDispatcherTest
,
2280 CursorVisibilityChangedWhileCaptureWindowInAnotherDispatcher
) {
2281 test::EventCountDelegate delegate
;
2282 scoped_ptr
<Window
> window(CreateTestWindowWithDelegate(&delegate
, 123,
2283 gfx::Rect(20, 10, 10, 20), root_window()));
2286 scoped_ptr
<WindowTreeHost
> second_host(
2287 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2288 second_host
->InitHost();
2289 WindowEventDispatcher
* second_dispatcher
= second_host
->dispatcher();
2291 // Install an InputStateLookup on the Env that always claims that a
2292 // mouse-button is down.
2293 test::EnvTestHelper(Env::GetInstance()).SetInputStateLookup(
2294 scoped_ptr
<InputStateLookup
>(new AlwaysMouseDownInputStateLookup()));
2296 window
->SetCapture();
2298 // Because the mouse button is down, setting the capture on |window| will set
2299 // it as the mouse-move handler for |root_window()|.
2300 EXPECT_EQ(window
.get(), host()->dispatcher()->mouse_moved_handler());
2302 // This does not set |window| as the mouse-move handler for the second
2304 EXPECT_EQ(NULL
, second_dispatcher
->mouse_moved_handler());
2306 // However, some capture-client updates the capture in each root-window on a
2307 // capture. Emulate that here. Because of this, the second dispatcher also has
2308 // |window| as the mouse-move handler.
2309 client::CaptureDelegate
* second_capture_delegate
= second_dispatcher
;
2310 second_capture_delegate
->UpdateCapture(NULL
, window
.get());
2311 EXPECT_EQ(window
.get(), second_dispatcher
->mouse_moved_handler());
2313 // Reset the mouse-event counts for |window|.
2314 delegate
.GetMouseMotionCountsAndReset();
2316 // Notify both hosts that the cursor is now hidden. This should send a single
2317 // mouse-exit event to |window|.
2318 host()->OnCursorVisibilityChanged(false);
2319 second_host
->OnCursorVisibilityChanged(false);
2320 EXPECT_EQ("0 0 1", delegate
.GetMouseMotionCountsAndReset());
2323 TEST_F(WindowEventDispatcherTest
,
2324 RedirectedEventToDifferentDispatcherLocation
) {
2325 scoped_ptr
<WindowTreeHost
> second_host(
2326 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2327 second_host
->InitHost();
2328 client::SetCaptureClient(second_host
->window(),
2329 client::GetCaptureClient(root_window()));
2331 test::EventCountDelegate delegate
;
2332 scoped_ptr
<Window
> window_first(CreateTestWindowWithDelegate(&delegate
, 123,
2333 gfx::Rect(20, 10, 10, 20), root_window()));
2334 window_first
->Show();
2336 scoped_ptr
<Window
> window_second(CreateTestWindowWithDelegate(&delegate
, 12,
2337 gfx::Rect(10, 10, 20, 30), second_host
->window()));
2338 window_second
->Show();
2340 window_second
->SetCapture();
2341 EXPECT_EQ(window_second
.get(),
2342 client::GetCaptureWindow(root_window()));
2344 // Send an event to the first host. Make sure it goes to |window_second| in
2345 // |second_host| instead (since it has capture).
2346 EventFilterRecorder recorder_first
;
2347 window_first
->AddPreTargetHandler(&recorder_first
);
2348 EventFilterRecorder recorder_second
;
2349 window_second
->AddPreTargetHandler(&recorder_second
);
2350 const gfx::Point
event_location(25, 15);
2351 ui::MouseEvent
mouse(ui::ET_MOUSE_PRESSED
, event_location
,
2352 event_location
, ui::EF_LEFT_MOUSE_BUTTON
,
2353 ui::EF_LEFT_MOUSE_BUTTON
);
2354 DispatchEventUsingWindowDispatcher(&mouse
);
2355 EXPECT_TRUE(recorder_first
.events().empty());
2356 ASSERT_EQ(1u, recorder_second
.events().size());
2357 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, recorder_second
.events()[0]);
2358 EXPECT_EQ(event_location
.ToString(),
2359 recorder_second
.mouse_locations()[0].ToString());
2362 class AsyncWindowDelegate
: public test::TestWindowDelegate
{
2364 AsyncWindowDelegate(WindowEventDispatcher
* dispatcher
)
2365 : dispatcher_(dispatcher
) {}
2367 void set_window(Window
* window
) {
2371 void OnTouchEvent(ui::TouchEvent
* event
) override
{
2372 // Convert touch event back to root window coordinates.
2373 event
->ConvertLocationToTarget(window_
, window_
->GetRootWindow());
2374 event
->DisableSynchronousHandling();
2375 dispatcher_
->ProcessedTouchEvent(event
, window_
, ui::ER_UNHANDLED
);
2376 event
->StopPropagation();
2379 WindowEventDispatcher
* dispatcher_
;
2382 DISALLOW_COPY_AND_ASSIGN(AsyncWindowDelegate
);
2385 // Tests that gesture events dispatched through the asynchronous flow have
2386 // co-ordinates in the right co-ordinate space.
2387 TEST_F(WindowEventDispatcherTest
, GestureEventCoordinates
) {
2388 const float kX
= 67.3f
;
2389 const float kY
= 97.8f
;
2391 const int kWindowOffset
= 50;
2392 EventFilterRecorder recorder
;
2393 root_window()->AddPreTargetHandler(&recorder
);
2394 AsyncWindowDelegate
delegate(host()->dispatcher());
2395 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
2396 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2399 gfx::Rect(kWindowOffset
, kWindowOffset
, 100, 100),
2401 window
->AddPreTargetHandler(&handler
);
2403 delegate
.set_window(window
.get());
2405 ui::TouchEvent
touch_pressed_event(
2406 ui::ET_TOUCH_PRESSED
, gfx::PointF(kX
, kY
), 0, ui::EventTimeForNow());
2408 DispatchEventUsingWindowDispatcher(&touch_pressed_event
);
2410 ASSERT_EQ(1u, recorder
.touch_locations().size());
2411 EXPECT_EQ(gfx::Point(kX
- kWindowOffset
, kY
- kWindowOffset
).ToString(),
2412 recorder
.touch_locations()[0].ToString());
2414 ASSERT_EQ(2u, recorder
.gesture_locations().size());
2415 EXPECT_EQ(gfx::Point(kX
- kWindowOffset
, kY
- kWindowOffset
).ToString(),
2416 recorder
.gesture_locations()[0].ToString());
2419 // Tests that a scroll-generating touch-event is marked as such.
2420 TEST_F(WindowEventDispatcherTest
, TouchMovesMarkedWhenCausingScroll
) {
2421 EventFilterRecorder recorder
;
2422 root_window()->AddPreTargetHandler(&recorder
);
2424 const gfx::Point
location(20, 20);
2425 ui::TouchEvent
press(
2426 ui::ET_TOUCH_PRESSED
, location
, 0, ui::EventTimeForNow());
2427 DispatchEventUsingWindowDispatcher(&press
);
2428 EXPECT_FALSE(recorder
.LastTouchMayCauseScrolling());
2429 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_PRESSED
));
2432 ui::TouchEvent
move(ui::ET_TOUCH_MOVED
,
2433 location
+ gfx::Vector2d(100, 100),
2435 ui::EventTimeForNow());
2436 DispatchEventUsingWindowDispatcher(&move
);
2437 EXPECT_TRUE(recorder
.LastTouchMayCauseScrolling());
2438 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
2439 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
2440 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE
));
2443 ui::TouchEvent
move2(ui::ET_TOUCH_MOVED
,
2444 location
+ gfx::Vector2d(200, 200),
2446 ui::EventTimeForNow());
2447 DispatchEventUsingWindowDispatcher(&move2
);
2448 EXPECT_TRUE(recorder
.LastTouchMayCauseScrolling());
2449 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
2450 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE
));
2453 // Delay the release to avoid fling generation.
2454 ui::TouchEvent
release(
2455 ui::ET_TOUCH_RELEASED
,
2456 location
+ gfx::Vector2dF(200, 200),
2458 ui::EventTimeForNow() + base::TimeDelta::FromSeconds(1));
2459 DispatchEventUsingWindowDispatcher(&release
);
2460 EXPECT_FALSE(recorder
.LastTouchMayCauseScrolling());
2461 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_RELEASED
));
2462 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_END
));
2464 root_window()->RemovePreTargetHandler(&recorder
);