window_positioner_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2013 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/window_positioner.h"
  5. #include <string>
  6. #include "ash/constants/app_types.h"
  7. #include "ash/public/cpp/shelf_config.h"
  8. #include "ash/shell.h"
  9. #include "ash/test/ash_test_base.h"
  10. #include "ash/test/toplevel_window.h"
  11. #include "ash/test_shell_delegate.h"
  12. #include "ash/wm/window_positioner.h"
  13. #include "ash/wm/window_state.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "ui/aura/client/aura_constants.h"
  16. #include "ui/aura/client/window_types.h"
  17. #include "ui/aura/window.h"
  18. #include "ui/display/scoped_display_for_new_windows.h"
  19. #include "ui/display/screen.h"
  20. #include "ui/views/widget/widget.h"
  21. namespace ash {
  22. class WindowPositionerTest : public AshTestBase {
  23. public:
  24. WindowPositionerTest() = default;
  25. WindowPositionerTest(const WindowPositionerTest&) = delete;
  26. WindowPositionerTest& operator=(const WindowPositionerTest&) = delete;
  27. ~WindowPositionerTest() override = default;
  28. };
  29. TEST_F(WindowPositionerTest, OpenDefaultWindowOnSecondDisplay) {
  30. UpdateDisplay("500x400,1400x900");
  31. aura::Window* second_root_window = Shell::GetAllRootWindows()[1];
  32. display::ScopedDisplayForNewWindows display_for_new_windows(
  33. second_root_window);
  34. shell::ToplevelWindow::CreateParams params;
  35. params.can_resize = true;
  36. params.can_maximize = true;
  37. views::Widget* widget = shell::ToplevelWindow::CreateToplevelWindow(params);
  38. gfx::Rect bounds = widget->GetWindowBoundsInScreen();
  39. // The window should be in the 2nd display with the default size.
  40. EXPECT_EQ("300x300", bounds.size().ToString());
  41. EXPECT_TRUE(display::Screen::GetScreen()
  42. ->GetDisplayNearestWindow(second_root_window)
  43. .bounds()
  44. .Contains(bounds));
  45. }
  46. // Tests that second window inherits first window's maximized state as well as
  47. // its restore bounds.
  48. TEST_F(WindowPositionerTest, SecondMaximizedWindowHasProperRestoreSize) {
  49. UpdateDisplay("1400x900");
  50. const int bottom_inset = 900 - ShelfConfig::Get()->shelf_size();
  51. shell::ToplevelWindow::CreateParams params;
  52. params.can_resize = true;
  53. params.can_maximize = true;
  54. views::Widget* widget1 = shell::ToplevelWindow::CreateToplevelWindow(params);
  55. gfx::Rect bounds = widget1->GetWindowBoundsInScreen();
  56. // The window should have default size.
  57. EXPECT_FALSE(widget1->IsMaximized());
  58. EXPECT_EQ("300x300", bounds.size().ToString());
  59. widget1->Maximize();
  60. // The window should be maximized.
  61. bounds = widget1->GetWindowBoundsInScreen();
  62. EXPECT_TRUE(widget1->IsMaximized());
  63. EXPECT_EQ(gfx::Rect(0, 0, 1400, bottom_inset).ToString(), bounds.ToString());
  64. // Create another window
  65. views::Widget* widget2 = shell::ToplevelWindow::CreateToplevelWindow(params);
  66. // The second window should be maximized.
  67. bounds = widget2->GetWindowBoundsInScreen();
  68. EXPECT_TRUE(widget2->IsMaximized());
  69. EXPECT_EQ(gfx::Rect(0, 0, 1400, bottom_inset).ToString(), bounds.ToString());
  70. widget2->Restore();
  71. // Second window's restored size should be set to default size.
  72. bounds = widget2->GetWindowBoundsInScreen();
  73. EXPECT_EQ("300x300", bounds.size().ToString());
  74. }
  75. TEST_F(WindowPositionerTest, IgnoreFullscreenInAutoRearrange) {
  76. // Set bigger than 1366 so that the new window is opened in normal state.
  77. UpdateDisplay("1400x800");
  78. // 1st window mimics fullscreen browser window behavior.
  79. shell::ToplevelWindow::CreateParams params;
  80. params.can_resize = true;
  81. params.can_maximize = true;
  82. views::Widget* widget1 = shell::ToplevelWindow::CreateToplevelWindow(params);
  83. WindowState* managed_state = WindowState::Get(widget1->GetNativeWindow());
  84. EXPECT_TRUE(managed_state->GetWindowPositionManaged());
  85. EXPECT_EQ("300x300", widget1->GetWindowBoundsInScreen().size().ToString());
  86. widget1->SetFullscreen(true);
  87. ASSERT_EQ("1400x800", widget1->GetWindowBoundsInScreen().size().ToString());
  88. // 2nd window mimics windowed v1 app.
  89. params.use_saved_placement = false;
  90. views::Widget* widget2 = shell::ToplevelWindow::CreateToplevelWindow(params);
  91. WindowState* state_2 = WindowState::Get(widget2->GetNativeWindow());
  92. EXPECT_TRUE(state_2->GetWindowPositionManaged());
  93. EXPECT_EQ("300x300", widget2->GetWindowBoundsInScreen().size().ToString());
  94. // Restores to the original size.
  95. widget1->SetFullscreen(false);
  96. ASSERT_EQ("300x300", widget1->GetWindowBoundsInScreen().size().ToString());
  97. // Closing 2nd widget triggers the rearrange logic but the 1st
  98. // widget should stay in the current size.
  99. widget2->CloseNow();
  100. ASSERT_EQ("300x300", widget1->GetWindowBoundsInScreen().size().ToString());
  101. }
  102. // Tests auto managed windows do not auto-position if there are other windows
  103. // opened.
  104. TEST_F(WindowPositionerTest, AutoRearrangeOnHideOrRemove) {
  105. // Create 2 browser windows.
  106. std::unique_ptr<aura::Window> window1 =
  107. CreateAppWindow(gfx::Rect(200, 200, 330, 230), AppType::BROWSER);
  108. std::unique_ptr<aura::Window> window2 =
  109. CreateAppWindow(gfx::Rect(400, 600, 330, 230), AppType::BROWSER);
  110. // Create 1 app window.
  111. std::unique_ptr<aura::Window> window3 =
  112. CreateAppWindow(gfx::Rect(300, 200, 330, 230), AppType::SYSTEM_APP);
  113. WindowState::Get(window1.get())->SetWindowPositionManaged(true);
  114. WindowState::Get(window2.get())->SetWindowPositionManaged(true);
  115. // Closing 2nd browser window triggers the rearrange logic but the 1st
  116. // browser window should stay in the current place.
  117. window2.reset();
  118. EXPECT_EQ(gfx::Rect(200, 200, 330, 230), window1->GetBoundsInScreen());
  119. }
  120. } // namespace ash