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/test_activation_delegate.h"
9 #include "ash/wm/window_util.h"
10 #include "ui/aura/client/cursor_client_observer.h"
11 #include "ui/aura/client/focus_client.h"
12 #include "ui/aura/env.h"
13 #include "ui/aura/test/aura_test_base.h"
14 #include "ui/aura/test/test_window_delegate.h"
15 #include "ui/aura/test/test_windows.h"
16 #include "ui/base/cursor/cursor.h"
17 #include "ui/base/hit_test.h"
18 #include "ui/events/event.h"
19 #include "ui/events/event_processor.h"
20 #include "ui/events/event_utils.h"
21 #include "ui/events/test/event_generator.h"
22 #include "ui/events/test/test_event_handler.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/wm/core/compound_event_filter.h"
25 #include "ui/wm/public/activation_client.h"
26 #include "ui/wm/public/activation_delegate.h"
30 class TestingCursorClientObserver
: public aura::client::CursorClientObserver
{
32 TestingCursorClientObserver()
33 : cursor_visibility_(false),
34 did_visibility_change_(false) {}
35 void reset() { cursor_visibility_
= did_visibility_change_
= false; }
36 bool is_cursor_visible() const { return cursor_visibility_
; }
37 bool did_visibility_change() const { return did_visibility_change_
; }
39 // Overridden from aura::client::CursorClientObserver:
40 void OnCursorVisibilityChanged(bool is_visible
) override
{
41 cursor_visibility_
= is_visible
;
42 did_visibility_change_
= true;
46 bool cursor_visibility_
;
47 bool did_visibility_change_
;
49 DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver
);
52 base::TimeDelta
getTime() {
53 return ui::EventTimeForNow();
56 // A slightly changed TestEventHandler which can be configured to return a
57 // specified value for key/mouse event handling.
58 class CustomEventHandler
: public ui::test::TestEventHandler
{
61 : key_result_(ui::ER_UNHANDLED
),
62 mouse_result_(ui::ER_UNHANDLED
) {
65 ~CustomEventHandler() override
{}
67 void set_key_event_handling_result(ui::EventResult result
) {
71 void set_mouse_event_handling_result(ui::EventResult result
) {
72 mouse_result_
= result
;
75 // Overridden from ui::EventHandler:
76 void OnKeyEvent(ui::KeyEvent
* event
) override
{
77 ui::test::TestEventHandler::OnKeyEvent(event
);
78 if (key_result_
& ui::ER_HANDLED
)
80 if (key_result_
& ui::ER_CONSUMED
)
81 event
->StopPropagation();
84 void OnMouseEvent(ui::MouseEvent
* event
) override
{
85 ui::test::TestEventHandler::OnMouseEvent(event
);
86 if (mouse_result_
& ui::ER_HANDLED
)
88 if (mouse_result_
& ui::ER_CONSUMED
)
89 event
->StopPropagation();
93 ui::EventResult key_result_
;
94 ui::EventResult mouse_result_
;
96 DISALLOW_COPY_AND_ASSIGN(CustomEventHandler
);
103 typedef test::AshTestBase WindowManagerTest
;
105 class NonFocusableDelegate
: public aura::test::TestWindowDelegate
{
107 NonFocusableDelegate() {}
110 bool CanFocus() override
{ return false; }
112 DISALLOW_COPY_AND_ASSIGN(NonFocusableDelegate
);
115 class HitTestWindowDelegate
: public aura::test::TestWindowDelegate
{
117 HitTestWindowDelegate()
118 : hittest_code_(HTNOWHERE
) {
120 ~HitTestWindowDelegate() override
{}
121 void set_hittest_code(int hittest_code
) { hittest_code_
= hittest_code
; }
124 // Overridden from TestWindowDelegate:
125 int GetNonClientComponent(const gfx::Point
& point
) const override
{
126 return hittest_code_
;
131 DISALLOW_COPY_AND_ASSIGN(HitTestWindowDelegate
);
134 TEST_F(WindowManagerTest
, Focus
) {
135 // The IME event filter interferes with the basic key event propagation we
136 // attempt to do here, so we disable it.
139 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
140 root_window
->SetBounds(gfx::Rect(0, 0, 510, 510));
142 // Supplied ids are negative so as not to collide with shell ids.
143 // TODO(beng): maybe introduce a MAKE_SHELL_ID() macro that generates a safe
144 // id beyond shell id max?
145 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
146 SK_ColorWHITE
, -1, gfx::Rect(10, 10, 500, 500)));
147 scoped_ptr
<aura::Window
> w11(aura::test::CreateTestWindow(
148 SK_ColorGREEN
, -11, gfx::Rect(5, 5, 100, 100), w1
.get()));
149 scoped_ptr
<aura::Window
> w111(aura::test::CreateTestWindow(
150 SK_ColorCYAN
, -111, gfx::Rect(5, 5, 75, 75), w11
.get()));
151 scoped_ptr
<aura::Window
> w1111(aura::test::CreateTestWindow(
152 SK_ColorRED
, -1111, gfx::Rect(5, 5, 50, 50), w111
.get()));
153 scoped_ptr
<aura::Window
> w12(aura::test::CreateTestWindow(
154 SK_ColorMAGENTA
, -12, gfx::Rect(10, 420, 25, 25), w1
.get()));
155 aura::test::ColorTestWindowDelegate
* w121delegate
=
156 new aura::test::ColorTestWindowDelegate(SK_ColorYELLOW
);
157 scoped_ptr
<aura::Window
> w121(aura::test::CreateTestWindowWithDelegate(
158 w121delegate
, -121, gfx::Rect(5, 5, 5, 5), w12
.get()));
159 aura::test::ColorTestWindowDelegate
* w122delegate
=
160 new aura::test::ColorTestWindowDelegate(SK_ColorRED
);
161 scoped_ptr
<aura::Window
> w122(aura::test::CreateTestWindowWithDelegate(
162 w122delegate
, -122, gfx::Rect(10, 5, 5, 5), w12
.get()));
163 aura::test::ColorTestWindowDelegate
* w123delegate
=
164 new aura::test::ColorTestWindowDelegate(SK_ColorRED
);
165 scoped_ptr
<aura::Window
> w123(aura::test::CreateTestWindowWithDelegate(
166 w123delegate
, -123, gfx::Rect(15, 5, 5, 5), w12
.get()));
167 scoped_ptr
<aura::Window
> w13(aura::test::CreateTestWindow(
168 SK_ColorGRAY
, -13, gfx::Rect(5, 470, 50, 50), w1
.get()));
170 // Click on a sub-window (w121) to focus it.
171 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w121
.get());
172 generator
.ClickLeftButton();
174 aura::client::FocusClient
* focus_client
=
175 aura::client::GetFocusClient(w121
.get());
176 EXPECT_EQ(w121
.get(), focus_client
->GetFocusedWindow());
178 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
180 // The key press should be sent to the focused sub-window.
181 ui::KeyEvent
keyev(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
182 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&keyev
);
183 ASSERT_FALSE(details
.dispatcher_destroyed
);
184 EXPECT_EQ(ui::VKEY_E
, w121delegate
->last_key_code());
186 // Touch on a sub-window (w122) to focus it.
187 gfx::Point click_point
= w122
->bounds().CenterPoint();
188 aura::Window::ConvertPointToTarget(w122
->parent(), root_window
, &click_point
);
189 ui::TouchEvent
touchev(ui::ET_TOUCH_PRESSED
, click_point
, 0, getTime());
190 details
= dispatcher
->OnEventFromSource(&touchev
);
191 ASSERT_FALSE(details
.dispatcher_destroyed
);
192 focus_client
= aura::client::GetFocusClient(w122
.get());
193 EXPECT_EQ(w122
.get(), focus_client
->GetFocusedWindow());
195 // The key press should be sent to the focused sub-window.
196 keyev
= ui::KeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
197 details
= dispatcher
->OnEventFromSource(&keyev
);
198 ASSERT_FALSE(details
.dispatcher_destroyed
);
199 EXPECT_EQ(ui::VKEY_E
, w122delegate
->last_key_code());
201 // Hiding the focused window will set the focus to its parent if
204 EXPECT_EQ(aura::client::GetFocusClient(w12
.get()),
205 aura::client::GetFocusClient(w122
.get()));
207 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
209 // Sets the focus back to w122.
212 EXPECT_EQ(w122
.get(),
213 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
215 // Removing the focused window from parent should set the focus to
216 // its parent if it's focusable.
217 w12
->RemoveChild(w122
.get());
218 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w122
.get()));
220 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
222 // Set the focus to w123, but make the w1 not activatable.
223 test::TestActivationDelegate
activation_delegate(false);
225 EXPECT_EQ(w123
.get(),
226 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
227 aura::client::SetActivationDelegate(w1
.get(), &activation_delegate
);
229 // Hiding the focused window will set the focus to NULL because
230 // parent window is not focusable.
232 EXPECT_EQ(aura::client::GetFocusClient(w12
.get()),
233 aura::client::GetFocusClient(w123
.get()));
234 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
235 keyev
= ui::KeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
236 details
= dispatcher
->OnEventFromSource(&keyev
);
237 EXPECT_FALSE(keyev
.handled() || details
.dispatcher_destroyed
);
239 // Set the focus back to w123
240 aura::client::SetActivationDelegate(w1
.get(), NULL
);
243 EXPECT_EQ(w123
.get(),
244 aura::client::GetFocusClient(w12
.get())->GetFocusedWindow());
245 aura::client::SetActivationDelegate(w1
.get(), &activation_delegate
);
247 // Removing the focused window will set the focus to NULL because
248 // parent window is not focusable.
249 w12
->RemoveChild(w123
.get());
250 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w123
.get()));
251 keyev
= ui::KeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_E
, ui::EF_NONE
);
252 details
= dispatcher
->OnEventFromSource(&keyev
);
253 EXPECT_FALSE(keyev
.handled() || details
.dispatcher_destroyed
);
255 // Must set to NULL since the activation delegate will be destroyed before
257 aura::client::SetActivationDelegate(w1
.get(), NULL
);
260 // Various assertion testing for activating windows.
261 TEST_F(WindowManagerTest
, ActivateOnMouse
) {
262 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
264 test::TestActivationDelegate d1
;
265 aura::test::TestWindowDelegate wd
;
266 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShellWithDelegate(
267 &wd
, -1, gfx::Rect(10, 10, 50, 50)));
268 d1
.SetWindow(w1
.get());
269 test::TestActivationDelegate d2
;
270 scoped_ptr
<aura::Window
> w2(CreateTestWindowInShellWithDelegate(
271 &wd
, -2, gfx::Rect(70, 70, 50, 50)));
272 d2
.SetWindow(w2
.get());
274 aura::client::FocusClient
* focus_client
=
275 aura::client::GetFocusClient(w1
.get());
281 wm::ActivateWindow(w1
.get());
282 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
283 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
284 EXPECT_EQ(1, d1
.activated_count());
285 EXPECT_EQ(0, d1
.lost_active_count());
290 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w2
.get());
291 generator
.ClickLeftButton();
293 // Window2 should have become active.
294 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
295 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
296 EXPECT_EQ(0, d1
.activated_count());
297 EXPECT_EQ(1, d1
.lost_active_count());
298 EXPECT_EQ(1, d2
.activated_count());
299 EXPECT_EQ(0, d2
.lost_active_count());
305 // Click back on window1, but set it up so w1 doesn't activate on click.
306 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w1
.get());
307 d1
.set_activate(false);
308 generator
.ClickLeftButton();
310 // Window2 should still be active and focused.
311 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
312 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
313 EXPECT_EQ(0, d1
.activated_count());
314 EXPECT_EQ(0, d1
.lost_active_count());
315 EXPECT_EQ(0, d2
.activated_count());
316 EXPECT_EQ(0, d2
.lost_active_count());
321 // Destroy window2, this should make window1 active.
322 d1
.set_activate(true);
324 EXPECT_EQ(0, d2
.activated_count());
325 EXPECT_EQ(1, d2
.lost_active_count());
326 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
327 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
328 EXPECT_EQ(1, d1
.activated_count());
329 EXPECT_EQ(0, d1
.lost_active_count());
331 // Clicking an active window with a child shouldn't steal the
332 // focus from the child.
334 scoped_ptr
<aura::Window
> w11(CreateTestWindowWithDelegate(
335 &wd
, -11, gfx::Rect(10, 10, 10, 10), w1
.get()));
336 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
338 // First set the focus to the child |w11|.
339 generator
.ClickLeftButton();
340 EXPECT_EQ(w11
.get(), focus_client
->GetFocusedWindow());
341 EXPECT_EQ(w1
.get(), wm::GetActiveWindow());
343 // Then click the parent active window. The focus shouldn't move.
344 gfx::Point left_top
= w1
->bounds().origin();
345 aura::Window::ConvertPointToTarget(w1
->parent(), root_window
, &left_top
);
346 left_top
.Offset(1, 1);
347 generator
.MoveMouseTo(left_top
);
348 generator
.ClickLeftButton();
349 EXPECT_EQ(w11
.get(), focus_client
->GetFocusedWindow());
350 EXPECT_EQ(w1
.get(), wm::GetActiveWindow());
353 // Clicking on a non-focusable window inside a background window should still
354 // give focus to the background window.
356 NonFocusableDelegate nfd
;
357 scoped_ptr
<aura::Window
> w11(CreateTestWindowWithDelegate(
358 &nfd
, -1, gfx::Rect(10, 10, 10, 10), w1
.get()));
359 // Move focus to |w2| first.
360 scoped_ptr
<aura::Window
> w2(CreateTestWindowInShellWithDelegate(
361 &wd
, -1, gfx::Rect(70, 70, 50, 50)));
362 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(), w2
.get());
363 generator
.ClickLeftButton();
364 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
365 EXPECT_FALSE(w11
->CanFocus());
367 // Click on |w11|. This should focus w1.
368 generator
.MoveMouseToCenterOf(w11
.get());
369 generator
.ClickLeftButton();
370 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
374 TEST_F(WindowManagerTest
, PanelActivation
) {
375 aura::test::TestWindowDelegate wd
;
376 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShellWithDelegate(
377 &wd
, -1, gfx::Rect(10, 10, 50, 50)));
378 aura::test::TestWindowDelegate pd
;
379 scoped_ptr
<aura::Window
> p1(CreateTestWindowInShellWithDelegateAndType(
380 &pd
, ui::wm::WINDOW_TYPE_PANEL
, -1, gfx::Rect(10, 10, 50, 50)));
381 aura::client::FocusClient
* focus_client
=
382 aura::client::GetFocusClient(w1
.get());
385 wm::ActivateWindow(w1
.get());
386 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
389 wm::ActivateWindow(p1
.get());
390 EXPECT_TRUE(wm::IsActiveWindow(p1
.get()));
391 EXPECT_EQ(p1
.get(), focus_client
->GetFocusedWindow());
394 wm::ActivateWindow(w1
.get());
395 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
396 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
398 // Clicking on a non-activatable window should not change the active window.
400 NonFocusableDelegate nfd
;
401 scoped_ptr
<aura::Window
> w3(CreateTestWindowInShellWithDelegate(
402 &nfd
, -1, gfx::Rect(70, 70, 50, 50)));
403 ui::test::EventGenerator
generator3(Shell::GetPrimaryRootWindow(),
405 wm::ActivateWindow(p1
.get());
406 EXPECT_TRUE(wm::IsActiveWindow(p1
.get()));
407 generator3
.ClickLeftButton();
408 EXPECT_TRUE(wm::IsActiveWindow(p1
.get()));
412 // Essentially the same as ActivateOnMouse, but for touch events.
413 TEST_F(WindowManagerTest
, ActivateOnTouch
) {
414 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
416 test::TestActivationDelegate d1
;
417 aura::test::TestWindowDelegate wd
;
418 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShellWithDelegate(
419 &wd
, -1, gfx::Rect(10, 10, 50, 50)));
420 d1
.SetWindow(w1
.get());
421 test::TestActivationDelegate d2
;
422 scoped_ptr
<aura::Window
> w2(CreateTestWindowInShellWithDelegate(
423 &wd
, -2, gfx::Rect(70, 70, 50, 50)));
424 d2
.SetWindow(w2
.get());
426 aura::client::FocusClient
* focus_client
=
427 aura::client::GetFocusClient(w1
.get());
433 wm::ActivateWindow(w1
.get());
434 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
435 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
436 EXPECT_EQ(1, d1
.activated_count());
437 EXPECT_EQ(0, d1
.lost_active_count());
441 gfx::Point press_point
= w2
->bounds().CenterPoint();
442 aura::Window::ConvertPointToTarget(w2
->parent(), root_window
, &press_point
);
443 ui::TouchEvent
touchev1(ui::ET_TOUCH_PRESSED
, press_point
, 0, getTime());
445 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
446 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&touchev1
);
447 ASSERT_FALSE(details
.dispatcher_destroyed
);
449 // Window2 should have become active.
450 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
451 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
452 EXPECT_EQ(0, d1
.activated_count());
453 EXPECT_EQ(1, d1
.lost_active_count());
454 EXPECT_EQ(1, d2
.activated_count());
455 EXPECT_EQ(0, d2
.lost_active_count());
459 // Touch window1, but set it up so w1 doesn't activate on touch.
460 press_point
= w1
->bounds().CenterPoint();
461 aura::Window::ConvertPointToTarget(w1
->parent(), root_window
, &press_point
);
462 d1
.set_activate(false);
463 ui::TouchEvent
touchev2(ui::ET_TOUCH_PRESSED
, press_point
, 1, getTime());
464 details
= dispatcher
->OnEventFromSource(&touchev2
);
465 ASSERT_FALSE(details
.dispatcher_destroyed
);
467 // Window2 should still be active and focused.
468 EXPECT_TRUE(wm::IsActiveWindow(w2
.get()));
469 EXPECT_EQ(w2
.get(), focus_client
->GetFocusedWindow());
470 EXPECT_EQ(0, d1
.activated_count());
471 EXPECT_EQ(0, d1
.lost_active_count());
472 EXPECT_EQ(0, d2
.activated_count());
473 EXPECT_EQ(0, d2
.lost_active_count());
477 // Destroy window2, this should make window1 active.
478 d1
.set_activate(true);
480 EXPECT_EQ(0, d2
.activated_count());
481 EXPECT_EQ(1, d2
.lost_active_count());
482 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
483 EXPECT_EQ(w1
.get(), focus_client
->GetFocusedWindow());
484 EXPECT_EQ(1, d1
.activated_count());
485 EXPECT_EQ(0, d1
.lost_active_count());
488 TEST_F(WindowManagerTest
, MouseEventCursors
) {
489 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
492 const int kWindowLeft
= 123;
493 const int kWindowTop
= 45;
494 HitTestWindowDelegate window_delegate
;
495 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithDelegate(
498 gfx::Rect(kWindowLeft
, kWindowTop
, 640, 480)));
500 // Create two mouse movement events we can switch between.
501 gfx::Point
point1(kWindowLeft
, kWindowTop
);
502 aura::Window::ConvertPointToTarget(window
->parent(), root_window
, &point1
);
504 gfx::Point
point2(kWindowLeft
+ 1, kWindowTop
+ 1);
505 aura::Window::ConvertPointToTarget(window
->parent(), root_window
, &point2
);
507 aura::WindowTreeHost
* host
= root_window
->GetHost();
508 ui::EventProcessor
* dispatcher
= host
->event_processor();
510 // Cursor starts as a pointer (set during Shell::Init()).
511 EXPECT_EQ(ui::kCursorPointer
, host
->last_cursor().native_type());
514 // Resize edges and corners show proper cursors.
515 window_delegate
.set_hittest_code(HTBOTTOM
);
516 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
517 ui::EventTimeForNow(), 0, 0);
518 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
519 ASSERT_FALSE(details
.dispatcher_destroyed
);
520 EXPECT_EQ(ui::kCursorSouthResize
, host
->last_cursor().native_type());
524 window_delegate
.set_hittest_code(HTBOTTOMLEFT
);
525 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
526 ui::EventTimeForNow(), 0, 0);
527 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
528 ASSERT_FALSE(details
.dispatcher_destroyed
);
529 EXPECT_EQ(ui::kCursorSouthWestResize
, host
->last_cursor().native_type());
533 window_delegate
.set_hittest_code(HTBOTTOMRIGHT
);
534 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
535 ui::EventTimeForNow(), 0, 0);
536 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
537 ASSERT_FALSE(details
.dispatcher_destroyed
);
538 EXPECT_EQ(ui::kCursorSouthEastResize
, host
->last_cursor().native_type());
542 window_delegate
.set_hittest_code(HTLEFT
);
543 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
544 ui::EventTimeForNow(), 0, 0);
545 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
546 ASSERT_FALSE(details
.dispatcher_destroyed
);
547 EXPECT_EQ(ui::kCursorWestResize
, host
->last_cursor().native_type());
551 window_delegate
.set_hittest_code(HTRIGHT
);
552 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
553 ui::EventTimeForNow(), 0, 0);
554 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
555 ASSERT_FALSE(details
.dispatcher_destroyed
);
556 EXPECT_EQ(ui::kCursorEastResize
, host
->last_cursor().native_type());
560 window_delegate
.set_hittest_code(HTTOP
);
561 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
562 ui::EventTimeForNow(), 0, 0);
563 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
564 ASSERT_FALSE(details
.dispatcher_destroyed
);
565 EXPECT_EQ(ui::kCursorNorthResize
, host
->last_cursor().native_type());
569 window_delegate
.set_hittest_code(HTTOPLEFT
);
570 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
571 ui::EventTimeForNow(), 0, 0);
572 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
573 ASSERT_FALSE(details
.dispatcher_destroyed
);
574 EXPECT_EQ(ui::kCursorNorthWestResize
, host
->last_cursor().native_type());
578 window_delegate
.set_hittest_code(HTTOPRIGHT
);
579 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, point2
, point2
,
580 ui::EventTimeForNow(), 0, 0);
581 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move2
);
582 ASSERT_FALSE(details
.dispatcher_destroyed
);
583 EXPECT_EQ(ui::kCursorNorthEastResize
, host
->last_cursor().native_type());
587 // Client area uses null cursor.
588 window_delegate
.set_hittest_code(HTCLIENT
);
589 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, point1
, point1
,
590 ui::EventTimeForNow(), 0, 0);
591 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&move1
);
592 ASSERT_FALSE(details
.dispatcher_destroyed
);
593 EXPECT_EQ(ui::kCursorNull
, host
->last_cursor().native_type());
598 #define MAYBE_TransformActivate DISABLED_TransformActivate
600 #define MAYBE_TransformActivate TransformActivate
602 TEST_F(WindowManagerTest
, MAYBE_TransformActivate
) {
603 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
604 gfx::Size size
= root_window
->bounds().size();
605 EXPECT_EQ(gfx::Rect(size
).ToString(),
606 Shell::GetScreen()->GetDisplayNearestPoint(
607 gfx::Point()).bounds().ToString());
609 // Rotate it clock-wise 90 degrees.
610 gfx::Transform transform
;
611 transform
.Translate(size
.width(), 0);
612 transform
.Rotate(90.0f
);
613 root_window
->GetHost()->SetRootTransform(transform
);
615 test::TestActivationDelegate d1
;
616 aura::test::TestWindowDelegate wd
;
617 scoped_ptr
<aura::Window
> w1(
618 CreateTestWindowInShellWithDelegate(&wd
, 1, gfx::Rect(0, 15, 50, 50)));
619 d1
.SetWindow(w1
.get());
622 gfx::Point
miss_point(5, 5);
623 transform
.TransformPoint(&miss_point
);
624 ui::MouseEvent
mouseev1(ui::ET_MOUSE_PRESSED
, miss_point
, miss_point
,
625 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
626 ui::EF_LEFT_MOUSE_BUTTON
);
627 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
628 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&mouseev1
);
629 ASSERT_FALSE(details
.dispatcher_destroyed
);
630 EXPECT_EQ(NULL
, aura::client::GetFocusClient(w1
.get())->GetFocusedWindow());
631 ui::MouseEvent
mouseup(ui::ET_MOUSE_RELEASED
, miss_point
, miss_point
,
632 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
633 ui::EF_LEFT_MOUSE_BUTTON
);
634 details
= dispatcher
->OnEventFromSource(&mouseup
);
635 ASSERT_FALSE(details
.dispatcher_destroyed
);
637 gfx::Point
hit_point(5, 15);
638 transform
.TransformPoint(&hit_point
);
639 ui::MouseEvent
mouseev2(ui::ET_MOUSE_PRESSED
, hit_point
, hit_point
,
640 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
641 ui::EF_LEFT_MOUSE_BUTTON
);
642 details
= dispatcher
->OnEventFromSource(&mouseev2
);
643 ASSERT_FALSE(details
.dispatcher_destroyed
);
644 EXPECT_TRUE(wm::IsActiveWindow(w1
.get()));
646 aura::client::GetFocusClient(w1
.get())->GetFocusedWindow());
649 TEST_F(WindowManagerTest
, AdditionalFilters
) {
650 // The IME event filter interferes with the basic key event propagation we
651 // attempt to do here, so we disable it.
653 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
655 // Creates a window and make it active
656 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
657 SK_ColorWHITE
, -1, gfx::Rect(0, 0, 100, 100)));
658 wm::ActivateWindow(w1
.get());
660 // Creates two addition filters
661 scoped_ptr
<CustomEventHandler
> f1(new CustomEventHandler
);
662 scoped_ptr
<CustomEventHandler
> f2(new CustomEventHandler
);
664 // Adds them to root window event filter.
665 ::wm::CompoundEventFilter
* env_filter
=
666 Shell::GetInstance()->env_filter();
667 env_filter
->AddHandler(f1
.get());
668 env_filter
->AddHandler(f2
.get());
670 // Dispatches mouse and keyboard events.
671 ui::KeyEvent
key_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
672 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
673 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&key_event
);
674 ASSERT_FALSE(details
.dispatcher_destroyed
);
675 ui::MouseEvent
mouse_pressed(ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0),
676 gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
677 details
= dispatcher
->OnEventFromSource(&mouse_pressed
);
678 ASSERT_FALSE(details
.dispatcher_destroyed
);
680 // Both filters should get the events.
681 EXPECT_EQ(1, f1
->num_key_events());
682 EXPECT_EQ(1, f1
->num_mouse_events());
683 EXPECT_EQ(1, f2
->num_key_events());
684 EXPECT_EQ(1, f2
->num_mouse_events());
689 // Makes f1 consume events.
690 f1
->set_key_event_handling_result(ui::ER_CONSUMED
);
691 f1
->set_mouse_event_handling_result(ui::ER_CONSUMED
);
693 // Dispatches events.
694 details
= dispatcher
->OnEventFromSource(&key_event
);
695 ASSERT_FALSE(details
.dispatcher_destroyed
);
696 ui::MouseEvent
mouse_released(ui::ET_MOUSE_RELEASED
, gfx::Point(0, 0),
697 gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
698 details
= dispatcher
->OnEventFromSource(&mouse_released
);
699 ASSERT_FALSE(details
.dispatcher_destroyed
);
701 // f1 should still get the events but f2 no longer gets them.
702 EXPECT_EQ(1, f1
->num_key_events());
703 EXPECT_EQ(1, f1
->num_mouse_events());
704 EXPECT_EQ(0, f2
->num_key_events());
705 EXPECT_EQ(0, f2
->num_mouse_events());
710 // Remove f1 from additonal filters list.
711 env_filter
->RemoveHandler(f1
.get());
713 // Dispatches events.
714 details
= dispatcher
->OnEventFromSource(&key_event
);
715 ASSERT_FALSE(details
.dispatcher_destroyed
);
716 details
= dispatcher
->OnEventFromSource(&mouse_pressed
);
717 ASSERT_FALSE(details
.dispatcher_destroyed
);
719 // f1 should get no events since it's out and f2 should get them.
720 EXPECT_EQ(0, f1
->num_key_events());
721 EXPECT_EQ(0, f1
->num_mouse_events());
722 EXPECT_EQ(1, f2
->num_key_events());
723 EXPECT_EQ(1, f2
->num_mouse_events());
725 env_filter
->RemoveHandler(f2
.get());
728 #if defined(OS_CHROMEOS) || defined(OS_WIN)
729 // Touch visually hides the cursor on ChromeOS and Windows
730 TEST_F(WindowManagerTest
, UpdateCursorVisibility
) {
731 ui::test::EventGenerator
& generator
= GetEventGenerator();
732 ::wm::CursorManager
* cursor_manager
=
733 ash::Shell::GetInstance()->cursor_manager();
735 generator
.MoveMouseTo(gfx::Point(0, 0));
736 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
737 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
738 generator
.PressTouch();
739 EXPECT_FALSE(cursor_manager
->IsCursorVisible());
740 EXPECT_FALSE(cursor_manager
->IsMouseEventsEnabled());
741 generator
.MoveMouseTo(gfx::Point(0, 0));
742 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
743 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
744 generator
.ReleaseTouch();
745 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
746 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
748 #endif // defined(OS_CHROMEOS) || defined(OS_WIN)
750 #if defined(OS_CHROMEOS)
751 // ChromeOS is the only platform for which the cursor is hidden on keypress
752 // (crbug.com/304296).
753 TEST_F(WindowManagerTest
, UpdateCursorVisibilityOnKeyEvent
) {
754 ui::test::EventGenerator
& generator
= GetEventGenerator();
755 ::wm::CursorManager
* cursor_manager
=
756 ash::Shell::GetInstance()->cursor_manager();
758 // Pressing a key hides the cursor but does not disable mouse events.
759 generator
.PressKey(ui::VKEY_A
, ui::EF_NONE
);
760 EXPECT_FALSE(cursor_manager
->IsCursorVisible());
761 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
762 // Moving mouse shows the cursor.
763 generator
.MoveMouseTo(gfx::Point(0, 0));
764 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
765 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
766 // Releasing a key does does not hide the cursor and does not disable mouse
768 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_NONE
);
769 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
770 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
773 // Test that pressing an accelerator does not hide the cursor.
774 TEST_F(WindowManagerTest
, UpdateCursorVisibilityAccelerator
) {
775 ui::test::EventGenerator
& generator
= GetEventGenerator();
776 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
778 ASSERT_TRUE(cursor_manager
->IsCursorVisible());
780 // Press Ctrl+A, release A first.
781 generator
.PressKey(ui::VKEY_CONTROL
, ui::EF_CONTROL_DOWN
);
782 generator
.PressKey(ui::VKEY_A
, ui::EF_CONTROL_DOWN
);
783 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_CONTROL_DOWN
);
784 generator
.ReleaseKey(ui::VKEY_CONTROL
, ui::EF_NONE
);
785 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
787 // Press Ctrl+A, release Ctrl 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_CONTROL
, ui::EF_NONE
);
791 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_NONE
);
792 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
795 TEST_F(WindowManagerTest
, TestCursorClientObserver
) {
796 ui::test::EventGenerator
& generator
= GetEventGenerator();
797 ::wm::CursorManager
* cursor_manager
=
798 ash::Shell::GetInstance()->cursor_manager();
800 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
801 SK_ColorWHITE
, -1, gfx::Rect(0, 0, 100, 100)));
802 wm::ActivateWindow(w1
.get());
804 // Add two observers. Both should have OnCursorVisibilityChanged()
805 // invoked when an event changes the visibility of the cursor.
806 TestingCursorClientObserver observer_a
;
807 TestingCursorClientObserver observer_b
;
808 cursor_manager
->AddObserver(&observer_a
);
809 cursor_manager
->AddObserver(&observer_b
);
811 // Initial state before any events have been sent.
814 EXPECT_FALSE(observer_a
.did_visibility_change());
815 EXPECT_FALSE(observer_b
.did_visibility_change());
816 EXPECT_FALSE(observer_a
.is_cursor_visible());
817 EXPECT_FALSE(observer_b
.is_cursor_visible());
819 // Keypress should hide the cursor.
820 generator
.PressKey(ui::VKEY_A
, ui::EF_NONE
);
821 EXPECT_TRUE(observer_a
.did_visibility_change());
822 EXPECT_TRUE(observer_b
.did_visibility_change());
823 EXPECT_FALSE(observer_a
.is_cursor_visible());
824 EXPECT_FALSE(observer_b
.is_cursor_visible());
826 // Mouse move should show the cursor.
829 generator
.MoveMouseTo(50, 50);
830 EXPECT_TRUE(observer_a
.did_visibility_change());
831 EXPECT_TRUE(observer_b
.did_visibility_change());
832 EXPECT_TRUE(observer_a
.is_cursor_visible());
833 EXPECT_TRUE(observer_b
.is_cursor_visible());
835 // Remove observer_b. Its OnCursorVisibilityChanged() should
836 // not be invoked past this point.
837 cursor_manager
->RemoveObserver(&observer_b
);
839 // Gesture tap should hide the cursor.
842 generator
.GestureTapAt(gfx::Point(25, 25));
843 EXPECT_TRUE(observer_a
.did_visibility_change());
844 EXPECT_FALSE(observer_b
.did_visibility_change());
845 EXPECT_FALSE(observer_a
.is_cursor_visible());
847 // Mouse move should show the cursor.
850 generator
.MoveMouseTo(50, 50);
851 EXPECT_TRUE(observer_a
.did_visibility_change());
852 EXPECT_FALSE(observer_b
.did_visibility_change());
853 EXPECT_TRUE(observer_a
.is_cursor_visible());
855 cursor_manager
->RemoveObserver(&observer_a
);
857 #endif // defined(OS_CHROMEOS)