OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 "testing/gtest/include/gtest/gtest.h" | |
6 #include "ui/base/animation/slide_animation.h" | |
7 #include "ui/base/animation/test_animation_delegate.h" | |
8 #include "views/animation/bounds_animator.h" | |
9 #include "views/view.h" | |
10 | |
11 using views::BoundsAnimator; | |
12 using ui::Animation; | |
13 using ui::SlideAnimation; | |
14 using ui::TestAnimationDelegate; | |
15 | |
16 namespace { | |
17 | |
18 class TestBoundsAnimator : public BoundsAnimator { | |
19 public: | |
20 explicit TestBoundsAnimator(views::View* view) : BoundsAnimator(view) { | |
21 } | |
22 | |
23 protected: | |
24 SlideAnimation* CreateAnimation() { | |
25 SlideAnimation* animation = BoundsAnimator::CreateAnimation(); | |
26 animation->SetSlideDuration(10); | |
27 return animation; | |
28 } | |
29 | |
30 private: | |
31 DISALLOW_COPY_AND_ASSIGN(TestBoundsAnimator); | |
32 }; | |
33 | |
34 class OwnedDelegate : public BoundsAnimator::OwnedAnimationDelegate { | |
35 public: | |
36 OwnedDelegate() { | |
37 deleted_ = false; | |
38 canceled_ = false; | |
39 } | |
40 | |
41 ~OwnedDelegate() { | |
42 deleted_ = true; | |
43 } | |
44 | |
45 static bool get_and_clear_deleted() { | |
46 bool value = deleted_; | |
47 deleted_ = false; | |
48 return value; | |
49 } | |
50 | |
51 static bool get_and_clear_canceled() { | |
52 bool value = canceled_; | |
53 canceled_ = false; | |
54 return value; | |
55 } | |
56 | |
57 // AnimationDelegate: | |
58 virtual void AnimationCanceled(const Animation* animation) { | |
59 canceled_ = true; | |
60 } | |
61 | |
62 private: | |
63 static bool deleted_; | |
64 static bool canceled_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(OwnedDelegate); | |
67 }; | |
68 | |
69 // static | |
70 bool OwnedDelegate::deleted_ = false; | |
71 | |
72 // static | |
73 bool OwnedDelegate::canceled_ = false; | |
74 | |
75 class TestView : public views::View { | |
76 public: | |
77 TestView() {} | |
78 virtual void SchedulePaintInRect(const gfx::Rect& r) { | |
79 if (dirty_rect_.IsEmpty()) | |
80 dirty_rect_ = r; | |
81 else | |
82 dirty_rect_ = dirty_rect_.Union(r); | |
83 } | |
84 | |
85 const gfx::Rect& dirty_rect() const { return dirty_rect_; } | |
86 | |
87 private: | |
88 gfx::Rect dirty_rect_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(TestView); | |
91 }; | |
92 | |
93 } // namespace | |
94 | |
95 class BoundsAnimatorTest : public testing::Test { | |
96 public: | |
97 BoundsAnimatorTest() : child_(new TestView()), animator_(&parent_) { | |
98 parent_.AddChildView(child_); | |
99 } | |
100 | |
101 TestView* parent() { return &parent_; } | |
102 TestView* child() { return child_; } | |
103 TestBoundsAnimator* animator() { return &animator_; } | |
104 | |
105 private: | |
106 MessageLoopForUI message_loop_; | |
107 TestView parent_; | |
108 TestView* child_; // Owned by |parent_|. | |
109 TestBoundsAnimator animator_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorTest); | |
112 }; | |
113 | |
114 // Checks animate view to. | |
115 TEST_F(BoundsAnimatorTest, AnimateViewTo) { | |
116 TestAnimationDelegate delegate; | |
117 gfx::Rect initial_bounds(0, 0, 10, 10); | |
118 child()->SetBoundsRect(initial_bounds); | |
119 gfx::Rect target_bounds(10, 10, 20, 20); | |
120 animator()->AnimateViewTo(child(), target_bounds); | |
121 animator()->SetAnimationDelegate(child(), &delegate, false); | |
122 | |
123 // The animator should be animating now. | |
124 EXPECT_TRUE(animator()->IsAnimating()); | |
125 | |
126 // Run the message loop; the delegate exits the loop when the animation is | |
127 // done. | |
128 MessageLoop::current()->Run(); | |
129 | |
130 // Make sure the bounds match of the view that was animated match. | |
131 EXPECT_EQ(target_bounds, child()->bounds()); | |
132 | |
133 // The parent should have been told to repaint as the animation progressed. | |
134 // The resulting rect is the union of the original and target bounds. | |
135 EXPECT_EQ(target_bounds.Union(initial_bounds), parent()->dirty_rect()); | |
136 } | |
137 | |
138 // Make sure an AnimationDelegate is deleted when canceled. | |
139 TEST_F(BoundsAnimatorTest, DeleteDelegateOnCancel) { | |
140 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10)); | |
141 animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true); | |
142 | |
143 animator()->Cancel(); | |
144 | |
145 // The animator should no longer be animating. | |
146 EXPECT_FALSE(animator()->IsAnimating()); | |
147 | |
148 // The cancel should both cancel the delegate and delete it. | |
149 EXPECT_TRUE(OwnedDelegate::get_and_clear_canceled()); | |
150 EXPECT_TRUE(OwnedDelegate::get_and_clear_deleted()); | |
151 } | |
152 | |
153 // Make sure an AnimationDelegate is deleted when another animation is | |
154 // scheduled. | |
155 TEST_F(BoundsAnimatorTest, DeleteDelegateOnNewAnimate) { | |
156 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10)); | |
157 animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true); | |
158 | |
159 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10)); | |
160 | |
161 // Starting a new animation should both cancel the delegate and delete it. | |
162 EXPECT_TRUE(OwnedDelegate::get_and_clear_deleted()); | |
163 EXPECT_TRUE(OwnedDelegate::get_and_clear_canceled()); | |
164 } | |
165 | |
166 // Makes sure StopAnimating works. | |
167 TEST_F(BoundsAnimatorTest, StopAnimating) { | |
168 scoped_ptr<OwnedDelegate> delegate(new OwnedDelegate()); | |
169 | |
170 animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10)); | |
171 animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true); | |
172 | |
173 animator()->StopAnimatingView(child()); | |
174 | |
175 // Shouldn't be animating now. | |
176 EXPECT_FALSE(animator()->IsAnimating()); | |
177 | |
178 // Stopping should both cancel the delegate and delete it. | |
179 EXPECT_TRUE(OwnedDelegate::get_and_clear_deleted()); | |
180 EXPECT_TRUE(OwnedDelegate::get_and_clear_canceled()); | |
181 } | |
OLD | NEW |