| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ | |
| 6 #define UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ | |
| 7 | |
| 8 #include "ui/views/animation/ink_drop_animation.h" | |
| 9 | |
| 10 #include <memory> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "ui/gfx/geometry/size.h" | |
| 15 #include "ui/views/animation/flood_fill_ink_drop_animation.h" | |
| 16 #include "ui/views/animation/ink_drop_animation_observer.h" | |
| 17 #include "ui/views/animation/ink_drop_state.h" | |
| 18 #include "ui/views/animation/square_ink_drop_animation.h" | |
| 19 #include "ui/views/animation/test/flood_fill_ink_drop_animation_test_api.h" | |
| 20 #include "ui/views/animation/test/ink_drop_animation_test_api.h" | |
| 21 #include "ui/views/animation/test/square_ink_drop_animation_test_api.h" | |
| 22 #include "ui/views/animation/test/test_ink_drop_animation_observer.h" | |
| 23 | |
| 24 namespace views { | |
| 25 namespace test { | |
| 26 | |
| 27 // Represents all the derivatives of the InkDropAnimation class. To be used with | |
| 28 // the InkDropAnimationTest fixture to test all derviatives. | |
| 29 enum InkDropAnimationTestTypes { | |
| 30 SQUARE_INK_DROP_ANIMATION, | |
| 31 FLOOD_FILL_INK_DROP_ANIMATION | |
| 32 }; | |
| 33 | |
| 34 // Test fixture for all InkDropAnimation class derivatives. | |
| 35 // | |
| 36 // To add a new derivative: | |
| 37 // 1. Add a value to the InkDropAnimationTestTypes enum. | |
| 38 // 2. Implement set up and tear down code for the new enum value in | |
| 39 // InkDropAnimationTest() and | |
| 40 // ~InkDropAnimationTest(). | |
| 41 // 3. Add the new enum value to the INSTANTIATE_TEST_CASE_P) Values list. | |
| 42 class InkDropAnimationTest | |
| 43 : public testing::TestWithParam<InkDropAnimationTestTypes> { | |
| 44 public: | |
| 45 InkDropAnimationTest(); | |
| 46 ~InkDropAnimationTest() override; | |
| 47 | |
| 48 protected: | |
| 49 TestInkDropAnimationObserver observer_; | |
| 50 | |
| 51 std::unique_ptr<InkDropAnimation> ink_drop_animation_; | |
| 52 | |
| 53 std::unique_ptr<InkDropAnimationTestApi> test_api_; | |
| 54 | |
| 55 private: | |
| 56 DISALLOW_COPY_AND_ASSIGN(InkDropAnimationTest); | |
| 57 }; | |
| 58 | |
| 59 InkDropAnimationTest::InkDropAnimationTest() { | |
| 60 switch (GetParam()) { | |
| 61 case SQUARE_INK_DROP_ANIMATION: { | |
| 62 SquareInkDropAnimation* square_ink_drop_animation = | |
| 63 new SquareInkDropAnimation(gfx::Size(10, 10), 2, gfx::Size(8, 8), 1, | |
| 64 gfx::Point(), SK_ColorBLACK); | |
| 65 ink_drop_animation_.reset(square_ink_drop_animation); | |
| 66 test_api_.reset( | |
| 67 new SquareInkDropAnimationTestApi(square_ink_drop_animation)); | |
| 68 break; | |
| 69 } | |
| 70 case FLOOD_FILL_INK_DROP_ANIMATION: { | |
| 71 FloodFillInkDropAnimation* flood_fill_ink_drop_animation = | |
| 72 new FloodFillInkDropAnimation(gfx::Rect(0, 0, 10, 10), gfx::Point(), | |
| 73 SK_ColorBLACK); | |
| 74 ink_drop_animation_.reset(flood_fill_ink_drop_animation); | |
| 75 test_api_.reset( | |
| 76 new FloodFillInkDropAnimationTestApi(flood_fill_ink_drop_animation)); | |
| 77 break; | |
| 78 } | |
| 79 } | |
| 80 ink_drop_animation_->set_observer(&observer_); | |
| 81 observer_.set_ink_drop_animation(ink_drop_animation_.get()); | |
| 82 test_api_->SetDisableAnimationTimers(true); | |
| 83 } | |
| 84 | |
| 85 InkDropAnimationTest::~InkDropAnimationTest() {} | |
| 86 | |
| 87 // Note: First argument is optional and intentionally left blank. | |
| 88 // (it's a prefix for the generated test cases) | |
| 89 INSTANTIATE_TEST_CASE_P(, | |
| 90 InkDropAnimationTest, | |
| 91 testing::Values(SQUARE_INK_DROP_ANIMATION, | |
| 92 FLOOD_FILL_INK_DROP_ANIMATION)); | |
| 93 | |
| 94 TEST_P(InkDropAnimationTest, InitialStateAfterConstruction) { | |
| 95 EXPECT_EQ(views::InkDropState::HIDDEN, | |
| 96 ink_drop_animation_->target_ink_drop_state()); | |
| 97 } | |
| 98 | |
| 99 // Verify no animations are used when animating from HIDDEN to HIDDEN. | |
| 100 TEST_P(InkDropAnimationTest, AnimateToHiddenFromInvisibleState) { | |
| 101 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_animation_->target_ink_drop_state()); | |
| 102 | |
| 103 ink_drop_animation_->AnimateToState(InkDropState::HIDDEN); | |
| 104 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 105 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 106 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_->GetCurrentOpacity()); | |
| 107 EXPECT_FALSE(ink_drop_animation_->IsVisible()); | |
| 108 } | |
| 109 | |
| 110 TEST_P(InkDropAnimationTest, AnimateToHiddenFromVisibleState) { | |
| 111 ink_drop_animation_->AnimateToState(InkDropState::ACTION_PENDING); | |
| 112 test_api_->CompleteAnimations(); | |
| 113 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 114 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 115 | |
| 116 EXPECT_NE(InkDropState::HIDDEN, ink_drop_animation_->target_ink_drop_state()); | |
| 117 | |
| 118 ink_drop_animation_->AnimateToState(InkDropState::HIDDEN); | |
| 119 test_api_->CompleteAnimations(); | |
| 120 | |
| 121 EXPECT_EQ(3, observer_.last_animation_started_ordinal()); | |
| 122 EXPECT_EQ(4, observer_.last_animation_ended_ordinal()); | |
| 123 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_->GetCurrentOpacity()); | |
| 124 } | |
| 125 | |
| 126 TEST_P(InkDropAnimationTest, ActionPendingOpacity) { | |
| 127 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 128 test_api_->CompleteAnimations(); | |
| 129 | |
| 130 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_->GetCurrentOpacity()); | |
| 131 } | |
| 132 | |
| 133 TEST_P(InkDropAnimationTest, QuickActionOpacity) { | |
| 134 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 135 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_TRIGGERED); | |
| 136 test_api_->CompleteAnimations(); | |
| 137 | |
| 138 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_->GetCurrentOpacity()); | |
| 139 } | |
| 140 | |
| 141 TEST_P(InkDropAnimationTest, SlowActionPendingOpacity) { | |
| 142 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 143 ink_drop_animation_->AnimateToState( | |
| 144 views::InkDropState::ALTERNATE_ACTION_PENDING); | |
| 145 test_api_->CompleteAnimations(); | |
| 146 | |
| 147 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_->GetCurrentOpacity()); | |
| 148 } | |
| 149 | |
| 150 TEST_P(InkDropAnimationTest, SlowActionOpacity) { | |
| 151 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 152 ink_drop_animation_->AnimateToState( | |
| 153 views::InkDropState::ALTERNATE_ACTION_PENDING); | |
| 154 ink_drop_animation_->AnimateToState( | |
| 155 views::InkDropState::ALTERNATE_ACTION_TRIGGERED); | |
| 156 test_api_->CompleteAnimations(); | |
| 157 | |
| 158 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_->GetCurrentOpacity()); | |
| 159 } | |
| 160 | |
| 161 TEST_P(InkDropAnimationTest, ActivatedOpacity) { | |
| 162 ink_drop_animation_->AnimateToState(views::InkDropState::ACTIVATED); | |
| 163 test_api_->CompleteAnimations(); | |
| 164 | |
| 165 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_->GetCurrentOpacity()); | |
| 166 } | |
| 167 | |
| 168 TEST_P(InkDropAnimationTest, DeactivatedOpacity) { | |
| 169 ink_drop_animation_->AnimateToState(views::InkDropState::ACTIVATED); | |
| 170 ink_drop_animation_->AnimateToState(views::InkDropState::DEACTIVATED); | |
| 171 test_api_->CompleteAnimations(); | |
| 172 | |
| 173 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_->GetCurrentOpacity()); | |
| 174 } | |
| 175 | |
| 176 // Verify animations are aborted during deletion and the | |
| 177 // InkDropAnimationObservers are notified. | |
| 178 TEST_P(InkDropAnimationTest, AnimationsAbortedDuringDeletion) { | |
| 179 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 180 ink_drop_animation_.reset(); | |
| 181 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 182 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 183 EXPECT_EQ(views::InkDropState::ACTION_PENDING, | |
| 184 observer_.last_animation_ended_context()); | |
| 185 EXPECT_EQ(InkDropAnimationEndedReason::PRE_EMPTED, | |
| 186 observer_.last_animation_ended_reason()); | |
| 187 } | |
| 188 | |
| 189 TEST_P(InkDropAnimationTest, VerifyObserversAreNotified) { | |
| 190 ink_drop_animation_->AnimateToState(InkDropState::ACTION_PENDING); | |
| 191 | |
| 192 EXPECT_TRUE(test_api_->HasActiveAnimations()); | |
| 193 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 194 EXPECT_TRUE(observer_.AnimationHasNotEnded()); | |
| 195 EXPECT_EQ(InkDropState::ACTION_PENDING, | |
| 196 observer_.last_animation_started_context()); | |
| 197 | |
| 198 test_api_->CompleteAnimations(); | |
| 199 | |
| 200 EXPECT_FALSE(test_api_->HasActiveAnimations()); | |
| 201 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 202 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 203 EXPECT_EQ(InkDropState::ACTION_PENDING, | |
| 204 observer_.last_animation_ended_context()); | |
| 205 } | |
| 206 | |
| 207 TEST_P(InkDropAnimationTest, VerifyObserversAreNotifiedOfSuccessfulAnimations) { | |
| 208 ink_drop_animation_->AnimateToState(InkDropState::ACTION_PENDING); | |
| 209 test_api_->CompleteAnimations(); | |
| 210 | |
| 211 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 212 EXPECT_EQ(InkDropAnimationEndedReason::SUCCESS, | |
| 213 observer_.last_animation_ended_reason()); | |
| 214 } | |
| 215 | |
| 216 TEST_P(InkDropAnimationTest, VerifyObserversAreNotifiedOfPreemptedAnimations) { | |
| 217 ink_drop_animation_->AnimateToState(InkDropState::ACTION_PENDING); | |
| 218 ink_drop_animation_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING); | |
| 219 | |
| 220 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 221 EXPECT_EQ(InkDropAnimationEndedReason::PRE_EMPTED, | |
| 222 observer_.last_animation_ended_reason()); | |
| 223 } | |
| 224 | |
| 225 TEST_P(InkDropAnimationTest, InkDropStatesPersistWhenCallingAnimateToState) { | |
| 226 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 227 ink_drop_animation_->AnimateToState(views::InkDropState::ACTIVATED); | |
| 228 EXPECT_EQ(views::InkDropState::ACTIVATED, | |
| 229 ink_drop_animation_->target_ink_drop_state()); | |
| 230 } | |
| 231 | |
| 232 TEST_P(InkDropAnimationTest, HideImmediatelyWithoutActiveAnimations) { | |
| 233 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 234 test_api_->CompleteAnimations(); | |
| 235 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 236 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 237 | |
| 238 EXPECT_FALSE(test_api_->HasActiveAnimations()); | |
| 239 EXPECT_NE(InkDropState::HIDDEN, ink_drop_animation_->target_ink_drop_state()); | |
| 240 | |
| 241 ink_drop_animation_->HideImmediately(); | |
| 242 | |
| 243 EXPECT_FALSE(test_api_->HasActiveAnimations()); | |
| 244 EXPECT_EQ(views::InkDropState::HIDDEN, | |
| 245 ink_drop_animation_->target_ink_drop_state()); | |
| 246 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 247 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 248 | |
| 249 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_->GetCurrentOpacity()); | |
| 250 EXPECT_FALSE(ink_drop_animation_->IsVisible()); | |
| 251 } | |
| 252 | |
| 253 // Verifies all active animations are aborted and the InkDropState is set to | |
| 254 // HIDDEN after invoking HideImmediately(). | |
| 255 TEST_P(InkDropAnimationTest, HideImmediatelyWithActiveAnimations) { | |
| 256 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 257 EXPECT_TRUE(test_api_->HasActiveAnimations()); | |
| 258 EXPECT_NE(InkDropState::HIDDEN, ink_drop_animation_->target_ink_drop_state()); | |
| 259 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 260 | |
| 261 ink_drop_animation_->HideImmediately(); | |
| 262 | |
| 263 EXPECT_FALSE(test_api_->HasActiveAnimations()); | |
| 264 EXPECT_EQ(views::InkDropState::HIDDEN, | |
| 265 ink_drop_animation_->target_ink_drop_state()); | |
| 266 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 267 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 268 EXPECT_EQ(InkDropState::ACTION_PENDING, | |
| 269 observer_.last_animation_ended_context()); | |
| 270 EXPECT_EQ(InkDropAnimationEndedReason::PRE_EMPTED, | |
| 271 observer_.last_animation_ended_reason()); | |
| 272 | |
| 273 EXPECT_EQ(InkDropAnimation::kHiddenOpacity, test_api_->GetCurrentOpacity()); | |
| 274 EXPECT_FALSE(ink_drop_animation_->IsVisible()); | |
| 275 } | |
| 276 | |
| 277 TEST_P(InkDropAnimationTest, SnapToActivatedWithoutActiveAnimations) { | |
| 278 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 279 test_api_->CompleteAnimations(); | |
| 280 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 281 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 282 | |
| 283 EXPECT_FALSE(test_api_->HasActiveAnimations()); | |
| 284 EXPECT_NE(InkDropState::ACTIVATED, | |
| 285 ink_drop_animation_->target_ink_drop_state()); | |
| 286 | |
| 287 ink_drop_animation_->SnapToActivated(); | |
| 288 | |
| 289 EXPECT_FALSE(test_api_->HasActiveAnimations()); | |
| 290 EXPECT_EQ(views::InkDropState::ACTIVATED, | |
| 291 ink_drop_animation_->target_ink_drop_state()); | |
| 292 EXPECT_EQ(3, observer_.last_animation_started_ordinal()); | |
| 293 EXPECT_EQ(4, observer_.last_animation_ended_ordinal()); | |
| 294 | |
| 295 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_->GetCurrentOpacity()); | |
| 296 EXPECT_TRUE(ink_drop_animation_->IsVisible()); | |
| 297 } | |
| 298 | |
| 299 // Verifies all active animations are aborted and the InkDropState is set to | |
| 300 // ACTIVATED after invoking SnapToActivated(). | |
| 301 TEST_P(InkDropAnimationTest, SnapToActivatedWithActiveAnimations) { | |
| 302 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 303 EXPECT_TRUE(test_api_->HasActiveAnimations()); | |
| 304 EXPECT_NE(InkDropState::ACTIVATED, | |
| 305 ink_drop_animation_->target_ink_drop_state()); | |
| 306 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); | |
| 307 | |
| 308 ink_drop_animation_->SnapToActivated(); | |
| 309 | |
| 310 EXPECT_FALSE(test_api_->HasActiveAnimations()); | |
| 311 EXPECT_EQ(views::InkDropState::ACTIVATED, | |
| 312 ink_drop_animation_->target_ink_drop_state()); | |
| 313 EXPECT_EQ(3, observer_.last_animation_started_ordinal()); | |
| 314 EXPECT_EQ(4, observer_.last_animation_ended_ordinal()); | |
| 315 EXPECT_EQ(InkDropState::ACTIVATED, observer_.last_animation_ended_context()); | |
| 316 EXPECT_EQ(InkDropAnimationEndedReason::SUCCESS, | |
| 317 observer_.last_animation_ended_reason()); | |
| 318 | |
| 319 EXPECT_EQ(InkDropAnimation::kVisibleOpacity, test_api_->GetCurrentOpacity()); | |
| 320 EXPECT_TRUE(ink_drop_animation_->IsVisible()); | |
| 321 } | |
| 322 | |
| 323 TEST_P(InkDropAnimationTest, AnimateToVisibleFromHidden) { | |
| 324 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_animation_->target_ink_drop_state()); | |
| 325 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 326 EXPECT_TRUE(ink_drop_animation_->IsVisible()); | |
| 327 } | |
| 328 | |
| 329 // Verifies that the value of InkDropAnimation::target_ink_drop_state() returns | |
| 330 // the most recent value passed to AnimateToState() when notifying observers | |
| 331 // that an animation has started within the AnimateToState() function call. | |
| 332 TEST_P(InkDropAnimationTest, TargetInkDropStateOnAnimationStarted) { | |
| 333 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 334 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); | |
| 335 | |
| 336 EXPECT_EQ(3, observer_.last_animation_started_ordinal()); | |
| 337 EXPECT_EQ(views::InkDropState::HIDDEN, | |
| 338 observer_.target_state_at_last_animation_started()); | |
| 339 } | |
| 340 | |
| 341 // Verifies that the value of InkDropAnimation::target_ink_drop_state() returns | |
| 342 // the most recent value passed to AnimateToState() when notifying observers | |
| 343 // that an animation has ended within the AnimateToState() function call. | |
| 344 TEST_P(InkDropAnimationTest, TargetInkDropStateOnAnimationEnded) { | |
| 345 ink_drop_animation_->AnimateToState(views::InkDropState::ACTION_PENDING); | |
| 346 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); | |
| 347 | |
| 348 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); | |
| 349 EXPECT_EQ(views::InkDropState::HIDDEN, | |
| 350 observer_.target_state_at_last_animation_ended()); | |
| 351 } | |
| 352 | |
| 353 } // namespace test | |
| 354 } // namespace views | |
| 355 | |
| 356 #endif // UI_VIEWS_ANIMATION_INK_DROP_ANIMATION_UNITTEST_H_ | |
| OLD | NEW |