1 // Copyright (c) 2012 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/views/animation/bounds_animator.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/animation/slide_animation.h"
9 #include "ui/gfx/animation/test_animation_delegate.h"
10 #include "ui/views/view.h"
13 using gfx::SlideAnimation
;
14 using gfx::TestAnimationDelegate
;
19 class TestBoundsAnimator
: public BoundsAnimator
{
21 explicit TestBoundsAnimator(View
* view
) : BoundsAnimator(view
) {
25 SlideAnimation
* CreateAnimation() override
{
26 SlideAnimation
* animation
= BoundsAnimator::CreateAnimation();
27 animation
->SetSlideDuration(10);
32 DISALLOW_COPY_AND_ASSIGN(TestBoundsAnimator
);
35 class OwnedDelegate
: public gfx::AnimationDelegate
{
39 ~OwnedDelegate() override
{ deleted_
= true; }
41 static bool GetAndClearDeleted() {
42 bool value
= deleted_
;
47 static bool GetAndClearCanceled() {
48 bool value
= canceled_
;
53 // Overridden from gfx::AnimationDelegate:
54 void AnimationCanceled(const Animation
* animation
) override
{
60 static bool canceled_
;
62 DISALLOW_COPY_AND_ASSIGN(OwnedDelegate
);
66 bool OwnedDelegate::deleted_
= false;
67 bool OwnedDelegate::canceled_
= false;
69 class TestView
: public View
{
73 void SchedulePaintInRect(const gfx::Rect
& r
) override
{
74 if (dirty_rect_
.IsEmpty())
80 const gfx::Rect
& dirty_rect() const { return dirty_rect_
; }
83 gfx::Rect dirty_rect_
;
85 DISALLOW_COPY_AND_ASSIGN(TestView
);
90 class BoundsAnimatorTest
: public testing::Test
{
92 BoundsAnimatorTest() : child_(new TestView()), animator_(&parent_
) {
93 parent_
.AddChildView(child_
);
96 TestView
* parent() { return &parent_
; }
97 TestView
* child() { return child_
; }
98 TestBoundsAnimator
* animator() { return &animator_
; }
101 base::MessageLoopForUI message_loop_
;
103 TestView
* child_
; // Owned by |parent_|.
104 TestBoundsAnimator animator_
;
106 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorTest
);
109 // Checks animate view to.
110 TEST_F(BoundsAnimatorTest
, AnimateViewTo
) {
111 gfx::Rect
initial_bounds(0, 0, 10, 10);
112 child()->SetBoundsRect(initial_bounds
);
113 gfx::Rect
target_bounds(10, 10, 20, 20);
114 animator()->AnimateViewTo(child(), target_bounds
);
115 animator()->SetAnimationDelegate(
116 child(), scoped_ptr
<gfx::AnimationDelegate
>(new TestAnimationDelegate()));
118 // The animator should be animating now.
119 EXPECT_TRUE(animator()->IsAnimating());
121 // Run the message loop; the delegate exits the loop when the animation is
123 base::MessageLoop::current()->Run();
125 // Make sure the bounds match of the view that was animated match.
126 EXPECT_EQ(target_bounds
, child()->bounds());
128 // The parent should have been told to repaint as the animation progressed.
129 // The resulting rect is the union of the original and target bounds.
130 EXPECT_EQ(gfx::UnionRects(target_bounds
, initial_bounds
),
131 parent()->dirty_rect());
134 // Make sure an AnimationDelegate is deleted when canceled.
135 TEST_F(BoundsAnimatorTest
, DeleteDelegateOnCancel
) {
136 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
137 animator()->SetAnimationDelegate(
138 child(), scoped_ptr
<gfx::AnimationDelegate
>(new OwnedDelegate()));
140 animator()->Cancel();
142 // The animator should no longer be animating.
143 EXPECT_FALSE(animator()->IsAnimating());
145 // The cancel should both cancel the delegate and delete it.
146 EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
147 EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
150 // Make sure an AnimationDelegate is deleted when another animation is
152 TEST_F(BoundsAnimatorTest
, DeleteDelegateOnNewAnimate
) {
153 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
154 animator()->SetAnimationDelegate(
155 child(), scoped_ptr
<gfx::AnimationDelegate
>(new OwnedDelegate()));
157 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
159 // Starting a new animation should both cancel the delegate and delete it.
160 EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
161 EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
164 // Makes sure StopAnimating works.
165 TEST_F(BoundsAnimatorTest
, StopAnimating
) {
166 scoped_ptr
<OwnedDelegate
> delegate(new OwnedDelegate());
168 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
169 animator()->SetAnimationDelegate(
170 child(), scoped_ptr
<gfx::AnimationDelegate
>(new OwnedDelegate()));
172 animator()->StopAnimatingView(child());
174 // Shouldn't be animating now.
175 EXPECT_FALSE(animator()->IsAnimating());
177 // Stopping should both cancel the delegate and delete it.
178 EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
179 EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());