OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ | 5 #ifndef UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ |
6 #define UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ | 6 #define UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "ui/gfx/geometry/size.h" | 11 #include "ui/gfx/geometry/size.h" |
12 #include "ui/gfx/geometry/size_f.h" | 12 #include "ui/gfx/geometry/size_f.h" |
13 #include "ui/views/animation/ink_drop_animation.h" | 13 #include "ui/views/animation/ink_drop_animation.h" |
| 14 #include "ui/views/animation/ink_drop_animation_observer.h" |
14 #include "ui/views/animation/ink_drop_state.h" | 15 #include "ui/views/animation/ink_drop_state.h" |
15 #include "ui/views/animation/test/ink_drop_animation_test_api.h" | 16 #include "ui/views/animation/test/ink_drop_animation_test_api.h" |
16 | 17 |
17 namespace views { | 18 namespace views { |
18 namespace test { | 19 namespace test { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Transforms a copy of |point| with |transform| and returns it. | 23 // Transforms a copy of |point| with |transform| and returns it. |
23 gfx::Point TransformPoint(const gfx::Transform& transform, | 24 gfx::Point TransformPoint(const gfx::Transform& transform, |
24 const gfx::Point& point) { | 25 const gfx::Point& point) { |
25 gfx::Point transformed_point = point; | 26 gfx::Point transformed_point = point; |
26 transform.TransformPoint(&transformed_point); | 27 transform.TransformPoint(&transformed_point); |
27 return transformed_point; | 28 return transformed_point; |
28 } | 29 } |
29 | 30 |
| 31 class TestInkDropAnimationObserver : public InkDropAnimationObserver { |
| 32 public: |
| 33 TestInkDropAnimationObserver(); |
| 34 ~TestInkDropAnimationObserver() override; |
| 35 |
| 36 // Resets all cached observation data. |
| 37 void ResetObservations(); |
| 38 |
| 39 bool animation_started() const { return animation_started_; } |
| 40 |
| 41 bool animation_ended() const { return animation_ended_; } |
| 42 |
| 43 InkDropState last_animation_state_started() const { |
| 44 return last_animation_state_started_; |
| 45 } |
| 46 |
| 47 InkDropState last_animation_state_ended() const { |
| 48 return last_animation_state_ended_; |
| 49 } |
| 50 |
| 51 InkDropAnimationEndedReason last_animation_ended_reason() const { |
| 52 return last_animation_ended_reason_; |
| 53 } |
| 54 |
| 55 // InkDropAnimation: |
| 56 void InkDropAnimationStarted(InkDropState ink_drop_state) override; |
| 57 void InkDropAnimationEnded(InkDropState ink_drop_state, |
| 58 InkDropAnimationEndedReason reason) override; |
| 59 |
| 60 private: |
| 61 // True if InkDropAnimationStarted() has been invoked. |
| 62 bool animation_started_; |
| 63 |
| 64 // True if InkDropAnimationEnded() has been invoked. |
| 65 bool animation_ended_; |
| 66 |
| 67 // The |ink_drop_state| parameter used for the last invocation of |
| 68 // InkDropAnimationStarted(). Only valid if |animation_started_| is true. |
| 69 InkDropState last_animation_state_started_; |
| 70 |
| 71 // The |ink_drop_state| parameter used for the last invocation of |
| 72 // InkDropAnimationEnded(). Only valid if |animation_ended_| is true. |
| 73 InkDropState last_animation_state_ended_; |
| 74 |
| 75 // The |reason| parameter used for the last invocation of |
| 76 // InkDropAnimationEnded(). Only valid if |animation_ended_| is true. |
| 77 InkDropAnimationEndedReason last_animation_ended_reason_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(TestInkDropAnimationObserver); |
| 80 }; |
| 81 |
| 82 TestInkDropAnimationObserver::TestInkDropAnimationObserver() |
| 83 : animation_started_(false), |
| 84 animation_ended_(false), |
| 85 last_animation_state_started_(InkDropState::HIDDEN), |
| 86 last_animation_state_ended_(InkDropState::HIDDEN), |
| 87 last_animation_ended_reason_(InkDropAnimationEndedReason::SUCCESS) { |
| 88 ResetObservations(); |
| 89 } |
| 90 |
| 91 TestInkDropAnimationObserver::~TestInkDropAnimationObserver() {} |
| 92 |
| 93 void TestInkDropAnimationObserver::ResetObservations() { |
| 94 animation_started_ = false; |
| 95 animation_ended_ = false; |
| 96 last_animation_state_ended_ = InkDropState::HIDDEN; |
| 97 last_animation_state_started_ = InkDropState::HIDDEN; |
| 98 last_animation_ended_reason_ = InkDropAnimationEndedReason::SUCCESS; |
| 99 } |
| 100 |
| 101 void TestInkDropAnimationObserver::InkDropAnimationStarted( |
| 102 InkDropState ink_drop_state) { |
| 103 animation_started_ = true; |
| 104 last_animation_state_started_ = ink_drop_state; |
| 105 } |
| 106 |
| 107 void TestInkDropAnimationObserver::InkDropAnimationEnded( |
| 108 InkDropState ink_drop_state, |
| 109 InkDropAnimationEndedReason reason) { |
| 110 animation_ended_ = true; |
| 111 last_animation_state_ended_ = ink_drop_state; |
| 112 last_animation_ended_reason_ = reason; |
| 113 } |
| 114 |
30 } // namespace | 115 } // namespace |
31 | 116 |
32 class InkDropAnimationTest : public testing::Test { | 117 class InkDropAnimationTest : public testing::Test { |
33 public: | 118 public: |
34 InkDropAnimationTest() {} | 119 InkDropAnimationTest(); |
35 ~InkDropAnimationTest() override {} | 120 ~InkDropAnimationTest() override; |
36 | 121 |
37 protected: | 122 protected: |
38 scoped_ptr<InkDropAnimation> CreateInkDropAnimation() const; | 123 TestInkDropAnimationObserver observer_; |
| 124 |
| 125 InkDropAnimation ink_drop_animation_; |
| 126 |
| 127 InkDropAnimationTestApi test_api_; |
39 | 128 |
40 private: | 129 private: |
41 DISALLOW_COPY_AND_ASSIGN(InkDropAnimationTest); | 130 DISALLOW_COPY_AND_ASSIGN(InkDropAnimationTest); |
42 }; | 131 }; |
43 | 132 |
44 // Returns a new InkDropAnimation with default parameters. | 133 InkDropAnimationTest::InkDropAnimationTest() |
45 scoped_ptr<InkDropAnimation> InkDropAnimationTest::CreateInkDropAnimation() | 134 : ink_drop_animation_(gfx::Size(10, 10), 2, gfx::Size(8, 8), 1), |
46 const { | 135 test_api_(&ink_drop_animation_) { |
47 return make_scoped_ptr( | 136 ink_drop_animation_.AddObserver(&observer_); |
48 new InkDropAnimation(gfx::Size(10, 10), 2, gfx::Size(8, 8), 1)); | 137 test_api_.SetDisableAnimationTimers(true); |
49 } | 138 } |
| 139 |
| 140 InkDropAnimationTest::~InkDropAnimationTest() {} |
50 | 141 |
51 TEST_F(InkDropAnimationTest, InitialStateAfterConstruction) { | 142 TEST_F(InkDropAnimationTest, InitialStateAfterConstruction) { |
52 scoped_ptr<InkDropAnimation> ink_drop_animation = CreateInkDropAnimation(); | 143 EXPECT_EQ(views::InkDropState::HIDDEN, ink_drop_animation_.ink_drop_state()); |
53 EXPECT_EQ(views::InkDropState::HIDDEN, ink_drop_animation->ink_drop_state()); | 144 } |
| 145 |
| 146 TEST_F(InkDropAnimationTest, VerifyObserversAreNotified) { |
| 147 ink_drop_animation_.AnimateToState(InkDropState::ACTION_PENDING); |
| 148 |
| 149 ASSERT_TRUE(test_api_.HasActiveAnimations()); |
| 150 EXPECT_TRUE(observer_.animation_started()); |
| 151 EXPECT_EQ(InkDropState::ACTION_PENDING, |
| 152 observer_.last_animation_state_started()); |
| 153 EXPECT_FALSE(observer_.animation_ended()); |
| 154 |
| 155 observer_.ResetObservations(); |
| 156 test_api_.CompleteAnimations(); |
| 157 |
| 158 ASSERT_FALSE(test_api_.HasActiveAnimations()); |
| 159 EXPECT_FALSE(observer_.animation_started()); |
| 160 EXPECT_TRUE(observer_.animation_ended()); |
| 161 EXPECT_EQ(InkDropState::ACTION_PENDING, |
| 162 observer_.last_animation_state_ended()); |
| 163 } |
| 164 |
| 165 TEST_F(InkDropAnimationTest, VerifyObserversAreNotifiedOfSuccessfulAnimations) { |
| 166 ink_drop_animation_.AnimateToState(InkDropState::ACTION_PENDING); |
| 167 test_api_.CompleteAnimations(); |
| 168 |
| 169 ASSERT_TRUE(observer_.animation_ended()); |
| 170 EXPECT_EQ(InkDropAnimationObserver::InkDropAnimationEndedReason::SUCCESS, |
| 171 observer_.last_animation_ended_reason()); |
| 172 } |
| 173 |
| 174 TEST_F(InkDropAnimationTest, VerifyObserversAreNotifiedOfPreemptedAnimations) { |
| 175 ink_drop_animation_.AnimateToState(InkDropState::ACTION_PENDING); |
| 176 observer_.ResetObservations(); |
| 177 |
| 178 ink_drop_animation_.AnimateToState(InkDropState::SLOW_ACTION_PENDING); |
| 179 |
| 180 ASSERT_TRUE(observer_.animation_ended()); |
| 181 EXPECT_EQ(InkDropAnimationObserver::InkDropAnimationEndedReason::PRE_EMPTED, |
| 182 observer_.last_animation_ended_reason()); |
| 183 } |
| 184 |
| 185 TEST_F(InkDropAnimationTest, AnimateToHiddenFromInvisibleState) { |
| 186 ASSERT_EQ(InkDropState::HIDDEN, ink_drop_animation_.ink_drop_state()); |
| 187 |
| 188 ink_drop_animation_.AnimateToState(InkDropState::HIDDEN); |
| 189 EXPECT_TRUE(observer_.animation_started()); |
| 190 EXPECT_TRUE(observer_.animation_ended()); |
| 191 } |
| 192 |
| 193 TEST_F(InkDropAnimationTest, AnimateToHiddenFromVisibleState) { |
| 194 ink_drop_animation_.AnimateToState(InkDropState::ACTION_PENDING); |
| 195 test_api_.CompleteAnimations(); |
| 196 |
| 197 observer_.ResetObservations(); |
| 198 |
| 199 ASSERT_NE(InkDropState::HIDDEN, ink_drop_animation_.ink_drop_state()); |
| 200 |
| 201 ink_drop_animation_.AnimateToState(InkDropState::HIDDEN); |
| 202 |
| 203 EXPECT_TRUE(observer_.animation_started()); |
| 204 EXPECT_FALSE(observer_.animation_ended()); |
| 205 |
| 206 test_api_.CompleteAnimations(); |
| 207 |
| 208 EXPECT_TRUE(observer_.animation_started()); |
| 209 EXPECT_TRUE(observer_.animation_ended()); |
54 } | 210 } |
55 | 211 |
56 TEST_F(InkDropAnimationTest, AnimateToActionPending) { | 212 TEST_F(InkDropAnimationTest, AnimateToActionPending) { |
57 scoped_ptr<InkDropAnimation> ink_drop_animation = CreateInkDropAnimation(); | 213 ink_drop_animation_.AnimateToState(views::InkDropState::ACTION_PENDING); |
58 ink_drop_animation->AnimateToState(views::InkDropState::ACTION_PENDING); | 214 test_api_.CompleteAnimations(); |
| 215 |
59 EXPECT_EQ(views::InkDropState::ACTION_PENDING, | 216 EXPECT_EQ(views::InkDropState::ACTION_PENDING, |
60 ink_drop_animation->ink_drop_state()); | 217 ink_drop_animation_.ink_drop_state()); |
| 218 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_.GetCurrentOpacity()); |
61 } | 219 } |
62 | 220 |
63 TEST_F(InkDropAnimationTest, AnimateToQuickAction) { | 221 TEST_F(InkDropAnimationTest, AnimateToQuickAction) { |
64 scoped_ptr<InkDropAnimation> ink_drop_animation = CreateInkDropAnimation(); | 222 ink_drop_animation_.AnimateToState(views::InkDropState::ACTION_PENDING); |
65 ink_drop_animation->AnimateToState(views::InkDropState::QUICK_ACTION); | 223 ink_drop_animation_.AnimateToState(views::InkDropState::QUICK_ACTION); |
| 224 test_api_.CompleteAnimations(); |
| 225 |
66 EXPECT_EQ(views::InkDropState::QUICK_ACTION, | 226 EXPECT_EQ(views::InkDropState::QUICK_ACTION, |
67 ink_drop_animation->ink_drop_state()); | 227 ink_drop_animation_.ink_drop_state()); |
| 228 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_.GetCurrentOpacity()); |
68 } | 229 } |
69 | 230 |
70 TEST_F(InkDropAnimationTest, AnimateToSlowActionPending) { | 231 TEST_F(InkDropAnimationTest, AnimateToSlowActionPending) { |
71 scoped_ptr<InkDropAnimation> ink_drop_animation = CreateInkDropAnimation(); | 232 ink_drop_animation_.AnimateToState(views::InkDropState::ACTION_PENDING); |
72 ink_drop_animation->AnimateToState(views::InkDropState::SLOW_ACTION_PENDING); | 233 ink_drop_animation_.AnimateToState(views::InkDropState::SLOW_ACTION_PENDING); |
| 234 test_api_.CompleteAnimations(); |
| 235 |
73 EXPECT_EQ(views::InkDropState::SLOW_ACTION_PENDING, | 236 EXPECT_EQ(views::InkDropState::SLOW_ACTION_PENDING, |
74 ink_drop_animation->ink_drop_state()); | 237 ink_drop_animation_.ink_drop_state()); |
| 238 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_.GetCurrentOpacity()); |
75 } | 239 } |
76 | 240 |
77 TEST_F(InkDropAnimationTest, AnimateToSlowAction) { | 241 TEST_F(InkDropAnimationTest, AnimateToSlowAction) { |
78 scoped_ptr<InkDropAnimation> ink_drop_animation = CreateInkDropAnimation(); | 242 ink_drop_animation_.AnimateToState(views::InkDropState::ACTION_PENDING); |
79 ink_drop_animation->AnimateToState(views::InkDropState::SLOW_ACTION); | 243 ink_drop_animation_.AnimateToState(views::InkDropState::SLOW_ACTION_PENDING); |
| 244 ink_drop_animation_.AnimateToState(views::InkDropState::SLOW_ACTION); |
| 245 test_api_.CompleteAnimations(); |
| 246 |
80 EXPECT_EQ(views::InkDropState::SLOW_ACTION, | 247 EXPECT_EQ(views::InkDropState::SLOW_ACTION, |
81 ink_drop_animation->ink_drop_state()); | 248 ink_drop_animation_.ink_drop_state()); |
| 249 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_.GetCurrentOpacity()); |
82 } | 250 } |
83 | 251 |
84 TEST_F(InkDropAnimationTest, AnimateToActivated) { | 252 TEST_F(InkDropAnimationTest, AnimateToActivated) { |
85 scoped_ptr<InkDropAnimation> ink_drop_animation = CreateInkDropAnimation(); | 253 ink_drop_animation_.AnimateToState(views::InkDropState::ACTIVATED); |
86 ink_drop_animation->AnimateToState(views::InkDropState::ACTIVATED); | 254 test_api_.CompleteAnimations(); |
| 255 |
87 EXPECT_EQ(views::InkDropState::ACTIVATED, | 256 EXPECT_EQ(views::InkDropState::ACTIVATED, |
88 ink_drop_animation->ink_drop_state()); | 257 ink_drop_animation_.ink_drop_state()); |
| 258 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_.GetCurrentOpacity()); |
89 } | 259 } |
90 | 260 |
91 TEST_F(InkDropAnimationTest, AnimateToDeactivated) { | 261 TEST_F(InkDropAnimationTest, AnimateToDeactivated) { |
92 scoped_ptr<InkDropAnimation> ink_drop_animation = CreateInkDropAnimation(); | 262 ink_drop_animation_.AnimateToState(views::InkDropState::ACTIVATED); |
93 ink_drop_animation->AnimateToState(views::InkDropState::DEACTIVATED); | 263 ink_drop_animation_.AnimateToState(views::InkDropState::DEACTIVATED); |
| 264 test_api_.CompleteAnimations(); |
| 265 |
94 EXPECT_EQ(views::InkDropState::DEACTIVATED, | 266 EXPECT_EQ(views::InkDropState::DEACTIVATED, |
95 ink_drop_animation->ink_drop_state()); | 267 ink_drop_animation_.ink_drop_state()); |
96 } | 268 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_.GetCurrentOpacity()); |
97 | 269 } |
98 TEST_F(InkDropAnimationTest, | 270 |
99 TransformedPointsUsingTransformsFromCalculateCircleTransforms) { | 271 // Verifies all active animations are aborted and the InkDropState is set to |
| 272 // HIDDEN after invoking HideImmediately(). |
| 273 TEST_F(InkDropAnimationTest, HideImmediately) { |
| 274 ink_drop_animation_.AnimateToState(views::InkDropState::ACTION_PENDING); |
| 275 ASSERT_TRUE(test_api_.HasActiveAnimations()); |
| 276 ASSERT_NE(InkDropState::HIDDEN, ink_drop_animation_.ink_drop_state()); |
| 277 ASSERT_TRUE(observer_.animation_started()); |
| 278 |
| 279 observer_.ResetObservations(); |
| 280 ink_drop_animation_.HideImmediately(); |
| 281 |
| 282 EXPECT_FALSE(test_api_.HasActiveAnimations()); |
| 283 EXPECT_EQ(views::InkDropState::HIDDEN, ink_drop_animation_.ink_drop_state()); |
| 284 ASSERT_FALSE(observer_.animation_started()); |
| 285 } |
| 286 |
| 287 TEST(InkDropAnimationTransformTest, |
| 288 TransformedPointsUsingTransformsFromCalculateCircleTransforms) { |
100 const int kHalfDrawnSize = 5; | 289 const int kHalfDrawnSize = 5; |
101 const int kDrawnSize = 2 * kHalfDrawnSize; | 290 const int kDrawnSize = 2 * kHalfDrawnSize; |
102 | 291 |
103 const int kHalfTransformedSize = 100; | 292 const int kHalfTransformedSize = 100; |
104 const int kTransformedSize = 2 * kHalfTransformedSize; | 293 const int kTransformedSize = 2 * kHalfTransformedSize; |
105 | 294 |
106 // Constant points in the drawn space that will be transformed. | 295 // Constant points in the drawn space that will be transformed. |
107 const gfx::Point center(kHalfDrawnSize, kHalfDrawnSize); | 296 const gfx::Point center(kHalfDrawnSize, kHalfDrawnSize); |
108 const gfx::Point mid_left(0, kHalfDrawnSize); | 297 const gfx::Point mid_left(0, kHalfDrawnSize); |
109 const gfx::Point mid_right(kDrawnSize, kHalfDrawnSize); | 298 const gfx::Point mid_right(kDrawnSize, kHalfDrawnSize); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 // Vertical rectangle | 378 // Vertical rectangle |
190 EXPECT_EQ(gfx::Point(0, 0), TransformPoint(kVerticalTransform, center)); | 379 EXPECT_EQ(gfx::Point(0, 0), TransformPoint(kVerticalTransform, center)); |
191 EXPECT_EQ(gfx::Point(0, 0), TransformPoint(kVerticalTransform, mid_left)); | 380 EXPECT_EQ(gfx::Point(0, 0), TransformPoint(kVerticalTransform, mid_left)); |
192 EXPECT_EQ(gfx::Point(0, 0), TransformPoint(kVerticalTransform, mid_right)); | 381 EXPECT_EQ(gfx::Point(0, 0), TransformPoint(kVerticalTransform, mid_right)); |
193 EXPECT_EQ(gfx::Point(0, -kHalfTransformedSize), | 382 EXPECT_EQ(gfx::Point(0, -kHalfTransformedSize), |
194 TransformPoint(kVerticalTransform, top_mid)); | 383 TransformPoint(kVerticalTransform, top_mid)); |
195 EXPECT_EQ(gfx::Point(0, kHalfTransformedSize), | 384 EXPECT_EQ(gfx::Point(0, kHalfTransformedSize), |
196 TransformPoint(kVerticalTransform, bottom_mid)); | 385 TransformPoint(kVerticalTransform, bottom_mid)); |
197 } | 386 } |
198 | 387 |
199 TEST_F(InkDropAnimationTest, | 388 TEST(InkDropAnimationTransformTest, |
200 TransformedPointsUsingTransformsFromCalculateRectTransforms) { | 389 TransformedPointsUsingTransformsFromCalculateRectTransforms) { |
201 const int kHalfDrawnSize = 5; | 390 const int kHalfDrawnSize = 5; |
202 const int kDrawnSize = 2 * kHalfDrawnSize; | 391 const int kDrawnSize = 2 * kHalfDrawnSize; |
203 | 392 |
204 const int kTransformedRadius = 10; | 393 const int kTransformedRadius = 10; |
205 | 394 |
206 const int kHalfTransformedWidth = 100; | 395 const int kHalfTransformedWidth = 100; |
207 const int kTransformedWidth = 2 * kHalfTransformedWidth; | 396 const int kTransformedWidth = 2 * kHalfTransformedWidth; |
208 | 397 |
209 const int kHalfTransformedHeight = 100; | 398 const int kHalfTransformedHeight = 100; |
210 const int kTransformedHeight = 2 * kHalfTransformedHeight; | 399 const int kTransformedHeight = 2 * kHalfTransformedHeight; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 EXPECT_EQ(gfx::Point(0, -kHalfTransformedHeight), | 484 EXPECT_EQ(gfx::Point(0, -kHalfTransformedHeight), |
296 TransformPoint(kVerticalTransform, top_mid)); | 485 TransformPoint(kVerticalTransform, top_mid)); |
297 EXPECT_EQ(gfx::Point(0, kHalfTransformedHeight), | 486 EXPECT_EQ(gfx::Point(0, kHalfTransformedHeight), |
298 TransformPoint(kVerticalTransform, bottom_mid)); | 487 TransformPoint(kVerticalTransform, bottom_mid)); |
299 } | 488 } |
300 | 489 |
301 } // namespace test | 490 } // namespace test |
302 } // namespace views | 491 } // namespace views |
303 | 492 |
304 #endif // UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ | 493 #endif // UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ |
OLD | NEW |