shadow_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "ui/compositor_extra/shadow.h"
  5. #include "base/test/test_discardable_memory_allocator.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "ui/compositor/layer.h"
  8. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  9. #include "ui/gfx/geometry/insets.h"
  10. #include "ui/gfx/shadow_util.h"
  11. #include "ui/gfx/shadow_value.h"
  12. namespace ui {
  13. namespace {
  14. constexpr int kElevationLarge = 24;
  15. constexpr int kElevationSmall = 6;
  16. gfx::Insets InsetsForElevation(int elevation) {
  17. return -gfx::Insets(2 * elevation) +
  18. gfx::Insets::TLBR(elevation, 0, -elevation, 0);
  19. }
  20. gfx::Size NineboxImageSizeForElevationAndCornerRadius(int elevation,
  21. int corner_radius) {
  22. auto values = gfx::ShadowValue::MakeMdShadowValues(elevation);
  23. gfx::Rect bounds(0, 0, 1, 1);
  24. bounds.Inset(-gfx::ShadowValue::GetBlurRegion(values));
  25. bounds.Inset(-gfx::Insets(corner_radius));
  26. return bounds.size();
  27. }
  28. class ShadowTest : public testing::Test {
  29. public:
  30. ShadowTest(const ShadowTest&) = delete;
  31. ShadowTest& operator=(const ShadowTest&) = delete;
  32. protected:
  33. ShadowTest() {}
  34. ~ShadowTest() override {}
  35. void SetUp() override {
  36. base::DiscardableMemoryAllocator::SetInstance(
  37. &discardable_memory_allocator_);
  38. }
  39. void TearDown() override {
  40. base::DiscardableMemoryAllocator::SetInstance(nullptr);
  41. }
  42. private:
  43. base::TestDiscardableMemoryAllocator discardable_memory_allocator_;
  44. };
  45. // Test if the proper content bounds is calculated based on the current style.
  46. TEST_F(ShadowTest, SetContentBounds) {
  47. ScopedAnimationDurationScaleMode zero_duration_mode(
  48. ScopedAnimationDurationScaleMode::ZERO_DURATION);
  49. // Verify that layer bounds are outset from content bounds.
  50. Shadow shadow;
  51. {
  52. shadow.Init(kElevationLarge);
  53. gfx::Rect content_bounds(100, 100, 300, 300);
  54. shadow.SetContentBounds(content_bounds);
  55. EXPECT_EQ(content_bounds, shadow.content_bounds());
  56. gfx::Rect shadow_bounds(content_bounds);
  57. shadow_bounds.Inset(InsetsForElevation(kElevationLarge));
  58. EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  59. }
  60. {
  61. shadow.SetElevation(kElevationSmall);
  62. gfx::Rect content_bounds(100, 100, 300, 300);
  63. shadow.SetContentBounds(content_bounds);
  64. EXPECT_EQ(content_bounds, shadow.content_bounds());
  65. gfx::Rect shadow_bounds(content_bounds);
  66. shadow_bounds.Inset(InsetsForElevation(kElevationSmall));
  67. EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  68. }
  69. }
  70. // Test that the elevation is reduced when the contents are too small to handle
  71. // the full elevation.
  72. TEST_F(ShadowTest, AdjustElevationForSmallContents) {
  73. Shadow shadow;
  74. shadow.Init(kElevationLarge);
  75. {
  76. gfx::Rect content_bounds(100, 100, 300, 300);
  77. shadow.SetContentBounds(content_bounds);
  78. gfx::Rect shadow_bounds(content_bounds);
  79. shadow_bounds.Inset(InsetsForElevation(kElevationLarge));
  80. EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  81. }
  82. {
  83. constexpr int kWidth = 80;
  84. gfx::Rect content_bounds(100, 100, kWidth, 300);
  85. shadow.SetContentBounds(content_bounds);
  86. gfx::Rect shadow_bounds(content_bounds);
  87. shadow_bounds.Inset(InsetsForElevation((kWidth - 4) / 4));
  88. EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  89. }
  90. {
  91. constexpr int kHeight = 80;
  92. gfx::Rect content_bounds(100, 100, 300, kHeight);
  93. shadow.SetContentBounds(content_bounds);
  94. gfx::Rect shadow_bounds(content_bounds);
  95. shadow_bounds.Inset(InsetsForElevation((kHeight - 4) / 4));
  96. EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  97. }
  98. }
  99. // Test that rounded corner radius is handled correctly.
  100. TEST_F(ShadowTest, AdjustRoundedCornerRadius) {
  101. Shadow shadow;
  102. shadow.Init(kElevationSmall);
  103. gfx::Rect content_bounds(100, 100, 300, 300);
  104. shadow.SetContentBounds(content_bounds);
  105. EXPECT_EQ(content_bounds, shadow.content_bounds());
  106. shadow.SetRoundedCornerRadius(0);
  107. gfx::Rect shadow_bounds(content_bounds);
  108. shadow_bounds.Inset(InsetsForElevation(kElevationSmall));
  109. EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  110. EXPECT_EQ(NineboxImageSizeForElevationAndCornerRadius(6, 0),
  111. shadow.details_for_testing()->ninebox_image.size());
  112. }
  113. } // namespace
  114. } // namespace ui