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
);
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 remove it.
652 test::ShellTestApi
shell_test(Shell::GetInstance());
653 Shell::GetInstance()->RemovePreTargetHandler(
654 shell_test
.input_method_event_filter());
656 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
658 // Creates a window and make it active
659 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
660 SK_ColorWHITE
, -1, gfx::Rect(0, 0, 100, 100)));
661 wm::ActivateWindow(w1
.get());
663 // Creates two addition filters
664 scoped_ptr
<CustomEventHandler
> f1(new CustomEventHandler
);
665 scoped_ptr
<CustomEventHandler
> f2(new CustomEventHandler
);
667 // Adds them to root window event filter.
668 ::wm::CompoundEventFilter
* env_filter
=
669 Shell::GetInstance()->env_filter();
670 env_filter
->AddHandler(f1
.get());
671 env_filter
->AddHandler(f2
.get());
673 // Dispatches mouse and keyboard events.
674 ui::KeyEvent
key_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
675 ui::EventProcessor
* dispatcher
= root_window
->GetHost()->event_processor();
676 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&key_event
);
677 ASSERT_FALSE(details
.dispatcher_destroyed
);
678 ui::MouseEvent
mouse_pressed(ui::ET_MOUSE_PRESSED
, gfx::Point(0, 0),
679 gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
680 details
= dispatcher
->OnEventFromSource(&mouse_pressed
);
681 ASSERT_FALSE(details
.dispatcher_destroyed
);
683 // Both filters should get the events.
684 EXPECT_EQ(1, f1
->num_key_events());
685 EXPECT_EQ(1, f1
->num_mouse_events());
686 EXPECT_EQ(1, f2
->num_key_events());
687 EXPECT_EQ(1, f2
->num_mouse_events());
692 // Makes f1 consume events.
693 f1
->set_key_event_handling_result(ui::ER_CONSUMED
);
694 f1
->set_mouse_event_handling_result(ui::ER_CONSUMED
);
696 // Dispatches events.
697 details
= dispatcher
->OnEventFromSource(&key_event
);
698 ASSERT_FALSE(details
.dispatcher_destroyed
);
699 ui::MouseEvent
mouse_released(ui::ET_MOUSE_RELEASED
, gfx::Point(0, 0),
700 gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
701 details
= dispatcher
->OnEventFromSource(&mouse_released
);
702 ASSERT_FALSE(details
.dispatcher_destroyed
);
704 // f1 should still get the events but f2 no longer gets them.
705 EXPECT_EQ(1, f1
->num_key_events());
706 EXPECT_EQ(1, f1
->num_mouse_events());
707 EXPECT_EQ(0, f2
->num_key_events());
708 EXPECT_EQ(0, f2
->num_mouse_events());
713 // Remove f1 from additonal filters list.
714 env_filter
->RemoveHandler(f1
.get());
716 // Dispatches events.
717 details
= dispatcher
->OnEventFromSource(&key_event
);
718 ASSERT_FALSE(details
.dispatcher_destroyed
);
719 details
= dispatcher
->OnEventFromSource(&mouse_pressed
);
720 ASSERT_FALSE(details
.dispatcher_destroyed
);
722 // f1 should get no events since it's out and f2 should get them.
723 EXPECT_EQ(0, f1
->num_key_events());
724 EXPECT_EQ(0, f1
->num_mouse_events());
725 EXPECT_EQ(1, f2
->num_key_events());
726 EXPECT_EQ(1, f2
->num_mouse_events());
728 env_filter
->RemoveHandler(f2
.get());
731 #if defined(OS_CHROMEOS) || defined(OS_WIN)
732 // Touch visually hides the cursor on ChromeOS and Windows
733 TEST_F(WindowManagerTest
, UpdateCursorVisibility
) {
734 ui::test::EventGenerator
& generator
= GetEventGenerator();
735 ::wm::CursorManager
* cursor_manager
=
736 ash::Shell::GetInstance()->cursor_manager();
738 generator
.MoveMouseTo(gfx::Point(0, 0));
739 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
740 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
741 generator
.PressTouch();
742 EXPECT_FALSE(cursor_manager
->IsCursorVisible());
743 EXPECT_FALSE(cursor_manager
->IsMouseEventsEnabled());
744 generator
.MoveMouseTo(gfx::Point(0, 0));
745 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
746 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
747 generator
.ReleaseTouch();
748 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
749 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
751 #endif // defined(OS_CHROMEOS) || defined(OS_WIN)
753 #if defined(OS_CHROMEOS)
754 // ChromeOS is the only platform for which the cursor is hidden on keypress
755 // (crbug.com/304296).
756 TEST_F(WindowManagerTest
, UpdateCursorVisibilityOnKeyEvent
) {
757 ui::test::EventGenerator
& generator
= GetEventGenerator();
758 ::wm::CursorManager
* cursor_manager
=
759 ash::Shell::GetInstance()->cursor_manager();
761 // Pressing a key hides the cursor but does not disable mouse events.
762 generator
.PressKey(ui::VKEY_A
, ui::EF_NONE
);
763 EXPECT_FALSE(cursor_manager
->IsCursorVisible());
764 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
765 // Moving mouse shows the cursor.
766 generator
.MoveMouseTo(gfx::Point(0, 0));
767 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
768 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
769 // Releasing a key also hides the cursor but does not disable mouse events.
770 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_NONE
);
771 EXPECT_FALSE(cursor_manager
->IsCursorVisible());
772 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
773 // Moving mouse shows the cursor again.
774 generator
.MoveMouseTo(gfx::Point(0, 0));
775 EXPECT_TRUE(cursor_manager
->IsCursorVisible());
776 EXPECT_TRUE(cursor_manager
->IsMouseEventsEnabled());
779 TEST_F(WindowManagerTest
, TestCursorClientObserver
) {
780 ui::test::EventGenerator
& generator
= GetEventGenerator();
781 ::wm::CursorManager
* cursor_manager
=
782 ash::Shell::GetInstance()->cursor_manager();
784 scoped_ptr
<aura::Window
> w1(CreateTestWindowInShell(
785 SK_ColorWHITE
, -1, gfx::Rect(0, 0, 100, 100)));
786 wm::ActivateWindow(w1
.get());
788 // Add two observers. Both should have OnCursorVisibilityChanged()
789 // invoked when an event changes the visibility of the cursor.
790 TestingCursorClientObserver observer_a
;
791 TestingCursorClientObserver observer_b
;
792 cursor_manager
->AddObserver(&observer_a
);
793 cursor_manager
->AddObserver(&observer_b
);
795 // Initial state before any events have been sent.
798 EXPECT_FALSE(observer_a
.did_visibility_change());
799 EXPECT_FALSE(observer_b
.did_visibility_change());
800 EXPECT_FALSE(observer_a
.is_cursor_visible());
801 EXPECT_FALSE(observer_b
.is_cursor_visible());
803 // Keypress should hide the cursor.
804 generator
.PressKey(ui::VKEY_A
, ui::EF_NONE
);
805 EXPECT_TRUE(observer_a
.did_visibility_change());
806 EXPECT_TRUE(observer_b
.did_visibility_change());
807 EXPECT_FALSE(observer_a
.is_cursor_visible());
808 EXPECT_FALSE(observer_b
.is_cursor_visible());
810 // Mouse move should show the cursor.
813 generator
.MoveMouseTo(50, 50);
814 EXPECT_TRUE(observer_a
.did_visibility_change());
815 EXPECT_TRUE(observer_b
.did_visibility_change());
816 EXPECT_TRUE(observer_a
.is_cursor_visible());
817 EXPECT_TRUE(observer_b
.is_cursor_visible());
819 // Remove observer_b. Its OnCursorVisibilityChanged() should
820 // not be invoked past this point.
821 cursor_manager
->RemoveObserver(&observer_b
);
823 // Gesture tap should hide the cursor.
826 generator
.GestureTapAt(gfx::Point(25, 25));
827 EXPECT_TRUE(observer_a
.did_visibility_change());
828 EXPECT_FALSE(observer_b
.did_visibility_change());
829 EXPECT_FALSE(observer_a
.is_cursor_visible());
831 // Mouse move should show the cursor.
834 generator
.MoveMouseTo(50, 50);
835 EXPECT_TRUE(observer_a
.did_visibility_change());
836 EXPECT_FALSE(observer_b
.did_visibility_change());
837 EXPECT_TRUE(observer_a
.is_cursor_visible());
839 cursor_manager
->RemoveObserver(&observer_a
);
841 #endif // defined(OS_CHROMEOS)