gesture_provider_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/motion_event.h" 15#include "ui/events/test/mock_motion_event.h" 16#include "ui/gfx/geometry/point_f.h" 17 18using base::TimeDelta; 19using base::TimeTicks; 20using ui::test::MockMotionEvent; 21 22namespace ui { 23namespace { 24 25const float kFakeCoordX = 42.f; 26const float kFakeCoordY = 24.f; 27const TimeDelta kOneSecond = TimeDelta::FromSeconds(1); 28const TimeDelta kOneMicrosecond = TimeDelta::FromMicroseconds(1); 29const TimeDelta kDeltaTimeForFlingSequences = TimeDelta::FromMilliseconds(5); 30const float kMockTouchRadius = MockMotionEvent::TOUCH_MAJOR / 2; 31const float kMaxTwoFingerTapSeparation = 300; 32 33GestureProvider::Config CreateDefaultConfig() { 34 GestureProvider::Config sConfig; 35 // The longpress timeout is non-zero only to indicate ordering with respect to 36 // the showpress timeout. 37 sConfig.gesture_detector_config.showpress_timeout = base::TimeDelta(); 38 sConfig.gesture_detector_config.longpress_timeout = kOneMicrosecond; 39 40 // A valid doubletap timeout should always be non-zero. The value is used not 41 // only to trigger the timeout that confirms the tap event, but also to gate 42 // whether the second tap is in fact a double-tap (using a strict inequality 43 // between times for the first up and the second down events). We use 4 44 // microseconds simply to allow several intermediate events to occur before 45 // the second tap at microsecond intervals. 46 sConfig.gesture_detector_config.double_tap_timeout = kOneMicrosecond * 4; 47 sConfig.gesture_detector_config.double_tap_min_time = kOneMicrosecond * 2; 48 49 sConfig.scale_gesture_detector_config.gesture_detector_config = 50 sConfig.gesture_detector_config; 51 return sConfig; 52} 53 54gfx::RectF BoundsForSingleMockTouchAtLocation(float x, float y) { 55 float diameter = MockMotionEvent::TOUCH_MAJOR; 56 return gfx::RectF(x - diameter / 2, y - diameter / 2, diameter, diameter); 57} 58 59} // namespace 60 61class GestureProviderTest : public testing::Test, public GestureProviderClient { 62 public: 63 GestureProviderTest() {} 64 virtual ~GestureProviderTest() {} 65 66 static MockMotionEvent ObtainMotionEvent(base::TimeTicks event_time, 67 MotionEvent::Action action, 68 float x, 69 float y) { 70 return MockMotionEvent(action, event_time, x, y); 71 } 72 73 static MockMotionEvent ObtainMotionEvent(base::TimeTicks event_time, 74 MotionEvent::Action action, 75 float x0, 76 float y0, 77 float x1, 78 float y1) { 79 return MockMotionEvent(action, event_time, x0, y0, x1, y1); 80 } 81 82 static MockMotionEvent ObtainMotionEvent(base::TimeTicks event_time, 83 MotionEvent::Action action, 84 float x0, 85 float y0, 86 float x1, 87 float y1, 88 float x2, 89 float y2) { 90 return MockMotionEvent(action, event_time, x0, y0, x1, y1, x2, y2); 91 } 92 93 static MockMotionEvent ObtainMotionEvent( 94 base::TimeTicks event_time, 95 MotionEvent::Action action, 96 const std::vector<gfx::PointF>& positions) { 97 switch (positions.size()) { 98 case 1: 99 return MockMotionEvent( 100 action, event_time, positions[0].x(), positions[0].y()); 101 case 2: 102 return MockMotionEvent(action, 103 event_time, 104 positions[0].x(), 105 positions[0].y(), 106 positions[1].x(), 107 positions[1].y()); 108 case 3: 109 return MockMotionEvent(action, 110 event_time, 111 positions[0].x(), 112 positions[0].y(), 113 positions[1].x(), 114 positions[1].y(), 115 positions[2].x(), 116 positions[2].y()); 117 default: 118 CHECK(false) << "MockMotionEvent only supports 1-3 pointers"; 119 return MockMotionEvent(); 120 } 121 } 122 123 static MockMotionEvent ObtainMotionEvent(base::TimeTicks event_time, 124 MotionEvent::Action action) { 125 return ObtainMotionEvent(event_time, action, kFakeCoordX, kFakeCoordY); 126 } 127 128 // Test 129 virtual void SetUp() OVERRIDE { SetUpWithConfig(GetDefaultConfig()); } 130 131 virtual void TearDown() OVERRIDE { 132 gestures_.clear(); 133 gesture_provider_.reset(); 134 } 135 136 // GestureProviderClient 137 virtual void OnGestureEvent(const GestureEventData& gesture) OVERRIDE { 138 if (gesture.type() == ET_GESTURE_SCROLL_BEGIN) 139 active_scroll_begin_event_.reset(new GestureEventData(gesture)); 140 gestures_.push_back(gesture); 141 } 142 143 void SetUpWithConfig(const GestureProvider::Config& config) { 144 gesture_provider_.reset(new GestureProvider(config, this)); 145 gesture_provider_->SetMultiTouchZoomSupportEnabled(false); 146 } 147 148 void ResetGestureDetection() { 149 CancelActiveTouchSequence(); 150 gestures_.clear(); 151 } 152 bool CancelActiveTouchSequence() { 153 if (!gesture_provider_->current_down_event()) 154 return false; 155 return gesture_provider_->OnTouchEvent( 156 *gesture_provider_->current_down_event()->Cancel()); 157 } 158 159 bool HasReceivedGesture(EventType type) const { 160 for (size_t i = 0; i < gestures_.size(); ++i) { 161 if (gestures_[i].type() == type) 162 return true; 163 } 164 return false; 165 } 166 167 const GestureEventData& GetMostRecentGestureEvent() const { 168 EXPECT_FALSE(gestures_.empty()); 169 return gestures_.back(); 170 } 171 172 EventType GetMostRecentGestureEventType() const { 173 EXPECT_FALSE(gestures_.empty()); 174 return gestures_.back().type(); 175 } 176 177 size_t GetReceivedGestureCount() const { return gestures_.size(); } 178 179 const GestureEventData& GetReceivedGesture(size_t index) const { 180 EXPECT_LT(index, GetReceivedGestureCount()); 181 return gestures_[index]; 182 } 183 184 const GestureEventData* GetActiveScrollBeginEvent() const { 185 return active_scroll_begin_event_ ? active_scroll_begin_event_.get() : NULL; 186 } 187 188 const GestureProvider::Config& GetDefaultConfig() const { 189 static GestureProvider::Config sConfig = CreateDefaultConfig(); 190 return sConfig; 191 } 192 193 float GetTouchSlop() const { 194 return GetDefaultConfig().gesture_detector_config.touch_slop; 195 } 196 197 float GetMinScalingSpan() const { 198 return GetDefaultConfig().scale_gesture_detector_config.min_scaling_span; 199 } 200 201 float GetMinSwipeVelocity() const { 202 return GetDefaultConfig().gesture_detector_config.minimum_swipe_velocity; 203 } 204 205 base::TimeDelta GetLongPressTimeout() const { 206 return GetDefaultConfig().gesture_detector_config.longpress_timeout; 207 } 208 209 base::TimeDelta GetShowPressTimeout() const { 210 return GetDefaultConfig().gesture_detector_config.showpress_timeout; 211 } 212 213 base::TimeDelta GetDoubleTapTimeout() const { 214 return GetDefaultConfig().gesture_detector_config.double_tap_timeout; 215 } 216 217 base::TimeDelta GetDoubleTapMinTime() const { 218 return GetDefaultConfig().gesture_detector_config.double_tap_min_time; 219 } 220 221 base::TimeDelta GetValidDoubleTapDelay() const { 222 return (GetDoubleTapTimeout() + GetDoubleTapMinTime()) / 2; 223 } 224 225 void EnableBeginEndTypes() { 226 GestureProvider::Config config = GetDefaultConfig(); 227 config.gesture_begin_end_types_enabled = true; 228 SetUpWithConfig(config); 229 } 230 231 void EnableSwipe() { 232 GestureProvider::Config config = GetDefaultConfig(); 233 config.gesture_detector_config.swipe_enabled = true; 234 SetUpWithConfig(config); 235 } 236 237 void EnableTwoFingerTap(float max_distance_for_two_finger_tap, 238 base::TimeDelta two_finger_tap_timeout) { 239 GestureProvider::Config config = GetDefaultConfig(); 240 config.gesture_detector_config.two_finger_tap_enabled = true; 241 config.gesture_detector_config.two_finger_tap_max_separation = 242 max_distance_for_two_finger_tap; 243 config.gesture_detector_config.two_finger_tap_timeout = 244 two_finger_tap_timeout; 245 SetUpWithConfig(config); 246 } 247 248 void SetMinPinchUpdateSpanDelta(float min_pinch_update_span_delta) { 249 GestureProvider::Config config = GetDefaultConfig(); 250 config.scale_gesture_detector_config.min_pinch_update_span_delta = 251 min_pinch_update_span_delta; 252 SetUpWithConfig(config); 253 } 254 255 void SetMinGestureBoundsLength(float min_gesture_bound_length) { 256 GestureProvider::Config config = GetDefaultConfig(); 257 config.min_gesture_bounds_length = min_gesture_bound_length; 258 SetUpWithConfig(config); 259 } 260 261 void SetMaxGestureBoundsLength(float max_gesture_bound_length) { 262 GestureProvider::Config config = GetDefaultConfig(); 263 config.max_gesture_bounds_length = max_gesture_bound_length; 264 SetUpWithConfig(config); 265 } 266 267 void EnableTouchMajorUseInScaling() { 268 GestureProvider::Config config = GetDefaultConfig(); 269 config.scale_gesture_detector_config.use_touch_major_in_span = true; 270 SetUpWithConfig(config); 271 } 272 273 bool HasDownEvent() const { return gesture_provider_->current_down_event(); } 274 275 protected: 276 void CheckScrollEventSequenceForEndActionType( 277 MotionEvent::Action end_action_type) { 278 base::TimeTicks event_time = base::TimeTicks::Now(); 279 const float scroll_to_x = kFakeCoordX + 100; 280 const float scroll_to_y = kFakeCoordY + 100; 281 int motion_event_id = 0; 282 283 MockMotionEvent event = 284 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 285 event.SetId(++motion_event_id); 286 287 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 288 289 event = ObtainMotionEvent(event_time + kOneSecond, 290 MotionEvent::ACTION_MOVE, 291 scroll_to_x, 292 scroll_to_y); 293 event.SetId(++motion_event_id); 294 295 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 296 EXPECT_TRUE(gesture_provider_->IsScrollInProgress()); 297 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 298 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 299 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType()); 300 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(scroll_to_x, scroll_to_y), 301 GetMostRecentGestureEvent().details.bounding_box()); 302 ASSERT_EQ(3U, GetReceivedGestureCount()) << "Only TapDown, " 303 "ScrollBegin and ScrollBy " 304 "should have been sent"; 305 306 EXPECT_EQ(ET_GESTURE_SCROLL_BEGIN, GetReceivedGesture(1).type()); 307 EXPECT_EQ(motion_event_id, GetReceivedGesture(1).motion_event_id); 308 EXPECT_EQ(event_time + kOneSecond, GetReceivedGesture(1).time) 309 << "ScrollBegin should have the time of the ACTION_MOVE"; 310 311 event = ObtainMotionEvent( 312 event_time + kOneSecond, end_action_type, scroll_to_x, scroll_to_y); 313 event.SetId(++motion_event_id); 314 315 gesture_provider_->OnTouchEvent(event); 316 EXPECT_FALSE(gesture_provider_->IsScrollInProgress()); 317 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_END)); 318 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 319 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 320 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 321 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(scroll_to_x, scroll_to_y), 322 GetMostRecentGestureEvent().details.bounding_box()); 323 } 324 325 void OneFingerSwipe(float vx, float vy) { 326 std::vector<gfx::Vector2dF> velocities; 327 velocities.push_back(gfx::Vector2dF(vx, vy)); 328 MultiFingerSwipe(velocities); 329 } 330 331 void TwoFingerSwipe(float vx0, float vy0, float vx1, float vy1) { 332 std::vector<gfx::Vector2dF> velocities; 333 velocities.push_back(gfx::Vector2dF(vx0, vy0)); 334 velocities.push_back(gfx::Vector2dF(vx1, vy1)); 335 MultiFingerSwipe(velocities); 336 } 337 338 void ThreeFingerSwipe(float vx0, 339 float vy0, 340 float vx1, 341 float vy1, 342 float vx2, 343 float vy2) { 344 std::vector<gfx::Vector2dF> velocities; 345 velocities.push_back(gfx::Vector2dF(vx0, vy0)); 346 velocities.push_back(gfx::Vector2dF(vx1, vy1)); 347 velocities.push_back(gfx::Vector2dF(vx2, vy2)); 348 MultiFingerSwipe(velocities); 349 } 350 351 void MultiFingerSwipe(std::vector<gfx::Vector2dF> velocities) { 352 ASSERT_GT(velocities.size(), 0U); 353 354 base::TimeTicks event_time = base::TimeTicks::Now(); 355 356 std::vector<gfx::PointF> positions(velocities.size()); 357 for (size_t i = 0; i < positions.size(); ++i) 358 positions[i] = gfx::PointF(kFakeCoordX * (i + 1), kFakeCoordY * (i + 1)); 359 360 float dt = kDeltaTimeForFlingSequences.InSecondsF(); 361 362 // Each pointer down should be a separate event. 363 for (size_t i = 0; i < positions.size(); ++i) { 364 const size_t pointer_count = i + 1; 365 std::vector<gfx::PointF> event_positions(pointer_count); 366 event_positions.assign(positions.begin(), 367 positions.begin() + pointer_count); 368 MockMotionEvent event = 369 ObtainMotionEvent(event_time, 370 pointer_count > 1 ? MotionEvent::ACTION_POINTER_DOWN 371 : MotionEvent::ACTION_DOWN, 372 event_positions); 373 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 374 } 375 376 for (size_t i = 0; i < positions.size(); ++i) 377 positions[i] += gfx::ScaleVector2d(velocities[i], dt); 378 MockMotionEvent event = 379 ObtainMotionEvent(event_time + kDeltaTimeForFlingSequences, 380 MotionEvent::ACTION_MOVE, 381 positions); 382 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 383 384 for (size_t i = 0; i < positions.size(); ++i) 385 positions[i] += gfx::ScaleVector2d(velocities[i], dt); 386 event = ObtainMotionEvent(event_time + 2 * kDeltaTimeForFlingSequences, 387 MotionEvent::ACTION_MOVE, 388 positions); 389 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 390 391 event = ObtainMotionEvent(event_time + 2 * kDeltaTimeForFlingSequences, 392 MotionEvent::ACTION_POINTER_UP, 393 positions); 394 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 395 } 396 397 static void RunTasksAndWait(base::TimeDelta delay) { 398 base::MessageLoop::current()->PostDelayedTask( 399 FROM_HERE, base::MessageLoop::QuitClosure(), delay); 400 base::MessageLoop::current()->Run(); 401 } 402 403 std::vector<GestureEventData> gestures_; 404 scoped_ptr<GestureProvider> gesture_provider_; 405 scoped_ptr<GestureEventData> active_scroll_begin_event_; 406 base::MessageLoopForUI message_loop_; 407}; 408 409// Verify that a DOWN followed shortly by an UP will trigger a single tap. 410TEST_F(GestureProviderTest, GestureTap) { 411 base::TimeTicks event_time = base::TimeTicks::Now(); 412 int motion_event_id = 0; 413 414 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 415 416 MockMotionEvent event = 417 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 418 event.SetId(++motion_event_id); 419 420 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 421 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 422 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 423 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 424 GetMostRecentGestureEvent().details.bounding_box()); 425 426 event = ObtainMotionEvent(event_time + kOneMicrosecond, 427 MotionEvent::ACTION_UP); 428 event.SetId(++motion_event_id); 429 430 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 431 EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType()); 432 // Ensure tap details have been set. 433 EXPECT_EQ(1, GetMostRecentGestureEvent().details.tap_count()); 434 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 435 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 436 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 437 GetMostRecentGestureEvent().details.bounding_box()); 438} 439 440// Verify that a DOWN followed shortly by an UP will trigger 441// a ET_GESTURE_TAP_UNCONFIRMED event if double-tap is enabled. 442TEST_F(GestureProviderTest, GestureTapWithDelay) { 443 base::TimeTicks event_time = base::TimeTicks::Now(); 444 int motion_event_id = 0; 445 446 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 447 448 MockMotionEvent event = 449 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 450 event.SetId(++motion_event_id); 451 452 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 453 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 454 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 455 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 456 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 457 GetMostRecentGestureEvent().details.bounding_box()); 458 459 event = ObtainMotionEvent(event_time + kOneMicrosecond, 460 MotionEvent::ACTION_UP); 461 event.SetId(++motion_event_id); 462 463 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 464 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 465 // Ensure tap details have been set. 466 EXPECT_EQ(1, GetMostRecentGestureEvent().details.tap_count()); 467 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 468 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 469 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 470 GetMostRecentGestureEvent().details.bounding_box()); 471 472 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_TAP)); 473 RunTasksAndWait(GetDoubleTapTimeout()); 474 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_TAP)); 475} 476 477// Verify that a DOWN followed by a MOVE will trigger fling (but not LONG). 478TEST_F(GestureProviderTest, GestureFlingAndCancelLongPress) { 479 base::TimeTicks event_time = TimeTicks::Now(); 480 base::TimeDelta delta_time = kDeltaTimeForFlingSequences; 481 int motion_event_id = 0; 482 483 MockMotionEvent event = 484 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 485 event.SetId(++motion_event_id); 486 487 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 488 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 489 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 490 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 491 492 event = ObtainMotionEvent(event_time + delta_time, 493 MotionEvent::ACTION_MOVE, 494 kFakeCoordX * 10, 495 kFakeCoordY * 10); 496 event.SetId(++motion_event_id); 497 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 498 499 event = ObtainMotionEvent(event_time + delta_time * 2, 500 MotionEvent::ACTION_UP, 501 kFakeCoordX * 10, 502 kFakeCoordY * 10); 503 event.SetId(++motion_event_id); 504 505 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 506 EXPECT_EQ(ET_SCROLL_FLING_START, GetMostRecentGestureEventType()); 507 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 508 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 509 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_PRESS)); 510 EXPECT_EQ( 511 BoundsForSingleMockTouchAtLocation(kFakeCoordX * 10, kFakeCoordY * 10), 512 GetMostRecentGestureEvent().details.bounding_box()); 513} 514 515// Verify that for a normal scroll the following events are sent: 516// - ET_GESTURE_SCROLL_BEGIN 517// - ET_GESTURE_SCROLL_UPDATE 518// - ET_GESTURE_SCROLL_END 519TEST_F(GestureProviderTest, ScrollEventActionUpSequence) { 520 CheckScrollEventSequenceForEndActionType(MotionEvent::ACTION_UP); 521} 522 523// Verify that for a cancelled scroll the following events are sent: 524// - ET_GESTURE_SCROLL_BEGIN 525// - ET_GESTURE_SCROLL_UPDATE 526// - ET_GESTURE_SCROLL_END 527TEST_F(GestureProviderTest, ScrollEventActionCancelSequence) { 528 CheckScrollEventSequenceForEndActionType(MotionEvent::ACTION_CANCEL); 529} 530 531// Verify that for a normal fling (fling after scroll) the following events are 532// sent: 533// - ET_GESTURE_SCROLL_BEGIN 534// - ET_SCROLL_FLING_START 535TEST_F(GestureProviderTest, FlingEventSequence) { 536 base::TimeTicks event_time = base::TimeTicks::Now(); 537 base::TimeDelta delta_time = kDeltaTimeForFlingSequences; 538 int motion_event_id = 0; 539 540 MockMotionEvent event = 541 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 542 event.SetId(++motion_event_id); 543 544 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 545 546 event = ObtainMotionEvent(event_time + delta_time, 547 MotionEvent::ACTION_MOVE, 548 kFakeCoordX * 5, 549 kFakeCoordY * 5); 550 event.SetId(++motion_event_id); 551 552 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 553 EXPECT_TRUE(gesture_provider_->IsScrollInProgress()); 554 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 555 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType()); 556 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 557 ASSERT_EQ(3U, GetReceivedGestureCount()); 558 ASSERT_EQ(ET_GESTURE_SCROLL_BEGIN, GetReceivedGesture(1).type()); 559 EXPECT_EQ(motion_event_id, GetReceivedGesture(1).motion_event_id); 560 561 // We don't want to take a dependency here on exactly how hints are calculated 562 // for a fling (eg. may depend on velocity), so just validate the direction. 563 int hint_x = GetReceivedGesture(1).details.scroll_x_hint(); 564 int hint_y = GetReceivedGesture(1).details.scroll_y_hint(); 565 EXPECT_TRUE(hint_x > 0 && hint_y > 0 && hint_x > hint_y) 566 << "ScrollBegin hint should be in positive X axis"; 567 568 event = ObtainMotionEvent(event_time + delta_time * 2, 569 MotionEvent::ACTION_UP, 570 kFakeCoordX * 10, 571 kFakeCoordY * 10); 572 event.SetId(++motion_event_id); 573 574 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 575 EXPECT_FALSE(gesture_provider_->IsScrollInProgress()); 576 EXPECT_EQ(ET_SCROLL_FLING_START, GetMostRecentGestureEventType()); 577 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 578 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 579 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_END)); 580 EXPECT_EQ(event_time + delta_time * 2, GetMostRecentGestureEvent().time) 581 << "FlingStart should have the time of the ACTION_UP"; 582} 583 584TEST_F(GestureProviderTest, GestureCancelledWhenWindowFocusLost) { 585 const base::TimeTicks event_time = TimeTicks::Now(); 586 587 MockMotionEvent event = 588 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 589 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 590 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 591 592 RunTasksAndWait(GetLongPressTimeout() + GetShowPressTimeout() + 593 kOneMicrosecond); 594 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SHOW_PRESS)); 595 EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType()); 596 597 // The long press triggers window focus loss by opening a context menu. 598 EXPECT_TRUE(CancelActiveTouchSequence()); 599 EXPECT_FALSE(HasDownEvent()); 600 601 // A final ACTION_UP should have no effect. 602 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 603 MotionEvent::ACTION_UP); 604 EXPECT_FALSE(gesture_provider_->OnTouchEvent(event)); 605} 606 607TEST_F(GestureProviderTest, NoTapAfterScrollBegins) { 608 base::TimeTicks event_time = base::TimeTicks::Now(); 609 610 MockMotionEvent event = 611 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 612 613 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 614 615 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 616 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 617 event = ObtainMotionEvent(event_time + kOneMicrosecond, 618 MotionEvent::ACTION_MOVE, 619 kFakeCoordX + 50, 620 kFakeCoordY + 50); 621 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 622 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType()); 623 624 event = ObtainMotionEvent(event_time + kOneSecond, 625 MotionEvent::ACTION_UP, 626 kFakeCoordX + 50, 627 kFakeCoordY + 50); 628 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 629 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 630 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_TAP)); 631} 632 633TEST_F(GestureProviderTest, DoubleTap) { 634 base::TimeTicks event_time = base::TimeTicks::Now(); 635 636 MockMotionEvent event = 637 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 638 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 639 640 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 641 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 642 643 event = ObtainMotionEvent(event_time + kOneMicrosecond, 644 MotionEvent::ACTION_UP, 645 kFakeCoordX, 646 kFakeCoordY); 647 gesture_provider_->OnTouchEvent(event); 648 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 649 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 650 651 event_time += GetValidDoubleTapDelay(); 652 event = ObtainMotionEvent(event_time, 653 MotionEvent::ACTION_DOWN, 654 kFakeCoordX, 655 kFakeCoordY); 656 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 657 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 658 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 659 660 // Moving a very small amount of distance should not trigger the double tap 661 // drag zoom mode. 662 event = ObtainMotionEvent(event_time + kOneMicrosecond, 663 MotionEvent::ACTION_MOVE, 664 kFakeCoordX, 665 kFakeCoordY + 1); 666 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 667 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 668 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 669 670 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 671 MotionEvent::ACTION_UP, 672 kFakeCoordX, 673 kFakeCoordY + 1); 674 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 675 676 const GestureEventData& double_tap = GetMostRecentGestureEvent(); 677 EXPECT_EQ(ET_GESTURE_DOUBLE_TAP, double_tap.type()); 678 // Ensure tap details have been set. 679 EXPECT_EQ(10, double_tap.details.bounding_box().width()); 680 EXPECT_EQ(10, double_tap.details.bounding_box().height()); 681 EXPECT_EQ(1, double_tap.details.tap_count()); 682} 683 684TEST_F(GestureProviderTest, DoubleTapDragZoomBasic) { 685 const base::TimeTicks down_time_1 = TimeTicks::Now(); 686 const base::TimeTicks down_time_2 = down_time_1 + GetValidDoubleTapDelay(); 687 688 MockMotionEvent event = 689 ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN); 690 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 691 692 event = ObtainMotionEvent(down_time_1 + kOneMicrosecond, 693 MotionEvent::ACTION_UP, 694 kFakeCoordX, 695 kFakeCoordY); 696 gesture_provider_->OnTouchEvent(event); 697 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 698 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 699 700 event = ObtainMotionEvent( 701 down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 702 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 703 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 704 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 705 706 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond, 707 MotionEvent::ACTION_MOVE, 708 kFakeCoordX, 709 kFakeCoordY + 100); 710 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 711 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 712 ASSERT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType()); 713 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY + 100), 714 GetMostRecentGestureEvent().details.bounding_box()); 715 716 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2, 717 MotionEvent::ACTION_MOVE, 718 kFakeCoordX, 719 kFakeCoordY + 200); 720 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 721 ASSERT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType()); 722 EXPECT_LT(1.f, GetMostRecentGestureEvent().details.scale()); 723 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY + 200), 724 GetMostRecentGestureEvent().details.bounding_box()); 725 726 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3, 727 MotionEvent::ACTION_MOVE, 728 kFakeCoordX, 729 kFakeCoordY + 100); 730 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 731 ASSERT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType()); 732 EXPECT_GT(1.f, GetMostRecentGestureEvent().details.scale()); 733 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY + 100), 734 GetMostRecentGestureEvent().details.bounding_box()); 735 736 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 4, 737 MotionEvent::ACTION_UP, 738 kFakeCoordX, 739 kFakeCoordY - 200); 740 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 741 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_END)); 742 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 743 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY - 200), 744 GetMostRecentGestureEvent().details.bounding_box()); 745} 746 747// Generate a scroll gesture and verify that the resulting scroll motion event 748// has both absolute and relative position information. 749TEST_F(GestureProviderTest, ScrollUpdateValues) { 750 const float delta_x = 16; 751 const float delta_y = 84; 752 const float raw_offset_x = 17.3f; 753 const float raw_offset_y = 13.7f; 754 755 const base::TimeTicks event_time = TimeTicks::Now(); 756 757 MockMotionEvent event = 758 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 759 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 760 761 // Move twice so that we get two ET_GESTURE_SCROLL_UPDATE events and can 762 // compare the relative and absolute coordinates. 763 event = ObtainMotionEvent(event_time + kOneMicrosecond, 764 MotionEvent::ACTION_MOVE, 765 kFakeCoordX - delta_x / 2, 766 kFakeCoordY - delta_y / 2); 767 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 768 769 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 770 MotionEvent::ACTION_MOVE, 771 kFakeCoordX - delta_x, 772 kFakeCoordY - delta_y); 773 event.SetRawOffset(raw_offset_x, raw_offset_y); 774 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 775 776 // Make sure the reported gesture event has all the expected details. 777 ASSERT_LT(0U, GetReceivedGestureCount()); 778 GestureEventData gesture = GetMostRecentGestureEvent(); 779 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, gesture.type()); 780 EXPECT_EQ(event_time + kOneMicrosecond * 2, gesture.time); 781 EXPECT_EQ(kFakeCoordX - delta_x, gesture.x); 782 EXPECT_EQ(kFakeCoordY - delta_y, gesture.y); 783 EXPECT_EQ(kFakeCoordX - delta_x + raw_offset_x, gesture.raw_x); 784 EXPECT_EQ(kFakeCoordY - delta_y + raw_offset_y, gesture.raw_y); 785 EXPECT_EQ(1, gesture.details.touch_points()); 786 787 // No horizontal delta because of snapping. 788 EXPECT_EQ(0, gesture.details.scroll_x()); 789 EXPECT_EQ(-delta_y / 2, gesture.details.scroll_y()); 790} 791 792// Verify that fractional scroll deltas are rounded as expected and that 793// fractional scrolling doesn't break scroll snapping. 794TEST_F(GestureProviderTest, FractionalScroll) { 795 const float delta_x = 0.4f; 796 const float delta_y = 5.2f; 797 798 const base::TimeTicks event_time = TimeTicks::Now(); 799 800 MockMotionEvent event = 801 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 802 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 803 804 // Skip past the touch slop and move back. 805 event = ObtainMotionEvent(event_time, 806 MotionEvent::ACTION_MOVE, 807 kFakeCoordX, 808 kFakeCoordY + 100); 809 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 810 event = ObtainMotionEvent(event_time, 811 MotionEvent::ACTION_MOVE); 812 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 813 814 // Now move up slowly, mostly vertically but with a (fractional) bit of 815 // horizontal motion. 816 for(int i = 1; i <= 10; i++) { 817 event = ObtainMotionEvent(event_time + kOneMicrosecond * i, 818 MotionEvent::ACTION_MOVE, 819 kFakeCoordX + delta_x * i, 820 kFakeCoordY + delta_y * i); 821 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 822 823 ASSERT_LT(0U, GetReceivedGestureCount()); 824 GestureEventData gesture = GetMostRecentGestureEvent(); 825 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, gesture.type()); 826 EXPECT_EQ(event_time + kOneMicrosecond * i, gesture.time); 827 EXPECT_EQ(1, gesture.details.touch_points()); 828 829 // Verify that the event co-ordinates are still the precise values we 830 // supplied. 831 EXPECT_EQ(kFakeCoordX + delta_x * i, gesture.x); 832 EXPECT_EQ(kFakeCoordY + delta_y * i, gesture.y); 833 834 // Verify that we're scrolling vertically by the expected amount 835 // (modulo rounding). 836 EXPECT_GE(gesture.details.scroll_y(), (int)delta_y); 837 EXPECT_LE(gesture.details.scroll_y(), ((int)delta_y) + 1); 838 839 // And that there has been no horizontal motion at all. 840 EXPECT_EQ(0, gesture.details.scroll_x()); 841 } 842} 843 844// Generate a scroll gesture and verify that the resulting scroll begin event 845// has the expected hint values. 846TEST_F(GestureProviderTest, ScrollBeginValues) { 847 const float delta_x = 13; 848 const float delta_y = 89; 849 850 const base::TimeTicks event_time = TimeTicks::Now(); 851 852 MockMotionEvent event = 853 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 854 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 855 856 // Move twice such that the first event isn't sufficient to start 857 // scrolling on it's own. 858 event = ObtainMotionEvent(event_time + kOneMicrosecond, 859 MotionEvent::ACTION_MOVE, 860 kFakeCoordX + 2, 861 kFakeCoordY + 1); 862 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 863 EXPECT_FALSE(gesture_provider_->IsScrollInProgress()); 864 865 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 866 MotionEvent::ACTION_MOVE, 867 kFakeCoordX + delta_x, 868 kFakeCoordY + delta_y); 869 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 870 EXPECT_TRUE(gesture_provider_->IsScrollInProgress()); 871 872 const GestureEventData* scroll_begin_gesture = GetActiveScrollBeginEvent(); 873 ASSERT_TRUE(!!scroll_begin_gesture); 874 EXPECT_EQ(delta_x, scroll_begin_gesture->details.scroll_x_hint()); 875 EXPECT_EQ(delta_y, scroll_begin_gesture->details.scroll_y_hint()); 876} 877 878TEST_F(GestureProviderTest, LongPressAndTapCancelledWhenScrollBegins) { 879 base::TimeTicks event_time = base::TimeTicks::Now(); 880 881 MockMotionEvent event = 882 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 883 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 884 event = ObtainMotionEvent(event_time + kOneMicrosecond, 885 MotionEvent::ACTION_MOVE, 886 kFakeCoordX * 5, 887 kFakeCoordY * 5); 888 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 889 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 890 MotionEvent::ACTION_MOVE, 891 kFakeCoordX * 10, 892 kFakeCoordY * 10); 893 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 894 895 const base::TimeDelta long_press_timeout = 896 GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond; 897 RunTasksAndWait(long_press_timeout); 898 899 // No LONG_TAP as the LONG_PRESS timer is cancelled. 900 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_PRESS)); 901 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_TAP)); 902} 903 904// Verify that LONG_TAP is triggered after LONG_PRESS followed by an UP. 905TEST_F(GestureProviderTest, GestureLongTap) { 906 base::TimeTicks event_time = base::TimeTicks::Now(); 907 908 MockMotionEvent event = 909 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 910 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 911 912 const base::TimeDelta long_press_timeout = 913 GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond; 914 RunTasksAndWait(long_press_timeout); 915 916 EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType()); 917 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 918 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 919 GetMostRecentGestureEvent().details.bounding_box()); 920 921 event = ObtainMotionEvent(event_time + kOneSecond, MotionEvent::ACTION_UP); 922 gesture_provider_->OnTouchEvent(event); 923 EXPECT_EQ(ET_GESTURE_LONG_TAP, GetMostRecentGestureEventType()); 924 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 925 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 926 GetMostRecentGestureEvent().details.bounding_box()); 927} 928 929TEST_F(GestureProviderTest, GestureLongPressDoesNotPreventScrolling) { 930 base::TimeTicks event_time = base::TimeTicks::Now(); 931 932 MockMotionEvent event = 933 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 934 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 935 936 const base::TimeDelta long_press_timeout = 937 GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond; 938 RunTasksAndWait(long_press_timeout); 939 940 EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType()); 941 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 942 event = ObtainMotionEvent(event_time + long_press_timeout, 943 MotionEvent::ACTION_MOVE, 944 kFakeCoordX + 100, 945 kFakeCoordY + 100); 946 gesture_provider_->OnTouchEvent(event); 947 948 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType()); 949 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 950 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 951 952 event = ObtainMotionEvent(event_time + long_press_timeout, 953 MotionEvent::ACTION_UP); 954 gesture_provider_->OnTouchEvent(event); 955 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_TAP)); 956} 957 958TEST_F(GestureProviderTest, NoGestureLongPressDuringDoubleTap) { 959 base::TimeTicks event_time = base::TimeTicks::Now(); 960 int motion_event_id = 0; 961 962 MockMotionEvent event = ObtainMotionEvent( 963 event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 964 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 965 966 event = ObtainMotionEvent(event_time + kOneMicrosecond, 967 MotionEvent::ACTION_UP, 968 kFakeCoordX, 969 kFakeCoordY); 970 gesture_provider_->OnTouchEvent(event); 971 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 972 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 973 974 event_time += GetValidDoubleTapDelay(); 975 event = ObtainMotionEvent(event_time, 976 MotionEvent::ACTION_DOWN, 977 kFakeCoordX, 978 kFakeCoordY); 979 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 980 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 981 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 982 EXPECT_TRUE(gesture_provider_->IsDoubleTapInProgress()); 983 984 const base::TimeDelta long_press_timeout = 985 GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond; 986 RunTasksAndWait(long_press_timeout); 987 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_PRESS)); 988 989 event = ObtainMotionEvent(event_time + long_press_timeout, 990 MotionEvent::ACTION_MOVE, 991 kFakeCoordX + 20, 992 kFakeCoordY + 20); 993 event.SetId(++motion_event_id); 994 995 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 996 EXPECT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType()); 997 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 998 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 999 EXPECT_TRUE(gesture_provider_->IsDoubleTapInProgress()); 1000 1001 event = ObtainMotionEvent(event_time + long_press_timeout + kOneMicrosecond, 1002 MotionEvent::ACTION_UP, 1003 kFakeCoordX, 1004 kFakeCoordY + 1); 1005 event.SetId(++motion_event_id); 1006 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1007 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 1008 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 1009 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1010 EXPECT_FALSE(gesture_provider_->IsDoubleTapInProgress()); 1011} 1012 1013// Verify that the touch slop region is removed from the first scroll delta to 1014// avoid a jump when starting to scroll. 1015TEST_F(GestureProviderTest, TouchSlopRemovedFromScroll) { 1016 const float touch_slop = GetTouchSlop(); 1017 const float scroll_delta = 5; 1018 1019 base::TimeTicks event_time = base::TimeTicks::Now(); 1020 1021 MockMotionEvent event = 1022 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1023 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1024 1025 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 1026 MotionEvent::ACTION_MOVE, 1027 kFakeCoordX, 1028 kFakeCoordY + touch_slop + scroll_delta); 1029 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1030 1031 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType()); 1032 GestureEventData gesture = GetMostRecentGestureEvent(); 1033 EXPECT_EQ(0, gesture.details.scroll_x()); 1034 EXPECT_EQ(scroll_delta, gesture.details.scroll_y()); 1035 EXPECT_EQ(1, gesture.details.touch_points()); 1036} 1037 1038// Verify that movement within the touch slop region does not generate a scroll, 1039// and that the slop region is correct even when using fractional coordinates. 1040TEST_F(GestureProviderTest, NoScrollWithinTouchSlop) { 1041 const float touch_slop = GetTouchSlop(); 1042 const float scale_factor = 2.5f; 1043 const int touch_slop_pixels = static_cast<int>(scale_factor * touch_slop); 1044 1045 base::TimeTicks event_time = base::TimeTicks::Now(); 1046 1047 MockMotionEvent event = 1048 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1049 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1050 1051 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 1052 MotionEvent::ACTION_MOVE, 1053 kFakeCoordX + touch_slop_pixels / scale_factor, 1054 kFakeCoordY); 1055 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1056 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1057 1058 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 1059 MotionEvent::ACTION_MOVE, 1060 kFakeCoordX, 1061 kFakeCoordY + touch_slop_pixels / scale_factor); 1062 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1063 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1064 1065 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 1066 MotionEvent::ACTION_MOVE, 1067 kFakeCoordX - touch_slop_pixels / scale_factor, 1068 kFakeCoordY); 1069 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1070 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1071 1072 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 1073 MotionEvent::ACTION_MOVE, 1074 kFakeCoordX, 1075 kFakeCoordY - touch_slop_pixels / scale_factor); 1076 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1077 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1078 1079 event = 1080 ObtainMotionEvent(event_time + kOneMicrosecond * 2, 1081 MotionEvent::ACTION_MOVE, 1082 kFakeCoordX, 1083 kFakeCoordY + (touch_slop_pixels + 1.f) / scale_factor); 1084 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1085 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1086} 1087 1088TEST_F(GestureProviderTest, NoDoubleTapWhenTooRapid) { 1089 base::TimeTicks event_time = base::TimeTicks::Now(); 1090 1091 MockMotionEvent event = 1092 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1093 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1094 1095 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1096 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1097 1098 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1099 MotionEvent::ACTION_UP, 1100 kFakeCoordX, 1101 kFakeCoordY); 1102 gesture_provider_->OnTouchEvent(event); 1103 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 1104 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1105 1106 // If the second tap follows the first in too short a time span, no double-tap 1107 // will occur. 1108 event_time += (GetDoubleTapMinTime() / 2); 1109 event = ObtainMotionEvent(event_time, 1110 MotionEvent::ACTION_DOWN, 1111 kFakeCoordX, 1112 kFakeCoordY); 1113 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1114 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1115 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1116 1117 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1118 MotionEvent::ACTION_UP, 1119 kFakeCoordX, 1120 kFakeCoordY); 1121 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1122 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 1123} 1124 1125TEST_F(GestureProviderTest, NoDoubleTapWhenExplicitlyDisabled) { 1126 // Ensure that double-tap gestures can be disabled. 1127 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 1128 1129 base::TimeTicks event_time = base::TimeTicks::Now(); 1130 MockMotionEvent event = ObtainMotionEvent( 1131 event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 1132 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1133 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1134 1135 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1136 MotionEvent::ACTION_UP, 1137 kFakeCoordX, 1138 kFakeCoordY); 1139 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1140 EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType()); 1141 1142 event_time += GetValidDoubleTapDelay(); 1143 event = ObtainMotionEvent(event_time, 1144 MotionEvent::ACTION_DOWN, 1145 kFakeCoordX, 1146 kFakeCoordY); 1147 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1148 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1149 1150 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1151 MotionEvent::ACTION_UP, 1152 kFakeCoordX, 1153 kFakeCoordY); 1154 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1155 EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType()); 1156 1157 // Ensure that double-tap gestures can be interrupted. 1158 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 1159 1160 event_time = base::TimeTicks::Now(); 1161 event = ObtainMotionEvent( 1162 event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 1163 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1164 EXPECT_EQ(5U, GetReceivedGestureCount()); 1165 1166 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1167 MotionEvent::ACTION_UP, 1168 kFakeCoordX, 1169 kFakeCoordY); 1170 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1171 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 1172 1173 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 1174 EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType()); 1175 1176 // Ensure that double-tap gestures can be resumed. 1177 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 1178 1179 event_time += GetValidDoubleTapDelay(); 1180 event = ObtainMotionEvent(event_time, 1181 MotionEvent::ACTION_DOWN, 1182 kFakeCoordX, 1183 kFakeCoordY); 1184 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1185 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1186 1187 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1188 MotionEvent::ACTION_UP, 1189 kFakeCoordX, 1190 kFakeCoordY); 1191 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1192 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 1193 1194 event_time += GetValidDoubleTapDelay(); 1195 event = ObtainMotionEvent(event_time, 1196 MotionEvent::ACTION_DOWN, 1197 kFakeCoordX, 1198 kFakeCoordY); 1199 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1200 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1201 1202 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1203 MotionEvent::ACTION_UP, 1204 kFakeCoordX, 1205 kFakeCoordY); 1206 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1207 EXPECT_EQ(ET_GESTURE_DOUBLE_TAP, GetMostRecentGestureEventType()); 1208} 1209 1210TEST_F(GestureProviderTest, NoDelayedTapWhenDoubleTapSupportToggled) { 1211 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 1212 1213 base::TimeTicks event_time = base::TimeTicks::Now(); 1214 MockMotionEvent event = ObtainMotionEvent( 1215 event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 1216 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1217 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1218 EXPECT_EQ(1U, GetReceivedGestureCount()); 1219 1220 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1221 MotionEvent::ACTION_UP, 1222 kFakeCoordX, 1223 kFakeCoordY); 1224 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1225 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 1226 EXPECT_EQ(2U, GetReceivedGestureCount()); 1227 1228 // Disabling double-tap during the tap timeout should flush the delayed tap. 1229 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 1230 EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType()); 1231 EXPECT_EQ(3U, GetReceivedGestureCount()); 1232 1233 // No further timeout gestures should arrive. 1234 const base::TimeDelta long_press_timeout = 1235 GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond; 1236 RunTasksAndWait(long_press_timeout); 1237 EXPECT_EQ(3U, GetReceivedGestureCount()); 1238} 1239 1240TEST_F(GestureProviderTest, NoDoubleTapDragZoomWhenDisabledOnPlatform) { 1241 const base::TimeTicks down_time_1 = TimeTicks::Now(); 1242 const base::TimeTicks down_time_2 = down_time_1 + GetValidDoubleTapDelay(); 1243 1244 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 1245 1246 MockMotionEvent event = 1247 ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN); 1248 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1249 1250 event = ObtainMotionEvent(down_time_1 + kOneMicrosecond, 1251 MotionEvent::ACTION_UP, 1252 kFakeCoordX, 1253 kFakeCoordY); 1254 gesture_provider_->OnTouchEvent(event); 1255 1256 event = ObtainMotionEvent( 1257 down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 1258 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1259 1260 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond, 1261 MotionEvent::ACTION_MOVE, 1262 kFakeCoordX, 1263 kFakeCoordY + 100); 1264 1265 // The move should become a scroll, as doubletap drag zoom is disabled. 1266 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1267 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1268 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN)); 1269 1270 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2, 1271 MotionEvent::ACTION_MOVE, 1272 kFakeCoordX, 1273 kFakeCoordY + 200); 1274 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1275 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType()); 1276 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1277 EXPECT_EQ(down_time_2 + kOneMicrosecond * 2, 1278 GetMostRecentGestureEvent().time); 1279 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE)); 1280 1281 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3, 1282 MotionEvent::ACTION_UP, 1283 kFakeCoordX, 1284 kFakeCoordY + 200); 1285 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1286 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_END)); 1287} 1288 1289// Verify that double tap drag zoom feature is not invoked when the gesture 1290// handler is told to disable double tap gesture detection. 1291// The second tap sequence should be treated just as the first would be. 1292TEST_F(GestureProviderTest, NoDoubleTapDragZoomWhenDisabledOnPage) { 1293 const base::TimeTicks down_time_1 = TimeTicks::Now(); 1294 const base::TimeTicks down_time_2 = down_time_1 + GetValidDoubleTapDelay(); 1295 1296 gesture_provider_->SetDoubleTapSupportForPageEnabled(false); 1297 1298 MockMotionEvent event = 1299 ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN); 1300 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1301 1302 event = ObtainMotionEvent(down_time_1 + kOneMicrosecond, 1303 MotionEvent::ACTION_UP, 1304 kFakeCoordX, 1305 kFakeCoordY); 1306 gesture_provider_->OnTouchEvent(event); 1307 1308 event = ObtainMotionEvent( 1309 down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 1310 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1311 1312 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond, 1313 MotionEvent::ACTION_MOVE, 1314 kFakeCoordX, 1315 kFakeCoordY + 100); 1316 1317 // The move should become a scroll, as double tap drag zoom is disabled. 1318 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1319 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1320 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN)); 1321 1322 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2, 1323 MotionEvent::ACTION_MOVE, 1324 kFakeCoordX, 1325 kFakeCoordY + 200); 1326 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1327 EXPECT_EQ(ET_GESTURE_SCROLL_UPDATE, GetMostRecentGestureEventType()); 1328 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1329 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE)); 1330 1331 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3, 1332 MotionEvent::ACTION_UP, 1333 kFakeCoordX, 1334 kFakeCoordY + 200); 1335 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1336 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_END)); 1337} 1338 1339// Verify that updating double tap support during a double tap drag zoom 1340// disables double tap detection after the gesture has ended. 1341TEST_F(GestureProviderTest, FixedPageScaleDuringDoubleTapDragZoom) { 1342 base::TimeTicks down_time_1 = TimeTicks::Now(); 1343 base::TimeTicks down_time_2 = down_time_1 + GetValidDoubleTapDelay(); 1344 1345 gesture_provider_->SetDoubleTapSupportForPageEnabled(true); 1346 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 1347 1348 // Start a double-tap drag gesture. 1349 MockMotionEvent event = 1350 ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN); 1351 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1352 event = ObtainMotionEvent(down_time_1 + kOneMicrosecond, 1353 MotionEvent::ACTION_UP, 1354 kFakeCoordX, 1355 kFakeCoordY); 1356 gesture_provider_->OnTouchEvent(event); 1357 event = ObtainMotionEvent( 1358 down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 1359 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1360 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond, 1361 MotionEvent::ACTION_MOVE, 1362 kFakeCoordX, 1363 kFakeCoordY + 100); 1364 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1365 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1366 EXPECT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType()); 1367 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1368 1369 // Simulate setting a fixed page scale (or a mobile viewport); 1370 // this should not disrupt the current double-tap gesture. 1371 gesture_provider_->SetDoubleTapSupportForPageEnabled(false); 1372 1373 // Double tap zoom updates should continue. 1374 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2, 1375 MotionEvent::ACTION_MOVE, 1376 kFakeCoordX, 1377 kFakeCoordY + 200); 1378 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1379 EXPECT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType()); 1380 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1381 EXPECT_LT(1.f, GetMostRecentGestureEvent().details.scale()); 1382 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3, 1383 MotionEvent::ACTION_UP, 1384 kFakeCoordX, 1385 kFakeCoordY + 200); 1386 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1387 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_END)); 1388 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 1389 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1390 1391 // The double-tap gesture has finished, but the page scale is fixed. 1392 // The same event sequence should not generate any double tap getsures. 1393 gestures_.clear(); 1394 down_time_1 += kOneMicrosecond * 40; 1395 down_time_2 += kOneMicrosecond * 40; 1396 1397 // Start a double-tap drag gesture. 1398 event = ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN); 1399 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1400 event = ObtainMotionEvent(down_time_1 + kOneMicrosecond, 1401 MotionEvent::ACTION_UP, 1402 kFakeCoordX, 1403 kFakeCoordY); 1404 gesture_provider_->OnTouchEvent(event); 1405 event = ObtainMotionEvent( 1406 down_time_2, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 1407 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1408 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond, 1409 MotionEvent::ACTION_MOVE, 1410 kFakeCoordX, 1411 kFakeCoordY + 100); 1412 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1413 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1414 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN)); 1415 1416 // Double tap zoom updates should not be sent. 1417 // Instead, the second tap drag becomes a scroll gesture sequence. 1418 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2, 1419 MotionEvent::ACTION_MOVE, 1420 kFakeCoordX, 1421 kFakeCoordY + 200); 1422 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1423 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE)); 1424 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE)); 1425 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3, 1426 MotionEvent::ACTION_UP, 1427 kFakeCoordX, 1428 kFakeCoordY + 200); 1429 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1430 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_END)); 1431} 1432 1433// Verify that pinch zoom sends the proper event sequence. 1434TEST_F(GestureProviderTest, PinchZoom) { 1435 base::TimeTicks event_time = base::TimeTicks::Now(); 1436 const float touch_slop = GetTouchSlop(); 1437 const float raw_offset_x = 3.2f; 1438 const float raw_offset_y = 4.3f; 1439 int motion_event_id = 0; 1440 1441 gesture_provider_->SetDoubleTapSupportForPageEnabled(false); 1442 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 1443 gesture_provider_->SetMultiTouchZoomSupportEnabled(true); 1444 1445 int secondary_coord_x = kFakeCoordX + 20 * touch_slop; 1446 int secondary_coord_y = kFakeCoordY + 20 * touch_slop; 1447 1448 MockMotionEvent event = 1449 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1450 event.SetId(++motion_event_id); 1451 event.SetRawOffset(raw_offset_x, raw_offset_y); 1452 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1453 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1454 EXPECT_EQ(kFakeCoordX, GetMostRecentGestureEvent().x); 1455 EXPECT_EQ(kFakeCoordY, GetMostRecentGestureEvent().y); 1456 EXPECT_EQ(kFakeCoordX + raw_offset_x, GetMostRecentGestureEvent().raw_x); 1457 EXPECT_EQ(kFakeCoordY + raw_offset_y, GetMostRecentGestureEvent().raw_y); 1458 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1459 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 1460 GetMostRecentGestureEvent().details.bounding_box()); 1461 1462 // Toggling double-tap support should not take effect until the next sequence. 1463 gesture_provider_->SetDoubleTapSupportForPageEnabled(true); 1464 1465 event = ObtainMotionEvent(event_time, 1466 MotionEvent::ACTION_POINTER_DOWN, 1467 kFakeCoordX, 1468 kFakeCoordY, 1469 secondary_coord_x, 1470 secondary_coord_y); 1471 event.SetId(++motion_event_id); 1472 event.SetRawOffset(raw_offset_x, raw_offset_y); 1473 1474 gesture_provider_->OnTouchEvent(event); 1475 EXPECT_EQ(1U, GetReceivedGestureCount()); 1476 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1477 EXPECT_EQ(BoundsForSingleMockTouchAtLocation(kFakeCoordX, kFakeCoordY), 1478 GetMostRecentGestureEvent().details.bounding_box()); 1479 1480 secondary_coord_x += 5 * touch_slop; 1481 secondary_coord_y += 5 * touch_slop; 1482 event = ObtainMotionEvent(event_time, 1483 MotionEvent::ACTION_MOVE, 1484 kFakeCoordX, 1485 kFakeCoordY, 1486 secondary_coord_x, 1487 secondary_coord_y); 1488 event.SetId(++motion_event_id); 1489 event.SetRawOffset(raw_offset_x, raw_offset_y); 1490 1491 // Toggling double-tap support should not take effect until the next sequence. 1492 gesture_provider_->SetDoubleTapSupportForPageEnabled(false); 1493 1494 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1495 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 1496 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1497 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN)); 1498 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1499 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE)); 1500 1501 EXPECT_EQ((kFakeCoordX + secondary_coord_x) / 2, GetReceivedGesture(3).x); 1502 EXPECT_EQ((kFakeCoordY + secondary_coord_y) / 2, GetReceivedGesture(3).y); 1503 EXPECT_EQ((kFakeCoordX + secondary_coord_x) / 2 + raw_offset_x, 1504 GetReceivedGesture(3).raw_x); 1505 EXPECT_EQ((kFakeCoordY + secondary_coord_y) / 2 + raw_offset_y, 1506 GetReceivedGesture(3).raw_y); 1507 1508 EXPECT_EQ( 1509 gfx::RectF(kFakeCoordX - kMockTouchRadius, 1510 kFakeCoordY - kMockTouchRadius, 1511 secondary_coord_x - kFakeCoordX + kMockTouchRadius * 2, 1512 secondary_coord_y - kFakeCoordY + kMockTouchRadius * 2), 1513 GetMostRecentGestureEvent().details.bounding_box()); 1514 1515 secondary_coord_x += 2 * touch_slop; 1516 secondary_coord_y += 2 * touch_slop; 1517 event = ObtainMotionEvent(event_time, 1518 MotionEvent::ACTION_MOVE, 1519 kFakeCoordX, 1520 kFakeCoordY, 1521 secondary_coord_x, 1522 secondary_coord_y); 1523 event.SetId(++motion_event_id); 1524 1525 // Toggling double-tap support should not take effect until the next sequence. 1526 gesture_provider_->SetDoubleTapSupportForPageEnabled(true); 1527 1528 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1529 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 1530 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE)); 1531 EXPECT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType()); 1532 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1533 EXPECT_LT(1.f, GetMostRecentGestureEvent().details.scale()); 1534 EXPECT_EQ( 1535 gfx::RectF(kFakeCoordX - kMockTouchRadius, 1536 kFakeCoordY - kMockTouchRadius, 1537 secondary_coord_x - kFakeCoordX + kMockTouchRadius * 2, 1538 secondary_coord_y - kFakeCoordY + kMockTouchRadius * 2), 1539 GetMostRecentGestureEvent().details.bounding_box()); 1540 1541 event = ObtainMotionEvent(event_time, 1542 MotionEvent::ACTION_POINTER_UP, 1543 kFakeCoordX, 1544 kFakeCoordY, 1545 secondary_coord_x, 1546 secondary_coord_y); 1547 event.SetId(++motion_event_id); 1548 1549 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1550 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 1551 EXPECT_EQ(ET_GESTURE_PINCH_END, GetMostRecentGestureEventType()); 1552 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1553 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_END)); 1554 EXPECT_EQ( 1555 gfx::RectF(kFakeCoordX - kMockTouchRadius, 1556 kFakeCoordY - kMockTouchRadius, 1557 secondary_coord_x - kFakeCoordX + kMockTouchRadius * 2, 1558 secondary_coord_y - kFakeCoordY + kMockTouchRadius * 2), 1559 GetMostRecentGestureEvent().details.bounding_box()); 1560 1561 event = ObtainMotionEvent(event_time, MotionEvent::ACTION_UP); 1562 gesture_provider_->OnTouchEvent(event); 1563 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 1564 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1565 EXPECT_EQ(gfx::RectF(kFakeCoordX - kMockTouchRadius, 1566 kFakeCoordY - kMockTouchRadius, 1567 kMockTouchRadius * 2, 1568 kMockTouchRadius * 2), 1569 GetMostRecentGestureEvent().details.bounding_box()); 1570} 1571 1572// Verify that pinch zoom that uses touch major values sends the proper event 1573// sequence. 1574TEST_F(GestureProviderTest, PinchZoomWithTouchMajor) { 1575 EnableTouchMajorUseInScaling(); 1576 1577 base::TimeTicks event_time = base::TimeTicks::Now(); 1578 const float touch_slop = GetTouchSlop(); 1579 gesture_provider_->SetMultiTouchZoomSupportEnabled(true); 1580 1581 int secondary_coord_x = kFakeCoordX + 20 * touch_slop; 1582 int secondary_coord_y = kFakeCoordY + 20 * touch_slop; 1583 1584 MockMotionEvent event = 1585 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1586 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1587 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1588 1589 event = ObtainMotionEvent(event_time, 1590 MotionEvent::ACTION_POINTER_DOWN, 1591 kFakeCoordX, 1592 kFakeCoordY, 1593 secondary_coord_x, 1594 secondary_coord_y); 1595 1596 gesture_provider_->OnTouchEvent(event); 1597 EXPECT_EQ(1U, GetReceivedGestureCount()); 1598 1599 secondary_coord_x += 5 * touch_slop; 1600 secondary_coord_y += 5 * touch_slop; 1601 event = ObtainMotionEvent(event_time, 1602 MotionEvent::ACTION_MOVE, 1603 kFakeCoordX, 1604 kFakeCoordY, 1605 secondary_coord_x, 1606 secondary_coord_y); 1607 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1608 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1609 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN)); 1610 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1611 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE)); 1612 1613 secondary_coord_x += 2 * touch_slop; 1614 secondary_coord_y += 2 * touch_slop; 1615 event = ObtainMotionEvent(event_time, 1616 MotionEvent::ACTION_MOVE, 1617 kFakeCoordX, 1618 kFakeCoordY, 1619 secondary_coord_x, 1620 secondary_coord_y); 1621 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1622 EXPECT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType()); 1623 EXPECT_LT(1.f, GetMostRecentGestureEvent().details.scale()); 1624 1625 secondary_coord_x -= 2 * touch_slop; 1626 secondary_coord_y -= 2 * touch_slop; 1627 event = ObtainMotionEvent(event_time, 1628 MotionEvent::ACTION_MOVE, 1629 kFakeCoordX, 1630 kFakeCoordY, 1631 secondary_coord_x, 1632 secondary_coord_y); 1633 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1634 EXPECT_EQ(ET_GESTURE_PINCH_UPDATE, GetMostRecentGestureEventType()); 1635 EXPECT_GT(1.f, GetMostRecentGestureEvent().details.scale()); 1636 1637 event = ObtainMotionEvent(event_time, 1638 MotionEvent::ACTION_POINTER_UP, 1639 kFakeCoordX, 1640 kFakeCoordY, 1641 secondary_coord_x, 1642 secondary_coord_y); 1643 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1644 EXPECT_EQ(ET_GESTURE_PINCH_END, GetMostRecentGestureEventType()); 1645 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SCROLL_END)); 1646 1647 event = ObtainMotionEvent(event_time, MotionEvent::ACTION_UP); 1648 gesture_provider_->OnTouchEvent(event); 1649 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 1650} 1651 1652// Verify that no accidental pinching occurs if the touch size is large relative 1653// to the min scaling span when the touch major value is used in scaling. 1654TEST_F(GestureProviderTest, NoPinchZoomWithTouchMajorAndFatFinger) { 1655 EnableTouchMajorUseInScaling(); 1656 1657 base::TimeTicks event_time = base::TimeTicks::Now(); 1658 const float kFatFingerSize = GetMinScalingSpan() * 3.f; 1659 1660 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 1661 gesture_provider_->SetMultiTouchZoomSupportEnabled(true); 1662 1663 MockMotionEvent event = 1664 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1665 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1666 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1667 EXPECT_EQ(1U, GetReceivedGestureCount()); 1668 1669 event = ObtainMotionEvent(event_time + kOneSecond, 1670 MotionEvent::ACTION_MOVE); 1671 event.SetTouchMajor(0.1f); 1672 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1673 EXPECT_EQ(1U, GetReceivedGestureCount()); 1674 1675 event = ObtainMotionEvent(event_time + kOneSecond * 2, 1676 MotionEvent::ACTION_MOVE, 1677 kFakeCoordX + 1.f, 1678 kFakeCoordY); 1679 event.SetTouchMajor(1.f); 1680 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1681 EXPECT_EQ(1U, GetReceivedGestureCount()); 1682 1683 event = ObtainMotionEvent(event_time + kOneSecond * 3, 1684 MotionEvent::ACTION_MOVE); 1685 event.SetTouchMajor(kFatFingerSize * 3.5f); 1686 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1687 EXPECT_EQ(1U, GetReceivedGestureCount()); 1688 1689 event = ObtainMotionEvent(event_time + kOneSecond * 4, 1690 MotionEvent::ACTION_MOVE); 1691 event.SetTouchMajor(kFatFingerSize * 5.f); 1692 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1693 EXPECT_EQ(1U, GetReceivedGestureCount()); 1694} 1695 1696// Verify that multi-finger swipe sends the proper event sequence. 1697TEST_F(GestureProviderTest, MultiFingerSwipe) { 1698 EnableSwipe(); 1699 gesture_provider_->SetMultiTouchZoomSupportEnabled(false); 1700 const float min_swipe_velocity = GetMinSwipeVelocity(); 1701 1702 // One finger - swipe right 1703 OneFingerSwipe(2 * min_swipe_velocity, 0); 1704 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1705 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_right()); 1706 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1707 ResetGestureDetection(); 1708 1709 // One finger - swipe left 1710 OneFingerSwipe(-2 * min_swipe_velocity, 0); 1711 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1712 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_left()); 1713 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1714 ResetGestureDetection(); 1715 1716 // One finger - swipe down 1717 OneFingerSwipe(0, 2 * min_swipe_velocity); 1718 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1719 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_down()); 1720 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1721 ResetGestureDetection(); 1722 1723 // One finger - swipe up 1724 OneFingerSwipe(0, -2 * min_swipe_velocity); 1725 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1726 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_up()); 1727 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1728 ResetGestureDetection(); 1729 1730 // Two fingers 1731 // Swipe right. 1732 TwoFingerSwipe(min_swipe_velocity * 2, 0, min_swipe_velocity * 2, 0); 1733 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1734 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_right()); 1735 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1736 ResetGestureDetection(); 1737 1738 // Swipe left. 1739 TwoFingerSwipe(-min_swipe_velocity * 2, 0, -min_swipe_velocity * 2, 0); 1740 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1741 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_left()); 1742 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1743 ResetGestureDetection(); 1744 1745 // No swipe with different touch directions. 1746 TwoFingerSwipe(min_swipe_velocity * 2, 0, -min_swipe_velocity * 2, 0); 1747 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1748 ResetGestureDetection(); 1749 1750 // No swipe without a dominant direction. 1751 TwoFingerSwipe(min_swipe_velocity * 2, 1752 min_swipe_velocity * 2, 1753 min_swipe_velocity * 2, 1754 min_swipe_velocity * 2); 1755 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1756 ResetGestureDetection(); 1757 1758 // Swipe down with non-zero velocities on both axes and dominant direction. 1759 TwoFingerSwipe(-min_swipe_velocity, 1760 min_swipe_velocity * 4, 1761 -min_swipe_velocity, 1762 min_swipe_velocity * 4); 1763 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1764 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_down()); 1765 EXPECT_FALSE(GetMostRecentGestureEvent().details.swipe_left()); 1766 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1767 ResetGestureDetection(); 1768 1769 // Swipe up with non-zero velocities on both axes. 1770 TwoFingerSwipe(min_swipe_velocity, 1771 -min_swipe_velocity * 4, 1772 min_swipe_velocity, 1773 -min_swipe_velocity * 4); 1774 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1775 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_up()); 1776 EXPECT_FALSE(GetMostRecentGestureEvent().details.swipe_right()); 1777 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1778 ResetGestureDetection(); 1779 1780 // No swipe without sufficient velocity. 1781 TwoFingerSwipe(min_swipe_velocity / 2, 0, 0, 0); 1782 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1783 ResetGestureDetection(); 1784 1785 // Swipe up with one small and one medium velocity in slightly different but 1786 // not opposing directions. 1787 TwoFingerSwipe(min_swipe_velocity / 2, 1788 min_swipe_velocity / 2, 1789 0, 1790 min_swipe_velocity * 2); 1791 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1792 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_down()); 1793 EXPECT_FALSE(GetMostRecentGestureEvent().details.swipe_right()); 1794 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1795 ResetGestureDetection(); 1796 1797 // No swipe in orthogonal directions. 1798 TwoFingerSwipe(min_swipe_velocity * 2, 0, 0, min_swipe_velocity * 7); 1799 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1800 ResetGestureDetection(); 1801 1802 // Three finger swipe in same directions. 1803 ThreeFingerSwipe(min_swipe_velocity * 2, 1804 0, 1805 min_swipe_velocity * 3, 1806 0, 1807 min_swipe_velocity * 4, 1808 0); 1809 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1810 EXPECT_TRUE(GetMostRecentGestureEvent().details.swipe_right()); 1811 EXPECT_EQ(3, GetMostRecentGestureEvent().details.touch_points()); 1812 ResetGestureDetection(); 1813 1814 // No three finger swipe in different directions. 1815 ThreeFingerSwipe(min_swipe_velocity * 2, 1816 0, 1817 0, 1818 min_swipe_velocity * 3, 1819 min_swipe_velocity * 4, 1820 0); 1821 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_SWIPE)); 1822} 1823 1824// Verify that the timer of LONG_PRESS will be cancelled when scrolling begins 1825// so LONG_PRESS and LONG_TAP won't be triggered. 1826TEST_F(GestureProviderTest, GesturesCancelledAfterLongPressCausesLostFocus) { 1827 base::TimeTicks event_time = base::TimeTicks::Now(); 1828 1829 MockMotionEvent event = 1830 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1831 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1832 1833 const base::TimeDelta long_press_timeout = 1834 GetLongPressTimeout() + GetShowPressTimeout() + kOneMicrosecond; 1835 RunTasksAndWait(long_press_timeout); 1836 EXPECT_EQ(ET_GESTURE_LONG_PRESS, GetMostRecentGestureEventType()); 1837 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1838 1839 EXPECT_TRUE(CancelActiveTouchSequence()); 1840 EXPECT_FALSE(HasDownEvent()); 1841 1842 event = ObtainMotionEvent(event_time + long_press_timeout, 1843 MotionEvent::ACTION_UP); 1844 gesture_provider_->OnTouchEvent(event); 1845 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_LONG_TAP)); 1846} 1847 1848// Verify that inserting a touch cancel event will trigger proper touch and 1849// gesture sequence cancellation. 1850TEST_F(GestureProviderTest, CancelActiveTouchSequence) { 1851 base::TimeTicks event_time = base::TimeTicks::Now(); 1852 int motion_event_id = 0; 1853 1854 EXPECT_FALSE(CancelActiveTouchSequence()); 1855 EXPECT_EQ(0U, GetReceivedGestureCount()); 1856 1857 MockMotionEvent event = 1858 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 1859 event.SetId(++motion_event_id); 1860 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1861 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1862 EXPECT_EQ(motion_event_id, GetMostRecentGestureEvent().motion_event_id); 1863 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1864 1865 ASSERT_TRUE(CancelActiveTouchSequence()); 1866 EXPECT_FALSE(HasDownEvent()); 1867 1868 // Subsequent MotionEvent's are dropped until ACTION_DOWN. 1869 event = ObtainMotionEvent(event_time + kOneMicrosecond, 1870 MotionEvent::ACTION_MOVE); 1871 EXPECT_FALSE(gesture_provider_->OnTouchEvent(event)); 1872 1873 event = ObtainMotionEvent(event_time + kOneMicrosecond * 2, 1874 MotionEvent::ACTION_UP); 1875 EXPECT_FALSE(gesture_provider_->OnTouchEvent(event)); 1876 1877 event = ObtainMotionEvent(event_time + kOneMicrosecond * 3, 1878 MotionEvent::ACTION_DOWN); 1879 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1880 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1881 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1882} 1883 1884TEST_F(GestureProviderTest, DoubleTapDragZoomCancelledOnSecondaryPointerDown) { 1885 const base::TimeTicks down_time_1 = TimeTicks::Now(); 1886 const base::TimeTicks down_time_2 = down_time_1 + GetValidDoubleTapDelay(); 1887 1888 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 1889 1890 MockMotionEvent event = 1891 ObtainMotionEvent(down_time_1, MotionEvent::ACTION_DOWN); 1892 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1893 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1894 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1895 1896 event = 1897 ObtainMotionEvent(down_time_1 + kOneMicrosecond, MotionEvent::ACTION_UP); 1898 gesture_provider_->OnTouchEvent(event); 1899 EXPECT_EQ(ET_GESTURE_TAP_UNCONFIRMED, GetMostRecentGestureEventType()); 1900 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1901 1902 event = ObtainMotionEvent(down_time_2, MotionEvent::ACTION_DOWN); 1903 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1904 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1905 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1906 1907 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond, 1908 MotionEvent::ACTION_MOVE, 1909 kFakeCoordX, 1910 kFakeCoordY - 30); 1911 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1912 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 1913 EXPECT_EQ(ET_GESTURE_PINCH_BEGIN, GetMostRecentGestureEventType()); 1914 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1915 1916 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 2, 1917 MotionEvent::ACTION_POINTER_DOWN, 1918 kFakeCoordX, 1919 kFakeCoordY - 30, 1920 kFakeCoordX + 50, 1921 kFakeCoordY + 50); 1922 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1923 EXPECT_EQ(ET_GESTURE_PINCH_END, GetMostRecentGestureEventType()); 1924 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1925 1926 const size_t gesture_count = GetReceivedGestureCount(); 1927 event = ObtainMotionEvent(down_time_2 + kOneMicrosecond * 3, 1928 MotionEvent::ACTION_POINTER_UP, 1929 kFakeCoordX, 1930 kFakeCoordY - 30, 1931 kFakeCoordX + 50, 1932 kFakeCoordY + 50); 1933 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1934 EXPECT_EQ(gesture_count, GetReceivedGestureCount()); 1935 1936 event = ObtainMotionEvent(down_time_2 + kOneSecond, 1937 MotionEvent::ACTION_UP); 1938 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1939 EXPECT_EQ(gesture_count + 1, GetReceivedGestureCount()); 1940 EXPECT_EQ(ET_GESTURE_SCROLL_END, GetMostRecentGestureEventType()); 1941 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1942} 1943 1944// Verify that gesture begin and gesture end events are dispatched correctly. 1945TEST_F(GestureProviderTest, GestureBeginAndEnd) { 1946 EnableBeginEndTypes(); 1947 base::TimeTicks event_time = base::TimeTicks::Now(); 1948 const float raw_offset_x = 7.5f; 1949 const float raw_offset_y = 5.7f; 1950 1951 EXPECT_EQ(0U, GetReceivedGestureCount()); 1952 MockMotionEvent event = 1953 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN, 1, 1); 1954 event.pointer_count = 1; 1955 event.SetRawOffset(raw_offset_x, raw_offset_y); 1956 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1957 EXPECT_EQ(ET_GESTURE_BEGIN, GetReceivedGesture(0).type()); 1958 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 1959 EXPECT_EQ(2U, GetReceivedGestureCount()); 1960 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 1961 EXPECT_EQ(1, GetMostRecentGestureEvent().x); 1962 EXPECT_EQ(1, GetMostRecentGestureEvent().y); 1963 EXPECT_EQ(1 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 1964 EXPECT_EQ(1 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 1965 EXPECT_EQ(gfx::RectF(1 - kMockTouchRadius, 1966 1 - kMockTouchRadius, 1967 kMockTouchRadius * 2, 1968 kMockTouchRadius * 2), 1969 GetMostRecentGestureEvent().details.bounding_box()); 1970 1971 event = ObtainMotionEvent( 1972 event_time, MotionEvent::ACTION_POINTER_DOWN, 1, 1, 2, 2); 1973 event.pointer_count = 2; 1974 event.SetRawOffset(raw_offset_x, raw_offset_y); 1975 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1976 EXPECT_EQ(ET_GESTURE_BEGIN, GetMostRecentGestureEventType()); 1977 EXPECT_EQ(3U, GetReceivedGestureCount()); 1978 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 1979 EXPECT_EQ(2, GetMostRecentGestureEvent().x); 1980 EXPECT_EQ(2, GetMostRecentGestureEvent().y); 1981 EXPECT_EQ(2 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 1982 EXPECT_EQ(2 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 1983 1984 event = ObtainMotionEvent( 1985 event_time, MotionEvent::ACTION_POINTER_DOWN, 1, 1, 2, 2, 3, 3); 1986 event.pointer_count = 3; 1987 event.SetRawOffset(raw_offset_x, raw_offset_y); 1988 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 1989 EXPECT_EQ(ET_GESTURE_BEGIN, GetMostRecentGestureEventType()); 1990 EXPECT_EQ(4U, GetReceivedGestureCount()); 1991 EXPECT_EQ(3, GetMostRecentGestureEvent().details.touch_points()); 1992 EXPECT_EQ(3, GetMostRecentGestureEvent().x); 1993 EXPECT_EQ(3, GetMostRecentGestureEvent().y); 1994 EXPECT_EQ(3 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 1995 EXPECT_EQ(3 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 1996 1997 event = ObtainMotionEvent( 1998 event_time, MotionEvent::ACTION_POINTER_UP, 1, 1, 2, 2, 3, 3); 1999 event.pointer_count = 2; 2000 event.SetRawOffset(raw_offset_x, raw_offset_y); 2001 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2002 EXPECT_EQ(ET_GESTURE_END, GetMostRecentGestureEventType()); 2003 EXPECT_EQ(5U, GetReceivedGestureCount()); 2004 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 2005 EXPECT_EQ(1, GetMostRecentGestureEvent().x); 2006 EXPECT_EQ(1, GetMostRecentGestureEvent().y); 2007 EXPECT_EQ(1 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 2008 EXPECT_EQ(1 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 2009 2010 event = ObtainMotionEvent( 2011 event_time, MotionEvent::ACTION_POINTER_DOWN, 2, 2, 3, 3, 4, 4); 2012 event.SetRawOffset(raw_offset_x, raw_offset_y); 2013 event.pointer_count = 3; 2014 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2015 EXPECT_EQ(ET_GESTURE_BEGIN, GetMostRecentGestureEventType()); 2016 EXPECT_EQ(6U, GetReceivedGestureCount()); 2017 EXPECT_EQ(3, GetMostRecentGestureEvent().details.touch_points()); 2018 EXPECT_EQ(4, GetMostRecentGestureEvent().x); 2019 EXPECT_EQ(4, GetMostRecentGestureEvent().y); 2020 EXPECT_EQ(4 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 2021 EXPECT_EQ(4 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 2022 2023 event = ObtainMotionEvent( 2024 event_time, MotionEvent::ACTION_POINTER_UP, 2, 2, 3, 3, 4, 4); 2025 event.pointer_count = 2; 2026 event.SetRawOffset(raw_offset_x, raw_offset_y); 2027 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2028 EXPECT_EQ(ET_GESTURE_END, GetMostRecentGestureEventType()); 2029 EXPECT_EQ(7U, GetReceivedGestureCount()); 2030 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 2031 EXPECT_EQ(2, GetMostRecentGestureEvent().x); 2032 EXPECT_EQ(2, GetMostRecentGestureEvent().y); 2033 EXPECT_EQ(2 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 2034 EXPECT_EQ(2 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 2035 2036 event = 2037 ObtainMotionEvent(event_time, MotionEvent::ACTION_POINTER_UP, 3, 3, 4, 4); 2038 event.pointer_count = 1; 2039 event.SetRawOffset(raw_offset_x, raw_offset_y); 2040 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2041 EXPECT_EQ(ET_GESTURE_END, GetMostRecentGestureEventType()); 2042 EXPECT_EQ(8U, GetReceivedGestureCount()); 2043 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 2044 EXPECT_EQ(3, GetMostRecentGestureEvent().x); 2045 EXPECT_EQ(3, GetMostRecentGestureEvent().y); 2046 EXPECT_EQ(3 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 2047 EXPECT_EQ(3 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 2048 2049 2050 event = ObtainMotionEvent(event_time, MotionEvent::ACTION_UP, 4, 4); 2051 event.pointer_count = 1; 2052 event.SetRawOffset(raw_offset_x, raw_offset_y); 2053 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2054 EXPECT_EQ(ET_GESTURE_END, GetMostRecentGestureEventType()); 2055 EXPECT_EQ(9U, GetReceivedGestureCount()); 2056 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 2057 EXPECT_EQ(4, GetMostRecentGestureEvent().x); 2058 EXPECT_EQ(4, GetMostRecentGestureEvent().y); 2059 EXPECT_EQ(4 + raw_offset_x, GetMostRecentGestureEvent().raw_x); 2060 EXPECT_EQ(4 + raw_offset_y, GetMostRecentGestureEvent().raw_y); 2061} 2062 2063// Verify that gesture begin and gesture end events are dispatched correctly 2064// when an ACTION_CANCEL is received. 2065TEST_F(GestureProviderTest, GestureBeginAndEndOnCancel) { 2066 EnableBeginEndTypes(); 2067 base::TimeTicks event_time = base::TimeTicks::Now(); 2068 2069 EXPECT_EQ(0U, GetReceivedGestureCount()); 2070 MockMotionEvent event = 2071 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN, 1, 1); 2072 event.pointer_count = 1; 2073 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2074 EXPECT_EQ(ET_GESTURE_BEGIN, GetReceivedGesture(0).type()); 2075 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 2076 EXPECT_EQ(2U, GetReceivedGestureCount()); 2077 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 2078 EXPECT_EQ(gfx::RectF(1 - kMockTouchRadius, 2079 1 - kMockTouchRadius, 2080 kMockTouchRadius * 2, 2081 kMockTouchRadius * 2), 2082 GetMostRecentGestureEvent().details.bounding_box()); 2083 EXPECT_EQ(1, GetMostRecentGestureEvent().x); 2084 EXPECT_EQ(1, GetMostRecentGestureEvent().y); 2085 2086 event = ObtainMotionEvent( 2087 event_time, MotionEvent::ACTION_POINTER_DOWN, 1, 1, 2, 2); 2088 event.pointer_count = 2; 2089 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2090 EXPECT_EQ(ET_GESTURE_BEGIN, GetMostRecentGestureEventType()); 2091 EXPECT_EQ(3U, GetReceivedGestureCount()); 2092 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 2093 EXPECT_EQ(2, GetMostRecentGestureEvent().x); 2094 EXPECT_EQ(2, GetMostRecentGestureEvent().y); 2095 2096 2097 event = ObtainMotionEvent( 2098 event_time, MotionEvent::ACTION_POINTER_DOWN, 1, 1, 2, 2, 3, 3); 2099 event.pointer_count = 3; 2100 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2101 EXPECT_EQ(ET_GESTURE_BEGIN, GetMostRecentGestureEventType()); 2102 EXPECT_EQ(4U, GetReceivedGestureCount()); 2103 EXPECT_EQ(3, GetMostRecentGestureEvent().details.touch_points()); 2104 EXPECT_EQ(3, GetMostRecentGestureEvent().x); 2105 EXPECT_EQ(3, GetMostRecentGestureEvent().y); 2106 2107 event = ObtainMotionEvent( 2108 event_time, MotionEvent::ACTION_CANCEL, 1, 1, 2, 2, 3, 3); 2109 event.pointer_count = 3; 2110 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2111 EXPECT_EQ(7U, GetReceivedGestureCount()); 2112 EXPECT_EQ(3, GetReceivedGesture(4).details.touch_points()); 2113 EXPECT_EQ(ET_GESTURE_END, GetReceivedGesture(4).type()); 2114 EXPECT_EQ(2, GetReceivedGesture(5).details.touch_points()); 2115 EXPECT_EQ(ET_GESTURE_END, GetReceivedGesture(5).type()); 2116 EXPECT_EQ(1, GetReceivedGesture(6).details.touch_points()); 2117 EXPECT_EQ(ET_GESTURE_END, GetReceivedGesture(6).type()); 2118 EXPECT_EQ(1, GetReceivedGesture(4).x); 2119 EXPECT_EQ(1, GetReceivedGesture(4).y); 2120 EXPECT_EQ(2, GetReceivedGesture(5).x); 2121 EXPECT_EQ(2, GetReceivedGesture(5).y); 2122 EXPECT_EQ(3, GetReceivedGesture(6).x); 2123 EXPECT_EQ(3, GetReceivedGesture(6).y); 2124} 2125 2126// Test a simple two finger tap 2127TEST_F(GestureProviderTest, TwoFingerTap) { 2128 // The time between ACTION_POINTER_DOWN and ACTION_POINTER_UP must be <= the 2129 // two finger tap delay. 2130 EnableTwoFingerTap(kMaxTwoFingerTapSeparation, base::TimeDelta()); 2131 const float scaled_touch_slop = GetTouchSlop(); 2132 2133 base::TimeTicks event_time = base::TimeTicks::Now(); 2134 2135 MockMotionEvent event = 2136 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN, 0, 0); 2137 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2138 2139 event = ObtainMotionEvent(event_time, 2140 MotionEvent::ACTION_MOVE, 2141 0, 2142 scaled_touch_slop / 2); 2143 2144 event = ObtainMotionEvent(event_time, 2145 MotionEvent::ACTION_POINTER_DOWN, 2146 0, 2147 0, 2148 kMaxTwoFingerTapSeparation / 2, 2149 0); 2150 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2151 2152 event = 2153 ObtainMotionEvent(event_time, 2154 MotionEvent::ACTION_MOVE, 2155 0, 2156 -scaled_touch_slop / 2, 2157 kMaxTwoFingerTapSeparation / 2 + scaled_touch_slop / 2, 2158 0); 2159 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2160 2161 event = ObtainMotionEvent(event_time, 2162 MotionEvent::ACTION_POINTER_UP, 2163 0, 2164 0, 2165 kMaxTwoFingerTapSeparation, 2166 0); 2167 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2168 2169 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetReceivedGesture(0).type()); 2170 EXPECT_EQ(ET_GESTURE_SCROLL_BEGIN, GetReceivedGesture(1).type()); 2171 EXPECT_EQ(ET_GESTURE_TWO_FINGER_TAP, GetReceivedGesture(2).type()); 2172 EXPECT_EQ(3U, GetReceivedGestureCount()); 2173 2174 EXPECT_EQ(kMockTouchRadius * 2, 2175 GetReceivedGesture(2).details.first_finger_width()); 2176 EXPECT_EQ(kMockTouchRadius * 2, 2177 GetReceivedGesture(2).details.first_finger_height()); 2178} 2179 2180// Test preventing a two finger tap via finger movement. 2181TEST_F(GestureProviderTest, TwoFingerTapCancelledByFingerMovement) { 2182 EnableTwoFingerTap(kMaxTwoFingerTapSeparation, base::TimeDelta()); 2183 const float scaled_touch_slop = GetTouchSlop(); 2184 base::TimeTicks event_time = base::TimeTicks::Now(); 2185 2186 MockMotionEvent event = ObtainMotionEvent( 2187 event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 2188 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2189 2190 event = ObtainMotionEvent(event_time, 2191 MotionEvent::ACTION_POINTER_DOWN, 2192 kFakeCoordX, 2193 kFakeCoordY, 2194 kFakeCoordX, 2195 kFakeCoordY); 2196 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2197 2198 event = ObtainMotionEvent(event_time, 2199 MotionEvent::ACTION_MOVE, 2200 kFakeCoordX, 2201 kFakeCoordY, 2202 kFakeCoordX + scaled_touch_slop + 0.1, 2203 kFakeCoordY); 2204 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2205 2206 event = ObtainMotionEvent(event_time, 2207 MotionEvent::ACTION_POINTER_UP, 2208 kFakeCoordX, 2209 kFakeCoordY, 2210 kFakeCoordX, 2211 kFakeCoordY); 2212 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2213 2214 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetReceivedGesture(0).type()); 2215 EXPECT_EQ(ET_GESTURE_SCROLL_BEGIN, GetReceivedGesture(1).type()); 2216 EXPECT_EQ(2U, GetReceivedGestureCount()); 2217} 2218 2219// Test preventing a two finger tap by waiting too long before releasing the 2220// secondary pointer. 2221TEST_F(GestureProviderTest, TwoFingerTapCancelledByDelay) { 2222 base::TimeDelta two_finger_tap_timeout = kOneSecond; 2223 EnableTwoFingerTap(kMaxTwoFingerTapSeparation, two_finger_tap_timeout); 2224 base::TimeTicks event_time = base::TimeTicks::Now(); 2225 2226 MockMotionEvent event = ObtainMotionEvent( 2227 event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 2228 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2229 2230 event = ObtainMotionEvent(event_time, 2231 MotionEvent::ACTION_MOVE, 2232 kFakeCoordX, 2233 kFakeCoordY); 2234 2235 event = ObtainMotionEvent(event_time, 2236 MotionEvent::ACTION_POINTER_DOWN, 2237 kFakeCoordX, 2238 kFakeCoordY, 2239 kFakeCoordX + kMaxTwoFingerTapSeparation / 2, 2240 kFakeCoordY); 2241 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2242 2243 event = ObtainMotionEvent(event_time + kOneSecond + kOneMicrosecond, 2244 MotionEvent::ACTION_POINTER_UP, 2245 kFakeCoordX, 2246 kFakeCoordY, 2247 kFakeCoordX + kMaxTwoFingerTapSeparation / 2, 2248 kFakeCoordY); 2249 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2250 2251 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetReceivedGesture(0).type()); 2252 EXPECT_EQ(1U, GetReceivedGestureCount()); 2253} 2254 2255// Test preventing a two finger tap by pressing the secondary pointer too far 2256// from the first 2257TEST_F(GestureProviderTest, TwoFingerTapCancelledByDistanceBetweenPointers) { 2258 EnableTwoFingerTap(kMaxTwoFingerTapSeparation, base::TimeDelta()); 2259 base::TimeTicks event_time = base::TimeTicks::Now(); 2260 2261 MockMotionEvent event = ObtainMotionEvent( 2262 event_time, MotionEvent::ACTION_DOWN, kFakeCoordX, kFakeCoordY); 2263 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2264 2265 event = ObtainMotionEvent(event_time, 2266 MotionEvent::ACTION_POINTER_DOWN, 2267 kFakeCoordX, 2268 kFakeCoordY, 2269 kFakeCoordX + kMaxTwoFingerTapSeparation, 2270 kFakeCoordY); 2271 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2272 2273 event = ObtainMotionEvent(event_time, 2274 MotionEvent::ACTION_POINTER_UP, 2275 kFakeCoordX, 2276 kFakeCoordY, 2277 kFakeCoordX + kMaxTwoFingerTapSeparation, 2278 kFakeCoordY); 2279 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2280 2281 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetReceivedGesture(0).type()); 2282 EXPECT_EQ(1U, GetReceivedGestureCount()); 2283} 2284 2285// Verify that pinch zoom only sends updates which exceed the 2286// min_pinch_update_span_delta. 2287TEST_F(GestureProviderTest, PinchZoomWithThreshold) { 2288 const float kMinPinchUpdateDistance = 5; 2289 2290 base::TimeTicks event_time = base::TimeTicks::Now(); 2291 const float touch_slop = GetTouchSlop(); 2292 2293 SetMinPinchUpdateSpanDelta(kMinPinchUpdateDistance); 2294 gesture_provider_->SetDoubleTapSupportForPageEnabled(false); 2295 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(true); 2296 gesture_provider_->SetMultiTouchZoomSupportEnabled(true); 2297 2298 int secondary_coord_x = kFakeCoordX + 20 * touch_slop; 2299 int secondary_coord_y = kFakeCoordY + 20 * touch_slop; 2300 2301 // First finger down. 2302 MockMotionEvent event = 2303 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 2304 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2305 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 2306 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 2307 2308 // Second finger down. 2309 event = ObtainMotionEvent(event_time, 2310 MotionEvent::ACTION_POINTER_DOWN, 2311 kFakeCoordX, 2312 kFakeCoordY, 2313 secondary_coord_x, 2314 secondary_coord_y); 2315 2316 gesture_provider_->OnTouchEvent(event); 2317 EXPECT_EQ(1U, GetReceivedGestureCount()); 2318 EXPECT_EQ(1, GetMostRecentGestureEvent().details.touch_points()); 2319 2320 // Move second finger. 2321 secondary_coord_x += 5 * touch_slop; 2322 secondary_coord_y += 5 * touch_slop; 2323 event = ObtainMotionEvent(event_time, 2324 MotionEvent::ACTION_MOVE, 2325 kFakeCoordX, 2326 kFakeCoordY, 2327 secondary_coord_x, 2328 secondary_coord_y); 2329 2330 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2331 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 2332 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_BEGIN)); 2333 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE)); 2334 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_BEGIN)); 2335 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_SCROLL_UPDATE)); 2336 2337 // Small move, shouldn't trigger pinch. 2338 event = ObtainMotionEvent(event_time, 2339 MotionEvent::ACTION_MOVE, 2340 kFakeCoordX, 2341 kFakeCoordY, 2342 secondary_coord_x + kMinPinchUpdateDistance, 2343 secondary_coord_y); 2344 2345 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2346 EXPECT_FALSE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE)); 2347 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 2348 2349 // Small move, but combined with the previous move, should trigger pinch. We 2350 // need to overshoot kMinPinchUpdateDistance by a fair bit, as the span 2351 // calculation factors in touch radius. 2352 const float kOvershootMinPinchUpdateDistance = 3; 2353 event = ObtainMotionEvent(event_time, 2354 MotionEvent::ACTION_MOVE, 2355 kFakeCoordX, 2356 kFakeCoordY, 2357 secondary_coord_x + kMinPinchUpdateDistance + 2358 kOvershootMinPinchUpdateDistance, 2359 secondary_coord_y); 2360 2361 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2362 EXPECT_TRUE(HasReceivedGesture(ET_GESTURE_PINCH_UPDATE)); 2363 EXPECT_EQ(2, GetMostRecentGestureEvent().details.touch_points()); 2364} 2365 2366// Verify that the min gesture bound setting is honored. 2367TEST_F(GestureProviderTest, MinGestureBoundsLength) { 2368 const float kMinGestureBoundsLength = 10.f * kMockTouchRadius; 2369 SetMinGestureBoundsLength(kMinGestureBoundsLength); 2370 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 2371 2372 base::TimeTicks event_time = base::TimeTicks::Now(); 2373 MockMotionEvent event = 2374 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 2375 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2376 2377 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 2378 EXPECT_EQ(kMinGestureBoundsLength, 2379 GetMostRecentGestureEvent().details.bounding_box_f().width()); 2380 EXPECT_EQ(kMinGestureBoundsLength, 2381 GetMostRecentGestureEvent().details.bounding_box_f().height()); 2382 2383 event = 2384 ObtainMotionEvent(event_time + kOneMicrosecond, MotionEvent::ACTION_UP); 2385 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2386 EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType()); 2387 EXPECT_EQ(kMinGestureBoundsLength, 2388 GetMostRecentGestureEvent().details.bounding_box_f().width()); 2389 EXPECT_EQ(kMinGestureBoundsLength, 2390 GetMostRecentGestureEvent().details.bounding_box_f().height()); 2391} 2392 2393TEST_F(GestureProviderTest, MaxGestureBoundsLength) { 2394 const float kMaxGestureBoundsLength = kMockTouchRadius / 10.f; 2395 SetMaxGestureBoundsLength(kMaxGestureBoundsLength); 2396 gesture_provider_->SetDoubleTapSupportForPlatformEnabled(false); 2397 2398 base::TimeTicks event_time = base::TimeTicks::Now(); 2399 MockMotionEvent event = 2400 ObtainMotionEvent(event_time, MotionEvent::ACTION_DOWN); 2401 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2402 2403 EXPECT_EQ(ET_GESTURE_TAP_DOWN, GetMostRecentGestureEventType()); 2404 EXPECT_EQ(kMaxGestureBoundsLength, 2405 GetMostRecentGestureEvent().details.bounding_box_f().width()); 2406 EXPECT_EQ(kMaxGestureBoundsLength, 2407 GetMostRecentGestureEvent().details.bounding_box_f().height()); 2408 2409 event = 2410 ObtainMotionEvent(event_time + kOneMicrosecond, MotionEvent::ACTION_UP); 2411 EXPECT_TRUE(gesture_provider_->OnTouchEvent(event)); 2412 EXPECT_EQ(ET_GESTURE_TAP, GetMostRecentGestureEventType()); 2413 EXPECT_EQ(kMaxGestureBoundsLength, 2414 GetMostRecentGestureEvent().details.bounding_box_f().width()); 2415 EXPECT_EQ(kMaxGestureBoundsLength, 2416 GetMostRecentGestureEvent().details.bounding_box_f().height()); 2417} 2418 2419} // namespace ui 2420