gesture_recognizer_unittest.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1// Copyright (c) 2012 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/command_line.h" 6#include "base/memory/scoped_vector.h" 7#include "base/run_loop.h" 8#include "base/strings/string_number_conversions.h" 9#include "base/timer/timer.h" 10#include "testing/gtest/include/gtest/gtest.h" 11#include "ui/aura/env.h" 12#include "ui/aura/test/aura_test_base.h" 13#include "ui/aura/test/event_generator.h" 14#include "ui/aura/test/test_window_delegate.h" 15#include "ui/aura/test/test_windows.h" 16#include "ui/aura/window.h" 17#include "ui/aura/window_event_dispatcher.h" 18#include "ui/base/hit_test.h" 19#include "ui/base/ui_base_switches.h" 20#include "ui/events/event.h" 21#include "ui/events/event_switches.h" 22#include "ui/events/event_utils.h" 23#include "ui/events/gestures/gesture_configuration.h" 24#include "ui/events/gestures/gesture_recognizer_impl.h" 25#include "ui/events/gestures/gesture_sequence.h" 26#include "ui/events/gestures/gesture_types.h" 27#include "ui/gfx/point.h" 28#include "ui/gfx/rect.h" 29 30#include <queue> 31 32namespace aura { 33namespace test { 34 35namespace { 36 37std::string WindowIDAsString(ui::GestureConsumer* consumer) { 38 return consumer ? 39 base::IntToString(static_cast<Window*>(consumer)->id()) : "?"; 40} 41 42#define EXPECT_0_EVENTS(events) \ 43 EXPECT_EQ(0u, events.size()) 44 45#define EXPECT_1_EVENT(events, e0) \ 46 EXPECT_EQ(1u, events.size()); \ 47 EXPECT_EQ(e0, events[0]) 48 49#define EXPECT_2_EVENTS(events, e0, e1) \ 50 EXPECT_EQ(2u, events.size()); \ 51 EXPECT_EQ(e0, events[0]); \ 52 EXPECT_EQ(e1, events[1]) 53 54#define EXPECT_3_EVENTS(events, e0, e1, e2) \ 55 EXPECT_EQ(3u, events.size()); \ 56 EXPECT_EQ(e0, events[0]); \ 57 EXPECT_EQ(e1, events[1]); \ 58 EXPECT_EQ(e2, events[2]) 59 60#define EXPECT_4_EVENTS(events, e0, e1, e2, e3) \ 61 EXPECT_EQ(4u, events.size()); \ 62 EXPECT_EQ(e0, events[0]); \ 63 EXPECT_EQ(e1, events[1]); \ 64 EXPECT_EQ(e2, events[2]); \ 65 EXPECT_EQ(e3, events[3]) 66 67// A delegate that keeps track of gesture events. 68class GestureEventConsumeDelegate : public TestWindowDelegate { 69 public: 70 GestureEventConsumeDelegate() 71 : tap_(false), 72 tap_down_(false), 73 tap_cancel_(false), 74 begin_(false), 75 end_(false), 76 scroll_begin_(false), 77 scroll_update_(false), 78 scroll_end_(false), 79 pinch_begin_(false), 80 pinch_update_(false), 81 pinch_end_(false), 82 long_press_(false), 83 fling_(false), 84 two_finger_tap_(false), 85 show_press_(false), 86 swipe_left_(false), 87 swipe_right_(false), 88 swipe_up_(false), 89 swipe_down_(false), 90 scroll_x_(0), 91 scroll_y_(0), 92 scroll_velocity_x_(0), 93 scroll_velocity_y_(0), 94 velocity_x_(0), 95 velocity_y_(0), 96 scroll_x_ordinal_(0), 97 scroll_y_ordinal_(0), 98 scroll_velocity_x_ordinal_(0), 99 scroll_velocity_y_ordinal_(0), 100 velocity_x_ordinal_(0), 101 velocity_y_ordinal_(0), 102 scroll_x_hint_(0), 103 scroll_y_hint_(0), 104 tap_count_(0), 105 wait_until_event_(ui::ET_UNKNOWN) { 106 } 107 108 virtual ~GestureEventConsumeDelegate() {} 109 110 void Reset() { 111 events_.clear(); 112 tap_ = false; 113 tap_down_ = false; 114 tap_cancel_ = false; 115 begin_ = false; 116 end_ = false; 117 scroll_begin_ = false; 118 scroll_update_ = false; 119 scroll_end_ = false; 120 pinch_begin_ = false; 121 pinch_update_ = false; 122 pinch_end_ = false; 123 long_press_ = false; 124 fling_ = false; 125 two_finger_tap_ = false; 126 show_press_ = false; 127 swipe_left_ = false; 128 swipe_right_ = false; 129 swipe_up_ = false; 130 swipe_down_ = false; 131 132 scroll_begin_position_.SetPoint(0, 0); 133 tap_location_.SetPoint(0, 0); 134 gesture_end_location_.SetPoint(0, 0); 135 136 scroll_x_ = 0; 137 scroll_y_ = 0; 138 scroll_velocity_x_ = 0; 139 scroll_velocity_y_ = 0; 140 velocity_x_ = 0; 141 velocity_y_ = 0; 142 scroll_x_ordinal_ = 0; 143 scroll_y_ordinal_ = 0; 144 scroll_velocity_x_ordinal_ = 0; 145 scroll_velocity_y_ordinal_ = 0; 146 velocity_x_ordinal_ = 0; 147 velocity_y_ordinal_ = 0; 148 scroll_x_hint_ = 0; 149 scroll_y_hint_ = 0; 150 tap_count_ = 0; 151 scale_ = 0; 152 } 153 154 const std::vector<ui::EventType>& events() const { return events_; }; 155 156 bool tap() const { return tap_; } 157 bool tap_down() const { return tap_down_; } 158 bool tap_cancel() const { return tap_cancel_; } 159 bool begin() const { return begin_; } 160 bool end() const { return end_; } 161 bool scroll_begin() const { return scroll_begin_; } 162 bool scroll_update() const { return scroll_update_; } 163 bool scroll_end() const { return scroll_end_; } 164 bool pinch_begin() const { return pinch_begin_; } 165 bool pinch_update() const { return pinch_update_; } 166 bool pinch_end() const { return pinch_end_; } 167 bool long_press() const { return long_press_; } 168 bool long_tap() const { return long_tap_; } 169 bool fling() const { return fling_; } 170 bool two_finger_tap() const { return two_finger_tap_; } 171 bool show_press() const { return show_press_; } 172 bool swipe_left() const { return swipe_left_; } 173 bool swipe_right() const { return swipe_right_; } 174 bool swipe_up() const { return swipe_up_; } 175 bool swipe_down() const { return swipe_down_; } 176 177 const gfx::Point& scroll_begin_position() const { 178 return scroll_begin_position_; 179 } 180 181 const gfx::Point& tap_location() const { 182 return tap_location_; 183 } 184 185 const gfx::Point& gesture_end_location() const { 186 return gesture_end_location_; 187 } 188 189 float scroll_x() const { return scroll_x_; } 190 float scroll_y() const { return scroll_y_; } 191 float scroll_velocity_x() const { return scroll_velocity_x_; } 192 float scroll_velocity_y() const { return scroll_velocity_y_; } 193 float velocity_x() const { return velocity_x_; } 194 float velocity_y() const { return velocity_y_; } 195 float scroll_x_ordinal() const { return scroll_x_ordinal_; } 196 float scroll_y_ordinal() const { return scroll_y_ordinal_; } 197 float scroll_velocity_x_ordinal() const { return scroll_velocity_x_ordinal_; } 198 float scroll_velocity_y_ordinal() const { return scroll_velocity_y_ordinal_; } 199 float velocity_x_ordinal() const { return velocity_x_ordinal_; } 200 float velocity_y_ordinal() const { return velocity_y_ordinal_; } 201 float scroll_x_hint() const { return scroll_x_hint_; } 202 float scroll_y_hint() const { return scroll_y_hint_; } 203 float scale() const { return scale_; } 204 const gfx::Rect& bounding_box() const { return bounding_box_; } 205 int tap_count() const { return tap_count_; } 206 207 void WaitUntilReceivedGesture(ui::EventType type) { 208 wait_until_event_ = type; 209 run_loop_.reset(new base::RunLoop()); 210 run_loop_->Run(); 211 } 212 213 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE { 214 events_.push_back(gesture->type()); 215 bounding_box_ = gesture->details().bounding_box(); 216 switch (gesture->type()) { 217 case ui::ET_GESTURE_TAP: 218 tap_location_ = gesture->location(); 219 tap_count_ = gesture->details().tap_count(); 220 tap_ = true; 221 break; 222 case ui::ET_GESTURE_TAP_DOWN: 223 tap_down_ = true; 224 break; 225 case ui::ET_GESTURE_TAP_CANCEL: 226 tap_cancel_ = true; 227 break; 228 case ui::ET_GESTURE_BEGIN: 229 begin_ = true; 230 break; 231 case ui::ET_GESTURE_END: 232 end_ = true; 233 gesture_end_location_ = gesture->location(); 234 break; 235 case ui::ET_GESTURE_SCROLL_BEGIN: 236 scroll_begin_ = true; 237 scroll_begin_position_ = gesture->location(); 238 scroll_x_hint_ = gesture->details().scroll_x_hint(); 239 scroll_y_hint_ = gesture->details().scroll_y_hint(); 240 break; 241 case ui::ET_GESTURE_SCROLL_UPDATE: 242 scroll_update_ = true; 243 scroll_x_ += gesture->details().scroll_x(); 244 scroll_y_ += gesture->details().scroll_y(); 245 scroll_velocity_x_ = gesture->details().velocity_x(); 246 scroll_velocity_y_ = gesture->details().velocity_y(); 247 scroll_x_ordinal_ += gesture->details().scroll_x_ordinal(); 248 scroll_y_ordinal_ += gesture->details().scroll_y_ordinal(); 249 scroll_velocity_x_ordinal_ = gesture->details().velocity_x_ordinal(); 250 scroll_velocity_y_ordinal_ = gesture->details().velocity_y_ordinal(); 251 break; 252 case ui::ET_GESTURE_SCROLL_END: 253 EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0); 254 scroll_end_ = true; 255 break; 256 case ui::ET_GESTURE_PINCH_BEGIN: 257 pinch_begin_ = true; 258 break; 259 case ui::ET_GESTURE_PINCH_UPDATE: 260 pinch_update_ = true; 261 scale_ = gesture->details().scale(); 262 break; 263 case ui::ET_GESTURE_PINCH_END: 264 pinch_end_ = true; 265 break; 266 case ui::ET_GESTURE_LONG_PRESS: 267 long_press_ = true; 268 break; 269 case ui::ET_GESTURE_LONG_TAP: 270 long_tap_ = true; 271 break; 272 case ui::ET_SCROLL_FLING_START: 273 EXPECT_TRUE(gesture->details().velocity_x() != 0 || 274 gesture->details().velocity_y() != 0); 275 EXPECT_FALSE(scroll_end_); 276 fling_ = true; 277 velocity_x_ = gesture->details().velocity_x(); 278 velocity_y_ = gesture->details().velocity_y(); 279 velocity_x_ordinal_ = gesture->details().velocity_x_ordinal(); 280 velocity_y_ordinal_ = gesture->details().velocity_y_ordinal(); 281 break; 282 case ui::ET_GESTURE_TWO_FINGER_TAP: 283 two_finger_tap_ = true; 284 break; 285 case ui::ET_GESTURE_SHOW_PRESS: 286 show_press_ = true; 287 break; 288 case ui::ET_GESTURE_MULTIFINGER_SWIPE: 289 swipe_left_ = gesture->details().swipe_left(); 290 swipe_right_ = gesture->details().swipe_right(); 291 swipe_up_ = gesture->details().swipe_up(); 292 swipe_down_ = gesture->details().swipe_down(); 293 break; 294 default: 295 NOTREACHED(); 296 } 297 if (wait_until_event_ == gesture->type() && run_loop_) { 298 run_loop_->Quit(); 299 wait_until_event_ = ui::ET_UNKNOWN; 300 } 301 gesture->StopPropagation(); 302 } 303 304 private: 305 scoped_ptr<base::RunLoop> run_loop_; 306 std::vector<ui::EventType> events_; 307 308 bool tap_; 309 bool tap_down_; 310 bool tap_cancel_; 311 bool begin_; 312 bool end_; 313 bool scroll_begin_; 314 bool scroll_update_; 315 bool scroll_end_; 316 bool pinch_begin_; 317 bool pinch_update_; 318 bool pinch_end_; 319 bool long_press_; 320 bool long_tap_; 321 bool fling_; 322 bool two_finger_tap_; 323 bool show_press_; 324 bool swipe_left_; 325 bool swipe_right_; 326 bool swipe_up_; 327 bool swipe_down_; 328 329 gfx::Point scroll_begin_position_; 330 gfx::Point tap_location_; 331 gfx::Point gesture_end_location_; 332 333 float scroll_x_; 334 float scroll_y_; 335 float scroll_velocity_x_; 336 float scroll_velocity_y_; 337 float velocity_x_; 338 float velocity_y_; 339 float scroll_x_ordinal_; 340 float scroll_y_ordinal_; 341 float scroll_velocity_x_ordinal_; 342 float scroll_velocity_y_ordinal_; 343 float velocity_x_ordinal_; 344 float velocity_y_ordinal_; 345 float scroll_x_hint_; 346 float scroll_y_hint_; 347 float scale_; 348 gfx::Rect bounding_box_; 349 int tap_count_; 350 351 ui::EventType wait_until_event_; 352 353 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate); 354}; 355 356class QueueTouchEventDelegate : public GestureEventConsumeDelegate { 357 public: 358 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher) 359 : window_(NULL), 360 dispatcher_(dispatcher), 361 queue_events_(true) { 362 } 363 virtual ~QueueTouchEventDelegate() { 364 while(!queue_.empty()) { 365 delete queue_.front(); 366 queue_.pop(); 367 } 368 } 369 370 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE { 371 if (queue_events_) { 372 queue_.push(new ui::TouchEvent(*event, window_, window_)); 373 event->StopPropagation(); 374 } 375 } 376 377 void ReceivedAck() { 378 ReceivedAckImpl(false); 379 } 380 381 void ReceivedAckPreventDefaulted() { 382 ReceivedAckImpl(true); 383 } 384 385 void set_window(Window* w) { window_ = w; } 386 void set_queue_events(bool queue) { queue_events_ = queue; } 387 388 private: 389 void ReceivedAckImpl(bool prevent_defaulted) { 390 scoped_ptr<ui::TouchEvent> event(queue_.front()); 391 dispatcher_->ProcessedTouchEvent(event.get(), window_, 392 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED); 393 queue_.pop(); 394 } 395 396 std::queue<ui::TouchEvent*> queue_; 397 Window* window_; 398 WindowEventDispatcher* dispatcher_; 399 bool queue_events_; 400 401 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate); 402}; 403 404// A delegate that ignores gesture events but keeps track of [synthetic] mouse 405// events. 406class GestureEventSynthDelegate : public TestWindowDelegate { 407 public: 408 GestureEventSynthDelegate() 409 : mouse_enter_(false), 410 mouse_exit_(false), 411 mouse_press_(false), 412 mouse_release_(false), 413 mouse_move_(false), 414 double_click_(false) { 415 } 416 417 void Reset() { 418 mouse_enter_ = false; 419 mouse_exit_ = false; 420 mouse_press_ = false; 421 mouse_release_ = false; 422 mouse_move_ = false; 423 double_click_ = false; 424 } 425 426 bool mouse_enter() const { return mouse_enter_; } 427 bool mouse_exit() const { return mouse_exit_; } 428 bool mouse_press() const { return mouse_press_; } 429 bool mouse_move() const { return mouse_move_; } 430 bool mouse_release() const { return mouse_release_; } 431 bool double_click() const { return double_click_; } 432 433 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE { 434 switch (event->type()) { 435 case ui::ET_MOUSE_PRESSED: 436 double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK; 437 mouse_press_ = true; 438 break; 439 case ui::ET_MOUSE_RELEASED: 440 mouse_release_ = true; 441 break; 442 case ui::ET_MOUSE_MOVED: 443 mouse_move_ = true; 444 break; 445 case ui::ET_MOUSE_ENTERED: 446 mouse_enter_ = true; 447 break; 448 case ui::ET_MOUSE_EXITED: 449 mouse_exit_ = true; 450 break; 451 default: 452 NOTREACHED(); 453 } 454 event->SetHandled(); 455 } 456 457 private: 458 bool mouse_enter_; 459 bool mouse_exit_; 460 bool mouse_press_; 461 bool mouse_release_; 462 bool mouse_move_; 463 bool double_click_; 464 465 DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate); 466}; 467 468class TestOneShotGestureSequenceTimer 469 : public base::OneShotTimer<ui::GestureSequence> { 470 public: 471 TestOneShotGestureSequenceTimer() {} 472 473 void ForceTimeout() { 474 if (IsRunning()) { 475 user_task().Run(); 476 Stop(); 477 } 478 } 479 480 private: 481 DISALLOW_COPY_AND_ASSIGN(TestOneShotGestureSequenceTimer); 482}; 483 484class TimerTestGestureSequence : public ui::GestureSequence { 485 public: 486 explicit TimerTestGestureSequence(ui::GestureSequenceDelegate* delegate) 487 : ui::GestureSequence(delegate) { 488 } 489 490 void ForceTimeout() { 491 static_cast<TestOneShotGestureSequenceTimer*>( 492 GetLongPressTimer())->ForceTimeout(); 493 } 494 495 bool IsTimerRunning() { 496 return GetLongPressTimer()->IsRunning(); 497 } 498 499 virtual base::OneShotTimer<ui::GestureSequence>* CreateTimer() OVERRIDE { 500 return new TestOneShotGestureSequenceTimer(); 501 } 502 503 private: 504 DISALLOW_COPY_AND_ASSIGN(TimerTestGestureSequence); 505}; 506 507class TestGestureRecognizer : public ui::GestureRecognizerImpl { 508 public: 509 TestGestureRecognizer() : GestureRecognizerImpl() { 510 } 511 512 ui::GestureSequence* GetGestureSequenceForTesting(Window* window) { 513 return GetGestureSequenceForConsumer(window); 514 } 515 516 private: 517 DISALLOW_COPY_AND_ASSIGN(TestGestureRecognizer); 518}; 519 520class TimerTestGestureRecognizer : public TestGestureRecognizer { 521 public: 522 TimerTestGestureRecognizer() : TestGestureRecognizer() { 523 } 524 525 virtual ui::GestureSequence* CreateSequence( 526 ui::GestureSequenceDelegate* delegate) OVERRIDE { 527 return new TimerTestGestureSequence(delegate); 528 } 529 530 private: 531 DISALLOW_COPY_AND_ASSIGN(TimerTestGestureRecognizer); 532}; 533 534base::TimeDelta GetTime() { 535 return ui::EventTimeForNow(); 536} 537 538class ScopedGestureRecognizerSetter { 539 public: 540 // Takes ownership of |new_gr|. 541 explicit ScopedGestureRecognizerSetter(ui::GestureRecognizer* new_gr) 542 : new_gr_(new_gr) { 543 original_gr_ = ui::GestureRecognizer::Get(); 544 ui::SetGestureRecognizerForTesting(new_gr_.get()); 545 } 546 547 virtual ~ScopedGestureRecognizerSetter() { 548 ui::SetGestureRecognizerForTesting(original_gr_); 549 } 550 551 private: 552 ui::GestureRecognizer* original_gr_; 553 scoped_ptr<ui::GestureRecognizer> new_gr_; 554 555 DISALLOW_COPY_AND_ASSIGN(ScopedGestureRecognizerSetter); 556}; 557 558class TimedEvents { 559 private: 560 int simulated_now_; 561 562 public: 563 TimedEvents() : simulated_now_(0) { 564 } 565 566 base::TimeDelta Now() { 567 base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_); 568 simulated_now_++; 569 return t; 570 } 571 572 base::TimeDelta LeapForward(int time_in_millis) { 573 simulated_now_ += time_in_millis; 574 return base::TimeDelta::FromMilliseconds(simulated_now_); 575 } 576 577 base::TimeDelta InFuture(int time_in_millis) { 578 return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis); 579 } 580 581 void SendScrollEvents(ui::EventProcessor* dispatcher, 582 float x_start, 583 float y_start, 584 int dx, 585 int dy, 586 int touch_id, 587 int time_step, 588 int num_steps, 589 GestureEventConsumeDelegate* delegate) { 590 int x = x_start; 591 int y = y_start; 592 593 for (int i = 0; i < num_steps; i++) { 594 x += dx; 595 y += dy; 596 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y), 597 touch_id, 598 base::TimeDelta::FromMilliseconds(simulated_now_)); 599 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move); 600 ASSERT_FALSE(details.dispatcher_destroyed); 601 simulated_now_ += time_step; 602 } 603 } 604 605 void SendScrollEvent(ui::EventProcessor* dispatcher, 606 float x, 607 float y, 608 int touch_id, 609 GestureEventConsumeDelegate* delegate) { 610 delegate->Reset(); 611 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y), 612 touch_id, 613 base::TimeDelta::FromMilliseconds(simulated_now_)); 614 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move); 615 ASSERT_FALSE(details.dispatcher_destroyed); 616 simulated_now_++; 617 } 618}; 619 620// An event handler to keep track of events. 621class TestEventHandler : public ui::EventHandler { 622 public: 623 TestEventHandler() : touch_released_count_(0), touch_pressed_count_(0), 624 touch_moved_count_(0), touch_stationary_count_(0), 625 touch_cancelled_count_(0) { 626 } 627 628 virtual ~TestEventHandler() {} 629 630 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE { 631 switch (event->type()) { 632 case ui::ET_TOUCH_RELEASED: 633 touch_released_count_++; 634 break; 635 case ui::ET_TOUCH_PRESSED: 636 touch_pressed_count_++; 637 break; 638 case ui::ET_TOUCH_MOVED: 639 touch_moved_count_++; 640 break; 641 case ui::ET_TOUCH_STATIONARY: 642 touch_stationary_count_++; 643 break; 644 case ui::ET_TOUCH_CANCELLED: 645 touch_cancelled_count_++; 646 break; 647 default: 648 break; 649 } 650 } 651 652 void Reset() { 653 touch_released_count_ = 0; 654 touch_pressed_count_ = 0; 655 touch_moved_count_ = 0; 656 touch_stationary_count_ = 0; 657 touch_cancelled_count_ = 0; 658 } 659 660 int touch_released_count() const { return touch_released_count_; } 661 int touch_pressed_count() const { return touch_pressed_count_; } 662 int touch_moved_count() const { return touch_moved_count_; } 663 int touch_stationary_count() const { return touch_stationary_count_; } 664 int touch_cancelled_count() const { return touch_cancelled_count_; } 665 666 private: 667 int touch_released_count_; 668 int touch_pressed_count_; 669 int touch_moved_count_; 670 int touch_stationary_count_; 671 int touch_cancelled_count_; 672 673 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); 674}; 675 676// Removes the target window from its parent when it receives a touch-cancel 677// event. 678class RemoveOnTouchCancelHandler : public TestEventHandler { 679 public: 680 RemoveOnTouchCancelHandler() {} 681 virtual ~RemoveOnTouchCancelHandler() {} 682 683 private: 684 // ui::EventHandler: 685 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE { 686 TestEventHandler::OnTouchEvent(event); 687 if (event->type() == ui::ET_TOUCH_CANCELLED) { 688 Window* target = static_cast<Window*>(event->target()); 689 // This is tiptoeing around crbug.com/310172. If this event handler isn't 690 // removed, we enter an infinite loop. 691 target->RemovePreTargetHandler(this); 692 target->parent()->RemoveChild(target); 693 } 694 } 695 696 DISALLOW_COPY_AND_ASSIGN(RemoveOnTouchCancelHandler); 697}; 698 699} // namespace 700 701class GestureRecognizerTest : public AuraTestBase { 702 public: 703 GestureRecognizerTest() {} 704 705 virtual void SetUp() OVERRIDE { 706 CommandLine::ForCurrentProcess()->AppendSwitch( 707 switches::kEnableScrollPrediction); 708 AuraTestBase::SetUp(); 709 } 710 711 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerTest); 712}; 713 714// Check that appropriate touch events generate tap gesture events. 715TEST_F(GestureRecognizerTest, GestureEventTap) { 716 scoped_ptr<GestureEventConsumeDelegate> delegate( 717 new GestureEventConsumeDelegate()); 718 TimedEvents tes; 719 const int kWindowWidth = 123; 720 const int kWindowHeight = 45; 721 const int kTouchId = 2; 722 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 723 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 724 delegate.get(), -1234, bounds, root_window())); 725 726 delegate->Reset(); 727 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 728 kTouchId, tes.Now()); 729 DispatchEventUsingWindowDispatcher(&press); 730 EXPECT_FALSE(delegate->tap()); 731 EXPECT_FALSE(delegate->show_press()); 732 EXPECT_TRUE(delegate->tap_down()); 733 EXPECT_FALSE(delegate->tap_cancel()); 734 EXPECT_TRUE(delegate->begin()); 735 EXPECT_FALSE(delegate->scroll_begin()); 736 EXPECT_FALSE(delegate->scroll_update()); 737 EXPECT_FALSE(delegate->scroll_end()); 738 EXPECT_FALSE(delegate->long_press()); 739 740 delegate->Reset(); 741 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS); 742 EXPECT_TRUE(delegate->show_press()); 743 EXPECT_FALSE(delegate->tap_down()); 744 745 // Make sure there is enough delay before the touch is released so that it is 746 // recognized as a tap. 747 delegate->Reset(); 748 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 749 kTouchId, tes.LeapForward(50)); 750 751 DispatchEventUsingWindowDispatcher(&release); 752 EXPECT_TRUE(delegate->tap()); 753 EXPECT_FALSE(delegate->tap_down()); 754 EXPECT_FALSE(delegate->tap_cancel()); 755 EXPECT_FALSE(delegate->begin()); 756 EXPECT_TRUE(delegate->end()); 757 EXPECT_FALSE(delegate->scroll_begin()); 758 EXPECT_FALSE(delegate->scroll_update()); 759 EXPECT_FALSE(delegate->scroll_end()); 760 761 EXPECT_EQ(1, delegate->tap_count()); 762} 763 764// Check that appropriate touch events generate tap gesture events 765// when information about the touch radii are provided. 766TEST_F(GestureRecognizerTest, GestureEventTapRegion) { 767 scoped_ptr<GestureEventConsumeDelegate> delegate( 768 new GestureEventConsumeDelegate()); 769 TimedEvents tes; 770 const int kWindowWidth = 800; 771 const int kWindowHeight = 600; 772 const int kTouchId = 2; 773 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); 774 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 775 delegate.get(), -1234, bounds, root_window())); 776 777 // Test with no ET_TOUCH_MOVED events. 778 { 779 delegate->Reset(); 780 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 781 kTouchId, tes.Now()); 782 press.set_radius_x(5); 783 press.set_radius_y(12); 784 DispatchEventUsingWindowDispatcher(&press); 785 EXPECT_FALSE(delegate->tap()); 786 EXPECT_TRUE(delegate->tap_down()); 787 EXPECT_FALSE(delegate->tap_cancel()); 788 EXPECT_TRUE(delegate->begin()); 789 EXPECT_FALSE(delegate->scroll_begin()); 790 EXPECT_FALSE(delegate->scroll_update()); 791 EXPECT_FALSE(delegate->scroll_end()); 792 EXPECT_FALSE(delegate->long_press()); 793 794 // Make sure there is enough delay before the touch is released so that it 795 // is recognized as a tap. 796 delegate->Reset(); 797 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 798 kTouchId, tes.LeapForward(50)); 799 release.set_radius_x(5); 800 release.set_radius_y(12); 801 802 DispatchEventUsingWindowDispatcher(&release); 803 EXPECT_TRUE(delegate->tap()); 804 EXPECT_FALSE(delegate->tap_down()); 805 EXPECT_FALSE(delegate->tap_cancel()); 806 EXPECT_FALSE(delegate->begin()); 807 EXPECT_TRUE(delegate->end()); 808 EXPECT_FALSE(delegate->scroll_begin()); 809 EXPECT_FALSE(delegate->scroll_update()); 810 EXPECT_FALSE(delegate->scroll_end()); 811 812 EXPECT_EQ(1, delegate->tap_count()); 813 gfx::Point actual_point(delegate->tap_location()); 814 EXPECT_EQ(24, delegate->bounding_box().width()); 815 EXPECT_EQ(24, delegate->bounding_box().height()); 816 EXPECT_EQ(101, actual_point.x()); 817 EXPECT_EQ(201, actual_point.y()); 818 } 819 820 // Test with no ET_TOUCH_MOVED events but different touch points and radii. 821 { 822 delegate->Reset(); 823 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290), 824 kTouchId, tes.Now()); 825 press.set_radius_x(8); 826 press.set_radius_y(14); 827 DispatchEventUsingWindowDispatcher(&press); 828 EXPECT_FALSE(delegate->tap()); 829 EXPECT_TRUE(delegate->tap_down()); 830 EXPECT_FALSE(delegate->tap_cancel()); 831 EXPECT_TRUE(delegate->begin()); 832 EXPECT_FALSE(delegate->scroll_begin()); 833 EXPECT_FALSE(delegate->scroll_update()); 834 EXPECT_FALSE(delegate->scroll_end()); 835 EXPECT_FALSE(delegate->long_press()); 836 837 delegate->Reset(); 838 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291), 839 kTouchId, tes.LeapForward(50)); 840 release.set_radius_x(20); 841 release.set_radius_y(13); 842 843 DispatchEventUsingWindowDispatcher(&release); 844 EXPECT_TRUE(delegate->tap()); 845 EXPECT_FALSE(delegate->tap_down()); 846 EXPECT_FALSE(delegate->tap_cancel()); 847 EXPECT_FALSE(delegate->begin()); 848 EXPECT_TRUE(delegate->end()); 849 EXPECT_FALSE(delegate->scroll_begin()); 850 EXPECT_FALSE(delegate->scroll_update()); 851 EXPECT_FALSE(delegate->scroll_end()); 852 853 EXPECT_EQ(1, delegate->tap_count()); 854 gfx::Point actual_point(delegate->tap_location()); 855 EXPECT_EQ(40, delegate->bounding_box().width()); 856 EXPECT_EQ(40, delegate->bounding_box().height()); 857 EXPECT_EQ(367, actual_point.x()); 858 EXPECT_EQ(291, actual_point.y()); 859 } 860 861 // Test with a single ET_TOUCH_MOVED event. 862 { 863 delegate->Reset(); 864 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205), 865 kTouchId, tes.Now()); 866 press.set_radius_x(6); 867 press.set_radius_y(10); 868 DispatchEventUsingWindowDispatcher(&press); 869 EXPECT_FALSE(delegate->tap()); 870 EXPECT_TRUE(delegate->tap_down()); 871 EXPECT_FALSE(delegate->tap_cancel()); 872 EXPECT_TRUE(delegate->begin()); 873 EXPECT_FALSE(delegate->tap_cancel()); 874 EXPECT_FALSE(delegate->scroll_begin()); 875 EXPECT_FALSE(delegate->scroll_update()); 876 EXPECT_FALSE(delegate->scroll_end()); 877 EXPECT_FALSE(delegate->long_press()); 878 879 delegate->Reset(); 880 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204), 881 kTouchId, tes.LeapForward(50)); 882 move.set_radius_x(8); 883 move.set_radius_y(12); 884 DispatchEventUsingWindowDispatcher(&move); 885 EXPECT_FALSE(delegate->tap()); 886 EXPECT_FALSE(delegate->tap_down()); 887 EXPECT_FALSE(delegate->tap_cancel()); 888 EXPECT_FALSE(delegate->begin()); 889 EXPECT_FALSE(delegate->scroll_begin()); 890 EXPECT_FALSE(delegate->scroll_update()); 891 EXPECT_FALSE(delegate->scroll_end()); 892 EXPECT_FALSE(delegate->long_press()); 893 894 delegate->Reset(); 895 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204), 896 kTouchId, tes.LeapForward(50)); 897 release.set_radius_x(4); 898 release.set_radius_y(8); 899 900 DispatchEventUsingWindowDispatcher(&release); 901 EXPECT_TRUE(delegate->tap()); 902 EXPECT_FALSE(delegate->tap_down()); 903 EXPECT_FALSE(delegate->tap_cancel()); 904 EXPECT_FALSE(delegate->begin()); 905 EXPECT_TRUE(delegate->end()); 906 EXPECT_FALSE(delegate->scroll_begin()); 907 EXPECT_FALSE(delegate->scroll_update()); 908 EXPECT_FALSE(delegate->scroll_end()); 909 910 EXPECT_EQ(1, delegate->tap_count()); 911 gfx::Point actual_point(delegate->tap_location()); 912 EXPECT_EQ(25, delegate->bounding_box().width()); 913 EXPECT_EQ(24, delegate->bounding_box().height()); 914 EXPECT_EQ(48, actual_point.x()); 915 EXPECT_EQ(204, actual_point.y()); 916 } 917 918 // Test with a few ET_TOUCH_MOVED events. 919 { 920 delegate->Reset(); 921 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150), 922 kTouchId, tes.Now()); 923 press.set_radius_x(7); 924 press.set_radius_y(10); 925 DispatchEventUsingWindowDispatcher(&press); 926 EXPECT_FALSE(delegate->tap()); 927 EXPECT_TRUE(delegate->tap_down()); 928 EXPECT_FALSE(delegate->tap_cancel()); 929 EXPECT_TRUE(delegate->begin()); 930 EXPECT_FALSE(delegate->scroll_begin()); 931 EXPECT_FALSE(delegate->scroll_update()); 932 EXPECT_FALSE(delegate->scroll_end()); 933 EXPECT_FALSE(delegate->long_press()); 934 935 delegate->Reset(); 936 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151), 937 kTouchId, tes.LeapForward(50)); 938 move.set_radius_x(13); 939 move.set_radius_y(12); 940 DispatchEventUsingWindowDispatcher(&move); 941 EXPECT_FALSE(delegate->tap()); 942 EXPECT_FALSE(delegate->tap_down()); 943 EXPECT_FALSE(delegate->tap_cancel()); 944 EXPECT_FALSE(delegate->begin()); 945 EXPECT_FALSE(delegate->scroll_begin()); 946 EXPECT_FALSE(delegate->scroll_update()); 947 EXPECT_FALSE(delegate->scroll_end()); 948 EXPECT_FALSE(delegate->long_press()); 949 950 delegate->Reset(); 951 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149), 952 kTouchId, tes.LeapForward(50)); 953 move1.set_radius_x(16); 954 move1.set_radius_y(16); 955 DispatchEventUsingWindowDispatcher(&move1); 956 EXPECT_FALSE(delegate->tap()); 957 EXPECT_FALSE(delegate->tap_down()); 958 EXPECT_FALSE(delegate->tap_cancel()); 959 EXPECT_FALSE(delegate->begin()); 960 EXPECT_FALSE(delegate->scroll_begin()); 961 EXPECT_FALSE(delegate->scroll_update()); 962 EXPECT_FALSE(delegate->scroll_end()); 963 EXPECT_FALSE(delegate->long_press()); 964 965 delegate->Reset(); 966 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150), 967 kTouchId, tes.LeapForward(50)); 968 move2.set_radius_x(14); 969 move2.set_radius_y(10); 970 DispatchEventUsingWindowDispatcher(&move2); 971 EXPECT_FALSE(delegate->tap()); 972 EXPECT_FALSE(delegate->tap_down()); 973 EXPECT_FALSE(delegate->tap_cancel()); 974 EXPECT_FALSE(delegate->begin()); 975 EXPECT_FALSE(delegate->scroll_begin()); 976 EXPECT_FALSE(delegate->scroll_update()); 977 EXPECT_FALSE(delegate->scroll_end()); 978 EXPECT_FALSE(delegate->long_press()); 979 980 delegate->Reset(); 981 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149), 982 kTouchId, tes.LeapForward(50)); 983 release.set_radius_x(8); 984 release.set_radius_y(9); 985 986 DispatchEventUsingWindowDispatcher(&release); 987 EXPECT_TRUE(delegate->tap()); 988 EXPECT_FALSE(delegate->tap_down()); 989 EXPECT_FALSE(delegate->tap_cancel()); 990 EXPECT_FALSE(delegate->begin()); 991 EXPECT_TRUE(delegate->end()); 992 EXPECT_FALSE(delegate->scroll_begin()); 993 EXPECT_FALSE(delegate->scroll_update()); 994 EXPECT_FALSE(delegate->scroll_end()); 995 996 EXPECT_EQ(1, delegate->tap_count()); 997 gfx::Point actual_point(delegate->tap_location()); 998 EXPECT_EQ(33, delegate->bounding_box().width()); 999 EXPECT_EQ(32, delegate->bounding_box().height()); 1000 EXPECT_EQ(397, actual_point.x()); 1001 EXPECT_EQ(149, actual_point.y()); 1002 } 1003} 1004 1005// Check that appropriate touch events generate scroll gesture events. 1006TEST_F(GestureRecognizerTest, GestureEventScroll) { 1007 // We'll start by moving the touch point by (10.5, 10.5). We want 5 dips of 1008 // that distance to be consumed by the slop, so we set the slop radius to 1009 // sqrt(5 * 5 + 5 * 5). 1010 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click( 1011 sqrt(static_cast<double>(5 * 5 + 5 * 5))); 1012 scoped_ptr<GestureEventConsumeDelegate> delegate( 1013 new GestureEventConsumeDelegate()); 1014 TimedEvents tes; 1015 const int kWindowWidth = 123; 1016 const int kWindowHeight = 45; 1017 const int kTouchId = 5; 1018 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 1019 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1020 delegate.get(), -1234, bounds, root_window())); 1021 1022 delegate->Reset(); 1023 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1024 kTouchId, tes.Now()); 1025 DispatchEventUsingWindowDispatcher(&press); 1026 EXPECT_2_EVENTS(delegate->events(), 1027 ui::ET_GESTURE_BEGIN, 1028 ui::ET_GESTURE_TAP_DOWN); 1029 1030 // Move the touch-point enough so that it is considered as a scroll. This 1031 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures. 1032 // The first movement is diagonal, to ensure that we have a free scroll, 1033 // and not a rail scroll. 1034 tes.SendScrollEvent(event_processor(), 111.5, 211.5, kTouchId, 1035 delegate.get()); 1036 EXPECT_3_EVENTS(delegate->events(), 1037 ui::ET_GESTURE_TAP_CANCEL, 1038 ui::ET_GESTURE_SCROLL_BEGIN, 1039 ui::ET_GESTURE_SCROLL_UPDATE); 1040 // The slop consumed 5 dips 1041 EXPECT_FLOAT_EQ(5.5, delegate->scroll_x()); 1042 EXPECT_FLOAT_EQ(5.5, delegate->scroll_y()); 1043 EXPECT_EQ(gfx::Point(1, 1).ToString(), 1044 delegate->scroll_begin_position().ToString()); 1045 1046 // When scrolling with a single finger, the bounding box of the gesture should 1047 // be empty, since it's a single point and the radius for testing is zero. 1048 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 1049 1050 // Move some more to generate a few more scroll updates. 1051 tes.SendScrollEvent(event_processor(), 91, 192, kTouchId, delegate.get()); 1052 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); 1053 EXPECT_FLOAT_EQ(-20.5, delegate->scroll_x()); 1054 EXPECT_FLOAT_EQ(-19.5, delegate->scroll_y()); 1055 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 1056 1057 tes.SendScrollEvent(event_processor(), 121, 196, kTouchId, delegate.get()); 1058 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); 1059 EXPECT_EQ(30, delegate->scroll_x()); 1060 EXPECT_EQ(4, delegate->scroll_y()); 1061 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 1062 1063 // Release the touch. This should end the scroll. 1064 delegate->Reset(); 1065 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1066 kTouchId, 1067 tes.LeapForward(50)); 1068 DispatchEventUsingWindowDispatcher(&release); 1069 EXPECT_2_EVENTS(delegate->events(), 1070 ui::ET_SCROLL_FLING_START, 1071 ui::ET_GESTURE_END); 1072 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 1073} 1074 1075// Check that predicted scroll update positions are correct. 1076TEST_F(GestureRecognizerTest, GestureEventScrollPrediction) { 1077 const double prediction_interval = 0.03; 1078 ui::GestureConfiguration::set_scroll_prediction_seconds(prediction_interval); 1079 // We'll start by moving the touch point by (5, 5). We want all of that 1080 // distance to be consumed by the slop, so we set the slop radius to 1081 // sqrt(5 * 5 + 5 * 5). 1082 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click( 1083 sqrt(static_cast<double>(5 * 5 + 5 * 5))); 1084 1085 scoped_ptr<GestureEventConsumeDelegate> delegate( 1086 new GestureEventConsumeDelegate()); 1087 TimedEvents tes; 1088 const int kWindowWidth = 123; 1089 const int kWindowHeight = 45; 1090 const int kTouchId = 5; 1091 gfx::Rect bounds(95, 195, kWindowWidth, kWindowHeight); 1092 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1093 delegate.get(), -1234, bounds, root_window())); 1094 1095 delegate->Reset(); 1096 // Tracks the total scroll since we want to verify that the correct position 1097 // will be scrolled to throughout the prediction. 1098 gfx::Vector2dF total_scroll; 1099 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(96, 196), 1100 kTouchId, tes.Now()); 1101 DispatchEventUsingWindowDispatcher(&press); 1102 EXPECT_2_EVENTS(delegate->events(), 1103 ui::ET_GESTURE_BEGIN, 1104 ui::ET_GESTURE_TAP_DOWN); 1105 delegate->Reset(); 1106 1107 // Get rid of touch slop. 1108 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(101, 201), 1109 kTouchId, tes.Now()); 1110 DispatchEventUsingWindowDispatcher(&move); 1111 EXPECT_3_EVENTS(delegate->events(), 1112 ui::ET_GESTURE_TAP_CANCEL, 1113 ui::ET_GESTURE_SCROLL_BEGIN, 1114 ui::ET_GESTURE_SCROLL_UPDATE); 1115 total_scroll.set_x(total_scroll.x() + delegate->scroll_x()); 1116 total_scroll.set_y(total_scroll.y() + delegate->scroll_y()); 1117 1118 // Move the touch-point enough so that it is considered as a scroll. This 1119 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures. 1120 // The first movement is diagonal, to ensure that we have a free scroll, 1121 // and not a rail scroll. 1122 tes.LeapForward(30); 1123 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get()); 1124 EXPECT_1_EVENT(delegate->events(), 1125 ui::ET_GESTURE_SCROLL_UPDATE); 1126 EXPECT_GT(delegate->scroll_velocity_x(), 0); 1127 EXPECT_GT(delegate->scroll_velocity_y(), 0); 1128 total_scroll.set_x(total_scroll.x() + delegate->scroll_x()); 1129 total_scroll.set_y(total_scroll.y() + delegate->scroll_y()); 1130 EXPECT_EQ((int)(29 + delegate->scroll_velocity_x() * prediction_interval), 1131 (int)(total_scroll.x())); 1132 EXPECT_EQ((int)(29 + delegate->scroll_velocity_y() * prediction_interval), 1133 (int)(total_scroll.y())); 1134 1135 // Move some more to generate a few more scroll updates. 1136 tes.LeapForward(30); 1137 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get()); 1138 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); 1139 total_scroll.set_x(total_scroll.x() + delegate->scroll_x()); 1140 total_scroll.set_y(total_scroll.y() + delegate->scroll_y()); 1141 EXPECT_EQ((int)(9 + delegate->scroll_velocity_x() * prediction_interval), 1142 (int)(total_scroll.x())); 1143 EXPECT_EQ((int)(10 + delegate->scroll_velocity_y() * prediction_interval), 1144 (int)(total_scroll.y())); 1145 1146 tes.LeapForward(30); 1147 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get()); 1148 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); 1149 total_scroll.set_x(total_scroll.x() + delegate->scroll_x()); 1150 total_scroll.set_y(total_scroll.y() + delegate->scroll_y()); 1151 EXPECT_EQ((int)(39 + delegate->scroll_velocity_x() * prediction_interval), 1152 (int)(total_scroll.x())); 1153 EXPECT_EQ((int)(14 + delegate->scroll_velocity_y() * prediction_interval), 1154 (int)(total_scroll.y())); 1155 1156 // Release the touch. This should end the scroll. 1157 delegate->Reset(); 1158 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1159 kTouchId, 1160 tes.LeapForward(50)); 1161 DispatchEventUsingWindowDispatcher(&release); 1162} 1163 1164// Check that the bounding box during a scroll event is correct. 1165TEST_F(GestureRecognizerTest, GestureEventScrollBoundingBox) { 1166 TimedEvents tes; 1167 for (int radius = 1; radius <= 10; ++radius) { 1168 ui::GestureConfiguration::set_default_radius(radius); 1169 scoped_ptr<GestureEventConsumeDelegate> delegate( 1170 new GestureEventConsumeDelegate()); 1171 const int kWindowWidth = 123; 1172 const int kWindowHeight = 45; 1173 const int kTouchId = 5; 1174 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 1175 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1176 delegate.get(), -1234, bounds, root_window())); 1177 1178 const int kPositionX = 101; 1179 const int kPositionY = 201; 1180 delegate->Reset(); 1181 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, 1182 gfx::Point(kPositionX, kPositionY), 1183 kTouchId, 1184 tes.Now()); 1185 DispatchEventUsingWindowDispatcher(&press); 1186 EXPECT_EQ(gfx::Rect(kPositionX - radius, 1187 kPositionY - radius, 1188 radius * 2, 1189 radius * 2).ToString(), 1190 delegate->bounding_box().ToString()); 1191 1192 const int kScrollAmount = 50; 1193 tes.SendScrollEvents(event_processor(), kPositionX, kPositionY, 1194 1, 1, kTouchId, 1, kScrollAmount, delegate.get()); 1195 EXPECT_EQ(gfx::Point(1, 1).ToString(), 1196 delegate->scroll_begin_position().ToString()); 1197 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius, 1198 kPositionY + kScrollAmount - radius, 1199 radius * 2, 1200 radius * 2).ToString(), 1201 delegate->bounding_box().ToString()); 1202 1203 // Release the touch. This should end the scroll. 1204 delegate->Reset(); 1205 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, 1206 gfx::Point(kPositionX + kScrollAmount, 1207 kPositionY + kScrollAmount), 1208 kTouchId, press.time_stamp() + 1209 base::TimeDelta::FromMilliseconds(50)); 1210 DispatchEventUsingWindowDispatcher(&release); 1211 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius, 1212 kPositionY + kScrollAmount - radius, 1213 radius * 2, 1214 radius * 2).ToString(), 1215 delegate->bounding_box().ToString()); 1216 } 1217 ui::GestureConfiguration::set_default_radius(0); 1218} 1219 1220// Check Scroll End Events report correct velocities 1221// if the user was on a horizontal rail 1222TEST_F(GestureRecognizerTest, GestureEventHorizontalRailFling) { 1223 scoped_ptr<GestureEventConsumeDelegate> delegate( 1224 new GestureEventConsumeDelegate()); 1225 TimedEvents tes; 1226 const int kTouchId = 7; 1227 gfx::Rect bounds(0, 0, 1000, 1000); 1228 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1229 delegate.get(), -1234, bounds, root_window())); 1230 1231 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 1232 kTouchId, tes.Now()); 1233 DispatchEventUsingWindowDispatcher(&press); 1234 1235 // Get rid of touch slop. 1236 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0), 1237 kTouchId, tes.Now()); 1238 DispatchEventUsingWindowDispatcher(&move); 1239 delegate->Reset(); 1240 1241 1242 // Move the touch-point horizontally enough that it is considered a 1243 // horizontal scroll. 1244 tes.SendScrollEvent(event_processor(), 25, 1, kTouchId, delegate.get()); 1245 EXPECT_EQ(0, delegate->scroll_y()); 1246 EXPECT_EQ(1, delegate->scroll_y_ordinal()); 1247 EXPECT_EQ(20, delegate->scroll_x()); 1248 EXPECT_EQ(20, delegate->scroll_x_ordinal()); 1249 1250 // Get a high x velocity, while still staying on the rail 1251 tes.SendScrollEvents(event_processor(), 1, 1, 1252 100, 10, kTouchId, 1, 1253 ui::GestureConfiguration::points_buffered_for_velocity(), 1254 delegate.get()); 1255 // The y-velocity during the scroll should be 0 since this is in a horizontal 1256 // rail scroll. 1257 EXPECT_GT(delegate->scroll_velocity_x(), 0); 1258 EXPECT_EQ(0, delegate->scroll_velocity_y()); 1259 1260 delegate->Reset(); 1261 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1262 kTouchId, tes.Now()); 1263 DispatchEventUsingWindowDispatcher(&release); 1264 1265 EXPECT_TRUE(delegate->fling()); 1266 EXPECT_FALSE(delegate->scroll_end()); 1267 EXPECT_GT(delegate->velocity_x(), 0); 1268 EXPECT_EQ(0, delegate->velocity_y()); 1269} 1270 1271// Check Scroll End Events report correct velocities 1272// if the user was on a vertical rail 1273TEST_F(GestureRecognizerTest, GestureEventVerticalRailFling) { 1274 scoped_ptr<GestureEventConsumeDelegate> delegate( 1275 new GestureEventConsumeDelegate()); 1276 TimedEvents tes; 1277 const int kTouchId = 7; 1278 gfx::Rect bounds(0, 0, 1000, 1000); 1279 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1280 delegate.get(), -1234, bounds, root_window())); 1281 1282 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 1283 kTouchId, tes.Now()); 1284 DispatchEventUsingWindowDispatcher(&press); 1285 1286 // Get rid of touch slop. 1287 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5), 1288 kTouchId, tes.Now()); 1289 DispatchEventUsingWindowDispatcher(&move); 1290 delegate->Reset(); 1291 1292 // Move the touch-point vertically enough that it is considered a 1293 // vertical scroll. 1294 tes.SendScrollEvent(event_processor(), 1, 25, kTouchId, delegate.get()); 1295 EXPECT_EQ(20, delegate->scroll_y()); 1296 EXPECT_EQ(20, delegate->scroll_y_ordinal()); 1297 EXPECT_EQ(0, delegate->scroll_x()); 1298 EXPECT_EQ(1, delegate->scroll_x_ordinal()); 1299 EXPECT_EQ(0, delegate->scroll_velocity_x()); 1300 EXPECT_GT(delegate->scroll_velocity_x_ordinal(), 0); 1301 1302 // Get a high y velocity, while still staying on the rail 1303 tes.SendScrollEvents(event_processor(), 1, 6, 1304 10, 100, kTouchId, 1, 1305 ui::GestureConfiguration::points_buffered_for_velocity(), 1306 delegate.get()); 1307 EXPECT_EQ(0, delegate->scroll_velocity_x()); 1308 EXPECT_GT(delegate->scroll_velocity_y(), 0); 1309 1310 delegate->Reset(); 1311 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 206), 1312 kTouchId, tes.Now()); 1313 DispatchEventUsingWindowDispatcher(&release); 1314 1315 EXPECT_TRUE(delegate->fling()); 1316 EXPECT_FALSE(delegate->scroll_end()); 1317 EXPECT_EQ(0, delegate->velocity_x()); 1318 EXPECT_GT(delegate->velocity_y(), 0); 1319} 1320 1321// Check Scroll End Events reports zero velocities 1322// if the user is not on a rail 1323TEST_F(GestureRecognizerTest, GestureEventNonRailFling) { 1324 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(0); 1325 scoped_ptr<GestureEventConsumeDelegate> delegate( 1326 new GestureEventConsumeDelegate()); 1327 TimedEvents tes; 1328 const int kTouchId = 7; 1329 gfx::Rect bounds(0, 0, 1000, 1000); 1330 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1331 delegate.get(), -1234, bounds, root_window())); 1332 1333 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 1334 kTouchId, tes.Now()); 1335 DispatchEventUsingWindowDispatcher(&press); 1336 1337 // Move the touch-point such that a non-rail scroll begins 1338 tes.SendScrollEvent(event_processor(), 20, 20, kTouchId, delegate.get()); 1339 EXPECT_EQ(20, delegate->scroll_y()); 1340 EXPECT_EQ(20, delegate->scroll_x()); 1341 1342 tes.SendScrollEvents(event_processor(), 1, 1, 1343 10, 100, kTouchId, 1, 1344 ui::GestureConfiguration::points_buffered_for_velocity(), 1345 delegate.get()); 1346 1347 delegate->Reset(); 1348 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1349 kTouchId, tes.Now()); 1350 DispatchEventUsingWindowDispatcher(&release); 1351 1352 EXPECT_TRUE(delegate->fling()); 1353 EXPECT_FALSE(delegate->scroll_end()); 1354 EXPECT_GT(delegate->velocity_x(), 0); 1355 EXPECT_GT(delegate->velocity_y(), 0); 1356} 1357 1358// Check that appropriate touch events generate long press events 1359TEST_F(GestureRecognizerTest, GestureEventLongPress) { 1360 scoped_ptr<GestureEventConsumeDelegate> delegate( 1361 new GestureEventConsumeDelegate()); 1362 TimedEvents tes; 1363 const int kWindowWidth = 123; 1364 const int kWindowHeight = 45; 1365 const int kTouchId = 2; 1366 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 1367 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1368 delegate.get(), -1234, bounds, root_window())); 1369 1370 delegate->Reset(); 1371 1372 TimerTestGestureRecognizer* gesture_recognizer = 1373 new TimerTestGestureRecognizer(); 1374 1375 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 1376 1377 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1378 kTouchId, tes.Now()); 1379 DispatchEventUsingWindowDispatcher(&press1); 1380 EXPECT_TRUE(delegate->tap_down()); 1381 EXPECT_TRUE(delegate->begin()); 1382 EXPECT_FALSE(delegate->tap_cancel()); 1383 1384 // We haven't pressed long enough for a long press to occur 1385 EXPECT_FALSE(delegate->long_press()); 1386 1387 // Wait until the timer runs out 1388 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS); 1389 EXPECT_TRUE(delegate->long_press()); 1390 EXPECT_FALSE(delegate->tap_cancel()); 1391 1392 delegate->Reset(); 1393 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1394 kTouchId, tes.Now()); 1395 DispatchEventUsingWindowDispatcher(&release1); 1396 EXPECT_FALSE(delegate->long_press()); 1397 1398 // Note the tap down isn't cancelled until the release 1399 EXPECT_TRUE(delegate->tap_cancel()); 1400} 1401 1402// Check that scrolling cancels a long press 1403TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) { 1404 scoped_ptr<GestureEventConsumeDelegate> delegate( 1405 new GestureEventConsumeDelegate()); 1406 TimedEvents tes; 1407 ui::GestureConfiguration::set_long_press_time_in_seconds(.01); 1408 const int kWindowWidth = 123; 1409 const int kWindowHeight = 45; 1410 const int kTouchId = 6; 1411 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 1412 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1413 delegate.get(), -1234, bounds, root_window())); 1414 1415 delegate->Reset(); 1416 1417 TimerTestGestureRecognizer* gesture_recognizer = 1418 new TimerTestGestureRecognizer(); 1419 TimerTestGestureSequence* gesture_sequence = 1420 static_cast<TimerTestGestureSequence*>( 1421 gesture_recognizer->GetGestureSequenceForTesting(window.get())); 1422 1423 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 1424 1425 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1426 kTouchId, tes.Now()); 1427 DispatchEventUsingWindowDispatcher(&press1); 1428 EXPECT_TRUE(delegate->tap_down()); 1429 1430 // We haven't pressed long enough for a long press to occur 1431 EXPECT_FALSE(delegate->long_press()); 1432 EXPECT_FALSE(delegate->tap_cancel()); 1433 1434 // Scroll around, to cancel the long press 1435 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get()); 1436 // Wait until the timer runs out 1437 gesture_sequence->ForceTimeout(); 1438 EXPECT_FALSE(delegate->long_press()); 1439 EXPECT_TRUE(delegate->tap_cancel()); 1440 1441 delegate->Reset(); 1442 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1443 kTouchId, tes.LeapForward(10)); 1444 DispatchEventUsingWindowDispatcher(&release1); 1445 EXPECT_FALSE(delegate->long_press()); 1446 EXPECT_FALSE(delegate->tap_cancel()); 1447} 1448 1449// Check that appropriate touch events generate long tap events 1450TEST_F(GestureRecognizerTest, GestureEventLongTap) { 1451 scoped_ptr<GestureEventConsumeDelegate> delegate( 1452 new GestureEventConsumeDelegate()); 1453 TimedEvents tes; 1454 const int kWindowWidth = 123; 1455 const int kWindowHeight = 45; 1456 const int kTouchId = 2; 1457 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 1458 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1459 delegate.get(), -1234, bounds, root_window())); 1460 1461 delegate->Reset(); 1462 1463 TimerTestGestureRecognizer* gesture_recognizer = 1464 new TimerTestGestureRecognizer(); 1465 1466 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 1467 1468 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1469 kTouchId, tes.Now()); 1470 DispatchEventUsingWindowDispatcher(&press1); 1471 EXPECT_TRUE(delegate->tap_down()); 1472 EXPECT_TRUE(delegate->begin()); 1473 EXPECT_FALSE(delegate->tap_cancel()); 1474 1475 // We haven't pressed long enough for a long press to occur 1476 EXPECT_FALSE(delegate->long_press()); 1477 1478 // Wait until the timer runs out 1479 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS); 1480 EXPECT_TRUE(delegate->long_press()); 1481 EXPECT_FALSE(delegate->tap_cancel()); 1482 1483 delegate->Reset(); 1484 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1485 kTouchId, tes.Now()); 1486 DispatchEventUsingWindowDispatcher(&release1); 1487 EXPECT_FALSE(delegate->long_press()); 1488 EXPECT_TRUE(delegate->long_tap()); 1489 1490 // Note the tap down isn't cancelled until the release 1491 EXPECT_TRUE(delegate->tap_cancel()); 1492} 1493 1494// Check that second tap cancels a long press 1495TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) { 1496 scoped_ptr<GestureEventConsumeDelegate> delegate( 1497 new GestureEventConsumeDelegate()); 1498 TimedEvents tes; 1499 ui::GestureConfiguration::set_long_press_time_in_seconds(.01); 1500 const int kWindowWidth = 300; 1501 const int kWindowHeight = 400; 1502 const int kTouchId1 = 8; 1503 const int kTouchId2 = 2; 1504 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight); 1505 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1506 delegate.get(), -1234, bounds, root_window())); 1507 1508 TimerTestGestureRecognizer* gesture_recognizer = 1509 new TimerTestGestureRecognizer(); 1510 TimerTestGestureSequence* gesture_sequence = 1511 static_cast<TimerTestGestureSequence*>( 1512 gesture_recognizer->GetGestureSequenceForTesting(window.get())); 1513 1514 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 1515 1516 delegate->Reset(); 1517 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1518 kTouchId1, tes.Now()); 1519 DispatchEventUsingWindowDispatcher(&press); 1520 EXPECT_TRUE(delegate->tap_down()); 1521 EXPECT_TRUE(delegate->begin()); 1522 1523 // We haven't pressed long enough for a long press to occur 1524 EXPECT_FALSE(delegate->long_press()); 1525 1526 // Second tap, to cancel the long press 1527 delegate->Reset(); 1528 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 1529 kTouchId2, tes.Now()); 1530 DispatchEventUsingWindowDispatcher(&press2); 1531 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap. 1532 EXPECT_TRUE(delegate->tap_cancel()); 1533 EXPECT_TRUE(delegate->begin()); 1534 1535 // Wait until the timer runs out 1536 gesture_sequence->ForceTimeout(); 1537 1538 // No long press occurred 1539 EXPECT_FALSE(delegate->long_press()); 1540 1541 delegate->Reset(); 1542 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1543 kTouchId1, tes.Now()); 1544 DispatchEventUsingWindowDispatcher(&release1); 1545 EXPECT_FALSE(delegate->long_press()); 1546 EXPECT_TRUE(delegate->two_finger_tap()); 1547 EXPECT_FALSE(delegate->tap_cancel()); 1548} 1549 1550// Check that horizontal scroll gestures cause scrolls on horizontal rails. 1551// Also tests that horizontal rails can be broken. 1552TEST_F(GestureRecognizerTest, GestureEventHorizontalRailScroll) { 1553 scoped_ptr<GestureEventConsumeDelegate> delegate( 1554 new GestureEventConsumeDelegate()); 1555 TimedEvents tes; 1556 const int kTouchId = 7; 1557 gfx::Rect bounds(0, 0, 1000, 1000); 1558 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1559 delegate.get(), -1234, bounds, root_window())); 1560 1561 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 1562 kTouchId, tes.Now()); 1563 DispatchEventUsingWindowDispatcher(&press); 1564 1565 // Get rid of touch slop. 1566 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0), 1567 kTouchId, tes.Now()); 1568 1569 DispatchEventUsingWindowDispatcher(&move); 1570 delegate->Reset(); 1571 1572 // Move the touch-point horizontally enough that it is considered a 1573 // horizontal scroll. 1574 tes.SendScrollEvent(event_processor(), 25, 1, kTouchId, delegate.get()); 1575 EXPECT_EQ(0, delegate->scroll_y()); 1576 EXPECT_EQ(20, delegate->scroll_x()); 1577 1578 tes.SendScrollEvent(event_processor(), 30, 6, kTouchId, delegate.get()); 1579 EXPECT_TRUE(delegate->scroll_update()); 1580 EXPECT_EQ(5, delegate->scroll_x()); 1581 // y shouldn't change, as we're on a horizontal rail. 1582 EXPECT_EQ(0, delegate->scroll_y()); 1583 1584 // Send enough information that a velocity can be calculated for the gesture, 1585 // and we can break the rail 1586 tes.SendScrollEvents(event_processor(), 1, 1, 1587 6, 100, kTouchId, 1, 1588 ui::GestureConfiguration::points_buffered_for_velocity(), 1589 delegate.get()); 1590 // Since the scroll is not longer railing, the velocity should be set for both 1591 // axis. 1592 EXPECT_GT(delegate->scroll_velocity_x(), 0); 1593 EXPECT_GT(delegate->scroll_velocity_y(), 0); 1594 1595 tes.SendScrollEvent(event_processor(), 5, 0, kTouchId, delegate.get()); 1596 tes.SendScrollEvent(event_processor(), 10, 5, kTouchId, delegate.get()); 1597 1598 // The rail should be broken 1599 EXPECT_TRUE(delegate->scroll_update()); 1600 EXPECT_EQ(5, delegate->scroll_x()); 1601 EXPECT_EQ(5, delegate->scroll_y()); 1602} 1603 1604// Check that vertical scroll gestures cause scrolls on vertical rails. 1605// Also tests that vertical rails can be broken. 1606TEST_F(GestureRecognizerTest, GestureEventVerticalRailScroll) { 1607 scoped_ptr<GestureEventConsumeDelegate> delegate( 1608 new GestureEventConsumeDelegate()); 1609 TimedEvents tes; 1610 const int kTouchId = 7; 1611 gfx::Rect bounds(0, 0, 1000, 1000); 1612 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1613 delegate.get(), -1234, bounds, root_window())); 1614 1615 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 1616 kTouchId, tes.Now()); 1617 DispatchEventUsingWindowDispatcher(&press); 1618 1619 // Get rid of touch slop. 1620 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5), 1621 kTouchId, tes.Now()); 1622 DispatchEventUsingWindowDispatcher(&move); 1623 delegate->Reset(); 1624 1625 // Move the touch-point vertically enough that it is considered a 1626 // vertical scroll. 1627 tes.SendScrollEvent(event_processor(), 1, 25, kTouchId, delegate.get()); 1628 EXPECT_EQ(0, delegate->scroll_x()); 1629 EXPECT_EQ(20, delegate->scroll_y()); 1630 1631 tes.SendScrollEvent(event_processor(), 6, 30, kTouchId, delegate.get()); 1632 EXPECT_TRUE(delegate->scroll_update()); 1633 EXPECT_EQ(5, delegate->scroll_y()); 1634 // x shouldn't change, as we're on a vertical rail. 1635 EXPECT_EQ(0, delegate->scroll_x()); 1636 EXPECT_EQ(0, delegate->scroll_velocity_x()); 1637 1638 // Send enough information that a velocity can be calculated for the gesture, 1639 // and we can break the rail 1640 tes.SendScrollEvents(event_processor(), 1, 6, 1641 100, 1, kTouchId, 1, 1642 ui::GestureConfiguration::points_buffered_for_velocity(), 1643 delegate.get()); 1644 EXPECT_GT(delegate->scroll_velocity_x(), 0); 1645 EXPECT_GT(delegate->scroll_velocity_y(), 0); 1646 1647 tes.SendScrollEvent(event_processor(), 0, 5, kTouchId, delegate.get()); 1648 tes.SendScrollEvent(event_processor(), 5, 10, kTouchId, delegate.get()); 1649 1650 // The rail should be broken 1651 EXPECT_TRUE(delegate->scroll_update()); 1652 EXPECT_EQ(5, delegate->scroll_x()); 1653 EXPECT_EQ(5, delegate->scroll_y()); 1654} 1655 1656TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) { 1657 // We'll start by moving the touch point by (5, 5). We want all of that 1658 // distance to be consumed by the slop, so we set the slop radius to 1659 // sqrt(5 * 5 + 5 * 5). 1660 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click( 1661 sqrt(static_cast<double>(5 * 5 + 5 * 5))); 1662 1663 // First, tap. Then, do a scroll using the same touch-id. 1664 scoped_ptr<GestureEventConsumeDelegate> delegate( 1665 new GestureEventConsumeDelegate()); 1666 TimedEvents tes; 1667 const int kWindowWidth = 123; 1668 const int kWindowHeight = 45; 1669 const int kTouchId = 3; 1670 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 1671 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1672 delegate.get(), -1234, bounds, root_window())); 1673 1674 delegate->Reset(); 1675 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1676 kTouchId, tes.Now()); 1677 DispatchEventUsingWindowDispatcher(&press); 1678 EXPECT_FALSE(delegate->tap()); 1679 EXPECT_TRUE(delegate->tap_down()); 1680 EXPECT_FALSE(delegate->tap_cancel()); 1681 EXPECT_FALSE(delegate->scroll_begin()); 1682 EXPECT_FALSE(delegate->scroll_update()); 1683 EXPECT_FALSE(delegate->scroll_end()); 1684 1685 // Make sure there is enough delay before the touch is released so that it is 1686 // recognized as a tap. 1687 delegate->Reset(); 1688 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1689 kTouchId, tes.LeapForward(50)); 1690 DispatchEventUsingWindowDispatcher(&release); 1691 EXPECT_TRUE(delegate->tap()); 1692 EXPECT_FALSE(delegate->tap_down()); 1693 EXPECT_FALSE(delegate->tap_cancel()); 1694 EXPECT_FALSE(delegate->scroll_begin()); 1695 EXPECT_FALSE(delegate->scroll_update()); 1696 EXPECT_FALSE(delegate->scroll_end()); 1697 1698 // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger 1699 // a double-tap. 1700 delegate->Reset(); 1701 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1702 kTouchId, tes.LeapForward(1000)); 1703 DispatchEventUsingWindowDispatcher(&press1); 1704 EXPECT_FALSE(delegate->tap()); 1705 EXPECT_TRUE(delegate->tap_down()); 1706 EXPECT_FALSE(delegate->tap_cancel()); 1707 EXPECT_FALSE(delegate->scroll_begin()); 1708 EXPECT_FALSE(delegate->scroll_update()); 1709 EXPECT_FALSE(delegate->scroll_end()); 1710 1711 // Get rid of touch slop. 1712 ui::TouchEvent move_remove_slop(ui::ET_TOUCH_MOVED, gfx::Point(106, 206), 1713 kTouchId, tes.Now()); 1714 DispatchEventUsingWindowDispatcher(&move_remove_slop); 1715 EXPECT_TRUE(delegate->tap_cancel()); 1716 EXPECT_TRUE(delegate->scroll_begin()); 1717 EXPECT_FALSE(delegate->scroll_update()); 1718 EXPECT_EQ(5, delegate->scroll_x_hint()); 1719 EXPECT_EQ(5, delegate->scroll_y_hint()); 1720 1721 delegate->Reset(); 1722 1723 // Move the touch-point enough so that it is considered as a scroll. This 1724 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures. 1725 // The first movement is diagonal, to ensure that we have a free scroll, 1726 // and not a rail scroll. 1727 delegate->Reset(); 1728 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(135, 235), 1729 kTouchId, tes.Now()); 1730 DispatchEventUsingWindowDispatcher(&move); 1731 EXPECT_FALSE(delegate->tap()); 1732 EXPECT_FALSE(delegate->tap_down()); 1733 EXPECT_FALSE(delegate->tap_cancel()); 1734 EXPECT_FALSE(delegate->scroll_begin()); 1735 EXPECT_TRUE(delegate->scroll_update()); 1736 EXPECT_FALSE(delegate->scroll_end()); 1737 EXPECT_EQ(29, delegate->scroll_x()); 1738 EXPECT_EQ(29, delegate->scroll_y()); 1739 1740 // Move some more to generate a few more scroll updates. 1741 delegate->Reset(); 1742 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(115, 216), 1743 kTouchId, tes.Now()); 1744 DispatchEventUsingWindowDispatcher(&move1); 1745 EXPECT_FALSE(delegate->tap()); 1746 EXPECT_FALSE(delegate->tap_down()); 1747 EXPECT_FALSE(delegate->tap_cancel()); 1748 EXPECT_FALSE(delegate->scroll_begin()); 1749 EXPECT_TRUE(delegate->scroll_update()); 1750 EXPECT_FALSE(delegate->scroll_end()); 1751 EXPECT_EQ(-20, delegate->scroll_x()); 1752 EXPECT_EQ(-19, delegate->scroll_y()); 1753 EXPECT_EQ(0, delegate->scroll_x_hint()); 1754 EXPECT_EQ(0, delegate->scroll_y_hint()); 1755 1756 delegate->Reset(); 1757 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 220), 1758 kTouchId, tes.Now()); 1759 DispatchEventUsingWindowDispatcher(&move2); 1760 EXPECT_FALSE(delegate->tap()); 1761 EXPECT_FALSE(delegate->tap_down()); 1762 EXPECT_FALSE(delegate->tap_cancel()); 1763 EXPECT_FALSE(delegate->scroll_begin()); 1764 EXPECT_TRUE(delegate->scroll_update()); 1765 EXPECT_FALSE(delegate->scroll_end()); 1766 EXPECT_EQ(30, delegate->scroll_x()); 1767 EXPECT_EQ(4, delegate->scroll_y()); 1768 1769 // Release the touch. This should end the scroll. 1770 delegate->Reset(); 1771 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1772 kTouchId, tes.Now()); 1773 DispatchEventUsingWindowDispatcher(&release1); 1774 EXPECT_FALSE(delegate->tap()); 1775 EXPECT_FALSE(delegate->tap_down()); 1776 EXPECT_FALSE(delegate->tap_cancel()); 1777 EXPECT_FALSE(delegate->scroll_begin()); 1778 EXPECT_FALSE(delegate->scroll_update()); 1779 EXPECT_FALSE(delegate->scroll_end()); 1780 EXPECT_TRUE(delegate->fling()); 1781} 1782 1783TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) { 1784 scoped_ptr<QueueTouchEventDelegate> queued_delegate( 1785 new QueueTouchEventDelegate(host()->dispatcher())); 1786 const int kWindowWidth = 123; 1787 const int kWindowHeight = 45; 1788 const int kTouchId1 = 6; 1789 const int kTouchId2 = 4; 1790 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 1791 scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate( 1792 queued_delegate.get(), -1234, bounds, root_window())); 1793 1794 queued_delegate->set_window(queue.get()); 1795 1796 // Touch down on the window. This should not generate any gesture event. 1797 queued_delegate->Reset(); 1798 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1799 kTouchId1, GetTime()); 1800 DispatchEventUsingWindowDispatcher(&press); 1801 EXPECT_FALSE(queued_delegate->tap()); 1802 EXPECT_FALSE(queued_delegate->tap_down()); 1803 EXPECT_FALSE(queued_delegate->tap_cancel()); 1804 EXPECT_FALSE(queued_delegate->begin()); 1805 EXPECT_FALSE(queued_delegate->scroll_begin()); 1806 EXPECT_FALSE(queued_delegate->scroll_update()); 1807 EXPECT_FALSE(queued_delegate->scroll_end()); 1808 1809 // Introduce some delay before the touch is released so that it is recognized 1810 // as a tap. However, this still should not create any gesture events. 1811 queued_delegate->Reset(); 1812 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 1813 kTouchId1, press.time_stamp() + 1814 base::TimeDelta::FromMilliseconds(50)); 1815 DispatchEventUsingWindowDispatcher(&release); 1816 EXPECT_FALSE(queued_delegate->tap()); 1817 EXPECT_FALSE(queued_delegate->tap_down()); 1818 EXPECT_FALSE(queued_delegate->tap_cancel()); 1819 EXPECT_FALSE(queued_delegate->begin()); 1820 EXPECT_FALSE(queued_delegate->end()); 1821 EXPECT_FALSE(queued_delegate->scroll_begin()); 1822 EXPECT_FALSE(queued_delegate->scroll_update()); 1823 EXPECT_FALSE(queued_delegate->scroll_end()); 1824 1825 // Create another window, and place a touch-down on it. This should create a 1826 // tap-down gesture. 1827 scoped_ptr<GestureEventConsumeDelegate> delegate( 1828 new GestureEventConsumeDelegate()); 1829 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1830 delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window())); 1831 delegate->Reset(); 1832 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 20), 1833 kTouchId2, GetTime()); 1834 DispatchEventUsingWindowDispatcher(&press2); 1835 EXPECT_FALSE(delegate->tap()); 1836 EXPECT_TRUE(delegate->tap_down()); 1837 EXPECT_FALSE(delegate->tap_cancel()); 1838 EXPECT_FALSE(queued_delegate->begin()); 1839 EXPECT_FALSE(queued_delegate->end()); 1840 EXPECT_FALSE(delegate->scroll_begin()); 1841 EXPECT_FALSE(delegate->scroll_update()); 1842 EXPECT_FALSE(delegate->scroll_end()); 1843 1844 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), 1845 kTouchId2, GetTime()); 1846 DispatchEventUsingWindowDispatcher(&release2); 1847 1848 // Process the first queued event. 1849 queued_delegate->Reset(); 1850 queued_delegate->ReceivedAck(); 1851 EXPECT_FALSE(queued_delegate->tap()); 1852 EXPECT_TRUE(queued_delegate->tap_down()); 1853 EXPECT_TRUE(queued_delegate->begin()); 1854 EXPECT_FALSE(queued_delegate->tap_cancel()); 1855 EXPECT_FALSE(queued_delegate->end()); 1856 EXPECT_FALSE(queued_delegate->scroll_begin()); 1857 EXPECT_FALSE(queued_delegate->scroll_update()); 1858 EXPECT_FALSE(queued_delegate->scroll_end()); 1859 1860 // Now, process the second queued event. 1861 queued_delegate->Reset(); 1862 queued_delegate->ReceivedAck(); 1863 EXPECT_TRUE(queued_delegate->tap()); 1864 EXPECT_FALSE(queued_delegate->tap_down()); 1865 EXPECT_FALSE(queued_delegate->tap_cancel()); 1866 EXPECT_FALSE(queued_delegate->begin()); 1867 EXPECT_TRUE(queued_delegate->end()); 1868 EXPECT_FALSE(queued_delegate->scroll_begin()); 1869 EXPECT_FALSE(queued_delegate->scroll_update()); 1870 EXPECT_FALSE(queued_delegate->scroll_end()); 1871 1872 // Start all over. Press on the first window, then press again on the second 1873 // window. The second press should still go to the first window. 1874 queued_delegate->Reset(); 1875 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1876 kTouchId1, GetTime()); 1877 DispatchEventUsingWindowDispatcher(&press3); 1878 EXPECT_FALSE(queued_delegate->tap()); 1879 EXPECT_FALSE(queued_delegate->tap_down()); 1880 EXPECT_FALSE(queued_delegate->tap_cancel()); 1881 EXPECT_FALSE(queued_delegate->begin()); 1882 EXPECT_FALSE(queued_delegate->end()); 1883 EXPECT_FALSE(queued_delegate->begin()); 1884 EXPECT_FALSE(queued_delegate->end()); 1885 EXPECT_FALSE(queued_delegate->scroll_begin()); 1886 EXPECT_FALSE(queued_delegate->scroll_update()); 1887 EXPECT_FALSE(queued_delegate->scroll_end()); 1888 1889 queued_delegate->Reset(); 1890 delegate->Reset(); 1891 ui::TouchEvent press4(ui::ET_TOUCH_PRESSED, gfx::Point(103, 203), 1892 kTouchId2, GetTime()); 1893 DispatchEventUsingWindowDispatcher(&press4); 1894 EXPECT_FALSE(delegate->tap()); 1895 EXPECT_FALSE(delegate->tap_down()); 1896 EXPECT_FALSE(delegate->tap_cancel()); 1897 EXPECT_FALSE(delegate->begin()); 1898 EXPECT_FALSE(delegate->end()); 1899 EXPECT_FALSE(delegate->scroll_begin()); 1900 EXPECT_FALSE(delegate->scroll_update()); 1901 EXPECT_FALSE(delegate->scroll_end()); 1902 EXPECT_FALSE(queued_delegate->tap()); 1903 EXPECT_FALSE(queued_delegate->tap_down()); 1904 EXPECT_FALSE(queued_delegate->tap_cancel()); 1905 EXPECT_FALSE(queued_delegate->begin()); 1906 EXPECT_FALSE(queued_delegate->end()); 1907 EXPECT_FALSE(queued_delegate->scroll_begin()); 1908 EXPECT_FALSE(queued_delegate->scroll_update()); 1909 EXPECT_FALSE(queued_delegate->scroll_end()); 1910 1911 // Move the second touch-point enough so that it is considered a pinch. This 1912 // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures. 1913 queued_delegate->Reset(); 1914 delegate->Reset(); 1915 int x_move = ui::GestureConfiguration::max_touch_move_in_pixels_for_click(); 1916 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103 + x_move, 203), 1917 kTouchId2, GetTime()); 1918 DispatchEventUsingWindowDispatcher(&move); 1919 EXPECT_FALSE(delegate->tap()); 1920 EXPECT_FALSE(delegate->tap_down()); 1921 EXPECT_FALSE(delegate->tap_cancel()); 1922 EXPECT_FALSE(delegate->begin()); 1923 EXPECT_FALSE(delegate->scroll_begin()); 1924 EXPECT_FALSE(delegate->scroll_update()); 1925 EXPECT_FALSE(delegate->scroll_end()); 1926 EXPECT_FALSE(queued_delegate->tap()); 1927 EXPECT_FALSE(queued_delegate->tap_down()); 1928 EXPECT_FALSE(queued_delegate->tap_cancel()); 1929 EXPECT_FALSE(queued_delegate->begin()); 1930 EXPECT_FALSE(queued_delegate->scroll_begin()); 1931 EXPECT_FALSE(queued_delegate->scroll_update()); 1932 EXPECT_FALSE(queued_delegate->scroll_end()); 1933 1934 queued_delegate->Reset(); 1935 queued_delegate->ReceivedAck(); 1936 EXPECT_FALSE(queued_delegate->tap()); 1937 EXPECT_TRUE(queued_delegate->tap_down()); 1938 EXPECT_TRUE(queued_delegate->begin()); 1939 EXPECT_FALSE(queued_delegate->tap_cancel()); 1940 EXPECT_FALSE(queued_delegate->end()); 1941 EXPECT_FALSE(queued_delegate->scroll_begin()); 1942 EXPECT_FALSE(queued_delegate->scroll_update()); 1943 EXPECT_FALSE(queued_delegate->scroll_end()); 1944 1945 queued_delegate->Reset(); 1946 queued_delegate->ReceivedAck(); 1947 EXPECT_FALSE(queued_delegate->tap()); 1948 EXPECT_FALSE(queued_delegate->tap_down()); // no touch down for second tap. 1949 EXPECT_TRUE(queued_delegate->tap_cancel()); 1950 EXPECT_TRUE(queued_delegate->begin()); 1951 EXPECT_FALSE(queued_delegate->end()); 1952 EXPECT_FALSE(queued_delegate->scroll_begin()); 1953 EXPECT_FALSE(queued_delegate->scroll_update()); 1954 EXPECT_FALSE(queued_delegate->scroll_end()); 1955 EXPECT_FALSE(queued_delegate->pinch_begin()); 1956 EXPECT_FALSE(queued_delegate->pinch_update()); 1957 EXPECT_FALSE(queued_delegate->pinch_end()); 1958 1959 queued_delegate->Reset(); 1960 queued_delegate->ReceivedAck(); 1961 EXPECT_FALSE(queued_delegate->tap()); 1962 EXPECT_FALSE(queued_delegate->tap_down()); 1963 EXPECT_FALSE(queued_delegate->tap_cancel()); 1964 EXPECT_FALSE(queued_delegate->begin()); 1965 EXPECT_FALSE(queued_delegate->end()); 1966 EXPECT_TRUE(queued_delegate->scroll_begin()); 1967 EXPECT_FALSE(queued_delegate->scroll_update()); 1968 EXPECT_FALSE(queued_delegate->scroll_end()); 1969 EXPECT_TRUE(queued_delegate->pinch_begin()); 1970 EXPECT_FALSE(queued_delegate->pinch_update()); 1971 EXPECT_FALSE(queued_delegate->pinch_end()); 1972} 1973 1974// Check that appropriate touch events generate pinch gesture events. 1975TEST_F(GestureRecognizerTest, GestureEventPinchFromScroll) { 1976 scoped_ptr<GestureEventConsumeDelegate> delegate( 1977 new GestureEventConsumeDelegate()); 1978 TimedEvents tes; 1979 const int kWindowWidth = 300; 1980 const int kWindowHeight = 400; 1981 const int kTouchId1 = 5; 1982 const int kTouchId2 = 3; 1983 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight); 1984 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 1985 delegate.get(), -1234, bounds, root_window())); 1986 1987 delegate->Reset(); 1988 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 1989 kTouchId1, tes.Now()); 1990 DispatchEventUsingWindowDispatcher(&press); 1991 EXPECT_2_EVENTS(delegate->events(), 1992 ui::ET_GESTURE_BEGIN, 1993 ui::ET_GESTURE_TAP_DOWN); 1994 1995 // Move the touch-point enough so that it is considered as a scroll. This 1996 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures. 1997 delegate->Reset(); 1998 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301), 1999 kTouchId1, tes.Now()); 2000 DispatchEventUsingWindowDispatcher(&move); 2001 EXPECT_3_EVENTS(delegate->events(), 2002 ui::ET_GESTURE_TAP_CANCEL, 2003 ui::ET_GESTURE_SCROLL_BEGIN, 2004 ui::ET_GESTURE_SCROLL_UPDATE); 2005 2006 // Press the second finger. It should cause pinch-begin. Note that we will not 2007 // transition to two finger tap here because the touch points are far enough. 2008 delegate->Reset(); 2009 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 2010 kTouchId2, tes.Now()); 2011 DispatchEventUsingWindowDispatcher(&press2); 2012 EXPECT_2_EVENTS(delegate->events(), 2013 ui::ET_GESTURE_BEGIN, 2014 ui::ET_GESTURE_PINCH_BEGIN); 2015 EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(), 2016 delegate->bounding_box().ToString()); 2017 2018 // Move the first finger. 2019 delegate->Reset(); 2020 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201), 2021 kTouchId1, tes.Now()); 2022 DispatchEventUsingWindowDispatcher(&move3); 2023 EXPECT_2_EVENTS(delegate->events(), 2024 ui::ET_GESTURE_PINCH_UPDATE, 2025 ui::ET_GESTURE_SCROLL_UPDATE); 2026 EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(), 2027 delegate->bounding_box().ToString()); 2028 2029 // Now move the second finger. 2030 delegate->Reset(); 2031 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15), 2032 kTouchId2, tes.Now()); 2033 DispatchEventUsingWindowDispatcher(&move4); 2034 EXPECT_2_EVENTS(delegate->events(), 2035 ui::ET_GESTURE_PINCH_UPDATE, 2036 ui::ET_GESTURE_SCROLL_UPDATE); 2037 EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(), 2038 delegate->bounding_box().ToString()); 2039 2040 // Release the first finger. This should end pinch. 2041 delegate->Reset(); 2042 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2043 kTouchId1, tes.Now()); 2044 DispatchEventUsingWindowDispatcher(&release); 2045 EXPECT_2_EVENTS(delegate->events(), 2046 ui::ET_GESTURE_PINCH_END, 2047 ui::ET_GESTURE_END); 2048 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(), 2049 delegate->bounding_box().ToString()); 2050 2051 // Move the second finger. This should still generate a scroll. 2052 delegate->Reset(); 2053 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10), 2054 kTouchId2, tes.Now()); 2055 DispatchEventUsingWindowDispatcher(&move5); 2056 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); 2057 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 2058} 2059 2060TEST_F(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) { 2061scoped_ptr<GestureEventConsumeDelegate> delegate( 2062 new GestureEventConsumeDelegate()); 2063 TimedEvents tes; 2064 const int kWindowWidth = 300; 2065 const int kWindowHeight = 400; 2066 const int kTouchId1 = 5; 2067 const int kTouchId2 = 3; 2068 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight); 2069 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2070 delegate.get(), -1234, bounds, root_window())); 2071 2072 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301), 2073 kTouchId1, tes.Now()); 2074 DispatchEventUsingWindowDispatcher(&press); 2075 delegate->Reset(); 2076 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 2077 kTouchId2, tes.Now()); 2078 DispatchEventUsingWindowDispatcher(&press2); 2079 EXPECT_FALSE(delegate->pinch_begin()); 2080 2081 // Touch move triggers pinch begin. 2082 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get()); 2083 EXPECT_TRUE(delegate->pinch_begin()); 2084 EXPECT_FALSE(delegate->pinch_update()); 2085 2086 // Touch move triggers pinch update. 2087 tes.SendScrollEvent(event_processor(), 160, 200, kTouchId1, delegate.get()); 2088 EXPECT_FALSE(delegate->pinch_begin()); 2089 EXPECT_TRUE(delegate->pinch_update()); 2090 2091 // Pinch has started, now release the second finger 2092 delegate->Reset(); 2093 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2094 kTouchId1, tes.Now()); 2095 DispatchEventUsingWindowDispatcher(&release); 2096 EXPECT_TRUE(delegate->pinch_end()); 2097 2098 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId2, delegate.get()); 2099 EXPECT_TRUE(delegate->scroll_update()); 2100 2101 // Pinch again 2102 delegate->Reset(); 2103 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 2104 kTouchId1, tes.Now()); 2105 DispatchEventUsingWindowDispatcher(&press3); 2106 // Now the touch points are close. So we will go into two finger tap. 2107 // Move the touch-point enough to break two-finger-tap and enter pinch. 2108 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 202), 2109 kTouchId1, tes.Now()); 2110 DispatchEventUsingWindowDispatcher(&move2); 2111 EXPECT_TRUE(delegate->pinch_begin()); 2112 2113 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get()); 2114 EXPECT_TRUE(delegate->pinch_update()); 2115} 2116 2117TEST_F(GestureRecognizerTest, GestureEventPinchFromTap) { 2118 scoped_ptr<GestureEventConsumeDelegate> delegate( 2119 new GestureEventConsumeDelegate()); 2120 TimedEvents tes; 2121 const int kWindowWidth = 300; 2122 const int kWindowHeight = 400; 2123 const int kTouchId1 = 3; 2124 const int kTouchId2 = 5; 2125 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight); 2126 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2127 delegate.get(), -1234, bounds, root_window())); 2128 2129 delegate->Reset(); 2130 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301), 2131 kTouchId1, tes.Now()); 2132 DispatchEventUsingWindowDispatcher(&press); 2133 EXPECT_2_EVENTS(delegate->events(), 2134 ui::ET_GESTURE_BEGIN, 2135 ui::ET_GESTURE_TAP_DOWN); 2136 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 2137 2138 // Press the second finger far enough to break two finger tap. 2139 delegate->Reset(); 2140 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 2141 kTouchId2, tes.Now()); 2142 DispatchEventUsingWindowDispatcher(&press2); 2143 EXPECT_2_EVENTS(delegate->events(), 2144 ui::ET_GESTURE_TAP_CANCEL, 2145 ui::ET_GESTURE_BEGIN); 2146 EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(), 2147 delegate->bounding_box().ToString()); 2148 2149 // Move the first finger. 2150 delegate->Reset(); 2151 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201), 2152 kTouchId1, tes.Now()); 2153 DispatchEventUsingWindowDispatcher(&move3); 2154 EXPECT_2_EVENTS(delegate->events(), 2155 ui::ET_GESTURE_SCROLL_BEGIN, 2156 ui::ET_GESTURE_PINCH_BEGIN); 2157 EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(), 2158 delegate->bounding_box().ToString()); 2159 2160 // Now move the second finger. 2161 delegate->Reset(); 2162 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15), 2163 kTouchId2, tes.Now()); 2164 DispatchEventUsingWindowDispatcher(&move4); 2165 EXPECT_2_EVENTS(delegate->events(), 2166 ui::ET_GESTURE_PINCH_UPDATE, 2167 ui::ET_GESTURE_SCROLL_UPDATE); 2168 EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(), 2169 delegate->bounding_box().ToString()); 2170 2171 // Release the first finger. This should end pinch. 2172 delegate->Reset(); 2173 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2174 kTouchId1, tes.LeapForward(10)); 2175 DispatchEventUsingWindowDispatcher(&release); 2176 EXPECT_2_EVENTS(delegate->events(), 2177 ui::ET_GESTURE_PINCH_END, 2178 ui::ET_GESTURE_END); 2179 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(), 2180 delegate->bounding_box().ToString()); 2181 2182 // Move the second finger. This should still generate a scroll. 2183 delegate->Reset(); 2184 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10), 2185 kTouchId2, tes.Now()); 2186 DispatchEventUsingWindowDispatcher(&move5); 2187 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); 2188 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 2189} 2190 2191TEST_F(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) { 2192 scoped_ptr<GestureEventConsumeDelegate> delegate( 2193 new GestureEventConsumeDelegate()); 2194 TimedEvents tes; 2195 2196 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2197 6, tes.Now()); 2198 DispatchEventUsingWindowDispatcher(&release1); 2199 EXPECT_FALSE(delegate->tap()); 2200 EXPECT_FALSE(delegate->tap_down()); 2201} 2202 2203// Check that a touch is locked to the window of the closest current touch 2204// within max_separation_for_gesture_touches_in_pixels 2205TEST_F(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) { 2206 ui::GestureRecognizer* gesture_recognizer = new ui::GestureRecognizerImpl(); 2207 TimedEvents tes; 2208 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 2209 2210 ui::GestureConsumer* target; 2211 const int kNumWindows = 4; 2212 2213 scoped_ptr<GestureEventConsumeDelegate*[]> delegates( 2214 new GestureEventConsumeDelegate*[kNumWindows]); 2215 2216 ui::GestureConfiguration:: 2217 set_max_separation_for_gesture_touches_in_pixels(499); 2218 2219 scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]); 2220 window_bounds[0] = gfx::Rect(0, 0, 1, 1); 2221 window_bounds[1] = gfx::Rect(500, 0, 1, 1); 2222 window_bounds[2] = gfx::Rect(0, 500, 1, 1); 2223 window_bounds[3] = gfx::Rect(500, 500, 1, 1); 2224 2225 scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]); 2226 2227 // Instantiate windows with |window_bounds| and touch each window at 2228 // its origin. 2229 for (int i = 0; i < kNumWindows; ++i) { 2230 delegates[i] = new GestureEventConsumeDelegate(); 2231 windows[i] = CreateTestWindowWithDelegate( 2232 delegates[i], i, window_bounds[i], root_window()); 2233 windows[i]->set_id(i); 2234 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(), 2235 i, tes.Now()); 2236 DispatchEventUsingWindowDispatcher(&press); 2237 } 2238 2239 // Touches should now be associated with the closest touch within 2240 // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels 2241 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11), -1); 2242 EXPECT_EQ("0", WindowIDAsString(target)); 2243 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11), -1); 2244 EXPECT_EQ("1", WindowIDAsString(target)); 2245 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511), -1); 2246 EXPECT_EQ("2", WindowIDAsString(target)); 2247 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511), -1); 2248 EXPECT_EQ("3", WindowIDAsString(target)); 2249 2250 // Add a touch in the middle associated with windows[2] 2251 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500), 2252 kNumWindows, tes.Now()); 2253 DispatchEventUsingWindowDispatcher(&press); 2254 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250), 2255 kNumWindows, tes.Now()); 2256 DispatchEventUsingWindowDispatcher(&move); 2257 2258 target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250), -1); 2259 EXPECT_EQ("2", WindowIDAsString(target)); 2260 2261 // Make sure that ties are broken by distance to a current touch 2262 // Closer to the point in the bottom right. 2263 target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380), -1); 2264 EXPECT_EQ("3", WindowIDAsString(target)); 2265 2266 // This touch is closer to the point in the middle 2267 target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300), -1); 2268 EXPECT_EQ("2", WindowIDAsString(target)); 2269 2270 // A touch too far from other touches won't be locked to anything 2271 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1); 2272 EXPECT_TRUE(target == NULL); 2273 2274 // Move a touch associated with windows[2] to 1000, 1000 2275 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000), 2276 kNumWindows, tes.Now()); 2277 DispatchEventUsingWindowDispatcher(&move2); 2278 2279 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1); 2280 EXPECT_EQ("2", WindowIDAsString(target)); 2281 2282 for (int i = 0; i < kNumWindows; ++i) { 2283 // Delete windows before deleting delegates. 2284 delete windows[i]; 2285 delete delegates[i]; 2286 } 2287} 2288 2289// Check that a touch's target will not be effected by a touch on a different 2290// screen. 2291TEST_F(GestureRecognizerTest, GestureEventTouchLockIgnoresOtherScreens) { 2292 scoped_ptr<GestureEventConsumeDelegate> delegate( 2293 new GestureEventConsumeDelegate()); 2294 gfx::Rect bounds(0, 0, 10, 10); 2295 scoped_ptr<aura::Window> window( 2296 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window())); 2297 2298 const int kTouchId1 = 8; 2299 const int kTouchId2 = 2; 2300 TimedEvents tes; 2301 2302 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(5, 5), 2303 kTouchId1, tes.Now()); 2304 press1.set_source_device_id(1); 2305 DispatchEventUsingWindowDispatcher(&press1); 2306 2307 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20), 2308 kTouchId2, tes.Now()); 2309 press2.set_source_device_id(2); 2310 DispatchEventUsingWindowDispatcher(&press2); 2311 2312 // The second press should not have been locked to the same target as the 2313 // first, as they occured on different displays. 2314 EXPECT_NE( 2315 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1), 2316 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2)); 2317} 2318 2319// Check that touch events outside the root window are still handled 2320// by the root window's gesture sequence. 2321TEST_F(GestureRecognizerTest, GestureEventOutsideRootWindowTap) { 2322 TestGestureRecognizer* gesture_recognizer = 2323 new TestGestureRecognizer(); 2324 TimedEvents tes; 2325 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 2326 2327 scoped_ptr<aura::Window> window(CreateTestWindowWithBounds( 2328 gfx::Rect(-100, -100, 2000, 2000), root_window())); 2329 2330 ui::GestureSequence* window_gesture_sequence = 2331 gesture_recognizer->GetGestureSequenceForTesting(window.get()); 2332 2333 ui::GestureSequence* root_window_gesture_sequence = 2334 gesture_recognizer->GetGestureSequenceForTesting(root_window()); 2335 2336 gfx::Point pos1(-10, -10); 2337 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now()); 2338 DispatchEventUsingWindowDispatcher(&press1); 2339 2340 gfx::Point pos2(1000, 1000); 2341 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now()); 2342 DispatchEventUsingWindowDispatcher(&press2); 2343 2344 // As these presses were outside the root window, they should be 2345 // associated with the root window. 2346 EXPECT_EQ(0, window_gesture_sequence->point_count()); 2347 EXPECT_EQ(2, root_window_gesture_sequence->point_count()); 2348} 2349 2350TEST_F(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) { 2351 scoped_ptr<QueueTouchEventDelegate> delegate( 2352 new QueueTouchEventDelegate(host()->dispatcher())); 2353 TimedEvents tes; 2354 const int kTouchId = 2; 2355 gfx::Rect bounds(100, 200, 100, 100); 2356 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2357 delegate.get(), -1234, bounds, root_window())); 2358 delegate->set_window(window.get()); 2359 2360 delegate->Reset(); 2361 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2362 kTouchId, tes.Now()); 2363 DispatchEventUsingWindowDispatcher(&press); 2364 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2365 kTouchId, tes.LeapForward(50)); 2366 DispatchEventUsingWindowDispatcher(&release); 2367 2368 delegate->Reset(); 2369 delegate->ReceivedAck(); 2370 EXPECT_TRUE(delegate->tap_down()); 2371 delegate->Reset(); 2372 delegate->ReceivedAckPreventDefaulted(); 2373 EXPECT_FALSE(delegate->tap()); 2374 EXPECT_TRUE(delegate->tap_cancel()); 2375} 2376 2377TEST_F(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) { 2378 scoped_ptr<QueueTouchEventDelegate> delegate( 2379 new QueueTouchEventDelegate(host()->dispatcher())); 2380 TimedEvents tes; 2381 const int kTouchId1 = 7; 2382 const int kTouchId2 = 5; 2383 gfx::Rect bounds(10, 20, 100, 100); 2384 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2385 delegate.get(), -1234, bounds, root_window())); 2386 delegate->set_window(window.get()); 2387 2388 { 2389 delegate->Reset(); 2390 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1, 2391 tes.Now()); 2392 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1, 2393 tes.LeapForward(200)); 2394 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1, 2395 tes.LeapForward(50)); 2396 DispatchEventUsingWindowDispatcher(&press); 2397 DispatchEventUsingWindowDispatcher(&move); 2398 DispatchEventUsingWindowDispatcher(&release); 2399 delegate->Reset(); 2400 2401 // Ack the press event. 2402 delegate->ReceivedAck(); 2403 EXPECT_TRUE(delegate->tap_down()); 2404 delegate->Reset(); 2405 2406 // Ack the move event. 2407 delegate->ReceivedAck(); 2408 EXPECT_TRUE(delegate->tap_cancel()); 2409 EXPECT_TRUE(delegate->scroll_begin()); 2410 delegate->Reset(); 2411 2412 // Ack the release event. Although the release event has been processed, it 2413 // should still generate a scroll-end event. 2414 delegate->ReceivedAckPreventDefaulted(); 2415 EXPECT_TRUE(delegate->scroll_end()); 2416 } 2417 2418 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1, 2419 tes.Now()); 2420 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1, 2421 tes.LeapForward(200)); 2422 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1, 2423 tes.LeapForward(50)); 2424 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2, 2425 tes.Now()); 2426 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(45, 85), kTouchId2, 2427 tes.LeapForward(1000)); 2428 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(45, 85), kTouchId2, 2429 tes.LeapForward(14)); 2430 2431 // Do a pinch. 2432 DispatchEventUsingWindowDispatcher(&press); 2433 DispatchEventUsingWindowDispatcher(&move); 2434 DispatchEventUsingWindowDispatcher(&press2); 2435 DispatchEventUsingWindowDispatcher(&move2); 2436 DispatchEventUsingWindowDispatcher(&release); 2437 DispatchEventUsingWindowDispatcher(&release2); 2438 2439 // Ack the press and move events. 2440 delegate->Reset(); 2441 delegate->ReceivedAck(); 2442 EXPECT_TRUE(delegate->begin()); 2443 EXPECT_TRUE(delegate->tap_down()); 2444 2445 delegate->Reset(); 2446 delegate->ReceivedAck(); 2447 EXPECT_TRUE(delegate->scroll_begin()); 2448 2449 delegate->Reset(); 2450 delegate->ReceivedAck(); 2451 EXPECT_TRUE(delegate->begin()); 2452 EXPECT_TRUE(delegate->pinch_begin()); 2453 2454 delegate->Reset(); 2455 delegate->ReceivedAck(); 2456 EXPECT_TRUE(delegate->pinch_update()); 2457 2458 // Ack the first release. Although the release is processed, it should still 2459 // generate a pinch-end event. 2460 delegate->Reset(); 2461 delegate->ReceivedAckPreventDefaulted(); 2462 EXPECT_TRUE(delegate->pinch_end()); 2463 EXPECT_TRUE(delegate->end()); 2464 2465 delegate->Reset(); 2466 delegate->ReceivedAckPreventDefaulted(); 2467 EXPECT_TRUE(delegate->scroll_end()); 2468 EXPECT_TRUE(delegate->end()); 2469} 2470 2471TEST_F(GestureRecognizerTest, GestureEndLocation) { 2472 GestureEventConsumeDelegate delegate; 2473 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2474 &delegate, -1234, gfx::Rect(10, 10, 300, 300), root_window())); 2475 EventGenerator generator(root_window(), window.get()); 2476 const gfx::Point begin(20, 20); 2477 const gfx::Point end(150, 150); 2478 const gfx::Vector2d window_offset = 2479 window->bounds().origin().OffsetFromOrigin(); 2480 generator.GestureScrollSequence(begin, end, 2481 base::TimeDelta::FromMilliseconds(20), 2482 10); 2483 EXPECT_EQ((begin - window_offset).ToString(), 2484 delegate.scroll_begin_position().ToString()); 2485 EXPECT_EQ((end - window_offset).ToString(), 2486 delegate.gesture_end_location().ToString()); 2487} 2488 2489TEST_F(GestureRecognizerTest, CaptureSendsGestureEnd) { 2490 scoped_ptr<GestureEventConsumeDelegate> delegate( 2491 new GestureEventConsumeDelegate()); 2492 TestGestureRecognizer* gesture_recognizer = 2493 new TestGestureRecognizer(); 2494 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 2495 2496 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2497 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window())); 2498 EventGenerator generator(root_window()); 2499 2500 generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10)); 2501 generator.PressTouch(); 2502 RunAllPendingInMessageLoop(); 2503 2504 EXPECT_TRUE(delegate->tap_down()); 2505 2506 scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds( 2507 gfx::Rect(10, 10, 200, 200), root_window())); 2508 capture->SetCapture(); 2509 RunAllPendingInMessageLoop(); 2510 2511 EXPECT_TRUE(delegate->end()); 2512 EXPECT_TRUE(delegate->tap_cancel()); 2513} 2514 2515// Check that previous touch actions that are completely finished (either 2516// released or cancelled), do not receive extra synthetic cancels upon change of 2517// capture. 2518TEST_F(GestureRecognizerTest, CaptureDoesNotCancelFinishedTouches) { 2519 scoped_ptr<GestureEventConsumeDelegate> delegate( 2520 new GestureEventConsumeDelegate()); 2521 scoped_ptr<TestEventHandler> handler(new TestEventHandler); 2522 root_window()->AddPreTargetHandler(handler.get()); 2523 2524 // Create a window and set it as the capture window. 2525 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(delegate.get(), 2526 -1234, gfx::Rect(10, 10, 300, 300), root_window())); 2527 window1->SetCapture(); 2528 2529 EventGenerator generator(root_window()); 2530 TimedEvents tes; 2531 2532 // Generate two touch-press events on the window. 2533 scoped_ptr<ui::TouchEvent> touch0(new ui::TouchEvent(ui::ET_TOUCH_PRESSED, 2534 gfx::Point(20, 20), 0, 2535 tes.Now())); 2536 scoped_ptr<ui::TouchEvent> touch1(new ui::TouchEvent(ui::ET_TOUCH_PRESSED, 2537 gfx::Point(30, 30), 1, 2538 tes.Now())); 2539 generator.Dispatch(touch0.get()); 2540 generator.Dispatch(touch1.get()); 2541 RunAllPendingInMessageLoop(); 2542 EXPECT_EQ(2, handler->touch_pressed_count()); 2543 2544 // Advance time. 2545 tes.LeapForward(1000); 2546 2547 // End the two touches, one by a touch-release and one by a touch-cancel; to 2548 // cover both cases. 2549 touch0.reset(new ui::TouchEvent(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0, 2550 tes.Now())); 2551 touch1.reset(new ui::TouchEvent(ui::ET_TOUCH_CANCELLED, gfx::Point(30, 30), 1, 2552 tes.Now())); 2553 generator.Dispatch(touch0.get()); 2554 generator.Dispatch(touch1.get()); 2555 RunAllPendingInMessageLoop(); 2556 EXPECT_EQ(1, handler->touch_released_count()); 2557 EXPECT_EQ(1, handler->touch_cancelled_count()); 2558 2559 // Create a new window and set it as the new capture window. 2560 scoped_ptr<aura::Window> window2(CreateTestWindowWithBounds( 2561 gfx::Rect(100, 100, 300, 300), root_window())); 2562 window2->SetCapture(); 2563 RunAllPendingInMessageLoop(); 2564 // Check that setting capture does not generate any synthetic touch-cancels 2565 // for the two previously finished touch actions. 2566 EXPECT_EQ(1, handler->touch_cancelled_count()); 2567 2568 root_window()->RemovePreTargetHandler(handler.get()); 2569} 2570 2571TEST_F(GestureRecognizerTest, PressDoesNotCrash) { 2572 scoped_ptr<GestureEventConsumeDelegate> delegate( 2573 new GestureEventConsumeDelegate()); 2574 TestGestureRecognizer* gesture_recognizer = 2575 new TestGestureRecognizer(); 2576 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 2577 TimedEvents tes; 2578 2579 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2580 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window())); 2581 2582 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now()); 2583 press.set_radius_x(40); 2584 DispatchEventUsingWindowDispatcher(&press); 2585 EXPECT_TRUE(delegate->tap_down()); 2586 EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(), 2587 delegate->bounding_box().ToString()); 2588 delegate->Reset(); 2589 2590 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now()); 2591 DispatchEventUsingWindowDispatcher(&press2); 2592 2593 // This new press should not generate a tap-down. 2594 EXPECT_FALSE(delegate->begin()); 2595 EXPECT_FALSE(delegate->tap_down()); 2596 EXPECT_FALSE(delegate->tap_cancel()); 2597 EXPECT_FALSE(delegate->scroll_begin()); 2598} 2599 2600TEST_F(GestureRecognizerTest, TwoFingerTap) { 2601 scoped_ptr<GestureEventConsumeDelegate> delegate( 2602 new GestureEventConsumeDelegate()); 2603 const int kWindowWidth = 123; 2604 const int kWindowHeight = 45; 2605 const int kTouchId1 = 2; 2606 const int kTouchId2 = 3; 2607 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2608 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2609 delegate.get(), -1234, bounds, root_window())); 2610 TimedEvents tes; 2611 2612 delegate->Reset(); 2613 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2614 kTouchId1, tes.Now()); 2615 DispatchEventUsingWindowDispatcher(&press1); 2616 EXPECT_FALSE(delegate->tap()); 2617 EXPECT_TRUE(delegate->tap_down()); 2618 EXPECT_FALSE(delegate->tap_cancel()); 2619 EXPECT_FALSE(delegate->scroll_begin()); 2620 EXPECT_FALSE(delegate->scroll_update()); 2621 EXPECT_FALSE(delegate->scroll_end()); 2622 EXPECT_FALSE(delegate->long_press()); 2623 EXPECT_FALSE(delegate->two_finger_tap()); 2624 2625 delegate->Reset(); 2626 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 2627 kTouchId2, tes.Now()); 2628 DispatchEventUsingWindowDispatcher(&press2); 2629 EXPECT_FALSE(delegate->tap()); 2630 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap. 2631 EXPECT_TRUE(delegate->tap_cancel()); 2632 EXPECT_FALSE(delegate->scroll_begin()); 2633 EXPECT_FALSE(delegate->scroll_update()); 2634 EXPECT_FALSE(delegate->scroll_end()); 2635 EXPECT_FALSE(delegate->long_press()); 2636 EXPECT_FALSE(delegate->two_finger_tap()); 2637 2638 // Little bit of touch move should not affect our state. 2639 delegate->Reset(); 2640 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202), 2641 kTouchId1, tes.Now()); 2642 DispatchEventUsingWindowDispatcher(&move1); 2643 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202), 2644 kTouchId2, tes.Now()); 2645 DispatchEventUsingWindowDispatcher(&move2); 2646 EXPECT_FALSE(delegate->tap()); 2647 EXPECT_FALSE(delegate->tap_down()); 2648 EXPECT_FALSE(delegate->tap_cancel()); 2649 EXPECT_FALSE(delegate->scroll_begin()); 2650 EXPECT_FALSE(delegate->scroll_update()); 2651 EXPECT_FALSE(delegate->scroll_end()); 2652 EXPECT_FALSE(delegate->long_press()); 2653 EXPECT_FALSE(delegate->two_finger_tap()); 2654 2655 // Make sure there is enough delay before the touch is released so that it is 2656 // recognized as a tap. 2657 delegate->Reset(); 2658 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2659 kTouchId1, tes.LeapForward(50)); 2660 2661 DispatchEventUsingWindowDispatcher(&release1); 2662 EXPECT_FALSE(delegate->tap()); 2663 EXPECT_FALSE(delegate->tap_down()); 2664 EXPECT_FALSE(delegate->tap_cancel()); 2665 EXPECT_TRUE(delegate->scroll_begin()); 2666 EXPECT_FALSE(delegate->scroll_update()); 2667 EXPECT_FALSE(delegate->scroll_end()); 2668 EXPECT_TRUE(delegate->two_finger_tap()); 2669 2670 // Lift second finger. 2671 // Make sure there is enough delay before the touch is released so that it is 2672 // recognized as a tap. 2673 delegate->Reset(); 2674 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201), 2675 kTouchId2, tes.LeapForward(50)); 2676 2677 DispatchEventUsingWindowDispatcher(&release2); 2678 EXPECT_FALSE(delegate->tap()); 2679 EXPECT_FALSE(delegate->tap_down()); 2680 EXPECT_FALSE(delegate->tap_cancel()); 2681 EXPECT_FALSE(delegate->scroll_begin()); 2682 EXPECT_FALSE(delegate->scroll_update()); 2683 EXPECT_FALSE(delegate->scroll_end()); 2684 EXPECT_TRUE(delegate->fling()); 2685 EXPECT_FALSE(delegate->two_finger_tap()); 2686} 2687 2688TEST_F(GestureRecognizerTest, TwoFingerTapExpired) { 2689 scoped_ptr<GestureEventConsumeDelegate> delegate( 2690 new GestureEventConsumeDelegate()); 2691 const int kWindowWidth = 123; 2692 const int kWindowHeight = 45; 2693 const int kTouchId1 = 2; 2694 const int kTouchId2 = 3; 2695 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2696 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2697 delegate.get(), -1234, bounds, root_window())); 2698 TimedEvents tes; 2699 2700 delegate->Reset(); 2701 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2702 kTouchId1, tes.Now()); 2703 DispatchEventUsingWindowDispatcher(&press1); 2704 2705 delegate->Reset(); 2706 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 2707 kTouchId2, tes.Now()); 2708 DispatchEventUsingWindowDispatcher(&press2); 2709 2710 // Send release event after sufficient delay so that two finger time expires. 2711 delegate->Reset(); 2712 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2713 kTouchId1, tes.LeapForward(1000)); 2714 2715 DispatchEventUsingWindowDispatcher(&release1); 2716 EXPECT_FALSE(delegate->two_finger_tap()); 2717 2718 // Lift second finger. 2719 // Make sure there is enough delay before the touch is released so that it is 2720 // recognized as a tap. 2721 delegate->Reset(); 2722 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201), 2723 kTouchId2, tes.LeapForward(50)); 2724 2725 DispatchEventUsingWindowDispatcher(&release2); 2726 EXPECT_FALSE(delegate->two_finger_tap()); 2727} 2728 2729TEST_F(GestureRecognizerTest, TwoFingerTapChangesToPinch) { 2730 scoped_ptr<GestureEventConsumeDelegate> delegate( 2731 new GestureEventConsumeDelegate()); 2732 const int kWindowWidth = 123; 2733 const int kWindowHeight = 45; 2734 const int kTouchId1 = 2; 2735 const int kTouchId2 = 3; 2736 TimedEvents tes; 2737 2738 // Test moving first finger 2739 { 2740 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2741 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2742 delegate.get(), -1234, bounds, root_window())); 2743 2744 delegate->Reset(); 2745 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2746 kTouchId1, tes.Now()); 2747 DispatchEventUsingWindowDispatcher(&press1); 2748 2749 delegate->Reset(); 2750 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 2751 kTouchId2, tes.Now()); 2752 DispatchEventUsingWindowDispatcher(&press2); 2753 2754 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get()); 2755 EXPECT_FALSE(delegate->two_finger_tap()); 2756 EXPECT_TRUE(delegate->pinch_begin()); 2757 2758 // Make sure there is enough delay before the touch is released so that it 2759 // is recognized as a tap. 2760 delegate->Reset(); 2761 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2762 kTouchId2, tes.LeapForward(50)); 2763 2764 DispatchEventUsingWindowDispatcher(&release); 2765 EXPECT_FALSE(delegate->two_finger_tap()); 2766 EXPECT_TRUE(delegate->pinch_end()); 2767 } 2768 2769 // Test moving second finger 2770 { 2771 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2772 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2773 delegate.get(), -1234, bounds, root_window())); 2774 2775 delegate->Reset(); 2776 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2777 kTouchId1, tes.Now()); 2778 DispatchEventUsingWindowDispatcher(&press1); 2779 2780 delegate->Reset(); 2781 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 2782 kTouchId2, tes.Now()); 2783 DispatchEventUsingWindowDispatcher(&press2); 2784 2785 tes.SendScrollEvent(event_processor(), 101, 230, kTouchId2, delegate.get()); 2786 EXPECT_FALSE(delegate->two_finger_tap()); 2787 EXPECT_TRUE(delegate->pinch_begin()); 2788 2789 // Make sure there is enough delay before the touch is released so that it 2790 // is recognized as a tap. 2791 delegate->Reset(); 2792 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2793 kTouchId1, tes.LeapForward(50)); 2794 2795 DispatchEventUsingWindowDispatcher(&release); 2796 EXPECT_FALSE(delegate->two_finger_tap()); 2797 EXPECT_TRUE(delegate->pinch_end()); 2798 } 2799} 2800 2801TEST_F(GestureRecognizerTest, NoTwoFingerTapWhenFirstFingerHasScrolled) { 2802 scoped_ptr<GestureEventConsumeDelegate> delegate( 2803 new GestureEventConsumeDelegate()); 2804 const int kWindowWidth = 123; 2805 const int kWindowHeight = 45; 2806 const int kTouchId1 = 2; 2807 const int kTouchId2 = 3; 2808 TimedEvents tes; 2809 2810 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2811 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2812 delegate.get(), -1234, bounds, root_window())); 2813 2814 delegate->Reset(); 2815 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2816 kTouchId1, tes.Now()); 2817 DispatchEventUsingWindowDispatcher(&press1); 2818 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get()); 2819 2820 delegate->Reset(); 2821 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 2822 kTouchId2, tes.Now()); 2823 DispatchEventUsingWindowDispatcher(&press2); 2824 2825 EXPECT_TRUE(delegate->pinch_begin()); 2826 2827 // Make sure there is enough delay before the touch is released so that it 2828 // is recognized as a tap. 2829 delegate->Reset(); 2830 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2831 kTouchId2, tes.LeapForward(50)); 2832 2833 DispatchEventUsingWindowDispatcher(&release); 2834 EXPECT_FALSE(delegate->two_finger_tap()); 2835 EXPECT_TRUE(delegate->pinch_end()); 2836} 2837 2838TEST_F(GestureRecognizerTest, MultiFingerSwipe) { 2839 scoped_ptr<GestureEventConsumeDelegate> delegate( 2840 new GestureEventConsumeDelegate()); 2841 const int kWindowWidth = 123; 2842 const int kWindowHeight = 45; 2843 2844 gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight); 2845 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2846 delegate.get(), -1234, bounds, root_window())); 2847 2848 const int kSteps = 15; 2849 const int kTouchPoints = 4; 2850 gfx::Point points[kTouchPoints] = { 2851 gfx::Point(10, 30), 2852 gfx::Point(30, 20), 2853 gfx::Point(50, 30), 2854 gfx::Point(80, 50) 2855 }; 2856 2857 aura::test::EventGenerator generator(root_window(), window.get()); 2858 2859 for (int count = 2; count <= kTouchPoints; ++count) { 2860 generator.GestureMultiFingerScroll(count, points, 15, kSteps, 0, -150); 2861 EXPECT_TRUE(delegate->swipe_up()); 2862 delegate->Reset(); 2863 2864 generator.GestureMultiFingerScroll(count, points, 15, kSteps, 0, 150); 2865 EXPECT_TRUE(delegate->swipe_down()); 2866 delegate->Reset(); 2867 2868 generator.GestureMultiFingerScroll(count, points, 15, kSteps, -150, 0); 2869 EXPECT_TRUE(delegate->swipe_left()); 2870 delegate->Reset(); 2871 2872 generator.GestureMultiFingerScroll(count, points, 15, kSteps, 150, 0); 2873 EXPECT_TRUE(delegate->swipe_right()); 2874 delegate->Reset(); 2875 } 2876} 2877 2878TEST_F(GestureRecognizerTest, TwoFingerTapCancelled) { 2879 scoped_ptr<GestureEventConsumeDelegate> delegate( 2880 new GestureEventConsumeDelegate()); 2881 const int kWindowWidth = 123; 2882 const int kWindowHeight = 45; 2883 const int kTouchId1 = 2; 2884 const int kTouchId2 = 3; 2885 TimedEvents tes; 2886 2887 // Test canceling first finger. 2888 { 2889 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2890 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2891 delegate.get(), -1234, bounds, root_window())); 2892 2893 delegate->Reset(); 2894 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2895 kTouchId1, tes.Now()); 2896 DispatchEventUsingWindowDispatcher(&press1); 2897 2898 delegate->Reset(); 2899 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 2900 kTouchId2, tes.Now()); 2901 DispatchEventUsingWindowDispatcher(&press2); 2902 2903 delegate->Reset(); 2904 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201), 2905 kTouchId1, tes.Now()); 2906 DispatchEventUsingWindowDispatcher(&cancel); 2907 EXPECT_FALSE(delegate->two_finger_tap()); 2908 2909 // Make sure there is enough delay before the touch is released so that it 2910 // is recognized as a tap. 2911 delegate->Reset(); 2912 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2913 kTouchId2, tes.LeapForward(50)); 2914 2915 DispatchEventUsingWindowDispatcher(&release); 2916 EXPECT_FALSE(delegate->two_finger_tap()); 2917 } 2918 2919 // Test canceling second finger 2920 { 2921 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2922 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2923 delegate.get(), -1234, bounds, root_window())); 2924 2925 delegate->Reset(); 2926 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2927 kTouchId1, tes.Now()); 2928 DispatchEventUsingWindowDispatcher(&press1); 2929 2930 delegate->Reset(); 2931 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 2932 kTouchId2, tes.Now()); 2933 DispatchEventUsingWindowDispatcher(&press2); 2934 2935 delegate->Reset(); 2936 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201), 2937 kTouchId2, tes.Now()); 2938 DispatchEventUsingWindowDispatcher(&cancel); 2939 EXPECT_FALSE(delegate->two_finger_tap()); 2940 2941 // Make sure there is enough delay before the touch is released so that it 2942 // is recognized as a tap. 2943 delegate->Reset(); 2944 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 2945 kTouchId1, tes.LeapForward(50)); 2946 2947 DispatchEventUsingWindowDispatcher(&release); 2948 EXPECT_FALSE(delegate->two_finger_tap()); 2949 } 2950} 2951 2952TEST_F(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) { 2953 scoped_ptr<GestureEventConsumeDelegate> delegate( 2954 new GestureEventConsumeDelegate()); 2955 const int kWindowWidth = 523; 2956 const int kWindowHeight = 45; 2957 const int kTouchId1 = 2; 2958 const int kTouchId2 = 3; 2959 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 2960 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 2961 delegate.get(), -1234, bounds, root_window())); 2962 TimedEvents tes; 2963 2964 delegate->Reset(); 2965 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 2966 kTouchId1, tes.Now()); 2967 DispatchEventUsingWindowDispatcher(&press1); 2968 EXPECT_FALSE(delegate->tap()); 2969 EXPECT_TRUE(delegate->tap_down()); 2970 EXPECT_FALSE(delegate->tap_cancel()); 2971 EXPECT_FALSE(delegate->scroll_begin()); 2972 EXPECT_FALSE(delegate->scroll_update()); 2973 EXPECT_FALSE(delegate->scroll_end()); 2974 EXPECT_FALSE(delegate->long_press()); 2975 EXPECT_FALSE(delegate->two_finger_tap()); 2976 2977 delegate->Reset(); 2978 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201), 2979 kTouchId2, tes.Now()); 2980 DispatchEventUsingWindowDispatcher(&press2); 2981 EXPECT_FALSE(delegate->tap()); 2982 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap. 2983 EXPECT_TRUE(delegate->tap_cancel()); 2984 EXPECT_FALSE(delegate->scroll_begin()); 2985 EXPECT_FALSE(delegate->scroll_update()); 2986 EXPECT_FALSE(delegate->scroll_end()); 2987 EXPECT_FALSE(delegate->long_press()); 2988 EXPECT_FALSE(delegate->two_finger_tap()); 2989 EXPECT_FALSE(delegate->pinch_begin()); 2990 2991 delegate->Reset(); 2992 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301), 2993 kTouchId2, tes.Now()); 2994 DispatchEventUsingWindowDispatcher(&move2); 2995 EXPECT_FALSE(delegate->tap()); 2996 EXPECT_FALSE(delegate->tap_down()); 2997 EXPECT_FALSE(delegate->tap_cancel()); 2998 // Pinch & Scroll only when there is enough movement. 2999 EXPECT_TRUE(delegate->scroll_begin()); 3000 EXPECT_FALSE(delegate->scroll_update()); 3001 EXPECT_FALSE(delegate->scroll_end()); 3002 EXPECT_FALSE(delegate->long_press()); 3003 EXPECT_FALSE(delegate->two_finger_tap()); 3004 EXPECT_TRUE(delegate->pinch_begin()); 3005} 3006 3007// Verifies if a window is the target of multiple touch-ids and we hide the 3008// window everything is cleaned up correctly. 3009TEST_F(GestureRecognizerTest, FlushAllOnHide) { 3010 scoped_ptr<GestureEventConsumeDelegate> delegate( 3011 new GestureEventConsumeDelegate()); 3012 gfx::Rect bounds(0, 0, 200, 200); 3013 scoped_ptr<aura::Window> window( 3014 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window())); 3015 const int kTouchId1 = 8; 3016 const int kTouchId2 = 2; 3017 TimedEvents tes; 3018 3019 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 3020 kTouchId1, tes.Now()); 3021 DispatchEventUsingWindowDispatcher(&press1); 3022 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20), 3023 kTouchId2, tes.Now()); 3024 DispatchEventUsingWindowDispatcher(&press2); 3025 window->Hide(); 3026 EXPECT_EQ(NULL, 3027 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1)); 3028 EXPECT_EQ(NULL, 3029 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2)); 3030} 3031 3032TEST_F(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) { 3033 scoped_ptr<QueueTouchEventDelegate> delegate( 3034 new QueueTouchEventDelegate(host()->dispatcher())); 3035 const int kTouchId = 2; 3036 gfx::Rect bounds(100, 200, 100, 100); 3037 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3038 delegate.get(), -1234, bounds, root_window())); 3039 delegate->set_window(window.get()); 3040 TimedEvents tes; 3041 3042 TimerTestGestureRecognizer* gesture_recognizer = 3043 new TimerTestGestureRecognizer(); 3044 TimerTestGestureSequence* gesture_sequence = 3045 static_cast<TimerTestGestureSequence*>( 3046 gesture_recognizer->GetGestureSequenceForTesting(window.get())); 3047 3048 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 3049 3050 delegate->Reset(); 3051 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3052 kTouchId, tes.Now()); 3053 DispatchEventUsingWindowDispatcher(&press); 3054 // Scroll around, to cancel the long press 3055 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get()); 3056 3057 delegate->Reset(); 3058 delegate->ReceivedAck(); 3059 EXPECT_TRUE(delegate->tap_down()); 3060 EXPECT_TRUE(gesture_sequence->IsTimerRunning()); 3061 3062 delegate->Reset(); 3063 delegate->ReceivedAckPreventDefaulted(); 3064 EXPECT_FALSE(gesture_sequence->IsTimerRunning()); 3065 gesture_sequence->ForceTimeout(); 3066 EXPECT_FALSE(delegate->long_press()); 3067} 3068 3069// Same as GestureEventConsumeDelegate, but consumes all the touch-move events. 3070class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate { 3071 public: 3072 ConsumesTouchMovesDelegate() : consume_touch_move_(true) {} 3073 virtual ~ConsumesTouchMovesDelegate() {} 3074 3075 void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; } 3076 3077 private: 3078 virtual void OnTouchEvent(ui::TouchEvent* touch) OVERRIDE { 3079 if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED) 3080 touch->SetHandled(); 3081 else 3082 GestureEventConsumeDelegate::OnTouchEvent(touch); 3083 } 3084 3085 bool consume_touch_move_; 3086 3087 DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate); 3088}; 3089 3090// Same as GestureEventScroll, but tests that the behavior is the same 3091// even if all the touch-move events are consumed. 3092TEST_F(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) { 3093 scoped_ptr<ConsumesTouchMovesDelegate> delegate( 3094 new ConsumesTouchMovesDelegate()); 3095 const int kWindowWidth = 123; 3096 const int kWindowHeight = 45; 3097 const int kTouchId = 5; 3098 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3099 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3100 delegate.get(), -1234, bounds, root_window())); 3101 TimedEvents tes; 3102 3103 delegate->Reset(); 3104 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3105 kTouchId, tes.Now()); 3106 DispatchEventUsingWindowDispatcher(&press); 3107 EXPECT_FALSE(delegate->tap()); 3108 EXPECT_TRUE(delegate->tap_down()); 3109 EXPECT_FALSE(delegate->tap_cancel()); 3110 EXPECT_TRUE(delegate->begin()); 3111 EXPECT_FALSE(delegate->scroll_begin()); 3112 EXPECT_FALSE(delegate->scroll_update()); 3113 EXPECT_FALSE(delegate->scroll_end()); 3114 3115 // Move the touch-point enough so that it would normally be considered a 3116 // scroll. But since the touch-moves will be consumed, the scroll should not 3117 // start. 3118 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get()); 3119 EXPECT_FALSE(delegate->tap()); 3120 EXPECT_FALSE(delegate->tap_down()); 3121 EXPECT_TRUE(delegate->tap_cancel()); 3122 EXPECT_FALSE(delegate->begin()); 3123 EXPECT_FALSE(delegate->scroll_begin()); 3124 EXPECT_FALSE(delegate->scroll_update()); 3125 EXPECT_FALSE(delegate->scroll_end()); 3126 3127 // Release the touch back at the start point. This should end without causing 3128 // a tap. 3129 delegate->Reset(); 3130 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230), 3131 kTouchId, tes.LeapForward(50)); 3132 DispatchEventUsingWindowDispatcher(&release); 3133 EXPECT_FALSE(delegate->tap()); 3134 EXPECT_FALSE(delegate->tap_down()); 3135 EXPECT_FALSE(delegate->tap_cancel()); 3136 EXPECT_FALSE(delegate->begin()); 3137 EXPECT_TRUE(delegate->end()); 3138 EXPECT_FALSE(delegate->scroll_begin()); 3139 EXPECT_FALSE(delegate->scroll_update()); 3140 EXPECT_FALSE(delegate->scroll_end()); 3141} 3142 3143// Tests the behavior of 2F scroll when all the touch-move events are consumed. 3144TEST_F(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) { 3145 scoped_ptr<ConsumesTouchMovesDelegate> delegate( 3146 new ConsumesTouchMovesDelegate()); 3147 const int kWindowWidth = 123; 3148 const int kWindowHeight = 100; 3149 const int kTouchId1 = 2; 3150 const int kTouchId2 = 3; 3151 TimedEvents tes; 3152 3153 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3154 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3155 delegate.get(), -1234, bounds, root_window())); 3156 3157 delegate->Reset(); 3158 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3159 kTouchId1, tes.Now()); 3160 DispatchEventUsingWindowDispatcher(&press1); 3161 tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get()); 3162 3163 // First finger touches down and moves. 3164 EXPECT_FALSE(delegate->tap()); 3165 EXPECT_FALSE(delegate->scroll_begin()); 3166 EXPECT_FALSE(delegate->scroll_update()); 3167 EXPECT_FALSE(delegate->scroll_end()); 3168 3169 delegate->Reset(); 3170 // Second finger touches down and moves. 3171 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201), 3172 kTouchId2, tes.LeapForward(50)); 3173 DispatchEventUsingWindowDispatcher(&press2); 3174 tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get()); 3175 3176 // PinchBegin & ScrollBegin were not sent if the touch-move events were 3177 // consumed. 3178 EXPECT_FALSE(delegate->pinch_begin()); 3179 EXPECT_FALSE(delegate->scroll_begin()); 3180 3181 EXPECT_FALSE(delegate->tap()); 3182 EXPECT_FALSE(delegate->two_finger_tap()); 3183 3184 // Should be no PinchUpdate & ScrollUpdate. 3185 EXPECT_FALSE(delegate->pinch_update()); 3186 EXPECT_FALSE(delegate->pinch_end()); 3187 EXPECT_FALSE(delegate->scroll_update()); 3188 EXPECT_FALSE(delegate->scroll_end()); 3189 3190 delegate->Reset(); 3191 // Moves First finger again, no PinchUpdate & ScrollUpdate. 3192 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get()); 3193 3194 EXPECT_FALSE(delegate->pinch_update()); 3195 EXPECT_FALSE(delegate->pinch_end()); 3196 EXPECT_FALSE(delegate->scroll_update()); 3197 EXPECT_FALSE(delegate->scroll_end()); 3198 3199 // Stops consuming touch-move. 3200 delegate->set_consume_touch_move(false); 3201 3202 delegate->Reset(); 3203 // Making a pinch gesture. 3204 tes.SendScrollEvent(event_processor(), 161, 251, kTouchId1, delegate.get()); 3205 // If touch moves are ever consumed, we should not see PinchBegin/Update 3206 // even touch moves become not consumed. 3207 EXPECT_FALSE(delegate->scroll_begin()); 3208 EXPECT_FALSE(delegate->scroll_update()); 3209 EXPECT_FALSE(delegate->scroll_end()); 3210 3211 EXPECT_FALSE(delegate->pinch_begin()); 3212 EXPECT_FALSE(delegate->pinch_update()); 3213 EXPECT_FALSE(delegate->pinch_end()); 3214 3215 delegate->Reset(); 3216 tes.SendScrollEvent(event_processor(), 161, 241, kTouchId2, delegate.get()); 3217 EXPECT_FALSE(delegate->scroll_begin()); 3218 EXPECT_FALSE(delegate->scroll_update()); 3219 EXPECT_FALSE(delegate->scroll_end()); 3220 3221 EXPECT_FALSE(delegate->pinch_begin()); 3222 EXPECT_FALSE(delegate->pinch_update()); 3223 EXPECT_FALSE(delegate->pinch_end()); 3224 3225 delegate->Reset(); 3226 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3227 kTouchId1, tes.Now()); 3228 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201), 3229 kTouchId2, tes.Now()); 3230 DispatchEventUsingWindowDispatcher(&release1); 3231 DispatchEventUsingWindowDispatcher(&release2); 3232 3233 EXPECT_FALSE(delegate->tap()); 3234 // Touch release is not consumed, so we still see two finger tap. 3235 EXPECT_TRUE(delegate->two_finger_tap()); 3236 3237 // Should not see PinchEnd. 3238 EXPECT_TRUE(delegate->scroll_begin()); 3239 EXPECT_FALSE(delegate->scroll_update()); 3240 EXPECT_FALSE(delegate->scroll_end()); 3241 EXPECT_TRUE(delegate->fling()); 3242 3243 EXPECT_FALSE(delegate->pinch_begin()); 3244 EXPECT_FALSE(delegate->pinch_update()); 3245 EXPECT_FALSE(delegate->pinch_end()); 3246} 3247 3248// Like as GestureEventTouchMoveConsumed but tests the different behavior 3249// depending on whether the events were consumed before or after the scroll 3250// started. 3251TEST_F(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) { 3252 scoped_ptr<ConsumesTouchMovesDelegate> delegate( 3253 new ConsumesTouchMovesDelegate()); 3254 const int kWindowWidth = 123; 3255 const int kWindowHeight = 45; 3256 const int kTouchId = 5; 3257 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3258 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3259 delegate.get(), -1234, bounds, root_window())); 3260 TimedEvents tes; 3261 3262 delegate->Reset(); 3263 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3264 kTouchId, tes.Now()); 3265 DispatchEventUsingWindowDispatcher(&press); 3266 EXPECT_FALSE(delegate->tap()); 3267 EXPECT_TRUE(delegate->tap_down()); 3268 EXPECT_FALSE(delegate->tap_cancel()); 3269 EXPECT_TRUE(delegate->begin()); 3270 EXPECT_FALSE(delegate->scroll_begin()); 3271 EXPECT_FALSE(delegate->scroll_update()); 3272 EXPECT_FALSE(delegate->scroll_end()); 3273 3274 // Move the touch-point enough so that it would normally be considered a 3275 // scroll. But since the touch-moves will be consumed, the scroll should not 3276 // start. 3277 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get()); 3278 EXPECT_FALSE(delegate->tap()); 3279 EXPECT_FALSE(delegate->tap_down()); 3280 EXPECT_TRUE(delegate->tap_cancel()); 3281 EXPECT_FALSE(delegate->begin()); 3282 EXPECT_FALSE(delegate->scroll_begin()); 3283 EXPECT_FALSE(delegate->scroll_update()); 3284 EXPECT_FALSE(delegate->scroll_end()); 3285 3286 // Now, stop consuming touch-move events, and move the touch-point again. 3287 delegate->set_consume_touch_move(false); 3288 tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get()); 3289 EXPECT_FALSE(delegate->tap()); 3290 EXPECT_FALSE(delegate->tap_down()); 3291 EXPECT_FALSE(delegate->tap_cancel()); 3292 EXPECT_FALSE(delegate->begin()); 3293 EXPECT_FALSE(delegate->scroll_begin()); 3294 EXPECT_FALSE(delegate->scroll_update()); 3295 EXPECT_FALSE(delegate->scroll_end()); 3296 // No scroll has occurred, because an early touch move was consumed. 3297 EXPECT_EQ(0, delegate->scroll_x()); 3298 EXPECT_EQ(0, delegate->scroll_y()); 3299 EXPECT_EQ(gfx::Point(0, 0).ToString(), 3300 delegate->scroll_begin_position().ToString()); 3301 3302 // Start consuming touch-move events again. 3303 delegate->set_consume_touch_move(true); 3304 3305 // Move some more to generate a few more scroll updates. 3306 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get()); 3307 EXPECT_FALSE(delegate->tap()); 3308 EXPECT_FALSE(delegate->tap_down()); 3309 EXPECT_FALSE(delegate->tap_cancel()); 3310 EXPECT_FALSE(delegate->begin()); 3311 EXPECT_FALSE(delegate->scroll_begin()); 3312 EXPECT_FALSE(delegate->scroll_update()); 3313 EXPECT_FALSE(delegate->scroll_end()); 3314 EXPECT_EQ(0, delegate->scroll_x()); 3315 EXPECT_EQ(0, delegate->scroll_y()); 3316 3317 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get()); 3318 EXPECT_FALSE(delegate->tap()); 3319 EXPECT_FALSE(delegate->tap_down()); 3320 EXPECT_FALSE(delegate->tap_cancel()); 3321 EXPECT_FALSE(delegate->begin()); 3322 EXPECT_FALSE(delegate->scroll_begin()); 3323 EXPECT_FALSE(delegate->scroll_update()); 3324 EXPECT_FALSE(delegate->scroll_end()); 3325 EXPECT_EQ(0, delegate->scroll_x()); 3326 EXPECT_EQ(0, delegate->scroll_y()); 3327 3328 // Release the touch. 3329 delegate->Reset(); 3330 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3331 kTouchId, tes.LeapForward(50)); 3332 DispatchEventUsingWindowDispatcher(&release); 3333 EXPECT_FALSE(delegate->tap()); 3334 EXPECT_FALSE(delegate->tap_down()); 3335 EXPECT_FALSE(delegate->tap_cancel()); 3336 EXPECT_FALSE(delegate->begin()); 3337 EXPECT_TRUE(delegate->end()); 3338 EXPECT_FALSE(delegate->scroll_begin()); 3339 EXPECT_FALSE(delegate->scroll_update()); 3340 EXPECT_FALSE(delegate->scroll_end()); 3341 EXPECT_FALSE(delegate->fling()); 3342} 3343 3344// Check that appropriate touch events generate double tap gesture events. 3345TEST_F(GestureRecognizerTest, GestureEventDoubleTap) { 3346 scoped_ptr<GestureEventConsumeDelegate> delegate( 3347 new GestureEventConsumeDelegate()); 3348 const int kWindowWidth = 123; 3349 const int kWindowHeight = 45; 3350 const int kTouchId = 2; 3351 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3352 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3353 delegate.get(), -1234, bounds, root_window())); 3354 TimedEvents tes; 3355 3356 // First tap (tested in GestureEventTap) 3357 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201), 3358 kTouchId, tes.Now()); 3359 DispatchEventUsingWindowDispatcher(&press1); 3360 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201), 3361 kTouchId, tes.LeapForward(50)); 3362 DispatchEventUsingWindowDispatcher(&release1); 3363 delegate->Reset(); 3364 3365 // Second tap 3366 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203), 3367 kTouchId, tes.LeapForward(200)); 3368 DispatchEventUsingWindowDispatcher(&press2); 3369 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206), 3370 kTouchId, tes.LeapForward(50)); 3371 DispatchEventUsingWindowDispatcher(&release2); 3372 3373 EXPECT_TRUE(delegate->tap()); 3374 EXPECT_TRUE(delegate->tap_down()); 3375 EXPECT_FALSE(delegate->tap_cancel()); 3376 EXPECT_TRUE(delegate->begin()); 3377 EXPECT_TRUE(delegate->end()); 3378 EXPECT_FALSE(delegate->scroll_begin()); 3379 EXPECT_FALSE(delegate->scroll_update()); 3380 EXPECT_FALSE(delegate->scroll_end()); 3381 3382 EXPECT_EQ(2, delegate->tap_count()); 3383} 3384 3385// Check that appropriate touch events generate triple tap gesture events. 3386TEST_F(GestureRecognizerTest, GestureEventTripleTap) { 3387 scoped_ptr<GestureEventConsumeDelegate> delegate( 3388 new GestureEventConsumeDelegate()); 3389 const int kWindowWidth = 123; 3390 const int kWindowHeight = 45; 3391 const int kTouchId = 2; 3392 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3393 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3394 delegate.get(), -1234, bounds, root_window())); 3395 TimedEvents tes; 3396 3397 // First tap (tested in GestureEventTap) 3398 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201), 3399 kTouchId, tes.Now()); 3400 DispatchEventUsingWindowDispatcher(&press1); 3401 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201), 3402 kTouchId, tes.LeapForward(50)); 3403 DispatchEventUsingWindowDispatcher(&release1); 3404 3405 EXPECT_EQ(1, delegate->tap_count()); 3406 delegate->Reset(); 3407 3408 // Second tap (tested in GestureEventDoubleTap) 3409 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203), 3410 kTouchId, tes.LeapForward(200)); 3411 DispatchEventUsingWindowDispatcher(&press2); 3412 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206), 3413 kTouchId, tes.LeapForward(50)); 3414 DispatchEventUsingWindowDispatcher(&release2); 3415 3416 EXPECT_EQ(2, delegate->tap_count()); 3417 delegate->Reset(); 3418 3419 // Third tap 3420 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206), 3421 kTouchId, tes.LeapForward(200)); 3422 DispatchEventUsingWindowDispatcher(&press3); 3423 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206), 3424 kTouchId, tes.LeapForward(50)); 3425 DispatchEventUsingWindowDispatcher(&release3); 3426 3427 3428 EXPECT_TRUE(delegate->tap()); 3429 EXPECT_TRUE(delegate->tap_down()); 3430 EXPECT_FALSE(delegate->tap_cancel()); 3431 EXPECT_TRUE(delegate->begin()); 3432 EXPECT_TRUE(delegate->end()); 3433 EXPECT_FALSE(delegate->scroll_begin()); 3434 EXPECT_FALSE(delegate->scroll_update()); 3435 EXPECT_FALSE(delegate->scroll_end()); 3436 3437 EXPECT_EQ(3, delegate->tap_count()); 3438} 3439 3440// Check that we don't get a double tap when the two taps are far apart. 3441TEST_F(GestureRecognizerTest, TwoTapsFarApart) { 3442 scoped_ptr<GestureEventConsumeDelegate> delegate( 3443 new GestureEventConsumeDelegate()); 3444 const int kWindowWidth = 123; 3445 const int kWindowHeight = 45; 3446 const int kTouchId = 2; 3447 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3448 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3449 delegate.get(), -1234, bounds, root_window())); 3450 TimedEvents tes; 3451 3452 // First tap (tested in GestureEventTap) 3453 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3454 kTouchId, tes.Now()); 3455 DispatchEventUsingWindowDispatcher(&press1); 3456 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3457 kTouchId, tes.LeapForward(50)); 3458 DispatchEventUsingWindowDispatcher(&release1); 3459 delegate->Reset(); 3460 3461 // Second tap, close in time but far in distance 3462 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), 3463 kTouchId, tes.LeapForward(200)); 3464 DispatchEventUsingWindowDispatcher(&press2); 3465 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201), 3466 kTouchId, tes.LeapForward(50)); 3467 DispatchEventUsingWindowDispatcher(&release2); 3468 3469 EXPECT_TRUE(delegate->tap()); 3470 EXPECT_TRUE(delegate->tap_down()); 3471 EXPECT_FALSE(delegate->tap_cancel()); 3472 EXPECT_TRUE(delegate->begin()); 3473 EXPECT_TRUE(delegate->end()); 3474 EXPECT_FALSE(delegate->scroll_begin()); 3475 EXPECT_FALSE(delegate->scroll_update()); 3476 EXPECT_FALSE(delegate->scroll_end()); 3477 3478 EXPECT_EQ(1, delegate->tap_count()); 3479} 3480 3481// Check that we don't get a double tap when the two taps have a long enough 3482// delay in between. 3483TEST_F(GestureRecognizerTest, TwoTapsWithDelayBetween) { 3484 scoped_ptr<GestureEventConsumeDelegate> delegate( 3485 new GestureEventConsumeDelegate()); 3486 const int kWindowWidth = 123; 3487 const int kWindowHeight = 45; 3488 const int kTouchId = 2; 3489 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3490 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3491 delegate.get(), -1234, bounds, root_window())); 3492 TimedEvents tes; 3493 3494 // First tap (tested in GestureEventTap) 3495 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3496 kTouchId, tes.Now()); 3497 DispatchEventUsingWindowDispatcher(&press1); 3498 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3499 kTouchId, tes.LeapForward(50)); 3500 DispatchEventUsingWindowDispatcher(&release1); 3501 delegate->Reset(); 3502 3503 // Second tap, close in distance but after some delay 3504 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3505 kTouchId, tes.LeapForward(2000)); 3506 DispatchEventUsingWindowDispatcher(&press2); 3507 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3508 kTouchId, tes.LeapForward(50)); 3509 DispatchEventUsingWindowDispatcher(&release2); 3510 3511 EXPECT_TRUE(delegate->tap()); 3512 EXPECT_TRUE(delegate->tap_down()); 3513 EXPECT_FALSE(delegate->tap_cancel()); 3514 EXPECT_TRUE(delegate->begin()); 3515 EXPECT_TRUE(delegate->end()); 3516 EXPECT_FALSE(delegate->scroll_begin()); 3517 EXPECT_FALSE(delegate->scroll_update()); 3518 EXPECT_FALSE(delegate->scroll_end()); 3519 3520 EXPECT_EQ(1, delegate->tap_count()); 3521} 3522 3523// Checks that if the bounding-box of a gesture changes because of change in 3524// radius of a touch-point, and not because of change in position, then there 3525// are not gesture events from that. 3526TEST_F(GestureRecognizerTest, BoundingBoxRadiusChange) { 3527 scoped_ptr<GestureEventConsumeDelegate> delegate( 3528 new GestureEventConsumeDelegate()); 3529 const int kWindowWidth = 234; 3530 const int kWindowHeight = 345; 3531 const int kTouchId = 5, kTouchId2 = 7; 3532 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3533 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3534 delegate.get(), -1234, bounds, root_window())); 3535 TimedEvents tes; 3536 3537 ui::TouchEvent press1( 3538 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now()); 3539 DispatchEventUsingWindowDispatcher(&press1); 3540 EXPECT_TRUE(delegate->bounding_box().IsEmpty()); 3541 3542 delegate->Reset(); 3543 3544 ui::TouchEvent press2( 3545 ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2, 3546 tes.LeapForward(400)); 3547 press2.set_radius_x(5); 3548 DispatchEventUsingWindowDispatcher(&press2); 3549 EXPECT_FALSE(delegate->pinch_begin()); 3550 EXPECT_EQ(gfx::Rect(101, 201, 100, 0).ToString(), 3551 delegate->bounding_box().ToString()); 3552 3553 delegate->Reset(); 3554 3555 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(141, 201), kTouchId, 3556 tes.LeapForward(40)); 3557 DispatchEventUsingWindowDispatcher(&move1); 3558 EXPECT_TRUE(delegate->pinch_begin()); 3559 EXPECT_EQ(gfx::Rect(141, 201, 60, 0).ToString(), 3560 delegate->bounding_box().ToString()); 3561 3562 delegate->Reset(); 3563 3564 // The position doesn't move, but the radius changes. 3565 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 201), kTouchId, 3566 tes.LeapForward(40)); 3567 move2.set_radius_x(50); 3568 move2.set_radius_y(60); 3569 DispatchEventUsingWindowDispatcher(&move2); 3570 EXPECT_FALSE(delegate->tap()); 3571 EXPECT_FALSE(delegate->tap_cancel()); 3572 EXPECT_FALSE(delegate->scroll_update()); 3573 EXPECT_FALSE(delegate->pinch_update()); 3574 3575 delegate->Reset(); 3576} 3577 3578// Checks that slow scrolls deliver the correct deltas. 3579// In particular, fix for http;//crbug.com/150573. 3580TEST_F(GestureRecognizerTest, NoDriftInScroll) { 3581 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3); 3582 ui::GestureConfiguration::set_min_scroll_delta_squared(9); 3583 scoped_ptr<GestureEventConsumeDelegate> delegate( 3584 new GestureEventConsumeDelegate()); 3585 const int kWindowWidth = 234; 3586 const int kWindowHeight = 345; 3587 const int kTouchId = 5; 3588 TimedEvents tes; 3589 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3590 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3591 delegate.get(), -1234, bounds, root_window())); 3592 3593 ui::TouchEvent press1( 3594 ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now()); 3595 DispatchEventUsingWindowDispatcher(&press1); 3596 EXPECT_TRUE(delegate->begin()); 3597 3598 delegate->Reset(); 3599 3600 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId, 3601 tes.LeapForward(40)); 3602 DispatchEventUsingWindowDispatcher(&move1); 3603 EXPECT_FALSE(delegate->scroll_begin()); 3604 3605 delegate->Reset(); 3606 3607 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId, 3608 tes.LeapForward(40)); 3609 DispatchEventUsingWindowDispatcher(&move2); 3610 EXPECT_TRUE(delegate->tap_cancel()); 3611 EXPECT_TRUE(delegate->scroll_begin()); 3612 EXPECT_TRUE(delegate->scroll_update()); 3613 // 3 px consumed by touch slop region. 3614 EXPECT_EQ(-1, delegate->scroll_y()); 3615 EXPECT_EQ(-4, delegate->scroll_y_hint()); 3616 3617 delegate->Reset(); 3618 3619 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId, 3620 tes.LeapForward(40)); 3621 DispatchEventUsingWindowDispatcher(&move3); 3622 EXPECT_FALSE(delegate->scroll_update()); 3623 3624 delegate->Reset(); 3625 3626 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId, 3627 tes.LeapForward(40)); 3628 DispatchEventUsingWindowDispatcher(&move4); 3629 EXPECT_TRUE(delegate->scroll_update()); 3630 EXPECT_EQ(-1, delegate->scroll_y()); 3631 3632 delegate->Reset(); 3633} 3634 3635// Ensure that move events which are preventDefaulted will cause a tap 3636// cancel gesture event to be fired if the move would normally cause a 3637// scroll. See bug http://crbug.com/146397. 3638TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) { 3639 scoped_ptr<ConsumesTouchMovesDelegate> delegate( 3640 new ConsumesTouchMovesDelegate()); 3641 const int kTouchId = 5; 3642 gfx::Rect bounds(100, 200, 123, 45); 3643 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3644 delegate.get(), -1234, bounds, root_window())); 3645 TimedEvents tes; 3646 3647 delegate->Reset(); 3648 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3649 kTouchId, tes.Now()); 3650 3651 delegate->set_consume_touch_move(false); 3652 DispatchEventUsingWindowDispatcher(&press); 3653 delegate->set_consume_touch_move(true); 3654 delegate->Reset(); 3655 // Move the touch-point enough so that it would normally be considered a 3656 // scroll. But since the touch-moves will be consumed, the scroll should not 3657 // start. 3658 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get()); 3659 EXPECT_FALSE(delegate->tap()); 3660 EXPECT_FALSE(delegate->tap_down()); 3661 EXPECT_TRUE(delegate->tap_cancel()); 3662 EXPECT_FALSE(delegate->begin()); 3663 EXPECT_FALSE(delegate->scroll_begin()); 3664 EXPECT_FALSE(delegate->scroll_update()); 3665 EXPECT_FALSE(delegate->scroll_end()); 3666} 3667 3668TEST_F(GestureRecognizerTest, 3669 TransferEventDispatchesTouchCancel) { 3670 scoped_ptr<GestureEventConsumeDelegate> delegate( 3671 new GestureEventConsumeDelegate()); 3672 TimedEvents tes; 3673 const int kWindowWidth = 800; 3674 const int kWindowHeight = 600; 3675 const int kTouchId = 2; 3676 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); 3677 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3678 delegate.get(), -1234, bounds, root_window())); 3679 scoped_ptr<RemoveOnTouchCancelHandler> 3680 handler(new RemoveOnTouchCancelHandler()); 3681 window->AddPreTargetHandler(handler.get()); 3682 3683 // Start a gesture sequence on |window|. Then transfer the events to NULL. 3684 // Make sure |window| receives a touch-cancel event. 3685 delegate->Reset(); 3686 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3687 kTouchId, tes.Now()); 3688 ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 1, tes.Now()); 3689 DispatchEventUsingWindowDispatcher(&press); 3690 DispatchEventUsingWindowDispatcher(&p2); 3691 EXPECT_FALSE(delegate->tap()); 3692 EXPECT_TRUE(delegate->tap_down()); 3693 EXPECT_TRUE(delegate->tap_cancel()); 3694 EXPECT_TRUE(delegate->begin()); 3695 EXPECT_EQ(2, handler->touch_pressed_count()); 3696 delegate->Reset(); 3697 handler->Reset(); 3698 3699 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get(); 3700 EXPECT_EQ(window.get(), 3701 gesture_recognizer->GetTouchLockedTarget(press)); 3702 gesture_recognizer->TransferEventsTo(window.get(), NULL); 3703 EXPECT_EQ(NULL, 3704 gesture_recognizer->GetTouchLockedTarget(press)); 3705 // The event-handler removes |window| from its parent on the first 3706 // touch-cancel event, so it won't receive the second touch-cancel event. 3707 EXPECT_EQ(1, handler->touch_cancelled_count()); 3708} 3709 3710// Check that appropriate touch events generate show press events 3711TEST_F(GestureRecognizerTest, GestureEventShowPress) { 3712 scoped_ptr<GestureEventConsumeDelegate> delegate( 3713 new GestureEventConsumeDelegate()); 3714 TimedEvents tes; 3715 const int kWindowWidth = 123; 3716 const int kWindowHeight = 45; 3717 const int kTouchId = 2; 3718 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3719 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3720 delegate.get(), -1234, bounds, root_window())); 3721 3722 delegate->Reset(); 3723 3724 TimerTestGestureRecognizer* gesture_recognizer = 3725 new TimerTestGestureRecognizer(); 3726 3727 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 3728 3729 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3730 kTouchId, tes.Now()); 3731 DispatchEventUsingWindowDispatcher(&press1); 3732 EXPECT_TRUE(delegate->tap_down()); 3733 EXPECT_TRUE(delegate->begin()); 3734 EXPECT_FALSE(delegate->tap_cancel()); 3735 3736 // We haven't pressed long enough for a show press to occur 3737 EXPECT_FALSE(delegate->show_press()); 3738 3739 // Wait until the timer runs out 3740 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS); 3741 EXPECT_TRUE(delegate->show_press()); 3742 EXPECT_FALSE(delegate->tap_cancel()); 3743 3744 delegate->Reset(); 3745 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3746 kTouchId, tes.Now()); 3747 DispatchEventUsingWindowDispatcher(&release1); 3748 EXPECT_FALSE(delegate->long_press()); 3749 3750 // Note the tap down isn't cancelled until the release 3751 EXPECT_TRUE(delegate->tap_cancel()); 3752} 3753 3754// Check that scrolling cancels a show press 3755TEST_F(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) { 3756 scoped_ptr<GestureEventConsumeDelegate> delegate( 3757 new GestureEventConsumeDelegate()); 3758 TimedEvents tes; 3759 const int kWindowWidth = 123; 3760 const int kWindowHeight = 45; 3761 const int kTouchId = 6; 3762 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3763 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3764 delegate.get(), -1234, bounds, root_window())); 3765 3766 delegate->Reset(); 3767 3768 TimerTestGestureRecognizer* gesture_recognizer = 3769 new TimerTestGestureRecognizer(); 3770 3771 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 3772 3773 TimerTestGestureSequence* gesture_sequence = 3774 static_cast<TimerTestGestureSequence*>( 3775 gesture_recognizer->GetGestureSequenceForTesting(window.get())); 3776 3777 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3778 kTouchId, tes.Now()); 3779 DispatchEventUsingWindowDispatcher(&press1); 3780 EXPECT_TRUE(delegate->tap_down()); 3781 3782 // We haven't pressed long enough for a show press to occur 3783 EXPECT_FALSE(delegate->show_press()); 3784 EXPECT_FALSE(delegate->tap_cancel()); 3785 3786 // Scroll around, to cancel the show press 3787 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get()); 3788 // Wait until the timer runs out 3789 gesture_sequence->ForceTimeout(); 3790 EXPECT_FALSE(delegate->show_press()); 3791 EXPECT_TRUE(delegate->tap_cancel()); 3792 3793 delegate->Reset(); 3794 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3795 kTouchId, tes.LeapForward(10)); 3796 DispatchEventUsingWindowDispatcher(&release1); 3797 EXPECT_FALSE(delegate->show_press()); 3798 EXPECT_FALSE(delegate->tap_cancel()); 3799} 3800 3801// Test that show press events are sent immediately on tap 3802TEST_F(GestureRecognizerTest, GestureEventShowPressSentOnTap) { 3803 scoped_ptr<GestureEventConsumeDelegate> delegate( 3804 new GestureEventConsumeDelegate()); 3805 TimedEvents tes; 3806 const int kWindowWidth = 123; 3807 const int kWindowHeight = 45; 3808 const int kTouchId = 6; 3809 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3810 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3811 delegate.get(), -1234, bounds, root_window())); 3812 3813 delegate->Reset(); 3814 3815 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3816 kTouchId, tes.Now()); 3817 DispatchEventUsingWindowDispatcher(&press1); 3818 EXPECT_TRUE(delegate->tap_down()); 3819 3820 // We haven't pressed long enough for a show press to occur 3821 EXPECT_FALSE(delegate->show_press()); 3822 EXPECT_FALSE(delegate->tap_cancel()); 3823 3824 delegate->Reset(); 3825 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 3826 kTouchId, tes.LeapForward(50)); 3827 DispatchEventUsingWindowDispatcher(&release1); 3828 EXPECT_TRUE(delegate->show_press()); 3829 EXPECT_FALSE(delegate->tap_cancel()); 3830 EXPECT_TRUE(delegate->tap()); 3831} 3832 3833// Test that consuming the first move touch event prevents a scroll. 3834TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) { 3835 scoped_ptr<QueueTouchEventDelegate> delegate( 3836 new QueueTouchEventDelegate(host()->dispatcher())); 3837 TimedEvents tes; 3838 const int kTouchId = 7; 3839 gfx::Rect bounds(0, 0, 1000, 1000); 3840 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3841 delegate.get(), -1234, bounds, root_window())); 3842 3843 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 3844 kTouchId, tes.Now()); 3845 DispatchEventUsingWindowDispatcher(&press); 3846 delegate->ReceivedAck(); 3847 3848 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(2, 2), 3849 kTouchId, tes.Now()); 3850 DispatchEventUsingWindowDispatcher(&move1); 3851 delegate->ReceivedAckPreventDefaulted(); 3852 3853 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20), 3854 kTouchId, tes.Now()); 3855 DispatchEventUsingWindowDispatcher(&move2); 3856 delegate->ReceivedAck(); 3857 3858 EXPECT_FALSE(delegate->scroll_begin()); 3859 EXPECT_FALSE(delegate->scroll_update()); 3860} 3861 3862// Test that consuming the first touch move event of a touch point doesn't 3863// prevent pinching once an additional touch has been pressed. 3864TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMovePinchTest) { 3865 scoped_ptr<QueueTouchEventDelegate> delegate( 3866 new QueueTouchEventDelegate(host()->dispatcher())); 3867 TimedEvents tes; 3868 const int kTouchId1 = 7; 3869 const int kTouchId2 = 4; 3870 gfx::Rect bounds(0, 0, 1000, 1000); 3871 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3872 delegate.get(), -1234, bounds, root_window())); 3873 3874 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 3875 kTouchId1, tes.Now()); 3876 DispatchEventUsingWindowDispatcher(&press1); 3877 delegate->ReceivedAck(); 3878 3879 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(2, 2), 3880 kTouchId1, tes.Now()); 3881 DispatchEventUsingWindowDispatcher(&move1); 3882 delegate->ReceivedAckPreventDefaulted(); 3883 3884 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20), 3885 kTouchId1, tes.Now()); 3886 DispatchEventUsingWindowDispatcher(&move2); 3887 delegate->ReceivedAck(); 3888 3889 // We can't scroll, because a move has been consumed. 3890 EXPECT_FALSE(delegate->scroll_begin()); 3891 EXPECT_FALSE(delegate->scroll_update()); 3892 EXPECT_FALSE(delegate->pinch_begin()); 3893 3894 // An additional press will allow us to pinch. 3895 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 3896 kTouchId2, tes.Now()); 3897 DispatchEventUsingWindowDispatcher(&press2); 3898 delegate->ReceivedAck(); 3899 3900 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(20, 20), 3901 kTouchId2, tes.Now()); 3902 DispatchEventUsingWindowDispatcher(&move3); 3903 delegate->ReceivedAck(); 3904 3905 EXPECT_TRUE(delegate->pinch_begin()); 3906 EXPECT_FALSE(delegate->pinch_update()); 3907 3908 delegate->Reset(); 3909 3910 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(40, 40), 3911 kTouchId2, tes.Now()); 3912 DispatchEventUsingWindowDispatcher(&move4); 3913 delegate->ReceivedAck(); 3914 3915 EXPECT_TRUE(delegate->pinch_update()); 3916 EXPECT_EQ(10, delegate->scroll_x()); 3917 EXPECT_EQ(10, delegate->scroll_y()); 3918} 3919 3920// Test that consuming the first move touch doesn't prevent a tap. 3921TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) { 3922 scoped_ptr<QueueTouchEventDelegate> delegate( 3923 new QueueTouchEventDelegate(host()->dispatcher())); 3924 TimedEvents tes; 3925 const int kTouchId = 7; 3926 gfx::Rect bounds(0, 0, 1000, 1000); 3927 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3928 delegate.get(), -1234, bounds, root_window())); 3929 3930 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 3931 kTouchId, tes.Now()); 3932 DispatchEventUsingWindowDispatcher(&press); 3933 delegate->ReceivedAck(); 3934 3935 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2), 3936 kTouchId, tes.Now()); 3937 DispatchEventUsingWindowDispatcher(&move); 3938 delegate->ReceivedAckPreventDefaulted(); 3939 3940 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2), 3941 kTouchId, tes.LeapForward(50)); 3942 DispatchEventUsingWindowDispatcher(&release); 3943 delegate->ReceivedAck(); 3944 3945 EXPECT_TRUE(delegate->tap()); 3946} 3947 3948// Test that consuming the first move touch doesn't prevent a long press. 3949TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) { 3950 scoped_ptr<QueueTouchEventDelegate> delegate( 3951 new QueueTouchEventDelegate(host()->dispatcher())); 3952 TimedEvents tes; 3953 const int kWindowWidth = 123; 3954 const int kWindowHeight = 45; 3955 const int kTouchId = 2; 3956 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight); 3957 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3958 delegate.get(), -1234, bounds, root_window())); 3959 3960 delegate->Reset(); 3961 3962 TimerTestGestureRecognizer* gesture_recognizer = 3963 new TimerTestGestureRecognizer(); 3964 3965 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer); 3966 3967 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 3968 kTouchId, tes.Now()); 3969 DispatchEventUsingWindowDispatcher(&press1); 3970 delegate->ReceivedAck(); 3971 3972 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203), 3973 kTouchId, tes.Now()); 3974 DispatchEventUsingWindowDispatcher(&move); 3975 delegate->ReceivedAckPreventDefaulted(); 3976 3977 // Wait until the timer runs out 3978 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS); 3979 EXPECT_TRUE(delegate->long_press()); 3980} 3981 3982// Tests that the deltas are correct when leaving the slop region very slowly. 3983TEST_F(GestureRecognizerTest, TestExceedingSlopSlowly) { 3984 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3); 3985 scoped_ptr<GestureEventConsumeDelegate> delegate( 3986 new GestureEventConsumeDelegate()); 3987 const int kWindowWidth = 234; 3988 const int kWindowHeight = 345; 3989 const int kTouchId = 5; 3990 TimedEvents tes; 3991 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); 3992 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 3993 delegate.get(), -1234, bounds, root_window())); 3994 3995 ui::TouchEvent press( 3996 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now()); 3997 DispatchEventUsingWindowDispatcher(&press); 3998 EXPECT_FALSE(delegate->scroll_begin()); 3999 EXPECT_FALSE(delegate->scroll_update()); 4000 delegate->Reset(); 4001 4002 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId, 4003 tes.LeapForward(40)); 4004 DispatchEventUsingWindowDispatcher(&move1); 4005 EXPECT_FALSE(delegate->scroll_begin()); 4006 EXPECT_FALSE(delegate->scroll_update()); 4007 EXPECT_EQ(0, delegate->scroll_x()); 4008 EXPECT_EQ(0, delegate->scroll_x_hint()); 4009 delegate->Reset(); 4010 4011 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId, 4012 tes.LeapForward(40)); 4013 DispatchEventUsingWindowDispatcher(&move2); 4014 EXPECT_FALSE(delegate->scroll_begin()); 4015 EXPECT_FALSE(delegate->scroll_update()); 4016 EXPECT_EQ(0, delegate->scroll_x()); 4017 EXPECT_EQ(0, delegate->scroll_x_hint()); 4018 delegate->Reset(); 4019 4020 4021 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(13, 10), kTouchId, 4022 tes.LeapForward(40)); 4023 DispatchEventUsingWindowDispatcher(&move3); 4024 EXPECT_TRUE(delegate->scroll_begin()); 4025 EXPECT_FALSE(delegate->scroll_update()); 4026 EXPECT_EQ(0, delegate->scroll_x()); 4027 EXPECT_EQ(3, delegate->scroll_x_hint()); 4028 delegate->Reset(); 4029 4030 4031 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId, 4032 tes.LeapForward(40)); 4033 DispatchEventUsingWindowDispatcher(&move4); 4034 EXPECT_FALSE(delegate->scroll_begin()); 4035 EXPECT_TRUE(delegate->scroll_update()); 4036 EXPECT_EQ(1, delegate->scroll_x()); 4037 EXPECT_EQ(0, delegate->scroll_x_hint()); 4038 delegate->Reset(); 4039} 4040 4041TEST_F(GestureRecognizerTest, ScrollAlternatelyConsumedTest) { 4042 scoped_ptr<QueueTouchEventDelegate> delegate( 4043 new QueueTouchEventDelegate(host()->dispatcher())); 4044 TimedEvents tes; 4045 const int kWindowWidth = 3000; 4046 const int kWindowHeight = 3000; 4047 const int kTouchId = 2; 4048 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); 4049 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 4050 delegate.get(), -1234, bounds, root_window())); 4051 4052 delegate->Reset(); 4053 4054 int x = 0; 4055 int y = 0; 4056 4057 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y), 4058 kTouchId, tes.Now()); 4059 DispatchEventUsingWindowDispatcher(&press1); 4060 delegate->ReceivedAck(); 4061 EXPECT_FALSE(delegate->scroll_begin()); 4062 EXPECT_FALSE(delegate->scroll_update()); 4063 delegate->Reset(); 4064 4065 x += 100; 4066 y += 100; 4067 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y), 4068 kTouchId, tes.Now()); 4069 DispatchEventUsingWindowDispatcher(&move1); 4070 delegate->ReceivedAck(); 4071 EXPECT_TRUE(delegate->scroll_begin()); 4072 EXPECT_TRUE(delegate->scroll_update()); 4073 delegate->Reset(); 4074 4075 for (int i = 0; i < 3; ++i) { 4076 x += 10; 4077 y += 10; 4078 ui::TouchEvent move2( 4079 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now()); 4080 DispatchEventUsingWindowDispatcher(&move2); 4081 delegate->ReceivedAck(); 4082 EXPECT_FALSE(delegate->scroll_begin()); 4083 EXPECT_TRUE(delegate->scroll_update()); 4084 EXPECT_EQ(10, delegate->scroll_x()); 4085 EXPECT_EQ(10, delegate->scroll_y()); 4086 delegate->Reset(); 4087 4088 x += 20; 4089 y += 20; 4090 ui::TouchEvent move3( 4091 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now()); 4092 DispatchEventUsingWindowDispatcher(&move3); 4093 delegate->ReceivedAckPreventDefaulted(); 4094 EXPECT_FALSE(delegate->scroll_begin()); 4095 EXPECT_FALSE(delegate->scroll_update()); 4096 delegate->Reset(); 4097 } 4098} 4099 4100TEST_F(GestureRecognizerTest, PinchAlternatelyConsumedTest) { 4101 scoped_ptr<QueueTouchEventDelegate> delegate( 4102 new QueueTouchEventDelegate(host()->dispatcher())); 4103 TimedEvents tes; 4104 const int kWindowWidth = 3000; 4105 const int kWindowHeight = 3000; 4106 const int kTouchId1 = 5; 4107 const int kTouchId2 = 7; 4108 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); 4109 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( 4110 delegate.get(), -1234, bounds, root_window())); 4111 4112 delegate->Reset(); 4113 4114 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 4115 kTouchId1, tes.Now()); 4116 DispatchEventUsingWindowDispatcher(&press1); 4117 delegate->ReceivedAck(); 4118 EXPECT_FALSE(delegate->scroll_begin()); 4119 EXPECT_FALSE(delegate->scroll_update()); 4120 delegate->Reset(); 4121 4122 int x = 0; 4123 int y = 0; 4124 4125 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y), 4126 kTouchId2, tes.Now()); 4127 DispatchEventUsingWindowDispatcher(&press2); 4128 delegate->ReceivedAck(); 4129 EXPECT_FALSE(delegate->scroll_begin()); 4130 EXPECT_FALSE(delegate->scroll_update()); 4131 EXPECT_FALSE(delegate->pinch_begin()); 4132 EXPECT_FALSE(delegate->pinch_update()); 4133 4134 delegate->Reset(); 4135 4136 x += 100; 4137 y += 100; 4138 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y), 4139 kTouchId2, tes.Now()); 4140 DispatchEventUsingWindowDispatcher(&move1); 4141 delegate->ReceivedAck(); 4142 EXPECT_TRUE(delegate->scroll_begin()); 4143 EXPECT_FALSE(delegate->scroll_update()); 4144 EXPECT_TRUE(delegate->pinch_begin()); 4145 EXPECT_FALSE(delegate->pinch_update()); 4146 delegate->Reset(); 4147 4148 const float expected_scales[] = {1.5f, 1.2f, 1.125f}; 4149 4150 for (int i = 0; i < 3; ++i) { 4151 x += 50; 4152 y += 50; 4153 ui::TouchEvent move2( 4154 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now()); 4155 DispatchEventUsingWindowDispatcher(&move2); 4156 delegate->ReceivedAck(); 4157 EXPECT_FALSE(delegate->scroll_begin()); 4158 EXPECT_TRUE(delegate->scroll_update()); 4159 EXPECT_FALSE(delegate->scroll_end()); 4160 EXPECT_FALSE(delegate->pinch_begin()); 4161 EXPECT_TRUE(delegate->pinch_update()); 4162 EXPECT_FALSE(delegate->pinch_end()); 4163 EXPECT_EQ(25, delegate->scroll_x()); 4164 EXPECT_EQ(25, delegate->scroll_y()); 4165 EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale()); 4166 delegate->Reset(); 4167 4168 x += 100; 4169 y += 100; 4170 ui::TouchEvent move3( 4171 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now()); 4172 DispatchEventUsingWindowDispatcher(&move3); 4173 delegate->ReceivedAckPreventDefaulted(); 4174 EXPECT_FALSE(delegate->scroll_begin()); 4175 EXPECT_FALSE(delegate->scroll_update()); 4176 EXPECT_FALSE(delegate->scroll_end()); 4177 EXPECT_FALSE(delegate->pinch_begin()); 4178 EXPECT_FALSE(delegate->pinch_update()); 4179 EXPECT_FALSE(delegate->pinch_end()); 4180 delegate->Reset(); 4181 } 4182} 4183 4184} // namespace test 4185} // namespace aura 4186