Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ui / events / gesture_detection / gesture_provider_unittest.cc
1 // Copyright 2014 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/basictypes.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/events/event_constants.h"
12 #include "ui/events/gesture_detection/gesture_event_data.h"
13 #include "ui/events/gesture_detection/gesture_provider.h"
14 #include "ui/events/gesture_detection/mock_motion_event.h"
15 #include "ui/events/gesture_detection/motion_event.h"
16 #include "ui/gfx/geometry/point_f.h"
17
18 using base::TimeDelta;
19 using base::TimeTicks;
20
21 namespace ui {
22 namespace {
23
24 const float kFakeCoordX = 42.f;
25 const float kFakeCoordY = 24.f;
26 const TimeDelta kOneSecond = TimeDelta::FromSeconds(1);
27 const TimeDelta kOneMicrosecond = TimeDelta::FromMicroseconds(1);
28 const TimeDelta kDeltaTimeForFlingSequences = TimeDelta::FromMilliseconds(5);
29
30 GestureProvider::Config CreateDefaultConfig() {
31   GestureProvider::Config sConfig;
32   // The longpress timeout is non-zero only to indicate ordering with respect to
33   // the showpress timeout.
34   sConfig.gesture_detector_config.showpress_timeout = base::TimeDelta();
35   sConfig.gesture_detector_config.longpress_timeout = kOneMicrosecond;
36
37   // A valid doubletap timeout should always be non-zero. The value is used not
38   // only to trigger the timeout that confirms the tap event, but also to gate
39   // whether the second tap is in fact a double-tap (using a strict inequality
40   // between times for the first up and the second down events). We use 4
41   // microseconds simply to allow several intermediate events to occur before
42   // the second tap at microsecond intervals.
43   sConfig.gesture_detector_config.double_tap_timeout = kOneMicrosecond * 4;
44   return sConfig;
45 }
46
47 }  // namespace
48
49 class GestureProviderTest : public testing::Test, public GestureProviderClient {
50  public:
51   GestureProviderTest() {}
52   virtual ~GestureProviderTest() {}
53
54   static MockMotionEvent ObtainMotionEvent(base::TimeTicks event_time,
55                                            MotionEvent::Action action,
56                                            float x,
57                                            float y) {
58     return MockMotionEvent(action, event_time, x, y);
59   }
60
61   static MockMotionEvent ObtainMotionEvent(base::TimeTicks event_time,
62                                            MotionEvent::Action action,
63                                            float x0,
64                                            float y0,
65                                            float x1,
66                                            float y1) {
67     return MockMotionEvent(action, event_time, x0, y0, x1, y1);
68   }
69
70   static MockMotionEvent ObtainMotionEvent(base::TimeTicks event_time,
71                                            MotionEvent::Action action) {
72     return ObtainMotionEvent(event_time, action, kFakeCoordX, kFakeCoordY);
73   }
74
75   // Test
76   virtual void SetUp() OVERRIDE {
77     gesture_provider_.reset(new GestureProvider(GetDefaultConfig(), this));
78     gesture_provider_->SetMultiTouchSupportEnabled(false);
79   }
80
81   virtual void TearDown() OVERRIDE {
82     gestures_.clear();
83     gesture_provider_.reset();
84   }
85
86   // GestureProviderClient
87   virtual void OnGestureEvent(const GestureEventData& gesture) OVERRIDE {
88     if (gesture.type == ET_GESTURE_SCROLL_BEGIN)
89       active_scroll_begin_event_.reset(new GestureEventData(gesture));
90     gestures_.push_back(gesture);
91   }
92
93   bool CancelActiveTouchSequence() {
94     if (!gesture_provider_->current_down_event())
95       return false;
96     return gesture_provider_->OnTouchEvent(
97         *gesture_provider_->current_down_event()->Cancel());
98   }
99
100   bool HasReceivedGesture(EventType type) const {
101     for (size_t i = 0; i < gestures_.size(); ++i) {
102       if (gestures_[i].type == type)
103         return true;
104     }
105     return false;
106   }
107
108   const GestureEventData& GetMostRecentGestureEvent() const {
109     EXPECT_FALSE(gestures_.empty());
110     return gestures_.back();
111   }
112
113   const EventType GetMostRecentGestureEventType() const {
114     EXPECT_FALSE(gestures_.empty());
115     return gestures_.back().type;
116   }
117
118   size_t GetReceivedGestureCount() const { return gestures_.size(); }
119
120   const GestureEventData& GetReceivedGesture(size_t index) const {
121     EXPECT_LT(index, GetReceivedGestureCount());
122     return gestures_[index];
123   }
124
125   const GestureEventData* GetActiveScrollBeginEvent() const {
126     return active_scroll_begin_event_ ? active_scroll_begin_event_.get() : NULL;
127   }
128
129   const GestureProvider::Config& GetDefaultConfig() const {
130     static GestureProvider::Config sConfig = CreateDefaultConfig();
131     return sConfig;
132   }
133
134   int GetTouchSlop() const {
135     return GetDefaultConfig().gesture_detector_config.scaled_touch_slop;
136   }
137
138   base::TimeDelta GetLongPressTimeout() const {
139     return GetDefaultConfig().gesture_detector_config.longpress_timeout;
140   }
141
142   base::TimeDelta GetShowPressTimeout() const {
143     return GetDefaultConfig().gesture_detector_config.showpress_timeout;
144   }
145
146  protected:
147   void CheckScrollEventSequenceForEndActionType(
148       MotionEvent::Action end_action_type) {
149     base::TimeTicks event_time = base::TimeTicks::Now();
150     const int scroll_to_x = kFakeCoordX + 100;
151     const int scroll_to_y = kFakeCoordY + 100;
152
153     MockMotionEvent event =
154         ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
155
156     EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
157
158     event = ObtainMotionEvent(event_time + kOneSecond,
159                               MotionEvent::ACTION_MOVE,
160                               scroll_to_x,
161                               scroll_to_y);
162     EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
163     EXPECT_TRUE(gesture_provider_->IsScrollInProgress());
164     EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
165     EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType());
166     ASSERT_EQ(4U, GetReceivedGestureCount()) << "Only TapDown, TapCancel, "
167                                                 "ScrollBegin and ScrollBy "
168                                                 "should have been sent";
169
170     EXPECT_EQ(ET_GESTURE_TAP_CANCEL, GetReceivedGesture(1).type);
171     EXPECT_EQ(ET_GESTURE_SCROLL_BEGIN, GetReceivedGesture(2).type);
172     EXPECT_EQ(event_time + kOneSecond, GetReceivedGesture(2).time)
173         << "ScrollBegin should have the time of the ACTION_MOVE";
174
175     event = ObtainMotionEvent(
176         event_time + kOneSecond, end_action_type, scroll_to_x, scroll_to_y);
177     gesture_provider_->OnTouchEvent(event);
178     EXPECT_FALSE(gesture_provider_->IsScrollInProgress());
179     EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_END));
180     EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType());
181   }
182
183   static void RunTasksAndWait(base::TimeDelta delay) {
184     base::MessageLoop::current()->PostDelayedTask(
185         FROM_HERE, base::MessageLoop::QuitClosure(), delay);
186     base::MessageLoop::current()->Run();
187   }
188
189   std::vector<GestureEventData> gestures_;
190   scoped_ptr<GestureProvider> gesture_provider_;
191   scoped_ptr<GestureEventData> active_scroll_begin_event_;
192   base::MessageLoopForUI message_loop_;
193 };
194
195 // Verify that a DOWN followed shortly by an UP will trigger a single tap.
196 TEST_F(GestureProviderTest, GestureTapTap) {
197   base::TimeTicks event_time = base::TimeTicks::Now();
198
199   gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false);
200
201   MockMotionEvent event =
202       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
203   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
204   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
205
206   event = ObtainMotionEvent(event_time + kOneMicrosecond,
207                             MotionEvent::ACTION_UP);
208   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
209   EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType());
210   // Ensure tap details have been set.
211   EXPECT_EQ(10, GetMostRecentGestureEvent().details.bounding_box().width());
212   EXPECT_EQ(10, GetMostRecentGestureEvent().details.bounding_box().height());
213   EXPECT_EQ(1, GetMostRecentGestureEvent().details.tap_count());
214 }
215
216 // Verify that a DOWN followed shortly by an UP will trigger
217 // a ET_GESTURE_TAP_UNCONFIRMED event if double-tap is enabled.
218 TEST_F(GestureProviderTest, GestureTapTapWithDelay) {
219   base::TimeTicks event_time = base::TimeTicks::Now();
220
221   gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true);
222
223   MockMotionEvent event =
224       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
225   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
226   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
227
228   event = ObtainMotionEvent(event_time + kOneMicrosecond,
229                             MotionEvent::ACTION_UP);
230   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
231   EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType());
232   // Ensure tap details have been set.
233   EXPECT_EQ(10, GetMostRecentGestureEvent().details.bounding_box().width());
234   EXPECT_EQ(10, GetMostRecentGestureEvent().details.bounding_box().height());
235   EXPECT_EQ(1, GetMostRecentGestureEvent().details.tap_count());
236
237   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_TAP));
238 }
239
240 // Verify that a DOWN followed by a MOVE will trigger fling (but not LONG).
241 TEST_F(GestureProviderTest, GestureFlingAndCancelLongPress) {
242   base::TimeTicks event_time = TimeTicks::Now();
243   base::TimeDelta delta_time = kDeltaTimeForFlingSequences;
244
245   MockMotionEvent event =
246       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
247
248   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
249   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
250
251   event = ObtainMotionEvent(event_time + delta_time,
252                             MotionEvent::ACTION_MOVE,
253                             kFakeCoordX * 10,
254                             kFakeCoordY * 10);
255   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
256
257   event = ObtainMotionEvent(event_time + delta_time * 2,
258                             MotionEvent::ACTION_UP,
259                             kFakeCoordX * 10,
260                             kFakeCoordY * 10);
261   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
262   EXPECT_EQ(ET_SCROLL_FLING_START, GetMostRecentGestureEventType());
263   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_PRESS));
264 }
265
266 // Verify that for a normal scroll the following events are sent:
267 // - ET_GESTURE_SCROLL_BEGIN
268 // - ET_GESTURE_SCROLL_UPDATE
269 // - ET_GESTURE_SCROLL_END
270 TEST_F(GestureProviderTest, ScrollEventActionUpSequence) {
271   CheckScrollEventSequenceForEndActionType(MotionEvent::ACTION_UP);
272 }
273
274 // Verify that for a cancelled scroll the following events are sent:
275 // - ET_GESTURE_SCROLL_BEGIN
276 // - ET_GESTURE_SCROLL_UPDATE
277 // - ET_GESTURE_SCROLL_END
278 TEST_F(GestureProviderTest, ScrollEventActionCancelSequence) {
279   CheckScrollEventSequenceForEndActionType(MotionEvent::ACTION_CANCEL);
280 }
281
282 // Verify that for a normal fling (fling after scroll) the following events are
283 // sent:
284 // - ET_GESTURE_SCROLL_BEGIN
285 // - ET_SCROLL_FLING_START
286 TEST_F(GestureProviderTest, FlingEventSequence) {
287   base::TimeTicks event_time = base::TimeTicks::Now();
288   base::TimeDelta delta_time = kDeltaTimeForFlingSequences;
289
290   MockMotionEvent event =
291       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
292
293   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
294
295   event = ObtainMotionEvent(event_time + delta_time,
296                             MotionEvent::ACTION_MOVE,
297                             kFakeCoordX * 5,
298                             kFakeCoordY * 5);
299   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
300   EXPECT_TRUE(gesture_provider_->IsScrollInProgress());
301   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
302   EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType());
303   ASSERT_EQ(4U, GetReceivedGestureCount());
304   ASSERT_EQ(ET_GESTURE_SCROLL_BEGIN, GetReceivedGesture(2).type);
305
306   // We don't want to take a dependency here on exactly how hints are calculated
307   // for a fling (eg. may depend on velocity), so just validate the direction.
308   int hint_x = GetReceivedGesture(2).details.scroll_x_hint();
309   int hint_y = GetReceivedGesture(2).details.scroll_y_hint();
310   EXPECT_TRUE(hint_x > 0 && hint_y > 0 && hint_x > hint_y)
311       << "ScrollBegin hint should be in positive X axis";
312
313   event = ObtainMotionEvent(event_time + delta_time * 2,
314                             MotionEvent::ACTION_UP,
315                             kFakeCoordX * 10,
316                             kFakeCoordY * 10);
317   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
318   EXPECT_FALSE(gesture_provider_->IsScrollInProgress());
319   EXPECT_EQ(ET_SCROLL_FLING_START, GetMostRecentGestureEventType());
320   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_END));
321   EXPECT_EQ(event_time + delta_time * 2, GetMostRecentGestureEvent().time)
322       << "FlingStart should have the time of the ACTION_UP";
323 }
324
325 TEST_F(GestureProviderTest, TapCancelledWhenWindowFocusLost) {
326   const base::TimeTicks event_time = TimeTicks::Now();
327
328   MockMotionEvent event =
329       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
330   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
331   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
332
333   RunTasksAndWait(GetLongPressTimeout() + GetShowPressTimeout() +
334                   kOneMicrosecond);
335   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SHOW_PRESS));
336   EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType());
337
338   // The long press triggers window focus loss by opening a context menu
339   EXPECT_TRUE(CancelActiveTouchSequence());
340   EXPECT_EQ(ET_GESTURE_TAP_CANCEL, GetMostRecentGestureEventType());
341 }
342
343 TEST_F(GestureProviderTest, TapCancelledWhenScrollBegins) {
344   base::TimeTicks event_time = base::TimeTicks::Now();
345
346   MockMotionEvent event =
347       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
348
349   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
350
351   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
352   event = ObtainMotionEvent(event_time + kOneMicrosecond,
353                             MotionEvent::ACTION_MOVE,
354                             kFakeCoordX + 50,
355                             kFakeCoordY + 50);
356   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
357
358   EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType());
359   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_TAP_CANCEL));
360 }
361
362 TEST_F(GestureProviderTest, DoubleTap) {
363   base::TimeTicks event_time = base::TimeTicks::Now();
364
365   MockMotionEvent event =
366       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
367   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
368
369   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
370
371   event = ObtainMotionEvent(event_time + kOneMicrosecond,
372                             MotionEvent::ACTION_UP,
373                             kFakeCoordX,
374                             kFakeCoordY);
375   gesture_provider_->OnTouchEvent(event);
376   EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType());
377
378   event = ObtainMotionEvent(event_time + kOneMicrosecond * 2,
379                             MotionEvent::ACTION_DOWN,
380                             kFakeCoordX,
381                             kFakeCoordY);
382   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
383   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
384
385   // Moving a very small amount of distance should not trigger the double tap
386   // drag zoom mode.
387   event = ObtainMotionEvent(event_time + kOneMicrosecond * 3,
388                             MotionEvent::ACTION_MOVE,
389                             kFakeCoordX,
390                             kFakeCoordY + 1);
391   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
392   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
393
394   event = ObtainMotionEvent(event_time + kOneMicrosecond * 3,
395                             MotionEvent::ACTION_UP,
396                             kFakeCoordX,
397                             kFakeCoordY + 1);
398   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
399
400   const GestureEventData& double_tap = GetMostRecentGestureEvent();
401   EXPECT_EQ(ET_GESTURE_DOUBLE_TAP, double_tap.type);
402   // Ensure tap details have been set.
403   EXPECT_EQ(10, double_tap.details.bounding_box().width());
404   EXPECT_EQ(10, double_tap.details.bounding_box().height());
405   EXPECT_EQ(1, double_tap.details.tap_count());
406 }
407
408 TEST_F(GestureProviderTest, DoubleTapDragZoom) {
409   const base::TimeTicks down_time_1 = TimeTicks::Now();
410   const base::TimeTicks down_time_2 = down_time_1 + kOneMicrosecond * 2;
411
412   MockMotionEvent event =
413       ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN);
414   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
415
416   event = ObtainMotionEvent(down_time_1 + kOneMicrosecond,
417                             MotionEvent::ACTION_UP,
418                             kFakeCoordX,
419                             kFakeCoordY);
420   gesture_provider_->OnTouchEvent(event);
421   EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType());
422
423   event = ObtainMotionEvent(
424       down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY);
425   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
426   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
427
428   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond,
429                             MotionEvent::ACTION_MOVE,
430                             kFakeCoordX,
431                             kFakeCoordY + 100);
432   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
433   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
434   const GestureEventData* scroll_begin_gesture = GetActiveScrollBeginEvent();
435   ASSERT_TRUE(!!scroll_begin_gesture);
436   EXPECT_EQ(0, scroll_begin_gesture->details.scroll_x_hint());
437   EXPECT_EQ(100, scroll_begin_gesture->details.scroll_y_hint());
438   EXPECT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType());
439
440   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2,
441                             MotionEvent::ACTION_MOVE,
442                             kFakeCoordX,
443                             kFakeCoordY + 200);
444   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
445   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE));
446   EXPECT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType());
447
448   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3,
449                             MotionEvent::ACTION_UP,
450                             kFakeCoordX,
451                             kFakeCoordY + 200);
452   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
453   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_END));
454   EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType());
455 }
456
457 TEST_F(GestureProviderTest, DoubleTapDragZoomCancelledOnSecondaryPointerDown) {
458   const base::TimeTicks down_time_1 = TimeTicks::Now();
459   const base::TimeTicks down_time_2 = down_time_1 + kOneMicrosecond * 2;
460
461   MockMotionEvent event =
462       ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN);
463   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
464   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
465
466   event = ObtainMotionEvent(down_time_1 + kOneMicrosecond,
467                             MotionEvent::ACTION_UP);
468   gesture_provider_->OnTouchEvent(event);
469   EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType());
470
471   event = ObtainMotionEvent(down_time_2, MotionEvent::ACTION_DOWN);
472   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
473   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
474   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_TAP_CANCEL));
475
476   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond,
477                             MotionEvent::ACTION_MOVE,
478                             kFakeCoordX,
479                             kFakeCoordY - 30);
480   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
481   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
482   EXPECT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType());
483
484   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2,
485                             MotionEvent::ACTION_POINTER_DOWN,
486                             kFakeCoordX,
487                             kFakeCoordY - 30,
488                             kFakeCoordX + 50,
489                             kFakeCoordY + 50);
490   gesture_provider_->OnTouchEvent(event);
491   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_END));
492   EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType());
493   const size_t gesture_count = GetReceivedGestureCount();
494
495   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3,
496                             MotionEvent::ACTION_POINTER_UP,
497                             kFakeCoordX,
498                             kFakeCoordY - 30,
499                             kFakeCoordX + 50,
500                             kFakeCoordY + 50);
501   gesture_provider_->OnTouchEvent(event);
502   EXPECT_EQ(gesture_count, GetReceivedGestureCount());
503 }
504
505 // Generate a scroll gesture and verify that the resulting scroll motion event
506 // has both absolute and relative position information.
507 TEST_F(GestureProviderTest, ScrollUpdateValues) {
508   const int delta_x = 16;
509   const int delta_y = 84;
510
511   const base::TimeTicks event_time = TimeTicks::Now();
512
513   MockMotionEvent event =
514       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
515   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
516
517   // Move twice so that we get two ET_GESTURE_SCROLL_UPDATE events and can
518   // compare the relative and absolute coordinates.
519   event = ObtainMotionEvent(event_time + kOneMicrosecond,
520                             MotionEvent::ACTION_MOVE,
521                             kFakeCoordX - delta_x / 2,
522                             kFakeCoordY - delta_y / 2);
523   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
524
525   event = ObtainMotionEvent(event_time + kOneMicrosecond * 2,
526                             MotionEvent::ACTION_MOVE,
527                             kFakeCoordX - delta_x,
528                             kFakeCoordY - delta_y);
529   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
530
531   // Make sure the reported gesture event has all the expected details.
532   ASSERT_LT(0U, GetReceivedGestureCount());
533   GestureEventData gesture = GetMostRecentGestureEvent();
534   EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, gesture.type);
535   EXPECT_EQ(event_time + kOneMicrosecond * 2, gesture.time);
536   EXPECT_EQ(kFakeCoordX - delta_x, gesture.x);
537   EXPECT_EQ(kFakeCoordY - delta_y, gesture.y);
538
539   // No horizontal delta because of snapping.
540   EXPECT_EQ(0, gesture.details.scroll_x());
541   EXPECT_EQ(-delta_y / 2, gesture.details.scroll_y());
542 }
543
544 // Verify that fractional scroll deltas are rounded as expected and that
545 // fractional scrolling doesn't break scroll snapping.
546 TEST_F(GestureProviderTest, FractionalScroll) {
547   const float delta_x = 0.4f;
548   const float delta_y = 5.2f;
549
550   const base::TimeTicks event_time = TimeTicks::Now();
551
552   MockMotionEvent event =
553       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
554   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
555
556   // Skip past the touch slop and move back.
557   event = ObtainMotionEvent(event_time,
558                             MotionEvent::ACTION_MOVE,
559                             kFakeCoordX,
560                             kFakeCoordY + 100);
561   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
562   event = ObtainMotionEvent(event_time,
563                             MotionEvent::ACTION_MOVE);
564   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
565
566   // Now move up slowly, mostly vertically but with a (fractional) bit of
567   // horizontal motion.
568   for(int i = 1; i <= 10; i++) {
569     event = ObtainMotionEvent(event_time + kOneMicrosecond * i,
570                               MotionEvent::ACTION_MOVE,
571                               kFakeCoordX + delta_x * i,
572                               kFakeCoordY + delta_y * i);
573     EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
574
575     ASSERT_LT(0U, GetReceivedGestureCount());
576     GestureEventData gesture = GetMostRecentGestureEvent();
577     EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, gesture.type);
578     EXPECT_EQ(event_time + kOneMicrosecond * i, gesture.time);
579
580     // Verify that the event co-ordinates are still the precise values we
581     // supplied.
582     EXPECT_EQ(kFakeCoordX + delta_x * i, gesture.x);
583     EXPECT_EQ(kFakeCoordY + delta_y * i, gesture.y);
584
585     // Verify that we're scrolling vertically by the expected amount
586     // (modulo rounding).
587     EXPECT_GE(gesture.details.scroll_y(), (int)delta_y);
588     EXPECT_LE(gesture.details.scroll_y(), ((int)delta_y) + 1);
589
590     // And that there has been no horizontal motion at all.
591     EXPECT_EQ(0, gesture.details.scroll_x());
592   }
593 }
594
595 // Generate a scroll gesture and verify that the resulting scroll begin event
596 // has the expected hint values.
597 TEST_F(GestureProviderTest, ScrollBeginValues) {
598   const int delta_x = 13;
599   const int delta_y = 89;
600
601   const base::TimeTicks event_time = TimeTicks::Now();
602
603   MockMotionEvent event =
604       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
605   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
606
607   // Move twice such that the first event isn't sufficient to start
608   // scrolling on it's own.
609   event = ObtainMotionEvent(event_time + kOneMicrosecond,
610                             MotionEvent::ACTION_MOVE,
611                             kFakeCoordX + 2,
612                             kFakeCoordY + 1);
613   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
614   EXPECT_FALSE(gesture_provider_->IsScrollInProgress());
615
616   event = ObtainMotionEvent(event_time + kOneMicrosecond * 2,
617                             MotionEvent::ACTION_MOVE,
618                             kFakeCoordX + delta_x,
619                             kFakeCoordY + delta_y);
620   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
621   EXPECT_TRUE(gesture_provider_->IsScrollInProgress());
622
623   const GestureEventData* scroll_begin_gesture = GetActiveScrollBeginEvent();
624   ASSERT_TRUE(!!scroll_begin_gesture);
625   EXPECT_EQ(delta_x, scroll_begin_gesture->details.scroll_x_hint());
626   EXPECT_EQ(delta_y, scroll_begin_gesture->details.scroll_y_hint());
627 }
628
629 TEST_F(GestureProviderTest, LongPressAndTapCancelledWhenScrollBegins) {
630   base::TimeTicks event_time = base::TimeTicks::Now();
631
632   MockMotionEvent event =
633       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
634   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
635   event = ObtainMotionEvent(event_time + kOneMicrosecond,
636                             MotionEvent::ACTION_MOVE,
637                             kFakeCoordX * 5,
638                             kFakeCoordY * 5);
639   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
640   event = ObtainMotionEvent(event_time + kOneMicrosecond * 2,
641                             MotionEvent::ACTION_MOVE,
642                             kFakeCoordX * 10,
643                             kFakeCoordY * 10);
644   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
645
646   const base::TimeDelta long_press_timeout =
647       GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond;
648   RunTasksAndWait(long_press_timeout);
649
650   // No LONG_TAP as the LONG_PRESS timer is cancelled.
651   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_PRESS));
652   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_TAP));
653 }
654
655 // Verify that LONG_TAP is triggered after LONG_PRESS followed by an UP.
656 TEST_F(GestureProviderTest, GestureLongTap) {
657   base::TimeTicks event_time = base::TimeTicks::Now();
658
659   MockMotionEvent event =
660       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
661   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
662
663   const base::TimeDelta long_press_timeout =
664       GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond;
665   RunTasksAndWait(long_press_timeout);
666
667   EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType());
668
669   event = ObtainMotionEvent(event_time + kOneSecond, MotionEvent::ACTION_UP);
670   gesture_provider_->OnTouchEvent(event);
671   EXPECT_EQ(ET_GESTURE_LONG_TAP, GetMostRecentGestureEventType());
672 }
673
674 TEST_F(GestureProviderTest, GestureLongPressDoesNotPreventScrolling) {
675   base::TimeTicks event_time = base::TimeTicks::Now();
676
677   MockMotionEvent event =
678       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
679   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
680
681   const base::TimeDelta long_press_timeout =
682       GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond;
683   RunTasksAndWait(long_press_timeout);
684
685   EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType());
686   event = ObtainMotionEvent(event_time + long_press_timeout,
687                             MotionEvent::ACTION_MOVE,
688                             kFakeCoordX + 100,
689                             kFakeCoordY + 100);
690   gesture_provider_->OnTouchEvent(event);
691
692   EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType());
693   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
694   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_TAP_CANCEL));
695
696   event = ObtainMotionEvent(event_time + long_press_timeout,
697                             MotionEvent::ACTION_UP);
698   gesture_provider_->OnTouchEvent(event);
699   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_TAP));
700 }
701
702 TEST_F(GestureProviderTest, NoGestureLongPressDuringDoubleTap) {
703   base::TimeTicks event_time = base::TimeTicks::Now();
704
705   MockMotionEvent event = ObtainMotionEvent(
706       event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY);
707   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
708
709   event = ObtainMotionEvent(event_time + kOneMicrosecond,
710                             MotionEvent::ACTION_UP,
711                             kFakeCoordX,
712                             kFakeCoordY);
713   gesture_provider_->OnTouchEvent(event);
714   EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType());
715
716   event = ObtainMotionEvent(event_time + kOneMicrosecond,
717                             MotionEvent::ACTION_DOWN,
718                             kFakeCoordX,
719                             kFakeCoordY);
720   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
721   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
722   EXPECT_TRUE(gesture_provider_->IsDoubleTapInProgress());
723
724   const base::TimeDelta long_press_timeout =
725       GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond;
726   RunTasksAndWait(long_press_timeout);
727   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_PRESS));
728
729   event = ObtainMotionEvent(event_time + long_press_timeout,
730                             MotionEvent::ACTION_MOVE,
731                             kFakeCoordX + 20,
732                             kFakeCoordY + 20);
733   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
734   EXPECT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType());
735   EXPECT_TRUE(gesture_provider_->IsDoubleTapInProgress());
736
737   event = ObtainMotionEvent(event_time + long_press_timeout + kOneMicrosecond,
738                             MotionEvent::ACTION_UP,
739                             kFakeCoordX,
740                             kFakeCoordY + 1);
741   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
742   EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType());
743   EXPECT_FALSE(gesture_provider_->IsDoubleTapInProgress());
744 }
745
746 // Verify that the touch slop region is removed from the first scroll delta to
747 // avoid a jump when starting to scroll.
748 TEST_F(GestureProviderTest, TouchSlopRemovedFromScroll) {
749   const int scaled_touch_slop = GetTouchSlop();
750   const int scroll_delta = 5;
751
752   base::TimeTicks event_time = base::TimeTicks::Now();
753
754   MockMotionEvent event =
755       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
756   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
757
758   event = ObtainMotionEvent(event_time + kOneMicrosecond * 2,
759                             MotionEvent::ACTION_MOVE,
760                             kFakeCoordX,
761                             kFakeCoordY + scaled_touch_slop + scroll_delta);
762   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
763
764   EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType());
765   GestureEventData gesture = GetMostRecentGestureEvent();
766   EXPECT_EQ(0, gesture.details.scroll_x());
767   EXPECT_EQ(scroll_delta, gesture.details.scroll_y());
768 }
769
770 TEST_F(GestureProviderTest, NoDoubleTapWhenExplicitlyDisabled) {
771   gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false);
772
773   base::TimeTicks event_time = base::TimeTicks::Now();
774   MockMotionEvent event = ObtainMotionEvent(
775       event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY);
776   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
777   EXPECT_EQ(1U, GetReceivedGestureCount());
778   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
779
780   event = ObtainMotionEvent(event_time + kOneMicrosecond,
781                             MotionEvent::ACTION_UP,
782                             kFakeCoordX,
783                             kFakeCoordY);
784   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
785   EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType());
786
787   event = ObtainMotionEvent(event_time + kOneMicrosecond * 2,
788                             MotionEvent::ACTION_DOWN,
789                             kFakeCoordX,
790                             kFakeCoordY);
791   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
792   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
793
794   event = ObtainMotionEvent(event_time + kOneMicrosecond * 3,
795                             MotionEvent::ACTION_UP,
796                             kFakeCoordX,
797                             kFakeCoordY);
798   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
799   EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType());
800 }
801
802 TEST_F(GestureProviderTest, NoDoubleTapDragZoomWhenDisabledOnPlatform) {
803   const base::TimeTicks down_time_1 = TimeTicks::Now();
804   const base::TimeTicks down_time_2 = down_time_1 + kOneMicrosecond * 2;
805
806   gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false);
807
808   MockMotionEvent event =
809       ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN);
810   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
811
812   event = ObtainMotionEvent(down_time_1 + kOneMicrosecond,
813                             MotionEvent::ACTION_UP,
814                             kFakeCoordX,
815                             kFakeCoordY);
816   gesture_provider_->OnTouchEvent(event);
817
818   event = ObtainMotionEvent(
819       down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY);
820   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
821
822   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond,
823                             MotionEvent::ACTION_MOVE,
824                             kFakeCoordX,
825                             kFakeCoordY + 100);
826
827   // The move should become a scroll, as doubletap drag zoom is disabled.
828   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
829   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
830   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN));
831
832   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2,
833                             MotionEvent::ACTION_MOVE,
834                             kFakeCoordX,
835                             kFakeCoordY + 200);
836   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
837   EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType());
838   EXPECT_EQ(down_time_2 + kOneMicrosecond * 2,
839             GetMostRecentGestureEvent().time);
840   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE));
841
842   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3,
843                             MotionEvent::ACTION_UP,
844                             kFakeCoordX,
845                             kFakeCoordY + 200);
846   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
847   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_END));
848 }
849
850 // Verify that double tap drag zoom feature is not invoked when the gesture
851 // handler is told to disable double tap gesture detection.
852 // The second tap sequence should be treated just as the first would be.
853 TEST_F(GestureProviderTest, NoDoubleTapDragZoomWhenDisabledOnPage) {
854   const base::TimeTicks down_time_1 = TimeTicks::Now();
855   const base::TimeTicks down_time_2 = down_time_1 + kOneMicrosecond * 2;
856
857   gesture_provider_->SetDoubleTapSupportForPageEnabled(false);
858
859   MockMotionEvent event =
860       ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN);
861   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
862
863   event = ObtainMotionEvent(down_time_1 + kOneMicrosecond,
864                             MotionEvent::ACTION_UP,
865                             kFakeCoordX,
866                             kFakeCoordY);
867   gesture_provider_->OnTouchEvent(event);
868
869   event = ObtainMotionEvent(
870       down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY);
871   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
872
873   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond,
874                             MotionEvent::ACTION_MOVE,
875                             kFakeCoordX,
876                             kFakeCoordY + 100);
877
878   // The move should become a scroll, as double tap drag zoom is disabled.
879   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
880   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
881   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN));
882
883   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2,
884                             MotionEvent::ACTION_MOVE,
885                             kFakeCoordX,
886                             kFakeCoordY + 200);
887   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
888   EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType());
889   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE));
890
891   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3,
892                             MotionEvent::ACTION_UP,
893                             kFakeCoordX,
894                             kFakeCoordY + 200);
895   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
896   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_END));
897 }
898
899 // Verify that updating double tap support during a double tap drag zoom
900 // disables double tap detection after the gesture has ended.
901 TEST_F(GestureProviderTest, FixedPageScaleDuringDoubleTapDragZoom) {
902   base::TimeTicks down_time_1 = TimeTicks::Now();
903   base::TimeTicks down_time_2 = down_time_1 + kOneMicrosecond * 2;
904
905   // Start a double-tap drag gesture.
906   MockMotionEvent event =
907       ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN);
908   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
909   event = ObtainMotionEvent(down_time_1 + kOneMicrosecond,
910                             MotionEvent::ACTION_UP,
911                             kFakeCoordX,
912                             kFakeCoordY);
913   gesture_provider_->OnTouchEvent(event);
914   event = ObtainMotionEvent(
915       down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY);
916   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
917   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond,
918                             MotionEvent::ACTION_MOVE,
919                             kFakeCoordX,
920                             kFakeCoordY + 100);
921   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
922   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
923   EXPECT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType());
924
925   // Simulate setting a fixed page scale (or a mobile viewport);
926   // this should not disrupt the current double-tap gesture.
927   gesture_provider_->SetDoubleTapSupportForPageEnabled(false);
928
929   // Double tap zoom updates should continue.
930   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2,
931                             MotionEvent::ACTION_MOVE,
932                             kFakeCoordX,
933                             kFakeCoordY + 200);
934   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
935   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE));
936   EXPECT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType());
937   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3,
938                             MotionEvent::ACTION_UP,
939                             kFakeCoordX,
940                             kFakeCoordY + 200);
941   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
942   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_END));
943   EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType());
944
945   // The double-tap gesture has finished, but the page scale is fixed.
946   // The same event sequence should not generate any double tap getsures.
947   gestures_.clear();
948   down_time_1 += kOneMicrosecond * 40;
949   down_time_2 += kOneMicrosecond * 40;
950
951   // Start a double-tap drag gesture.
952   event = ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN);
953   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
954   event = ObtainMotionEvent(down_time_1 + kOneMicrosecond,
955                             MotionEvent::ACTION_UP,
956                             kFakeCoordX,
957                             kFakeCoordY);
958   gesture_provider_->OnTouchEvent(event);
959   event = ObtainMotionEvent(
960       down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY);
961   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
962   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond,
963                             MotionEvent::ACTION_MOVE,
964                             kFakeCoordX,
965                             kFakeCoordY + 100);
966   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
967   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
968   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN));
969
970   // Double tap zoom updates should not be sent.
971   // Instead, the second tap drag becomes a scroll gesture sequence.
972   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2,
973                             MotionEvent::ACTION_MOVE,
974                             kFakeCoordX,
975                             kFakeCoordY + 200);
976   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
977   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE));
978   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE));
979   event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3,
980                             MotionEvent::ACTION_UP,
981                             kFakeCoordX,
982                             kFakeCoordY + 200);
983   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
984   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_END));
985 }
986
987 // Verify that pinch zoom sends the proper event sequence.
988 TEST_F(GestureProviderTest, PinchZoom) {
989   base::TimeTicks event_time = base::TimeTicks::Now();
990   const int scaled_touch_slop = GetTouchSlop();
991
992   gesture_provider_->SetMultiTouchSupportEnabled(true);
993
994   int secondary_coord_x = kFakeCoordX + 20 * scaled_touch_slop;
995   int secondary_coord_y = kFakeCoordY + 20 * scaled_touch_slop;
996
997   MockMotionEvent event =
998       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
999   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
1000   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
1001
1002   event = ObtainMotionEvent(event_time,
1003                             MotionEvent::ACTION_POINTER_DOWN,
1004                             kFakeCoordX,
1005                             kFakeCoordY,
1006                             secondary_coord_x,
1007                             secondary_coord_y);
1008   gesture_provider_->OnTouchEvent(event);
1009   EXPECT_EQ(1U, GetReceivedGestureCount());
1010
1011   secondary_coord_x += 5 * scaled_touch_slop;
1012   secondary_coord_y += 5 * scaled_touch_slop;
1013
1014   event = ObtainMotionEvent(event_time,
1015                             MotionEvent::ACTION_MOVE,
1016                             kFakeCoordX,
1017                             kFakeCoordY,
1018                             secondary_coord_x,
1019                             secondary_coord_y);
1020
1021   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
1022   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN));
1023   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN));
1024   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE));
1025   EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE));
1026
1027   event = ObtainMotionEvent(event_time,
1028                             MotionEvent::ACTION_POINTER_UP,
1029                             kFakeCoordX,
1030                             kFakeCoordY,
1031                             secondary_coord_x,
1032                             secondary_coord_y);
1033
1034   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
1035   EXPECT_EQ(ET_GESTURE_PINCH_END, GetMostRecentGestureEventType());
1036   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_END));
1037
1038   event = ObtainMotionEvent(event_time, MotionEvent::ACTION_UP);
1039   gesture_provider_->OnTouchEvent(event);
1040   EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType());
1041 }
1042
1043 // Verify that the timer of LONG_PRESS will be cancelled when scrolling begins
1044 // so LONG_PRESS and LONG_TAP won't be triggered.
1045 TEST_F(GestureProviderTest, GesturesCancelledAfterLongPressCausesLostFocus) {
1046   base::TimeTicks event_time = base::TimeTicks::Now();
1047
1048   MockMotionEvent event =
1049       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
1050   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
1051
1052   const base::TimeDelta long_press_timeout =
1053       GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond;
1054   RunTasksAndWait(long_press_timeout);
1055   EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType());
1056
1057   EXPECT_TRUE(CancelActiveTouchSequence());
1058   EXPECT_EQ(ET_GESTURE_TAP_CANCEL, GetMostRecentGestureEventType());
1059
1060   event = ObtainMotionEvent(event_time + long_press_timeout,
1061                             MotionEvent::ACTION_UP);
1062   gesture_provider_->OnTouchEvent(event);
1063   EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_TAP));
1064 }
1065
1066 // Verify that inserting a touch cancel event will trigger proper touch and
1067 // gesture sequence cancellation.
1068 TEST_F(GestureProviderTest, CancelActiveTouchSequence) {
1069   base::TimeTicks event_time = base::TimeTicks::Now();
1070
1071   EXPECT_FALSE(CancelActiveTouchSequence());
1072   EXPECT_EQ(0U, GetReceivedGestureCount());
1073
1074   MockMotionEvent event =
1075       ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN);
1076   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
1077   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
1078
1079   ASSERT_TRUE(CancelActiveTouchSequence());
1080   EXPECT_EQ(ET_GESTURE_TAP_CANCEL, GetMostRecentGestureEventType());
1081
1082   // Subsequent MotionEvent's are dropped until ACTION_DOWN.
1083   event = ObtainMotionEvent(event_time + kOneMicrosecond,
1084                             MotionEvent::ACTION_MOVE);
1085   EXPECT_FALSE(gesture_provider_->OnTouchEvent(event));
1086
1087   event = ObtainMotionEvent(event_time + kOneMicrosecond * 2,
1088                             MotionEvent::ACTION_UP);
1089   EXPECT_FALSE(gesture_provider_->OnTouchEvent(event));
1090
1091   event = ObtainMotionEvent(event_time + kOneMicrosecond * 3,
1092                             MotionEvent::ACTION_DOWN);
1093   EXPECT_TRUE(gesture_provider_->OnTouchEvent(event));
1094   EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType());
1095 }
1096
1097 }  // namespace ui