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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/time/time.h"
7 #include "content/browser/renderer_host/input/synthetic_gesture.h"
8 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h"
9 #include "content/browser/renderer_host/input/synthetic_gesture_target.h"
10 #include "content/browser/renderer_host/input/synthetic_pinch_gesture.h"
11 #include "content/browser/renderer_host/input/synthetic_smooth_scroll_gesture.h"
12 #include "content/browser/renderer_host/input/synthetic_tap_gesture.h"
13 #include "content/browser/renderer_host/render_widget_host_delegate.h"
14 #include "content/common/input/synthetic_pinch_gesture_params.h"
15 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
16 #include "content/common/input/synthetic_tap_gesture_params.h"
17 #include "content/public/test/mock_render_process_host.h"
18 #include "content/public/test/test_browser_context.h"
19 #include "content/test/test_render_view_host.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 #include "ui/gfx/point.h"
23 #include "ui/gfx/point_f.h"
24 #include "ui/gfx/vector2d.h"
25 #include "ui/gfx/vector2d_f.h"
27 using blink::WebInputEvent;
28 using blink::WebMouseEvent;
29 using blink::WebMouseWheelEvent;
30 using blink::WebTouchEvent;
36 const int kFlushInputRateInMs = 16;
37 const int kPointerAssumedStoppedTimeMs = 43;
38 const int kTouchSlopInDips = 7;
40 class MockSyntheticGesture : public SyntheticGesture {
42 MockSyntheticGesture(bool* finished, int num_steps)
43 : finished_(finished),
44 num_steps_(num_steps),
48 virtual ~MockSyntheticGesture() {}
50 virtual Result ForwardInputEvents(const base::TimeTicks& timestamp,
51 SyntheticGestureTarget* target) OVERRIDE {
53 if (step_count_ == num_steps_) {
55 return SyntheticGesture::GESTURE_FINISHED;
56 } else if (step_count_ > num_steps_) {
58 // Return arbitrary failure.
59 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED;
62 return SyntheticGesture::GESTURE_RUNNING;
71 class MockSyntheticGestureTarget : public SyntheticGestureTarget {
73 MockSyntheticGestureTarget()
76 flush_requested_(false),
77 pointer_assumed_stopped_time_ms_(kPointerAssumedStoppedTimeMs) {}
78 virtual ~MockSyntheticGestureTarget() {}
80 // SyntheticGestureTarget:
81 virtual void DispatchInputEventToPlatform(
82 const WebInputEvent& event) OVERRIDE {}
84 virtual void OnSyntheticGestureCompleted(
85 SyntheticGesture::Result result) OVERRIDE {
86 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING);
87 if (result == SyntheticGesture::GESTURE_FINISHED)
93 virtual void SetNeedsFlush() OVERRIDE {
94 flush_requested_ = true;
97 virtual SyntheticGestureParams::GestureSourceType
98 GetDefaultSyntheticGestureSourceType() const OVERRIDE {
99 return SyntheticGestureParams::TOUCH_INPUT;
101 virtual bool SupportsSyntheticGestureSourceType(
102 SyntheticGestureParams::GestureSourceType gesture_source_type)
107 virtual base::TimeDelta PointerAssumedStoppedTime() const OVERRIDE {
108 return base::TimeDelta::FromMilliseconds(pointer_assumed_stopped_time_ms_);
111 void set_pointer_assumed_stopped_time_ms(int time_ms) {
112 pointer_assumed_stopped_time_ms_ = time_ms;
115 virtual int GetTouchSlopInDips() const OVERRIDE {
116 return kTouchSlopInDips;
119 int num_success() const { return num_success_; }
120 int num_failure() const { return num_failure_; }
122 bool flush_requested() const { return flush_requested_; }
123 void ClearFlushRequest() { flush_requested_ = false; }
129 bool flush_requested_;
131 int pointer_assumed_stopped_time_ms_;
134 class MockSyntheticSmoothScrollGestureTarget
135 : public MockSyntheticGestureTarget {
137 MockSyntheticSmoothScrollGestureTarget() {}
138 virtual ~MockSyntheticSmoothScrollGestureTarget() {}
140 gfx::Vector2dF scroll_distance() const { return scroll_distance_; }
143 gfx::Vector2dF scroll_distance_;
146 class MockSyntheticSmoothScrollMouseTarget
147 : public MockSyntheticSmoothScrollGestureTarget {
149 MockSyntheticSmoothScrollMouseTarget() {}
150 virtual ~MockSyntheticSmoothScrollMouseTarget() {}
152 virtual void DispatchInputEventToPlatform(
153 const WebInputEvent& event) OVERRIDE {
154 ASSERT_EQ(event.type, WebInputEvent::MouseWheel);
155 const WebMouseWheelEvent& mouse_wheel_event =
156 static_cast<const WebMouseWheelEvent&>(event);
157 scroll_distance_ -= gfx::Vector2dF(mouse_wheel_event.deltaX,
158 mouse_wheel_event.deltaY);
162 class MockSyntheticSmoothScrollTouchTarget
163 : public MockSyntheticSmoothScrollGestureTarget {
165 MockSyntheticSmoothScrollTouchTarget()
167 virtual ~MockSyntheticSmoothScrollTouchTarget() {}
169 virtual void DispatchInputEventToPlatform(
170 const WebInputEvent& event) OVERRIDE {
171 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type));
172 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event);
173 ASSERT_EQ(touch_event.touchesLength, 1U);
176 ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart);
177 anchor_.SetPoint(touch_event.touches[0].position.x,
178 touch_event.touches[0].position.y);
181 ASSERT_NE(touch_event.type, WebInputEvent::TouchStart);
182 ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel);
183 // Ignore move events.
185 if (touch_event.type == WebInputEvent::TouchEnd)
187 anchor_ - gfx::PointF(touch_event.touches[0].position.x,
188 touch_event.touches[0].position.y);
197 class MockSyntheticPinchTouchTarget : public MockSyntheticGestureTarget {
200 ZOOM_DIRECTION_UNKNOWN,
205 MockSyntheticPinchTouchTarget()
206 : total_num_pixels_covered_(0),
207 last_pointer_distance_(0),
208 zoom_direction_(ZOOM_DIRECTION_UNKNOWN),
210 virtual ~MockSyntheticPinchTouchTarget() {}
212 virtual void DispatchInputEventToPlatform(
213 const WebInputEvent& event) OVERRIDE {
214 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type));
215 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event);
216 ASSERT_EQ(touch_event.touchesLength, 2U);
219 ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart);
221 start_0_ = gfx::Point(touch_event.touches[0].position);
222 start_1_ = gfx::Point(touch_event.touches[1].position);
223 last_pointer_distance_ = (start_0_ - start_1_).Length();
227 ASSERT_NE(touch_event.type, WebInputEvent::TouchStart);
228 ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel);
230 gfx::PointF current_0 = gfx::Point(touch_event.touches[0].position);
231 gfx::PointF current_1 = gfx::Point(touch_event.touches[1].position);
233 total_num_pixels_covered_ =
234 (current_0 - start_0_).Length() + (current_1 - start_1_).Length();
235 float pointer_distance = (current_0 - current_1).Length();
237 if (last_pointer_distance_ != pointer_distance) {
238 if (zoom_direction_ == ZOOM_DIRECTION_UNKNOWN)
240 ComputeZoomDirection(last_pointer_distance_, pointer_distance);
244 ComputeZoomDirection(last_pointer_distance_, pointer_distance));
247 last_pointer_distance_ = pointer_distance;
251 float total_num_pixels_covered() const { return total_num_pixels_covered_; }
252 ZoomDirection zoom_direction() const { return zoom_direction_; }
255 ZoomDirection ComputeZoomDirection(float last_pointer_distance,
256 float current_pointer_distance) {
257 DCHECK_NE(last_pointer_distance, current_pointer_distance);
258 return last_pointer_distance < current_pointer_distance ? ZOOM_IN
262 float total_num_pixels_covered_;
263 float last_pointer_distance_;
264 ZoomDirection zoom_direction_;
265 gfx::PointF start_0_;
266 gfx::PointF start_1_;
270 class MockSyntheticTapGestureTarget : public MockSyntheticGestureTarget {
272 MockSyntheticTapGestureTarget() : state_(NOT_STARTED) {}
273 virtual ~MockSyntheticTapGestureTarget() {}
275 bool GestureFinished() const { return state_ == FINISHED; }
276 gfx::Point position() const { return position_; }
277 base::TimeDelta GetDuration() const { return stop_time_ - start_time_; }
286 gfx::Point position_;
287 base::TimeDelta start_time_;
288 base::TimeDelta stop_time_;
292 class MockSyntheticTapTouchTarget : public MockSyntheticTapGestureTarget {
294 MockSyntheticTapTouchTarget() {}
295 virtual ~MockSyntheticTapTouchTarget() {}
297 virtual void DispatchInputEventToPlatform(
298 const WebInputEvent& event) OVERRIDE {
299 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type));
300 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event);
301 ASSERT_EQ(touch_event.touchesLength, 1U);
305 EXPECT_EQ(touch_event.type, WebInputEvent::TouchStart);
306 position_ = gfx::Point(touch_event.touches[0].position);
307 start_time_ = base::TimeDelta::FromMilliseconds(
308 static_cast<int64>(touch_event.timeStampSeconds * 1000));
312 EXPECT_EQ(touch_event.type, WebInputEvent::TouchEnd);
313 EXPECT_EQ(position_, gfx::Point(touch_event.touches[0].position));
314 stop_time_ = base::TimeDelta::FromMilliseconds(
315 static_cast<int64>(touch_event.timeStampSeconds * 1000));
325 class MockSyntheticTapMouseTarget : public MockSyntheticTapGestureTarget {
327 MockSyntheticTapMouseTarget() {}
328 virtual ~MockSyntheticTapMouseTarget() {}
330 virtual void DispatchInputEventToPlatform(
331 const WebInputEvent& event) OVERRIDE {
332 ASSERT_TRUE(WebInputEvent::isMouseEventType(event.type));
333 const WebMouseEvent& mouse_event = static_cast<const WebMouseEvent&>(event);
337 EXPECT_EQ(mouse_event.type, WebInputEvent::MouseDown);
338 EXPECT_EQ(mouse_event.button, WebMouseEvent::ButtonLeft);
339 EXPECT_EQ(mouse_event.clickCount, 1);
340 position_ = gfx::Point(mouse_event.x, mouse_event.y);
341 start_time_ = base::TimeDelta::FromMilliseconds(
342 static_cast<int64>(mouse_event.timeStampSeconds * 1000));
346 EXPECT_EQ(mouse_event.type, WebInputEvent::MouseUp);
347 EXPECT_EQ(mouse_event.button, WebMouseEvent::ButtonLeft);
348 EXPECT_EQ(mouse_event.clickCount, 1);
349 EXPECT_EQ(position_, gfx::Point(mouse_event.x, mouse_event.y));
350 stop_time_ = base::TimeDelta::FromMilliseconds(
351 static_cast<int64>(mouse_event.timeStampSeconds * 1000));
361 class SyntheticGestureControllerTest : public testing::Test {
363 SyntheticGestureControllerTest() {}
364 virtual ~SyntheticGestureControllerTest() {}
367 template<typename MockGestureTarget>
368 void CreateControllerAndTarget() {
369 target_ = new MockGestureTarget();
371 controller_.reset(new SyntheticGestureController(
372 scoped_ptr<SyntheticGestureTarget>(target_)));
375 virtual void SetUp() OVERRIDE {
376 start_time_ = base::TimeTicks::Now();
380 virtual void TearDown() OVERRIDE {
383 time_ = base::TimeTicks();
386 void FlushInputUntilComplete() {
387 while (target_->flush_requested()) {
388 target_->ClearFlushRequest();
389 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs);
390 controller_->Flush(time_);
394 base::TimeDelta GetTotalTime() const { return time_ - start_time_; }
396 MockSyntheticGestureTarget* target_;
397 scoped_ptr<SyntheticGestureController> controller_;
398 base::TimeTicks start_time_;
399 base::TimeTicks time_;
402 TEST_F(SyntheticGestureControllerTest, SingleGesture) {
403 CreateControllerAndTarget<MockSyntheticGestureTarget>();
406 scoped_ptr<MockSyntheticGesture> gesture(
407 new MockSyntheticGesture(&finished, 3));
408 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
409 FlushInputUntilComplete();
411 EXPECT_TRUE(finished);
412 EXPECT_EQ(1, target_->num_success());
413 EXPECT_EQ(0, target_->num_failure());
416 TEST_F(SyntheticGestureControllerTest, GestureFailed) {
417 CreateControllerAndTarget<MockSyntheticGestureTarget>();
420 scoped_ptr<MockSyntheticGesture> gesture(
421 new MockSyntheticGesture(&finished, 0));
422 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
423 FlushInputUntilComplete();
425 EXPECT_TRUE(finished);
426 EXPECT_EQ(1, target_->num_failure());
427 EXPECT_EQ(0, target_->num_success());
430 TEST_F(SyntheticGestureControllerTest, SuccessiveGestures) {
431 CreateControllerAndTarget<MockSyntheticGestureTarget>();
433 bool finished_1, finished_2;
434 scoped_ptr<MockSyntheticGesture> gesture_1(
435 new MockSyntheticGesture(&finished_1, 2));
436 scoped_ptr<MockSyntheticGesture> gesture_2(
437 new MockSyntheticGesture(&finished_2, 4));
439 // Queue first gesture and wait for it to finish
440 controller_->QueueSyntheticGesture(gesture_1.PassAs<SyntheticGesture>());
441 FlushInputUntilComplete();
443 EXPECT_TRUE(finished_1);
444 EXPECT_EQ(1, target_->num_success());
445 EXPECT_EQ(0, target_->num_failure());
447 // Queue second gesture.
448 controller_->QueueSyntheticGesture(gesture_2.PassAs<SyntheticGesture>());
449 FlushInputUntilComplete();
451 EXPECT_TRUE(finished_2);
452 EXPECT_EQ(2, target_->num_success());
453 EXPECT_EQ(0, target_->num_failure());
456 TEST_F(SyntheticGestureControllerTest, TwoGesturesInFlight) {
457 CreateControllerAndTarget<MockSyntheticGestureTarget>();
459 bool finished_1, finished_2;
460 scoped_ptr<MockSyntheticGesture> gesture_1(
461 new MockSyntheticGesture(&finished_1, 2));
462 scoped_ptr<MockSyntheticGesture> gesture_2(
463 new MockSyntheticGesture(&finished_2, 4));
465 controller_->QueueSyntheticGesture(gesture_1.PassAs<SyntheticGesture>());
466 controller_->QueueSyntheticGesture(gesture_2.PassAs<SyntheticGesture>());
467 FlushInputUntilComplete();
469 EXPECT_TRUE(finished_1);
470 EXPECT_TRUE(finished_2);
472 EXPECT_EQ(2, target_->num_success());
473 EXPECT_EQ(0, target_->num_failure());
476 gfx::Vector2d AddTouchSlopToVector(const gfx::Vector2d& vector,
477 SyntheticGestureTarget* target) {
478 const int kTouchSlop = target->GetTouchSlopInDips();
492 return gfx::Vector2d(x, y);
495 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchVertical) {
496 CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
498 SyntheticSmoothScrollGestureParams params;
499 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
500 params.distance = gfx::Vector2d(0, 123);
501 params.anchor.SetPoint(89, 32);
503 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
504 new SyntheticSmoothScrollGesture(params));
505 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
506 FlushInputUntilComplete();
508 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
509 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
510 EXPECT_EQ(1, target_->num_success());
511 EXPECT_EQ(0, target_->num_failure());
512 EXPECT_EQ(AddTouchSlopToVector(params.distance, target_),
513 smooth_scroll_target->scroll_distance());
516 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchHorizontal) {
517 CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
519 SyntheticSmoothScrollGestureParams params;
520 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
521 params.distance = gfx::Vector2d(-234, 0);
522 params.anchor.SetPoint(12, -23);
524 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
525 new SyntheticSmoothScrollGesture(params));
526 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
527 FlushInputUntilComplete();
529 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
530 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
531 EXPECT_EQ(1, target_->num_success());
532 EXPECT_EQ(0, target_->num_failure());
533 EXPECT_EQ(AddTouchSlopToVector(params.distance, target_),
534 smooth_scroll_target->scroll_distance());
537 void CheckIsWithinRange(float scroll_distance,
539 SyntheticGestureTarget* target) {
540 if (target_distance > 0) {
541 EXPECT_LE(target_distance, scroll_distance);
542 EXPECT_LE(scroll_distance, target_distance + target->GetTouchSlopInDips());
544 EXPECT_GE(target_distance, scroll_distance);
545 EXPECT_GE(scroll_distance, target_distance - target->GetTouchSlopInDips());
549 void CheckScrollDistanceIsWithinRange(const gfx::Vector2dF& scroll_distance,
550 const gfx::Vector2d& target_distance,
551 SyntheticGestureTarget* target) {
552 CheckIsWithinRange(scroll_distance.x(), target_distance.x(), target);
553 CheckIsWithinRange(scroll_distance.y(), target_distance.y(), target);
556 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchDiagonal) {
557 CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
559 SyntheticSmoothScrollGestureParams params;
560 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
561 params.distance = gfx::Vector2d(413, -83);
562 params.anchor.SetPoint(0, 7);
564 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
565 new SyntheticSmoothScrollGesture(params));
566 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
567 FlushInputUntilComplete();
569 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
570 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
571 EXPECT_EQ(1, target_->num_success());
572 EXPECT_EQ(0, target_->num_failure());
573 CheckScrollDistanceIsWithinRange(
574 smooth_scroll_target->scroll_distance(), params.distance, target_);
577 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchLongStop) {
578 CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
580 // Create a smooth scroll with a short distance and set the pointer assumed
581 // stopped time high, so that the stopping should dominate the time the
582 // gesture is active.
583 SyntheticSmoothScrollGestureParams params;
584 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
585 params.distance = gfx::Vector2d(21, -12);
586 params.prevent_fling = true;
587 params.anchor.SetPoint(-98, -23);
589 target_->set_pointer_assumed_stopped_time_ms(543);
591 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
592 new SyntheticSmoothScrollGesture(params));
593 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
594 FlushInputUntilComplete();
596 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
597 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
598 EXPECT_EQ(1, target_->num_success());
599 EXPECT_EQ(0, target_->num_failure());
600 CheckScrollDistanceIsWithinRange(
601 smooth_scroll_target->scroll_distance(), params.distance, target_);
602 EXPECT_GE(GetTotalTime(), target_->PointerAssumedStoppedTime());
605 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchFling) {
606 CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
608 // Create a smooth scroll with a short distance and set the pointer assumed
609 // stopped time high. Disable 'prevent_fling' and check that the gesture
610 // finishes without waiting before it stops.
611 SyntheticSmoothScrollGestureParams params;
612 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
613 params.distance = gfx::Vector2d(-43, 19);
614 params.prevent_fling = false;
615 params.anchor.SetPoint(-89, 78);
617 target_->set_pointer_assumed_stopped_time_ms(543);
619 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
620 new SyntheticSmoothScrollGesture(params));
621 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
622 FlushInputUntilComplete();
624 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
625 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
626 EXPECT_EQ(1, target_->num_success());
627 EXPECT_EQ(0, target_->num_failure());
628 CheckScrollDistanceIsWithinRange(
629 smooth_scroll_target->scroll_distance(), params.distance, target_);
630 EXPECT_LE(GetTotalTime(), target_->PointerAssumedStoppedTime());
633 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchZeroDistance) {
634 CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
636 SyntheticSmoothScrollGestureParams params;
637 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
638 params.distance = gfx::Vector2d(0, 0);
639 params.anchor.SetPoint(-32, 43);
641 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
642 new SyntheticSmoothScrollGesture(params));
643 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
644 FlushInputUntilComplete();
646 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
647 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
648 EXPECT_EQ(1, target_->num_success());
649 EXPECT_EQ(0, target_->num_failure());
650 EXPECT_EQ(gfx::Vector2dF(0, 0), smooth_scroll_target->scroll_distance());
653 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureMouseVertical) {
654 CreateControllerAndTarget<MockSyntheticSmoothScrollMouseTarget>();
656 SyntheticSmoothScrollGestureParams params;
657 params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
658 params.distance = gfx::Vector2d(0, -234);
659 params.anchor.SetPoint(432, 89);
661 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
662 new SyntheticSmoothScrollGesture(params));
663 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
664 FlushInputUntilComplete();
666 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
667 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
668 EXPECT_EQ(1, target_->num_success());
669 EXPECT_EQ(0, target_->num_failure());
670 EXPECT_EQ(params.distance, smooth_scroll_target->scroll_distance());
673 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureMouseHorizontal) {
674 CreateControllerAndTarget<MockSyntheticSmoothScrollMouseTarget>();
676 SyntheticSmoothScrollGestureParams params;
677 params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
678 params.distance = gfx::Vector2d(345, 0);
679 params.anchor.SetPoint(90, 12);
681 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
682 new SyntheticSmoothScrollGesture(params));
683 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
684 FlushInputUntilComplete();
686 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
687 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
688 EXPECT_EQ(1, target_->num_success());
689 EXPECT_EQ(0, target_->num_failure());
690 EXPECT_EQ(params.distance, smooth_scroll_target->scroll_distance());
693 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureMouseDiagonal) {
694 CreateControllerAndTarget<MockSyntheticSmoothScrollMouseTarget>();
696 SyntheticSmoothScrollGestureParams params;
697 params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
698 params.distance = gfx::Vector2d(-194, 303);
699 params.anchor.SetPoint(90, 12);
701 scoped_ptr<SyntheticSmoothScrollGesture> gesture(
702 new SyntheticSmoothScrollGesture(params));
703 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
704 FlushInputUntilComplete();
706 MockSyntheticSmoothScrollGestureTarget* smooth_scroll_target =
707 static_cast<MockSyntheticSmoothScrollGestureTarget*>(target_);
708 EXPECT_EQ(1, target_->num_success());
709 EXPECT_EQ(0, target_->num_failure());
710 EXPECT_EQ(params.distance, smooth_scroll_target->scroll_distance());
713 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomIn) {
714 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
716 SyntheticPinchGestureParams params;
717 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
718 params.zoom_in = true;
719 params.total_num_pixels_covered = 345;
720 params.anchor.SetPoint(54, 89);
722 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
723 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
724 FlushInputUntilComplete();
726 MockSyntheticPinchTouchTarget* pinch_target =
727 static_cast<MockSyntheticPinchTouchTarget*>(target_);
728 EXPECT_EQ(1, target_->num_success());
729 EXPECT_EQ(0, target_->num_failure());
730 EXPECT_EQ(pinch_target->zoom_direction(),
731 MockSyntheticPinchTouchTarget::ZOOM_IN);
732 EXPECT_EQ(params.total_num_pixels_covered + 2 * target_->GetTouchSlopInDips(),
733 pinch_target->total_num_pixels_covered());
736 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomOut) {
737 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
739 SyntheticPinchGestureParams params;
740 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
741 params.zoom_in = false;
742 params.total_num_pixels_covered = 456;
743 params.anchor.SetPoint(-12, 93);
745 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
746 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
747 FlushInputUntilComplete();
749 MockSyntheticPinchTouchTarget* pinch_target =
750 static_cast<MockSyntheticPinchTouchTarget*>(target_);
751 EXPECT_EQ(1, target_->num_success());
752 EXPECT_EQ(0, target_->num_failure());
753 EXPECT_EQ(pinch_target->zoom_direction(),
754 MockSyntheticPinchTouchTarget::ZOOM_OUT);
755 EXPECT_EQ(params.total_num_pixels_covered + 2 * target_->GetTouchSlopInDips(),
756 pinch_target->total_num_pixels_covered());
759 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZeroPixelsCovered) {
760 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
762 SyntheticPinchGestureParams params;
763 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
764 params.zoom_in = true;
765 params.total_num_pixels_covered = 0;
767 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
768 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
769 FlushInputUntilComplete();
771 MockSyntheticPinchTouchTarget* pinch_target =
772 static_cast<MockSyntheticPinchTouchTarget*>(target_);
773 EXPECT_EQ(1, target_->num_success());
774 EXPECT_EQ(0, target_->num_failure());
775 EXPECT_EQ(pinch_target->zoom_direction(),
776 MockSyntheticPinchTouchTarget::ZOOM_DIRECTION_UNKNOWN);
777 EXPECT_EQ(0, pinch_target->total_num_pixels_covered());
780 TEST_F(SyntheticGestureControllerTest, TapGestureTouch) {
781 CreateControllerAndTarget<MockSyntheticTapTouchTarget>();
783 SyntheticTapGestureParams params;
784 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
785 params.duration_ms = 123;
786 params.position.SetPoint(87, -124);
788 scoped_ptr<SyntheticTapGesture> gesture(new SyntheticTapGesture(params));
789 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
790 FlushInputUntilComplete();
792 MockSyntheticTapTouchTarget* tap_target =
793 static_cast<MockSyntheticTapTouchTarget*>(target_);
794 EXPECT_EQ(1, target_->num_success());
795 EXPECT_EQ(0, target_->num_failure());
796 EXPECT_TRUE(tap_target->GestureFinished());
797 EXPECT_EQ(tap_target->position(), params.position);
798 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms);
799 EXPECT_GE(GetTotalTime(),
800 base::TimeDelta::FromMilliseconds(params.duration_ms));
803 TEST_F(SyntheticGestureControllerTest, TapGestureMouse) {
804 CreateControllerAndTarget<MockSyntheticTapMouseTarget>();
806 SyntheticTapGestureParams params;
807 params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
808 params.duration_ms = 79;
809 params.position.SetPoint(98, 123);
811 scoped_ptr<SyntheticTapGesture> gesture(new SyntheticTapGesture(params));
812 controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
813 FlushInputUntilComplete();
815 MockSyntheticTapMouseTarget* tap_target =
816 static_cast<MockSyntheticTapMouseTarget*>(target_);
817 EXPECT_EQ(1, target_->num_success());
818 EXPECT_EQ(0, target_->num_failure());
819 EXPECT_TRUE(tap_target->GestureFinished());
820 EXPECT_EQ(tap_target->position(), params.position);
821 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms);
822 EXPECT_GE(GetTotalTime(),
823 base::TimeDelta::FromMilliseconds(params.duration_ms));
828 } // namespace content