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 "content/browser/web_contents/aura/overscroll_window_delegate.h" |
| 6 |
| 7 #include "content/browser/renderer_host/overscroll_controller_delegate.h" |
| 8 #include "content/public/browser/overscroll_configuration.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/aura/test/aura_test_base.h" |
| 11 #include "ui/aura/window.h" |
| 12 #include "ui/events/test/event_generator.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 namespace { |
| 17 const int kTestWindowWidth = 600; |
| 18 } |
| 19 |
| 20 class OverscrollWindowDelegateTest : public aura::test::AuraTestBase, |
| 21 public OverscrollControllerDelegate { |
| 22 public: |
| 23 OverscrollWindowDelegateTest() |
| 24 : window_(nullptr), |
| 25 overscroll_complete_(false), |
| 26 overscroll_started_(false), |
| 27 current_mode_(OVERSCROLL_NONE), |
| 28 touch_start_threshold_(content::GetOverscrollConfig( |
| 29 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN)), |
| 30 touch_complete_threshold_(content::GetOverscrollConfig( |
| 31 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE)) {} |
| 32 |
| 33 ~OverscrollWindowDelegateTest() override {} |
| 34 |
| 35 void Reset() { |
| 36 overscroll_complete_ = false; |
| 37 overscroll_started_ = false; |
| 38 current_mode_ = OVERSCROLL_NONE; |
| 39 window_.reset(CreateNormalWindow( |
| 40 0, root_window(), new OverscrollWindowDelegate(this, gfx::Image()))); |
| 41 window_->SetBounds(gfx::Rect(0, 0, kTestWindowWidth, kTestWindowWidth)); |
| 42 } |
| 43 |
| 44 // Accessors. |
| 45 aura::Window* window() { return window_.get(); } |
| 46 |
| 47 bool overscroll_complete() { return overscroll_complete_; } |
| 48 bool overscroll_started() { return overscroll_started_; } |
| 49 |
| 50 OverscrollMode current_mode() { return current_mode_; } |
| 51 |
| 52 const float touch_start_threshold() { |
| 53 return touch_start_threshold_; |
| 54 } |
| 55 |
| 56 const float touch_complete_threshold() { |
| 57 return kTestWindowWidth * touch_complete_threshold_; |
| 58 } |
| 59 |
| 60 protected: |
| 61 // aura::test::AuraTestBase: |
| 62 void SetUp() override { |
| 63 aura::test::AuraTestBase::SetUp(); |
| 64 Reset(); |
| 65 } |
| 66 |
| 67 void TearDown() override { |
| 68 window_.reset(); |
| 69 aura::test::AuraTestBase::TearDown(); |
| 70 } |
| 71 |
| 72 private: |
| 73 // OverscrollControllerDelegate: |
| 74 gfx::Rect GetVisibleBounds() const override { |
| 75 return gfx::Rect(600, 0); |
| 76 } |
| 77 |
| 78 bool OnOverscrollUpdate(float delta_x, float delta_y) override { |
| 79 return true; |
| 80 } |
| 81 |
| 82 void OnOverscrollComplete(OverscrollMode overscroll_mode) override { |
| 83 overscroll_complete_ = true; |
| 84 } |
| 85 |
| 86 void OnOverscrollModeChange(OverscrollMode old_mode, |
| 87 OverscrollMode new_mode) override { |
| 88 current_mode_ = new_mode; |
| 89 if (current_mode_ != OVERSCROLL_NONE) |
| 90 overscroll_started_ = true; |
| 91 } |
| 92 |
| 93 // Window in which the overscroll window delegate is installed. |
| 94 scoped_ptr<aura::Window> window_; |
| 95 |
| 96 // State flags. |
| 97 bool overscroll_complete_; |
| 98 bool overscroll_started_; |
| 99 |
| 100 OverscrollMode current_mode_; |
| 101 |
| 102 // Config defined constants. |
| 103 const float touch_start_threshold_; |
| 104 const float touch_complete_threshold_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(OverscrollWindowDelegateTest); |
| 107 }; |
| 108 |
| 109 // Tests that the basic overscroll gesture works and sends updates to the |
| 110 // delegate. |
| 111 TEST_F(OverscrollWindowDelegateTest, BasicOverscroll) { |
| 112 ui::test::EventGenerator generator(root_window()); |
| 113 |
| 114 // Start an OVERSCROLL_EAST gesture. |
| 115 generator.GestureScrollSequence( |
| 116 gfx::Point(10, 10), |
| 117 gfx::Point(20 + touch_complete_threshold(), 10), |
| 118 base::TimeDelta::FromMilliseconds(10), |
| 119 10); |
| 120 EXPECT_TRUE(overscroll_started()); |
| 121 EXPECT_EQ(current_mode(), OVERSCROLL_EAST); |
| 122 EXPECT_TRUE(overscroll_complete()); |
| 123 |
| 124 Reset(); |
| 125 // Start an OVERSCROLL_WEST gesture. |
| 126 generator.GestureScrollSequence( |
| 127 gfx::Point(20 + touch_complete_threshold(), 10), |
| 128 gfx::Point(10, 10), |
| 129 base::TimeDelta::FromMilliseconds(10), |
| 130 10); |
| 131 EXPECT_TRUE(overscroll_started()); |
| 132 EXPECT_EQ(current_mode(), OVERSCROLL_WEST); |
| 133 EXPECT_TRUE(overscroll_complete()); |
| 134 } |
| 135 |
| 136 // Verifies that the OverscrollWindowDelegate direction is set correctly during |
| 137 // an overscroll. |
| 138 TEST_F(OverscrollWindowDelegateTest, BasicOverscrollModes) { |
| 139 ui::test::EventGenerator generator(root_window()); |
| 140 OverscrollWindowDelegate* delegate = |
| 141 static_cast<OverscrollWindowDelegate*>(window()->delegate()); |
| 142 |
| 143 // Start pressing a touch, but do not start the gesture yet. |
| 144 generator.MoveTouch(gfx::Point(10, 10)); |
| 145 generator.PressTouch(); |
| 146 EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE); |
| 147 |
| 148 // Slide the touch to the right. |
| 149 generator.MoveTouch(gfx::Point(20 + touch_complete_threshold(), 10)); |
| 150 EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_EAST); |
| 151 |
| 152 // Complete the gesture. |
| 153 generator.ReleaseTouch(); |
| 154 EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE); |
| 155 EXPECT_TRUE(overscroll_complete()); |
| 156 |
| 157 // Start another overscroll. |
| 158 generator.MoveTouch(gfx::Point(20 + touch_complete_threshold(), 10)); |
| 159 generator.PressTouch(); |
| 160 EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE); |
| 161 |
| 162 // Slide the touch to the left. |
| 163 generator.MoveTouch(gfx::Point(10, 10)); |
| 164 EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_WEST); |
| 165 |
| 166 // Complete the gesture. |
| 167 generator.ReleaseTouch(); |
| 168 EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE); |
| 169 EXPECT_TRUE(overscroll_complete()); |
| 170 } |
| 171 |
| 172 // Tests that the overscroll does not start until the gesture gets past a |
| 173 // particular threshold. |
| 174 TEST_F(OverscrollWindowDelegateTest, OverscrollThreshold) { |
| 175 ui::test::EventGenerator generator(root_window()); |
| 176 |
| 177 // Start an OVERSCROLL_EAST gesture. |
| 178 generator.GestureScrollSequence( |
| 179 gfx::Point(10, 10), |
| 180 gfx::Point(10 + touch_start_threshold(), 10), |
| 181 base::TimeDelta::FromMilliseconds(10), |
| 182 10); |
| 183 EXPECT_FALSE(overscroll_started()); |
| 184 EXPECT_EQ(current_mode(), OVERSCROLL_NONE); |
| 185 EXPECT_FALSE(overscroll_complete()); |
| 186 |
| 187 Reset(); |
| 188 // Start an OVERSCROLL_WEST gesture. |
| 189 generator.GestureScrollSequence( |
| 190 gfx::Point(10 + touch_start_threshold(), 10), |
| 191 gfx::Point(10, 10), |
| 192 base::TimeDelta::FromMilliseconds(10), |
| 193 10); |
| 194 EXPECT_FALSE(overscroll_started()); |
| 195 EXPECT_EQ(current_mode(), OVERSCROLL_NONE); |
| 196 EXPECT_FALSE(overscroll_complete()); |
| 197 } |
| 198 |
| 199 // Tests that the overscroll is aborted if the gesture does not get past the |
| 200 // completion threshold. |
| 201 TEST_F(OverscrollWindowDelegateTest, AbortOverscrollThreshold) { |
| 202 ui::test::EventGenerator generator(root_window()); |
| 203 |
| 204 // Start an OVERSCROLL_EAST gesture. |
| 205 generator.GestureScrollSequence( |
| 206 gfx::Point(10, 10), |
| 207 gfx::Point(20 + touch_start_threshold(), 10), |
| 208 base::TimeDelta::FromMilliseconds(10), |
| 209 10); |
| 210 EXPECT_TRUE(overscroll_started()); |
| 211 EXPECT_EQ(current_mode(), OVERSCROLL_NONE); |
| 212 EXPECT_FALSE(overscroll_complete()); |
| 213 |
| 214 Reset(); |
| 215 // Start an OVERSCROLL_WEST gesture. |
| 216 generator.GestureScrollSequence( |
| 217 gfx::Point(20 + touch_start_threshold(), 10), |
| 218 gfx::Point(10, 10), |
| 219 base::TimeDelta::FromMilliseconds(10), |
| 220 10); |
| 221 EXPECT_TRUE(overscroll_started()); |
| 222 EXPECT_EQ(current_mode(), OVERSCROLL_NONE); |
| 223 EXPECT_FALSE(overscroll_complete()); |
| 224 } |
| 225 |
| 226 // Tests that the overscroll is aborted if the delegate receives some other |
| 227 // event. |
| 228 TEST_F(OverscrollWindowDelegateTest, EventAbortsOverscroll) { |
| 229 ui::test::EventGenerator generator(root_window()); |
| 230 // Start an OVERSCROLL_EAST gesture, without releasing touch. |
| 231 generator.set_current_location(gfx::Point(10, 10)); |
| 232 generator.PressTouch(); |
| 233 int touch_x = touch_start_threshold() + 20; |
| 234 generator.MoveTouch(gfx::Point(touch_x, 10)); |
| 235 EXPECT_TRUE(overscroll_started()); |
| 236 EXPECT_EQ(current_mode(), OVERSCROLL_EAST); |
| 237 EXPECT_FALSE(overscroll_complete()); |
| 238 |
| 239 // Dispatch a mouse event, the overscroll should be cancelled. |
| 240 generator.PressLeftButton(); |
| 241 EXPECT_EQ(current_mode(), OVERSCROLL_NONE); |
| 242 EXPECT_FALSE(overscroll_complete()); |
| 243 |
| 244 // We should be able to restart the overscroll without lifting the finger. |
| 245 generator.MoveTouch(gfx::Point(touch_x + touch_start_threshold() + 10, 10)); |
| 246 EXPECT_EQ(current_mode(), OVERSCROLL_EAST); |
| 247 EXPECT_FALSE(overscroll_complete()); |
| 248 } |
| 249 |
| 250 } // namespace content |
OLD | NEW |