window_positioning_utils_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 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_positioning_utils.h"
  5. #include "ash/public/cpp/window_properties.h"
  6. #include "ash/test/ash_test_base.h"
  7. #include "ui/aura/client/aura_constants.h"
  8. #include "ui/aura/test/test_window_delegate.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/display/screen.h"
  11. namespace ash {
  12. using WindowPositioningUtilsTest = AshTestBase;
  13. TEST_F(WindowPositioningUtilsTest, SnapBoundsWithOddNumberedScreenWidth) {
  14. UpdateDisplay("999x700");
  15. auto window = CreateToplevelTestWindow();
  16. gfx::Rect left_bounds = GetDefaultSnappedWindowBoundsInParent(
  17. window.get(), SnapViewType::kPrimary);
  18. gfx::Rect right_bounds = GetDefaultSnappedWindowBoundsInParent(
  19. window.get(), SnapViewType::kSecondary);
  20. EXPECT_EQ(left_bounds.x(), 0);
  21. EXPECT_EQ(left_bounds.y(), 0);
  22. EXPECT_EQ(right_bounds.right(), 999);
  23. EXPECT_EQ(right_bounds.y(), 0);
  24. EXPECT_EQ(left_bounds.right(), right_bounds.x());
  25. EXPECT_NEAR(left_bounds.width(), 499, 1);
  26. EXPECT_NEAR(right_bounds.width(), 499, 1);
  27. }
  28. TEST_F(WindowPositioningUtilsTest, SnapBoundsWithMinimumSize) {
  29. UpdateDisplay("800x600");
  30. auto window = CreateToplevelTestWindow();
  31. auto* test_delegate =
  32. static_cast<aura::test::TestWindowDelegate*>(window->delegate());
  33. test_delegate->set_minimum_size(gfx::Size(300, 200));
  34. gfx::Rect left_bounds = GetDefaultSnappedWindowBoundsInParent(
  35. window.get(), SnapViewType::kPrimary);
  36. EXPECT_EQ(left_bounds.width(), 400);
  37. gfx::Rect right_bounds = GetDefaultSnappedWindowBoundsInParent(
  38. window.get(), SnapViewType::kSecondary);
  39. EXPECT_EQ(right_bounds.width(), 400);
  40. EXPECT_EQ(right_bounds.right(), 800);
  41. test_delegate->set_minimum_size(gfx::Size(600, 200));
  42. left_bounds = GetDefaultSnappedWindowBoundsInParent(window.get(),
  43. SnapViewType::kPrimary);
  44. EXPECT_EQ(left_bounds.width(), 600);
  45. right_bounds = GetDefaultSnappedWindowBoundsInParent(
  46. window.get(), SnapViewType::kSecondary);
  47. EXPECT_EQ(right_bounds.width(), 600);
  48. EXPECT_EQ(right_bounds.right(), 800);
  49. test_delegate->set_minimum_size(gfx::Size(1200, 200));
  50. left_bounds = GetDefaultSnappedWindowBoundsInParent(window.get(),
  51. SnapViewType::kPrimary);
  52. EXPECT_EQ(left_bounds.width(), 800);
  53. right_bounds = GetDefaultSnappedWindowBoundsInParent(
  54. window.get(), SnapViewType::kSecondary);
  55. EXPECT_EQ(right_bounds.width(), 800);
  56. EXPECT_EQ(right_bounds.right(), 800);
  57. }
  58. TEST_F(WindowPositioningUtilsTest, SnapBoundsWithUnresizableSnapProperty) {
  59. auto window = CreateToplevelTestWindow();
  60. window->SetProperty(aura::client::kResizeBehaviorKey,
  61. aura::client::kResizeBehaviorNone);
  62. {
  63. // Test landscape display.
  64. UpdateDisplay("800x600");
  65. const auto work_area =
  66. display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
  67. window->SetProperty(kUnresizableSnappedSizeKey, new gfx::Size(200, 0));
  68. gfx::Rect left_bounds = GetDefaultSnappedWindowBoundsInParent(
  69. window.get(), SnapViewType::kPrimary);
  70. gfx::Rect right_bounds = GetDefaultSnappedWindowBoundsInParent(
  71. window.get(), SnapViewType::kSecondary);
  72. EXPECT_EQ(left_bounds.x(), work_area.x());
  73. EXPECT_EQ(left_bounds.y(), work_area.y());
  74. EXPECT_EQ(left_bounds.width(), 200);
  75. EXPECT_EQ(right_bounds.right(), work_area.right());
  76. EXPECT_EQ(right_bounds.y(), work_area.y());
  77. EXPECT_EQ(right_bounds.width(), 200);
  78. }
  79. {
  80. // Test portrait display.
  81. UpdateDisplay("600x800");
  82. const auto work_area =
  83. display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
  84. window->SetProperty(kUnresizableSnappedSizeKey, new gfx::Size(0, 200));
  85. gfx::Rect top_bounds = GetDefaultSnappedWindowBoundsInParent(
  86. window.get(), SnapViewType::kPrimary);
  87. gfx::Rect bottom_bounds = GetDefaultSnappedWindowBoundsInParent(
  88. window.get(), SnapViewType::kSecondary);
  89. EXPECT_EQ(top_bounds.x(), work_area.x());
  90. EXPECT_EQ(top_bounds.y(), work_area.y());
  91. EXPECT_EQ(top_bounds.height(), 200);
  92. EXPECT_EQ(bottom_bounds.x(), work_area.x());
  93. EXPECT_EQ(bottom_bounds.bottom(), work_area.bottom());
  94. EXPECT_EQ(bottom_bounds.height(), 200);
  95. }
  96. }
  97. } // namespace ash