default_window_resizer_unittest.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2018 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/default_window_resizer.h"
  5. #include "ash/root_window_controller.h"
  6. #include "ash/shell.h"
  7. #include "ash/test/ash_test_base.h"
  8. #include "base/test/metrics/histogram_tester.h"
  9. #include "ui/aura/client/aura_constants.h"
  10. #include "ui/aura/test/test_window_delegate.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/base/hit_test.h"
  13. #include "ui/base/ui_base_types.h"
  14. #include "ui/compositor/test/test_utils.h"
  15. namespace ash {
  16. class DefaultWindowResizerTest : public AshTestBase {
  17. public:
  18. DefaultWindowResizerTest() = default;
  19. DefaultWindowResizerTest(const DefaultWindowResizerTest&) = delete;
  20. DefaultWindowResizerTest& operator=(const DefaultWindowResizerTest&) = delete;
  21. ~DefaultWindowResizerTest() override = default;
  22. void SetUp() override {
  23. AshTestBase::SetUp();
  24. UpdateDisplay("1200x1000");
  25. delegate_.set_minimum_size(gfx::Size(10, 10));
  26. delegate_.set_maximum_size(gfx::Size(500, 500));
  27. aspect_ratio_window_ = std::make_unique<aura::Window>(
  28. &delegate_, aura::client::WINDOW_TYPE_NORMAL);
  29. aspect_ratio_window_->Init(ui::LAYER_NOT_DRAWN);
  30. ParentWindowInPrimaryRootWindow(aspect_ratio_window_.get());
  31. }
  32. void TearDown() override {
  33. aspect_ratio_window_.reset();
  34. AshTestBase::TearDown();
  35. }
  36. protected:
  37. static WindowResizer* CreateDefaultWindowResizer(
  38. aura::Window* window,
  39. const gfx::PointF& point_in_parent,
  40. int window_component) {
  41. return CreateWindowResizer(window, point_in_parent, window_component,
  42. ::wm::WINDOW_MOVE_SOURCE_MOUSE)
  43. .release();
  44. }
  45. aura::test::TestWindowDelegate delegate_;
  46. std::unique_ptr<aura::Window> aspect_ratio_window_;
  47. base::HistogramTester histograms_;
  48. };
  49. // Tests window resizing with a square aspect ratio.
  50. TEST_F(DefaultWindowResizerTest, WindowResizeWithAspectRatioSquare) {
  51. aspect_ratio_window_->SetProperty(aura::client::kAspectRatio,
  52. new gfx::SizeF(1.0, 1.0));
  53. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  54. ASSERT_EQ(1U, root_windows.size());
  55. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  56. aspect_ratio_window_->SetBoundsInScreen(
  57. gfx::Rect(200, 200, 200, 200),
  58. display::Screen::GetScreen()->GetDisplayNearestWindow(root_windows[0]));
  59. EXPECT_EQ("200,200 200x200", aspect_ratio_window_->bounds().ToString());
  60. std::unique_ptr<WindowResizer> resizer(CreateDefaultWindowResizer(
  61. aspect_ratio_window_.get(), gfx::PointF(), HTTOPLEFT));
  62. ASSERT_TRUE(resizer.get());
  63. // Move the mouse near the top left edge.
  64. resizer->Drag(gfx::PointF(50, 50), 0);
  65. resizer->CompleteDrag();
  66. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  67. EXPECT_EQ("250,250 150x150", aspect_ratio_window_->bounds().ToString());
  68. }
  69. // Tests window resizing with a horizontal orientation aspect ratio.
  70. TEST_F(DefaultWindowResizerTest, WindowResizeWithAspectRatioHorizontal) {
  71. aspect_ratio_window_->SetProperty(aura::client::kAspectRatio,
  72. new gfx::SizeF(2.0, 1.0));
  73. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  74. ASSERT_EQ(1U, root_windows.size());
  75. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  76. aspect_ratio_window_->SetBoundsInScreen(
  77. gfx::Rect(200, 200, 400, 200),
  78. display::Screen::GetScreen()->GetDisplayNearestWindow(root_windows[0]));
  79. EXPECT_EQ("200,200 400x200", aspect_ratio_window_->bounds().ToString());
  80. std::unique_ptr<WindowResizer> resizer(CreateDefaultWindowResizer(
  81. aspect_ratio_window_.get(), gfx::PointF(), HTBOTTOMRIGHT));
  82. ASSERT_TRUE(resizer.get());
  83. // Move the mouse near the top left edge.
  84. resizer->Drag(gfx::PointF(50, 50), 0);
  85. resizer->CompleteDrag();
  86. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  87. EXPECT_EQ("200,200 500x250", aspect_ratio_window_->bounds().ToString());
  88. }
  89. // Tests window resizing with a vertical orientation aspect ratio.
  90. TEST_F(DefaultWindowResizerTest, WindowResizeWithAspectRatioVertical) {
  91. aspect_ratio_window_->SetProperty(aura::client::kAspectRatio,
  92. new gfx::SizeF(1.0, 2.0));
  93. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  94. ASSERT_EQ(1U, root_windows.size());
  95. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  96. aspect_ratio_window_->SetBoundsInScreen(
  97. gfx::Rect(200, 200, 200, 400),
  98. display::Screen::GetScreen()->GetDisplayNearestWindow(root_windows[0]));
  99. EXPECT_EQ("200,200 200x400", aspect_ratio_window_->bounds().ToString());
  100. std::unique_ptr<WindowResizer> resizer(CreateDefaultWindowResizer(
  101. aspect_ratio_window_.get(), gfx::PointF(), HTBOTTOM));
  102. ASSERT_TRUE(resizer.get());
  103. // Move the mouse near the top left edge.
  104. resizer->Drag(gfx::PointF(50, 50), 0);
  105. resizer->CompleteDrag();
  106. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  107. EXPECT_EQ("200,200 225x450", aspect_ratio_window_->bounds().ToString());
  108. }
  109. // Tests window dragging with a vertical orientation aspect ratio.
  110. TEST_F(DefaultWindowResizerTest, WindowDragWithAspectRatioVertical) {
  111. aspect_ratio_window_->SetProperty(aura::client::kAspectRatio,
  112. new gfx::SizeF(1.0, 2.0));
  113. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  114. ASSERT_EQ(1U, root_windows.size());
  115. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  116. aspect_ratio_window_->SetBoundsInScreen(
  117. gfx::Rect(200, 200, 200, 400),
  118. display::Screen::GetScreen()->GetDisplayNearestWindow(root_windows[0]));
  119. EXPECT_EQ("200,200 200x400", aspect_ratio_window_->bounds().ToString());
  120. std::unique_ptr<WindowResizer> resizer(CreateDefaultWindowResizer(
  121. aspect_ratio_window_.get(), gfx::PointF(), HTCAPTION));
  122. ASSERT_TRUE(resizer.get());
  123. // Move the mouse near the top left edge.
  124. resizer->Drag(gfx::PointF(50, 50), 0);
  125. resizer->CompleteDrag();
  126. EXPECT_EQ(root_windows[0], aspect_ratio_window_->GetRootWindow());
  127. EXPECT_EQ("250,250 200x400", aspect_ratio_window_->bounds().ToString());
  128. }
  129. TEST_F(DefaultWindowResizerTest, NoResizeHistogramOnMove) {
  130. std::unique_ptr<aura::Window> window = std::make_unique<aura::Window>(
  131. &delegate_, aura::client::WINDOW_TYPE_NORMAL);
  132. window->Init(ui::LAYER_NOT_DRAWN);
  133. ParentWindowInPrimaryRootWindow(window.get());
  134. window->SetBounds(gfx::Rect(0, 0, 50, 50));
  135. std::unique_ptr<WindowResizer> resizer(
  136. CreateDefaultWindowResizer(window.get(), gfx::PointF(), HTCAPTION));
  137. ASSERT_TRUE(resizer.get());
  138. // Move the window. A move should not generate a resize histogram.
  139. resizer->Drag(gfx::PointF(50, 50), 0);
  140. EXPECT_EQ(gfx::Point(50, 50), window->bounds().origin());
  141. resizer->CompleteDrag();
  142. EXPECT_TRUE(
  143. ui::WaitForNextFrameToBePresented(window->GetHost()->compositor()));
  144. histograms_.ExpectTotalCount("Ash.InteractiveWindowResize.TimeToPresent", 0);
  145. }
  146. TEST_F(DefaultWindowResizerTest, ResizeHistogram) {
  147. std::unique_ptr<aura::Window> window = std::make_unique<aura::Window>(
  148. &delegate_, aura::client::WINDOW_TYPE_NORMAL);
  149. window->Init(ui::LAYER_NOT_DRAWN);
  150. ParentWindowInPrimaryRootWindow(window.get());
  151. window->SetBounds(gfx::Rect(0, 0, 50, 50));
  152. std::unique_ptr<WindowResizer> resizer(
  153. CreateDefaultWindowResizer(window.get(), gfx::PointF(), HTRIGHT));
  154. ASSERT_TRUE(resizer.get());
  155. // Resize the window, which should generate a resize histogram.
  156. resizer->Drag(gfx::PointF(50, 50), 0);
  157. EXPECT_NE(gfx::Size(50, 50), window->bounds().size());
  158. resizer->CompleteDrag();
  159. EXPECT_TRUE(
  160. ui::WaitForNextFrameToBePresented(window->GetHost()->compositor()));
  161. histograms_.ExpectTotalCount("Ash.InteractiveWindowResize.TimeToPresent", 1);
  162. }
  163. } // namespace ash