ambient_animation_resizer_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2022 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/ambient/ui/ambient_animation_resizer.h"
  5. #include <memory>
  6. #include "base/logging.h"
  7. #include "cc/test/skia_common.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/gfx/geometry/insets.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. #include "ui/gfx/geometry/size.h"
  13. #include "ui/gfx/geometry/vector2d.h"
  14. #include "ui/lottie/animation.h"
  15. #include "ui/views/border.h"
  16. #include "ui/views/controls/animated_image_view.h"
  17. namespace ash {
  18. namespace {
  19. using ::testing::Eq;
  20. std::unique_ptr<views::AnimatedImageView> CreateAnimatedImageView(
  21. const gfx::Size& animation_size,
  22. const gfx::Rect& view_bounds) {
  23. auto view = std::make_unique<views::AnimatedImageView>();
  24. view->SetAnimatedImage(std::make_unique<lottie::Animation>(
  25. cc::CreateSkottie(animation_size,
  26. /*duration_secs=*/1)));
  27. view->SetBoundsRect(view_bounds);
  28. return view;
  29. }
  30. } // namespace
  31. TEST(AmbientAnimationResizerTest, LandscapeScalesDownWidthAndCropsHeight) {
  32. auto view =
  33. CreateAnimatedImageView(gfx::Size(2000, 1500), gfx::Rect(1000, 600));
  34. AmbientAnimationResizer::Resize(*view);
  35. EXPECT_THAT(view->GetImageBounds(), Eq(gfx::Rect(0, -75, 1000, 750)));
  36. }
  37. TEST(AmbientAnimationResizerTest,
  38. LandscapeScalesDownWidthAndCropsHeightWithInsets) {
  39. auto view =
  40. CreateAnimatedImageView(gfx::Size(2000, 1500), gfx::Rect(1000, 600));
  41. // Content bounds are 500 x 300.
  42. view->SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(150, 250)));
  43. AmbientAnimationResizer::Resize(*view);
  44. EXPECT_TRUE(view->GetImageBounds().ApproximatelyEqual(
  45. gfx::Rect(250, 150 - (75 / 2), 500, 375), /*tolerance=*/1));
  46. }
  47. TEST(AmbientAnimationResizerTest, LandscapeScalesDownWidthAndHeight) {
  48. auto view =
  49. CreateAnimatedImageView(gfx::Size(2000, 1500), gfx::Rect(1000, 750));
  50. AmbientAnimationResizer::Resize(*view);
  51. EXPECT_THAT(view->GetImageBounds(), Eq(gfx::Rect(0, 0, 1000, 750)));
  52. }
  53. TEST(AmbientAnimationResizerTest,
  54. LandscapeScalesDownWidthAndDoesNotCropHeight) {
  55. auto view =
  56. CreateAnimatedImageView(gfx::Size(2000, 1500), gfx::Rect(1000, 800));
  57. AmbientAnimationResizer::Resize(*view);
  58. EXPECT_THAT(view->GetImageBounds(), Eq(gfx::Rect(0, 25, 1000, 750)));
  59. }
  60. TEST(AmbientAnimationResizerTest, LandscapeScalesUpByWidth) {
  61. auto view =
  62. CreateAnimatedImageView(gfx::Size(2000, 1500), gfx::Rect(2500, 1500));
  63. AmbientAnimationResizer::Resize(*view);
  64. EXPECT_TRUE(view->GetImageBounds().ApproximatelyEqual(
  65. gfx::Rect(0, -(375 / 2), 2500, 1875), /*tolerance=*/1));
  66. }
  67. TEST(AmbientAnimationResizerTest, LandscapeAppliesJitter) {
  68. auto view =
  69. CreateAnimatedImageView(gfx::Size(2000, 1500), gfx::Rect(1000, 600));
  70. AmbientAnimationResizer::Resize(*view, /*padding_for_jitter=*/10);
  71. // New Height: 1500 * (1020 / 2000) = 765
  72. // New Y origin: -(765 - 600) / 2 = 82.5
  73. EXPECT_THAT(view->GetImageBounds(), Eq(gfx::Rect(-10, -82, 1020, 765)));
  74. }
  75. TEST(AmbientAnimationResizerTest, PortraitScalesDownWidthAndCropsHeight) {
  76. auto view =
  77. CreateAnimatedImageView(gfx::Size(1500, 2000), gfx::Rect(750, 1200));
  78. AmbientAnimationResizer::Resize(*view);
  79. EXPECT_THAT(view->GetImageBounds(), Eq(gfx::Rect(0, 100, 750, 1000)));
  80. }
  81. TEST(AmbientAnimationResizerTest,
  82. PortraitScalesDownWidthAndCropsHeightWithInsets) {
  83. auto view =
  84. CreateAnimatedImageView(gfx::Size(1500, 2000), gfx::Rect(600, 1000));
  85. // Content bounds are 300 x 500.
  86. view->SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(250, 150)));
  87. AmbientAnimationResizer::Resize(*view);
  88. EXPECT_TRUE(view->GetImageBounds().ApproximatelyEqual(
  89. gfx::Rect(150, 250 + (500 - 400) / 2, 300, 400), /*tolerance=*/1));
  90. }
  91. TEST(AmbientAnimationResizerTest, PortraitScalesDownWidthAndHeight) {
  92. auto view =
  93. CreateAnimatedImageView(gfx::Size(1500, 2000), gfx::Rect(750, 1000));
  94. AmbientAnimationResizer::Resize(*view);
  95. EXPECT_THAT(view->GetImageBounds(), Eq(gfx::Rect(0, 0, 750, 1000)));
  96. }
  97. TEST(AmbientAnimationResizerTest, PortraitScalesDownWidthAndDoesNotCropHeight) {
  98. auto view =
  99. CreateAnimatedImageView(gfx::Size(1500, 2000), gfx::Rect(600, 1000));
  100. AmbientAnimationResizer::Resize(*view);
  101. // Scaled height = 2000 / (1500 / 600) = 800
  102. // Image y offset = (1000 - 800) / 2 = 100
  103. EXPECT_THAT(view->GetImageBounds(), Eq(gfx::Rect(0, 100, 600, 800)));
  104. }
  105. TEST(AmbientAnimationResizerTest, PortraitScalesUpByWidth) {
  106. auto view =
  107. CreateAnimatedImageView(gfx::Size(1500, 2000), gfx::Rect(1800, 2000));
  108. AmbientAnimationResizer::Resize(*view);
  109. EXPECT_TRUE(view->GetImageBounds().ApproximatelyEqual(
  110. gfx::Rect(0, -200, 1800, 2400), /*tolerance=*/1));
  111. }
  112. TEST(AmbientAnimationResizerTest, PortraitAppliesJitter) {
  113. auto view =
  114. CreateAnimatedImageView(gfx::Size(1500, 2000), gfx::Rect(600, 1000));
  115. AmbientAnimationResizer::Resize(*view, /*padding_for_jitter=*/10);
  116. // New Height: 2000 * (620 / 1500) = 826 2/3
  117. // New Y origin: -(1000 - 827) / 2 = 86.5
  118. EXPECT_TRUE(view->GetImageBounds().ApproximatelyEqual(
  119. gfx::Rect(-10, 87, 620, 827), /*tolerance=*/1));
  120. }
  121. } // namespace ash