Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Unified Diff: ui/views/animation/test/test_animation_observer.h

Issue 1896953003: Added a views::test::TestInkDropAnimationObserverHelper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added test_animation_observer.h file. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/animation/test/test_animation_observer.h
diff --git a/ui/views/animation/test/test_animation_observer.h b/ui/views/animation/test/test_animation_observer.h
new file mode 100644
index 0000000000000000000000000000000000000000..5a6cacc3b495f0582ba83d50a116b81895bf9427
--- /dev/null
+++ b/ui/views/animation/test/test_animation_observer.h
@@ -0,0 +1,152 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UI_VIEWS_ANIMATION_TEST_TEST_ANIMATION_OBSERVER_H_
+#define UI_VIEWS_ANIMATION_TEST_TEST_ANIMATION_OBSERVER_H_
+
+#include <algorithm>
+
+#include "base/macros.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/views/animation/animation_observer.h"
+
+namespace views {
+namespace test {
+
+// Test double implementation for AnimationObserver<ContextType> observers that
+// tracks the last observations of the AnimationStarted() and AnimationEnded()
+// methods.
+//
+// TestAnimationObserver inherits from the ParentType template parameter where
+// the ParentType should have AnimationObserver<ContextType> as an ancestor
+// type.
+template <typename ParentType, typename ContextType>
+class TestAnimationObserver : public ParentType {
+ public:
+ TestAnimationObserver()
+ : last_animation_started_ordinal_(-1),
+ last_animation_started_context_(),
+ last_animation_ended_ordinal_(-1),
+ last_animation_ended_context_(),
+ last_animation_ended_reason_(ParentType::SUCCESS) {}
+
+ virtual ~TestAnimationObserver() {}
+
+ int last_animation_started_ordinal() const {
+ return last_animation_started_ordinal_;
+ }
+
+ ContextType last_animation_started_context() const {
+ return last_animation_started_context_;
+ }
+
+ int last_animation_ended_ordinal() const {
+ return last_animation_ended_ordinal_;
+ }
+
+ ContextType last_animation_ended_context() const {
+ return last_animation_ended_context_;
+ }
+
+ typename ParentType::AnimationEndedReason last_animation_ended_reason()
+ const {
+ return last_animation_ended_reason_;
+ }
+
+ // AnimationObserver<ContextType> overrides:
+
+ void AnimationStarted(ContextType context) override {
+ last_animation_started_context_ = context;
+ last_animation_started_ordinal_ = GetNextOrdinal();
+ }
+
+ void AnimationEnded(
+ ContextType context,
+ typename ParentType::AnimationEndedReason reason) override {
+ last_animation_ended_context_ = context;
+ last_animation_ended_ordinal_ = GetNextOrdinal();
+ last_animation_ended_reason_ = reason;
+ }
+
+ //
+ // Collection of assertion predicates to be used with GTest test assertions.
+ // i.e. EXPECT_TRUE/EXPECT_FALSE and the ASSERT_ counterparts.
+ //
+ // Example:
+ //
+ // TestInkDropAnimationObserver observer;
+ // observer.set_ink_drop_animation(ink_drop_animation);
+ // EXPECT_TRUE(observer.AnimationHasNotStarted());
+ //
+
+ // Passes *_TRUE assertions when an InkDropAnimationStarted() event has been
+ // observed.
+ testing::AssertionResult AnimationHasStarted() {
+ if (last_animation_started_ordinal() > 0) {
+ return testing::AssertionSuccess()
+ << "Animations were started at ordinal="
+ << last_animation_started_ordinal() << ".";
+ }
+ return testing::AssertionFailure() << "Animations have not started.";
+ }
+
+ // Passes *_TRUE assertions when an InkDropAnimationStarted() event has NOT
+ // been observed.
+ testing::AssertionResult AnimationHasNotStarted() {
+ if (last_animation_started_ordinal() < 0)
+ return testing::AssertionSuccess();
+ return testing::AssertionFailure() << "Animations were started at ordinal="
+ << last_animation_started_ordinal()
+ << ".";
+ }
+
+ // Passes *_TRUE assertions when an InkDropAnimationEnded() event has been
+ // observed.
+ testing::AssertionResult AnimationHasEnded() {
+ if (last_animation_ended_ordinal() > 0) {
+ return testing::AssertionSuccess() << "Animations were ended at ordinal="
+ << last_animation_ended_ordinal()
+ << ".";
+ }
+ return testing::AssertionFailure() << "Animations have not ended.";
+ }
+
+ // Passes *_TRUE assertions when an InkDropAnimationEnded() event has NOT been
+ // observed.
+ testing::AssertionResult AnimationHasNotEnded() {
+ if (last_animation_ended_ordinal() < 0)
+ return testing::AssertionSuccess();
+ return testing::AssertionFailure() << "Animations were ended at ordinal="
+ << last_animation_ended_ordinal() << ".";
+ }
+
+ private:
+ // Returns the next event ordinal. The first returned ordinal will be 1.
+ int GetNextOrdinal() const {
+ return std::max(1, std::max(last_animation_started_ordinal_,
+ last_animation_ended_ordinal_) +
+ 1);
+ }
+
+ // The ordinal time of the last AnimationStarted() call.
+ int last_animation_started_ordinal_;
+
+ // The |context| passed to the last call to AnimationStarted().
+ ContextType last_animation_started_context_;
+
+ // The ordinal time of the last AnimationEnded() call.
+ int last_animation_ended_ordinal_;
+
+ // The |context| passed to the last call to AnimationEnded().
+ ContextType last_animation_ended_context_;
+
+ typename ParentType::AnimationEndedReason last_animation_ended_reason_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestAnimationObserver);
+};
+
+} // namespace test
+} // namespace views
+
+#endif // UI_VIEWS_ANIMATION_TEST_TEST_ANIMATION_OBSERVER_H_

Powered by Google App Engine
This is Rietveld 408576698