OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/scoped_vector.h" | 6 #include "base/memory/scoped_vector.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/timer/timer.h" | 9 #include "base/timer/timer.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 base::TimeDelta::FromMilliseconds(simulated_now_)); | 524 base::TimeDelta::FromMilliseconds(simulated_now_)); |
525 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move); | 525 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move); |
526 ASSERT_FALSE(details.dispatcher_destroyed); | 526 ASSERT_FALSE(details.dispatcher_destroyed); |
527 simulated_now_++; | 527 simulated_now_++; |
528 } | 528 } |
529 }; | 529 }; |
530 | 530 |
531 // An event handler to keep track of events. | 531 // An event handler to keep track of events. |
532 class TestEventHandler : public ui::EventHandler { | 532 class TestEventHandler : public ui::EventHandler { |
533 public: | 533 public: |
534 TestEventHandler() : touch_released_count_(0), | 534 TestEventHandler() |
535 touch_pressed_count_(0), | 535 : touch_released_count_(0), |
536 touch_moved_count_(0), | 536 touch_pressed_count_(0), |
537 touch_cancelled_count_(0) { | 537 touch_moved_count_(0) {} |
538 } | |
539 | 538 |
540 virtual ~TestEventHandler() {} | 539 virtual ~TestEventHandler() {} |
541 | 540 |
542 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE { | 541 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE { |
543 switch (event->type()) { | 542 switch (event->type()) { |
544 case ui::ET_TOUCH_RELEASED: | 543 case ui::ET_TOUCH_RELEASED: |
545 touch_released_count_++; | 544 touch_released_count_++; |
546 break; | 545 break; |
547 case ui::ET_TOUCH_PRESSED: | 546 case ui::ET_TOUCH_PRESSED: |
548 touch_pressed_count_++; | 547 touch_pressed_count_++; |
549 break; | 548 break; |
550 case ui::ET_TOUCH_MOVED: | 549 case ui::ET_TOUCH_MOVED: |
551 touch_moved_count_++; | 550 touch_moved_count_++; |
552 break; | 551 break; |
553 case ui::ET_TOUCH_CANCELLED: | 552 case ui::ET_TOUCH_CANCELLED: |
554 touch_cancelled_count_++; | 553 cancelled_touch_points_.push_back(event->location()); |
555 break; | 554 break; |
556 default: | 555 default: |
557 break; | 556 break; |
558 } | 557 } |
559 } | 558 } |
560 | 559 |
561 void Reset() { | 560 void Reset() { |
562 touch_released_count_ = 0; | 561 touch_released_count_ = 0; |
563 touch_pressed_count_ = 0; | 562 touch_pressed_count_ = 0; |
564 touch_moved_count_ = 0; | 563 touch_moved_count_ = 0; |
565 touch_cancelled_count_ = 0; | 564 cancelled_touch_points_.clear(); |
566 } | 565 } |
567 | 566 |
568 int touch_released_count() const { return touch_released_count_; } | 567 int touch_released_count() const { return touch_released_count_; } |
569 int touch_pressed_count() const { return touch_pressed_count_; } | 568 int touch_pressed_count() const { return touch_pressed_count_; } |
570 int touch_moved_count() const { return touch_moved_count_; } | 569 int touch_moved_count() const { return touch_moved_count_; } |
571 int touch_cancelled_count() const { return touch_cancelled_count_; } | 570 int touch_cancelled_count() const { |
| 571 return static_cast<int>(cancelled_touch_points_.size()); |
| 572 } |
| 573 const std::vector<gfx::PointF>& cancelled_touch_points() const { |
| 574 return cancelled_touch_points_; |
| 575 } |
572 | 576 |
573 private: | 577 private: |
574 int touch_released_count_; | 578 int touch_released_count_; |
575 int touch_pressed_count_; | 579 int touch_pressed_count_; |
576 int touch_moved_count_; | 580 int touch_moved_count_; |
577 int touch_cancelled_count_; | 581 std::vector<gfx::PointF> cancelled_touch_points_; |
578 | 582 |
579 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); | 583 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); |
580 }; | 584 }; |
581 | 585 |
582 // Removes the target window from its parent when it receives a touch-cancel | 586 // Removes the target window from its parent when it receives a touch-cancel |
583 // event. | 587 // event. |
584 class RemoveOnTouchCancelHandler : public TestEventHandler { | 588 class RemoveOnTouchCancelHandler : public TestEventHandler { |
585 public: | 589 public: |
586 RemoveOnTouchCancelHandler() {} | 590 RemoveOnTouchCancelHandler() {} |
587 virtual ~RemoveOnTouchCancelHandler() {} | 591 virtual ~RemoveOnTouchCancelHandler() {} |
(...skipping 2930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3518 EXPECT_FALSE(delegate->scroll_end()); | 3522 EXPECT_FALSE(delegate->scroll_end()); |
3519 } | 3523 } |
3520 | 3524 |
3521 TEST_F(GestureRecognizerTest, | 3525 TEST_F(GestureRecognizerTest, |
3522 TransferEventDispatchesTouchCancel) { | 3526 TransferEventDispatchesTouchCancel) { |
3523 scoped_ptr<GestureEventConsumeDelegate> delegate( | 3527 scoped_ptr<GestureEventConsumeDelegate> delegate( |
3524 new GestureEventConsumeDelegate()); | 3528 new GestureEventConsumeDelegate()); |
3525 TimedEvents tes; | 3529 TimedEvents tes; |
3526 const int kWindowWidth = 800; | 3530 const int kWindowWidth = 800; |
3527 const int kWindowHeight = 600; | 3531 const int kWindowHeight = 600; |
3528 const int kTouchId = 2; | 3532 const int kTouchId1 = 1; |
| 3533 const int kTouchId2 = 2; |
3529 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); | 3534 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); |
3530 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( | 3535 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( |
3531 delegate.get(), -1234, bounds, root_window())); | 3536 delegate.get(), -1234, bounds, root_window())); |
3532 scoped_ptr<RemoveOnTouchCancelHandler> | 3537 scoped_ptr<TestEventHandler> handler(new TestEventHandler()); |
3533 handler(new RemoveOnTouchCancelHandler()); | |
3534 window->AddPreTargetHandler(handler.get()); | 3538 window->AddPreTargetHandler(handler.get()); |
3535 | 3539 |
3536 // Start a gesture sequence on |window|. Then transfer the events to NULL. | 3540 // Start a gesture sequence on |window|. Then transfer the events to NULL. |
3537 // Make sure |window| receives a touch-cancel event. | 3541 // Make sure |window| receives a touch-cancel event. |
3538 delegate->Reset(); | 3542 delegate->Reset(); |
3539 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), | 3543 ui::TouchEvent press( |
3540 kTouchId, tes.Now()); | 3544 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now()); |
3541 ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 1, tes.Now()); | |
3542 DispatchEventUsingWindowDispatcher(&press); | 3545 DispatchEventUsingWindowDispatcher(&press); |
| 3546 EXPECT_2_EVENTS( |
| 3547 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN); |
| 3548 delegate->Reset(); |
| 3549 ui::TouchEvent p2( |
| 3550 ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), kTouchId2, tes.Now()); |
3543 DispatchEventUsingWindowDispatcher(&p2); | 3551 DispatchEventUsingWindowDispatcher(&p2); |
3544 EXPECT_FALSE(delegate->tap()); | 3552 EXPECT_2_EVENTS( |
3545 EXPECT_TRUE(delegate->tap_down()); | 3553 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN); |
3546 EXPECT_TRUE(delegate->tap_cancel()); | 3554 delegate->Reset(); |
3547 EXPECT_TRUE(delegate->begin()); | 3555 ui::TouchEvent move( |
| 3556 ui::ET_TOUCH_MOVED, gfx::Point(350, 300), kTouchId2, tes.Now()); |
| 3557 DispatchEventUsingWindowDispatcher(&move); |
| 3558 EXPECT_3_EVENTS(delegate->events(), |
| 3559 ui::ET_GESTURE_SCROLL_BEGIN, |
| 3560 ui::ET_GESTURE_SCROLL_UPDATE, |
| 3561 ui::ET_GESTURE_PINCH_BEGIN); |
3548 EXPECT_EQ(2, handler->touch_pressed_count()); | 3562 EXPECT_EQ(2, handler->touch_pressed_count()); |
3549 delegate->Reset(); | 3563 delegate->Reset(); |
3550 handler->Reset(); | 3564 handler->Reset(); |
3551 | 3565 |
3552 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get(); | 3566 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get(); |
3553 EXPECT_EQ(window.get(), | 3567 EXPECT_EQ(window.get(), |
3554 gesture_recognizer->GetTouchLockedTarget(press)); | 3568 gesture_recognizer->GetTouchLockedTarget(press)); |
3555 gesture_recognizer->TransferEventsTo(window.get(), NULL); | 3569 gesture_recognizer->TransferEventsTo(window.get(), NULL); |
3556 EXPECT_EQ(NULL, | 3570 EXPECT_EQ(NULL, |
3557 gesture_recognizer->GetTouchLockedTarget(press)); | 3571 gesture_recognizer->GetTouchLockedTarget(press)); |
3558 // The event-handler removes |window| from its parent on the first | 3572 EXPECT_4_EVENTS(delegate->events(), |
3559 // touch-cancel event, so it won't receive the second touch-cancel event. | 3573 ui::ET_GESTURE_PINCH_END, |
3560 EXPECT_EQ(1, handler->touch_cancelled_count()); | 3574 ui::ET_GESTURE_SCROLL_END, |
| 3575 ui::ET_GESTURE_END, |
| 3576 ui::ET_GESTURE_END); |
| 3577 const std::vector<gfx::PointF>& points = handler->cancelled_touch_points(); |
| 3578 EXPECT_EQ(2U, points.size()); |
| 3579 EXPECT_EQ(gfx::Point(101, 201), points[0]); |
| 3580 EXPECT_EQ(gfx::Point(350, 300), points[1]); |
3561 } | 3581 } |
3562 | 3582 |
3563 // Check that appropriate touch events generate show press events | 3583 // Check that appropriate touch events generate show press events |
3564 TEST_F(GestureRecognizerTest, GestureEventShowPress) { | 3584 TEST_F(GestureRecognizerTest, GestureEventShowPress) { |
3565 scoped_ptr<GestureEventConsumeDelegate> delegate( | 3585 scoped_ptr<GestureEventConsumeDelegate> delegate( |
3566 new GestureEventConsumeDelegate()); | 3586 new GestureEventConsumeDelegate()); |
3567 TimedEvents tes; | 3587 TimedEvents tes; |
3568 const int kWindowWidth = 123; | 3588 const int kWindowWidth = 123; |
3569 const int kWindowHeight = 45; | 3589 const int kWindowHeight = 45; |
3570 const int kTouchId = 2; | 3590 const int kTouchId = 2; |
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4258 ui::TouchEvent move3( | 4278 ui::TouchEvent move3( |
4259 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now()); | 4279 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now()); |
4260 DispatchEventUsingWindowDispatcher(&move3); | 4280 DispatchEventUsingWindowDispatcher(&move3); |
4261 | 4281 |
4262 delegate->ReceivedAck(); | 4282 delegate->ReceivedAck(); |
4263 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); | 4283 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); |
4264 } | 4284 } |
4265 | 4285 |
4266 } // namespace test | 4286 } // namespace test |
4267 } // namespace aura | 4287 } // namespace aura |
OLD | NEW |