File: shadow_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (303 lines) | stat: -rw-r--r-- 11,117 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/compositor_extra/shadow.h"

#include "base/test/test_discardable_memory_allocator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/shadow_util.h"
#include "ui/gfx/shadow_value.h"

namespace ui {
namespace {

using ::testing::FieldsAre;

constexpr int kElevationLarge = 24;
constexpr int kElevationSmall = 6;

// A specific elevation used for testing EvictUniquelyOwnedDetail.
constexpr int kElevationUnique = 66;

gfx::Insets InsetsForElevation(int elevation) {
  return -gfx::Insets(2 * elevation) +
         gfx::Insets::TLBR(elevation, 0, -elevation, 0);
}

gfx::Size NineboxImageSizeForElevationAndCornerRadius(int elevation,
                                                      int corner_radius) {
  auto values = gfx::ShadowValue::MakeMdShadowValues(elevation);
  gfx::Rect bounds(0, 0, 1, 1);
  bounds.Inset(-gfx::ShadowValue::GetBlurRegion(values));
  bounds.Inset(-gfx::Insets(corner_radius));
  return bounds.size();
}

// Calculates the minimum shadow content size for given elevation and corner
// radius.
gfx::Size MinContentSizeForElevationAndCornerRadius(int elevation,
                                                    int corner_radius) {
  const int dimension = 4 * elevation + 2 * corner_radius;
  return gfx::Size(dimension, dimension);
}

class ShadowTest : public testing::Test {
 public:
  ShadowTest(const ShadowTest&) = delete;
  ShadowTest& operator=(const ShadowTest&) = delete;

 protected:
  ShadowTest() {}
  ~ShadowTest() override {}

  void SetUp() override {
    base::DiscardableMemoryAllocator::SetInstance(
        &discardable_memory_allocator_);
  }

  void TearDown() override {
    base::DiscardableMemoryAllocator::SetInstance(nullptr);
  }

