always_on_top_controller_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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
  3. // found in the LICENSE file.
  4. #include "ash/wm/always_on_top_controller.h"
  5. #include "ash/keyboard/keyboard_controller_impl.h"
  6. #include "ash/keyboard/ui/keyboard_ui.h"
  7. #include "ash/keyboard/ui/keyboard_ui_controller.h"
  8. #include "ash/keyboard/ui/test/keyboard_test_util.h"
  9. #include "ash/public/cpp/keyboard/keyboard_switches.h"
  10. #include "ash/public/cpp/shell_window_ids.h"
  11. #include "ash/root_window_controller.h"
  12. #include "ash/shell.h"
  13. #include "ash/test/ash_test_base.h"
  14. #include "ash/wm/desks/desks_util.h"
  15. #include "ash/wm/window_state.h"
  16. #include "ash/wm/wm_event.h"
  17. #include "ash/wm/workspace/workspace_layout_manager.h"
  18. #include "base/command_line.h"
  19. #include "base/memory/ptr_util.h"
  20. #include "ui/aura/client/aura_constants.h"
  21. namespace ash {
  22. class AlwaysOnTopControllerTest : public AshTestBase {
  23. public:
  24. AlwaysOnTopControllerTest() = default;
  25. AlwaysOnTopControllerTest(const AlwaysOnTopControllerTest&) = delete;
  26. AlwaysOnTopControllerTest& operator=(const AlwaysOnTopControllerTest&) =
  27. delete;
  28. ~AlwaysOnTopControllerTest() override = default;
  29. void SetUp() override {
  30. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  31. keyboard::switches::kEnableVirtualKeyboard);
  32. AshTestBase::SetUp();
  33. }
  34. };
  35. class TestLayoutManager : public WorkspaceLayoutManager {
  36. public:
  37. explicit TestLayoutManager(aura::Window* window)
  38. : WorkspaceLayoutManager(window),
  39. keyboard_displacing_bounds_changed_(false) {}
  40. TestLayoutManager(const TestLayoutManager&) = delete;
  41. TestLayoutManager& operator=(const TestLayoutManager&) = delete;
  42. ~TestLayoutManager() override = default;
  43. void OnKeyboardDisplacingBoundsChanged(const gfx::Rect& bounds) override {
  44. keyboard_displacing_bounds_changed_ = true;
  45. WorkspaceLayoutManager::OnKeyboardDisplacingBoundsChanged(bounds);
  46. }
  47. bool keyboard_displacing_bounds_changed() const {
  48. return keyboard_displacing_bounds_changed_;
  49. }
  50. private:
  51. bool keyboard_displacing_bounds_changed_;
  52. };
  53. // Verifies that the always on top controller is notified of keyboard bounds
  54. // changing events.
  55. TEST_F(AlwaysOnTopControllerTest, NotifyKeyboardBoundsChanging) {
  56. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  57. aura::Window* always_on_top_container =
  58. Shell::GetContainer(root_window, kShellWindowId_AlwaysOnTopContainer);
  59. // Install test layout manager.
  60. TestLayoutManager* manager = new TestLayoutManager(always_on_top_container);
  61. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  62. AlwaysOnTopController* always_on_top_controller =
  63. controller->always_on_top_controller();
  64. always_on_top_controller->SetLayoutManagerForTest(base::WrapUnique(manager));
  65. // Show the keyboard to change the displacing bounds.
  66. auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  67. keyboard_controller->SetKeyboardWindowBounds(gfx::Rect(0, 0, 100, 100));
  68. EXPECT_FALSE(manager->keyboard_displacing_bounds_changed());
  69. keyboard_controller->ShowKeyboard(true /* locked */);
  70. ASSERT_TRUE(keyboard::WaitUntilShown());
  71. // Verify that test manager was notified of bounds change.
  72. EXPECT_TRUE(manager->keyboard_displacing_bounds_changed());
  73. }
  74. TEST_F(AlwaysOnTopControllerTest,
  75. AlwaysOnTopContainerReturnedForFloatingWindow) {
  76. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  77. AlwaysOnTopController* always_on_top_controller =
  78. controller->always_on_top_controller();
  79. const gfx::Rect bounds(100, 100, 200, 200);
  80. std::unique_ptr<aura::Window> always_on_top_window(
  81. CreateTestWindowInShellWithBounds(bounds));
  82. always_on_top_window->SetProperty(aura::client::kZOrderingKey,
  83. ui::ZOrderLevel::kFloatingWindow);
  84. aura::Window* container =
  85. always_on_top_controller->GetContainer(always_on_top_window.get());
  86. ASSERT_TRUE(container);
  87. EXPECT_EQ(kShellWindowId_AlwaysOnTopContainer, container->GetId());
  88. }
  89. TEST_F(AlwaysOnTopControllerTest, PipContainerReturnedForFloatingPipWindow) {
  90. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  91. AlwaysOnTopController* always_on_top_controller =
  92. controller->always_on_top_controller();
  93. const gfx::Rect bounds(100, 100, 200, 200);
  94. std::unique_ptr<aura::Window> pip_window(
  95. CreateTestWindowInShellWithBounds(bounds));
  96. WindowState* window_state = WindowState::Get(pip_window.get());
  97. const WMEvent enter_pip(WM_EVENT_PIP);
  98. window_state->OnWMEvent(&enter_pip);
  99. pip_window->SetProperty(aura::client::kZOrderingKey,
  100. ui::ZOrderLevel::kFloatingWindow);
  101. EXPECT_TRUE(window_state->IsPip());
  102. aura::Window* container =
  103. always_on_top_controller->GetContainer(pip_window.get());
  104. ASSERT_TRUE(container);
  105. EXPECT_EQ(kShellWindowId_PipContainer, container->GetId());
  106. }
  107. TEST_F(AlwaysOnTopControllerTest,
  108. DefaultContainerReturnedForWindowNotAlwaysOnTop) {
  109. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  110. AlwaysOnTopController* always_on_top_controller =
  111. controller->always_on_top_controller();
  112. const gfx::Rect bounds(100, 100, 200, 200);
  113. std::unique_ptr<aura::Window> window(
  114. CreateTestWindowInShellWithBounds(bounds));
  115. aura::Window* container =
  116. always_on_top_controller->GetContainer(window.get());
  117. ASSERT_TRUE(container);
  118. EXPECT_EQ(desks_util::GetActiveDeskContainerId(), container->GetId());
  119. }
  120. TEST_F(AlwaysOnTopControllerTest,
  121. FloatingWindowMovedBetweenContainersWhenPipStateChanges) {
  122. const gfx::Rect bounds(100, 100, 200, 200);
  123. std::unique_ptr<aura::Window> window(
  124. CreateTestWindowInShellWithBounds(bounds));
  125. window->SetProperty(aura::client::kZOrderingKey,
  126. ui::ZOrderLevel::kFloatingWindow);
  127. EXPECT_EQ(kShellWindowId_AlwaysOnTopContainer, window->parent()->GetId());
  128. WindowState* window_state = WindowState::Get(window.get());
  129. const WMEvent enter_pip(WM_EVENT_PIP);
  130. window_state->OnWMEvent(&enter_pip);
  131. EXPECT_TRUE(window_state->IsPip());
  132. EXPECT_EQ(kShellWindowId_PipContainer, window->parent()->GetId());
  133. const WMEvent enter_normal(WM_EVENT_NORMAL);
  134. window_state->OnWMEvent(&enter_normal);
  135. EXPECT_FALSE(window_state->IsPip());
  136. EXPECT_EQ(kShellWindowId_AlwaysOnTopContainer, window->parent()->GetId());
  137. }
  138. } // namespace ash