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.
5 #include "ui/app_list/views/image_shadow_animator.h"
7 #include "ui/views/test/views_test_base.h"
11 bool ShadowsEqual(const gfx::ShadowValue
& a
, const gfx::ShadowValue
& b
) {
12 return a
.blur() == b
.blur() && a
.color() == b
.color() &&
13 a
.offset() == b
.offset();
21 class ImageShadowAnimatorTest
: public views::ViewsTestBase
,
22 public ImageShadowAnimator::Delegate
{
24 ImageShadowAnimatorTest()
25 : shadow_animator_(this), animation_updated_(false) {}
26 ~ImageShadowAnimatorTest() override
{}
28 bool animation_updated() { return animation_updated_
; }
29 void reset_animation_updated() { animation_updated_
= false; }
30 ImageShadowAnimator
* shadow_animator() { return &shadow_animator_
; }
32 // ImageShadowAnimator::Delegate overrides:
33 void ImageShadowAnimationProgressed(ImageShadowAnimator
* animator
) override
{
34 animation_updated_
= true;
37 void UpdateShadowImageForProgress(double progress
) {
38 shadow_animator_
.UpdateShadowImageForProgress(progress
);
41 gfx::ShadowValues
GetShadowValuesForProgress(double progress
) {
42 return shadow_animator_
.GetShadowValuesForProgress(progress
);
46 ImageShadowAnimator shadow_animator_
;
47 bool animation_updated_
;
49 DISALLOW_COPY_AND_ASSIGN(ImageShadowAnimatorTest
);
52 TEST_F(ImageShadowAnimatorTest
, TweenShadow
) {
53 gfx::ShadowValues start
;
54 start
.push_back(gfx::ShadowValue(gfx::Vector2d(0, 1), 2,
55 SkColorSetA(SK_ColorBLACK
, 0xA0)));
56 start
.push_back(gfx::ShadowValue(gfx::Vector2d(0, 2), 4,
57 SkColorSetA(SK_ColorBLACK
, 0x80)));
59 gfx::ShadowValues end
;
60 end
.push_back(gfx::ShadowValue(gfx::Vector2d(-2, 3), 4,
61 SkColorSetA(SK_ColorBLACK
, 0xC0)));
62 end
.push_back(gfx::ShadowValue(gfx::Vector2d(4, 4), 8,
63 SkColorSetA(SK_ColorBLACK
, 0x60)));
65 shadow_animator()->SetStartAndEndShadows(start
, end
);
67 gfx::ShadowValues result_shadows
= GetShadowValuesForProgress(0.5);
69 // Although the blur value of this shadow should be 3, it is snapped to an
70 // even value to prevent offset issues in clients.
71 EXPECT_TRUE(ShadowsEqual(gfx::ShadowValue(gfx::Vector2d(-1, 2), 4,
72 SkColorSetA(SK_ColorBLACK
, 0xB0)),
75 EXPECT_TRUE(ShadowsEqual(gfx::ShadowValue(gfx::Vector2d(2, 3), 6,
76 SkColorSetA(SK_ColorBLACK
, 0x70)),
80 TEST_F(ImageShadowAnimatorTest
, ImageSize
) {
81 gfx::ShadowValues start
;
82 start
.push_back(gfx::ShadowValue(gfx::Vector2d(0, 2), 2, 0xA0000000));
84 gfx::ShadowValues end
;
85 end
.push_back(gfx::ShadowValue(gfx::Vector2d(0, 8), 8, 0x60000000));
87 shadow_animator()->SetStartAndEndShadows(start
, end
);
89 EXPECT_FALSE(animation_updated());
92 bitmap
.allocN32Pixels(8, 8);
93 bitmap
.eraseColor(SK_ColorBLUE
);
94 shadow_animator()->SetOriginalImage(
95 gfx::ImageSkia::CreateFrom1xBitmap(bitmap
));
96 // Setting the image should notify the delegate.
97 EXPECT_TRUE(animation_updated());
99 // Check the shadowed image grows as it animates, due to the increasing blur
100 // and vertical offset.
101 EXPECT_EQ(gfx::Size(10, 11), shadow_animator()->shadow_image().size());
102 UpdateShadowImageForProgress(0.5);
103 EXPECT_EQ(gfx::Size(14, 16), shadow_animator()->shadow_image().size());
104 UpdateShadowImageForProgress(1);
105 EXPECT_EQ(gfx::Size(16, 20), shadow_animator()->shadow_image().size());
109 } // namespace app_list