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/point.h"
33 #include "ui/gfx/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
) {
455 const Events
& events() const { return events_
; }
457 const EventLocations
& mouse_locations() const { return mouse_locations_
; }
458 gfx::Point
mouse_location(int i
) const { return mouse_locations_
[i
]; }
459 const EventLocations
& touch_locations() const { return touch_locations_
; }
460 const EventLocations
& gesture_locations() const { return gesture_locations_
; }
461 const EventFlags
& mouse_event_flags() const { return mouse_event_flags_
; }
463 void WaitUntilReceivedEvent(ui::EventType type
) {
464 wait_until_event_
= type
;
465 run_loop_
.reset(new base::RunLoop());
469 Events
GetAndResetEvents() {
470 Events events
= events_
;
477 mouse_locations_
.clear();
478 touch_locations_
.clear();
479 gesture_locations_
.clear();
480 mouse_event_flags_
.clear();
483 // ui::EventHandler overrides:
484 void OnEvent(ui::Event
* event
) override
{
485 ui::EventHandler::OnEvent(event
);
486 events_
.push_back(event
->type());
487 if (wait_until_event_
== event
->type() && run_loop_
) {
489 wait_until_event_
= ui::ET_UNKNOWN
;
493 void OnMouseEvent(ui::MouseEvent
* event
) override
{
494 mouse_locations_
.push_back(event
->location());
495 mouse_event_flags_
.push_back(event
->flags());
498 void OnTouchEvent(ui::TouchEvent
* event
) override
{
499 touch_locations_
.push_back(event
->location());
502 void OnGestureEvent(ui::GestureEvent
* event
) override
{
503 gesture_locations_
.push_back(event
->location());
506 bool HasReceivedEvent(ui::EventType type
) {
507 return std::find(events_
.begin(), events_
.end(), type
) != events_
.end();
511 scoped_ptr
<base::RunLoop
> run_loop_
;
512 ui::EventType wait_until_event_
;
515 EventLocations mouse_locations_
;
516 EventLocations touch_locations_
;
517 EventLocations gesture_locations_
;
518 EventFlags mouse_event_flags_
;
520 DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder
);
523 // Converts an EventType to a string.
524 std::string
EventTypeToString(ui::EventType type
) {
526 case ui::ET_TOUCH_RELEASED
:
527 return "TOUCH_RELEASED";
529 case ui::ET_TOUCH_CANCELLED
:
530 return "TOUCH_CANCELLED";
532 case ui::ET_TOUCH_PRESSED
:
533 return "TOUCH_PRESSED";
535 case ui::ET_TOUCH_MOVED
:
536 return "TOUCH_MOVED";
538 case ui::ET_MOUSE_PRESSED
:
539 return "MOUSE_PRESSED";
541 case ui::ET_MOUSE_DRAGGED
:
542 return "MOUSE_DRAGGED";
544 case ui::ET_MOUSE_RELEASED
:
545 return "MOUSE_RELEASED";
547 case ui::ET_MOUSE_MOVED
:
548 return "MOUSE_MOVED";
550 case ui::ET_MOUSE_ENTERED
:
551 return "MOUSE_ENTERED";
553 case ui::ET_MOUSE_EXITED
:
554 return "MOUSE_EXITED";
556 case ui::ET_GESTURE_SCROLL_BEGIN
:
557 return "GESTURE_SCROLL_BEGIN";
559 case ui::ET_GESTURE_SCROLL_END
:
560 return "GESTURE_SCROLL_END";
562 case ui::ET_GESTURE_SCROLL_UPDATE
:
563 return "GESTURE_SCROLL_UPDATE";
565 case ui::ET_GESTURE_PINCH_BEGIN
:
566 return "GESTURE_PINCH_BEGIN";
568 case ui::ET_GESTURE_PINCH_END
:
569 return "GESTURE_PINCH_END";
571 case ui::ET_GESTURE_PINCH_UPDATE
:
572 return "GESTURE_PINCH_UPDATE";
574 case ui::ET_GESTURE_TAP
:
575 return "GESTURE_TAP";
577 case ui::ET_GESTURE_TAP_DOWN
:
578 return "GESTURE_TAP_DOWN";
580 case ui::ET_GESTURE_TAP_CANCEL
:
581 return "GESTURE_TAP_CANCEL";
583 case ui::ET_GESTURE_SHOW_PRESS
:
584 return "GESTURE_SHOW_PRESS";
586 case ui::ET_GESTURE_BEGIN
:
587 return "GESTURE_BEGIN";
589 case ui::ET_GESTURE_END
:
590 return "GESTURE_END";
593 // We should explicitly require each event type.
594 NOTREACHED() << "Received unexpected event: " << type
;
600 std::string
EventTypesToString(const EventFilterRecorder::Events
& events
) {
602 for (size_t i
= 0; i
< events
.size(); ++i
) {
605 result
+= EventTypeToString(events
[i
]);
612 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
613 #define MAYBE(x) DISABLED_##x
618 // Verifies a repost mouse event targets the window with capture (if there is
620 // Flaky on 32-bit Windows bots. http://crbug.com/388290
621 TEST_F(WindowEventDispatcherTest
, MAYBE(RepostTargetsCaptureWindow
)) {
622 // Set capture on |window| generate a mouse event (that is reposted) and not
623 // over |window| and verify |window| gets it (|window| gets it because it has
625 EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
626 EventFilterRecorder recorder
;
627 scoped_ptr
<Window
> window(CreateNormalWindow(1, root_window(), NULL
));
628 window
->SetBounds(gfx::Rect(20, 20, 40, 30));
629 window
->AddPreTargetHandler(&recorder
);
630 window
->SetCapture();
631 const ui::MouseEvent
press_event(
632 ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
633 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
634 host()->dispatcher()->RepostEvent(press_event
);
635 RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
636 // Mouse moves/enters may be generated. We only care about a pressed.
637 EXPECT_TRUE(EventTypesToString(recorder
.events()).find("MOUSE_PRESSED") !=
638 std::string::npos
) << EventTypesToString(recorder
.events());
641 TEST_F(WindowEventDispatcherTest
, MouseMovesHeld
) {
642 EventFilterRecorder recorder
;
643 root_window()->AddPreTargetHandler(&recorder
);
645 test::TestWindowDelegate delegate
;
646 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
647 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
649 ui::MouseEvent
mouse_move_event(ui::ET_MOUSE_MOVED
, gfx::Point(0, 0),
650 gfx::Point(0, 0), 0, 0);
651 DispatchEventUsingWindowDispatcher(&mouse_move_event
);
652 // Discard MOUSE_ENTER.
655 host()->dispatcher()->HoldPointerMoves();
657 // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
658 ui::MouseEvent
mouse_dragged_event(ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0),
659 gfx::Point(0, 0), 0, 0);
660 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
661 EXPECT_TRUE(recorder
.events().empty());
663 // Check that we do dispatch the held MOUSE_DRAGGED event before another type
665 ui::MouseEvent
mouse_pressed_event(ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0),
666 gfx::Point(0, 0), 0, 0);
667 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
668 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
669 EventTypesToString(recorder
.events()));
672 // Check that we coalesce held MOUSE_DRAGGED events. Note that here (and
673 // elsewhere in this test) we re-define each event prior to dispatch so that
674 // it has the correct state (phase, handled, target, etc.).
675 mouse_dragged_event
= ui::MouseEvent(
676 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
677 ui::MouseEvent
mouse_dragged_event2(ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10),
678 gfx::Point(10, 10), 0, 0);
679 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
680 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
681 EXPECT_TRUE(recorder
.events().empty());
682 mouse_pressed_event
= ui::MouseEvent(
683 ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
684 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
685 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
686 EventTypesToString(recorder
.events()));
689 // Check that on ReleasePointerMoves, held events are not dispatched
690 // immediately, but posted instead.
691 mouse_dragged_event
= ui::MouseEvent(
692 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
693 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
694 host()->dispatcher()->ReleasePointerMoves();
695 EXPECT_TRUE(recorder
.events().empty());
696 RunAllPendingInMessageLoop();
697 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder
.events()));
700 // However if another message comes in before the dispatch of the posted
701 // event, check that the posted event is dispatched before this new event.
702 host()->dispatcher()->HoldPointerMoves();
703 mouse_dragged_event
= ui::MouseEvent(
704 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
705 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
706 host()->dispatcher()->ReleasePointerMoves();
707 mouse_pressed_event
= ui::MouseEvent(
708 ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
709 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
710 EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
711 EventTypesToString(recorder
.events()));
713 RunAllPendingInMessageLoop();
714 EXPECT_TRUE(recorder
.events().empty());
716 // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
718 host()->dispatcher()->HoldPointerMoves();
719 mouse_dragged_event
= ui::MouseEvent(
720 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
721 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
722 host()->dispatcher()->ReleasePointerMoves();
723 mouse_dragged_event2
= ui::MouseEvent(
724 ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
725 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
726 EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder
.events()));
728 RunAllPendingInMessageLoop();
729 EXPECT_TRUE(recorder
.events().empty());
731 // Check that synthetic mouse move event has a right location when issued
732 // while holding pointer moves.
733 mouse_dragged_event
= ui::MouseEvent(
734 ui::ET_MOUSE_DRAGGED
, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
735 mouse_dragged_event2
= ui::MouseEvent(
736 ui::ET_MOUSE_DRAGGED
, gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
737 ui::MouseEvent
mouse_dragged_event3(ui::ET_MOUSE_DRAGGED
, gfx::Point(28, 28),
738 gfx::Point(28, 28), 0, 0);
739 host()->dispatcher()->HoldPointerMoves();
740 DispatchEventUsingWindowDispatcher(&mouse_dragged_event
);
741 DispatchEventUsingWindowDispatcher(&mouse_dragged_event2
);
742 window
->SetBounds(gfx::Rect(15, 15, 80, 80));
743 DispatchEventUsingWindowDispatcher(&mouse_dragged_event3
);
744 RunAllPendingInMessageLoop();
745 EXPECT_TRUE(recorder
.events().empty());
746 host()->dispatcher()->ReleasePointerMoves();
747 RunAllPendingInMessageLoop();
748 EXPECT_EQ("MOUSE_MOVED", EventTypesToString(recorder
.events()));
749 EXPECT_EQ(gfx::Point(13, 13), recorder
.mouse_location(0));
751 root_window()->RemovePreTargetHandler(&recorder
);
754 TEST_F(WindowEventDispatcherTest
, TouchMovesHeld
) {
755 EventFilterRecorder recorder
;
756 root_window()->AddPreTargetHandler(&recorder
);
758 test::TestWindowDelegate delegate
;
759 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
760 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
762 // Starting the touch and throwing out the first few events, since the system
763 // is going to generate synthetic mouse events that are not relevant to the
765 ui::TouchEvent
touch_pressed_event(
766 ui::ET_TOUCH_PRESSED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
767 DispatchEventUsingWindowDispatcher(&touch_pressed_event
);
768 recorder
.WaitUntilReceivedEvent(ui::ET_GESTURE_SHOW_PRESS
);
771 host()->dispatcher()->HoldPointerMoves();
773 // Check that we don't immediately dispatch the TOUCH_MOVED event.
774 ui::TouchEvent
touch_moved_event(
775 ui::ET_TOUCH_MOVED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
776 ui::TouchEvent
touch_moved_event2(
777 ui::ET_TOUCH_MOVED
, gfx::Point(11, 10), 0, ui::EventTimeForNow());
778 ui::TouchEvent
touch_moved_event3(
779 ui::ET_TOUCH_MOVED
, gfx::Point(12, 10), 0, ui::EventTimeForNow());
781 DispatchEventUsingWindowDispatcher(&touch_moved_event
);
782 EXPECT_TRUE(recorder
.events().empty());
784 // Check that on ReleasePointerMoves, held events are not dispatched
785 // immediately, but posted instead.
786 DispatchEventUsingWindowDispatcher(&touch_moved_event2
);
787 host()->dispatcher()->ReleasePointerMoves();
788 EXPECT_TRUE(recorder
.events().empty());
790 RunAllPendingInMessageLoop();
791 EXPECT_EQ("TOUCH_MOVED", EventTypesToString(recorder
.events()));
794 // If another touch event occurs then the held touch should be dispatched
795 // immediately before it.
796 ui::TouchEvent
touch_released_event(
797 ui::ET_TOUCH_RELEASED
, gfx::Point(10, 10), 0, ui::EventTimeForNow());
799 host()->dispatcher()->HoldPointerMoves();
800 DispatchEventUsingWindowDispatcher(&touch_moved_event3
);
801 DispatchEventUsingWindowDispatcher(&touch_released_event
);
802 EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP GESTURE_END",
803 EventTypesToString(recorder
.events()));
805 host()->dispatcher()->ReleasePointerMoves();
806 RunAllPendingInMessageLoop();
807 EXPECT_TRUE(recorder
.events().empty());
810 // Verifies that a direct call to ProcessedTouchEvent() with a
811 // TOUCH_PRESSED event does not cause a crash.
812 TEST_F(WindowEventDispatcherTest
, CallToProcessedTouchEvent
) {
813 test::TestWindowDelegate delegate
;
814 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
815 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
817 ui::TouchEvent
touch(
818 ui::ET_TOUCH_PRESSED
, gfx::Point(10, 10), 1, ui::EventTimeForNow());
819 host()->dispatcher()->ProcessedTouchEvent(
820 &touch
, window
.get(), ui::ER_UNHANDLED
);
823 // This event handler requests the dispatcher to start holding pointer-move
824 // events when it receives the first scroll-update gesture.
825 class HoldPointerOnScrollHandler
: public ui::test::TestEventHandler
{
827 HoldPointerOnScrollHandler(WindowEventDispatcher
* dispatcher
,
828 EventFilterRecorder
* filter
)
829 : dispatcher_(dispatcher
),
831 holding_moves_(false) {}
832 ~HoldPointerOnScrollHandler() override
{}
835 // ui::test::TestEventHandler:
836 void OnGestureEvent(ui::GestureEvent
* gesture
) override
{
837 if (!holding_moves_
&& gesture
->type() == ui::ET_GESTURE_SCROLL_UPDATE
) {
838 holding_moves_
= true;
839 dispatcher_
->HoldPointerMoves();
841 } else if (gesture
->type() == ui::ET_GESTURE_SCROLL_END
) {
842 dispatcher_
->ReleasePointerMoves();
843 holding_moves_
= false;
847 WindowEventDispatcher
* dispatcher_
;
848 EventFilterRecorder
* filter_
;
851 DISALLOW_COPY_AND_ASSIGN(HoldPointerOnScrollHandler
);
854 // Tests that touch-move events don't contribute to an in-progress scroll
855 // gesture if touch-move events are being held by the dispatcher.
856 TEST_F(WindowEventDispatcherTest
, TouchMovesHeldOnScroll
) {
857 EventFilterRecorder recorder
;
858 root_window()->AddPreTargetHandler(&recorder
);
859 test::TestWindowDelegate delegate
;
860 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
861 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
862 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
863 window
->AddPreTargetHandler(&handler
);
865 ui::test::EventGenerator
generator(root_window());
866 generator
.GestureScrollSequence(
867 gfx::Point(60, 60), gfx::Point(10, 60),
868 base::TimeDelta::FromMilliseconds(100), 25);
870 // |handler| will have reset |filter| and started holding the touch-move
871 // events when scrolling started. At the end of the scroll (i.e. upon
872 // touch-release), the held touch-move event will have been dispatched first,
873 // along with the subsequent events (i.e. touch-release, scroll-end, and
875 const EventFilterRecorder::Events
& events
= recorder
.events();
877 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
878 "GESTURE_SCROLL_END GESTURE_END",
879 EventTypesToString(events
));
880 ASSERT_EQ(2u, recorder
.touch_locations().size());
881 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
882 recorder
.touch_locations()[0].ToString());
883 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
884 recorder
.touch_locations()[1].ToString());
887 // Tests that a 'held' touch-event does contribute to gesture event when it is
889 TEST_F(WindowEventDispatcherTest
, HeldTouchMoveContributesToGesture
) {
890 EventFilterRecorder recorder
;
891 root_window()->AddPreTargetHandler(&recorder
);
893 const gfx::Point
location(20, 20);
894 ui::TouchEvent
press(
895 ui::ET_TOUCH_PRESSED
, location
, 0, ui::EventTimeForNow());
896 DispatchEventUsingWindowDispatcher(&press
);
897 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_PRESSED
));
900 host()->dispatcher()->HoldPointerMoves();
902 ui::TouchEvent
move(ui::ET_TOUCH_MOVED
,
903 location
+ gfx::Vector2d(100, 100),
905 ui::EventTimeForNow());
906 DispatchEventUsingWindowDispatcher(&move
);
907 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
908 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
911 host()->dispatcher()->ReleasePointerMoves();
912 EXPECT_FALSE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
913 RunAllPendingInMessageLoop();
914 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_TOUCH_MOVED
));
915 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN
));
916 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE
));
918 root_window()->RemovePreTargetHandler(&recorder
);
921 // Tests that synthetic mouse events are ignored when mouse
922 // events are disabled.
923 TEST_F(WindowEventDispatcherTest
, DispatchSyntheticMouseEvents
) {
924 EventFilterRecorder recorder
;
925 root_window()->AddPreTargetHandler(&recorder
);
927 test::TestWindowDelegate delegate
;
928 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
929 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
931 window
->SetCapture();
933 test::TestCursorClient
cursor_client(root_window());
935 // Dispatch a non-synthetic mouse event when mouse events are enabled.
936 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
937 gfx::Point(10, 10), 0, 0);
938 DispatchEventUsingWindowDispatcher(&mouse1
);
939 EXPECT_FALSE(recorder
.events().empty());
942 // Dispatch a synthetic mouse event when mouse events are enabled.
943 ui::MouseEvent
mouse2(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
944 gfx::Point(10, 10), ui::EF_IS_SYNTHESIZED
, 0);
945 DispatchEventUsingWindowDispatcher(&mouse2
);
946 EXPECT_FALSE(recorder
.events().empty());
949 // Dispatch a synthetic mouse event when mouse events are disabled.
950 cursor_client
.DisableMouseEvents();
951 DispatchEventUsingWindowDispatcher(&mouse2
);
952 EXPECT_TRUE(recorder
.events().empty());
953 root_window()->RemovePreTargetHandler(&recorder
);
956 // Tests that a mouse-move event is not synthesized when a mouse-button is down.
957 TEST_F(WindowEventDispatcherTest
, DoNotSynthesizeWhileButtonDown
) {
958 EventFilterRecorder recorder
;
959 test::TestWindowDelegate delegate
;
960 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
961 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
964 window
->AddPreTargetHandler(&recorder
);
965 // Dispatch a non-synthetic mouse event when mouse events are enabled.
966 ui::MouseEvent
mouse1(ui::ET_MOUSE_PRESSED
, gfx::Point(10, 10),
967 gfx::Point(10, 10), ui::EF_LEFT_MOUSE_BUTTON
,
968 ui::EF_LEFT_MOUSE_BUTTON
);
969 DispatchEventUsingWindowDispatcher(&mouse1
);
970 ASSERT_EQ(1u, recorder
.events().size());
971 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, recorder
.events()[0]);
972 window
->RemovePreTargetHandler(&recorder
);
975 // Move |window| away from underneath the cursor.
976 root_window()->AddPreTargetHandler(&recorder
);
977 window
->SetBounds(gfx::Rect(30, 30, 100, 100));
978 EXPECT_TRUE(recorder
.events().empty());
979 RunAllPendingInMessageLoop();
980 EXPECT_TRUE(recorder
.events().empty());
981 root_window()->RemovePreTargetHandler(&recorder
);
984 #if defined(OS_WIN) && defined(ARCH_CPU_X86)
985 #define MAYBE(x) DISABLED_##x
990 // Tests synthetic mouse events generated when window bounds changes such that
991 // the cursor previously outside the window becomes inside, or vice versa.
992 // Do not synthesize events if the window ignores events or is invisible.
993 // Flaky on 32-bit Windows bots. http://crbug.com/388272
994 TEST_F(WindowEventDispatcherTest
,
995 MAYBE(SynthesizeMouseEventsOnWindowBoundsChanged
)) {
996 test::TestWindowDelegate delegate
;
997 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
998 &delegate
, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
1000 window
->SetCapture();
1002 EventFilterRecorder recorder
;
1003 window
->AddPreTargetHandler(&recorder
);
1005 // Dispatch a non-synthetic mouse event to place cursor inside window bounds.
1006 ui::MouseEvent
mouse(ui::ET_MOUSE_MOVED
, gfx::Point(10, 10),
1007 gfx::Point(10, 10), 0, 0);
1008 DispatchEventUsingWindowDispatcher(&mouse
);
1009 EXPECT_FALSE(recorder
.events().empty());
1012 // Update the window bounds so that cursor is now outside the window.
1013 // This should trigger a synthetic MOVED event.
1014 gfx::Rect
bounds1(20, 20, 100, 100);
1015 window
->SetBounds(bounds1
);
1016 RunAllPendingInMessageLoop();
1017 ASSERT_FALSE(recorder
.events().empty());
1018 ASSERT_FALSE(recorder
.mouse_event_flags().empty());
1019 EXPECT_EQ(ui::ET_MOUSE_MOVED
, recorder
.events().back());
1020 EXPECT_EQ(ui::EF_IS_SYNTHESIZED
, recorder
.mouse_event_flags().back());
1023 // Set window to ignore events.
1024 window
->set_ignore_events(true);
1026 // Update the window bounds so that cursor is back inside the window.
1027 // This should not trigger a synthetic event.
1028 gfx::Rect
bounds2(5, 5, 100, 100);
1029 window
->SetBounds(bounds2
);
1030 RunAllPendingInMessageLoop();
1031 EXPECT_TRUE(recorder
.events().empty());
1034 // Set window to accept events but invisible.
1035 window
->set_ignore_events(false);
1039 // Update the window bounds so that cursor is outside the window.
1040 // This should not trigger a synthetic event.
1041 window
->SetBounds(bounds1
);
1042 RunAllPendingInMessageLoop();
1043 EXPECT_TRUE(recorder
.events().empty());
1046 // Tests that a mouse exit is dispatched to the last known cursor location
1047 // when the cursor becomes invisible.
1048 TEST_F(WindowEventDispatcherTest
, DispatchMouseExitWhenCursorHidden
) {
1049 EventFilterRecorder recorder
;
1050 root_window()->AddPreTargetHandler(&recorder
);
1052 test::TestWindowDelegate delegate
;
1053 gfx::Point
window_origin(7, 18);
1054 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1055 &delegate
, 1234, gfx::Rect(window_origin
, gfx::Size(100, 100)),
1059 // Dispatch a mouse move event into the window.
1060 gfx::Point
mouse_location(gfx::Point(15, 25));
1061 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, mouse_location
,
1062 mouse_location
, 0, 0);
1063 EXPECT_TRUE(recorder
.events().empty());
1064 DispatchEventUsingWindowDispatcher(&mouse1
);
1065 EXPECT_FALSE(recorder
.events().empty());
1068 // Hide the cursor and verify a mouse exit was dispatched.
1069 host()->OnCursorVisibilityChanged(false);
1070 EXPECT_FALSE(recorder
.events().empty());
1071 EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder
.events()));
1073 // Verify the mouse exit was dispatched at the correct location
1074 // (in the correct coordinate space).
1075 int translated_x
= mouse_location
.x() - window_origin
.x();
1076 int translated_y
= mouse_location
.y() - window_origin
.y();
1077 gfx::Point
translated_point(translated_x
, translated_y
);
1078 EXPECT_EQ(recorder
.mouse_location(0).ToString(), translated_point
.ToString());
1079 root_window()->RemovePreTargetHandler(&recorder
);
1082 // Tests that a synthetic mouse exit is dispatched to the last known cursor
1083 // location after mouse events are disabled on the cursor client.
1084 TEST_F(WindowEventDispatcherTest
,
1085 DispatchSyntheticMouseExitAfterMouseEventsDisabled
) {
1086 EventFilterRecorder recorder
;
1087 root_window()->AddPreTargetHandler(&recorder
);
1089 test::TestWindowDelegate delegate
;
1090 gfx::Point
window_origin(7, 18);
1091 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1092 &delegate
, 1234, gfx::Rect(window_origin
, gfx::Size(100, 100)),
1096 // Dispatch a mouse move event into the window.
1097 gfx::Point
mouse_location(gfx::Point(15, 25));
1098 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, mouse_location
,
1099 mouse_location
, 0, 0);
1100 EXPECT_TRUE(recorder
.events().empty());
1101 DispatchEventUsingWindowDispatcher(&mouse1
);
1102 EXPECT_FALSE(recorder
.events().empty());
1105 test::TestCursorClient
cursor_client(root_window());
1106 cursor_client
.DisableMouseEvents();
1108 gfx::Point
mouse_exit_location(gfx::Point(150, 150));
1109 ui::MouseEvent
mouse2(ui::ET_MOUSE_EXITED
, gfx::Point(150, 150),
1110 gfx::Point(150, 150), ui::EF_IS_SYNTHESIZED
, 0);
1111 DispatchEventUsingWindowDispatcher(&mouse2
);
1113 EXPECT_FALSE(recorder
.events().empty());
1114 // We get the mouse exited event twice in our filter. Once during the
1115 // predispatch phase and during the actual dispatch.
1116 EXPECT_EQ("MOUSE_EXITED MOUSE_EXITED", EventTypesToString(recorder
.events()));
1118 // Verify the mouse exit was dispatched at the correct location
1119 // (in the correct coordinate space).
1120 int translated_x
= mouse_exit_location
.x() - window_origin
.x();
1121 int translated_y
= mouse_exit_location
.y() - window_origin
.y();
1122 gfx::Point
translated_point(translated_x
, translated_y
);
1123 EXPECT_EQ(recorder
.mouse_location(0).ToString(), translated_point
.ToString());
1124 root_window()->RemovePreTargetHandler(&recorder
);
1127 class DeletingEventFilter
: public ui::EventHandler
{
1129 DeletingEventFilter()
1130 : delete_during_pre_handle_(false) {}
1131 ~DeletingEventFilter() override
{}
1133 void Reset(bool delete_during_pre_handle
) {
1134 delete_during_pre_handle_
= delete_during_pre_handle
;
1138 // Overridden from ui::EventHandler:
1139 void OnKeyEvent(ui::KeyEvent
* event
) override
{
1140 if (delete_during_pre_handle_
)
1141 delete event
->target();
1144 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1145 if (delete_during_pre_handle_
)
1146 delete event
->target();
1149 bool delete_during_pre_handle_
;
1151 DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter
);
1154 class DeletingWindowDelegate
: public test::TestWindowDelegate
{
1156 DeletingWindowDelegate()
1158 delete_during_handle_(false),
1159 got_event_(false) {}
1160 ~DeletingWindowDelegate() override
{}
1162 void Reset(Window
* window
, bool delete_during_handle
) {
1164 delete_during_handle_
= delete_during_handle
;
1167 bool got_event() const { return got_event_
; }
1170 // Overridden from WindowDelegate:
1171 void OnKeyEvent(ui::KeyEvent
* event
) override
{
1172 if (delete_during_handle_
)
1177 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1178 if (delete_during_handle_
)
1184 bool delete_during_handle_
;
1187 DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate
);
1190 TEST_F(WindowEventDispatcherTest
, DeleteWindowDuringDispatch
) {
1191 // Verifies that we can delete a window during each phase of event handling.
1192 // Deleting the window should not cause a crash, only prevent further
1193 // processing from occurring.
1194 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1195 DeletingWindowDelegate d11
;
1196 Window
* w11
= CreateNormalWindow(11, w1
.get(), &d11
);
1197 WindowTracker tracker
;
1198 DeletingEventFilter w1_filter
;
1199 w1
->AddPreTargetHandler(&w1_filter
);
1200 client::GetFocusClient(w1
.get())->FocusWindow(w11
);
1202 ui::test::EventGenerator
generator(root_window(), w11
);
1204 // First up, no one deletes anything.
1206 d11
.Reset(w11
, false);
1208 generator
.PressLeftButton();
1209 EXPECT_TRUE(tracker
.Contains(w11
));
1210 EXPECT_TRUE(d11
.got_event());
1211 generator
.ReleaseLeftButton();
1213 // Delegate deletes w11. This will prevent the post-handle step from applying.
1214 w1_filter
.Reset(false);
1215 d11
.Reset(w11
, true);
1216 generator
.PressKey(ui::VKEY_A
, 0);
1217 EXPECT_FALSE(tracker
.Contains(w11
));
1218 EXPECT_TRUE(d11
.got_event());
1220 // Pre-handle step deletes w11. This will prevent the delegate and the post-
1221 // handle steps from applying.
1222 w11
= CreateNormalWindow(11, w1
.get(), &d11
);
1223 w1_filter
.Reset(true);
1224 d11
.Reset(w11
, false);
1225 generator
.PressLeftButton();
1226 EXPECT_FALSE(tracker
.Contains(w11
));
1227 EXPECT_FALSE(d11
.got_event());
1232 // A window delegate that detaches the parent of the target's parent window when
1233 // it receives a tap event.
1234 class DetachesParentOnTapDelegate
: public test::TestWindowDelegate
{
1236 DetachesParentOnTapDelegate() {}
1237 ~DetachesParentOnTapDelegate() override
{}
1240 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1241 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
1242 event
->SetHandled();
1246 if (event
->type() == ui::ET_GESTURE_TAP
) {
1247 Window
* parent
= static_cast<Window
*>(event
->target())->parent();
1248 parent
->parent()->RemoveChild(parent
);
1249 event
->SetHandled();
1253 DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate
);
1258 // Tests that the gesture recognizer is reset for all child windows when a
1259 // window hides. No expectations, just checks that the test does not crash.
1260 TEST_F(WindowEventDispatcherTest
,
1261 GestureRecognizerResetsTargetWhenParentHides
) {
1262 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1263 DetachesParentOnTapDelegate delegate
;
1264 scoped_ptr
<Window
> parent(CreateNormalWindow(22, w1
.get(), NULL
));
1265 Window
* child
= CreateNormalWindow(11, parent
.get(), &delegate
);
1266 ui::test::EventGenerator
generator(root_window(), child
);
1267 generator
.GestureTapAt(gfx::Point(40, 40));
1272 // A window delegate that processes nested gestures on tap.
1273 class NestedGestureDelegate
: public test::TestWindowDelegate
{
1275 NestedGestureDelegate(ui::test::EventGenerator
* generator
,
1276 const gfx::Point tap_location
)
1277 : generator_(generator
),
1278 tap_location_(tap_location
),
1279 gesture_end_count_(0) {}
1280 ~NestedGestureDelegate() override
{}
1282 int gesture_end_count() const { return gesture_end_count_
; }
1285 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1286 switch (event
->type()) {
1287 case ui::ET_GESTURE_TAP_DOWN
:
1288 event
->SetHandled();
1290 case ui::ET_GESTURE_TAP
:
1292 generator_
->GestureTapAt(tap_location_
);
1293 event
->SetHandled();
1295 case ui::ET_GESTURE_END
:
1296 ++gesture_end_count_
;
1303 ui::test::EventGenerator
* generator_
;
1304 const gfx::Point tap_location_
;
1305 int gesture_end_count_
;
1306 DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate
);
1311 // Tests that gesture end is delivered after nested gesture processing.
1312 TEST_F(WindowEventDispatcherTest
, GestureEndDeliveredAfterNestedGestures
) {
1313 NestedGestureDelegate
d1(NULL
, gfx::Point());
1314 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &d1
));
1315 w1
->SetBounds(gfx::Rect(0, 0, 100, 100));
1317 ui::test::EventGenerator
nested_generator(root_window(), w1
.get());
1318 NestedGestureDelegate
d2(&nested_generator
, w1
->bounds().CenterPoint());
1319 scoped_ptr
<Window
> w2(CreateNormalWindow(1, root_window(), &d2
));
1320 w2
->SetBounds(gfx::Rect(100, 0, 100, 100));
1322 // Tap on w2 which triggers nested gestures for w1.
1323 ui::test::EventGenerator
generator(root_window(), w2
.get());
1324 generator
.GestureTapAt(w2
->bounds().CenterPoint());
1326 // Both windows should get their gesture end events.
1327 EXPECT_EQ(1, d1
.gesture_end_count());
1328 EXPECT_EQ(1, d2
.gesture_end_count());
1331 // Tests whether we can repost the Tap down gesture event.
1332 TEST_F(WindowEventDispatcherTest
, RepostTapdownGestureTest
) {
1333 EventFilterRecorder recorder
;
1334 root_window()->AddPreTargetHandler(&recorder
);
1336 test::TestWindowDelegate delegate
;
1337 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1338 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1340 ui::GestureEventDetails
details(ui::ET_GESTURE_TAP_DOWN
);
1341 gfx::Point
point(10, 10);
1342 ui::GestureEvent
event(point
.x(),
1345 ui::EventTimeForNow(),
1347 host()->dispatcher()->RepostEvent(event
);
1348 RunAllPendingInMessageLoop();
1349 // TODO(rbyers): Currently disabled - crbug.com/170987
1350 EXPECT_FALSE(EventTypesToString(recorder
.events()).find("GESTURE_TAP_DOWN") !=
1353 root_window()->RemovePreTargetHandler(&recorder
);
1356 // This class inherits from the EventFilterRecorder class which provides a
1357 // facility to record events. This class additionally provides a facility to
1358 // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
1359 // events after that.
1360 class RepostGestureEventRecorder
: public EventFilterRecorder
{
1362 RepostGestureEventRecorder(aura::Window
* repost_source
,
1363 aura::Window
* repost_target
)
1364 : repost_source_(repost_source
),
1365 repost_target_(repost_target
),
1367 done_cleanup_(false) {}
1369 ~RepostGestureEventRecorder() override
{}
1371 void OnTouchEvent(ui::TouchEvent
* event
) override
{
1372 if (reposted_
&& event
->type() == ui::ET_TOUCH_PRESSED
) {
1373 done_cleanup_
= true;
1376 EventFilterRecorder::OnTouchEvent(event
);
1379 void OnGestureEvent(ui::GestureEvent
* event
) override
{
1380 EXPECT_EQ(done_cleanup_
? repost_target_
: repost_source_
, event
->target());
1381 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
1383 EXPECT_NE(repost_target_
, event
->target());
1385 repost_target_
->GetHost()->dispatcher()->RepostEvent(*event
);
1386 // Ensure that the reposted gesture event above goes to the
1388 repost_source_
->GetRootWindow()->RemoveChild(repost_source_
);
1392 EventFilterRecorder::OnGestureEvent(event
);
1395 // Ignore mouse events as they don't fire at all times. This causes
1396 // the GestureRepostEventOrder test to fail randomly.
1397 void OnMouseEvent(ui::MouseEvent
* event
) override
{}
1400 aura::Window
* repost_source_
;
1401 aura::Window
* repost_target_
;
1402 // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
1404 // set true if we're done cleaning up after hiding repost_source_;
1406 DISALLOW_COPY_AND_ASSIGN(RepostGestureEventRecorder
);
1409 // Tests whether events which are generated after the reposted gesture event
1410 // are received after that. In this case the scroll sequence events should
1411 // be received after the reposted gesture event.
1412 TEST_F(WindowEventDispatcherTest
, GestureRepostEventOrder
) {
1413 // Expected events at the end for the repost_target window defined below.
1414 const char kExpectedTargetEvents
[] =
1415 // TODO)(rbyers): Gesture event reposting is disabled - crbug.com/279039.
1416 // "GESTURE_BEGIN GESTURE_TAP_DOWN "
1417 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1418 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE TOUCH_MOVED "
1419 "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
1420 "GESTURE_SCROLL_END GESTURE_END";
1421 // We create two windows.
1422 // The first window (repost_source) is the one to which the initial tap
1423 // gesture is sent. It reposts this event to the second window
1425 // We then generate the scroll sequence for repost_target and look for two
1426 // ET_GESTURE_TAP_DOWN events in the event list at the end.
1427 test::TestWindowDelegate delegate
;
1428 scoped_ptr
<aura::Window
> repost_target(CreateTestWindowWithDelegate(
1429 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1431 scoped_ptr
<aura::Window
> repost_source(CreateTestWindowWithDelegate(
1432 &delegate
, 1, gfx::Rect(0, 0, 50, 50), root_window()));
1434 RepostGestureEventRecorder
repost_event_recorder(repost_source
.get(),
1435 repost_target
.get());
1436 root_window()->AddPreTargetHandler(&repost_event_recorder
);
1438 // Generate a tap down gesture for the repost_source. This will be reposted
1439 // to repost_target.
1440 ui::test::EventGenerator
repost_generator(root_window(), repost_source
.get());
1441 repost_generator
.GestureTapAt(gfx::Point(40, 40));
1442 RunAllPendingInMessageLoop();
1444 ui::test::EventGenerator
scroll_generator(root_window(), repost_target
.get());
1445 scroll_generator
.GestureScrollSequence(
1447 gfx::Point(100, 100),
1448 base::TimeDelta::FromMilliseconds(100),
1450 RunAllPendingInMessageLoop();
1452 int tap_down_count
= 0;
1453 for (size_t i
= 0; i
< repost_event_recorder
.events().size(); ++i
) {
1454 if (repost_event_recorder
.events()[i
] == ui::ET_GESTURE_TAP_DOWN
)
1458 // We expect two tap down events. One from the repost and the other one from
1459 // the scroll sequence posted above.
1460 // TODO(rbyers): Currently disabled - crbug.com/170987
1461 EXPECT_EQ(1, tap_down_count
);
1463 EXPECT_EQ(kExpectedTargetEvents
,
1464 EventTypesToString(repost_event_recorder
.events()));
1465 root_window()->RemovePreTargetHandler(&repost_event_recorder
);
1468 class OnMouseExitDeletingEventFilter
: public EventFilterRecorder
{
1470 OnMouseExitDeletingEventFilter() : window_to_delete_(NULL
) {}
1471 ~OnMouseExitDeletingEventFilter() override
{}
1473 void set_window_to_delete(Window
* window_to_delete
) {
1474 window_to_delete_
= window_to_delete
;
1478 // Overridden from ui::EventHandler:
1479 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1480 EventFilterRecorder::OnMouseEvent(event
);
1481 if (window_to_delete_
) {
1482 delete window_to_delete_
;
1483 window_to_delete_
= NULL
;
1487 Window
* window_to_delete_
;
1489 DISALLOW_COPY_AND_ASSIGN(OnMouseExitDeletingEventFilter
);
1492 // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
1493 // a child, but the child is destroyed because of the synthesized mouse-exit
1494 // event generated on the previous mouse_moved_handler_.
1495 TEST_F(WindowEventDispatcherTest
, DeleteWindowDuringMouseMovedDispatch
) {
1496 // Create window 1 and set its event filter. Window 1 will take ownership of
1497 // the event filter.
1498 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
1499 OnMouseExitDeletingEventFilter w1_filter
;
1500 w1
->AddPreTargetHandler(&w1_filter
);
1501 w1
->SetBounds(gfx::Rect(20, 20, 60, 60));
1502 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_moved_handler());
1504 ui::test::EventGenerator
generator(root_window(), w1
.get());
1506 // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
1508 generator
.MoveMouseTo(51, 51);
1509 EXPECT_EQ(w1
.get(), host()->dispatcher()->mouse_moved_handler());
1511 // Create window 2 under the mouse cursor and stack it above window 1.
1512 Window
* w2
= CreateNormalWindow(2, root_window(), NULL
);
1513 w2
->SetBounds(gfx::Rect(30, 30, 40, 40));
1514 root_window()->StackChildAbove(w2
, w1
.get());
1516 // Set window 2 as the window that is to be deleted when a mouse-exited event
1517 // happens on window 1.
1518 w1_filter
.set_window_to_delete(w2
);
1520 // Move mosue over window 2. This should generate a mouse-exited event for
1521 // window 1 resulting in deletion of window 2. The original mouse-moved event
1522 // that was targeted to window 2 should be dropped since window 2 is
1523 // destroyed. This test passes if no crash happens.
1524 generator
.MoveMouseTo(52, 52);
1525 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_moved_handler());
1527 // Check events received by window 1.
1528 EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
1529 EventTypesToString(w1_filter
.events()));
1534 // Used to track if OnWindowDestroying() is invoked and if there is a valid
1535 // RootWindow at such time.
1536 class ValidRootDuringDestructionWindowObserver
: public aura::WindowObserver
{
1538 ValidRootDuringDestructionWindowObserver(bool* got_destroying
,
1539 bool* has_valid_root
)
1540 : got_destroying_(got_destroying
),
1541 has_valid_root_(has_valid_root
) {
1545 void OnWindowDestroying(aura::Window
* window
) override
{
1546 *got_destroying_
= true;
1547 *has_valid_root_
= (window
->GetRootWindow() != NULL
);
1551 bool* got_destroying_
;
1552 bool* has_valid_root_
;
1554 DISALLOW_COPY_AND_ASSIGN(ValidRootDuringDestructionWindowObserver
);
1559 // Verifies GetRootWindow() from ~Window returns a valid root.
1560 TEST_F(WindowEventDispatcherTest
, ValidRootDuringDestruction
) {
1561 bool got_destroying
= false;
1562 bool has_valid_root
= false;
1563 ValidRootDuringDestructionWindowObserver
observer(&got_destroying
,
1566 scoped_ptr
<WindowTreeHost
> host(
1567 WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100)));
1569 // Owned by WindowEventDispatcher.
1570 Window
* w1
= CreateNormalWindow(1, host
->window(), NULL
);
1571 w1
->AddObserver(&observer
);
1573 EXPECT_TRUE(got_destroying
);
1574 EXPECT_TRUE(has_valid_root
);
1579 // See description above DontResetHeldEvent for details.
1580 class DontResetHeldEventWindowDelegate
: public test::TestWindowDelegate
{
1582 explicit DontResetHeldEventWindowDelegate(aura::Window
* root
)
1584 mouse_event_count_(0) {}
1585 ~DontResetHeldEventWindowDelegate() override
{}
1587 int mouse_event_count() const { return mouse_event_count_
; }
1589 // TestWindowDelegate:
1590 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1591 if ((event
->flags() & ui::EF_SHIFT_DOWN
) != 0 &&
1592 mouse_event_count_
++ == 0) {
1593 ui::MouseEvent
mouse_event(ui::ET_MOUSE_PRESSED
,
1594 gfx::Point(10, 10), gfx::Point(10, 10),
1595 ui::EF_SHIFT_DOWN
, 0);
1596 root_
->GetHost()->dispatcher()->RepostEvent(mouse_event
);
1602 int mouse_event_count_
;
1604 DISALLOW_COPY_AND_ASSIGN(DontResetHeldEventWindowDelegate
);
1609 // Verifies RootWindow doesn't reset |RootWindow::held_repostable_event_| after
1610 // dispatching. This is done by using DontResetHeldEventWindowDelegate, which
1611 // tracks the number of events with ui::EF_SHIFT_DOWN set (all reposted events
1612 // have EF_SHIFT_DOWN). When the first event is seen RepostEvent() is used to
1613 // schedule another reposted event.
1614 TEST_F(WindowEventDispatcherTest
, DontResetHeldEvent
) {
1615 DontResetHeldEventWindowDelegate
delegate(root_window());
1616 scoped_ptr
<Window
> w1(CreateNormalWindow(1, root_window(), &delegate
));
1617 w1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1618 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
1619 gfx::Point(10, 10), gfx::Point(10, 10),
1620 ui::EF_SHIFT_DOWN
, 0);
1621 root_window()->GetHost()->dispatcher()->RepostEvent(pressed
);
1622 ui::MouseEvent
pressed2(ui::ET_MOUSE_PRESSED
,
1623 gfx::Point(10, 10), gfx::Point(10, 10), 0, 0);
1624 // Dispatch an event to flush event scheduled by way of RepostEvent().
1625 DispatchEventUsingWindowDispatcher(&pressed2
);
1626 // Delegate should have seen reposted event (identified by way of
1627 // EF_SHIFT_DOWN). Dispatch another event to flush the second
1629 EXPECT_EQ(1, delegate
.mouse_event_count());
1630 DispatchEventUsingWindowDispatcher(&pressed2
);
1631 EXPECT_EQ(2, delegate
.mouse_event_count());
1636 // See description above DeleteHostFromHeldMouseEvent for details.
1637 class DeleteHostFromHeldMouseEventDelegate
1638 : public test::TestWindowDelegate
{
1640 explicit DeleteHostFromHeldMouseEventDelegate(WindowTreeHost
* host
)
1642 got_mouse_event_(false),
1643 got_destroy_(false) {
1645 ~DeleteHostFromHeldMouseEventDelegate() override
{}
1647 bool got_mouse_event() const { return got_mouse_event_
; }
1648 bool got_destroy() const { return got_destroy_
; }
1650 // TestWindowDelegate:
1651 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1652 if ((event
->flags() & ui::EF_SHIFT_DOWN
) != 0) {
1653 got_mouse_event_
= true;
1657 void OnWindowDestroyed(Window
* window
) override
{ got_destroy_
= true; }
1660 WindowTreeHost
* host_
;
1661 bool got_mouse_event_
;
1664 DISALLOW_COPY_AND_ASSIGN(DeleteHostFromHeldMouseEventDelegate
);
1669 // Verifies if a WindowTreeHost is deleted from dispatching a held mouse event
1671 TEST_F(WindowEventDispatcherTest
, DeleteHostFromHeldMouseEvent
) {
1672 // Should be deleted by |delegate|.
1673 WindowTreeHost
* h2
= WindowTreeHost::Create(gfx::Rect(0, 0, 100, 100));
1675 DeleteHostFromHeldMouseEventDelegate
delegate(h2
);
1677 Window
* w1
= CreateNormalWindow(1, h2
->window(), &delegate
);
1678 w1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1679 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
1680 gfx::Point(10, 10), gfx::Point(10, 10),
1681 ui::EF_SHIFT_DOWN
, 0);
1682 h2
->dispatcher()->RepostEvent(pressed
);
1683 // RunAllPendingInMessageLoop() to make sure the |pressed| is run.
1684 RunAllPendingInMessageLoop();
1685 EXPECT_TRUE(delegate
.got_mouse_event());
1686 EXPECT_TRUE(delegate
.got_destroy());
1689 TEST_F(WindowEventDispatcherTest
, WindowHideCancelsActiveTouches
) {
1690 EventFilterRecorder recorder
;
1691 root_window()->AddPreTargetHandler(&recorder
);
1693 test::TestWindowDelegate delegate
;
1694 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1695 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1697 gfx::Point position1
= root_window()->bounds().origin();
1698 ui::TouchEvent
press(
1699 ui::ET_TOUCH_PRESSED
, position1
, 0, ui::EventTimeForNow());
1700 DispatchEventUsingWindowDispatcher(&press
);
1702 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN",
1703 EventTypesToString(recorder
.GetAndResetEvents()));
1707 EXPECT_EQ(ui::ET_TOUCH_CANCELLED
, recorder
.events()[0]);
1708 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_TAP_CANCEL
));
1709 EXPECT_TRUE(recorder
.HasReceivedEvent(ui::ET_GESTURE_END
));
1710 EXPECT_EQ(3U, recorder
.events().size());
1711 root_window()->RemovePreTargetHandler(&recorder
);
1714 TEST_F(WindowEventDispatcherTest
, WindowHideCancelsActiveGestures
) {
1715 EventFilterRecorder recorder
;
1716 root_window()->AddPreTargetHandler(&recorder
);
1718 test::TestWindowDelegate delegate
;
1719 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1720 &delegate
, 1, gfx::Rect(0, 0, 100, 100), root_window()));
1722 gfx::Point position1
= root_window()->bounds().origin();
1723 gfx::Point position2
= root_window()->bounds().CenterPoint();
1724 ui::TouchEvent
press(
1725 ui::ET_TOUCH_PRESSED
, position1
, 0, ui::EventTimeForNow());
1726 DispatchEventUsingWindowDispatcher(&press
);
1728 ui::TouchEvent
move(
1729 ui::ET_TOUCH_MOVED
, position2
, 0, ui::EventTimeForNow());
1730 DispatchEventUsingWindowDispatcher(&move
);
1732 ui::TouchEvent
press2(
1733 ui::ET_TOUCH_PRESSED
, position1
, 1, ui::EventTimeForNow());
1734 DispatchEventUsingWindowDispatcher(&press2
);
1736 // TODO(tdresser): once the unified Gesture Recognizer has stuck, remove the
1737 // special casing here. See crbug.com/332418 for details.
1738 std::string expected
=
1739 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1740 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1741 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_PINCH_BEGIN";
1743 std::string expected_ugr
=
1744 "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1745 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1746 "TOUCH_PRESSED GESTURE_BEGIN";
1748 std::string events_string
= EventTypesToString(recorder
.GetAndResetEvents());
1749 EXPECT_TRUE((expected
== events_string
) || (expected_ugr
== events_string
));
1754 "TOUCH_CANCELLED GESTURE_PINCH_END GESTURE_END TOUCH_CANCELLED "
1755 "GESTURE_SCROLL_END GESTURE_END";
1757 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END TOUCH_CANCELLED "
1760 events_string
= EventTypesToString(recorder
.GetAndResetEvents());
1761 EXPECT_TRUE((expected
== events_string
) || (expected_ugr
== events_string
));
1763 root_window()->RemovePreTargetHandler(&recorder
);
1766 // Places two windows side by side. Presses down on one window, and starts a
1767 // scroll. Sets capture on the other window and ensures that the "ending" events
1768 // aren't sent to the window which gained capture.
1769 TEST_F(WindowEventDispatcherTest
, EndingEventDoesntRetarget
) {
1770 EventFilterRecorder recorder1
;
1771 EventFilterRecorder recorder2
;
1772 scoped_ptr
<Window
> window1(CreateNormalWindow(1, root_window(), NULL
));
1773 window1
->SetBounds(gfx::Rect(0, 0, 40, 40));
1775 scoped_ptr
<Window
> window2(CreateNormalWindow(2, root_window(), NULL
));
1776 window2
->SetBounds(gfx::Rect(40, 0, 40, 40));
1778 window1
->AddPreTargetHandler(&recorder1
);
1779 window2
->AddPreTargetHandler(&recorder2
);
1781 gfx::Point position
= window1
->bounds().origin();
1782 ui::TouchEvent
press(
1783 ui::ET_TOUCH_PRESSED
, position
, 0, ui::EventTimeForNow());
1784 DispatchEventUsingWindowDispatcher(&press
);
1786 gfx::Point position2
= window1
->bounds().CenterPoint();
1787 ui::TouchEvent
move(
1788 ui::ET_TOUCH_MOVED
, position2
, 0, ui::EventTimeForNow());
1789 DispatchEventUsingWindowDispatcher(&move
);
1791 window2
->SetCapture();
1793 EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
1794 "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
1795 "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END",
1796 EventTypesToString(recorder1
.events()));
1798 EXPECT_TRUE(recorder2
.events().empty());
1803 // This class creates and manages a window which is destroyed as soon as
1804 // capture is lost. This is the case for the drag and drop capture window.
1805 class CaptureWindowTracker
: public test::TestWindowDelegate
{
1807 CaptureWindowTracker() {}
1808 ~CaptureWindowTracker() override
{}
1810 void CreateCaptureWindow(aura::Window
* root_window
) {
1811 capture_window_
.reset(test::CreateTestWindowWithDelegate(
1812 this, -1234, gfx::Rect(20, 20, 20, 20), root_window
));
1813 capture_window_
->SetCapture();
1817 capture_window_
.reset();
1820 void OnCaptureLost() override
{ capture_window_
.reset(); }
1822 void OnWindowDestroyed(Window
* window
) override
{
1823 TestWindowDelegate::OnWindowDestroyed(window
);
1824 capture_window_
.reset();
1827 aura::Window
* capture_window() { return capture_window_
.get(); }
1830 scoped_ptr
<aura::Window
> capture_window_
;
1832 DISALLOW_COPY_AND_ASSIGN(CaptureWindowTracker
);
1837 // Verifies handling loss of capture by the capture window being hidden.
1838 TEST_F(WindowEventDispatcherTest
, CaptureWindowHidden
) {
1839 CaptureWindowTracker capture_window_tracker
;
1840 capture_window_tracker
.CreateCaptureWindow(root_window());
1841 capture_window_tracker
.capture_window()->Hide();
1842 EXPECT_EQ(NULL
, capture_window_tracker
.capture_window());
1845 // Verifies handling loss of capture by the capture window being destroyed.
1846 TEST_F(WindowEventDispatcherTest
, CaptureWindowDestroyed
) {
1847 CaptureWindowTracker capture_window_tracker
;
1848 capture_window_tracker
.CreateCaptureWindow(root_window());
1849 capture_window_tracker
.reset();
1850 EXPECT_EQ(NULL
, capture_window_tracker
.capture_window());
1853 class ExitMessageLoopOnMousePress
: public ui::test::TestEventHandler
{
1855 ExitMessageLoopOnMousePress() {}
1856 ~ExitMessageLoopOnMousePress() override
{}
1859 void OnMouseEvent(ui::MouseEvent
* event
) override
{
1860 ui::test::TestEventHandler::OnMouseEvent(event
);
1861 if (event
->type() == ui::ET_MOUSE_PRESSED
)
1862 base::MessageLoopForUI::current()->Quit();
1866 DISALLOW_COPY_AND_ASSIGN(ExitMessageLoopOnMousePress
);
1869 class WindowEventDispatcherTestWithMessageLoop
1870 : public WindowEventDispatcherTest
{
1872 WindowEventDispatcherTestWithMessageLoop() {}
1873 ~WindowEventDispatcherTestWithMessageLoop() override
{}
1876 // Reset any event the window may have received when bringing up the window
1877 // (e.g. mouse-move events if the mouse cursor is over the window).
1880 // Start a nested message-loop, post an event to be dispatched, and then
1881 // terminate the message-loop. When the message-loop unwinds and gets back,
1882 // the reposted event should not have fired.
1883 scoped_ptr
<ui::MouseEvent
> mouse(new ui::MouseEvent(ui::ET_MOUSE_PRESSED
,
1888 message_loop()->PostTask(
1890 base::Bind(&WindowEventDispatcherTestWithMessageLoop::RepostEventHelper
,
1891 host()->dispatcher(),
1892 base::Passed(&mouse
)));
1893 message_loop()->PostTask(FROM_HERE
, message_loop()->QuitClosure());
1895 base::MessageLoop::ScopedNestableTaskAllower
allow(message_loop());
1898 EXPECT_EQ(0, handler_
.num_mouse_events());
1900 // Let the current message-loop run. The event-handler will terminate the
1901 // message-loop when it receives the reposted event.
1904 base::MessageLoop
* message_loop() {
1905 return base::MessageLoopForUI::current();
1909 void SetUp() override
{
1910 WindowEventDispatcherTest::SetUp();
1911 window_
.reset(CreateNormalWindow(1, root_window(), NULL
));
1912 window_
->AddPreTargetHandler(&handler_
);
1915 void TearDown() override
{
1917 WindowEventDispatcherTest::TearDown();
1921 // Used to avoid a copying |event| when binding to a closure.
1922 static void RepostEventHelper(WindowEventDispatcher
* dispatcher
,
1923 scoped_ptr
<ui::MouseEvent
> event
) {
1924 dispatcher
->RepostEvent(*event
);
1927 scoped_ptr
<Window
> window_
;
1928 ExitMessageLoopOnMousePress handler_
;
1930 DISALLOW_COPY_AND_ASSIGN(WindowEventDispatcherTestWithMessageLoop
);
1933 TEST_F(WindowEventDispatcherTestWithMessageLoop
, EventRepostedInNonNestedLoop
) {
1934 CHECK(!message_loop()->is_running());
1935 // Perform the test in a callback, so that it runs after the message-loop
1937 message_loop()->PostTask(
1938 FROM_HERE
, base::Bind(
1939 &WindowEventDispatcherTestWithMessageLoop::RunTest
,
1940 base::Unretained(this)));
1941 message_loop()->Run();
1944 class WindowEventDispatcherTestInHighDPI
: public WindowEventDispatcherTest
{
1946 WindowEventDispatcherTestInHighDPI() {}
1947 ~WindowEventDispatcherTestInHighDPI() override
{}
1950 void SetUp() override
{
1951 WindowEventDispatcherTest::SetUp();
1952 test_screen()->SetDeviceScaleFactor(2.f
);
1956 TEST_F(WindowEventDispatcherTestInHighDPI
, EventLocationTransform
) {
1957 test::TestWindowDelegate delegate
;
1958 scoped_ptr
<aura::Window
> child(test::CreateTestWindowWithDelegate(&delegate
,
1959 1234, gfx::Rect(20, 20, 100, 100), root_window()));
1962 ui::test::TestEventHandler handler_child
;
1963 ui::test::TestEventHandler handler_root
;
1964 root_window()->AddPreTargetHandler(&handler_root
);
1965 child
->AddPreTargetHandler(&handler_child
);
1968 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
1969 gfx::Point(30, 30), gfx::Point(30, 30),
1970 ui::EF_NONE
, ui::EF_NONE
);
1971 DispatchEventUsingWindowDispatcher(&move
);
1972 EXPECT_EQ(0, handler_child
.num_mouse_events());
1973 EXPECT_EQ(1, handler_root
.num_mouse_events());
1977 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
1978 gfx::Point(50, 50), gfx::Point(50, 50),
1979 ui::EF_NONE
, ui::EF_NONE
);
1980 DispatchEventUsingWindowDispatcher(&move
);
1981 // The child receives an ENTER, and a MOVED event.
1982 EXPECT_EQ(2, handler_child
.num_mouse_events());
1983 // The root receives both the ENTER and the MOVED events dispatched to
1984 // |child|, as well as an EXIT event.
1985 EXPECT_EQ(3, handler_root
.num_mouse_events());
1988 child
->RemovePreTargetHandler(&handler_child
);
1989 root_window()->RemovePreTargetHandler(&handler_root
);
1992 TEST_F(WindowEventDispatcherTestInHighDPI
, TouchMovesHeldOnScroll
) {
1993 EventFilterRecorder recorder
;
1994 root_window()->AddPreTargetHandler(&recorder
);
1995 test::TestWindowDelegate delegate
;
1996 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
1997 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
1998 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
1999 window
->AddPreTargetHandler(&handler
);
2001 ui::test::EventGenerator
generator(root_window());
2002 generator
.GestureScrollSequence(
2003 gfx::Point(120, 120), gfx::Point(20, 120),
2004 base::TimeDelta::FromMilliseconds(100), 25);
2006 // |handler| will have reset |filter| and started holding the touch-move
2007 // events when scrolling started. At the end of the scroll (i.e. upon
2008 // touch-release), the held touch-move event will have been dispatched first,
2009 // along with the subsequent events (i.e. touch-release, scroll-end, and
2011 const EventFilterRecorder::Events
& events
= recorder
.events();
2013 "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
2014 "GESTURE_SCROLL_END GESTURE_END",
2015 EventTypesToString(events
));
2016 ASSERT_EQ(2u, recorder
.touch_locations().size());
2017 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2018 recorder
.touch_locations()[0].ToString());
2019 EXPECT_EQ(gfx::Point(-40, 10).ToString(),
2020 recorder
.touch_locations()[1].ToString());
2023 class SelfDestructDelegate
: public test::TestWindowDelegate
{
2025 SelfDestructDelegate() {}
2026 ~SelfDestructDelegate() override
{}
2028 void OnMouseEvent(ui::MouseEvent
* event
) override
{ window_
.reset(); }
2030 void set_window(scoped_ptr
<aura::Window
> window
) {
2031 window_
= window
.Pass();
2033 bool has_window() const { return !!window_
.get(); }
2036 scoped_ptr
<aura::Window
> window_
;
2037 DISALLOW_COPY_AND_ASSIGN(SelfDestructDelegate
);
2040 TEST_F(WindowEventDispatcherTest
, SynthesizedLocatedEvent
) {
2041 ui::test::EventGenerator
generator(root_window());
2042 generator
.MoveMouseTo(10, 10);
2044 Env::GetInstance()->last_mouse_location().ToString());
2046 // Synthesized event should not update the mouse location.
2047 ui::MouseEvent
mouseev(ui::ET_MOUSE_MOVED
, gfx::Point(), gfx::Point(),
2048 ui::EF_IS_SYNTHESIZED
, 0);
2049 generator
.Dispatch(&mouseev
);
2051 Env::GetInstance()->last_mouse_location().ToString());
2053 generator
.MoveMouseTo(0, 0);
2055 Env::GetInstance()->last_mouse_location().ToString());
2057 // Make sure the location gets updated when a syntheiszed enter
2058 // event destroyed the window.
2059 SelfDestructDelegate delegate
;
2060 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2061 &delegate
, 1, gfx::Rect(50, 50, 100, 100), root_window()));
2062 delegate
.set_window(window
.Pass());
2063 EXPECT_TRUE(delegate
.has_window());
2065 generator
.MoveMouseTo(100, 100);
2066 EXPECT_FALSE(delegate
.has_window());
2067 EXPECT_EQ("100,100",
2068 Env::GetInstance()->last_mouse_location().ToString());
2071 // Tests that the window which has capture can get destroyed as a result of
2072 // ui::ET_MOUSE_CAPTURE_CHANGED event dispatched in
2073 // WindowEventDispatcher::UpdateCapture without causing a "use after free".
2074 TEST_F(WindowEventDispatcherTest
, DestroyWindowOnCaptureChanged
) {
2075 SelfDestructDelegate delegate
;
2076 scoped_ptr
<aura::Window
> window_first(CreateTestWindowWithDelegate(
2077 &delegate
, 1, gfx::Rect(20, 10, 10, 20), root_window()));
2078 Window
* window_first_raw
= window_first
.get();
2079 window_first
->Show();
2080 window_first
->SetCapture();
2081 delegate
.set_window(window_first
.Pass());
2082 EXPECT_TRUE(delegate
.has_window());
2084 scoped_ptr
<aura::Window
> window_second(
2085 test::CreateTestWindowWithId(2, root_window()));
2086 window_second
->Show();
2088 client::CaptureDelegate
* capture_delegate
= host()->dispatcher();
2089 capture_delegate
->UpdateCapture(window_first_raw
, window_second
.get());
2090 EXPECT_FALSE(delegate
.has_window());
2093 class StaticFocusClient
: public client::FocusClient
{
2095 explicit StaticFocusClient(Window
* focused
)
2096 : focused_(focused
) {}
2097 ~StaticFocusClient() override
{}
2100 // client::FocusClient:
2101 void AddObserver(client::FocusChangeObserver
* observer
) override
{}
2102 void RemoveObserver(client::FocusChangeObserver
* observer
) override
{}
2103 void FocusWindow(Window
* window
) override
{}
2104 void ResetFocusWithinActiveWindow(Window
* window
) override
{}
2105 Window
* GetFocusedWindow() override
{ return focused_
; }
2109 DISALLOW_COPY_AND_ASSIGN(StaticFocusClient
);
2112 // Tests that host-cancel-mode event can be dispatched to a dispatcher safely
2113 // when the focused window does not live in the dispatcher's tree.
2114 TEST_F(WindowEventDispatcherTest
, HostCancelModeWithFocusedWindowOutside
) {
2115 test::TestWindowDelegate delegate
;
2116 scoped_ptr
<Window
> focused(CreateTestWindowWithDelegate(&delegate
, 123,
2117 gfx::Rect(20, 30, 100, 50), NULL
));
2118 StaticFocusClient
focus_client(focused
.get());
2119 client::SetFocusClient(root_window(), &focus_client
);
2120 EXPECT_FALSE(root_window()->Contains(focused
.get()));
2121 EXPECT_EQ(focused
.get(),
2122 client::GetFocusClient(root_window())->GetFocusedWindow());
2123 host()->dispatcher()->DispatchCancelModeEvent();
2124 EXPECT_EQ(focused
.get(),
2125 client::GetFocusClient(root_window())->GetFocusedWindow());
2128 // Dispatches a mouse-move event to |target| when it receives a mouse-move
2130 class DispatchEventHandler
: public ui::EventHandler
{
2132 explicit DispatchEventHandler(Window
* target
)
2134 dispatched_(false) {}
2135 ~DispatchEventHandler() override
{}
2137 bool dispatched() const { return dispatched_
; }
2139 // ui::EventHandler:
2140 void OnMouseEvent(ui::MouseEvent
* mouse
) override
{
2141 if (mouse
->type() == ui::ET_MOUSE_MOVED
) {
2142 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, target_
->bounds().CenterPoint(),
2143 target_
->bounds().CenterPoint(), ui::EF_NONE
, ui::EF_NONE
);
2144 ui::EventDispatchDetails details
=
2145 target_
->GetHost()->dispatcher()->OnEventFromSource(&move
);
2146 ASSERT_FALSE(details
.dispatcher_destroyed
);
2147 EXPECT_FALSE(details
.target_destroyed
);
2148 EXPECT_EQ(target_
, move
.target());
2151 ui::EventHandler::OnMouseEvent(mouse
);
2157 DISALLOW_COPY_AND_ASSIGN(DispatchEventHandler
);
2160 // Moves |window| to |root_window| when it receives a mouse-move event.
2161 class MoveWindowHandler
: public ui::EventHandler
{
2163 MoveWindowHandler(Window
* window
, Window
* root_window
)
2164 : window_to_move_(window
),
2165 root_window_to_move_to_(root_window
) {}
2166 ~MoveWindowHandler() override
{}
2169 // ui::EventHandler:
2170 void OnMouseEvent(ui::MouseEvent
* mouse
) override
{
2171 if (mouse
->type() == ui::ET_MOUSE_MOVED
) {
2172 root_window_to_move_to_
->AddChild(window_to_move_
);
2174 ui::EventHandler::OnMouseEvent(mouse
);
2177 Window
* window_to_move_
;
2178 Window
* root_window_to_move_to_
;
2180 DISALLOW_COPY_AND_ASSIGN(MoveWindowHandler
);
2183 // Tests that nested event dispatch works correctly if the target of the older
2184 // event being dispatched is moved to a different dispatcher in response to an
2185 // event in the inner loop.
2186 TEST_F(WindowEventDispatcherTest
, NestedEventDispatchTargetMoved
) {
2187 scoped_ptr
<WindowTreeHost
> second_host(
2188 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2189 second_host
->InitHost();
2190 Window
* second_root
= second_host
->window();
2192 // Create two windows parented to |root_window()|.
2193 test::TestWindowDelegate delegate
;
2194 scoped_ptr
<Window
> first(CreateTestWindowWithDelegate(&delegate
, 123,
2195 gfx::Rect(20, 10, 10, 20), root_window()));
2196 scoped_ptr
<Window
> second(CreateTestWindowWithDelegate(&delegate
, 234,
2197 gfx::Rect(40, 10, 50, 20), root_window()));
2199 // Setup a handler on |first| so that it dispatches an event to |second| when
2200 // |first| receives an event.
2201 DispatchEventHandler
dispatch_event(second
.get());
2202 first
->AddPreTargetHandler(&dispatch_event
);
2204 // Setup a handler on |second| so that it moves |first| into |second_root|
2205 // when |second| receives an event.
2206 MoveWindowHandler
move_window(first
.get(), second_root
);
2207 second
->AddPreTargetHandler(&move_window
);
2209 // Some sanity checks: |first| is inside |root_window()|'s tree.
2210 EXPECT_EQ(root_window(), first
->GetRootWindow());
2211 // The two root windows are different.
2212 EXPECT_NE(root_window(), second_root
);
2214 // Dispatch an event to |first|.
2215 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, first
->bounds().CenterPoint(),
2216 first
->bounds().CenterPoint(), ui::EF_NONE
, ui::EF_NONE
);
2217 ui::EventDispatchDetails details
=
2218 host()->dispatcher()->OnEventFromSource(&move
);
2219 ASSERT_FALSE(details
.dispatcher_destroyed
);
2220 EXPECT_TRUE(details
.target_destroyed
);
2221 EXPECT_EQ(first
.get(), move
.target());
2222 EXPECT_TRUE(dispatch_event
.dispatched());
2223 EXPECT_EQ(second_root
, first
->GetRootWindow());
2225 first
->RemovePreTargetHandler(&dispatch_event
);
2226 second
->RemovePreTargetHandler(&move_window
);
2229 class AlwaysMouseDownInputStateLookup
: public InputStateLookup
{
2231 AlwaysMouseDownInputStateLookup() {}
2232 ~AlwaysMouseDownInputStateLookup() override
{}
2235 // InputStateLookup:
2236 bool IsMouseButtonDown() const override
{ return true; }
2238 DISALLOW_COPY_AND_ASSIGN(AlwaysMouseDownInputStateLookup
);
2241 TEST_F(WindowEventDispatcherTest
,
2242 CursorVisibilityChangedWhileCaptureWindowInAnotherDispatcher
) {
2243 test::EventCountDelegate delegate
;
2244 scoped_ptr
<Window
> window(CreateTestWindowWithDelegate(&delegate
, 123,
2245 gfx::Rect(20, 10, 10, 20), root_window()));
2248 scoped_ptr
<WindowTreeHost
> second_host(
2249 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2250 second_host
->InitHost();
2251 WindowEventDispatcher
* second_dispatcher
= second_host
->dispatcher();
2253 // Install an InputStateLookup on the Env that always claims that a
2254 // mouse-button is down.
2255 test::EnvTestHelper(Env::GetInstance()).SetInputStateLookup(
2256 scoped_ptr
<InputStateLookup
>(new AlwaysMouseDownInputStateLookup()));
2258 window
->SetCapture();
2260 // Because the mouse button is down, setting the capture on |window| will set
2261 // it as the mouse-move handler for |root_window()|.
2262 EXPECT_EQ(window
.get(), host()->dispatcher()->mouse_moved_handler());
2264 // This does not set |window| as the mouse-move handler for the second
2266 EXPECT_EQ(NULL
, second_dispatcher
->mouse_moved_handler());
2268 // However, some capture-client updates the capture in each root-window on a
2269 // capture. Emulate that here. Because of this, the second dispatcher also has
2270 // |window| as the mouse-move handler.
2271 client::CaptureDelegate
* second_capture_delegate
= second_dispatcher
;
2272 second_capture_delegate
->UpdateCapture(NULL
, window
.get());
2273 EXPECT_EQ(window
.get(), second_dispatcher
->mouse_moved_handler());
2275 // Reset the mouse-event counts for |window|.
2276 delegate
.GetMouseMotionCountsAndReset();
2278 // Notify both hosts that the cursor is now hidden. This should send a single
2279 // mouse-exit event to |window|.
2280 host()->OnCursorVisibilityChanged(false);
2281 second_host
->OnCursorVisibilityChanged(false);
2282 EXPECT_EQ("0 0 1", delegate
.GetMouseMotionCountsAndReset());
2285 TEST_F(WindowEventDispatcherTest
,
2286 RedirectedEventToDifferentDispatcherLocation
) {
2287 scoped_ptr
<WindowTreeHost
> second_host(
2288 WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
2289 second_host
->InitHost();
2290 client::SetCaptureClient(second_host
->window(),
2291 client::GetCaptureClient(root_window()));
2293 test::EventCountDelegate delegate
;
2294 scoped_ptr
<Window
> window_first(CreateTestWindowWithDelegate(&delegate
, 123,
2295 gfx::Rect(20, 10, 10, 20), root_window()));
2296 window_first
->Show();
2298 scoped_ptr
<Window
> window_second(CreateTestWindowWithDelegate(&delegate
, 12,
2299 gfx::Rect(10, 10, 20, 30), second_host
->window()));
2300 window_second
->Show();
2302 window_second
->SetCapture();
2303 EXPECT_EQ(window_second
.get(),
2304 client::GetCaptureWindow(root_window()));
2306 // Send an event to the first host. Make sure it goes to |window_second| in
2307 // |second_host| instead (since it has capture).
2308 EventFilterRecorder recorder_first
;
2309 window_first
->AddPreTargetHandler(&recorder_first
);
2310 EventFilterRecorder recorder_second
;
2311 window_second
->AddPreTargetHandler(&recorder_second
);
2312 const gfx::Point
event_location(25, 15);
2313 ui::MouseEvent
mouse(ui::ET_MOUSE_PRESSED
, event_location
,
2314 event_location
, ui::EF_LEFT_MOUSE_BUTTON
,
2315 ui::EF_LEFT_MOUSE_BUTTON
);
2316 DispatchEventUsingWindowDispatcher(&mouse
);
2317 EXPECT_TRUE(recorder_first
.events().empty());
2318 ASSERT_EQ(1u, recorder_second
.events().size());
2319 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, recorder_second
.events()[0]);
2320 EXPECT_EQ(event_location
.ToString(),
2321 recorder_second
.mouse_locations()[0].ToString());
2324 class AsyncWindowDelegate
: public test::TestWindowDelegate
{
2326 AsyncWindowDelegate(WindowEventDispatcher
* dispatcher
)
2327 : dispatcher_(dispatcher
) {}
2329 void set_window(Window
* window
) {
2333 void OnTouchEvent(ui::TouchEvent
* event
) override
{
2334 // Convert touch event back to root window coordinates.
2335 event
->ConvertLocationToTarget(window_
, window_
->GetRootWindow());
2336 dispatcher_
->ProcessedTouchEvent(event
, window_
, ui::ER_UNHANDLED
);
2337 event
->StopPropagation();
2340 WindowEventDispatcher
* dispatcher_
;
2343 DISALLOW_COPY_AND_ASSIGN(AsyncWindowDelegate
);
2346 // Tests that gesture events dispatched through the asynchronous flow have
2347 // co-ordinates in the right co-ordinate space.
2348 TEST_F(WindowEventDispatcherTest
, GestureEventCoordinates
) {
2349 const float kX
= 67.3f
;
2350 const float kY
= 97.8f
;
2352 const int kWindowOffset
= 50;
2353 EventFilterRecorder recorder
;
2354 root_window()->AddPreTargetHandler(&recorder
);
2355 AsyncWindowDelegate
delegate(host()->dispatcher());
2356 HoldPointerOnScrollHandler
handler(host()->dispatcher(), &recorder
);
2357 scoped_ptr
<aura::Window
> window(CreateTestWindowWithDelegate(
2360 gfx::Rect(kWindowOffset
, kWindowOffset
, 100, 100),
2362 window
->AddPreTargetHandler(&handler
);
2364 delegate
.set_window(window
.get());
2366 ui::TouchEvent
touch_pressed_event(
2367 ui::ET_TOUCH_PRESSED
, gfx::PointF(kX
, kY
), 0, ui::EventTimeForNow());
2369 DispatchEventUsingWindowDispatcher(&touch_pressed_event
);
2371 ASSERT_EQ(1u, recorder
.touch_locations().size());
2372 EXPECT_EQ(gfx::Point(kX
- kWindowOffset
, kY
- kWindowOffset
).ToString(),
2373 recorder
.touch_locations()[0].ToString());
2375 ASSERT_EQ(2u, recorder
.gesture_locations().size());
2376 EXPECT_EQ(gfx::Point(kX
- kWindowOffset
, kY
- kWindowOffset
).ToString(),
2377 recorder
.gesture_locations()[0].ToString());