OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include "chrome/browser/ui/views/frame/scroll_end_effect_controller_ash.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/ui/views/frame/contents_container.h" |
| 9 #include "chrome/browser/ui/views/frame/scroll_end_effect_controller_delegate.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/views/controls/webview/webview.h" |
| 12 |
| 13 using ::testing::Test; |
| 14 |
| 15 class ContentsContainerStub : public ContentsContainer { |
| 16 public: |
| 17 ContentsContainerStub() : ContentsContainer(new views::WebView(NULL)), |
| 18 times_activate_called_(0), |
| 19 times_deactivate_called_(0), |
| 20 times_update_called_(0), |
| 21 last_update_value_(0), |
| 22 last_scrolling_direction_(false) { |
| 23 } |
| 24 virtual ~ContentsContainerStub() {} |
| 25 |
| 26 int times_activate_called() { return times_activate_called_; } |
| 27 int times_deactivate_called() { return times_deactivate_called_; } |
| 28 int times_update_called() { return times_update_called_; } |
| 29 int last_update_value() { return last_update_value_; } |
| 30 bool last_scrolling_direction() { return last_scrolling_direction_; } |
| 31 |
| 32 // ContentsContainer overrides |
| 33 virtual void ActivateScrollEndEffect() OVERRIDE { |
| 34 times_activate_called_++; |
| 35 } |
| 36 virtual void DeactivateScrollEndEffect() OVERRIDE { |
| 37 times_deactivate_called_++; |
| 38 } |
| 39 virtual void UpdateScrollEndEffectHeightDelta(int height_delta, |
| 40 bool scrolling_down) OVERRIDE { |
| 41 times_update_called_++; |
| 42 last_update_value_ = height_delta; |
| 43 last_scrolling_direction_ = scrolling_down; |
| 44 } |
| 45 |
| 46 private: |
| 47 int times_activate_called_; |
| 48 int times_deactivate_called_; |
| 49 int times_update_called_; |
| 50 int last_update_value_; |
| 51 bool last_scrolling_direction_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(ContentsContainerStub); |
| 54 }; |
| 55 |
| 56 class ScrollEndEffectControllerDelegateStub : |
| 57 public ScrollEndEffectControllerDelegate { |
| 58 public: |
| 59 explicit ScrollEndEffectControllerDelegateStub( |
| 60 ContentsContainerStub* container) { |
| 61 container_ = container; |
| 62 } |
| 63 virtual ~ScrollEndEffectControllerDelegateStub() {} |
| 64 |
| 65 // ScrollEndEffectControllerDelegate overrides |
| 66 virtual ContentsContainer* GetContentsContainer() OVERRIDE { |
| 67 return container_; |
| 68 } |
| 69 |
| 70 virtual gfx::Rect GetFrameBounds() OVERRIDE { |
| 71 return frame_bounds_; |
| 72 } |
| 73 |
| 74 virtual void SetFrameBounds(gfx::Rect bounds) OVERRIDE { |
| 75 frame_bounds_ = bounds; |
| 76 } |
| 77 |
| 78 private: |
| 79 ContentsContainerStub* container_; // non-owned |
| 80 gfx::Rect frame_bounds_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(ScrollEndEffectControllerDelegateStub); |
| 83 }; |
| 84 |
| 85 class ScrollEndEffectControllerAshTest : public Test { |
| 86 public: |
| 87 ScrollEndEffectControllerAshTest() : |
| 88 delegate_(&container_), |
| 89 controller_(&delegate_) { |
| 90 controller_.animation_->SetSlideDuration(0); |
| 91 } |
| 92 virtual ~ScrollEndEffectControllerAshTest() {} |
| 93 |
| 94 ContentsContainerStub* container() { |
| 95 return &container_; |
| 96 } |
| 97 |
| 98 ScrollEndEffectControllerDelegateStub* delegate() { |
| 99 return &delegate_; |
| 100 } |
| 101 |
| 102 ScrollEndEffectControllerAsh* controller() { |
| 103 return &controller_; |
| 104 } |
| 105 |
| 106 gfx::SlideAnimation* animation() { |
| 107 return controller_.animation_.get(); |
| 108 } |
| 109 |
| 110 void ActivateEffect() { |
| 111 controller_.ActivateEffect(); |
| 112 } |
| 113 |
| 114 void DeactivateEffect() { |
| 115 controller_.DeactivateEffect(); |
| 116 } |
| 117 |
| 118 bool IsEffectActive() { |
| 119 return controller_.is_effect_active_; |
| 120 } |
| 121 |
| 122 void ApplyDelta(int delta_y) { |
| 123 controller_.ApplyDelta(delta_y); |
| 124 } |
| 125 |
| 126 void SetCurrentDeltaY(int delta_y) { |
| 127 controller_.current_delta_y_ = delta_y; |
| 128 } |
| 129 |
| 130 void SetStartDeltaY(int delta_y) { |
| 131 controller_.start_delta_y_ = delta_y; |
| 132 } |
| 133 |
| 134 void SetEndDeltaY(int delta_y) { |
| 135 controller_.end_delta_y_ = delta_y; |
| 136 } |
| 137 |
| 138 private: |
| 139 ContentsContainerStub container_; |
| 140 ScrollEndEffectControllerDelegateStub delegate_; |
| 141 ScrollEndEffectControllerAsh controller_; |
| 142 }; |
| 143 |
| 144 TEST_F(ScrollEndEffectControllerAshTest, OverscrollUpdate) { |
| 145 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
| 146 controller()->OverscrollUpdate(0); |
| 147 EXPECT_FALSE(IsEffectActive()); |
| 148 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 149 delegate()->GetFrameBounds().ToString()); |
| 150 |
| 151 controller()->OverscrollUpdate(5); |
| 152 EXPECT_TRUE(IsEffectActive()); |
| 153 EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), |
| 154 delegate()->GetFrameBounds().ToString()); |
| 155 |
| 156 DeactivateEffect(); |
| 157 // -10 gets clipped to -5 due to |kScrollEndEffectFactor|. |
| 158 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
| 159 controller()->OverscrollUpdate(-10); |
| 160 EXPECT_TRUE(IsEffectActive()); |
| 161 EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), |
| 162 delegate()->GetFrameBounds().ToString()); |
| 163 |
| 164 controller()->OverscrollUpdate(0); |
| 165 EXPECT_FALSE(IsEffectActive()); |
| 166 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 167 delegate()->GetFrameBounds().ToString()); |
| 168 |
| 169 ActivateEffect(); |
| 170 controller()->OverscrollUpdate(5); |
| 171 EXPECT_TRUE(IsEffectActive()); |
| 172 EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), |
| 173 delegate()->GetFrameBounds().ToString()); |
| 174 |
| 175 // -10 gets clipped to -5 due to |kScrollEndEffectFactor|. |
| 176 controller()->OverscrollUpdate(-10); |
| 177 EXPECT_TRUE(IsEffectActive()); |
| 178 EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), |
| 179 delegate()->GetFrameBounds().ToString()); |
| 180 } |
| 181 |
| 182 TEST_F(ScrollEndEffectControllerAshTest, AnimationEnded) { |
| 183 // If the animation was to 0, then deactivate. |
| 184 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
| 185 ActivateEffect(); |
| 186 SetCurrentDeltaY(0); |
| 187 controller()->AnimationEnded(animation()); |
| 188 EXPECT_FALSE(IsEffectActive()); |
| 189 EXPECT_EQ(1, container()->times_deactivate_called()); |
| 190 |
| 191 // If the animation was not to 0, then don't deactivate. |
| 192 ActivateEffect(); |
| 193 SetCurrentDeltaY(10); |
| 194 controller()->AnimationEnded(animation()); |
| 195 EXPECT_TRUE(IsEffectActive()); |
| 196 EXPECT_EQ(1, container()->times_deactivate_called()); |
| 197 } |
| 198 |
| 199 TEST_F(ScrollEndEffectControllerAshTest, AnimationProgressed) { |
| 200 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
| 201 ActivateEffect(); |
| 202 SetStartDeltaY(10); |
| 203 SetEndDeltaY(0); |
| 204 |
| 205 controller()->AnimationProgressed(animation()); |
| 206 EXPECT_EQ(gfx::Rect(0, 10, 100, 90).ToString(), |
| 207 delegate()->GetFrameBounds().ToString()); |
| 208 EXPECT_EQ(1, container()->times_update_called()); |
| 209 EXPECT_EQ(10, container()->last_update_value()); |
| 210 EXPECT_TRUE(container()->last_scrolling_direction()); |
| 211 |
| 212 animation()->Reset(0.5); |
| 213 controller()->AnimationProgressed(animation()); |
| 214 EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), |
| 215 delegate()->GetFrameBounds().ToString()); |
| 216 EXPECT_EQ(2, container()->times_update_called()); |
| 217 EXPECT_EQ(5, container()->last_update_value()); |
| 218 EXPECT_TRUE(container()->last_scrolling_direction()); |
| 219 |
| 220 animation()->Reset(1.0); |
| 221 controller()->AnimationProgressed(animation()); |
| 222 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 223 delegate()->GetFrameBounds().ToString()); |
| 224 EXPECT_EQ(3, container()->times_update_called()); |
| 225 EXPECT_EQ(0, container()->last_update_value()); |
| 226 |
| 227 SetStartDeltaY(-10); |
| 228 |
| 229 animation()->Reset(0.0); |
| 230 controller()->AnimationProgressed(animation()); |
| 231 EXPECT_EQ(gfx::Rect(0, 0, 100, 90).ToString(), |
| 232 delegate()->GetFrameBounds().ToString()); |
| 233 EXPECT_EQ(4, container()->times_update_called()); |
| 234 EXPECT_EQ(10, container()->last_update_value()); |
| 235 EXPECT_FALSE(container()->last_scrolling_direction()); |
| 236 |
| 237 animation()->Reset(0.5); |
| 238 controller()->AnimationProgressed(animation()); |
| 239 EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), |
| 240 delegate()->GetFrameBounds().ToString()); |
| 241 EXPECT_EQ(5, container()->times_update_called()); |
| 242 EXPECT_EQ(5, container()->last_update_value()); |
| 243 EXPECT_FALSE(container()->last_scrolling_direction()); |
| 244 |
| 245 animation()->Reset(1.0); |
| 246 controller()->AnimationProgressed(animation()); |
| 247 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 248 delegate()->GetFrameBounds().ToString()); |
| 249 EXPECT_EQ(6, container()->times_update_called()); |
| 250 EXPECT_EQ(0, container()->last_update_value()); |
| 251 } |
| 252 |
| 253 TEST_F(ScrollEndEffectControllerAshTest, ActivateEffect) { |
| 254 ActivateEffect(); |
| 255 EXPECT_TRUE(IsEffectActive()); |
| 256 EXPECT_EQ(1, container()->times_activate_called()); |
| 257 } |
| 258 |
| 259 TEST_F(ScrollEndEffectControllerAshTest, DeactivateEffect) { |
| 260 DeactivateEffect(); |
| 261 EXPECT_FALSE(IsEffectActive()); |
| 262 EXPECT_EQ(1, container()->times_deactivate_called()); |
| 263 } |
| 264 |
| 265 TEST_F(ScrollEndEffectControllerAshTest, ApplyDelta) { |
| 266 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); |
| 267 ActivateEffect(); |
| 268 |
| 269 ApplyDelta(10); |
| 270 EXPECT_EQ(gfx::Rect(0, 10, 100, 90).ToString(), |
| 271 delegate()->GetFrameBounds().ToString()); |
| 272 EXPECT_EQ(1, container()->times_update_called()); |
| 273 EXPECT_EQ(10, container()->last_update_value()); |
| 274 EXPECT_TRUE(container()->last_scrolling_direction()); |
| 275 |
| 276 ApplyDelta(-10); |
| 277 EXPECT_EQ(gfx::Rect(0, 0, 100, 90).ToString(), |
| 278 delegate()->GetFrameBounds().ToString()); |
| 279 EXPECT_EQ(2, container()->times_update_called()); |
| 280 EXPECT_EQ(10, container()->last_update_value()); |
| 281 EXPECT_FALSE(container()->last_scrolling_direction()); |
| 282 } |
OLD | NEW |