 private:
  base::TestDiscardableMemoryAllocator discardable_memory_allocator_;
};

// Test if the proper content bounds is calculated based on the current style.
TEST_F(ShadowTest, SetContentBounds) {
  ScopedAnimationDurationScaleMode zero_duration_mode(
      ScopedAnimationDurationScaleMode::ZERO_DURATION);
  // Verify that layer bounds are outset from content bounds.
  Shadow shadow;
  {
    shadow.Init(kElevationLarge);
    gfx::Rect content_bounds(100, 100, 300, 300);
    shadow.SetContentBounds(content_bounds);
    EXPECT_EQ(content_bounds, shadow.content_bounds());
    gfx::Rect shadow_bounds(content_bounds);
    shadow_bounds.Inset(InsetsForElevation(kElevationLarge));
    EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  }

  {
    shadow.SetElevation(kElevationSmall);
    gfx::Rect content_bounds(100, 100, 300, 300);
    shadow.SetContentBounds(content_bounds);
    EXPECT_EQ(content_bounds, shadow.content_bounds());
    gfx::Rect shadow_bounds(content_bounds);
    shadow_bounds.Inset(InsetsForElevation(kElevationSmall));
    EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  }
}

// Test if the shadow's layer bounds are modified, setting the same content
// bounds can reset the layer bounds.
TEST_F(ShadowTest, ResetLayerBoundsBySettingSameContentBounds) {
  Shadow shadow;
  shadow.Init(kElevationLarge);
  gfx::Rect content_bounds(100, 100, 300, 300);
  shadow.SetContentBounds(content_bounds);
  EXPECT_EQ(content_bounds, shadow.content_bounds());

  const gfx::Rect layer_bounds = shadow.layer()->bounds();

  // Change shadow's layer bounds.
  const gfx::Rect modified_bounds(200, 200, 150, 400);
  shadow.layer()->SetBounds(modified_bounds);
  EXPECT_EQ(shadow.layer()->bounds(), modified_bounds);

  // Reset layer bounds by setting the same content bounds.
  shadow.SetContentBounds(content_bounds);
  EXPECT_EQ(layer_bounds, shadow.layer()->bounds());
}

// Test that the elevation is reduced when the contents are too small to handle
// the full elevation.
TEST_F(ShadowTest, AdjustElevationForSmallContents) {
  Shadow shadow;
  shadow.Init(kElevationLarge);
  {
    gfx::Rect content_bounds(100, 100, 300, 300);
    shadow.SetContentBounds(content_bounds);
    gfx::Rect shadow_bounds(content_bounds);
    shadow_bounds.Inset(InsetsForElevation(kElevationLarge));
    EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  }

  {
    constexpr int kWidth = 80;
    gfx::Rect content_bounds(100, 100, kWidth, 300);
    shadow.SetContentBounds(content_bounds);
    gfx::Rect shadow_bounds(content_bounds);
    shadow_bounds.Inset(InsetsForElevation((kWidth - 4) / 4));
    EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  }

  {
    constexpr int kHeight = 80;
    gfx::Rect content_bounds(100, 100, 300, kHeight);
    shadow.SetContentBounds(content_bounds);
    gfx::Rect shadow_bounds(content_bounds);
    shadow_bounds.Inset(InsetsForElevation((kHeight - 4) / 4));
    EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  }
}

// Test that rounded corner radius is handled correctly.
TEST_F(ShadowTest, AdjustRoundedCornerRadius) {
  Shadow shadow;
  shadow.Init(kElevationSmall);
  gfx::Rect content_bounds(100, 100, 300, 300);
  shadow.SetContentBounds(content_bounds);
  EXPECT_EQ(content_bounds, shadow.content_bounds());
  shadow.SetRoundedCornerRadius(0);
  gfx::Rect shadow_bounds(content_bounds);
  shadow_bounds.Inset(InsetsForElevation(kElevationSmall));
  EXPECT_EQ(shadow_bounds, shadow.layer()->bounds());
  EXPECT_EQ(NineboxImageSizeForElevationAndCornerRadius(6, 0),
            shadow.details_for_testing()->nine_patch_image.size());
}

// Test that the uniquely owned shadow image is evicted from the cache when new
// shadow details are created.
TEST_F(ShadowTest, EvictUniquelyOwnedDetail) {
  // Insert a new shadow with unique details which will evict existing details
  // from the cache.
  {
    Shadow shadow_new;
    shadow_new.Init(kElevationUnique);
    shadow_new.SetRoundedCornerRadius(2);

    const gfx::Size min_content_size =
        MinContentSizeForElevationAndCornerRadius(kElevationUnique, 2);
    shadow_new.SetContentBounds(gfx::Rect(min_content_size));
    // The cache size should be 1.
    EXPECT_EQ(1u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());

    // Creating a shadow with the same detail won't increase the cache size.
    Shadow shadow_same;
    shadow_same.Init(kElevationUnique);
    shadow_same.SetRoundedCornerRadius(2);
    shadow_same.SetContentBounds(
        gfx::Rect(gfx::Point(10, 10), min_content_size + gfx::Size(50, 50)));
    // The cache size is unchanged.
    EXPECT_EQ(1u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());

    // Creating a new uniquely owned detail will increase the cache size.
    gfx::ShadowDetails::Get(kElevationUnique, 3);
    EXPECT_EQ(2u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());

    // Creating a shadow with different details will replace the uniquely owned
    // detail.
    Shadow shadow_small;
    shadow_small.Init(kElevationSmall);
    shadow_small.SetRoundedCornerRadius(2);
    shadow_small.SetContentBounds(gfx::Rect(
        MinContentSizeForElevationAndCornerRadius(kElevationSmall, 2)));
    EXPECT_EQ(2u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());

    // Changing the shadow appearance will insert a new detail in the cache and
    // make the old detail uniquely owned.
    shadow_small.SetRoundedCornerRadius(3);
    EXPECT_EQ(3u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());

    // Changing the shadow with another appearance will replace the uniquely
    // owned detail.
    shadow_small.SetRoundedCornerRadius(4);
    EXPECT_EQ(3u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());
  }

  // After destroying the all the shadows, the cache has 3 uniquely owned
  // details.
  EXPECT_EQ(3u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());

  // After inserting a new detail, the uniquely owned details will be evicted.
  Shadow shadow_large;
  shadow_large.Init(kElevationLarge);
  shadow_large.SetRoundedCornerRadius(2);
  shadow_large.SetContentBounds(
      gfx::Rect(MinContentSizeForElevationAndCornerRadius(kElevationLarge, 2)));
  // The cache size is unchanged.
  EXPECT_EQ(1u, gfx::ShadowDetails::GetDetailsCacheSizeForTest());
}

class ShadowColorTest : public ShadowTest,
                        public testing::WithParamInterface<gfx::ShadowStyle> {
 public:
  ShadowColorTest() = default;
  ShadowColorTest(const ShadowColorTest&) = delete;
  ShadowColorTest& operator=(const ShadowColorTest&) = delete;
  ~ShadowColorTest() override = default;

  static std::vector<gfx::ShadowStyle> GetTestParamValues() {
#if BUILDFLAG(IS_CHROMEOS)
    return {gfx::ShadowStyle::kMaterialDesign,
            gfx::ShadowStyle::kChromeOSSystemUI};
#else
    return {gfx::ShadowStyle::kMaterialDesign};
#endif
  }
};

INSTANTIATE_TEST_SUITE_P(
    All,
    ShadowColorTest,
    testing::ValuesIn(ShadowColorTest::GetTestParamValues()));

// Tests the shadow colors are updated when setting elevation to colors map.
TEST_P(ShadowColorTest, ElevationToColorsMap) {
  Shadow shadow;
  shadow.Init(kElevationSmall);
  shadow.SetShadowStyle(GetParam());
  // Set the content bounds which is big enough for the large elevation.
  shadow.SetContentBounds(
      gfx::Rect(MinContentSizeForElevationAndCornerRadius(kElevationLarge, 0)));

  // Cache the default colors.
  const auto& values = shadow.details_for_testing()->values;
  const SkColor default_key_color = values[0].color();
  const SkColor default_ambient_color = values[1].color();

  // Set a color map.
  const SkColor small_key_color = SkColorSetA(SK_ColorRED, 0x3d);
  const SkColor small_ambient_color = SkColorSetA(SK_ColorBLUE, 0x1a);
  const SkColor large_key_color = SkColorSetA(SK_ColorGREEN, 0x41);
  const SkColor large_ambient_color = SkColorSetA(SK_ColorYELLOW, 0x26);
  Shadow::ElevationToColorsMap color_map;
  color_map[kElevationSmall] =
      std::make_pair(small_key_color, small_ambient_color);
  color_map[kElevationLarge] =
      std::make_pair(large_key_color, large_ambient_color);
  shadow.SetElevationToColorsMap(color_map);

  // A lambda to get key and ambient shadow colors.
  auto get_colors =
      [](const ui::Shadow& shadow) -> std::pair<SkColor, SkColor> {
    const auto& values = shadow.details_for_testing()->values;
    return std::make_pair(values[0].color(), values[1].color());
  };

  // Check if shadow colors are updated.
  EXPECT_THAT(get_colors(shadow),
              FieldsAre(small_key_color, small_ambient_color));

  // Check if shadow colors are updated when the shadow changes to another
  // specified elevation.
  shadow.SetElevation(kElevationLarge);
  EXPECT_THAT(get_colors(shadow),
              FieldsAre(large_key_color, large_ambient_color));

  // Check if the shadow colors change back to default colors when the shadow
  // changes to a non-specified elevation.
  shadow.SetElevation(kElevationSmall + 1);
  EXPECT_THAT(get_colors(shadow),
              FieldsAre(default_key_color, default_ambient_color));
}

}  // namespace
}  // namespace ui