| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 "base/memory/scoped_ptr.h" | |
| 6 #include "base/time/time.h" | |
| 7 #include "content/browser/renderer_host/input/synthetic_gesture_controller_new.h
" | |
| 8 #include "content/browser/renderer_host/input/synthetic_gesture_new.h" | |
| 9 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" | |
| 10 #include "content/browser/renderer_host/input/synthetic_pinch_gesture_new.h" | |
| 11 #include "content/browser/renderer_host/input/synthetic_smooth_scroll_gesture_ne
w.h" | |
| 12 #include "content/browser/renderer_host/render_widget_host_delegate.h" | |
| 13 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 14 #include "content/common/input/input_event.h" | |
| 15 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" | |
| 16 #include "content/public/test/mock_render_process_host.h" | |
| 17 #include "content/public/test/test_browser_context.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
| 20 #include "ui/gfx/point_f.h" | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const int kFlushInputRateInMs = 16; | |
| 27 | |
| 28 class MockSyntheticGesture : public SyntheticGestureNew { | |
| 29 public: | |
| 30 MockSyntheticGesture(bool* finished, int num_steps) | |
| 31 : finished_(finished), | |
| 32 num_steps_(num_steps), | |
| 33 step_count_(0) { | |
| 34 *finished_ = false; | |
| 35 } | |
| 36 virtual ~MockSyntheticGesture() {} | |
| 37 | |
| 38 virtual Result ForwardInputEvents(const base::TimeDelta& interval, | |
| 39 SyntheticGestureTarget* target) OVERRIDE { | |
| 40 step_count_++; | |
| 41 if (step_count_ == num_steps_) { | |
| 42 *finished_ = true; | |
| 43 return SyntheticGestureNew::GESTURE_FINISHED; | |
| 44 } else if (step_count_ > num_steps_) { | |
| 45 *finished_ = true; | |
| 46 // Return arbitrary failure. | |
| 47 return SyntheticGestureNew::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; | |
| 48 } else { | |
| 49 return SyntheticGestureNew::GESTURE_RUNNING; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 protected: | |
| 54 bool* finished_; | |
| 55 int num_steps_; | |
| 56 int step_count_; | |
| 57 }; | |
| 58 | |
| 59 class MockSyntheticGestureTarget : public SyntheticGestureTarget { | |
| 60 public: | |
| 61 MockSyntheticGestureTarget() | |
| 62 : num_success_(0), | |
| 63 num_failure_(0), | |
| 64 flush_requested_(false) {} | |
| 65 virtual ~MockSyntheticGestureTarget() {} | |
| 66 | |
| 67 // SyntheticGestureTarget: | |
| 68 virtual void DispatchInputEventToPlatform(const InputEvent& event) OVERRIDE {} | |
| 69 | |
| 70 virtual void OnSyntheticGestureCompleted( | |
| 71 SyntheticGestureNew::Result result) OVERRIDE { | |
| 72 DCHECK_NE(result, SyntheticGestureNew::GESTURE_RUNNING); | |
| 73 if (result == SyntheticGestureNew::GESTURE_FINISHED) | |
| 74 num_success_++; | |
| 75 else | |
| 76 num_failure_++; | |
| 77 } | |
| 78 | |
| 79 virtual void SetNeedsFlush() OVERRIDE { | |
| 80 flush_requested_ = true; | |
| 81 } | |
| 82 | |
| 83 virtual SyntheticGestureParams::GestureSourceType | |
| 84 GetDefaultSyntheticGestureSourceType() const OVERRIDE { | |
| 85 return SyntheticGestureParams::TOUCH_INPUT; | |
| 86 } | |
| 87 virtual bool SupportsSyntheticGestureSourceType( | |
| 88 SyntheticGestureParams::GestureSourceType gesture_source_type) | |
| 89 const OVERRIDE { | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 int num_success() const { return num_success_; } | |
| 94 int num_failure() const { return num_failure_; } | |
| 95 | |
| 96 bool flush_requested() const { return flush_requested_; } | |
| 97 void ClearFlushRequest() { flush_requested_ = false; } | |
| 98 | |
| 99 private: | |
| 100 int num_success_; | |
| 101 int num_failure_; | |
| 102 | |
| 103 bool flush_requested_; | |
| 104 }; | |
| 105 | |
| 106 class MockSyntheticSmoothScrollMouseTarget : public MockSyntheticGestureTarget { | |
| 107 public: | |
| 108 MockSyntheticSmoothScrollMouseTarget() : scroll_distance_(0) {} | |
| 109 virtual ~MockSyntheticSmoothScrollMouseTarget() {} | |
| 110 | |
| 111 virtual void DispatchInputEventToPlatform(const InputEvent& event) OVERRIDE { | |
| 112 const blink::WebInputEvent* web_event = event.web_event.get(); | |
| 113 DCHECK_EQ(web_event->type, blink::WebInputEvent::MouseWheel); | |
| 114 const blink::WebMouseWheelEvent* mouse_wheel_event = | |
| 115 static_cast<const blink::WebMouseWheelEvent*>(web_event); | |
| 116 DCHECK_EQ(mouse_wheel_event->deltaX, 0); | |
| 117 scroll_distance_ -= mouse_wheel_event->deltaY; | |
| 118 } | |
| 119 | |
| 120 float scroll_distance() const { return scroll_distance_; } | |
| 121 | |
| 122 private: | |
| 123 float scroll_distance_; | |
| 124 }; | |
| 125 | |
| 126 class MockSyntheticSmoothScrollTouchTarget : public MockSyntheticGestureTarget { | |
| 127 public: | |
| 128 MockSyntheticSmoothScrollTouchTarget() | |
| 129 : scroll_distance_(0), anchor_y_(0), started_(false) {} | |
| 130 virtual ~MockSyntheticSmoothScrollTouchTarget() {} | |
| 131 | |
| 132 virtual void DispatchInputEventToPlatform(const InputEvent& event) OVERRIDE { | |
| 133 const blink::WebInputEvent* web_event = event.web_event.get(); | |
| 134 ASSERT_TRUE(blink::WebInputEvent::isTouchEventType(web_event->type)); | |
| 135 const blink::WebTouchEvent* touch_event = | |
| 136 static_cast<const blink::WebTouchEvent*>(web_event); | |
| 137 ASSERT_EQ(touch_event->touchesLength, (unsigned int)1); | |
| 138 | |
| 139 if (!started_) { | |
| 140 ASSERT_EQ(touch_event->type, blink::WebInputEvent::TouchStart); | |
| 141 anchor_y_ = touch_event->touches[0].position.y; | |
| 142 started_ = true; | |
| 143 } else { | |
| 144 ASSERT_NE(touch_event->type, blink::WebInputEvent::TouchStart); | |
| 145 ASSERT_NE(touch_event->type, blink::WebInputEvent::TouchCancel); | |
| 146 // Ignore move events. | |
| 147 | |
| 148 if (touch_event->type == blink::WebInputEvent::TouchEnd) | |
| 149 scroll_distance_ = anchor_y_ - touch_event->touches[0].position.y; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 float scroll_distance() const { return scroll_distance_; } | |
| 154 | |
| 155 private: | |
| 156 float scroll_distance_; | |
| 157 float anchor_y_; | |
| 158 bool started_; | |
| 159 }; | |
| 160 | |
| 161 class MockSyntheticPinchTouchTarget : public MockSyntheticGestureTarget { | |
| 162 public: | |
| 163 enum ZoomDirection { | |
| 164 ZOOM_DIRECTION_UNKNOWN, | |
| 165 ZOOM_IN, | |
| 166 ZOOM_OUT | |
| 167 }; | |
| 168 | |
| 169 MockSyntheticPinchTouchTarget() | |
| 170 : total_num_pixels_covered_(0), | |
| 171 last_pointer_distance_(0), | |
| 172 zoom_direction_(ZOOM_DIRECTION_UNKNOWN), | |
| 173 started_(false) {} | |
| 174 virtual ~MockSyntheticPinchTouchTarget() {} | |
| 175 | |
| 176 virtual void DispatchInputEventToPlatform(const InputEvent& event) OVERRIDE { | |
| 177 const blink::WebInputEvent* web_event = event.web_event.get(); | |
| 178 ASSERT_TRUE(blink::WebInputEvent::isTouchEventType(web_event->type)); | |
| 179 const blink::WebTouchEvent* touch_event = | |
| 180 static_cast<const blink::WebTouchEvent*>(web_event); | |
| 181 ASSERT_EQ(touch_event->touchesLength, (unsigned int)2); | |
| 182 | |
| 183 if (!started_) { | |
| 184 ASSERT_EQ(touch_event->type, blink::WebInputEvent::TouchStart); | |
| 185 | |
| 186 start_0_ = gfx::Point(touch_event->touches[0].position); | |
| 187 start_1_ = gfx::Point(touch_event->touches[1].position); | |
| 188 last_pointer_distance_ = (start_0_ - start_1_).Length(); | |
| 189 | |
| 190 started_ = true; | |
| 191 } else { | |
| 192 ASSERT_NE(touch_event->type, blink::WebInputEvent::TouchStart); | |
| 193 ASSERT_NE(touch_event->type, blink::WebInputEvent::TouchCancel); | |
| 194 | |
| 195 gfx::PointF current_0 = gfx::Point(touch_event->touches[0].position); | |
| 196 gfx::PointF current_1 = gfx::Point(touch_event->touches[1].position); | |
| 197 | |
| 198 total_num_pixels_covered_ = | |
| 199 (current_0 - start_0_).Length() + (current_1 - start_1_).Length(); | |
| 200 float pointer_distance = (current_0 - current_1).Length(); | |
| 201 | |
| 202 if (last_pointer_distance_ != pointer_distance) { | |
| 203 if (zoom_direction_ == ZOOM_DIRECTION_UNKNOWN) | |
| 204 zoom_direction_ = | |
| 205 ComputeZoomDirection(last_pointer_distance_, pointer_distance); | |
| 206 else | |
| 207 EXPECT_EQ( | |
| 208 zoom_direction_, | |
| 209 ComputeZoomDirection(last_pointer_distance_, pointer_distance)); | |
| 210 } | |
| 211 | |
| 212 last_pointer_distance_ = pointer_distance; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 float total_num_pixels_covered() const { return total_num_pixels_covered_; } | |
| 217 ZoomDirection zoom_direction() const { return zoom_direction_; } | |
| 218 | |
| 219 private: | |
| 220 ZoomDirection ComputeZoomDirection(float last_pointer_distance, | |
| 221 float current_pointer_distance) { | |
| 222 DCHECK_NE(last_pointer_distance, current_pointer_distance); | |
| 223 return last_pointer_distance < current_pointer_distance ? ZOOM_IN | |
| 224 : ZOOM_OUT; | |
| 225 } | |
| 226 float total_num_pixels_covered_; | |
| 227 float last_pointer_distance_; | |
| 228 ZoomDirection zoom_direction_; | |
| 229 gfx::PointF start_0_; | |
| 230 gfx::PointF start_1_; | |
| 231 bool started_; | |
| 232 }; | |
| 233 | |
| 234 class SyntheticGestureControllerNewTest : public testing::Test { | |
| 235 public: | |
| 236 SyntheticGestureControllerNewTest() {} | |
| 237 virtual ~SyntheticGestureControllerNewTest() {} | |
| 238 | |
| 239 protected: | |
| 240 template<typename MockGestureTarget> | |
| 241 void CreateControllerAndTarget() { | |
| 242 target_ = new MockGestureTarget(); | |
| 243 | |
| 244 controller_.reset(new SyntheticGestureControllerNew( | |
| 245 scoped_ptr<SyntheticGestureTarget>(target_))); | |
| 246 } | |
| 247 | |
| 248 virtual void SetUp() OVERRIDE { | |
| 249 time_ = base::TimeTicks::Now(); | |
| 250 } | |
| 251 | |
| 252 virtual void TearDown() OVERRIDE { | |
| 253 controller_.reset(); | |
| 254 target_ = NULL; | |
| 255 time_ = base::TimeTicks(); | |
| 256 } | |
| 257 | |
| 258 void FlushInputUntilComplete() { | |
| 259 while (target_->flush_requested()) { | |
| 260 target_->ClearFlushRequest(); | |
| 261 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs); | |
| 262 controller_->Flush(time_); | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 MockSyntheticGestureTarget* target_; | |
| 267 scoped_ptr<SyntheticGestureControllerNew> controller_; | |
| 268 base::TimeTicks time_; | |
| 269 }; | |
| 270 | |
| 271 TEST_F(SyntheticGestureControllerNewTest, SingleGesture) { | |
| 272 CreateControllerAndTarget<MockSyntheticGestureTarget>(); | |
| 273 | |
| 274 bool finished; | |
| 275 scoped_ptr<MockSyntheticGesture> gesture( | |
| 276 new MockSyntheticGesture(&finished, 3)); | |
| 277 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGestureNew>()); | |
| 278 FlushInputUntilComplete(); | |
| 279 | |
| 280 EXPECT_TRUE(finished); | |
| 281 EXPECT_EQ(1, target_->num_success()); | |
| 282 EXPECT_EQ(0, target_->num_failure()); | |
| 283 } | |
| 284 | |
| 285 TEST_F(SyntheticGestureControllerNewTest, GestureFailed) { | |
| 286 CreateControllerAndTarget<MockSyntheticGestureTarget>(); | |
| 287 | |
| 288 bool finished; | |
| 289 scoped_ptr<MockSyntheticGesture> gesture( | |
| 290 new MockSyntheticGesture(&finished, 0)); | |
| 291 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGestureNew>()); | |
| 292 FlushInputUntilComplete(); | |
| 293 | |
| 294 EXPECT_TRUE(finished); | |
| 295 EXPECT_EQ(1, target_->num_failure()); | |
| 296 EXPECT_EQ(0, target_->num_success()); | |
| 297 } | |
| 298 | |
| 299 TEST_F(SyntheticGestureControllerNewTest, SuccessiveGestures) { | |
| 300 CreateControllerAndTarget<MockSyntheticGestureTarget>(); | |
| 301 | |
| 302 bool finished_1, finished_2; | |
| 303 scoped_ptr<MockSyntheticGesture> gesture_1( | |
| 304 new MockSyntheticGesture(&finished_1, 2)); | |
| 305 scoped_ptr<MockSyntheticGesture> gesture_2( | |
| 306 new MockSyntheticGesture(&finished_2, 4)); | |
| 307 | |
| 308 // Queue first gesture and wait for it to finish | |
| 309 controller_->QueueSyntheticGesture(gesture_1.PassAs<SyntheticGestureNew>()); | |
| 310 FlushInputUntilComplete(); | |
| 311 | |
| 312 EXPECT_TRUE(finished_1); | |
| 313 EXPECT_EQ(1, target_->num_success()); | |
| 314 EXPECT_EQ(0, target_->num_failure()); | |
| 315 | |
| 316 // Queue second gesture. | |
| 317 controller_->QueueSyntheticGesture(gesture_2.PassAs<SyntheticGestureNew>()); | |
| 318 FlushInputUntilComplete(); | |
| 319 | |
| 320 EXPECT_TRUE(finished_2); | |
| 321 EXPECT_EQ(2, target_->num_success()); | |
| 322 EXPECT_EQ(0, target_->num_failure()); | |
| 323 } | |
| 324 | |
| 325 TEST_F(SyntheticGestureControllerNewTest, TwoGesturesInFlight) { | |
| 326 CreateControllerAndTarget<MockSyntheticGestureTarget>(); | |
| 327 | |
| 328 bool finished_1, finished_2; | |
| 329 scoped_ptr<MockSyntheticGesture> gesture_1( | |
| 330 new MockSyntheticGesture(&finished_1, 2)); | |
| 331 scoped_ptr<MockSyntheticGesture> gesture_2( | |
| 332 new MockSyntheticGesture(&finished_2, 4)); | |
| 333 | |
| 334 controller_->QueueSyntheticGesture(gesture_1.PassAs<SyntheticGestureNew>()); | |
| 335 controller_->QueueSyntheticGesture(gesture_2.PassAs<SyntheticGestureNew>()); | |
| 336 FlushInputUntilComplete(); | |
| 337 | |
| 338 EXPECT_TRUE(finished_1); | |
| 339 EXPECT_TRUE(finished_2); | |
| 340 | |
| 341 EXPECT_EQ(2, target_->num_success()); | |
| 342 EXPECT_EQ(0, target_->num_failure()); | |
| 343 } | |
| 344 | |
| 345 TEST_F(SyntheticGestureControllerNewTest, SmoothScrollGestureTouch) { | |
| 346 CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>(); | |
| 347 | |
| 348 SyntheticSmoothScrollGestureParams params; | |
| 349 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
| 350 params.distance = 100; | |
| 351 | |
| 352 scoped_ptr<SyntheticSmoothScrollGestureNew> gesture( | |
| 353 new SyntheticSmoothScrollGestureNew(params)); | |
| 354 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGestureNew>()); | |
| 355 FlushInputUntilComplete(); | |
| 356 | |
| 357 EXPECT_EQ(1, target_->num_success()); | |
| 358 EXPECT_EQ(0, target_->num_failure()); | |
| 359 EXPECT_LE(params.distance, static_cast<MockSyntheticSmoothScrollTouchTarget*>( | |
| 360 target_)->scroll_distance()); | |
| 361 } | |
| 362 | |
| 363 TEST_F(SyntheticGestureControllerNewTest, SmoothScrollGestureMouse) { | |
| 364 CreateControllerAndTarget<MockSyntheticSmoothScrollMouseTarget>(); | |
| 365 | |
| 366 SyntheticSmoothScrollGestureParams params; | |
| 367 params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT; | |
| 368 params.distance = -100; | |
| 369 | |
| 370 scoped_ptr<SyntheticSmoothScrollGestureNew> gesture( | |
| 371 new SyntheticSmoothScrollGestureNew(params)); | |
| 372 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGestureNew>()); | |
| 373 FlushInputUntilComplete(); | |
| 374 | |
| 375 EXPECT_EQ(1, target_->num_success()); | |
| 376 EXPECT_EQ(0, target_->num_failure()); | |
| 377 EXPECT_GE(params.distance, static_cast<MockSyntheticSmoothScrollTouchTarget*>( | |
| 378 target_)->scroll_distance()); | |
| 379 } | |
| 380 | |
| 381 TEST_F(SyntheticGestureControllerNewTest, PinchGestureTouchZoomIn) { | |
| 382 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); | |
| 383 | |
| 384 SyntheticPinchGestureParams params; | |
| 385 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
| 386 params.zoom_in = true; | |
| 387 params.total_num_pixels_covered = 100; | |
| 388 | |
| 389 scoped_ptr<SyntheticPinchGestureNew> gesture( | |
| 390 new SyntheticPinchGestureNew(params)); | |
| 391 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGestureNew>()); | |
| 392 FlushInputUntilComplete(); | |
| 393 | |
| 394 EXPECT_EQ(1, target_->num_success()); | |
| 395 EXPECT_EQ(0, target_->num_failure()); | |
| 396 EXPECT_EQ( | |
| 397 static_cast<MockSyntheticPinchTouchTarget*>(target_)->zoom_direction(), | |
| 398 MockSyntheticPinchTouchTarget::ZOOM_IN); | |
| 399 EXPECT_LE( | |
| 400 params.total_num_pixels_covered, | |
| 401 static_cast<MockSyntheticPinchTouchTarget*>(target_) | |
| 402 ->total_num_pixels_covered()); | |
| 403 } | |
| 404 | |
| 405 TEST_F(SyntheticGestureControllerNewTest, PinchGestureTouchZoomOut) { | |
| 406 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); | |
| 407 | |
| 408 SyntheticPinchGestureParams params; | |
| 409 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
| 410 params.zoom_in = false; | |
| 411 params.total_num_pixels_covered = 100; | |
| 412 | |
| 413 scoped_ptr<SyntheticPinchGestureNew> gesture( | |
| 414 new SyntheticPinchGestureNew(params)); | |
| 415 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGestureNew>()); | |
| 416 FlushInputUntilComplete(); | |
| 417 | |
| 418 EXPECT_EQ(1, target_->num_success()); | |
| 419 EXPECT_EQ(0, target_->num_failure()); | |
| 420 EXPECT_EQ( | |
| 421 static_cast<MockSyntheticPinchTouchTarget*>(target_)->zoom_direction(), | |
| 422 MockSyntheticPinchTouchTarget::ZOOM_OUT); | |
| 423 EXPECT_LE( | |
| 424 params.total_num_pixels_covered, | |
| 425 static_cast<MockSyntheticPinchTouchTarget*>(target_) | |
| 426 ->total_num_pixels_covered()); | |
| 427 } | |
| 428 | |
| 429 } // namespace | |
| 430 | |
| 431 } // namespace content | |
| OLD | NEW |