Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / animation / bounds_animator_unittest.cc
blob27f2b58bc8bf6340b958ed955212c40bd651d08b
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"
12 using gfx::Animation;
13 using gfx::SlideAnimation;
14 using gfx::TestAnimationDelegate;
16 namespace views {
17 namespace {
19 class TestBoundsAnimator : public BoundsAnimator {
20 public:
21 explicit TestBoundsAnimator(View* view) : BoundsAnimator(view) {
24 protected:
25 SlideAnimation* CreateAnimation() override {
26 SlideAnimation* animation = BoundsAnimator::CreateAnimation();
27 animation->SetSlideDuration(10);
28 return animation;
31 private:
32 DISALLOW_COPY_AND_ASSIGN(TestBoundsAnimator);
35 class OwnedDelegate : public gfx::AnimationDelegate {
36 public:
37 OwnedDelegate() {}
39 ~OwnedDelegate() override { deleted_ = true; }
41 static bool GetAndClearDeleted() {
42 bool value = deleted_;
43 deleted_ = false;
44 return value;
47 static bool GetAndClearCanceled() {
48 bool value = canceled_;
49 canceled_ = false;
50 return value;
53 // Overridden from gfx::AnimationDelegate:
54 void AnimationCanceled(const Animation* animation) override {
55 canceled_ = true;
58 private:
59 static bool deleted_;
60 static bool canceled_;
62 DISALLOW_COPY_AND_ASSIGN(OwnedDelegate);
65 // static
66 bool OwnedDelegate::deleted_ = false;
67 bool OwnedDelegate::canceled_ = false;
69 class TestView : public View {
70 public:
71 TestView() {}
73 void SchedulePaintInRect(const gfx::Rect& r) override {
74 if (dirty_rect_.IsEmpty())
75 dirty_rect_ = r;
76 else
77 dirty_rect_.Union(r);
80 const gfx::Rect& dirty_rect() const { return dirty_rect_; }
82 private:
83 gfx::Rect dirty_rect_;
85 DISALLOW_COPY_AND_ASSIGN(TestView);
88 } // namespace
90 class BoundsAnimatorTest : public testing::Test {
91 public:
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_; }
100 private:
101 base::MessageLoopForUI message_loop_;
102 TestView parent_;
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
122 // done.
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
151 // scheduled.
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());
182 } // namespace views