Revert of Rename isSystemLocationEnabled to isLocationEnabled. (patchset #1 id:1...
[chromium-blink-merge.git] / ash / wm / window_manager_unittest.cc
blob6a51a0f990976d5ea99aba37843432ba1a9a1b02
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/shell.h"
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"
30 namespace {
32 class TestingCursorClientObserver : public aura::client::CursorClientObserver {
33 public:
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;
47 private:
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 {
61 public:
62 CustomEventHandler()
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) {
70 key_result_ = 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)
81 event->SetHandled();
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)
89 event->SetHandled();
90 if (mouse_result_ & ui::ER_CONSUMED)
91 event->StopPropagation();
94 private:
95 ui::EventResult key_result_;
96 ui::EventResult mouse_result_;
98 DISALLOW_COPY_AND_ASSIGN(CustomEventHandler);
101 } // namespace
103 namespace ash {
105 typedef test::AshTestBase WindowManagerTest;
107 class NonFocusableDelegate : public aura::test::TestWindowDelegate {
108 public:
109 NonFocusableDelegate() {}
111 private:
112 bool CanFocus() override { return false; }
114 DISALLOW_COPY_AND_ASSIGN(NonFocusableDelegate);
117 class HitTestWindowDelegate : public aura::test::TestWindowDelegate {
118 public:
119 HitTestWindowDelegate()
120 : hittest_code_(HTNOWHERE) {
122 ~HitTestWindowDelegate() override {}
123 void set_hittest_code(int hittest_code) { hittest_code_ = hittest_code; }
125 private:
126 // Overridden from TestWindowDelegate:
127 int GetNonClientComponent(const gfx::Point& point) const override {
128 return hittest_code_;
131 int 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
206 // it's focusable.
207 w122->Hide();
208 EXPECT_EQ(aura::client::GetFocusClient(w12.get()),
209 aura::client::GetFocusClient(w122.get()));
210 EXPECT_EQ(w12.get(),
211 aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
213 // Sets the focus back to w122.
214 w122->Show();
215 w122->Focus();
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()));
223 EXPECT_EQ(w12.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);
228 w123->Focus();
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.
235 w123->Hide();
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);
245 w123->Show();
246 w123->Focus();
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());
277 d1.Clear();
278 d2.Clear();
280 // Activate window1.
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());
286 d1.Clear();
289 // Click on window2.
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());
300 d1.Clear();
301 d2.Clear();
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());
317 d1.Clear();
318 d2.Clear();
321 // Destroy window2, this should make window1 active.
322 d1.set_activate(true);
323 w2.reset();
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(),
337 w11.get());
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());
384 // Activate w1.
385 wm::ActivateWindow(w1.get());
386 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
388 // Activate p1.
389 wm::ActivateWindow(p1.get());
390 EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
391 EXPECT_EQ(p1.get(), focus_client->GetFocusedWindow());
393 // Activate w1.
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(),
404 w3.get());
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());
429 d1.Clear();
430 d2.Clear();
432 // Activate window1.
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());
438 d1.Clear();
440 // Touch window2.
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());
456 d1.Clear();
457 d2.Clear();
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());
474 d1.Clear();
475 d2.Clear();
477 // Destroy window2, this should make window1 active.
478 d1.set_activate(true);
479 w2.reset();
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();
491 // Create a window.
492 const int kWindowLeft = 123;
493 const int kWindowTop = 45;
494 HitTestWindowDelegate window_delegate;
495 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
496 &window_delegate,
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, 0, 0);
517 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
518 ASSERT_FALSE(details.dispatcher_destroyed);
519 EXPECT_EQ(ui::kCursorSouthResize, host->last_cursor().native_type());
523 window_delegate.set_hittest_code(HTBOTTOMLEFT);
524 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
525 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
526 ASSERT_FALSE(details.dispatcher_destroyed);
527 EXPECT_EQ(ui::kCursorSouthWestResize, host->last_cursor().native_type());
531 window_delegate.set_hittest_code(HTBOTTOMRIGHT);
532 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
533 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
534 ASSERT_FALSE(details.dispatcher_destroyed);
535 EXPECT_EQ(ui::kCursorSouthEastResize, host->last_cursor().native_type());
539 window_delegate.set_hittest_code(HTLEFT);
540 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
541 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
542 ASSERT_FALSE(details.dispatcher_destroyed);
543 EXPECT_EQ(ui::kCursorWestResize, host->last_cursor().native_type());
547 window_delegate.set_hittest_code(HTRIGHT);
548 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
549 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
550 ASSERT_FALSE(details.dispatcher_destroyed);
551 EXPECT_EQ(ui::kCursorEastResize, host->last_cursor().native_type());
555 window_delegate.set_hittest_code(HTTOP);
556 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
557 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
558 ASSERT_FALSE(details.dispatcher_destroyed);
559 EXPECT_EQ(ui::kCursorNorthResize, host->last_cursor().native_type());
563 window_delegate.set_hittest_code(HTTOPLEFT);
564 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
565 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
566 ASSERT_FALSE(details.dispatcher_destroyed);
567 EXPECT_EQ(ui::kCursorNorthWestResize, host->last_cursor().native_type());
571 window_delegate.set_hittest_code(HTTOPRIGHT);
572 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
573 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
574 ASSERT_FALSE(details.dispatcher_destroyed);
575 EXPECT_EQ(ui::kCursorNorthEastResize, host->last_cursor().native_type());
579 // Client area uses null cursor.
580 window_delegate.set_hittest_code(HTCLIENT);
581 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
582 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
583 ASSERT_FALSE(details.dispatcher_destroyed);
584 EXPECT_EQ(ui::kCursorNull, host->last_cursor().native_type());
588 #if defined(OS_WIN)
589 #define MAYBE_TransformActivate DISABLED_TransformActivate
590 #else
591 #define MAYBE_TransformActivate TransformActivate
592 #endif
593 TEST_F(WindowManagerTest, MAYBE_TransformActivate) {
594 aura::Window* root_window = Shell::GetPrimaryRootWindow();
595 gfx::Size size = root_window->bounds().size();
596 EXPECT_EQ(gfx::Rect(size).ToString(),
597 Shell::GetScreen()->GetDisplayNearestPoint(
598 gfx::Point()).bounds().ToString());
600 // Rotate it clock-wise 90 degrees.
601 gfx::Transform transform;
602 transform.Translate(size.width(), 0);
603 transform.Rotate(90.0f);
604 root_window->GetHost()->SetRootTransform(transform);
606 test::TestActivationDelegate d1;
607 aura::test::TestWindowDelegate wd;
608 scoped_ptr<aura::Window> w1(
609 CreateTestWindowInShellWithDelegate(&wd, 1, gfx::Rect(0, 15, 50, 50)));
610 d1.SetWindow(w1.get());
611 w1->Show();
613 gfx::Point miss_point(5, 5);
614 transform.TransformPoint(&miss_point);
615 ui::MouseEvent mouseev1(ui::ET_MOUSE_PRESSED,
616 miss_point,
617 miss_point,
618 ui::EF_LEFT_MOUSE_BUTTON,
619 ui::EF_LEFT_MOUSE_BUTTON);
620 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
621 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&mouseev1);
622 ASSERT_FALSE(details.dispatcher_destroyed);
623 EXPECT_EQ(NULL, aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
624 ui::MouseEvent mouseup(ui::ET_MOUSE_RELEASED,
625 miss_point,
626 miss_point,
627 ui::EF_LEFT_MOUSE_BUTTON,
628 ui::EF_LEFT_MOUSE_BUTTON);
629 details = dispatcher->OnEventFromSource(&mouseup);
630 ASSERT_FALSE(details.dispatcher_destroyed);
632 gfx::Point hit_point(5, 15);
633 transform.TransformPoint(&hit_point);
634 ui::MouseEvent mouseev2(ui::ET_MOUSE_PRESSED,
635 hit_point,
636 hit_point,
637 ui::EF_LEFT_MOUSE_BUTTON,
638 ui::EF_LEFT_MOUSE_BUTTON);
639 details = dispatcher->OnEventFromSource(&mouseev2);
640 ASSERT_FALSE(details.dispatcher_destroyed);
641 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
642 EXPECT_EQ(w1.get(),
643 aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
646 TEST_F(WindowManagerTest, AdditionalFilters) {
647 // The IME event filter interferes with the basic key event propagation we
648 // attempt to do here, so we remove it.
649 test::ShellTestApi shell_test(Shell::GetInstance());
650 Shell::GetInstance()->RemovePreTargetHandler(
651 shell_test.input_method_event_filter());
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(
676 ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0), 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());
686 f1->Reset();
687 f2->Reset();
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(
697 ui::ET_MOUSE_RELEASED, gfx::Point(0, 0), gfx::Point(0, 0), 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());
707 f1->Reset();
708 f2->Reset();
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 also hides the cursor but does not disable mouse events.
767 generator.ReleaseKey(ui::VKEY_A, ui::EF_NONE);
768 EXPECT_FALSE(cursor_manager->IsCursorVisible());
769 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
770 // Moving mouse shows the cursor again.
771 generator.MoveMouseTo(gfx::Point(0, 0));
772 EXPECT_TRUE(cursor_manager->IsCursorVisible());
773 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
776 TEST_F(WindowManagerTest, TestCursorClientObserver) {
777 ui::test::EventGenerator& generator = GetEventGenerator();
778 ::wm::CursorManager* cursor_manager =
779 ash::Shell::GetInstance()->cursor_manager();
781 scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
782 SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100)));
783 wm::ActivateWindow(w1.get());
785 // Add two observers. Both should have OnCursorVisibilityChanged()
786 // invoked when an event changes the visibility of the cursor.
787 TestingCursorClientObserver observer_a;
788 TestingCursorClientObserver observer_b;
789 cursor_manager->AddObserver(&observer_a);
790 cursor_manager->AddObserver(&observer_b);
792 // Initial state before any events have been sent.
793 observer_a.reset();
794 observer_b.reset();
795 EXPECT_FALSE(observer_a.did_visibility_change());
796 EXPECT_FALSE(observer_b.did_visibility_change());
797 EXPECT_FALSE(observer_a.is_cursor_visible());
798 EXPECT_FALSE(observer_b.is_cursor_visible());
800 // Keypress should hide the cursor.
801 generator.PressKey(ui::VKEY_A, ui::EF_NONE);
802 EXPECT_TRUE(observer_a.did_visibility_change());
803 EXPECT_TRUE(observer_b.did_visibility_change());
804 EXPECT_FALSE(observer_a.is_cursor_visible());
805 EXPECT_FALSE(observer_b.is_cursor_visible());
807 // Mouse move should show the cursor.
808 observer_a.reset();
809 observer_b.reset();
810 generator.MoveMouseTo(50, 50);
811 EXPECT_TRUE(observer_a.did_visibility_change());
812 EXPECT_TRUE(observer_b.did_visibility_change());
813 EXPECT_TRUE(observer_a.is_cursor_visible());
814 EXPECT_TRUE(observer_b.is_cursor_visible());
816 // Remove observer_b. Its OnCursorVisibilityChanged() should
817 // not be invoked past this point.
818 cursor_manager->RemoveObserver(&observer_b);
820 // Gesture tap should hide the cursor.
821 observer_a.reset();
822 observer_b.reset();
823 generator.GestureTapAt(gfx::Point(25, 25));
824 EXPECT_TRUE(observer_a.did_visibility_change());
825 EXPECT_FALSE(observer_b.did_visibility_change());
826 EXPECT_FALSE(observer_a.is_cursor_visible());
828 // Mouse move should show the cursor.
829 observer_a.reset();
830 observer_b.reset();
831 generator.MoveMouseTo(50, 50);
832 EXPECT_TRUE(observer_a.did_visibility_change());
833 EXPECT_FALSE(observer_b.did_visibility_change());
834 EXPECT_TRUE(observer_a.is_cursor_visible());
836 #endif // defined(OS_CHROMEOS)
838 } // namespace ash