Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / ui / views / animation / bounds_animator_unittest.cc
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.
4
5 #include "ui/views/animation/bounds_animator.h"
6
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"
11
12 using gfx::Animation;
13 using gfx::SlideAnimation;
14 using gfx::TestAnimationDelegate;
15
16 namespace views {
17 namespace {
18
19 class TestBoundsAnimator : public BoundsAnimator {
20  public:
21   explicit TestBoundsAnimator(View* view) : BoundsAnimator(view) {
22   }
23
24  protected:
25   SlideAnimation* CreateAnimation() override {
26     SlideAnimation* animation = BoundsAnimator::CreateAnimation();
27     animation->SetSlideDuration(10);
28     return animation;
29   }
30
31  private:
32   DISALLOW_COPY_AND_ASSIGN(TestBoundsAnimator);
33 };
34
35 class OwnedDelegate : public gfx::AnimationDelegate {
36  public:
37   OwnedDelegate() {}
38
39   ~OwnedDelegate() override { deleted_ = true; }
40
41   static bool GetAndClearDeleted() {
42     bool value = deleted_;
43     deleted_ = false;
44     return value;
45   }
46
47   static bool GetAndClearCanceled() {
48     bool value = canceled_;
49     canceled_ = false;
50     return value;
51   }
52
53   // Overridden from gfx::AnimationDelegate:
54   void AnimationCanceled(const Animation* animation) override {
55     canceled_ = true;
56   }
57
58  private:
59   static bool deleted_;
60   static bool canceled_;
61
62   DISALLOW_COPY_AND_ASSIGN(OwnedDelegate);
63 };
64
65 // static
66 bool OwnedDelegate::deleted_ = false;
67 bool OwnedDelegate::canceled_ = false;
68
69 class TestView : public View {
70  public:
71   TestView() {}
72
73   void SchedulePaintInRect(const gfx::Rect& r) override {
74     if (dirty_rect_.IsEmpty())
75       dirty_rect_ = r;
76     else
77       dirty_rect_.Union(r);
78   }
79
80   const gfx::Rect& dirty_rect() const { return dirty_rect_; }
81
82  private:
83   gfx::Rect dirty_rect_;
84
85   DISALLOW_COPY_AND_ASSIGN(TestView);
86 };
87
88 }  // namespace
89
90 class BoundsAnimatorTest : public testing::Test {
91  public:
92   BoundsAnimatorTest() : child_(new TestView()), animator_(&parent_) {
93     parent_.AddChildView(child_);
94   }
95
96   TestView* parent() { return &parent_; }
97   TestView* child() { return child_; }
98   TestBoundsAnimator* animator() { return &animator_; }
99
100  private:
101   base::MessageLoopForUI message_loop_;
102   TestView parent_;
103   TestView* child_;  // Owned by |parent_|.
104   TestBoundsAnimator animator_;
105
106   DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorTest);
107 };
108
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()));
117
118   // The animator should be animating now.
119   EXPECT_TRUE(animator()->IsAnimating());
120
121   // Run the message loop; the delegate exits the loop when the animation is
122   // done.
123   base::MessageLoop::current()->Run();
124
125   // Make sure the bounds match of the view that was animated match.
126   EXPECT_EQ(target_bounds, child()->bounds());
127
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());
132 }
133
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()));
139
140   animator()->Cancel();
141
142   // The animator should no longer be animating.
143   EXPECT_FALSE(animator()->IsAnimating());
144
145   // The cancel should both cancel the delegate and delete it.
146   EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
147   EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
148 }
149
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()));
156
157   animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
158
159   // Starting a new animation should both cancel the delegate and delete it.
160   EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
161   EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
162 }
163
164 // Makes sure StopAnimating works.
165 TEST_F(BoundsAnimatorTest, StopAnimating) {
166   scoped_ptr<OwnedDelegate> delegate(new OwnedDelegate());
167
168   animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
169   animator()->SetAnimationDelegate(
170       child(), scoped_ptr<gfx::AnimationDelegate>(new OwnedDelegate()));
171
172   animator()->StopAnimatingView(child());
173
174   // Shouldn't be animating now.
175   EXPECT_FALSE(animator()->IsAnimating());
176
177   // Stopping should both cancel the delegate and delete it.
178   EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
179   EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
180 }
181
182 }  // namespace views