split_view_highlight_view_unittest.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/splitview/split_view_highlight_view.h"
  5. #include "ash/display/screen_orientation_controller.h"
  6. #include "ash/display/screen_orientation_controller_test_api.h"
  7. #include "ash/shell.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "ash/wm/splitview/split_view_constants.h"
  10. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  11. #include "base/test/icu_test_util.h"
  12. #include "ui/compositor/layer.h"
  13. #include "ui/compositor/layer_animator.h"
  14. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  15. #include "ui/display/test/display_manager_test_api.h"
  16. #include "ui/views/widget/widget.h"
  17. #include "ui/views/widget/widget_delegate.h"
  18. namespace ash {
  19. class SplitViewHighlightViewTest : public AshTestBase {
  20. public:
  21. SplitViewHighlightViewTest() = default;
  22. ~SplitViewHighlightViewTest() override = default;
  23. SplitViewHighlightViewTest(const SplitViewHighlightViewTest&) = delete;
  24. SplitViewHighlightViewTest& operator=(const SplitViewHighlightViewTest&) =
  25. delete;
  26. // AshTestBase:
  27. void SetUp() override {
  28. AshTestBase::SetUp();
  29. widget_ = CreateTestWidget();
  30. left_highlight_ =
  31. widget_->widget_delegate()->GetContentsView()->AddChildView(
  32. std::make_unique<SplitViewHighlightView>(false));
  33. right_highlight_ =
  34. widget_->widget_delegate()->GetContentsView()->AddChildView(
  35. std::make_unique<SplitViewHighlightView>(true));
  36. }
  37. void SetLeftBounds(const gfx::Rect& bounds, bool animate) {
  38. SetBounds(bounds, /*is_left=*/true, animate);
  39. }
  40. void SetRightBounds(const gfx::Rect& bounds, bool animate) {
  41. SetBounds(bounds, /*is_left=*/false, animate);
  42. }
  43. protected:
  44. SplitViewHighlightView* left_highlight_;
  45. SplitViewHighlightView* right_highlight_;
  46. std::unique_ptr<views::Widget> widget_;
  47. private:
  48. void SetBounds(const gfx::Rect& bounds, bool is_left, bool animate) {
  49. // The animation type only determines the duration and tween. For testing,
  50. // any valid animation type would work.
  51. auto animation_type =
  52. animate ? absl::make_optional(SPLITVIEW_ANIMATION_PREVIEW_AREA_SLIDE_IN)
  53. : absl::nullopt;
  54. auto* highlight_view = is_left ? left_highlight_ : right_highlight_;
  55. highlight_view->SetBounds(bounds, animation_type);
  56. }
  57. };
  58. TEST_F(SplitViewHighlightViewTest, HighlightGrows) {
  59. ui::ScopedAnimationDurationScaleMode scoped_animation_duration(
  60. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  61. // Tests that before animating, we set the bounds to the desired bounds and
  62. // clip the rect to the size of the old bounds.
  63. gfx::Rect start_bounds(100, 100);
  64. gfx::Rect end_bounds(200, 100);
  65. SetLeftBounds(start_bounds, /*animate=*/false);
  66. SetLeftBounds(end_bounds, /*animate=*/true);
  67. EXPECT_EQ(end_bounds, left_highlight_->bounds());
  68. EXPECT_EQ(start_bounds, left_highlight_->layer()->clip_rect());
  69. // After the animation is finished the clip rect should be removed.
  70. left_highlight_->layer()->GetAnimator()->StopAnimating();
  71. EXPECT_EQ(gfx::Rect(), left_highlight_->layer()->clip_rect());
  72. // Tests that for right highlights, the clip is shifted as the animation is
  73. // mirrored.
  74. start_bounds = gfx::Rect(100, 0, 100, 100);
  75. end_bounds = gfx::Rect(200, 100);
  76. SetRightBounds(start_bounds, /*animate=*/false);
  77. SetRightBounds(end_bounds, /*animate=*/true);
  78. EXPECT_EQ(end_bounds, right_highlight_->bounds());
  79. EXPECT_EQ(start_bounds, right_highlight_->layer()->clip_rect());
  80. // After the animation is finished the clip rect should be removed.
  81. right_highlight_->layer()->GetAnimator()->StopAnimating();
  82. EXPECT_EQ(gfx::Rect(), right_highlight_->layer()->clip_rect());
  83. }
  84. TEST_F(SplitViewHighlightViewTest, HighlightShrinks) {
  85. ui::ScopedAnimationDurationScaleMode scoped_animation_duration(
  86. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  87. // Tests that when the highlight shrinks, the bounds do not get set until the
  88. // animation is complete.
  89. gfx::Rect start_bounds(200, 100);
  90. gfx::Rect end_bounds(100, 100);
  91. SetLeftBounds(start_bounds, /*animate=*/false);
  92. SetLeftBounds(end_bounds, /*animate=*/true);
  93. EXPECT_EQ(start_bounds, left_highlight_->bounds());
  94. EXPECT_EQ(start_bounds, left_highlight_->layer()->clip_rect());
  95. // After the animation is finished the clip rect should be removed and the
  96. // bounds should be set.
  97. left_highlight_->layer()->GetAnimator()->StopAnimating();
  98. EXPECT_EQ(end_bounds, left_highlight_->bounds());
  99. EXPECT_EQ(gfx::Rect(), left_highlight_->layer()->clip_rect());
  100. }
  101. TEST_F(SplitViewHighlightViewTest, PortraitMode) {
  102. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  103. // Set display to portrait mode.
  104. int64_t display_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
  105. display::DisplayManager* display_manager = Shell::Get()->display_manager();
  106. display::test::ScopedSetInternalDisplayId set_internal(display_manager,
  107. display_id);
  108. ScreenOrientationControllerTestApi test_api(
  109. Shell::Get()->screen_orientation_controller());
  110. test_api.SetDisplayRotation(display::Display::ROTATE_90,
  111. display::Display::RotationSource::ACTIVE);
  112. ui::ScopedAnimationDurationScaleMode scoped_animation_duration(
  113. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  114. for (bool is_rtl : {false, true}) {
  115. // RTL should not affect portrait highlights.
  116. base::test::ScopedRestoreICUDefaultLocale scoped_locale(is_rtl ? "he"
  117. : "en_US");
  118. SCOPED_TRACE(is_rtl ? "RTL" : "LTR");
  119. // Tests that before animating, we set the bounds to the desired bounds and
  120. // clip the rect to the size of the old bounds.
  121. gfx::Rect start_bounds(100, 100);
  122. gfx::Rect end_bounds(100, 200);
  123. SetLeftBounds(start_bounds, /*animate=*/false);
  124. SetLeftBounds(end_bounds, /*animate=*/true);
  125. EXPECT_EQ(end_bounds, left_highlight_->bounds());
  126. EXPECT_EQ(start_bounds, left_highlight_->layer()->clip_rect());
  127. // After the animation is finished the clip rect should be removed.
  128. left_highlight_->layer()->GetAnimator()->StopAnimating();
  129. EXPECT_EQ(gfx::Rect(), left_highlight_->layer()->clip_rect());
  130. // Tests that for bottom highlights, the clip is shifted as the animation is
  131. // comes from bottom up instead of top down.
  132. start_bounds = gfx::Rect(0, 100, 100, 100);
  133. end_bounds = gfx::Rect(200, 100);
  134. SetRightBounds(start_bounds, /*animate=*/false);
  135. SetRightBounds(end_bounds, /*animate=*/true);
  136. EXPECT_EQ(end_bounds, right_highlight_->bounds());
  137. EXPECT_EQ(start_bounds, right_highlight_->layer()->clip_rect());
  138. // After the animation is finished the clip rect should be removed.
  139. right_highlight_->layer()->GetAnimator()->StopAnimating();
  140. EXPECT_EQ(gfx::Rect(), right_highlight_->layer()->clip_rect());
  141. }
  142. }
  143. // Tests that the highlights work as in expected in RTL.
  144. TEST_F(SplitViewHighlightViewTest, HighlightInRtl) {
  145. base::test::ScopedRestoreICUDefaultLocale scoped_locale("he");
  146. ui::ScopedAnimationDurationScaleMode scoped_animation_duration(
  147. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  148. // In RTL, the right highlight gets mirrored bounds, so its start and end
  149. // bounds will have the same origin.
  150. const gfx::Rect start_bounds(0, 0, 100, 100);
  151. const gfx::Rect end_bounds(0, 0, 200, 100);
  152. SetRightBounds(start_bounds, /*animate=*/false);
  153. SetRightBounds(end_bounds, /*animate=*/true);
  154. EXPECT_EQ(end_bounds, right_highlight_->bounds());
  155. EXPECT_EQ(gfx::Rect(100, 0, 100, 100),
  156. right_highlight_->layer()->clip_rect());
  157. right_highlight_->layer()->GetAnimator()->StopAnimating();
  158. EXPECT_EQ(gfx::Rect(), right_highlight_->layer()->clip_rect());
  159. }
  160. } // namespace ash