| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/wm/window_cycle_controller.h" | 5 #include "ash/common/wm/window_cycle_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "ash/aura/wm_window_aura.h" | 10 #include "ash/aura/wm_window_aura.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include "ui/aura/window.h" | 33 #include "ui/aura/window.h" |
| 34 #include "ui/aura/window_event_dispatcher.h" | 34 #include "ui/aura/window_event_dispatcher.h" |
| 35 #include "ui/events/event_handler.h" | 35 #include "ui/events/event_handler.h" |
| 36 #include "ui/events/test/event_generator.h" | 36 #include "ui/events/test/event_generator.h" |
| 37 #include "ui/gfx/geometry/rect.h" | 37 #include "ui/gfx/geometry/rect.h" |
| 38 | 38 |
| 39 namespace ash { | 39 namespace ash { |
| 40 | 40 |
| 41 namespace { | 41 namespace { |
| 42 | 42 |
| 43 class KeyEventCounter : public ui::EventHandler { | 43 class EventCounter : public ui::EventHandler { |
| 44 public: | 44 public: |
| 45 KeyEventCounter() : key_events_(0) {} | 45 EventCounter() : key_events_(0), mouse_events_(0) {} |
| 46 ~KeyEventCounter() override {} | 46 ~EventCounter() override {} |
| 47 | 47 |
| 48 size_t GetCountAndReset() { | 48 int GetKeyEventCountAndReset() { |
| 49 size_t count = key_events_; | 49 int count = key_events_; |
| 50 key_events_ = 0; | 50 key_events_ = 0; |
| 51 return count; | 51 return count; |
| 52 } | 52 } |
| 53 | 53 |
| 54 int GetMouseEventCountAndReset() { |
| 55 int count = mouse_events_; |
| 56 mouse_events_ = 0; |
| 57 return count; |
| 58 } |
| 59 |
| 54 // ui::EventHandler: | 60 // ui::EventHandler: |
| 55 void OnKeyEvent(ui::KeyEvent* event) override { key_events_++; } | 61 void OnKeyEvent(ui::KeyEvent* event) override { key_events_++; } |
| 62 void OnMouseEvent(ui::MouseEvent* event) override { mouse_events_++; } |
| 56 | 63 |
| 57 private: | 64 private: |
| 58 size_t key_events_; | 65 int key_events_; |
| 66 int mouse_events_; |
| 59 | 67 |
| 60 DISALLOW_COPY_AND_ASSIGN(KeyEventCounter); | 68 DISALLOW_COPY_AND_ASSIGN(EventCounter); |
| 61 }; | 69 }; |
| 62 | 70 |
| 63 bool IsWindowMinimized(aura::Window* window) { | 71 bool IsWindowMinimized(aura::Window* window) { |
| 64 return WmWindowAura::Get(window)->GetWindowState()->IsMinimized(); | 72 return WmWindowAura::Get(window)->GetWindowState()->IsMinimized(); |
| 65 } | 73 } |
| 66 | 74 |
| 67 } // namespace | 75 } // namespace |
| 68 | 76 |
| 69 using aura::test::CreateTestWindowWithId; | 77 using aura::test::CreateTestWindowWithId; |
| 70 using aura::test::TestWindowDelegate; | 78 using aura::test::TestWindowDelegate; |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 // Cycling again should now select panel1. | 559 // Cycling again should now select panel1. |
| 552 controller->HandleCycleWindow(WindowCycleController::FORWARD); | 560 controller->HandleCycleWindow(WindowCycleController::FORWARD); |
| 553 controller->StopCycling(); | 561 controller->StopCycling(); |
| 554 EXPECT_TRUE(wm::IsActiveWindow(panel1.get())); | 562 EXPECT_TRUE(wm::IsActiveWindow(panel1.get())); |
| 555 } | 563 } |
| 556 | 564 |
| 557 // Tests that the tab key events are not sent to the window. | 565 // Tests that the tab key events are not sent to the window. |
| 558 TEST_F(WindowCycleControllerTest, TabKeyNotLeaked) { | 566 TEST_F(WindowCycleControllerTest, TabKeyNotLeaked) { |
| 559 std::unique_ptr<Window> w0(CreateTestWindowInShellWithId(0)); | 567 std::unique_ptr<Window> w0(CreateTestWindowInShellWithId(0)); |
| 560 std::unique_ptr<Window> w1(CreateTestWindowInShellWithId(1)); | 568 std::unique_ptr<Window> w1(CreateTestWindowInShellWithId(1)); |
| 561 KeyEventCounter key_count; | 569 EventCounter event_count; |
| 562 w0->AddPreTargetHandler(&key_count); | 570 w0->AddPreTargetHandler(&event_count); |
| 563 w1->AddPreTargetHandler(&key_count); | 571 w1->AddPreTargetHandler(&event_count); |
| 564 ui::test::EventGenerator& generator = GetEventGenerator(); | 572 ui::test::EventGenerator& generator = GetEventGenerator(); |
| 565 wm::GetWindowState(w0.get())->Activate(); | 573 wm::GetWindowState(w0.get())->Activate(); |
| 566 generator.PressKey(ui::VKEY_MENU, ui::EF_NONE); | 574 generator.PressKey(ui::VKEY_MENU, ui::EF_NONE); |
| 567 EXPECT_EQ(1u, key_count.GetCountAndReset()); | 575 EXPECT_EQ(1, event_count.GetKeyEventCountAndReset()); |
| 568 generator.PressKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); | 576 generator.PressKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); |
| 569 EXPECT_EQ(0u, key_count.GetCountAndReset()); | 577 EXPECT_EQ(0, event_count.GetKeyEventCountAndReset()); |
| 570 generator.ReleaseKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); | 578 generator.ReleaseKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); |
| 571 EXPECT_EQ(0u, key_count.GetCountAndReset()); | 579 EXPECT_EQ(0, event_count.GetKeyEventCountAndReset()); |
| 572 generator.ReleaseKey(ui::VKEY_MENU, ui::EF_NONE); | 580 generator.ReleaseKey(ui::VKEY_MENU, ui::EF_NONE); |
| 573 EXPECT_TRUE(wm::GetWindowState(w1.get())->IsActive()); | 581 EXPECT_TRUE(wm::GetWindowState(w1.get())->IsActive()); |
| 574 EXPECT_EQ(0u, key_count.GetCountAndReset()); | 582 EXPECT_EQ(0, event_count.GetKeyEventCountAndReset()); |
| 583 } |
| 584 |
| 585 // While the UI is active, mouse events are captured. |
| 586 TEST_F(WindowCycleControllerTest, MouseEventsCaptured) { |
| 587 // This delegate allows the window to receive mouse events. |
| 588 aura::test::TestWindowDelegate delegate; |
| 589 std::unique_ptr<Window> w0(CreateTestWindowInShellWithDelegate( |
| 590 &delegate, 0, gfx::Rect(0, 0, 100, 100))); |
| 591 std::unique_ptr<Window> w1(CreateTestWindowInShellWithId(1)); |
| 592 EventCounter event_count; |
| 593 w0->AddPreTargetHandler(&event_count); |
| 594 w1->SetTargetHandler(&event_count); |
| 595 ui::test::EventGenerator& generator = GetEventGenerator(); |
| 596 wm::ActivateWindow(w0.get()); |
| 597 |
| 598 // Events get through. |
| 599 generator.MoveMouseToCenterOf(w0.get()); |
| 600 generator.ClickLeftButton(); |
| 601 EXPECT_LT(0, event_count.GetMouseEventCountAndReset()); |
| 602 |
| 603 // Start cycling. |
| 604 WindowCycleController* controller = WmShell::Get()->window_cycle_controller(); |
| 605 controller->HandleCycleWindow(WindowCycleController::FORWARD); |
| 606 |
| 607 // Events don't get through. |
| 608 generator.ClickLeftButton(); |
| 609 EXPECT_EQ(0, event_count.GetMouseEventCountAndReset()); |
| 610 |
| 611 // Stop cycling: once again, events get through. |
| 612 controller->StopCycling(); |
| 613 generator.ClickLeftButton(); |
| 614 EXPECT_LT(0, event_count.GetMouseEventCountAndReset()); |
| 615 } |
| 616 |
| 617 // If mouse capture is lost, the UI closes. |
| 618 TEST_F(WindowCycleControllerTest, MouseCaptureLost) { |
| 619 // This delegate allows the window to receive mouse events. |
| 620 aura::test::TestWindowDelegate delegate; |
| 621 std::unique_ptr<Window> w0(CreateTestWindowInShellWithDelegate( |
| 622 &delegate, 0, gfx::Rect(0, 0, 100, 100))); |
| 623 std::unique_ptr<Window> w1(CreateTestWindowInShellWithId(1)); |
| 624 |
| 625 // Start cycling. |
| 626 WindowCycleController* controller = WmShell::Get()->window_cycle_controller(); |
| 627 controller->HandleCycleWindow(WindowCycleController::FORWARD); |
| 628 |
| 629 // Some other widget grabs capture and this causes Alt+Tab to cease. |
| 630 std::unique_ptr<views::Widget> widget = CreateTestWidget( |
| 631 nullptr, kShellWindowId_DefaultContainer, gfx::Rect(1, 2, 3, 4)); |
| 632 widget->SetCapture(nullptr); |
| 633 EXPECT_FALSE(controller->IsCycling()); |
| 575 } | 634 } |
| 576 | 635 |
| 577 // Tests that we can cycle past fullscreen windows: https://crbug.com/622396. | 636 // Tests that we can cycle past fullscreen windows: https://crbug.com/622396. |
| 578 // Fullscreen windows are special in that they are allowed to handle alt+tab | 637 // Fullscreen windows are special in that they are allowed to handle alt+tab |
| 579 // keypresses, which means the window cycle event filter should not handle | 638 // keypresses, which means the window cycle event filter should not handle |
| 580 // the tab press else it prevents cycling past that window. | 639 // the tab press else it prevents cycling past that window. |
| 581 TEST_F(WindowCycleControllerTest, TabPastFullscreenWindow) { | 640 TEST_F(WindowCycleControllerTest, TabPastFullscreenWindow) { |
| 582 std::unique_ptr<Window> w0(CreateTestWindowInShellWithId(0)); | 641 std::unique_ptr<Window> w0(CreateTestWindowInShellWithId(0)); |
| 583 std::unique_ptr<Window> w1(CreateTestWindowInShellWithId(1)); | 642 std::unique_ptr<Window> w1(CreateTestWindowInShellWithId(1)); |
| 584 wm::WMEvent maximize_event(wm::WM_EVENT_FULLSCREEN); | 643 wm::WMEvent maximize_event(wm::WM_EVENT_FULLSCREEN); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 597 generator.PressKey(ui::VKEY_MENU, ui::EF_NONE); | 656 generator.PressKey(ui::VKEY_MENU, ui::EF_NONE); |
| 598 | 657 |
| 599 generator.PressKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); | 658 generator.PressKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); |
| 600 generator.ReleaseKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); | 659 generator.ReleaseKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); |
| 601 | 660 |
| 602 // Because w0 and w1 are full-screen, the event should be passed to the | 661 // Because w0 and w1 are full-screen, the event should be passed to the |
| 603 // browser window to handle it (which if the browser doesn't handle it will | 662 // browser window to handle it (which if the browser doesn't handle it will |
| 604 // pass on the alt+tab to continue cycling). To make this test work with or | 663 // pass on the alt+tab to continue cycling). To make this test work with or |
| 605 // without the new alt+tab selector we check for the event on either | 664 // without the new alt+tab selector we check for the event on either |
| 606 // fullscreen window. | 665 // fullscreen window. |
| 607 KeyEventCounter key_count; | 666 EventCounter event_count; |
| 608 w0->AddPreTargetHandler(&key_count); | 667 w0->AddPreTargetHandler(&event_count); |
| 609 w1->AddPreTargetHandler(&key_count); | 668 w1->AddPreTargetHandler(&event_count); |
| 610 generator.PressKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); | 669 generator.PressKey(ui::VKEY_TAB, ui::EF_ALT_DOWN); |
| 611 EXPECT_EQ(1u, key_count.GetCountAndReset()); | 670 EXPECT_EQ(1, event_count.GetKeyEventCountAndReset()); |
| 612 } | 671 } |
| 613 | 672 |
| 614 } // namespace ash | 673 } // namespace ash |
| OLD | NEW |