screen_util_unittest.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/screen_util.h"
  5. #include <memory>
  6. #include "ash/accessibility/magnifier/docked_magnifier_controller.h"
  7. #include "ash/public/cpp/shelf_config.h"
  8. #include "ash/root_window_controller.h"
  9. #include "ash/shelf/shelf.h"
  10. #include "ash/shell.h"
  11. #include "ash/test/ash_test_base.h"
  12. #include "ash/wm/desks/desks_util.h"
  13. #include "ash/wm/window_util.h"
  14. #include "ash/wm/wm_event.h"
  15. #include "ui/aura/env.h"
  16. #include "ui/aura/window.h"
  17. #include "ui/aura/window_event_dispatcher.h"
  18. #include "ui/display/manager/display_manager.h"
  19. #include "ui/display/unified_desktop_utils.h"
  20. #include "ui/views/widget/widget.h"
  21. #include "ui/views/widget/widget_delegate.h"
  22. #include "ui/wm/core/coordinate_conversion.h"
  23. namespace ash {
  24. using ScreenUtilTest = AshTestBase;
  25. TEST_F(ScreenUtilTest, Bounds) {
  26. UpdateDisplay("700x600,600x500");
  27. views::Widget* primary = views::Widget::CreateWindowWithContext(
  28. nullptr, GetContext(), gfx::Rect(10, 10, 100, 100));
  29. primary->Show();
  30. views::Widget* secondary = views::Widget::CreateWindowWithContext(
  31. nullptr, GetContext(), gfx::Rect(710, 10, 100, 100));
  32. secondary->Show();
  33. // Maximized bounds.
  34. const int bottom_inset_first = 600 - ShelfConfig::Get()->shelf_size();
  35. const int bottom_inset_second = 500 - ShelfConfig::Get()->shelf_size();
  36. EXPECT_EQ(
  37. gfx::Rect(0, 0, 700, bottom_inset_first).ToString(),
  38. screen_util::GetMaximizedWindowBoundsInParent(primary->GetNativeView())
  39. .ToString());
  40. EXPECT_EQ(
  41. gfx::Rect(0, 0, 600, bottom_inset_second).ToString(),
  42. screen_util::GetMaximizedWindowBoundsInParent(secondary->GetNativeView())
  43. .ToString());
  44. // Display bounds
  45. EXPECT_EQ("0,0 700x600",
  46. screen_util::GetDisplayBoundsInParent(primary->GetNativeView())
  47. .ToString());
  48. EXPECT_EQ("0,0 600x500",
  49. screen_util::GetDisplayBoundsInParent(secondary->GetNativeView())
  50. .ToString());
  51. // Work area bounds
  52. EXPECT_EQ(
  53. gfx::Rect(0, 0, 700, bottom_inset_first).ToString(),
  54. screen_util::GetDisplayWorkAreaBoundsInParent(primary->GetNativeView())
  55. .ToString());
  56. EXPECT_EQ(
  57. gfx::Rect(0, 0, 600, bottom_inset_second).ToString(),
  58. screen_util::GetDisplayWorkAreaBoundsInParent(secondary->GetNativeView())
  59. .ToString());
  60. }
  61. // Test verifies a stable handling of secondary screen widget changes
  62. // (crbug.com/226132).
  63. TEST_F(ScreenUtilTest, StabilityTest) {
  64. UpdateDisplay("700x600,600x500");
  65. views::Widget* secondary = views::Widget::CreateWindowWithContext(
  66. nullptr, GetContext(), gfx::Rect(710, 10, 100, 100));
  67. EXPECT_EQ(Shell::GetAllRootWindows()[1],
  68. secondary->GetNativeView()->GetRootWindow());
  69. secondary->Show();
  70. secondary->Maximize();
  71. secondary->Show();
  72. secondary->SetFullscreen(true);
  73. secondary->Hide();
  74. secondary->Close();
  75. }
  76. TEST_F(ScreenUtilTest, ConvertRect) {
  77. UpdateDisplay("700x600,600x500");
  78. views::Widget* primary = views::Widget::CreateWindowWithContext(
  79. nullptr, GetContext(), gfx::Rect(10, 10, 100, 100));
  80. primary->Show();
  81. views::Widget* secondary = views::Widget::CreateWindowWithContext(
  82. nullptr, GetContext(), gfx::Rect(710, 10, 100, 100));
  83. secondary->Show();
  84. gfx::Rect r1(10, 10, 100, 100);
  85. ::wm::ConvertRectFromScreen(primary->GetNativeView(), &r1);
  86. EXPECT_EQ("0,0 100x100", r1.ToString());
  87. gfx::Rect r2(720, 20, 100, 100);
  88. ::wm::ConvertRectFromScreen(secondary->GetNativeView(), &r2);
  89. EXPECT_EQ("10,10 100x100", r2.ToString());
  90. gfx::Rect r3(30, 30, 100, 100);
  91. ::wm::ConvertRectToScreen(primary->GetNativeView(), &r3);
  92. EXPECT_EQ("40,40 100x100", r3.ToString());
  93. gfx::Rect r4(40, 40, 100, 100);
  94. ::wm::ConvertRectToScreen(secondary->GetNativeView(), &r4);
  95. EXPECT_EQ("750,50 100x100", r4.ToString());
  96. }
  97. TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktop) {
  98. display_manager()->SetUnifiedDesktopEnabled(true);
  99. views::Widget* widget = views::Widget::CreateWindowWithContext(
  100. nullptr, GetContext(), gfx::Rect(10, 10, 100, 100));
  101. aura::Window* window = widget->GetNativeWindow();
  102. UpdateDisplay("500x400");
  103. EXPECT_EQ("0,0 500x400",
  104. screen_util::GetDisplayBoundsWithShelf(window).ToString());
  105. UpdateDisplay("500x400,600x400");
  106. EXPECT_EQ("0,0 500x400",
  107. screen_util::GetDisplayBoundsWithShelf(window).ToString());
  108. // Move to the 2nd physical display. Shelf's display still should be
  109. // the first.
  110. widget->SetBounds(gfx::Rect(800, 0, 100, 100));
  111. ASSERT_EQ("800,0 100x100", widget->GetWindowBoundsInScreen().ToString());
  112. EXPECT_EQ("0,0 500x400",
  113. screen_util::GetDisplayBoundsWithShelf(window).ToString());
  114. UpdateDisplay("600x500");
  115. EXPECT_EQ("0,0 600x500",
  116. screen_util::GetDisplayBoundsWithShelf(window).ToString());
  117. }
  118. TEST_F(ScreenUtilTest, ShelfDisplayBoundsInUnifiedDesktopGrid) {
  119. UpdateDisplay("500x400,400x600,300x600,200x300,600x200,350x400");
  120. display_manager()->SetUnifiedDesktopEnabled(true);
  121. views::Widget* widget = views::Widget::CreateWindowWithContext(
  122. nullptr, GetContext(), gfx::Rect(10, 10, 100, 100));
  123. aura::Window* window = widget->GetNativeWindow();
  124. display::DisplayIdList list = display_manager()->GetConnectedDisplayIdList();
  125. ASSERT_EQ(6u, list.size());
  126. // Create a 3 x 2 vertical layout matrix and set it.
  127. // [500 x 400] [400 x 600]
  128. // [300 x 600] [200 x 300]
  129. // [600 x 200] [350 x 400]
  130. display::UnifiedDesktopLayoutMatrix matrix;
  131. matrix.resize(3u);
  132. matrix[0].emplace_back(list[0]);
  133. matrix[0].emplace_back(list[1]);
  134. matrix[1].emplace_back(list[2]);
  135. matrix[1].emplace_back(list[3]);
  136. matrix[2].emplace_back(list[4]);
  137. matrix[2].emplace_back(list[5]);
  138. display_manager()->SetUnifiedDesktopMatrix(matrix);
  139. display::Screen* screen = display::Screen::GetScreen();
  140. EXPECT_EQ(gfx::Size(766, 1254), screen->GetPrimaryDisplay().size());
  141. Shelf* shelf = Shell::GetPrimaryRootWindowController()->shelf();
  142. EXPECT_EQ(shelf->alignment(), ShelfAlignment::kBottom);
  143. // Regardless of where the window is, the shelf with a bottom alignment is
  144. // always in the bottom left display in the matrix.
  145. EXPECT_EQ(gfx::Rect(0, 1057, 593, 198),
  146. screen_util::GetDisplayBoundsWithShelf(window));
  147. // Move to the bottom right display.
  148. widget->SetBounds(gfx::Rect(620, 940, 100, 100));
  149. EXPECT_EQ(gfx::Rect(0, 1057, 593, 198),
  150. screen_util::GetDisplayBoundsWithShelf(window));
  151. // Change the shelf alignment to left, and expect that it now resides in the
  152. // top left display in the matrix.
  153. shelf->SetAlignment(ShelfAlignment::kLeft);
  154. EXPECT_EQ(gfx::Rect(0, 0, 499, 400),
  155. screen_util::GetDisplayBoundsWithShelf(window));
  156. // Change the shelf alignment to right, and expect that it now resides in the
  157. // top right display in the matrix.
  158. shelf->SetAlignment(ShelfAlignment::kRight);
  159. EXPECT_EQ(gfx::Rect(499, 0, 267, 400),
  160. screen_util::GetDisplayBoundsWithShelf(window));
  161. // Change alignment back to bottom and change the unified display zoom factor.
  162. // Expect that the display with shelf bounds will take into account the zoom
  163. // factor.
  164. shelf->SetAlignment(ShelfAlignment::kBottom);
  165. display_manager()->UpdateZoomFactor(display::kUnifiedDisplayId, 3.f);
  166. const display::Display unified_display =
  167. display_manager()->GetDisplayForId(display::kUnifiedDisplayId);
  168. EXPECT_FLOAT_EQ(unified_display.device_scale_factor(), 3.f);
  169. EXPECT_EQ(gfx::Rect(0, 352, 198, 67),
  170. screen_util::GetDisplayBoundsWithShelf(window));
  171. }
  172. TEST_F(ScreenUtilTest, SnapBoundsToDisplayEdge) {
  173. UpdateDisplay("2400x1600*1.5");
  174. gfx::Rect bounds(1555, 0, 45, 1066);
  175. views::Widget* widget =
  176. views::Widget::CreateWindowWithContext(nullptr, GetContext(), bounds);
  177. aura::Window* window = widget->GetNativeWindow();
  178. gfx::Rect snapped_bounds =
  179. screen_util::SnapBoundsToDisplayEdge(bounds, window);
  180. EXPECT_EQ(snapped_bounds, gfx::Rect(1555, 0, 45, 1067));
  181. bounds = gfx::Rect(5, 1000, 1595, 66);
  182. snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
  183. EXPECT_EQ(snapped_bounds, gfx::Rect(5, 1000, 1595, 67));
  184. UpdateDisplay("800x600");
  185. bounds = gfx::Rect(0, 552, 800, 48);
  186. snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
  187. EXPECT_EQ(snapped_bounds, gfx::Rect(0, 552, 800, 48));
  188. UpdateDisplay("2400x1800*1.8/r");
  189. EXPECT_EQ(gfx::Size(1000, 1333),
  190. display::Screen::GetScreen()->GetPrimaryDisplay().size());
  191. bounds = gfx::Rect(950, 0, 50, 1333);
  192. snapped_bounds = screen_util::SnapBoundsToDisplayEdge(bounds, window);
  193. EXPECT_EQ(snapped_bounds, gfx::Rect(950, 0, 50, 1334));
  194. }
  195. // Tests that making a window fullscreen while the Docked Magnifier is enabled
  196. // won't make its bounds occupy the entire screen bounds, but will take into
  197. // account the Docked Magnifier height.
  198. TEST_F(ScreenUtilTest, FullscreenWindowBoundsWithDockedMagnifier) {
  199. UpdateDisplay("1366x768");
  200. std::unique_ptr<aura::Window> window = CreateToplevelTestWindow(
  201. gfx::Rect(300, 300, 200, 150), desks_util::GetActiveDeskContainerId());
  202. auto* docked_magnifier_controller =
  203. Shell::Get()->docked_magnifier_controller();
  204. docked_magnifier_controller->SetEnabled(true);
  205. const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
  206. WindowState::Get(window.get())->OnWMEvent(&event);
  207. constexpr gfx::Rect kDisplayBounds{1366, 768};
  208. EXPECT_NE(window->bounds(), kDisplayBounds);
  209. gfx::Rect expected_bounds = kDisplayBounds;
  210. expected_bounds.Inset(gfx::Insets().set_top(
  211. docked_magnifier_controller->GetTotalMagnifierHeight()));
  212. EXPECT_EQ(expected_bounds, window->bounds());
  213. }
  214. } // namespace ash