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