c55b600f614b25f40b91ed60685abc82a217870d
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / synthetic_gesture_controller_unittest.cc
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.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"
26
27 using blink::WebInputEvent;
28 using blink::WebMouseEvent;
29 using blink::WebMouseWheelEvent;
30 using blink::WebTouchEvent;
31
32 namespace content {
33
34 namespace {
35
36 const int kFlushInputRateInMs = 16;
37 const int kPointerAssumedStoppedTimeMs = 43;
38 const int kTouchSlopInDips = 7;
39
40 class MockSyntheticGesture : public SyntheticGesture {
41  public:
42   MockSyntheticGesture(bool* finished, int num_steps)
43       : finished_(finished),
44         num_steps_(num_steps),
45         step_count_(0) {
46     *finished_ = false;
47   }
48   virtual ~MockSyntheticGesture() {}
49
50   virtual Result ForwardInputEvents(const base::TimeTicks& timestamp,
51                                     SyntheticGestureTarget* target) OVERRIDE {
52     step_count_++;
53     if (step_count_ == num_steps_) {
54       *finished_ = true;
55       return SyntheticGesture::GESTURE_FINISHED;
56     } else if (step_count_ > num_steps_) {
57       *finished_ = true;
58       // Return arbitrary failure.
59       return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED;
60     }
61
62     return SyntheticGesture::GESTURE_RUNNING;
63   }
64
65  protected:
66   bool* finished_;
67   int num_steps_;
68   int step_count_;
69 };
70
71 class MockSyntheticGestureTarget : public SyntheticGestureTarget {
72  public:
73   MockSyntheticGestureTarget()
74       : num_success_(0),
75         num_failure_(0),
76         flush_requested_(false),
77         pointer_assumed_stopped_time_ms_(kPointerAssumedStoppedTimeMs) {}
78   virtual ~MockSyntheticGestureTarget() {}
79
80   // SyntheticGestureTarget:
81   virtual void DispatchInputEventToPlatform(
82       const WebInputEvent& event) OVERRIDE {}
83
84   virtual void OnSyntheticGestureCompleted(
85       SyntheticGesture::Result result) OVERRIDE {
86     DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING);
87     if (result == SyntheticGesture::GESTURE_FINISHED)
88       num_success_++;
89     else
90       num_failure_++;
91   }
92
93   virtual void SetNeedsFlush() OVERRIDE {
94     flush_requested_ = true;
95   }
96
97   virtual SyntheticGestureParams::GestureSourceType
98   GetDefaultSyntheticGestureSourceType() const OVERRIDE {
99     return SyntheticGestureParams::TOUCH_INPUT;
100   }
101   virtual bool SupportsSyntheticGestureSourceType(
102       SyntheticGestureParams::GestureSourceType gesture_source_type)
103       const OVERRIDE {
104     return true;
105   }
106
107   virtual base::TimeDelta PointerAssumedStoppedTime() const OVERRIDE {
108     return base::TimeDelta::FromMilliseconds(pointer_assumed_stopped_time_ms_);
109   }
110
111   void set_pointer_assumed_stopped_time_ms(int time_ms) {
112     pointer_assumed_stopped_time_ms_ = time_ms;
113   }
114
115   virtual int GetTouchSlopInDips() const OVERRIDE {
116     return kTouchSlopInDips;
117   }
118
119   int num_success() const { return num_success_; }
120   int num_failure() const { return num_failure_; }
121
122   bool flush_requested() const { return flush_requested_; }
123   void ClearFlushRequest() { flush_requested_ = false; }
124
125  private:
126   int num_success_;
127   int num_failure_;
128
129   bool flush_requested_;
130
131   int pointer_assumed_stopped_time_ms_;
132 };
133
134 class MockSyntheticSmoothScrollGestureTarget
135     : public MockSyntheticGestureTarget {
136  public:
137   MockSyntheticSmoothScrollGestureTarget() {}
138   virtual ~MockSyntheticSmoothScrollGestureTarget() {}
139
140   gfx::Vector2dF scroll_distance() const { return scroll_distance_; }
141
142  protected:
143   gfx::Vector2dF scroll_distance_;
144 };
145
146 class MockSyntheticSmoothScrollMouseTarget
147     : public MockSyntheticSmoothScrollGestureTarget {
148  public:
149   MockSyntheticSmoothScrollMouseTarget() {}
150   virtual ~MockSyntheticSmoothScrollMouseTarget() {}
151
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);
159   }
160 };
161
162 class MockSyntheticSmoothScrollTouchTarget
163     : public MockSyntheticSmoothScrollGestureTarget {
164  public:
165   MockSyntheticSmoothScrollTouchTarget()
166       : started_(false) {}
167   virtual ~MockSyntheticSmoothScrollTouchTarget() {}
168
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);
174
175     if (!started_) {
176       ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart);
177       anchor_.SetPoint(touch_event.touches[0].position.x,
178                        touch_event.touches[0].position.y);
179       started_ = true;
180     } else {
181       ASSERT_NE(touch_event.type, WebInputEvent::TouchStart);
182       ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel);
183       // Ignore move events.
184
185       if (touch_event.type == WebInputEvent::TouchEnd)
186         scroll_distance_ =
187             anchor_ - gfx::PointF(touch_event.touches[0].position.x,
188                                   touch_event.touches[0].position.y);
189     }
190   }
191
192  protected:
193   gfx::Point anchor_;
194   bool started_;
195 };
196
197 class MockSyntheticPinchTouchTarget : public MockSyntheticGestureTarget {
198  public:
199   enum ZoomDirection {
200     ZOOM_DIRECTION_UNKNOWN,
201     ZOOM_IN,
202     ZOOM_OUT
203   };
204
205   MockSyntheticPinchTouchTarget()
206       : total_num_pixels_covered_(0),
207         last_pointer_distance_(0),
208         zoom_direction_(ZOOM_DIRECTION_UNKNOWN),
209         started_(false) {}
210   virtual ~MockSyntheticPinchTouchTarget() {}
211
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);
217
218     if (!started_) {
219       ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart);
220
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();
224
225       started_ = true;
226     } else {
227       ASSERT_NE(touch_event.type, WebInputEvent::TouchStart);
228       ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel);
229
230       gfx::PointF current_0 = gfx::Point(touch_event.touches[0].position);
231       gfx::PointF current_1 = gfx::Point(touch_event.touches[1].position);
232
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();
236
237       if (last_pointer_distance_ != pointer_distance) {
238         if (zoom_direction_ == ZOOM_DIRECTION_UNKNOWN)
239           zoom_direction_ =
240               ComputeZoomDirection(last_pointer_distance_, pointer_distance);
241         else
242           EXPECT_EQ(
243               zoom_direction_,
244               ComputeZoomDirection(last_pointer_distance_, pointer_distance));
245       }
246
247       last_pointer_distance_ = pointer_distance;
248     }
249   }
250
251   float total_num_pixels_covered() const { return total_num_pixels_covered_; }
252   ZoomDirection zoom_direction() const { return zoom_direction_; }
253
254  private:
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
259                                                             : ZOOM_OUT;
260   }
261
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_;
267   bool started_;
268 };
269
270 class MockSyntheticTapGestureTarget : public MockSyntheticGestureTarget {
271  public:
272   MockSyntheticTapGestureTarget() : state_(NOT_STARTED) {}
273   virtual ~MockSyntheticTapGestureTarget() {}
274
275   bool GestureFinished() const { return state_ == FINISHED; }
276   gfx::Point position() const { return position_; }
277   base::TimeDelta GetDuration() const { return stop_time_ - start_time_; }
278
279  protected:
280   enum GestureState {
281     NOT_STARTED,
282     STARTED,
283     FINISHED
284   };
285
286   gfx::Point position_;
287   base::TimeDelta start_time_;
288   base::TimeDelta stop_time_;
289   GestureState state_;
290 };
291
292 class MockSyntheticTapTouchTarget : public MockSyntheticTapGestureTarget {
293  public:
294   MockSyntheticTapTouchTarget() {}
295   virtual ~MockSyntheticTapTouchTarget() {}
296
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);
302
303     switch (state_) {
304       case NOT_STARTED:
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));
309         state_ = STARTED;
310         break;
311       case STARTED:
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));
316         state_ = FINISHED;
317         break;
318       case FINISHED:
319         EXPECT_FALSE(true);
320         break;
321     }
322   }
323 };
324
325 class MockSyntheticTapMouseTarget : public MockSyntheticTapGestureTarget {
326  public:
327   MockSyntheticTapMouseTarget() {}
328   virtual ~MockSyntheticTapMouseTarget() {}
329
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);
334
335     switch (state_) {
336       case NOT_STARTED:
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));
343         state_ = STARTED;
344         break;
345       case STARTED:
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));
352         state_ = FINISHED;
353         break;
354       case FINISHED:
355         EXPECT_FALSE(true);
356         break;
357     }
358   }
359 };
360
361 class SyntheticGestureControllerTest : public testing::Test {
362  public:
363   SyntheticGestureControllerTest() {}
364   virtual ~SyntheticGestureControllerTest() {}
365
366  protected:
367   template<typename MockGestureTarget>
368   void CreateControllerAndTarget() {
369     target_ = new MockGestureTarget();
370
371     controller_.reset(new SyntheticGestureController(
372         scoped_ptr<SyntheticGestureTarget>(target_)));
373   }
374
375   virtual void SetUp() OVERRIDE {
376     start_time_ = base::TimeTicks::Now();
377     time_ = start_time_;
378   }
379
380   virtual void TearDown() OVERRIDE {
381     controller_.reset();
382     target_ = NULL;
383     time_ = base::TimeTicks();
384   }
385
386   void FlushInputUntilComplete() {
387     while (target_->flush_requested()) {
388       target_->ClearFlushRequest();
389       time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs);
390       controller_->Flush(time_);
391     }
392   }
393
394   base::TimeDelta GetTotalTime() const { return time_ - start_time_; }
395
396   MockSyntheticGestureTarget* target_;
397   scoped_ptr<SyntheticGestureController> controller_;
398   base::TimeTicks start_time_;
399   base::TimeTicks time_;
400 };
401
402 TEST_F(SyntheticGestureControllerTest, SingleGesture) {
403   CreateControllerAndTarget<MockSyntheticGestureTarget>();
404
405   bool finished;
406   scoped_ptr<MockSyntheticGesture> gesture(
407       new MockSyntheticGesture(&finished, 3));
408   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
409   FlushInputUntilComplete();
410
411   EXPECT_TRUE(finished);
412   EXPECT_EQ(1, target_->num_success());
413   EXPECT_EQ(0, target_->num_failure());
414 }
415
416 TEST_F(SyntheticGestureControllerTest, GestureFailed) {
417   CreateControllerAndTarget<MockSyntheticGestureTarget>();
418
419   bool finished;
420   scoped_ptr<MockSyntheticGesture> gesture(
421       new MockSyntheticGesture(&finished, 0));
422   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
423   FlushInputUntilComplete();
424
425   EXPECT_TRUE(finished);
426   EXPECT_EQ(1, target_->num_failure());
427   EXPECT_EQ(0, target_->num_success());
428 }
429
430 TEST_F(SyntheticGestureControllerTest, SuccessiveGestures) {
431   CreateControllerAndTarget<MockSyntheticGestureTarget>();
432
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));
438
439   // Queue first gesture and wait for it to finish
440   controller_->QueueSyntheticGesture(gesture_1.PassAs<SyntheticGesture>());
441   FlushInputUntilComplete();
442
443   EXPECT_TRUE(finished_1);
444   EXPECT_EQ(1, target_->num_success());
445   EXPECT_EQ(0, target_->num_failure());
446
447   // Queue second gesture.
448   controller_->QueueSyntheticGesture(gesture_2.PassAs<SyntheticGesture>());
449   FlushInputUntilComplete();
450
451   EXPECT_TRUE(finished_2);
452   EXPECT_EQ(2, target_->num_success());
453   EXPECT_EQ(0, target_->num_failure());
454 }
455
456 TEST_F(SyntheticGestureControllerTest, TwoGesturesInFlight) {
457   CreateControllerAndTarget<MockSyntheticGestureTarget>();
458
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));
464
465   controller_->QueueSyntheticGesture(gesture_1.PassAs<SyntheticGesture>());
466   controller_->QueueSyntheticGesture(gesture_2.PassAs<SyntheticGesture>());
467   FlushInputUntilComplete();
468
469   EXPECT_TRUE(finished_1);
470   EXPECT_TRUE(finished_2);
471
472   EXPECT_EQ(2, target_->num_success());
473   EXPECT_EQ(0, target_->num_failure());
474 }
475
476 gfx::Vector2d AddTouchSlopToVector(const gfx::Vector2d& vector,
477                                    SyntheticGestureTarget* target) {
478   const int kTouchSlop = target->GetTouchSlopInDips();
479
480   int x = vector.x();
481   if (x > 0)
482     x += kTouchSlop;
483   else if (x < 0)
484     x -= kTouchSlop;
485
486   int y = vector.y();
487   if (y > 0)
488     y += kTouchSlop;
489   else if (y < 0)
490     y -= kTouchSlop;
491
492   return gfx::Vector2d(x, y);
493 }
494
495 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchVertical) {
496   CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
497
498   SyntheticSmoothScrollGestureParams params;
499   params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
500   params.distance = gfx::Vector2d(0, 123);
501   params.anchor.SetPoint(89, 32);
502
503   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
504       new SyntheticSmoothScrollGesture(params));
505   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
506   FlushInputUntilComplete();
507
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());
514 }
515
516 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchHorizontal) {
517   CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
518
519   SyntheticSmoothScrollGestureParams params;
520   params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
521   params.distance = gfx::Vector2d(-234, 0);
522   params.anchor.SetPoint(12, -23);
523
524   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
525       new SyntheticSmoothScrollGesture(params));
526   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
527   FlushInputUntilComplete();
528
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());
535 }
536
537 void CheckIsWithinRange(float scroll_distance,
538                         int target_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());
543   } else {
544     EXPECT_GE(target_distance, scroll_distance);
545     EXPECT_GE(scroll_distance, target_distance - target->GetTouchSlopInDips());
546   }
547 }
548
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);
554 }
555
556 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchDiagonal) {
557   CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
558
559   SyntheticSmoothScrollGestureParams params;
560   params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
561   params.distance = gfx::Vector2d(413, -83);
562   params.anchor.SetPoint(0, 7);
563
564   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
565       new SyntheticSmoothScrollGesture(params));
566   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
567   FlushInputUntilComplete();
568
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_);
575 }
576
577 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchLongStop) {
578   CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
579
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);
588
589   target_->set_pointer_assumed_stopped_time_ms(543);
590
591   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
592       new SyntheticSmoothScrollGesture(params));
593   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
594   FlushInputUntilComplete();
595
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());
603 }
604
605 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchFling) {
606   CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
607
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);
616
617   target_->set_pointer_assumed_stopped_time_ms(543);
618
619   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
620       new SyntheticSmoothScrollGesture(params));
621   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
622   FlushInputUntilComplete();
623
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());
631 }
632
633 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureTouchZeroDistance) {
634   CreateControllerAndTarget<MockSyntheticSmoothScrollTouchTarget>();
635
636   SyntheticSmoothScrollGestureParams params;
637   params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
638   params.distance = gfx::Vector2d(0, 0);
639   params.anchor.SetPoint(-32, 43);
640
641   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
642       new SyntheticSmoothScrollGesture(params));
643   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
644   FlushInputUntilComplete();
645
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());
651 }
652
653 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureMouseVertical) {
654   CreateControllerAndTarget<MockSyntheticSmoothScrollMouseTarget>();
655
656   SyntheticSmoothScrollGestureParams params;
657   params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
658   params.distance = gfx::Vector2d(0, -234);
659   params.anchor.SetPoint(432, 89);
660
661   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
662       new SyntheticSmoothScrollGesture(params));
663   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
664   FlushInputUntilComplete();
665
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());
671 }
672
673 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureMouseHorizontal) {
674   CreateControllerAndTarget<MockSyntheticSmoothScrollMouseTarget>();
675
676   SyntheticSmoothScrollGestureParams params;
677   params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
678   params.distance = gfx::Vector2d(345, 0);
679   params.anchor.SetPoint(90, 12);
680
681   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
682       new SyntheticSmoothScrollGesture(params));
683   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
684   FlushInputUntilComplete();
685
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());
691 }
692
693 TEST_F(SyntheticGestureControllerTest, SmoothScrollGestureMouseDiagonal) {
694   CreateControllerAndTarget<MockSyntheticSmoothScrollMouseTarget>();
695
696   SyntheticSmoothScrollGestureParams params;
697   params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
698   params.distance = gfx::Vector2d(-194, 303);
699   params.anchor.SetPoint(90, 12);
700
701   scoped_ptr<SyntheticSmoothScrollGesture> gesture(
702       new SyntheticSmoothScrollGesture(params));
703   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
704   FlushInputUntilComplete();
705
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());
711 }
712
713 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomIn) {
714   CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
715
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);
721
722   scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
723   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
724   FlushInputUntilComplete();
725
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());
734 }
735
736 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomOut) {
737   CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
738
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);
744
745   scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
746   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
747   FlushInputUntilComplete();
748
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());
757 }
758
759 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZeroPixelsCovered) {
760   CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
761
762   SyntheticPinchGestureParams params;
763   params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
764   params.zoom_in = true;
765   params.total_num_pixels_covered = 0;
766
767   scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
768   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
769   FlushInputUntilComplete();
770
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());
778 }
779
780 TEST_F(SyntheticGestureControllerTest, TapGestureTouch) {
781   CreateControllerAndTarget<MockSyntheticTapTouchTarget>();
782
783   SyntheticTapGestureParams params;
784   params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
785   params.duration_ms = 123;
786   params.position.SetPoint(87, -124);
787
788   scoped_ptr<SyntheticTapGesture> gesture(new SyntheticTapGesture(params));
789   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
790   FlushInputUntilComplete();
791
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));
801 }
802
803 TEST_F(SyntheticGestureControllerTest, TapGestureMouse) {
804   CreateControllerAndTarget<MockSyntheticTapMouseTarget>();
805
806   SyntheticTapGestureParams params;
807   params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT;
808   params.duration_ms = 79;
809   params.position.SetPoint(98, 123);
810
811   scoped_ptr<SyntheticTapGesture> gesture(new SyntheticTapGesture(params));
812   controller_->QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
813   FlushInputUntilComplete();
814
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));
824 }
825
826 }  // namespace
827
828 }  // namespace content