OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_TEST_TEST_ANIMATION_OBSERVER_H_ |
| 6 #define UI_VIEWS_ANIMATION_TEST_TEST_ANIMATION_OBSERVER_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/views/animation/animation_observer.h" |
| 13 |
| 14 namespace views { |
| 15 namespace test { |
| 16 |
| 17 // Test double implementation for AnimationObserver<ContextType> observers that |
| 18 // tracks the last observations of the AnimationStarted() and AnimationEnded() |
| 19 // methods. |
| 20 // |
| 21 // TestAnimationObserver inherits from the ParentType template parameter where |
| 22 // the ParentType should have AnimationObserver<ContextType> as an ancestor |
| 23 // type. |
| 24 template <typename ParentType, typename ContextType> |
| 25 class TestAnimationObserver : public ParentType { |
| 26 public: |
| 27 TestAnimationObserver() |
| 28 : last_animation_started_ordinal_(-1), |
| 29 last_animation_started_context_(), |
| 30 last_animation_ended_ordinal_(-1), |
| 31 last_animation_ended_context_(), |
| 32 last_animation_ended_reason_(ParentType::SUCCESS) {} |
| 33 |
| 34 virtual ~TestAnimationObserver() {} |
| 35 |
| 36 int last_animation_started_ordinal() const { |
| 37 return last_animation_started_ordinal_; |
| 38 } |
| 39 |
| 40 ContextType last_animation_started_context() const { |
| 41 return last_animation_started_context_; |
| 42 } |
| 43 |
| 44 int last_animation_ended_ordinal() const { |
| 45 return last_animation_ended_ordinal_; |
| 46 } |
| 47 |
| 48 ContextType last_animation_ended_context() const { |
| 49 return last_animation_ended_context_; |
| 50 } |
| 51 |
| 52 typename ParentType::AnimationEndedReason last_animation_ended_reason() |
| 53 const { |
| 54 return last_animation_ended_reason_; |
| 55 } |
| 56 |
| 57 // AnimationObserver<ContextType> overrides: |
| 58 |
| 59 void AnimationStarted(ContextType context) override { |
| 60 last_animation_started_context_ = context; |
| 61 last_animation_started_ordinal_ = GetNextOrdinal(); |
| 62 } |
| 63 |
| 64 void AnimationEnded( |
| 65 ContextType context, |
| 66 typename ParentType::AnimationEndedReason reason) override { |
| 67 last_animation_ended_context_ = context; |
| 68 last_animation_ended_ordinal_ = GetNextOrdinal(); |
| 69 last_animation_ended_reason_ = reason; |
| 70 } |
| 71 |
| 72 // |
| 73 // Collection of assertion predicates to be used with GTest test assertions. |
| 74 // i.e. EXPECT_TRUE/EXPECT_FALSE and the ASSERT_ counterparts. |
| 75 // |
| 76 // Example: |
| 77 // |
| 78 // TestInkDropAnimationObserver observer; |
| 79 // observer.set_ink_drop_animation(ink_drop_animation); |
| 80 // EXPECT_TRUE(observer.AnimationHasNotStarted()); |
| 81 // |
| 82 |
| 83 // Passes *_TRUE assertions when an InkDropAnimationStarted() event has been |
| 84 // observed. |
| 85 testing::AssertionResult AnimationHasStarted() { |
| 86 if (last_animation_started_ordinal() > 0) { |
| 87 return testing::AssertionSuccess() |
| 88 << "Animations were started at ordinal=" |
| 89 << last_animation_started_ordinal() << "."; |
| 90 } |
| 91 return testing::AssertionFailure() << "Animations have not started."; |
| 92 } |
| 93 |
| 94 // Passes *_TRUE assertions when an InkDropAnimationStarted() event has NOT |
| 95 // been observed. |
| 96 testing::AssertionResult AnimationHasNotStarted() { |
| 97 if (last_animation_started_ordinal() < 0) |
| 98 return testing::AssertionSuccess(); |
| 99 return testing::AssertionFailure() << "Animations were started at ordinal=" |
| 100 << last_animation_started_ordinal() |
| 101 << "."; |
| 102 } |
| 103 |
| 104 // Passes *_TRUE assertions when an InkDropAnimationEnded() event has been |
| 105 // observed. |
| 106 testing::AssertionResult AnimationHasEnded() { |
| 107 if (last_animation_ended_ordinal() > 0) { |
| 108 return testing::AssertionSuccess() << "Animations were ended at ordinal=" |
| 109 << last_animation_ended_ordinal() |
| 110 << "."; |
| 111 } |
| 112 return testing::AssertionFailure() << "Animations have not ended."; |
| 113 } |
| 114 |
| 115 // Passes *_TRUE assertions when an InkDropAnimationEnded() event has NOT been |
| 116 // observed. |
| 117 testing::AssertionResult AnimationHasNotEnded() { |
| 118 if (last_animation_ended_ordinal() < 0) |
| 119 return testing::AssertionSuccess(); |
| 120 return testing::AssertionFailure() << "Animations were ended at ordinal=" |
| 121 << last_animation_ended_ordinal() << "."; |
| 122 } |
| 123 |
| 124 private: |
| 125 // Returns the next event ordinal. The first returned ordinal will be 1. |
| 126 int GetNextOrdinal() const { |
| 127 return std::max(1, std::max(last_animation_started_ordinal_, |
| 128 last_animation_ended_ordinal_) + |
| 129 1); |
| 130 } |
| 131 |
| 132 // The ordinal time of the last AnimationStarted() call. |
| 133 int last_animation_started_ordinal_; |
| 134 |
| 135 // The |context| passed to the last call to AnimationStarted(). |
| 136 ContextType last_animation_started_context_; |
| 137 |
| 138 // The ordinal time of the last AnimationEnded() call. |
| 139 int last_animation_ended_ordinal_; |
| 140 |
| 141 // The |context| passed to the last call to AnimationEnded(). |
| 142 ContextType last_animation_ended_context_; |
| 143 |
| 144 typename ParentType::AnimationEndedReason last_animation_ended_reason_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(TestAnimationObserver); |
| 147 }; |
| 148 |
| 149 } // namespace test |
| 150 } // namespace views |
| 151 |
| 152 #endif // UI_VIEWS_ANIMATION_TEST_TEST_ANIMATION_OBSERVER_H_ |
OLD | NEW |