| 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 #include "ui/views/animation/ink_drop_hover.h" | 5 #include "ui/views/animation/ink_drop_hover.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/time/time.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 13 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
| 13 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
| 15 #include "ui/views/animation/test/ink_drop_hover_test_api.h" |
| 16 #include "ui/views/animation/test/test_ink_drop_hover_observer.h" |
| 14 | 17 |
| 15 namespace views { | 18 namespace views { |
| 16 namespace test { | 19 namespace test { |
| 17 | 20 |
| 18 class InkDropHoverTest : public testing::Test { | 21 class InkDropHoverTest : public testing::Test { |
| 19 public: | 22 public: |
| 20 InkDropHoverTest(); | 23 InkDropHoverTest(); |
| 21 ~InkDropHoverTest() override; | 24 ~InkDropHoverTest() override; |
| 22 | 25 |
| 23 protected: | 26 protected: |
| 24 std::unique_ptr<InkDropHover> CreateInkDropHover() const; | 27 // The test target. |
| 28 std::unique_ptr<InkDropHover> ink_drop_hover_; |
| 29 |
| 30 // Allows privileged access to the the |ink_drop_hover_|. |
| 31 InkDropHoverTestApi test_api_; |
| 32 |
| 33 // Observer of the test target. |
| 34 TestInkDropHoverObserver observer_; |
| 25 | 35 |
| 26 private: | 36 private: |
| 27 // Enables zero duration animations during the tests. | |
| 28 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_; | |
| 29 | |
| 30 DISALLOW_COPY_AND_ASSIGN(InkDropHoverTest); | 37 DISALLOW_COPY_AND_ASSIGN(InkDropHoverTest); |
| 31 }; | 38 }; |
| 32 | 39 |
| 33 InkDropHoverTest::InkDropHoverTest() { | 40 InkDropHoverTest::InkDropHoverTest() |
| 34 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( | 41 : ink_drop_hover_( |
| 35 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); | 42 new InkDropHover(gfx::Size(10, 10), 3, gfx::Point(), SK_ColorBLACK)), |
| 43 test_api_(ink_drop_hover_.get()) { |
| 44 ink_drop_hover_->set_observer(&observer_); |
| 45 |
| 46 test_api_.SetDisableAnimationTimers(true); |
| 36 } | 47 } |
| 37 | 48 |
| 38 InkDropHoverTest::~InkDropHoverTest() {} | 49 InkDropHoverTest::~InkDropHoverTest() {} |
| 39 | 50 |
| 40 std::unique_ptr<InkDropHover> InkDropHoverTest::CreateInkDropHover() const { | |
| 41 return base::WrapUnique( | |
| 42 new InkDropHover(gfx::Size(10, 10), 3, gfx::Point(), SK_ColorBLACK)); | |
| 43 } | |
| 44 | |
| 45 TEST_F(InkDropHoverTest, InitialStateAfterConstruction) { | 51 TEST_F(InkDropHoverTest, InitialStateAfterConstruction) { |
| 46 std::unique_ptr<InkDropHover> ink_drop_hover = CreateInkDropHover(); | 52 EXPECT_FALSE(ink_drop_hover_->IsFadingInOrVisible()); |
| 47 EXPECT_FALSE(ink_drop_hover->IsFadingInOrVisible()); | |
| 48 } | 53 } |
| 49 | 54 |
| 50 TEST_F(InkDropHoverTest, IsHoveredStateTransitions) { | 55 TEST_F(InkDropHoverTest, IsHoveredStateTransitions) { |
| 51 std::unique_ptr<InkDropHover> ink_drop_hover = CreateInkDropHover(); | 56 ink_drop_hover_->FadeIn(base::TimeDelta::FromSeconds(1)); |
| 57 EXPECT_TRUE(ink_drop_hover_->IsFadingInOrVisible()); |
| 52 | 58 |
| 53 ink_drop_hover->FadeIn(base::TimeDelta::FromMilliseconds(0)); | 59 test_api_.CompleteAnimations(); |
| 54 EXPECT_TRUE(ink_drop_hover->IsFadingInOrVisible()); | 60 EXPECT_TRUE(ink_drop_hover_->IsFadingInOrVisible()); |
| 55 | 61 |
| 56 ink_drop_hover->FadeOut(base::TimeDelta::FromMilliseconds(0), | 62 ink_drop_hover_->FadeOut(base::TimeDelta::FromSeconds(1), |
| 57 false /* explode */); | 63 false /* explode */); |
| 58 EXPECT_FALSE(ink_drop_hover->IsFadingInOrVisible()); | 64 EXPECT_FALSE(ink_drop_hover_->IsFadingInOrVisible()); |
| 65 |
| 66 test_api_.CompleteAnimations(); |
| 67 EXPECT_FALSE(ink_drop_hover_->IsFadingInOrVisible()); |
| 68 } |
| 69 |
| 70 TEST_F(InkDropHoverTest, VerifyObserversAreNotified) { |
| 71 ink_drop_hover_->FadeIn(base::TimeDelta::FromSeconds(1)); |
| 72 |
| 73 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); |
| 74 EXPECT_FALSE(observer_.AnimationHasEnded()); |
| 75 |
| 76 test_api_.CompleteAnimations(); |
| 77 |
| 78 EXPECT_TRUE(observer_.AnimationHasEnded()); |
| 79 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); |
| 80 } |
| 81 |
| 82 TEST_F(InkDropHoverTest, VerifyObserversAreNotifiedWithCorrectAnimationType) { |
| 83 ink_drop_hover_->FadeIn(base::TimeDelta::FromSeconds(1)); |
| 84 |
| 85 EXPECT_TRUE(observer_.AnimationHasStarted()); |
| 86 EXPECT_EQ(InkDropHover::FADE_IN, observer_.last_animation_started_context()); |
| 87 |
| 88 test_api_.CompleteAnimations(); |
| 89 EXPECT_TRUE(observer_.AnimationHasEnded()); |
| 90 EXPECT_EQ(InkDropHover::FADE_IN, observer_.last_animation_started_context()); |
| 91 |
| 92 ink_drop_hover_->FadeOut(base::TimeDelta::FromSeconds(1), |
| 93 false /* explode */); |
| 94 EXPECT_EQ(InkDropHover::FADE_OUT, observer_.last_animation_started_context()); |
| 95 |
| 96 test_api_.CompleteAnimations(); |
| 97 EXPECT_EQ(InkDropHover::FADE_OUT, observer_.last_animation_started_context()); |
| 98 } |
| 99 |
| 100 TEST_F(InkDropHoverTest, VerifyObserversAreNotifiedOfSuccessfulAnimations) { |
| 101 ink_drop_hover_->FadeIn(base::TimeDelta::FromSeconds(1)); |
| 102 test_api_.CompleteAnimations(); |
| 103 |
| 104 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); |
| 105 EXPECT_EQ(InkDropAnimationEndedReason::SUCCESS, |
| 106 observer_.last_animation_ended_reason()); |
| 107 } |
| 108 |
| 109 TEST_F(InkDropHoverTest, VerifyObserversAreNotifiedOfPreemptedAnimations) { |
| 110 ink_drop_hover_->FadeIn(base::TimeDelta::FromSeconds(1)); |
| 111 ink_drop_hover_->FadeOut(base::TimeDelta::FromSeconds(1), |
| 112 false /* explode */); |
| 113 |
| 114 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); |
| 115 EXPECT_EQ(InkDropHover::FADE_IN, observer_.last_animation_ended_context()); |
| 116 EXPECT_EQ(InkDropAnimationEndedReason::PRE_EMPTED, |
| 117 observer_.last_animation_ended_reason()); |
| 118 } |
| 119 |
| 120 // Confirms there is no crash. |
| 121 TEST_F(InkDropHoverTest, NullObserverIsSafe) { |
| 122 ink_drop_hover_->set_observer(nullptr); |
| 123 |
| 124 ink_drop_hover_->FadeIn(base::TimeDelta::FromSeconds(1)); |
| 125 test_api_.CompleteAnimations(); |
| 126 |
| 127 ink_drop_hover_->FadeOut(base::TimeDelta::FromMilliseconds(0), |
| 128 false /* explode */); |
| 129 test_api_.CompleteAnimations(); |
| 130 EXPECT_FALSE(ink_drop_hover_->IsFadingInOrVisible()); |
| 131 } |
| 132 |
| 133 // Verify animations are aborted during deletion and the InkDropHoverObservers |
| 134 // are notified. |
| 135 TEST_F(InkDropHoverTest, AnimationsAbortedDuringDeletion) { |
| 136 ink_drop_hover_->FadeIn(base::TimeDelta::FromSeconds(1)); |
| 137 ink_drop_hover_.reset(); |
| 138 EXPECT_EQ(1, observer_.last_animation_started_ordinal()); |
| 139 EXPECT_EQ(2, observer_.last_animation_ended_ordinal()); |
| 140 EXPECT_EQ(InkDropHover::FADE_IN, observer_.last_animation_ended_context()); |
| 141 EXPECT_EQ(InkDropAnimationEndedReason::PRE_EMPTED, |
| 142 observer_.last_animation_ended_reason()); |
| 59 } | 143 } |
| 60 | 144 |
| 61 } // namespace test | 145 } // namespace test |
| 62 } // namespace views | 146 } // namespace views |
| OLD | NEW |