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.
6 #include "ash/shell_window_ids.h"
7 #include "ash/test/ash_test_base.h"
8 #include "ash/test/shell_test_api.h"
9 #include "ash/test/test_activation_delegate.h"
10 #include "ash/wm/window_util.h"
11 #include "ui/aura/client/cursor_client_observer.h"
12 #include "ui/aura/client/focus_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/test/aura_test_base.h"
15 #include "ui/aura/test/test_window_delegate.h"
16 #include "ui/aura/test/test_windows.h"
17 #include "ui/base/cursor/cursor.h"
18 #include "ui/base/hit_test.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_processor.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/test/event_generator.h"
23 #include "ui/events/test/test_event_handler.h"
24 #include "ui/gfx/screen.h"
25 #include "ui/wm/core/compound_event_filter.h"
26 #include "ui/wm/core/input_method_event_filter.h"
27 #include "ui/wm/public/activation_client.h"
28 #include "ui/wm/public/activation_delegate.h"
32 class TestingCursorClientObserver
: public aura::client::CursorClientObserver
{
34 TestingCursorClientObserver()
35 : cursor_visibility_(false),
36 did_visibility_change_(false) {}
37 void reset() { cursor_visibility_
= did_visibility_change_
= false; }
38 bool is_cursor_visible() const { return cursor_visibility_
; }
39 bool did_visibility_change() const { return did_visibility_change_
; }
41 // Overridden from aura::client::CursorClientObserver:
42 void OnCursorVisibilityChanged(bool is_visible
) override
{
43 cursor_visibility_
= is_visible
;
44 did_visibility_change_
= true;
48 bool cursor_visibility_
;
49 bool did_visibility_change_
;
51 DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver
);
54 base::TimeDelta
getTime() {
55 return ui::EventTimeForNow();
58 // A slightly changed TestEventHandler which can be configured to return a
59 // specified value for key/mouse event handling.
60 class CustomEventHandler
: public ui::test::TestEventHandler
{
63 : key_result_(ui::ER_UNHANDLED
),
64 mouse_result_(ui::ER_UNHANDLED
) {
67 ~CustomEventHandler() override
{}
69 void set_key_event_handling_result(ui::EventResult result
) {
73 void set_mouse_event_handling_result(ui::EventResult result
) {
74 mouse_result_
= result
;
77 // Overridden from ui::EventHandler:
78 void OnKeyEvent(ui::KeyEvent
* event
) override
{
79 ui::test::TestEventHandler::OnKeyEvent(event
);
80 if (key_result_
& ui::ER_HANDLED
)
82 if (key_result_
& ui::ER_CONSUMED
)
83 event
->StopPropagation();
86 void OnMouseEvent(ui::MouseEvent
* event
) override
{
87 ui::test::TestEventHandler::OnMouseEvent(event
);
88 if (mouse_result_
& ui::ER_HANDLED
)
90 if (mouse_result_
& ui::ER_CONSUMED
)
91 event
->StopPropagation();
95 ui::EventResult key_result_
;
96 ui::EventResult mouse_result_
;
98 DISALLOW_COPY_AND_ASSIGN(CustomEventHandler
);
105 typedef test::AshTestBase WindowManagerTest
;
107 class NonFocusableDelegate
: public aura::test::TestWindowDelegate
{
109 NonFocusableDelegate() {}
112 bool CanFocus() override
{ return false; }
114 DISALLOW_COPY_AND_ASSIGN(NonFocusableDelegate
);
117 class HitTestWindowDelegate
: public aura::test::TestWindowDelegate
{
119 HitTestWindowDelegate()
120 : hittest_code_(HTNOWHERE
) {
122 ~HitTestWindowDelegate() override
{}
123 void set_hittest_code(int hittest_code
) { hittest_code_
= hittest_code
; }
126 // Overridden from TestWindowDelegate:
127 int GetNonClientComponent(const gfx::Point
& point
) const override
{
128 return hittest_code_
;
133 DISALLOW_COPY_AND_ASSIGN(HitTestWindowDelegate
);
136 TEST_F(WindowManagerTest
, Focus
) {
137 // The IME event filter interferes with the basic key event propagation we
138 // attempt to do here, so we remove it.
139 test::ShellTestApi
shell_test(Shell::GetInstance());
140 Shell::GetInstance()->RemovePreTargetHandler(
141 shell_test
.input_method_event_filter());
143 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
144 root_window
->SetBounds(gfx::Rect(0, 0, 510, 510));
146 // Supplied ids are negative so as not to collide with shell ids.
147 // TODO(beng): maybe introduce a MAKE_SHELL_ID() macro that generates a safe
148 // id beyond shell id max?
149 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
150 SK_ColorWHITE
, -1, gfx::Rect(10, 10, 500, 500)));
151 scoped_ptr
<aura::Window
> w11(aura::test::CreateTestWindow(
152 SK_ColorGREEN
, -11, gfx::Rect(5, 5, 100, 100), w1
.get()));
153 scoped_ptr
<aura::Window
> w111(aura::test::CreateTestWindow(
154 SK_ColorCYAN
, -111, gfx::Rect(5, 5, 75, 75), w11
.get()));
155 scoped_ptr
<aura::Window
> w1111(aura::test::CreateTestWindow(
156 SK_ColorRED
, -1111, gfx::Rect(5, 5, 50, 50), w111
.get()));
157 scoped_ptr
<aura::Window
> w12(aura::test::CreateTestWindow(
158 SK_ColorMAGENTA
, -12, gfx::Rect(10, 420, 25, 25), w1
.get()));
159 aura::test::ColorTestWindowDelegate
* w121delegate
=
160 new aura::test::ColorTestWindowDelegate(SK_ColorYELLOW
);
161 scoped_ptr
<aura::Window
> w121(aura::test::CreateTestWindowWithDelegate(
162 w121delegate
, -121, gfx::Rect(5, 5, 5, 5), w12
.get()));
163 aura::test::ColorTestWindowDelegate
* w122delegate
=
164 new aura::test::ColorTestWindowDelegate(SK_ColorRED
);
165 scoped_ptr
<aura::Window
> w122(aura::test::CreateTestWindowWithDelegate(
166 w122delegate
, -122, gfx::Rect(10, 5, 5, 5), w12
.get()));
167 aura::test::ColorTestWindowDelegate
* w123delegate
=
168 new aura::test::ColorTestWindowDelegate(SK_ColorRED
);
169 scoped_ptr
<aura::Window
> w123(aura::test::CreateTestWindowWithDelegate(
170 w123delegate
, -123, gfx::Rect(15, 5, 5, 5), w12
.get()));
171 scoped_ptr
<aura::Window
> w13(aura::test::CreateTestWindow(
172 SK_ColorGRAY
, -13, gfx::Rect(5, 470, 50, 50), w1
.get()));
174 // Click on a sub-window (w121) to focus it.
175 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w121
.get());
176 generator
.ClickLeftButton();
178 aura::client::FocusClient
* focus_client
=
179 aura::client::GetFocusClient(w121
.get());
180 EXPECT_EQ(w121
.get(), focus_client
->GetFocusedWindow());
182 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
184 // The key press should be sent to the focused sub-window.
185 ui::KeyEvent
keyev(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
186 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&keyev
);
187 ASSERT_FALSE(details
.dispatcher_destroyed
);
188 EXPECT_EQ(ui::VKEY_E
, w121delegate
->last_key_code());
190 // Touch on a sub-window (w122) to focus it.
191 gfx::Point click_point
= w122
->bounds().CenterPoint();
192 aura::Window::ConvertPointToTarget(w122
->parent(), root_window
, &click_point
);
193 ui::TouchEvent
touchev(ui::ET_TOUCH_PRESSED
, click_point
, 0, getTime());
194 details
= dispatcher
->OnEventFromSource(&touchev
);
195 ASSERT_FALSE(details
.dispatcher_destroyed
);
196 focus_client
= aura::client::GetFocusClient(w122
.get());
197 EXPECT_EQ(w122
.get(), focus_client
->GetFocusedWindow());
199 // The key press should be sent to the focused sub-window.
200 keyev
= ui::KeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
201 details
= dispatcher
->OnEventFromSource(&keyev
);
202 ASSERT_FALSE(details
.dispatcher_destroyed
);
203 EXPECT_EQ(ui::VKEY_E
, w122delegate
->last_key_code());
205 // Hiding the focused window will set the focus to its parent if
208 EXPECT_EQ(aura::client::GetFocusClient(w12
.get()),
209 aura::client::GetFocusClient(w122
.get()));
211 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
213 // Sets the focus back to w122.
216 EXPECT_EQ(w122
.get(),
217 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
219 // Removing the focused window from parent should set the focus to
220 // its parent if it's focusable.
221 w12
->RemoveChild(w122
.get());
222 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w122
.get()));
224 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
226 // Set the focus to w123, but make the w1 not activatable.
227 test::TestActivationDelegate
activation_delegate(false);
229 EXPECT_EQ(w123
.get(),
230 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
231 aura::client::SetActivationDelegate(w1
.get(), &activation_delegate
);
233 // Hiding the focused window will set the focus to NULL because
234 // parent window is not focusable.
236 EXPECT_EQ(aura::client::GetFocusClient(w12
.get()),
237 aura::client::GetFocusClient(w123
.get()));
238 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
239 keyev
= ui::KeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
240 details
= dispatcher
->OnEventFromSource(&keyev
);
241 EXPECT_FALSE(keyev
.handled() || details
.dispatcher_destroyed
);
243 // Set the focus back to w123
244 aura::client::SetActivationDelegate(w1
.get(), NULL
);
247 EXPECT_EQ(w123
.get(),
248 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
249 aura::client::SetActivationDelegate(w1
.get(), &activation_delegate
);
251 // Removing the focused window will set the focus to NULL because
252 // parent window is not focusable.
253 w12
->RemoveChild(w123
.get());
254 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w123
.get()));
255 keyev
= ui::KeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
256 details
= dispatcher
->OnEventFromSource(&keyev
);
257 EXPECT_FALSE(keyev
.handled() || details
.dispatcher_destroyed
);
259 // Must set to NULL since the activation delegate will be destroyed before
261 aura::client::SetActivationDelegate(w1
.get(), NULL
);
264 // Various assertion testing for activating windows.
265 TEST_F(WindowManagerTest
, ActivateOnMouse
) {
266 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
268 test::TestActivationDelegate d1
;
269 aura::test::TestWindowDelegate wd
;
270 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShellWithDelegate(
271 &wd
, -1, gfx::Rect(10, 10, 50, 50)));
272 d1
.SetWindow(w1
.get());
273 test::TestActivationDelegate d2
;
274 scoped_ptr
<aura::Window
> w2(CreateTestWindowInShellWithDelegate(
275 &wd
, -2, gfx::Rect(70, 70, 50, 50)));
276 d2
.SetWindow(w2
.get());
278 aura::client::FocusClient
* focus_client
=
279 aura::client::GetFocusClient(w1
.get());
285 wm::ActivateWindow(w1
.get());
286 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
287 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
288 EXPECT_EQ(1, d1
.activated_count());
289 EXPECT_EQ(0, d1
.lost_active_count());
294 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w2
.get());
295 generator
.ClickLeftButton();
297 // Window2 should have become active.
298 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
299 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
300 EXPECT_EQ(0, d1
.activated_count());
301 EXPECT_EQ(1, d1
.lost_active_count());
302 EXPECT_EQ(1, d2
.activated_count());
303 EXPECT_EQ(0, d2
.lost_active_count());
309 // Click back on window1, but set it up so w1 doesn't activate on click.
310 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w1
.get());
311 d1
.set_activate(false);
312 generator
.ClickLeftButton();
314 // Window2 should still be active and focused.
315 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
316 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
317 EXPECT_EQ(0, d1
.activated_count());
318 EXPECT_EQ(0, d1
.lost_active_count());
319 EXPECT_EQ(0, d2
.activated_count());
320 EXPECT_EQ(0, d2
.lost_active_count());
325 // Destroy window2, this should make window1 active.
326 d1
.set_activate(true);
328 EXPECT_EQ(0, d2
.activated_count());
329 EXPECT_EQ(1, d2
.lost_active_count());
330 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
331 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
332 EXPECT_EQ(1, d1
.activated_count());
333 EXPECT_EQ(0, d1
.lost_active_count());
335 // Clicking an active window with a child shouldn't steal the
336 // focus from the child.
338 scoped_ptr
<aura::Window
> w11(CreateTestWindowWithDelegate(
339 &wd
, -11, gfx::Rect(10, 10, 10, 10), w1
.get()));
340 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
342 // First set the focus to the child |w11|.
343 generator
.ClickLeftButton();
344 EXPECT_EQ(w11
.get(), focus_client
->GetFocusedWindow());
345 EXPECT_EQ(w1
.get(), wm::GetActiveWindow());
347 // Then click the parent active window. The focus shouldn't move.
348 gfx::Point left_top
= w1
->bounds().origin();
349 aura::Window::ConvertPointToTarget(w1
->parent(), root_window
, &left_top
);
350 left_top
.Offset(1, 1);
351 generator
.MoveMouseTo(left_top
);
352 generator
.ClickLeftButton();
353 EXPECT_EQ(w11
.get(), focus_client
->GetFocusedWindow());
354 EXPECT_EQ(w1
.get(), wm::GetActiveWindow());
357 // Clicking on a non-focusable window inside a background window should still
358 // give focus to the background window.
360 NonFocusableDelegate nfd
;
361 scoped_ptr
<aura::Window
> w11(CreateTestWindowWithDelegate(
362 &nfd
, -1, gfx::Rect(10, 10, 10, 10), w1
.get()));
363 // Move focus to |w2| first.
364 scoped_ptr
<aura::Window
> w2(CreateTestWindowInShellWithDelegate(
365 &wd
, -1, gfx::Rect(70, 70, 50, 50)));
366 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w2
.get());
367 generator
.ClickLeftButton();
368 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
369 EXPECT_FALSE(w11
->CanFocus());
371 // Click on |w11|. This should focus w1.
372 generator
.MoveMouseToCenterOf(w11
.get());
373 generator
.ClickLeftButton();
374 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
378 TEST_F(WindowManagerTest
, PanelActivation
) {
379 aura::test::TestWindowDelegate wd
;
380 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShellWithDelegate(
381 &wd
, -1, gfx::Rect(10, 10, 50, 50)));
382 aura::test::TestWindowDelegate pd
;
383 scoped_ptr
<aura::Window
> p1(CreateTestWindowInShellWithDelegateAndType(
384 &pd
, ui::wm::WINDOW_TYPE_PANEL
, -1, gfx::Rect(10, 10, 50, 50)));
385 aura::client::FocusClient
* focus_client
=
386 aura::client::GetFocusClient(w1
.get());
389 wm::ActivateWindow(w1
.get());
390 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
393 wm::ActivateWindow(p1
.get());
394 EXPECT_TRUE(wm::IsActiveWindow(p1
.get()));
395 EXPECT_EQ(p1
.get(), focus_client
->GetFocusedWindow());
398 wm::ActivateWindow(w1
.get());
399 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
400 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
402 // Clicking on a non-activatable window should not change the active window.
404 NonFocusableDelegate nfd
;
405 scoped_ptr
<aura::Window
> w3(CreateTestWindowInShellWithDelegate(
406 &nfd
, -1, gfx::Rect(70, 70, 50, 50)));
407 ui::test::EventGenerator
generator3(Shell::GetPrimaryRootWindow(),
409 wm::ActivateWindow(p1
.get());
410 EXPECT_TRUE(wm::IsActiveWindow(p1
.get()));
411 generator3
.ClickLeftButton();
412 EXPECT_TRUE(wm::IsActiveWindow(p1
.get()));
416 // Essentially the same as ActivateOnMouse, but for touch events.
417 TEST_F(WindowManagerTest
, ActivateOnTouch
) {
418 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
420 test::TestActivationDelegate d1
;
421 aura::test::TestWindowDelegate wd
;
422 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShellWithDelegate(
423 &wd
, -1, gfx::Rect(10, 10, 50, 50)));
424 d1
.SetWindow(w1
.get());
425 test::TestActivationDelegate d2
;
426 scoped_ptr
<aura::Window
> w2(CreateTestWindowInShellWithDelegate(
427 &wd
, -2, gfx::Rect(70, 70, 50, 50)));
428 d2
.SetWindow(w2
.get());
430 aura::client::FocusClient
* focus_client
=
431 aura::client::GetFocusClient(w1
.get());
437 wm::ActivateWindow(w1
.get());
438 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
439 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
440 EXPECT_EQ(1, d1
.activated_count());
441 EXPECT_EQ(0, d1
.lost_active_count());
445 gfx::Point press_point
= w2
->bounds().CenterPoint();
446 aura::Window::ConvertPointToTarget(w2
->parent(), root_window
, &press_point
);
447 ui::TouchEvent
touchev1(ui::ET_TOUCH_PRESSED
, press_point
, 0, getTime());
449 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
450 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&touchev1
);
451 ASSERT_FALSE(details
.dispatcher_destroyed
);
453 // Window2 should have become active.
454 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
455 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
456 EXPECT_EQ(0, d1
.activated_count());
457 EXPECT_EQ(1, d1
.lost_active_count());
458 EXPECT_EQ(1, d2
.activated_count());
459 EXPECT_EQ(0, d2
.lost_active_count());
463 // Touch window1, but set it up so w1 doesn't activate on touch.
464 press_point
= w1
->bounds().CenterPoint();
465 aura::Window::ConvertPointToTarget(w1
->parent(), root_window
, &press_point
);
466 d1
.set_activate(false);
467 ui::TouchEvent
touchev2(ui::ET_TOUCH_PRESSED
, press_point
, 1, getTime());
468 details
= dispatcher
->OnEventFromSource(&touchev2
);
469 ASSERT_FALSE(details
.dispatcher_destroyed
);
471 // Window2 should still be active and focused.
472 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
473 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
474 EXPECT_EQ(0, d1
.activated_count());
475 EXPECT_EQ(0, d1
.lost_active_count());
476 EXPECT_EQ(0, d2
.activated_count());
477 EXPECT_EQ(0, d2
.lost_active_count());
481 // Destroy window2, this should make window1 active.
482 d1
.set_activate(true);
484 EXPECT_EQ(0, d2
.activated_count());
485 EXPECT_EQ(1, d2
.lost_active_count());
486 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
487 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
488 EXPECT_EQ(1, d1
.activated_count());
489 EXPECT_EQ(0, d1
.lost_active_count());
492 TEST_F(WindowManagerTest
, MouseEventCursors
) {
493 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
496 const int kWindowLeft
= 123;
497 const int kWindowTop
= 45;
498 HitTestWindowDelegate window_delegate
;
499 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithDelegate(
502 gfx::Rect(kWindowLeft
, kWindowTop
, 640, 480)));
504 // Create two mouse movement events we can switch between.
505 gfx::Point
point1(kWindowLeft
, kWindowTop
);
506 aura::Window::ConvertPointToTarget(window
->parent(), root_window
, &point1
);
508 gfx::Point
point2(kWindowLeft
+ 1, kWindowTop
+ 1);
509 aura::Window::ConvertPointToTarget(window
->parent(), root_window
, &point2
);
511 aura::WindowTreeHost
* host
= root_window
->GetHost();
512 ui::EventProcessor
* dispatcher
= host
->event_processor();
514 // Cursor starts as a pointer (set during Shell::Init()).
515 EXPECT_EQ(ui::kCursorPointer
, host
->last_cursor().native_type());
518 // Resize edges and corners show proper cursors.
519 window_delegate
.set_hittest_code(HTBOTTOM
);
520 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
521 ui::EventTimeForNow(), 0, 0);
522 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
523 ASSERT_FALSE(details
.dispatcher_destroyed
);
524 EXPECT_EQ(ui::kCursorSouthResize
, host
->last_cursor().native_type());
528 window_delegate
.set_hittest_code(HTBOTTOMLEFT
);
529 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
530 ui::EventTimeForNow(), 0, 0);
531 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
532 ASSERT_FALSE(details
.dispatcher_destroyed
);
533 EXPECT_EQ(ui::kCursorSouthWestResize
, host
->last_cursor().native_type());
537 window_delegate
.set_hittest_code(HTBOTTOMRIGHT
);
538 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
539 ui::EventTimeForNow(), 0, 0);
540 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
541 ASSERT_FALSE(details
.dispatcher_destroyed
);
542 EXPECT_EQ(ui::kCursorSouthEastResize
, host
->last_cursor().native_type());
546 window_delegate
.set_hittest_code(HTLEFT
);
547 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
548 ui::EventTimeForNow(), 0, 0);
549 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
550 ASSERT_FALSE(details
.dispatcher_destroyed
);
551 EXPECT_EQ(ui::kCursorWestResize
, host
->last_cursor().native_type());
555 window_delegate
.set_hittest_code(HTRIGHT
);
556 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
557 ui::EventTimeForNow(), 0, 0);
558 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
559 ASSERT_FALSE(details
.dispatcher_destroyed
);
560 EXPECT_EQ(ui::kCursorEastResize
, host
->last_cursor().native_type());
564 window_delegate
.set_hittest_code(HTTOP
);
565 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
566 ui::EventTimeForNow(), 0, 0);
567 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
568 ASSERT_FALSE(details
.dispatcher_destroyed
);
569 EXPECT_EQ(ui::kCursorNorthResize
, host
->last_cursor().native_type());
573 window_delegate
.set_hittest_code(HTTOPLEFT
);
574 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
575 ui::EventTimeForNow(), 0, 0);
576 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
577 ASSERT_FALSE(details
.dispatcher_destroyed
);
578 EXPECT_EQ(ui::kCursorNorthWestResize
, host
->last_cursor().native_type());
582 window_delegate
.set_hittest_code(HTTOPRIGHT
);
583 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
584 ui::EventTimeForNow(), 0, 0);
585 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
586 ASSERT_FALSE(details
.dispatcher_destroyed
);
587 EXPECT_EQ(ui::kCursorNorthEastResize
, host
->last_cursor().native_type());
591 // Client area uses null cursor.
592 window_delegate
.set_hittest_code(HTCLIENT
);
593 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
594 ui::EventTimeForNow(), 0, 0);
595 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
596 ASSERT_FALSE(details
.dispatcher_destroyed
);
597 EXPECT_EQ(ui::kCursorNull
, host
->last_cursor().native_type());
602 #define MAYBE_TransformActivate DISABLED_TransformActivate
604 #define MAYBE_TransformActivate TransformActivate
606 TEST_F(WindowManagerTest
, MAYBE_TransformActivate
) {
607 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
608 gfx::Size size
= root_window
->bounds().size();
609 EXPECT_EQ(gfx::Rect(size
).ToString(),
610 Shell::GetScreen()->GetDisplayNearestPoint(
611 gfx::Point()).bounds().ToString());
613 // Rotate it clock-wise 90 degrees.
614 gfx::Transform transform
;
615 transform
.Translate(size
.width(), 0);
616 transform
.Rotate(90.0f
);
617 root_window
->GetHost()->SetRootTransform(transform
);
619 test::TestActivationDelegate d1
;
620 aura::test::TestWindowDelegate wd
;
621 scoped_ptr
<aura::Window
> w1(
622 CreateTestWindowInShellWithDelegate(&wd
, 1, gfx::Rect(0, 15, 50, 50)));
623 d1
.SetWindow(w1
.get());
626 gfx::Point
miss_point(5, 5);
627 transform
.TransformPoint(&miss_point
);
628 ui::MouseEvent
mouseev1(ui::ET_MOUSE_PRESSED
, miss_point
, miss_point
,
629 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
630 ui::EF_LEFT_MOUSE_BUTTON
);
631 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
632 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&mouseev1
);
633 ASSERT_FALSE(details
.dispatcher_destroyed
);
634 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w1
.get())->GetFocusedWindow());
635 ui::MouseEvent
mouseup(ui::ET_MOUSE_RELEASED
, miss_point
, miss_point
,
636 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
637 ui::EF_LEFT_MOUSE_BUTTON
);
638 details
= dispatcher
->OnEventFromSource(&mouseup
);
639 ASSERT_FALSE(details
.dispatcher_destroyed
);
641 gfx::Point
hit_point(5, 15);
642 transform
.TransformPoint(&hit_point
);
643 ui::MouseEvent
mouseev2(ui::ET_MOUSE_PRESSED
, hit_point
, hit_point
,
644 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
645 ui::EF_LEFT_MOUSE_BUTTON
);
646 details
= dispatcher
->OnEventFromSource(&mouseev2
);
647 ASSERT_FALSE(details
.dispatcher_destroyed
);
648 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
650 aura::client::GetFocusClient(w1
.get())->GetFocusedWindow());
653 TEST_F(WindowManagerTest
, AdditionalFilters
) {
654 // The IME event filter interferes with the basic key event propagation we
655 // attempt to do here, so we remove it.
656 test::ShellTestApi
shell_test(Shell::GetInstance());
657 Shell::GetInstance()->RemovePreTargetHandler(
658 shell_test
.input_method_event_filter());
660 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
662 // Creates a window and make it active
663 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
664 SK_ColorWHITE
, -1, gfx::Rect(0, 0, 100, 100)));
665 wm::ActivateWindow(w1
.get());
667 // Creates two addition filters
668 scoped_ptr
<CustomEventHandler
> f1(new CustomEventHandler
);
669 scoped_ptr
<CustomEventHandler
> f2(new CustomEventHandler
);
671 // Adds them to root window event filter.
672 ::wm::CompoundEventFilter
* env_filter
=
673 Shell::GetInstance()->env_filter();
674 env_filter
->AddHandler(f1
.get());
675 env_filter
->AddHandler(f2
.get());
677 // Dispatches mouse and keyboard events.
678 ui::KeyEvent
key_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
679 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
680 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&key_event
);
681 ASSERT_FALSE(details
.dispatcher_destroyed
);
682 ui::MouseEvent
mouse_pressed(ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0),
683 gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
684 details
= dispatcher
->OnEventFromSource(&mouse_pressed
);
685 ASSERT_FALSE(details
.dispatcher_destroyed
);
687 // Both filters should get the events.
688 EXPECT_EQ(1, f1
->num_key_events());
689 EXPECT_EQ(1, f1
->num_mouse_events());
690 EXPECT_EQ(1, f2
->num_key_events());
691 EXPECT_EQ(1, f2
->num_mouse_events());
696 // Makes f1 consume events.
697 f1
->set_key_event_handling_result(ui::ER_CONSUMED
);
698 f1
->set_mouse_event_handling_result(ui::ER_CONSUMED
);
700 // Dispatches events.
701 details
= dispatcher
->OnEventFromSource(&key_event
);
702 ASSERT_FALSE(details
.dispatcher_destroyed
);
703 ui::MouseEvent
mouse_released(ui::ET_MOUSE_RELEASED
, gfx::Point(0, 0),
704 gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
705 details
= dispatcher
->OnEventFromSource(&mouse_released
);
706 ASSERT_FALSE(details
.dispatcher_destroyed
);
708 // f1 should still get the events but f2 no longer gets them.
709 EXPECT_EQ(1, f1
->num_key_events());
710 EXPECT_EQ(1, f1
->num_mouse_events());
711 EXPECT_EQ(0, f2
->num_key_events());
712 EXPECT_EQ(0, f2
->num_mouse_events());
717 // Remove f1 from additonal filters list.
718 env_filter
->RemoveHandler(f1
.get());
720 // Dispatches events.
721 details
= dispatcher
->OnEventFromSource(&key_event
);
722 ASSERT_FALSE(details
.dispatcher_destroyed
);
723 details
= dispatcher
->OnEventFromSource(&mouse_pressed
);
724 ASSERT_FALSE(details
.dispatcher_destroyed
);
726 // f1 should get no events since it's out and f2 should get them.
727 EXPECT_EQ(0, f1
->num_key_events());
728 EXPECT_EQ(0, f1
->num_mouse_events());
729 EXPECT_EQ(1, f2
->num_key_events());
730 EXPECT_EQ(1, f2
->num_mouse_events());
732 env_filter
->RemoveHandler(f2
.get());
735 #if defined(OS_CHROMEOS) || defined(OS_WIN)
736 // Touch visually hides the cursor on ChromeOS and Windows
737 TEST_F(WindowManagerTest
, UpdateCursorVisibility
) {
738 ui::test::EventGenerator
& generator
= GetEventGenerator();
739 ::wm::CursorManager
* cursor_manager
=
740 ash::Shell::GetInstance()->cursor_manager();
742 generator
.MoveMouseTo(gfx::Point(0, 0));
743 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
744 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
745 generator
.PressTouch();
746 EXPECT_FALSE(cursor_manager
->IsCursorVisible());
747 EXPECT_FALSE(cursor_manager
->IsMouseEventsEnabled());
748 generator
.MoveMouseTo(gfx::Point(0, 0));
749 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
750 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
751 generator
.ReleaseTouch();
752 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
753 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
755 #endif // defined(OS_CHROMEOS) || defined(OS_WIN)
757 #if defined(OS_CHROMEOS)
758 // ChromeOS is the only platform for which the cursor is hidden on keypress
759 // (crbug.com/304296).
760 TEST_F(WindowManagerTest
, UpdateCursorVisibilityOnKeyEvent
) {
761 ui::test::EventGenerator
& generator
= GetEventGenerator();
762 ::wm::CursorManager
* cursor_manager
=
763 ash::Shell::GetInstance()->cursor_manager();
765 // Pressing a key hides the cursor but does not disable mouse events.
766 generator
.PressKey(ui::VKEY_A
, ui::EF_NONE
);
767 EXPECT_FALSE(cursor_manager
->IsCursorVisible());
768 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
769 // Moving mouse shows the cursor.
770 generator
.MoveMouseTo(gfx::Point(0, 0));
771 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
772 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
773 // Releasing a key does does not hide the cursor and does not disable mouse
775 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_NONE
);
776 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
777 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
780 // Test that pressing an accelerator does not hide the cursor.
781 TEST_F(WindowManagerTest
, UpdateCursorVisibilityAccelerator
) {
782 ui::test::EventGenerator
& generator
= GetEventGenerator();
783 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
785 ASSERT_TRUE(cursor_manager
->IsCursorVisible());
787 // Press Ctrl+A, release A first.
788 generator
.PressKey(ui::VKEY_CONTROL
, ui::EF_CONTROL_DOWN
);
789 generator
.PressKey(ui::VKEY_A
, ui::EF_CONTROL_DOWN
);
790 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_CONTROL_DOWN
);
791 generator
.ReleaseKey(ui::VKEY_CONTROL
, ui::EF_NONE
);
792 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
794 // Press Ctrl+A, release Ctrl first.
795 generator
.PressKey(ui::VKEY_CONTROL
, ui::EF_CONTROL_DOWN
);
796 generator
.PressKey(ui::VKEY_A
, ui::EF_CONTROL_DOWN
);
797 generator
.ReleaseKey(ui::VKEY_CONTROL
, ui::EF_NONE
);
798 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_NONE
);
799 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
802 TEST_F(WindowManagerTest
, TestCursorClientObserver
) {
803 ui::test::EventGenerator
& generator
= GetEventGenerator();
804 ::wm::CursorManager
* cursor_manager
=
805 ash::Shell::GetInstance()->cursor_manager();
807 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
808 SK_ColorWHITE
, -1, gfx::Rect(0, 0, 100, 100)));
809 wm::ActivateWindow(w1
.get());
811 // Add two observers. Both should have OnCursorVisibilityChanged()
812 // invoked when an event changes the visibility of the cursor.
813 TestingCursorClientObserver observer_a
;
814 TestingCursorClientObserver observer_b
;
815 cursor_manager
->AddObserver(&observer_a
);
816 cursor_manager
->AddObserver(&observer_b
);
818 // Initial state before any events have been sent.
821 EXPECT_FALSE(observer_a
.did_visibility_change());
822 EXPECT_FALSE(observer_b
.did_visibility_change());
823 EXPECT_FALSE(observer_a
.is_cursor_visible());
824 EXPECT_FALSE(observer_b
.is_cursor_visible());
826 // Keypress should hide the cursor.
827 generator
.PressKey(ui::VKEY_A
, ui::EF_NONE
);
828 EXPECT_TRUE(observer_a
.did_visibility_change());
829 EXPECT_TRUE(observer_b
.did_visibility_change());
830 EXPECT_FALSE(observer_a
.is_cursor_visible());
831 EXPECT_FALSE(observer_b
.is_cursor_visible());
833 // Mouse move should show the cursor.
836 generator
.MoveMouseTo(50, 50);
837 EXPECT_TRUE(observer_a
.did_visibility_change());
838 EXPECT_TRUE(observer_b
.did_visibility_change());
839 EXPECT_TRUE(observer_a
.is_cursor_visible());
840 EXPECT_TRUE(observer_b
.is_cursor_visible());
842 // Remove observer_b. Its OnCursorVisibilityChanged() should
843 // not be invoked past this point.
844 cursor_manager
->RemoveObserver(&observer_b
);
846 // Gesture tap should hide the cursor.
849 generator
.GestureTapAt(gfx::Point(25, 25));
850 EXPECT_TRUE(observer_a
.did_visibility_change());
851 EXPECT_FALSE(observer_b
.did_visibility_change());
852 EXPECT_FALSE(observer_a
.is_cursor_visible());
854 // Mouse move should show the cursor.
857 generator
.MoveMouseTo(50, 50);
858 EXPECT_TRUE(observer_a
.did_visibility_change());
859 EXPECT_FALSE(observer_b
.did_visibility_change());
860 EXPECT_TRUE(observer_a
.is_cursor_visible());
862 cursor_manager
->RemoveObserver(&observer_a
);
864 #endif // defined(OS_CHROMEOS)