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/base/animation/slide_animation.h"
9 #include "ui/base/animation/test_animation_delegate.h"
10 #include "ui/views/view.h"
13 using ui::SlideAnimation
;
14 using ui::TestAnimationDelegate
;
19 class TestBoundsAnimator
: public BoundsAnimator
{
21 explicit TestBoundsAnimator(View
* view
) : BoundsAnimator(view
) {
25 SlideAnimation
* CreateAnimation() {
26 SlideAnimation
* animation
= BoundsAnimator::CreateAnimation();
27 animation
->SetSlideDuration(10);
32 DISALLOW_COPY_AND_ASSIGN(TestBoundsAnimator
);
35 class OwnedDelegate
: public BoundsAnimator::OwnedAnimationDelegate
{
39 virtual ~OwnedDelegate() {
43 static bool GetAndClearDeleted() {
44 bool value
= deleted_
;
49 static bool GetAndClearCanceled() {
50 bool value
= canceled_
;
55 // Overridden from ui::AnimationDelegate:
56 virtual void AnimationCanceled(const Animation
* animation
) OVERRIDE
{
62 static bool canceled_
;
64 DISALLOW_COPY_AND_ASSIGN(OwnedDelegate
);
68 bool OwnedDelegate::deleted_
= false;
69 bool OwnedDelegate::canceled_
= false;
71 class TestView
: public View
{
75 virtual void SchedulePaintInRect(const gfx::Rect
& r
) OVERRIDE
{
76 if (dirty_rect_
.IsEmpty())
79 dirty_rect_
= dirty_rect_
.Union(r
);
82 const gfx::Rect
& dirty_rect() const { return dirty_rect_
; }
85 gfx::Rect dirty_rect_
;
87 DISALLOW_COPY_AND_ASSIGN(TestView
);
92 class BoundsAnimatorTest
: public testing::Test
{
94 BoundsAnimatorTest() : child_(new TestView()), animator_(&parent_
) {
95 parent_
.AddChildView(child_
);
98 TestView
* parent() { return &parent_
; }
99 TestView
* child() { return child_
; }
100 TestBoundsAnimator
* animator() { return &animator_
; }
103 MessageLoopForUI message_loop_
;
105 TestView
* child_
; // Owned by |parent_|.
106 TestBoundsAnimator animator_
;
108 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorTest
);
111 // Checks animate view to.
112 TEST_F(BoundsAnimatorTest
, AnimateViewTo
) {
113 TestAnimationDelegate delegate
;
114 gfx::Rect
initial_bounds(0, 0, 10, 10);
115 child()->SetBoundsRect(initial_bounds
);
116 gfx::Rect
target_bounds(10, 10, 20, 20);
117 animator()->AnimateViewTo(child(), target_bounds
);
118 animator()->SetAnimationDelegate(child(), &delegate
, false);
120 // The animator should be animating now.
121 EXPECT_TRUE(animator()->IsAnimating());
123 // Run the message loop; the delegate exits the loop when the animation is
125 MessageLoop::current()->Run();
127 // Make sure the bounds match of the view that was animated match.
128 EXPECT_EQ(target_bounds
, child()->bounds());
130 // The parent should have been told to repaint as the animation progressed.
131 // The resulting rect is the union of the original and target bounds.
132 EXPECT_EQ(target_bounds
.Union(initial_bounds
), parent()->dirty_rect());
135 // Make sure an AnimationDelegate is deleted when canceled.
136 TEST_F(BoundsAnimatorTest
, DeleteDelegateOnCancel
) {
137 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
138 animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
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(child(), new OwnedDelegate(), true);
156 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
158 // Starting a new animation should both cancel the delegate and delete it.
159 EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
160 EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
163 // Makes sure StopAnimating works.
164 TEST_F(BoundsAnimatorTest
, StopAnimating
) {
165 scoped_ptr
<OwnedDelegate
> delegate(new OwnedDelegate());
167 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
168 animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
170 animator()->StopAnimatingView(child());
172 // Shouldn't be animating now.
173 EXPECT_FALSE(animator()->IsAnimating());
175 // Stopping should both cancel the delegate and delete it.
176 EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
177 EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());