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. 4 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/event_generator.h" 16 #include "ui/aura/test/test_window_delegate.h" 17 #include "ui/aura/test/test_windows.h" 18 #include "ui/base/cursor/cursor.h" 19 #include "ui/base/hit_test.h" 20 #include "ui/events/event.h" 21 #include "ui/events/event_processor.h" 22 #include "ui/events/event_utils.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" 29 30 namespace { 31 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_; } 40 41 // Overridden from aura::client::CursorClientObserver: 42 virtual void OnCursorVisibilityChanged(bool is_visible) OVERRIDE { 43 cursor_visibility_ = is_visible; 44 did_visibility_change_ = true; 45 } 46 47 private: 48 bool cursor_visibility_; 49 bool did_visibility_change_; 50 51 DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver); 52 }; 53 54 base::TimeDelta getTime() { 55 return ui::EventTimeForNow(); 56 } 57 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) { 65 } 66 67 virtual ~CustomEventHandler() {} 68 69 void set_key_event_handling_result(ui::EventResult result) { 70 key_result_ = result; 71 } 72 73 void set_mouse_event_handling_result(ui::EventResult result) { 74 mouse_result_ = result; 75 } 76 77 // Overridden from ui::EventHandler: 78 virtual 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(); 84 } 85 86 virtual 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(); 92 } 93 94 private: 95 ui::EventResult key_result_; 96 ui::EventResult mouse_result_; 97 98 DISALLOW_COPY_AND_ASSIGN(CustomEventHandler); 99 }; 100 101 } // namespace 102 103 namespace ash { 104 105 typedef test::AshTestBase WindowManagerTest; 106 107 class NonFocusableDelegate : public aura::test::TestWindowDelegate { 108 public: 109 NonFocusableDelegate() {} 110 111 private: 112 virtual bool CanFocus() OVERRIDE { 113 return false; 114 } 115 116 DISALLOW_COPY_AND_ASSIGN(NonFocusableDelegate); 117 }; 118 119 class HitTestWindowDelegate : public aura::test::TestWindowDelegate { 120 public: 121 HitTestWindowDelegate() 122 : hittest_code_(HTNOWHERE) { 123 } 124 virtual ~HitTestWindowDelegate() {} 125 void set_hittest_code(int hittest_code) { hittest_code_ = hittest_code; } 126 127 private: 128 // Overridden from TestWindowDelegate: 129 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { 130 return hittest_code_; 131 } 132 133 int hittest_code_; 134 135 DISALLOW_COPY_AND_ASSIGN(HitTestWindowDelegate); 136 }; 137 138 TEST_F(WindowManagerTest, Focus) { 139 // The IME event filter interferes with the basic key event propagation we 140 // attempt to do here, so we remove it. 141 test::ShellTestApi shell_test(Shell::GetInstance()); 142 Shell::GetInstance()->RemovePreTargetHandler( 143 shell_test.input_method_event_filter()); 144 145 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 146 root_window->SetBounds(gfx::Rect(0, 0, 510, 510)); 147 148 // Supplied ids are negative so as not to collide with shell ids. 149 // TODO(beng): maybe introduce a MAKE_SHELL_ID() macro that generates a safe 150 // id beyond shell id max? 151 scoped_ptr<aura::Window> w1(CreateTestWindowInShell( 152 SK_ColorWHITE, -1, gfx::Rect(10, 10, 500, 500))); 153 scoped_ptr<aura::Window> w11(aura::test::CreateTestWindow( 154 SK_ColorGREEN, -11, gfx::Rect(5, 5, 100, 100), w1.get())); 155 scoped_ptr<aura::Window> w111(aura::test::CreateTestWindow( 156 SK_ColorCYAN, -111, gfx::Rect(5, 5, 75, 75), w11.get())); 157 scoped_ptr<aura::Window> w1111(aura::test::CreateTestWindow( 158 SK_ColorRED, -1111, gfx::Rect(5, 5, 50, 50), w111.get())); 159 scoped_ptr<aura::Window> w12(aura::test::CreateTestWindow( 160 SK_ColorMAGENTA, -12, gfx::Rect(10, 420, 25, 25), w1.get())); 161 aura::test::ColorTestWindowDelegate* w121delegate = 162 new aura::test::ColorTestWindowDelegate(SK_ColorYELLOW); 163 scoped_ptr<aura::Window> w121(aura::test::CreateTestWindowWithDelegate( 164 w121delegate, -121, gfx::Rect(5, 5, 5, 5), w12.get())); 165 aura::test::ColorTestWindowDelegate* w122delegate = 166 new aura::test::ColorTestWindowDelegate(SK_ColorRED); 167 scoped_ptr<aura::Window> w122(aura::test::CreateTestWindowWithDelegate( 168 w122delegate, -122, gfx::Rect(10, 5, 5, 5), w12.get())); 169 aura::test::ColorTestWindowDelegate* w123delegate = 170 new aura::test::ColorTestWindowDelegate(SK_ColorRED); 171 scoped_ptr<aura::Window> w123(aura::test::CreateTestWindowWithDelegate( 172 w123delegate, -123, gfx::Rect(15, 5, 5, 5), w12.get())); 173 scoped_ptr<aura::Window> w13(aura::test::CreateTestWindow( 174 SK_ColorGRAY, -13, gfx::Rect(5, 470, 50, 50), w1.get())); 175 176 // Click on a sub-window (w121) to focus it. 177 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), 178 w121.get()); 179 generator.ClickLeftButton(); 180 181 aura::client::FocusClient* focus_client = 182 aura::client::GetFocusClient(w121.get()); 183 EXPECT_EQ(w121.get(), focus_client->GetFocusedWindow()); 184 185 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor(); 186 187 // The key press should be sent to the focused sub-window. 188 ui::KeyEvent keyev(ui::ET_KEY_PRESSED, ui::VKEY_E, 0, false); 189 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&keyev); 190 ASSERT_FALSE(details.dispatcher_destroyed); 191 EXPECT_EQ(ui::VKEY_E, w121delegate->last_key_code()); 192 193 // Touch on a sub-window (w122) to focus it. 194 gfx::Point click_point = w122->bounds().CenterPoint(); 195 aura::Window::ConvertPointToTarget(w122->parent(), root_window, &click_point); 196 ui::TouchEvent touchev(ui::ET_TOUCH_PRESSED, click_point, 0, getTime()); 197 details = dispatcher->OnEventFromSource(&touchev); 198 ASSERT_FALSE(details.dispatcher_destroyed); 199 focus_client = aura::client::GetFocusClient(w122.get()); 200 EXPECT_EQ(w122.get(), focus_client->GetFocusedWindow()); 201 202 // The key press should be sent to the focused sub-window. 203 details = dispatcher->OnEventFromSource(&keyev); 204 ASSERT_FALSE(details.dispatcher_destroyed); 205 EXPECT_EQ(ui::VKEY_E, w122delegate->last_key_code()); 206 207 // Hiding the focused window will set the focus to its parent if 208 // it's focusable. 209 w122->Hide(); 210 EXPECT_EQ(aura::client::GetFocusClient(w12.get()), 211 aura::client::GetFocusClient(w122.get())); 212 EXPECT_EQ(w12.get(), 213 aura::client::GetFocusClient(w12.get())->GetFocusedWindow()); 214 215 // Sets the focus back to w122. 216 w122->Show(); 217 w122->Focus(); 218 EXPECT_EQ(w122.get(), 219 aura::client::GetFocusClient(w12.get())->GetFocusedWindow()); 220 221 // Removing the focused window from parent should set the focus to 222 // its parent if it's focusable. 223 w12->RemoveChild(w122.get()); 224 EXPECT_EQ(NULL, aura::client::GetFocusClient(w122.get())); 225 EXPECT_EQ(w12.get(), 226 aura::client::GetFocusClient(w12.get())->GetFocusedWindow()); 227 228 // Set the focus to w123, but make the w1 not activatable. 229 test::TestActivationDelegate activation_delegate(false); 230 w123->Focus(); 231 EXPECT_EQ(w123.get(), 232 aura::client::GetFocusClient(w12.get())->GetFocusedWindow()); 233 aura::client::SetActivationDelegate(w1.get(), &activation_delegate); 234 235 // Hiding the focused window will set the focus to NULL because 236 // parent window is not focusable. 237 w123->Hide(); 238 EXPECT_EQ(aura::client::GetFocusClient(w12.get()), 239 aura::client::GetFocusClient(w123.get())); 240 EXPECT_EQ(NULL, aura::client::GetFocusClient(w12.get())->GetFocusedWindow()); 241 details = dispatcher->OnEventFromSource(&keyev); 242 EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed); 243 244 // Set the focus back to w123 245 aura::client::SetActivationDelegate(w1.get(), NULL); 246 w123->Show(); 247 w123->Focus(); 248 EXPECT_EQ(w123.get(), 249 aura::client::GetFocusClient(w12.get())->GetFocusedWindow()); 250 aura::client::SetActivationDelegate(w1.get(), &activation_delegate); 251 252 // Removing the focused window will set the focus to NULL because 253 // parent window is not focusable. 254 w12->RemoveChild(w123.get()); 255 EXPECT_EQ(NULL, aura::client::GetFocusClient(w123.get())); 256 details = dispatcher->OnEventFromSource(&keyev); 257 EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed); 258 } 259 260 // Various assertion testing for activating windows. 261 TEST_F(WindowManagerTest, ActivateOnMouse) { 262 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 263 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()); 273 274 aura::client::FocusClient* focus_client = 275 aura::client::GetFocusClient(w1.get()); 276 277 d1.Clear(); 278 d2.Clear(); 279 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(); 287 288 { 289 // Click on window2. 290 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), 291 w2.get()); 292 generator.ClickLeftButton(); 293 294 // Window2 should have become active. 295 EXPECT_TRUE(wm::IsActiveWindow(w2.get())); 296 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow()); 297 EXPECT_EQ(0, d1.activated_count()); 298 EXPECT_EQ(1, d1.lost_active_count()); 299 EXPECT_EQ(1, d2.activated_count()); 300 EXPECT_EQ(0, d2.lost_active_count()); 301 d1.Clear(); 302 d2.Clear(); 303 } 304 305 { 306 // Click back on window1, but set it up so w1 doesn't activate on click. 307 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), 308 w1.get()); 309 d1.set_activate(false); 310 generator.ClickLeftButton(); 311 312 // Window2 should still be active and focused. 313 EXPECT_TRUE(wm::IsActiveWindow(w2.get())); 314 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow()); 315 EXPECT_EQ(0, d1.activated_count()); 316 EXPECT_EQ(0, d1.lost_active_count()); 317 EXPECT_EQ(0, d2.activated_count()); 318 EXPECT_EQ(0, d2.lost_active_count()); 319 d1.Clear(); 320 d2.Clear(); 321 } 322 323 // Destroy window2, this should make window1 active. 324 d1.set_activate(true); 325 w2.reset(); 326 EXPECT_EQ(0, d2.activated_count()); 327 EXPECT_EQ(1, d2.lost_active_count()); 328 EXPECT_TRUE(wm::IsActiveWindow(w1.get())); 329 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow()); 330 EXPECT_EQ(1, d1.activated_count()); 331 EXPECT_EQ(0, d1.lost_active_count()); 332 333 // Clicking an active window with a child shouldn't steal the 334 // focus from the child. 335 { 336 scoped_ptr<aura::Window> w11(CreateTestWindowWithDelegate( 337 &wd, -11, gfx::Rect(10, 10, 10, 10), w1.get())); 338 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), 339 w11.get()); 340 // First set the focus to the child |w11|. 341 generator.ClickLeftButton(); 342 EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow()); 343 EXPECT_EQ(w1.get(), wm::GetActiveWindow()); 344 345 // Then click the parent active window. The focus shouldn't move. 346 gfx::Point left_top = w1->bounds().origin(); 347 aura::Window::ConvertPointToTarget(w1->parent(), root_window, &left_top); 348 left_top.Offset(1, 1); 349 generator.MoveMouseTo(left_top); 350 generator.ClickLeftButton(); 351 EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow()); 352 EXPECT_EQ(w1.get(), wm::GetActiveWindow()); 353 } 354 355 // Clicking on a non-focusable window inside a background window should still 356 // give focus to the background window. 357 { 358 NonFocusableDelegate nfd; 359 scoped_ptr<aura::Window> w11(CreateTestWindowWithDelegate( 360 &nfd, -1, gfx::Rect(10, 10, 10, 10), w1.get())); 361 // Move focus to |w2| first. 362 scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate( 363 &wd, -1, gfx::Rect(70, 70, 50, 50))); 364 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), 365 w2.get()); 366 generator.ClickLeftButton(); 367 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow()); 368 EXPECT_FALSE(w11->CanFocus()); 369 370 // Click on |w11|. This should focus w1. 371 generator.MoveMouseToCenterOf(w11.get()); 372 generator.ClickLeftButton(); 373 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow()); 374 } 375 } 376 377 TEST_F(WindowManagerTest, PanelActivation) { 378 aura::test::TestWindowDelegate wd; 379 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate( 380 &wd, -1, gfx::Rect(10, 10, 50, 50))); 381 aura::test::TestWindowDelegate pd; 382 scoped_ptr<aura::Window> p1(CreateTestWindowInShellWithDelegateAndType( 383 &pd, ui::wm::WINDOW_TYPE_PANEL, -1, gfx::Rect(10, 10, 50, 50))); 384 aura::client::FocusClient* focus_client = 385 aura::client::GetFocusClient(w1.get()); 386 387 // Activate w1. 388 wm::ActivateWindow(w1.get()); 389 EXPECT_TRUE(wm::IsActiveWindow(w1.get())); 390 391 // Activate p1. 392 wm::ActivateWindow(p1.get()); 393 EXPECT_TRUE(wm::IsActiveWindow(p1.get())); 394 EXPECT_EQ(p1.get(), focus_client->GetFocusedWindow()); 395 396 // Activate w1. 397 wm::ActivateWindow(w1.get()); 398 EXPECT_TRUE(wm::IsActiveWindow(w1.get())); 399 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow()); 400 401 // Clicking on a non-activatable window should not change the active window. 402 { 403 NonFocusableDelegate nfd; 404 scoped_ptr<aura::Window> w3(CreateTestWindowInShellWithDelegate( 405 &nfd, -1, gfx::Rect(70, 70, 50, 50))); 406 aura::test::EventGenerator generator3(Shell::GetPrimaryRootWindow(), 407 w3.get()); 408 wm::ActivateWindow(p1.get()); 409 EXPECT_TRUE(wm::IsActiveWindow(p1.get())); 410 generator3.ClickLeftButton(); 411 EXPECT_TRUE(wm::IsActiveWindow(p1.get())); 412 } 413 } 414 415 // Essentially the same as ActivateOnMouse, but for touch events. 416 TEST_F(WindowManagerTest, ActivateOnTouch) { 417 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 418 419 test::TestActivationDelegate d1; 420 aura::test::TestWindowDelegate wd; 421 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate( 422 &wd, -1, gfx::Rect(10, 10, 50, 50))); 423 d1.SetWindow(w1.get()); 424 test::TestActivationDelegate d2; 425 scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate( 426 &wd, -2, gfx::Rect(70, 70, 50, 50))); 427 d2.SetWindow(w2.get()); 428 429 aura::client::FocusClient* focus_client = 430 aura::client::GetFocusClient(w1.get()); 431 432 d1.Clear(); 433 d2.Clear(); 434 435 // Activate window1. 436 wm::ActivateWindow(w1.get()); 437 EXPECT_TRUE(wm::IsActiveWindow(w1.get())); 438 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow()); 439 EXPECT_EQ(1, d1.activated_count()); 440 EXPECT_EQ(0, d1.lost_active_count()); 441 d1.Clear(); 442 443 // Touch window2. 444 gfx::Point press_point = w2->bounds().CenterPoint(); 445 aura::Window::ConvertPointToTarget(w2->parent(), root_window, &press_point); 446 ui::TouchEvent touchev1(ui::ET_TOUCH_PRESSED, press_point, 0, getTime()); 447 448 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor(); 449 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&touchev1); 450 ASSERT_FALSE(details.dispatcher_destroyed); 451 452 // Window2 should have become active. 453 EXPECT_TRUE(wm::IsActiveWindow(w2.get())); 454 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow()); 455 EXPECT_EQ(0, d1.activated_count()); 456 EXPECT_EQ(1, d1.lost_active_count()); 457 EXPECT_EQ(1, d2.activated_count()); 458 EXPECT_EQ(0, d2.lost_active_count()); 459 d1.Clear(); 460 d2.Clear(); 461 462 // Touch window1, but set it up so w1 doesn't activate on touch. 463 press_point = w1->bounds().CenterPoint(); 464 aura::Window::ConvertPointToTarget(w1->parent(), root_window, &press_point); 465 d1.set_activate(false); 466 ui::TouchEvent touchev2(ui::ET_TOUCH_PRESSED, press_point, 1, getTime()); 467 details = dispatcher->OnEventFromSource(&touchev2); 468 ASSERT_FALSE(details.dispatcher_destroyed); 469 470 // Window2 should still be active and focused. 471 EXPECT_TRUE(wm::IsActiveWindow(w2.get())); 472 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow()); 473 EXPECT_EQ(0, d1.activated_count()); 474 EXPECT_EQ(0, d1.lost_active_count()); 475 EXPECT_EQ(0, d2.activated_count()); 476 EXPECT_EQ(0, d2.lost_active_count()); 477 d1.Clear(); 478 d2.Clear(); 479 480 // Destroy window2, this should make window1 active. 481 d1.set_activate(true); 482 w2.reset(); 483 EXPECT_EQ(0, d2.activated_count()); 484 EXPECT_EQ(1, d2.lost_active_count()); 485 EXPECT_TRUE(wm::IsActiveWindow(w1.get())); 486 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow()); 487 EXPECT_EQ(1, d1.activated_count()); 488 EXPECT_EQ(0, d1.lost_active_count()); 489 } 490 491 TEST_F(WindowManagerTest, MouseEventCursors) { 492 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 493 494 // Create a window. 495 const int kWindowLeft = 123; 496 const int kWindowTop = 45; 497 HitTestWindowDelegate window_delegate; 498 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate( 499 &window_delegate, 500 -1, 501 gfx::Rect(kWindowLeft, kWindowTop, 640, 480))); 502 503 // Create two mouse movement events we can switch between. 504 gfx::Point point1(kWindowLeft, kWindowTop); 505 aura::Window::ConvertPointToTarget(window->parent(), root_window, &point1); 506 507 gfx::Point point2(kWindowLeft + 1, kWindowTop + 1); 508 aura::Window::ConvertPointToTarget(window->parent(), root_window, &point2); 509 510 aura::WindowTreeHost* host = root_window->GetHost(); 511 ui::EventProcessor* dispatcher = host->event_processor(); 512 513 // Cursor starts as a pointer (set during Shell::Init()). 514 EXPECT_EQ(ui::kCursorPointer, host->last_cursor().native_type()); 515 516 { 517 // Resize edges and corners show proper cursors. 518 window_delegate.set_hittest_code(HTBOTTOM); 519 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0); 520 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1); 521 ASSERT_FALSE(details.dispatcher_destroyed); 522 EXPECT_EQ(ui::kCursorSouthResize, host->last_cursor().native_type()); 523 } 524 525 { 526 window_delegate.set_hittest_code(HTBOTTOMLEFT); 527 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0); 528 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2); 529 ASSERT_FALSE(details.dispatcher_destroyed); 530 EXPECT_EQ(ui::kCursorSouthWestResize, host->last_cursor().native_type()); 531 } 532 533 { 534 window_delegate.set_hittest_code(HTBOTTOMRIGHT); 535 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 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()); 539 } 540 541 { 542 window_delegate.set_hittest_code(HTLEFT); 543 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0); 544 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2); 545 ASSERT_FALSE(details.dispatcher_destroyed); 546 EXPECT_EQ(ui::kCursorWestResize, host->last_cursor().native_type()); 547 } 548 549 { 550 window_delegate.set_hittest_code(HTRIGHT); 551 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0); 552 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1); 553 ASSERT_FALSE(details.dispatcher_destroyed); 554 EXPECT_EQ(ui::kCursorEastResize, host->last_cursor().native_type()); 555 } 556 557 { 558 window_delegate.set_hittest_code(HTTOP); 559 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0); 560 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2); 561 ASSERT_FALSE(details.dispatcher_destroyed); 562 EXPECT_EQ(ui::kCursorNorthResize, host->last_cursor().native_type()); 563 } 564 565 { 566 window_delegate.set_hittest_code(HTTOPLEFT); 567 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0); 568 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1); 569 ASSERT_FALSE(details.dispatcher_destroyed); 570 EXPECT_EQ(ui::kCursorNorthWestResize, host->last_cursor().native_type()); 571 } 572 573 { 574 window_delegate.set_hittest_code(HTTOPRIGHT); 575 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0); 576 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2); 577 ASSERT_FALSE(details.dispatcher_destroyed); 578 EXPECT_EQ(ui::kCursorNorthEastResize, host->last_cursor().native_type()); 579 } 580 581 { 582 // Client area uses null cursor. 583 window_delegate.set_hittest_code(HTCLIENT); 584 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0); 585 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1); 586 ASSERT_FALSE(details.dispatcher_destroyed); 587 EXPECT_EQ(ui::kCursorNull, host->last_cursor().native_type()); 588 } 589 } 590 591 #if defined(OS_WIN) 592 #define MAYBE_TransformActivate DISABLED_TransformActivate 593 #else 594 #define MAYBE_TransformActivate TransformActivate 595 #endif 596 TEST_F(WindowManagerTest, MAYBE_TransformActivate) { 597 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 598 gfx::Size size = root_window->bounds().size(); 599 EXPECT_EQ(gfx::Rect(size).ToString(), 600 Shell::GetScreen()->GetDisplayNearestPoint( 601 gfx::Point()).bounds().ToString()); 602 603 // Rotate it clock-wise 90 degrees. 604 gfx::Transform transform; 605 transform.Translate(size.width(), 0); 606 transform.Rotate(90.0f); 607 root_window->GetHost()->SetRootTransform(transform); 608 609 test::TestActivationDelegate d1; 610 aura::test::TestWindowDelegate wd; 611 scoped_ptr<aura::Window> w1( 612 CreateTestWindowInShellWithDelegate(&wd, 1, gfx::Rect(0, 15, 50, 50))); 613 d1.SetWindow(w1.get()); 614 w1->Show(); 615 616 gfx::Point miss_point(5, 5); 617 transform.TransformPoint(&miss_point); 618 ui::MouseEvent mouseev1(ui::ET_MOUSE_PRESSED, 619 miss_point, 620 miss_point, 621 ui::EF_LEFT_MOUSE_BUTTON, 622 ui::EF_LEFT_MOUSE_BUTTON); 623 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor(); 624 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&mouseev1); 625 ASSERT_FALSE(details.dispatcher_destroyed); 626 EXPECT_EQ(NULL, aura::client::GetFocusClient(w1.get())->GetFocusedWindow()); 627 ui::MouseEvent mouseup(ui::ET_MOUSE_RELEASED, 628 miss_point, 629 miss_point, 630 ui::EF_LEFT_MOUSE_BUTTON, 631 ui::EF_LEFT_MOUSE_BUTTON); 632 details = dispatcher->OnEventFromSource(&mouseup); 633 ASSERT_FALSE(details.dispatcher_destroyed); 634 635 gfx::Point hit_point(5, 15); 636 transform.TransformPoint(&hit_point); 637 ui::MouseEvent mouseev2(ui::ET_MOUSE_PRESSED, 638 hit_point, 639 hit_point, 640 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())); 645 EXPECT_EQ(w1.get(), 646 aura::client::GetFocusClient(w1.get())->GetFocusedWindow()); 647 } 648 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()); 655 656 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 657 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()); 662 663 // Creates two addition filters 664 scoped_ptr<CustomEventHandler> f1(new CustomEventHandler); 665 scoped_ptr<CustomEventHandler> f2(new CustomEventHandler); 666 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()); 672 673 // Dispatches mouse and keyboard events. 674 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false); 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( 679 ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0); 680 details = dispatcher->OnEventFromSource(&mouse_pressed); 681 ASSERT_FALSE(details.dispatcher_destroyed); 682 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()); 688 689 f1->Reset(); 690 f2->Reset(); 691 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); 695 696 // Dispatches events. 697 details = dispatcher->OnEventFromSource(&key_event); 698 ASSERT_FALSE(details.dispatcher_destroyed); 699 ui::MouseEvent mouse_released( 700 ui::ET_MOUSE_RELEASED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0); 701 details = dispatcher->OnEventFromSource(&mouse_released); 702 ASSERT_FALSE(details.dispatcher_destroyed); 703 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()); 709 710 f1->Reset(); 711 f2->Reset(); 712 713 // Remove f1 from additonal filters list. 714 env_filter->RemoveHandler(f1.get()); 715 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); 721 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()); 727 728 env_filter->RemoveHandler(f2.get()); 729 } 730 731 #if defined(OS_CHROMEOS) || defined(OS_WIN) 732 // Touch visually hides the cursor on ChromeOS and Windows 733 TEST_F(WindowManagerTest, UpdateCursorVisibility) { 734 aura::test::EventGenerator& generator = GetEventGenerator(); 735 ::wm::CursorManager* cursor_manager = 736 ash::Shell::GetInstance()->cursor_manager(); 737 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()); 750 } 751 #endif // defined(OS_CHROMEOS) || defined(OS_WIN) 752 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 aura::test::EventGenerator& generator = GetEventGenerator(); 758 ::wm::CursorManager* cursor_manager = 759 ash::Shell::GetInstance()->cursor_manager(); 760 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()); 777 } 778 779 TEST_F(WindowManagerTest, TestCursorClientObserver) { 780 aura::test::EventGenerator& generator = GetEventGenerator(); 781 ::wm::CursorManager* cursor_manager = 782 ash::Shell::GetInstance()->cursor_manager(); 783 784 scoped_ptr<aura::Window> w1(CreateTestWindowInShell( 785 SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100))); 786 wm::ActivateWindow(w1.get()); 787 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); 794 795 // Initial state before any events have been sent. 796 observer_a.reset(); 797 observer_b.reset(); 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()); 802 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()); 809 810 // Mouse move should show the cursor. 811 observer_a.reset(); 812 observer_b.reset(); 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()); 818 819 // Remove observer_b. Its OnCursorVisibilityChanged() should 820 // not be invoked past this point. 821 cursor_manager->RemoveObserver(&observer_b); 822 823 // Gesture tap should hide the cursor. 824 observer_a.reset(); 825 observer_b.reset(); 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()); 830 831 // Mouse move should show the cursor. 832 observer_a.reset(); 833 observer_b.reset(); 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()); 838 } 839 #endif // defined(OS_CHROMEOS) 840 841 } // namespace ash 842