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/input_event.h"
15 #include "content/common/input/synthetic_pinch_gesture_params.h"
16 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
17 #include "content/common/input/synthetic_tap_gesture_params.h"
18 #include "content/public/test/mock_render_process_host.h"
19 #include "content/public/test/test_browser_context.h"
20 #include "content/test/test_render_view_host.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/WebKit/public/web/WebInputEvent.h"
23 #include "ui/gfx/point.h"
24 #include "ui/gfx/point_f.h"
25 #include "ui/gfx/vector2d.h"
26 #include "ui/gfx/vector2d_f.h"
32 const int kFlushInputRateInMs
= 16;
33 const int kPointerAssumedStoppedTimeMs
= 43;
34 const int kTouchSlopInDips
= 7;
36 class MockSyntheticGesture
: public SyntheticGesture
{
38 MockSyntheticGesture(bool* finished
, int num_steps
)
39 : finished_(finished
),
40 num_steps_(num_steps
),
44 virtual ~MockSyntheticGesture() {}
46 virtual Result
ForwardInputEvents(const base::TimeTicks
& timestamp
,
47 SyntheticGestureTarget
* target
) OVERRIDE
{
49 if (step_count_
== num_steps_
) {
51 return SyntheticGesture::GESTURE_FINISHED
;
52 } else if (step_count_
> num_steps_
) {
54 // Return arbitrary failure.
55 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED
;
58 return SyntheticGesture::GESTURE_RUNNING
;
67 class MockSyntheticGestureTarget
: public SyntheticGestureTarget
{
69 MockSyntheticGestureTarget()
72 flush_requested_(false),
73 pointer_assumed_stopped_time_ms_(kPointerAssumedStoppedTimeMs
) {}
74 virtual ~MockSyntheticGestureTarget() {}
76 // SyntheticGestureTarget:
77 virtual void DispatchInputEventToPlatform(const InputEvent
& event
) OVERRIDE
{}
79 virtual void OnSyntheticGestureCompleted(
80 SyntheticGesture::Result result
) OVERRIDE
{
81 DCHECK_NE(result
, SyntheticGesture::GESTURE_RUNNING
);
82 if (result
== SyntheticGesture::GESTURE_FINISHED
)
88 virtual void SetNeedsFlush() OVERRIDE
{
89 flush_requested_
= true;
92 virtual SyntheticGestureParams::GestureSourceType
93 GetDefaultSyntheticGestureSourceType() const OVERRIDE
{
94 return SyntheticGestureParams::TOUCH_INPUT
;
96 virtual bool SupportsSyntheticGestureSourceType(
97 SyntheticGestureParams::GestureSourceType gesture_source_type
)
102 virtual base::TimeDelta
PointerAssumedStoppedTime() const OVERRIDE
{
103 return base::TimeDelta::FromMilliseconds(pointer_assumed_stopped_time_ms_
);
106 void set_pointer_assumed_stopped_time_ms(int time_ms
) {
107 pointer_assumed_stopped_time_ms_
= time_ms
;
110 virtual int GetTouchSlopInDips() const OVERRIDE
{
111 return kTouchSlopInDips
;
114 int num_success() const { return num_success_
; }
115 int num_failure() const { return num_failure_
; }
117 bool flush_requested() const { return flush_requested_
; }
118 void ClearFlushRequest() { flush_requested_
= false; }
124 bool flush_requested_
;
126 int pointer_assumed_stopped_time_ms_
;
129 class MockSyntheticSmoothScrollGestureTarget
130 : public MockSyntheticGestureTarget
{
132 MockSyntheticSmoothScrollGestureTarget() {}
133 virtual ~MockSyntheticSmoothScrollGestureTarget() {}
135 gfx::Vector2dF
scroll_distance() const { return scroll_distance_
; }
138 gfx::Vector2dF scroll_distance_
;
141 class MockSyntheticSmoothScrollMouseTarget
142 : public MockSyntheticSmoothScrollGestureTarget
{
144 MockSyntheticSmoothScrollMouseTarget() {}
145 virtual ~MockSyntheticSmoothScrollMouseTarget() {}
147 virtual void DispatchInputEventToPlatform(const InputEvent
& event
) OVERRIDE
{
148 const blink::WebInputEvent
* web_event
= event
.web_event
.get();
149 ASSERT_EQ(web_event
->type
, blink::WebInputEvent::MouseWheel
);
150 const blink::WebMouseWheelEvent
* mouse_wheel_event
=
151 static_cast<const blink::WebMouseWheelEvent
*>(web_event
);
152 scroll_distance_
-= gfx::Vector2dF(mouse_wheel_event
->deltaX
,
153 mouse_wheel_event
->deltaY
);
157 class MockSyntheticSmoothScrollTouchTarget
158 : public MockSyntheticSmoothScrollGestureTarget
{
160 MockSyntheticSmoothScrollTouchTarget()
162 virtual ~MockSyntheticSmoothScrollTouchTarget() {}
164 virtual void DispatchInputEventToPlatform(const InputEvent
& event
) OVERRIDE
{
165 const blink::WebInputEvent
* web_event
= event
.web_event
.get();
166 ASSERT_TRUE(blink::WebInputEvent::isTouchEventType(web_event
->type
));
167 const blink::WebTouchEvent
* touch_event
=
168 static_cast<const blink::WebTouchEvent
*>(web_event
);
169 ASSERT_EQ(touch_event
->touchesLength
, (unsigned int)1);
172 ASSERT_EQ(touch_event
->type
, blink::WebInputEvent::TouchStart
);
173 anchor_
.SetPoint(touch_event
->touches
[0].position
.x
,
174 touch_event
->touches
[0].position
.y
);
177 ASSERT_NE(touch_event
->type
, blink::WebInputEvent::TouchStart
);
178 ASSERT_NE(touch_event
->type
, blink::WebInputEvent::TouchCancel
);
179 // Ignore move events.
181 if (touch_event
->type
== blink::WebInputEvent::TouchEnd
)
183 anchor_
- gfx::PointF(touch_event
->touches
[0].position
.x
,
184 touch_event
->touches
[0].position
.y
);
193 class MockSyntheticPinchTouchTarget
: public MockSyntheticGestureTarget
{
196 ZOOM_DIRECTION_UNKNOWN
,
201 MockSyntheticPinchTouchTarget()
202 : total_num_pixels_covered_(0),
203 last_pointer_distance_(0),
204 zoom_direction_(ZOOM_DIRECTION_UNKNOWN
),
206 virtual ~MockSyntheticPinchTouchTarget() {}
208 virtual void DispatchInputEventToPlatform(const InputEvent
& event
) OVERRIDE
{
209 const blink::WebInputEvent
* web_event
= event
.web_event
.get();
210 ASSERT_TRUE(blink::WebInputEvent::isTouchEventType(web_event
->type
));
211 const blink::WebTouchEvent
* touch_event
=
212 static_cast<const blink::WebTouchEvent
*>(web_event
);
213 ASSERT_EQ(touch_event
->touchesLength
, (unsigned int)2);
216 ASSERT_EQ(touch_event
->type
, blink::WebInputEvent::TouchStart
);
218 start_0_
= gfx::Point(touch_event
->touches
[0].position
);
219 start_1_
= gfx::Point(touch_event
->touches
[1].position
);
220 last_pointer_distance_
= (start_0_
- start_1_
).Length();
224 ASSERT_NE(touch_event
->type
, blink::WebInputEvent::TouchStart
);
225 ASSERT_NE(touch_event
->type
, blink::WebInputEvent::TouchCancel
);
227 gfx::PointF current_0
= gfx::Point(touch_event
->touches
[0].position
);
228 gfx::PointF current_1
= gfx::Point(touch_event
->touches
[1].position
);
230 total_num_pixels_covered_
=
231 (current_0
- start_0_
).Length() + (current_1
- start_1_
).Length();
232 float pointer_distance
= (current_0
- current_1
).Length();
234 if (last_pointer_distance_
!= pointer_distance
) {
235 if (zoom_direction_
== ZOOM_DIRECTION_UNKNOWN
)
237 ComputeZoomDirection(last_pointer_distance_
, pointer_distance
);
241 ComputeZoomDirection(last_pointer_distance_
, pointer_distance
));
244 last_pointer_distance_
= pointer_distance
;
248 float total_num_pixels_covered() const { return total_num_pixels_covered_
; }
249 ZoomDirection
zoom_direction() const { return zoom_direction_
; }
252 ZoomDirection
ComputeZoomDirection(float last_pointer_distance
,
253 float current_pointer_distance
) {
254 DCHECK_NE(last_pointer_distance
, current_pointer_distance
);
255 return last_pointer_distance
< current_pointer_distance
? ZOOM_IN
259 float total_num_pixels_covered_
;
260 float last_pointer_distance_
;
261 ZoomDirection zoom_direction_
;
262 gfx::PointF start_0_
;
263 gfx::PointF start_1_
;
267 class MockSyntheticTapGestureTarget
: public MockSyntheticGestureTarget
{
269 MockSyntheticTapGestureTarget() : state_(NOT_STARTED
) {}
270 virtual ~MockSyntheticTapGestureTarget() {}
272 bool GestureFinished() const { return state_
== FINISHED
; }
273 gfx::Point
position() const { return position_
; }
274 base::TimeDelta
GetDuration() const { return stop_time_
- start_time_
; }
283 gfx::Point position_
;
284 base::TimeDelta start_time_
;
285 base::TimeDelta stop_time_
;
289 class MockSyntheticTapTouchTarget
: public MockSyntheticTapGestureTarget
{
291 MockSyntheticTapTouchTarget() {}
292 virtual ~MockSyntheticTapTouchTarget() {}
294 virtual void DispatchInputEventToPlatform(const InputEvent
& event
) OVERRIDE
{
295 const blink::WebInputEvent
* web_event
= event
.web_event
.get();
296 ASSERT_TRUE(blink::WebInputEvent::isTouchEventType(web_event
->type
));
297 const blink::WebTouchEvent
* touch_event
=
298 static_cast<const blink::WebTouchEvent
*>(web_event
);
299 ASSERT_EQ(touch_event
->touchesLength
, (unsigned int)1);
303 EXPECT_EQ(touch_event
->type
, blink::WebInputEvent::TouchStart
);
304 position_
= gfx::Point(touch_event
->touches
[0].position
);
305 start_time_
= base::TimeDelta::FromMilliseconds(
306 static_cast<int64
>(touch_event
->timeStampSeconds
* 1000));
310 EXPECT_EQ(touch_event
->type
, blink::WebInputEvent::TouchEnd
);
311 EXPECT_EQ(position_
, gfx::Point(touch_event
->touches
[0].position
));
312 stop_time_
= base::TimeDelta::FromMilliseconds(
313 static_cast<int64
>(touch_event
->timeStampSeconds
* 1000));
323 class MockSyntheticTapMouseTarget
: public MockSyntheticTapGestureTarget
{
325 MockSyntheticTapMouseTarget() {}
326 virtual ~MockSyntheticTapMouseTarget() {}
328 virtual void DispatchInputEventToPlatform(const InputEvent
& event
) OVERRIDE
{
329 const blink::WebInputEvent
* web_event
= event
.web_event
.get();
330 ASSERT_TRUE(blink::WebInputEvent::isMouseEventType(web_event
->type
));
331 const blink::WebMouseEvent
* mouse_event
=
332 static_cast<const blink::WebMouseEvent
*>(web_event
);
336 EXPECT_EQ(mouse_event
->type
, blink::WebInputEvent::MouseDown
);
337 EXPECT_EQ(mouse_event
->button
, blink::WebMouseEvent::ButtonLeft
);
338 EXPECT_EQ(mouse_event
->clickCount
, 1);
339 position_
= gfx::Point(mouse_event
->x
, mouse_event
->y
);
340 start_time_
= base::TimeDelta::FromMilliseconds(
341 static_cast<int64
>(mouse_event
->timeStampSeconds
* 1000));
345 EXPECT_EQ(mouse_event
->type
, blink::WebInputEvent::MouseUp
);
346 EXPECT_EQ(mouse_event
->button
, blink::WebMouseEvent::ButtonLeft
);
347 EXPECT_EQ(mouse_event
->clickCount
, 1);
348 EXPECT_EQ(position_
, gfx::Point(mouse_event
->x
, mouse_event
->y
));
349 stop_time_
= base::TimeDelta::FromMilliseconds(
350 static_cast<int64
>(mouse_event
->timeStampSeconds
* 1000));
360 class SyntheticGestureControllerTest
: public testing::Test
{
362 SyntheticGestureControllerTest() {}
363 virtual ~SyntheticGestureControllerTest() {}
366 template<typename MockGestureTarget
>
367 void CreateControllerAndTarget() {
368 target_
= new MockGestureTarget();
370 controller_
.reset(new SyntheticGestureController(
371 scoped_ptr
<SyntheticGestureTarget
>(target_
)));
374 virtual void SetUp() OVERRIDE
{
375 start_time_
= base::TimeTicks::Now();
379 virtual void TearDown() OVERRIDE
{
382 time_
= base::TimeTicks();
385 void FlushInputUntilComplete() {
386 while (target_
->flush_requested()) {
387 target_
->ClearFlushRequest();
388 time_
+= base::TimeDelta::FromMilliseconds(kFlushInputRateInMs
);
389 controller_
->Flush(time_
);
393 base::TimeDelta
GetTotalTime() const { return time_
- start_time_
; }
395 MockSyntheticGestureTarget
* target_
;
396 scoped_ptr
<SyntheticGestureController
> controller_
;
397 base::TimeTicks start_time_
;
398 base::TimeTicks time_
;
401 TEST_F(SyntheticGestureControllerTest
, SingleGesture
) {
402 CreateControllerAndTarget
<MockSyntheticGestureTarget
>();
405 scoped_ptr
<MockSyntheticGesture
> gesture(
406 new MockSyntheticGesture(&finished
, 3));
407 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
408 FlushInputUntilComplete();
410 EXPECT_TRUE(finished
);
411 EXPECT_EQ(1, target_
->num_success());
412 EXPECT_EQ(0, target_
->num_failure());
415 TEST_F(SyntheticGestureControllerTest
, GestureFailed
) {
416 CreateControllerAndTarget
<MockSyntheticGestureTarget
>();
419 scoped_ptr
<MockSyntheticGesture
> gesture(
420 new MockSyntheticGesture(&finished
, 0));
421 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
422 FlushInputUntilComplete();
424 EXPECT_TRUE(finished
);
425 EXPECT_EQ(1, target_
->num_failure());
426 EXPECT_EQ(0, target_
->num_success());
429 TEST_F(SyntheticGestureControllerTest
, SuccessiveGestures
) {
430 CreateControllerAndTarget
<MockSyntheticGestureTarget
>();
432 bool finished_1
, finished_2
;
433 scoped_ptr
<MockSyntheticGesture
> gesture_1(
434 new MockSyntheticGesture(&finished_1
, 2));
435 scoped_ptr
<MockSyntheticGesture
> gesture_2(
436 new MockSyntheticGesture(&finished_2
, 4));
438 // Queue first gesture and wait for it to finish
439 controller_
->QueueSyntheticGesture(gesture_1
.PassAs
<SyntheticGesture
>());
440 FlushInputUntilComplete();
442 EXPECT_TRUE(finished_1
);
443 EXPECT_EQ(1, target_
->num_success());
444 EXPECT_EQ(0, target_
->num_failure());
446 // Queue second gesture.
447 controller_
->QueueSyntheticGesture(gesture_2
.PassAs
<SyntheticGesture
>());
448 FlushInputUntilComplete();
450 EXPECT_TRUE(finished_2
);
451 EXPECT_EQ(2, target_
->num_success());
452 EXPECT_EQ(0, target_
->num_failure());
455 TEST_F(SyntheticGestureControllerTest
, TwoGesturesInFlight
) {
456 CreateControllerAndTarget
<MockSyntheticGestureTarget
>();
458 bool finished_1
, finished_2
;
459 scoped_ptr
<MockSyntheticGesture
> gesture_1(
460 new MockSyntheticGesture(&finished_1
, 2));
461 scoped_ptr
<MockSyntheticGesture
> gesture_2(
462 new MockSyntheticGesture(&finished_2
, 4));
464 controller_
->QueueSyntheticGesture(gesture_1
.PassAs
<SyntheticGesture
>());
465 controller_
->QueueSyntheticGesture(gesture_2
.PassAs
<SyntheticGesture
>());
466 FlushInputUntilComplete();
468 EXPECT_TRUE(finished_1
);
469 EXPECT_TRUE(finished_2
);
471 EXPECT_EQ(2, target_
->num_success());
472 EXPECT_EQ(0, target_
->num_failure());
475 gfx::Vector2d
AddTouchSlopToVector(const gfx::Vector2d
& vector
,
476 SyntheticGestureTarget
* target
) {
477 const int kTouchSlop
= target
->GetTouchSlopInDips();
491 return gfx::Vector2d(x
, y
);
494 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureTouchVertical
) {
495 CreateControllerAndTarget
<MockSyntheticSmoothScrollTouchTarget
>();
497 SyntheticSmoothScrollGestureParams params
;
498 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
499 params
.distance
= gfx::Vector2d(0, 123);
500 params
.anchor
.SetPoint(89, 32);
502 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
503 new SyntheticSmoothScrollGesture(params
));
504 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
505 FlushInputUntilComplete();
507 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
508 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
509 EXPECT_EQ(1, target_
->num_success());
510 EXPECT_EQ(0, target_
->num_failure());
511 EXPECT_EQ(AddTouchSlopToVector(params
.distance
, target_
),
512 smooth_scroll_target
->scroll_distance());
515 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureTouchHorizontal
) {
516 CreateControllerAndTarget
<MockSyntheticSmoothScrollTouchTarget
>();
518 SyntheticSmoothScrollGestureParams params
;
519 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
520 params
.distance
= gfx::Vector2d(-234, 0);
521 params
.anchor
.SetPoint(12, -23);
523 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
524 new SyntheticSmoothScrollGesture(params
));
525 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
526 FlushInputUntilComplete();
528 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
529 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
530 EXPECT_EQ(1, target_
->num_success());
531 EXPECT_EQ(0, target_
->num_failure());
532 EXPECT_EQ(AddTouchSlopToVector(params
.distance
, target_
),
533 smooth_scroll_target
->scroll_distance());
536 void CheckIsWithinRange(float scroll_distance
,
538 SyntheticGestureTarget
* target
) {
539 if (target_distance
> 0) {
540 EXPECT_LE(target_distance
, scroll_distance
);
541 EXPECT_LE(scroll_distance
, target_distance
+ target
->GetTouchSlopInDips());
543 EXPECT_GE(target_distance
, scroll_distance
);
544 EXPECT_GE(scroll_distance
, target_distance
- target
->GetTouchSlopInDips());
548 void CheckScrollDistanceIsWithinRange(const gfx::Vector2dF
& scroll_distance
,
549 const gfx::Vector2d
& target_distance
,
550 SyntheticGestureTarget
* target
) {
551 CheckIsWithinRange(scroll_distance
.x(), target_distance
.x(), target
);
552 CheckIsWithinRange(scroll_distance
.y(), target_distance
.y(), target
);
555 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureTouchDiagonal
) {
556 CreateControllerAndTarget
<MockSyntheticSmoothScrollTouchTarget
>();
558 SyntheticSmoothScrollGestureParams params
;
559 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
560 params
.distance
= gfx::Vector2d(413, -83);
561 params
.anchor
.SetPoint(0, 7);
563 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
564 new SyntheticSmoothScrollGesture(params
));
565 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
566 FlushInputUntilComplete();
568 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
569 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
570 EXPECT_EQ(1, target_
->num_success());
571 EXPECT_EQ(0, target_
->num_failure());
572 CheckScrollDistanceIsWithinRange(
573 smooth_scroll_target
->scroll_distance(), params
.distance
, target_
);
576 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureTouchLongStop
) {
577 CreateControllerAndTarget
<MockSyntheticSmoothScrollTouchTarget
>();
579 // Create a smooth scroll with a short distance and set the pointer assumed
580 // stopped time high, so that the stopping should dominate the time the
581 // gesture is active.
582 SyntheticSmoothScrollGestureParams params
;
583 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
584 params
.distance
= gfx::Vector2d(21, -12);
585 params
.prevent_fling
= true;
586 params
.anchor
.SetPoint(-98, -23);
588 target_
->set_pointer_assumed_stopped_time_ms(543);
590 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
591 new SyntheticSmoothScrollGesture(params
));
592 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
593 FlushInputUntilComplete();
595 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
596 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
597 EXPECT_EQ(1, target_
->num_success());
598 EXPECT_EQ(0, target_
->num_failure());
599 CheckScrollDistanceIsWithinRange(
600 smooth_scroll_target
->scroll_distance(), params
.distance
, target_
);
601 EXPECT_GE(GetTotalTime(), target_
->PointerAssumedStoppedTime());
604 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureTouchFling
) {
605 CreateControllerAndTarget
<MockSyntheticSmoothScrollTouchTarget
>();
607 // Create a smooth scroll with a short distance and set the pointer assumed
608 // stopped time high. Disable 'prevent_fling' and check that the gesture
609 // finishes without waiting before it stops.
610 SyntheticSmoothScrollGestureParams params
;
611 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
612 params
.distance
= gfx::Vector2d(-43, 19);
613 params
.prevent_fling
= false;
614 params
.anchor
.SetPoint(-89, 78);
616 target_
->set_pointer_assumed_stopped_time_ms(543);
618 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
619 new SyntheticSmoothScrollGesture(params
));
620 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
621 FlushInputUntilComplete();
623 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
624 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
625 EXPECT_EQ(1, target_
->num_success());
626 EXPECT_EQ(0, target_
->num_failure());
627 CheckScrollDistanceIsWithinRange(
628 smooth_scroll_target
->scroll_distance(), params
.distance
, target_
);
629 EXPECT_LE(GetTotalTime(), target_
->PointerAssumedStoppedTime());
632 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureTouchZeroDistance
) {
633 CreateControllerAndTarget
<MockSyntheticSmoothScrollTouchTarget
>();
635 SyntheticSmoothScrollGestureParams params
;
636 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
637 params
.distance
= gfx::Vector2d(0, 0);
638 params
.anchor
.SetPoint(-32, 43);
640 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
641 new SyntheticSmoothScrollGesture(params
));
642 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
643 FlushInputUntilComplete();
645 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
646 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
647 EXPECT_EQ(1, target_
->num_success());
648 EXPECT_EQ(0, target_
->num_failure());
649 EXPECT_EQ(gfx::Vector2dF(0, 0), smooth_scroll_target
->scroll_distance());
652 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureMouseVertical
) {
653 CreateControllerAndTarget
<MockSyntheticSmoothScrollMouseTarget
>();
655 SyntheticSmoothScrollGestureParams params
;
656 params
.gesture_source_type
= SyntheticGestureParams::MOUSE_INPUT
;
657 params
.distance
= gfx::Vector2d(0, -234);
658 params
.anchor
.SetPoint(432, 89);
660 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
661 new SyntheticSmoothScrollGesture(params
));
662 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
663 FlushInputUntilComplete();
665 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
666 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
667 EXPECT_EQ(1, target_
->num_success());
668 EXPECT_EQ(0, target_
->num_failure());
669 EXPECT_EQ(params
.distance
, smooth_scroll_target
->scroll_distance());
672 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureMouseHorizontal
) {
673 CreateControllerAndTarget
<MockSyntheticSmoothScrollMouseTarget
>();
675 SyntheticSmoothScrollGestureParams params
;
676 params
.gesture_source_type
= SyntheticGestureParams::MOUSE_INPUT
;
677 params
.distance
= gfx::Vector2d(345, 0);
678 params
.anchor
.SetPoint(90, 12);
680 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
681 new SyntheticSmoothScrollGesture(params
));
682 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
683 FlushInputUntilComplete();
685 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
686 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
687 EXPECT_EQ(1, target_
->num_success());
688 EXPECT_EQ(0, target_
->num_failure());
689 EXPECT_EQ(params
.distance
, smooth_scroll_target
->scroll_distance());
692 TEST_F(SyntheticGestureControllerTest
, SmoothScrollGestureMouseDiagonal
) {
693 CreateControllerAndTarget
<MockSyntheticSmoothScrollMouseTarget
>();
695 SyntheticSmoothScrollGestureParams params
;
696 params
.gesture_source_type
= SyntheticGestureParams::MOUSE_INPUT
;
697 params
.distance
= gfx::Vector2d(-194, 303);
698 params
.anchor
.SetPoint(90, 12);
700 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
701 new SyntheticSmoothScrollGesture(params
));
702 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
703 FlushInputUntilComplete();
705 MockSyntheticSmoothScrollGestureTarget
* smooth_scroll_target
=
706 static_cast<MockSyntheticSmoothScrollGestureTarget
*>(target_
);
707 EXPECT_EQ(1, target_
->num_success());
708 EXPECT_EQ(0, target_
->num_failure());
709 EXPECT_EQ(params
.distance
, smooth_scroll_target
->scroll_distance());
712 TEST_F(SyntheticGestureControllerTest
, PinchGestureTouchZoomIn
) {
713 CreateControllerAndTarget
<MockSyntheticPinchTouchTarget
>();
715 SyntheticPinchGestureParams params
;
716 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
717 params
.zoom_in
= true;
718 params
.total_num_pixels_covered
= 345;
719 params
.anchor
.SetPoint(54, 89);
721 scoped_ptr
<SyntheticPinchGesture
> gesture(new SyntheticPinchGesture(params
));
722 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
723 FlushInputUntilComplete();
725 MockSyntheticPinchTouchTarget
* pinch_target
=
726 static_cast<MockSyntheticPinchTouchTarget
*>(target_
);
727 EXPECT_EQ(1, target_
->num_success());
728 EXPECT_EQ(0, target_
->num_failure());
729 EXPECT_EQ(pinch_target
->zoom_direction(),
730 MockSyntheticPinchTouchTarget::ZOOM_IN
);
731 EXPECT_EQ(params
.total_num_pixels_covered
+ 2 * target_
->GetTouchSlopInDips(),
732 pinch_target
->total_num_pixels_covered());
735 TEST_F(SyntheticGestureControllerTest
, PinchGestureTouchZoomOut
) {
736 CreateControllerAndTarget
<MockSyntheticPinchTouchTarget
>();
738 SyntheticPinchGestureParams params
;
739 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
740 params
.zoom_in
= false;
741 params
.total_num_pixels_covered
= 456;
742 params
.anchor
.SetPoint(-12, 93);
744 scoped_ptr
<SyntheticPinchGesture
> gesture(new SyntheticPinchGesture(params
));
745 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
746 FlushInputUntilComplete();
748 MockSyntheticPinchTouchTarget
* pinch_target
=
749 static_cast<MockSyntheticPinchTouchTarget
*>(target_
);
750 EXPECT_EQ(1, target_
->num_success());
751 EXPECT_EQ(0, target_
->num_failure());
752 EXPECT_EQ(pinch_target
->zoom_direction(),
753 MockSyntheticPinchTouchTarget::ZOOM_OUT
);
754 EXPECT_EQ(params
.total_num_pixels_covered
+ 2 * target_
->GetTouchSlopInDips(),
755 pinch_target
->total_num_pixels_covered());
758 TEST_F(SyntheticGestureControllerTest
, PinchGestureTouchZeroPixelsCovered
) {
759 CreateControllerAndTarget
<MockSyntheticPinchTouchTarget
>();
761 SyntheticPinchGestureParams params
;
762 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
763 params
.zoom_in
= true;
764 params
.total_num_pixels_covered
= 0;
766 scoped_ptr
<SyntheticPinchGesture
> gesture(new SyntheticPinchGesture(params
));
767 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
768 FlushInputUntilComplete();
770 MockSyntheticPinchTouchTarget
* pinch_target
=
771 static_cast<MockSyntheticPinchTouchTarget
*>(target_
);
772 EXPECT_EQ(1, target_
->num_success());
773 EXPECT_EQ(0, target_
->num_failure());
774 EXPECT_EQ(pinch_target
->zoom_direction(),
775 MockSyntheticPinchTouchTarget::ZOOM_DIRECTION_UNKNOWN
);
776 EXPECT_EQ(0, pinch_target
->total_num_pixels_covered());
779 TEST_F(SyntheticGestureControllerTest
, TapGestureTouch
) {
780 CreateControllerAndTarget
<MockSyntheticTapTouchTarget
>();
782 SyntheticTapGestureParams params
;
783 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
784 params
.duration_ms
= 123;
785 params
.position
.SetPoint(87, -124);
787 scoped_ptr
<SyntheticTapGesture
> gesture(new SyntheticTapGesture(params
));
788 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
789 FlushInputUntilComplete();
791 MockSyntheticTapTouchTarget
* tap_target
=
792 static_cast<MockSyntheticTapTouchTarget
*>(target_
);
793 EXPECT_EQ(1, target_
->num_success());
794 EXPECT_EQ(0, target_
->num_failure());
795 EXPECT_TRUE(tap_target
->GestureFinished());
796 EXPECT_EQ(tap_target
->position(), params
.position
);
797 EXPECT_EQ(tap_target
->GetDuration().InMilliseconds(), params
.duration_ms
);
798 EXPECT_GE(GetTotalTime(),
799 base::TimeDelta::FromMilliseconds(params
.duration_ms
));
802 TEST_F(SyntheticGestureControllerTest
, TapGestureMouse
) {
803 CreateControllerAndTarget
<MockSyntheticTapMouseTarget
>();
805 SyntheticTapGestureParams params
;
806 params
.gesture_source_type
= SyntheticGestureParams::MOUSE_INPUT
;
807 params
.duration_ms
= 79;
808 params
.position
.SetPoint(98, 123);
810 scoped_ptr
<SyntheticTapGesture
> gesture(new SyntheticTapGesture(params
));
811 controller_
->QueueSyntheticGesture(gesture
.PassAs
<SyntheticGesture
>());
812 FlushInputUntilComplete();
814 MockSyntheticTapMouseTarget
* tap_target
=
815 static_cast<MockSyntheticTapMouseTarget
*>(target_
);
816 EXPECT_EQ(1, target_
->num_success());
817 EXPECT_EQ(0, target_
->num_failure());
818 EXPECT_TRUE(tap_target
->GestureFinished());
819 EXPECT_EQ(tap_target
->position(), params
.position
);
820 EXPECT_EQ(tap_target
->GetDuration().InMilliseconds(), params
.duration_ms
);
821 EXPECT_GE(GetTotalTime(),
822 base::TimeDelta::FromMilliseconds(params
.duration_ms
));
827 } // namespace content