Chromium Code Reviews| 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_; | |
|
sadrul
2013/10/25 18:28:25
DISALLOW_...
rharrison
2013/11/18 21:52:26
Done.
| |
| 52 }; | |
| 53 | |
| 54 class ScrollEndEffectControllerDelegateStub : | |
| 55 public ScrollEndEffectControllerDelegate { | |
| 56 public: | |
| 57 explicit ScrollEndEffectControllerDelegateStub( | |
| 58 ContentsContainerStub* container) { | |
| 59 container_ = container; | |
| 60 } | |
| 61 virtual ~ScrollEndEffectControllerDelegateStub() {} | |
| 62 | |
| 63 // ScrollEndEffectControllerDelegate overrides | |
| 64 virtual ContentsContainer* GetContentsContainer() OVERRIDE { | |
| 65 return container_; | |
| 66 } | |
| 67 | |
| 68 virtual gfx::Rect GetFrameBounds() OVERRIDE { | |
| 69 return frame_bounds_; | |
| 70 } | |
| 71 | |
| 72 virtual void SetFrameBounds(gfx::Rect bounds) OVERRIDE { | |
| 73 frame_bounds_ = bounds; | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 ContentsContainerStub* container_; // non-owned | |
| 78 gfx::Rect frame_bounds_; | |
|
sadrul
2013/10/25 18:28:25
ditto
rharrison
2013/11/18 21:52:26
Done.
| |
| 79 }; | |
| 80 | |
| 81 class ScrollEndEffectControllerAshTest : public Test { | |
| 82 public: | |
| 83 ScrollEndEffectControllerAshTest() : | |
| 84 delegate_(&container_), | |
| 85 controller_(&delegate_) { | |
| 86 controller_.animation_->SetSlideDuration(0); | |
| 87 } | |
| 88 virtual ~ScrollEndEffectControllerAshTest() {} | |
| 89 | |
| 90 ContentsContainerStub* container() { | |
| 91 return &container_; | |
| 92 } | |
| 93 | |
| 94 ScrollEndEffectControllerDelegateStub* delegate() { | |
| 95 return &delegate_; | |
| 96 } | |
| 97 | |
| 98 ScrollEndEffectControllerAsh* controller() { | |
| 99 return &controller_; | |
| 100 } | |
| 101 | |
| 102 gfx::SlideAnimation* animation() { | |
| 103 return controller_.animation_.get(); | |
| 104 } | |
| 105 | |
| 106 void ActivateEffect() { | |
| 107 controller_.ActivateEffect(); | |
| 108 } | |
| 109 | |
| 110 void DeactivateEffect() { | |
| 111 controller_.DeactivateEffect(); | |
| 112 } | |
| 113 | |
| 114 bool IsEffectActive() { | |
| 115 return controller_.is_effect_active_; | |
| 116 } | |
| 117 | |
| 118 void ApplyDelta(int delta_y) { | |
| 119 controller_.ApplyDelta(delta_y); | |
| 120 } | |
| 121 | |
| 122 void SetCurrentDeltaY(int delta_y) { | |
| 123 controller_.current_delta_y_ = delta_y; | |
| 124 } | |
| 125 | |
| 126 void SetStartDeltaY(int delta_y) { | |
| 127 controller_.start_delta_y_ = delta_y; | |
| 128 } | |
| 129 | |
| 130 void SetEndDeltaY(int delta_y) { | |
| 131 controller_.end_delta_y_ = delta_y; | |
| 132 } | |
| 133 | |
| 134 private: | |
| 135 ContentsContainerStub container_; | |
| 136 ScrollEndEffectControllerDelegateStub delegate_; | |
| 137 ScrollEndEffectControllerAsh controller_; | |
| 138 }; | |
| 139 | |
| 140 TEST_F(ScrollEndEffectControllerAshTest, OverscrollUpdate) { | |
| 141 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); | |
| 142 controller()->OverscrollUpdate(0); | |
| 143 EXPECT_FALSE(IsEffectActive()); | |
| 144 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), | |
| 145 delegate()->GetFrameBounds().ToString()); | |
| 146 | |
| 147 controller()->OverscrollUpdate(5); | |
| 148 EXPECT_TRUE(IsEffectActive()); | |
| 149 EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), | |
| 150 delegate()->GetFrameBounds().ToString()); | |
| 151 | |
| 152 DeactivateEffect(); | |
| 153 // -10 gets clipped to -5 due to |kScrollEndEffectFactor|. | |
| 154 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); | |
| 155 controller()->OverscrollUpdate(-10); | |
| 156 EXPECT_TRUE(IsEffectActive()); | |
| 157 EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), | |
| 158 delegate()->GetFrameBounds().ToString()); | |
| 159 | |
| 160 controller()->OverscrollUpdate(0); | |
| 161 EXPECT_FALSE(IsEffectActive()); | |
| 162 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), | |
| 163 delegate()->GetFrameBounds().ToString()); | |
| 164 | |
| 165 ActivateEffect(); | |
| 166 controller()->OverscrollUpdate(5); | |
| 167 EXPECT_TRUE(IsEffectActive()); | |
| 168 EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), | |
| 169 delegate()->GetFrameBounds().ToString()); | |
| 170 | |
| 171 // -10 gets clipped to -5 due to |kScrollEndEffectFactor|. | |
| 172 controller()->OverscrollUpdate(-10); | |
| 173 EXPECT_TRUE(IsEffectActive()); | |
| 174 EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), | |
| 175 delegate()->GetFrameBounds().ToString()); | |
| 176 } | |
| 177 | |
| 178 TEST_F(ScrollEndEffectControllerAshTest, AnimationEnded) { | |
| 179 // If the animation was to 0, then deactivate. | |
| 180 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); | |
| 181 ActivateEffect(); | |
| 182 SetCurrentDeltaY(0); | |
| 183 controller()->AnimationEnded(animation()); | |
| 184 EXPECT_FALSE(IsEffectActive()); | |
| 185 EXPECT_EQ(1, container()->times_deactivate_called()); | |
| 186 | |
| 187 // If the animation was not to 0, then don't deactivate. | |
| 188 ActivateEffect(); | |
| 189 SetCurrentDeltaY(10); | |
| 190 controller()->AnimationEnded(animation()); | |
| 191 EXPECT_TRUE(IsEffectActive()); | |
| 192 EXPECT_EQ(1, container()->times_deactivate_called()); | |
| 193 } | |
| 194 | |
| 195 TEST_F(ScrollEndEffectControllerAshTest, AnimationProgressed) { | |
| 196 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); | |
| 197 ActivateEffect(); | |
| 198 SetStartDeltaY(10); | |
| 199 SetEndDeltaY(0); | |
| 200 | |
| 201 controller()->AnimationProgressed(animation()); | |
| 202 EXPECT_EQ(gfx::Rect(0, 10, 100, 90).ToString(), | |
| 203 delegate()->GetFrameBounds().ToString()); | |
| 204 EXPECT_EQ(1, container()->times_update_called()); | |
| 205 EXPECT_EQ(10, container()->last_update_value()); | |
| 206 EXPECT_TRUE(container()->last_scrolling_direction()); | |
| 207 | |
| 208 animation()->Reset(0.5); | |
| 209 controller()->AnimationProgressed(animation()); | |
| 210 EXPECT_EQ(gfx::Rect(0, 5, 100, 95).ToString(), | |
| 211 delegate()->GetFrameBounds().ToString()); | |
| 212 EXPECT_EQ(2, container()->times_update_called()); | |
| 213 EXPECT_EQ(5, container()->last_update_value()); | |
| 214 EXPECT_TRUE(container()->last_scrolling_direction()); | |
| 215 | |
| 216 animation()->Reset(1.0); | |
| 217 controller()->AnimationProgressed(animation()); | |
| 218 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), | |
| 219 delegate()->GetFrameBounds().ToString()); | |
| 220 EXPECT_EQ(3, container()->times_update_called()); | |
| 221 EXPECT_EQ(0, container()->last_update_value()); | |
| 222 | |
| 223 SetStartDeltaY(-10); | |
| 224 | |
| 225 animation()->Reset(0.0); | |
| 226 controller()->AnimationProgressed(animation()); | |
| 227 EXPECT_EQ(gfx::Rect(0, 0, 100, 90).ToString(), | |
| 228 delegate()->GetFrameBounds().ToString()); | |
| 229 EXPECT_EQ(4, container()->times_update_called()); | |
| 230 EXPECT_EQ(10, container()->last_update_value()); | |
| 231 EXPECT_FALSE(container()->last_scrolling_direction()); | |
| 232 | |
| 233 animation()->Reset(0.5); | |
| 234 controller()->AnimationProgressed(animation()); | |
| 235 EXPECT_EQ(gfx::Rect(0, 0, 100, 95).ToString(), | |
| 236 delegate()->GetFrameBounds().ToString()); | |
| 237 EXPECT_EQ(5, container()->times_update_called()); | |
| 238 EXPECT_EQ(5, container()->last_update_value()); | |
| 239 EXPECT_FALSE(container()->last_scrolling_direction()); | |
| 240 | |
| 241 animation()->Reset(1.0); | |
| 242 controller()->AnimationProgressed(animation()); | |
| 243 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), | |
| 244 delegate()->GetFrameBounds().ToString()); | |
| 245 EXPECT_EQ(6, container()->times_update_called()); | |
| 246 EXPECT_EQ(0, container()->last_update_value()); | |
| 247 } | |
| 248 | |
| 249 TEST_F(ScrollEndEffectControllerAshTest, ActivateEffect) { | |
| 250 ActivateEffect(); | |
| 251 EXPECT_TRUE(IsEffectActive()); | |
| 252 EXPECT_EQ(1, container()->times_activate_called()); | |
| 253 } | |
| 254 | |
| 255 TEST_F(ScrollEndEffectControllerAshTest, DeactivateEffect) { | |
| 256 DeactivateEffect(); | |
| 257 EXPECT_FALSE(IsEffectActive()); | |
| 258 EXPECT_EQ(1, container()->times_deactivate_called()); | |
| 259 } | |
| 260 | |
| 261 TEST_F(ScrollEndEffectControllerAshTest, ApplyDelta) { | |
| 262 delegate()->SetFrameBounds(gfx::Rect(0, 0, 100, 100)); | |
| 263 ActivateEffect(); | |
| 264 | |
| 265 ApplyDelta(10); | |
| 266 EXPECT_EQ(gfx::Rect(0, 10, 100, 90).ToString(), | |
| 267 delegate()->GetFrameBounds().ToString()); | |
| 268 EXPECT_EQ(1, container()->times_update_called()); | |
| 269 EXPECT_EQ(10, container()->last_update_value()); | |
| 270 EXPECT_TRUE(container()->last_scrolling_direction()); | |
| 271 | |
| 272 ApplyDelta(-10); | |
| 273 EXPECT_EQ(gfx::Rect(0, 0, 100, 90).ToString(), | |
| 274 delegate()->GetFrameBounds().ToString()); | |
| 275 EXPECT_EQ(2, container()->times_update_called()); | |
| 276 EXPECT_EQ(10, container()->last_update_value()); | |
| 277 EXPECT_FALSE(container()->last_scrolling_direction()); | |
| 278 } | |
| OLD | NEW |