Index: chrome/browser/ui/views/frame/scroll_end_effect_controller_ash_unittest.cc |
diff --git a/chrome/browser/ui/views/frame/scroll_end_effect_controller_ash_unittest.cc b/chrome/browser/ui/views/frame/scroll_end_effect_controller_ash_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..36aacc24fd11ac2bf3ca14144038aebcd6d687a4 |
--- /dev/null |
+++ b/chrome/browser/ui/views/frame/scroll_end_effect_controller_ash_unittest.cc |
@@ -0,0 +1,282 @@ |
+// Copyright (c) 2013 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. |
+ |
+#include "chrome/browser/ui/views/frame/scroll_end_effect_controller_ash.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "chrome/browser/ui/views/frame/contents_container.h" |
+#include "chrome/browser/ui/views/frame/scroll_end_effect_controller_delegate.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/views/controls/webview/webview.h" |
+ |
+using ::testing::Test; |
+ |
+class ContentsContainerStub : public ContentsContainer { |
+ public: |
+ ContentsContainerStub() : ContentsContainer(new views::WebView(NULL)), |
+ times_activate_called_(0), |
+ times_deactivate_called_(0), |
+ times_update_called_(0), |
+ last_update_value_(0), |
+ last_scrolling_direction_(false) { |
+ } |
+ virtual ~ContentsContainerStub() {} |
+ |
+ int times_activate_called() { return times_activate_called_; } |
+ int times_deactivate_called() { return times_deactivate_called_; } |
+ int times_update_called() { return times_update_called_; } |
+ int last_update_value() { return last_update_value_; } |
+ bool last_scrolling_direction() { return last_scrolling_direction_; } |
+ |
+ // ContentsContainer overrides |
+ virtual void ActivateScrollEndEffect() OVERRIDE { |
+ times_activate_called_++; |
+ } |
+ virtual void DeactivateScrollEndEffect() OVERRIDE { |
+ times_deactivate_called_++; |
+ } |
+ virtual void UpdateScrollEndEffectHeightDelta(int height_delta, |
+ bool scrolling_down) OVERRIDE { |
+ times_update_called_++; |
+ last_update_value_ = height_delta; |
+ last_scrolling_direction_ = scrolling_down; |
+ } |
+ |
+ private: |
+ int times_activate_called_; |
+ int times_deactivate_called_; |
+ int times_update_called_; |
+ int last_update_value_; |
+ bool last_scrolling_direction_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ContentsContainerStub); |
+}; |
+ |
+class ScrollEndEffectControllerDelegateStub : |
+ public ScrollEndEffectControllerDelegate { |
+ public: |
+ explicit ScrollEndEffectControllerDelegateStub( |
+ ContentsContainerStub* container) { |
+ container_ = container; |
+ } |
+ virtual ~ScrollEndEffectControllerDelegateStub() {} |
+ |
+ // ScrollEndEffectControllerDelegate overrides |
+ virtual ContentsContainer* GetContentsContainer() OVERRIDE { |
+ return container_; |
+ } |
+ |
+ virtual gfx::Rect GetFrameBounds() OVERRIDE { |
+ return frame_bounds_; |
+ } |
+ |
+ virtual void SetFrameBounds(gfx::Rect bounds) OVERRIDE { |
+ frame_bounds_ = bounds; |
+ } |
+ |
+ private: |
+ ContentsContainerStub* container_; // non-owned |
+ gfx::Rect frame_bounds_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ScrollEndEffectControllerDelegateStub); |
+}; |
+ |
+class ScrollEndEffectControllerAshTest : public Test { |
+ public: |
+ ScrollEndEffectControllerAshTest() : |
+ delegate_(&container_), |
+ controller_(&delegate_) { |
+ controller_.animation_->SetSlideDuration(0); |
+ } |
+ virtual ~ScrollEndEffectControllerAshTest() {} |
+ |
+ ContentsContainerStub* container() { |
+ return &container_; |
+ } |
+ |
+ ScrollEndEffectControllerDelegateStub* delegate() { |
+ return &delegate_; |
+ } |
+ |
+ ScrollEndEffectControllerAsh* controller() { |
+ return &controller_; |
+ } |
+ |
+ gfx::SlideAnimation* animation() { |
+ return controller_.animation_.get(); |
+ } |
+ |
+ void ActivateEffect() { |
+ controller_.ActivateEffect(); |
+ } |
+ |
+ void DeactivateEffect() { |
+ controller_.DeactivateEffect(); |
+ } |
+ |
+ bool IsEffectActive() { |
+ return controller_.is_effect_active_; |
+ } |
+ |
+ void ApplyDelta(int delta_y) { |
+ controller_.ApplyDelta(delta_y); |
+ } |
+ |
+ void SetCurrentDeltaY(int delta_y) { |
+ controller_.current_delta_y_ = delta_y; |
+ } |
+ |
+ void SetStartDeltaY(int delta_y) { |
+ controller_.start_delta_y_ = delta_y; |
+ } |
+ |
+ void SetEndDeltaY(int delta_y) { |
+ controller_.end_delta_y_ = delta_y; |
+ } |
+ |
+ private: |
+ ContentsContainerStub container_; |
+ ScrollEndEffectControllerDelegateStub delegate_; |
+ ScrollEndEffectControllerAsh controller_; |
+}; |
+ |
+TEST_F(ScrollEndEffectControllerAshTest, OverscrollUpdate) { |
+ delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
+ controller()->OverscrollUpdate(0); |
+ EXPECT_FALSE(IsEffectActive()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ |
+ controller()->OverscrollUpdate(5); |
+ EXPECT_TRUE(IsEffectActive()); |
+ EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ |
+ DeactivateEffect(); |
+ // -10 gets clipped to -5 due to |kScrollEndEffectFactor|. |
+ delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
+ controller()->OverscrollUpdate(-10); |
+ EXPECT_TRUE(IsEffectActive()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ |
+ controller()->OverscrollUpdate(0); |
+ EXPECT_FALSE(IsEffectActive()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ |
+ ActivateEffect(); |
+ controller()->OverscrollUpdate(5); |
+ EXPECT_TRUE(IsEffectActive()); |
+ EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ |
+ // -10 gets clipped to -5 due to |kScrollEndEffectFactor|. |
+ controller()->OverscrollUpdate(-10); |
+ EXPECT_TRUE(IsEffectActive()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+} |
+ |
+TEST_F(ScrollEndEffectControllerAshTest, AnimationEnded) { |
+ // If the animation was to 0, then deactivate. |
+ delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
+ ActivateEffect(); |
+ SetCurrentDeltaY(0); |
+ controller()->AnimationEnded(animation()); |
+ EXPECT_FALSE(IsEffectActive()); |
+ EXPECT_EQ(1, container()->times_deactivate_called()); |
+ |
+ // If the animation was not to 0, then don't deactivate. |
+ ActivateEffect(); |
+ SetCurrentDeltaY(10); |
+ controller()->AnimationEnded(animation()); |
+ EXPECT_TRUE(IsEffectActive()); |
+ EXPECT_EQ(1, container()->times_deactivate_called()); |
+} |
+ |
+TEST_F(ScrollEndEffectControllerAshTest, AnimationProgressed) { |
+ delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
+ ActivateEffect(); |
+ SetStartDeltaY(10); |
+ SetEndDeltaY(0); |
+ |
+ controller()->AnimationProgressed(animation()); |
+ EXPECT_EQ(gfx::Rect(0, 10, 100, 90).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(1, container()->times_update_called()); |
+ EXPECT_EQ(10, container()->last_update_value()); |
+ EXPECT_TRUE(container()->last_scrolling_direction()); |
+ |
+ animation()->Reset(0.5); |
+ controller()->AnimationProgressed(animation()); |
+ EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(2, container()->times_update_called()); |
+ EXPECT_EQ(5, container()->last_update_value()); |
+ EXPECT_TRUE(container()->last_scrolling_direction()); |
+ |
+ animation()->Reset(1.0); |
+ controller()->AnimationProgressed(animation()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(3, container()->times_update_called()); |
+ EXPECT_EQ(0, container()->last_update_value()); |
+ |
+ SetStartDeltaY(-10); |
+ |
+ animation()->Reset(0.0); |
+ controller()->AnimationProgressed(animation()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 90).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(4, container()->times_update_called()); |
+ EXPECT_EQ(10, container()->last_update_value()); |
+ EXPECT_FALSE(container()->last_scrolling_direction()); |
+ |
+ animation()->Reset(0.5); |
+ controller()->AnimationProgressed(animation()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(5, container()->times_update_called()); |
+ EXPECT_EQ(5, container()->last_update_value()); |
+ EXPECT_FALSE(container()->last_scrolling_direction()); |
+ |
+ animation()->Reset(1.0); |
+ controller()->AnimationProgressed(animation()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(6, container()->times_update_called()); |
+ EXPECT_EQ(0, container()->last_update_value()); |
+} |
+ |
+TEST_F(ScrollEndEffectControllerAshTest, ActivateEffect) { |
+ ActivateEffect(); |
+ EXPECT_TRUE(IsEffectActive()); |
+ EXPECT_EQ(1, container()->times_activate_called()); |
+} |
+ |
+TEST_F(ScrollEndEffectControllerAshTest, DeactivateEffect) { |
+ DeactivateEffect(); |
+ EXPECT_FALSE(IsEffectActive()); |
+ EXPECT_EQ(1, container()->times_deactivate_called()); |
+} |
+ |
+TEST_F(ScrollEndEffectControllerAshTest, ApplyDelta) { |
+ delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
+ ActivateEffect(); |
+ |
+ ApplyDelta(10); |
+ EXPECT_EQ(gfx::Rect(0, 10, 100, 90).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(1, container()->times_update_called()); |
+ EXPECT_EQ(10, container()->last_update_value()); |
+ EXPECT_TRUE(container()->last_scrolling_direction()); |
+ |
+ ApplyDelta(-10); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 90).ToString(), |
+ delegate()->GetFrameBounds().ToString()); |
+ EXPECT_EQ(2, container()->times_update_called()); |
+ EXPECT_EQ(10, container()->last_update_value()); |
+ EXPECT_FALSE(container()->last_scrolling_direction()); |
+} |