OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 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 "content/browser/renderer_host/input/synthetic_gesture_controller.h" | 5 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 28 matching lines...) Expand all Loading... | |
39 #include "ui/gfx/geometry/vector2d_f.h" | 39 #include "ui/gfx/geometry/vector2d_f.h" |
40 | 40 |
41 using blink::WebInputEvent; | 41 using blink::WebInputEvent; |
42 using blink::WebMouseEvent; | 42 using blink::WebMouseEvent; |
43 using blink::WebMouseWheelEvent; | 43 using blink::WebMouseWheelEvent; |
44 using blink::WebTouchEvent; | 44 using blink::WebTouchEvent; |
45 using blink::WebTouchPoint; | 45 using blink::WebTouchPoint; |
46 | 46 |
47 namespace content { | 47 namespace content { |
48 | 48 |
49 class TestSyntheticGestureController : public SyntheticGestureController { | |
50 public: | |
51 TestSyntheticGestureController( | |
52 std::unique_ptr<SyntheticGestureTarget> gesture_target) | |
53 : SyntheticGestureController(std::move(gesture_target)) {} | |
54 ~TestSyntheticGestureController() override {} | |
55 | |
56 int PointerIndex(int index) const { | |
57 CHECK_GE(index, 0); | |
58 CHECK_LT(index, WebTouchEvent::touchesLengthCap); | |
59 return pointer_action_controller_.index_map_[index]; | |
60 ; | |
tdresser
2016/05/20 14:24:30
Remove extra ;
lanwei
2016/05/23 16:40:50
Done.
| |
61 } | |
62 }; | |
63 | |
49 namespace { | 64 namespace { |
50 | 65 |
51 const int kFlushInputRateInMs = 16; | 66 const int kFlushInputRateInMs = 16; |
52 const int kPointerAssumedStoppedTimeMs = 43; | 67 const int kPointerAssumedStoppedTimeMs = 43; |
53 const float kTouchSlopInDips = 7.0f; | 68 const float kTouchSlopInDips = 7.0f; |
54 const float kMinScalingSpanInDips = 27.5f; | 69 const float kMinScalingSpanInDips = 27.5f; |
55 const int kTouchPointersLength = 16; | 70 const int kTouchPointersLength = 16; |
56 | 71 |
57 enum TouchGestureType { TOUCH_SCROLL, TOUCH_DRAG }; | 72 enum TouchGestureType { TOUCH_SCROLL, TOUCH_DRAG }; |
58 | 73 |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
475 | 490 |
476 class MockSyntheticPointerActionTarget : public MockSyntheticGestureTarget { | 491 class MockSyntheticPointerActionTarget : public MockSyntheticGestureTarget { |
477 public: | 492 public: |
478 MockSyntheticPointerActionTarget() {} | 493 MockSyntheticPointerActionTarget() {} |
479 ~MockSyntheticPointerActionTarget() override {} | 494 ~MockSyntheticPointerActionTarget() override {} |
480 | 495 |
481 gfx::PointF positions(int index) const { return positions_[index]; } | 496 gfx::PointF positions(int index) const { return positions_[index]; } |
482 int indexes(int index) const { return indexes_[index]; } | 497 int indexes(int index) const { return indexes_[index]; } |
483 WebTouchPoint::State states(int index) { return states_[index]; } | 498 WebTouchPoint::State states(int index) { return states_[index]; } |
484 unsigned touch_length() const { return touch_length_; } | 499 unsigned touch_length() const { return touch_length_; } |
485 WebInputEvent::Type type() const { return type_; } | |
486 | 500 |
487 protected: | 501 protected: |
488 gfx::PointF positions_[kTouchPointersLength]; | 502 gfx::PointF positions_[kTouchPointersLength]; |
489 unsigned touch_length_; | 503 unsigned touch_length_; |
490 int indexes_[kTouchPointersLength]; | 504 int indexes_[kTouchPointersLength]; |
491 WebTouchPoint::State states_[kTouchPointersLength]; | 505 WebTouchPoint::State states_[kTouchPointersLength]; |
492 WebInputEvent::Type type_; | |
493 }; | 506 }; |
494 | 507 |
495 class MockSyntheticPointerTouchActionTarget | 508 class MockSyntheticPointerTouchActionTarget |
496 : public MockSyntheticPointerActionTarget { | 509 : public MockSyntheticPointerActionTarget { |
497 public: | 510 public: |
498 MockSyntheticPointerTouchActionTarget() {} | 511 MockSyntheticPointerTouchActionTarget() {} |
499 ~MockSyntheticPointerTouchActionTarget() override {} | 512 ~MockSyntheticPointerTouchActionTarget() override {} |
500 | 513 |
501 void DispatchInputEventToPlatform(const WebInputEvent& event) override { | 514 void DispatchInputEventToPlatform(const WebInputEvent& event) override { |
502 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); | 515 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); |
503 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); | 516 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); |
504 type_ = touch_event.type; | |
505 for (size_t i = 0; i < touch_event.touchesLength; ++i) { | 517 for (size_t i = 0; i < touch_event.touchesLength; ++i) { |
506 indexes_[i] = touch_event.touches[i].id; | 518 indexes_[i] = touch_event.touches[i].id; |
507 positions_[i] = gfx::PointF(touch_event.touches[i].position); | 519 positions_[i] = gfx::PointF(touch_event.touches[i].position); |
508 states_[i] = touch_event.touches[i].state; | 520 states_[i] = touch_event.touches[i].state; |
509 } | 521 } |
510 touch_length_ = touch_event.touchesLength; | 522 touch_length_ = touch_event.touchesLength; |
511 } | 523 } |
512 }; | 524 }; |
513 | 525 |
514 class SyntheticGestureControllerTestBase { | 526 class SyntheticGestureControllerTestBase { |
515 public: | 527 public: |
516 SyntheticGestureControllerTestBase() {} | 528 SyntheticGestureControllerTestBase() {} |
517 ~SyntheticGestureControllerTestBase() {} | 529 ~SyntheticGestureControllerTestBase() {} |
518 | 530 |
519 protected: | 531 protected: |
520 template<typename MockGestureTarget> | 532 template<typename MockGestureTarget> |
521 void CreateControllerAndTarget() { | 533 void CreateControllerAndTarget() { |
522 target_ = new MockGestureTarget(); | 534 target_ = new MockGestureTarget(); |
523 controller_.reset(new SyntheticGestureController( | 535 controller_.reset(new TestSyntheticGestureController( |
524 std::unique_ptr<SyntheticGestureTarget>(target_))); | 536 std::unique_ptr<SyntheticGestureTarget>(target_))); |
525 } | 537 } |
526 | 538 |
527 void QueueSyntheticGesture(std::unique_ptr<SyntheticGesture> gesture) { | 539 void QueueSyntheticGesture(std::unique_ptr<SyntheticGesture> gesture) { |
528 controller_->QueueSyntheticGesture( | 540 controller_->QueueSyntheticGesture( |
529 std::move(gesture), | 541 std::move(gesture), |
530 base::Bind( | 542 base::Bind( |
531 &SyntheticGestureControllerTestBase::OnSyntheticGestureCompleted, | 543 &SyntheticGestureControllerTestBase::OnSyntheticGestureCompleted, |
532 base::Unretained(this))); | 544 base::Unretained(this))); |
533 } | 545 } |
534 | 546 |
547 void QueueSyntheticPointerAction( | |
548 const SyntheticPointerActionParams& gesture_params) { | |
549 controller_->QueueSyntheticPointerAction( | |
550 gesture_params, base::Bind(&SyntheticGestureControllerTestBase:: | |
551 OnSyntheticPointerActionCompleted, | |
552 base::Unretained(this))); | |
553 } | |
554 | |
535 void FlushInputUntilComplete() { | 555 void FlushInputUntilComplete() { |
536 while (target_->flush_requested()) { | 556 while (target_->flush_requested()) { |
537 while (target_->flush_requested()) { | 557 while (target_->flush_requested()) { |
538 target_->ClearFlushRequest(); | 558 target_->ClearFlushRequest(); |
539 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs); | 559 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs); |
540 controller_->Flush(time_); | 560 controller_->Flush(time_); |
541 } | 561 } |
542 controller_->OnDidFlushInput(); | 562 controller_->OnDidFlushInput(); |
543 } | 563 } |
544 } | 564 } |
545 | 565 |
546 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { | 566 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { |
547 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); | 567 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); |
548 if (result == SyntheticGesture::GESTURE_FINISHED) | 568 if (result == SyntheticGesture::GESTURE_FINISHED) |
549 num_success_++; | 569 num_success_++; |
550 else | 570 else |
551 num_failure_++; | 571 num_failure_++; |
552 } | 572 } |
553 | 573 |
574 void OnSyntheticPointerActionCompleted(SyntheticGesture::Result result) { | |
575 if (result == SyntheticGesture::POINTER_ACTION_PROCESSED) { | |
576 num_success_++; | |
577 action_state_ = PROCESSED; | |
578 } else if (result == SyntheticGesture::POINTER_ACTION_FINISHED) { | |
579 num_success_++; | |
580 action_state_ = FINISHED; | |
581 } else { | |
582 num_failure_++; | |
583 action_state_ = OTHER; | |
584 } | |
585 } | |
586 | |
554 base::TimeDelta GetTotalTime() const { return time_ - start_time_; } | 587 base::TimeDelta GetTotalTime() const { return time_ - start_time_; } |
555 | 588 |
589 int PointerIndex(int index) const { return controller_->PointerIndex(index); } | |
590 | |
591 enum PointerActionState { PROCESSED, FINISHED, OTHER }; | |
592 | |
556 MockSyntheticGestureTarget* target_; | 593 MockSyntheticGestureTarget* target_; |
557 std::unique_ptr<SyntheticGestureController> controller_; | 594 std::unique_ptr<TestSyntheticGestureController> controller_; |
558 base::TimeTicks start_time_; | 595 base::TimeTicks start_time_; |
559 base::TimeTicks time_; | 596 base::TimeTicks time_; |
560 int num_success_; | 597 int num_success_; |
561 int num_failure_; | 598 int num_failure_; |
599 PointerActionState action_state_; | |
562 }; | 600 }; |
563 | 601 |
564 class SyntheticGestureControllerTest | 602 class SyntheticGestureControllerTest |
565 : public SyntheticGestureControllerTestBase, | 603 : public SyntheticGestureControllerTestBase, |
566 public testing::Test { | 604 public testing::Test { |
567 protected: | 605 protected: |
568 void SetUp() override { | 606 void SetUp() override { |
569 start_time_ = base::TimeTicks::Now(); | 607 start_time_ = base::TimeTicks::Now(); |
570 time_ = start_time_; | 608 time_ = start_time_; |
571 num_success_ = 0; | 609 num_success_ = 0; |
572 num_failure_ = 0; | 610 num_failure_ = 0; |
611 action_state_ = OTHER; | |
573 } | 612 } |
574 | 613 |
575 void TearDown() override { | 614 void TearDown() override { |
576 controller_.reset(); | 615 controller_.reset(); |
577 target_ = nullptr; | 616 target_ = nullptr; |
578 time_ = base::TimeTicks(); | 617 time_ = base::TimeTicks(); |
579 } | 618 } |
580 }; | 619 }; |
581 | 620 |
582 class SyntheticGestureControllerTestWithParam | 621 class SyntheticGestureControllerTestWithParam |
583 : public SyntheticGestureControllerTestBase, | 622 : public SyntheticGestureControllerTestBase, |
584 public testing::TestWithParam<bool> { | 623 public testing::TestWithParam<bool> { |
585 protected: | 624 protected: |
586 void SetUp() override { | 625 void SetUp() override { |
587 start_time_ = base::TimeTicks::Now(); | 626 start_time_ = base::TimeTicks::Now(); |
588 time_ = start_time_; | 627 time_ = start_time_; |
589 num_success_ = 0; | 628 num_success_ = 0; |
590 num_failure_ = 0; | 629 num_failure_ = 0; |
630 action_state_ = OTHER; | |
591 } | 631 } |
592 | 632 |
593 void TearDown() override { | 633 void TearDown() override { |
594 controller_.reset(); | 634 controller_.reset(); |
595 target_ = nullptr; | 635 target_ = nullptr; |
596 time_ = base::TimeTicks(); | 636 time_ = base::TimeTicks(); |
597 } | 637 } |
598 }; | 638 }; |
599 | 639 |
600 TEST_F(SyntheticGestureControllerTest, SingleGesture) { | 640 TEST_F(SyntheticGestureControllerTest, SingleGesture) { |
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1468 EXPECT_TRUE(tap_target->GestureFinished()); | 1508 EXPECT_TRUE(tap_target->GestureFinished()); |
1469 EXPECT_EQ(tap_target->position(), params.position); | 1509 EXPECT_EQ(tap_target->position(), params.position); |
1470 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms); | 1510 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms); |
1471 EXPECT_GE(GetTotalTime(), | 1511 EXPECT_GE(GetTotalTime(), |
1472 base::TimeDelta::FromMilliseconds(params.duration_ms)); | 1512 base::TimeDelta::FromMilliseconds(params.duration_ms)); |
1473 } | 1513 } |
1474 | 1514 |
1475 TEST_F(SyntheticGestureControllerTest, PointerTouchAction) { | 1515 TEST_F(SyntheticGestureControllerTest, PointerTouchAction) { |
1476 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); | 1516 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); |
1477 | 1517 |
1478 SyntheticPointerActionParams params = SyntheticPointerActionParams( | 1518 SyntheticPointerActionParams params1 = SyntheticPointerActionParams( |
1479 SyntheticPointerActionParams::PointerActionType::PRESS); | 1519 SyntheticPointerActionParams::PointerActionType::PRESS); |
1480 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | 1520 params1.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
1481 params.set_index(0); | 1521 params1.set_index(0); |
1482 params.set_position(gfx::PointF(54, 89)); | 1522 params1.set_position(gfx::PointF(54, 89)); |
1483 SyntheticTouchPointer synthetic_pointer; | 1523 SyntheticTouchPointer synthetic_pointer; |
1484 | 1524 |
1485 std::unique_ptr<SyntheticPointerAction> gesture( | 1525 // Send a touch press for one finger. |
1486 new SyntheticPointerAction(params, &synthetic_pointer)); | 1526 SyntheticPointerActionParams process_params = SyntheticPointerActionParams( |
1487 QueueSyntheticGesture(std::move(gesture)); | 1527 SyntheticPointerActionParams::PointerActionType::PROCESS); |
1528 process_params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
1529 QueueSyntheticPointerAction(params1); | |
1530 QueueSyntheticPointerAction(process_params); | |
1488 FlushInputUntilComplete(); | 1531 FlushInputUntilComplete(); |
1489 | 1532 |
1490 MockSyntheticPointerTouchActionTarget* pointer_touch_target = | 1533 MockSyntheticPointerTouchActionTarget* pointer_touch_target = |
1491 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); | 1534 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); |
1492 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); | 1535 EXPECT_EQ(1, num_success_); |
tdresser
2016/05/20 14:24:30
Why remove the type comparison?
lanwei
2016/05/23 16:40:50
Because when we have one finger move and one finge
tdresser
2016/05/24 20:35:38
I wouldn't say it's not meaningful - it depends on
| |
1493 EXPECT_EQ(pointer_touch_target->positions(0), params.position()); | 1536 EXPECT_EQ(0, num_failure_); |
1537 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0)); | |
1538 EXPECT_EQ(pointer_touch_target->positions(0), params1.position()); | |
1494 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); | 1539 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); |
1495 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); | 1540 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); |
1541 EXPECT_EQ(PROCESSED, action_state_); | |
1496 | 1542 |
1497 params.set_index(1); | 1543 // Send a touch move for the first finger and a touch press for the second |
1498 params.set_position(gfx::PointF(79, 132)); | 1544 // finger. |
1499 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); | 1545 params1.set_pointer_action_type( |
1500 QueueSyntheticGesture(std::move(gesture)); | 1546 SyntheticPointerActionParams::PointerActionType::MOVE); |
1547 params1.set_position(gfx::PointF(133, 156)); | |
1548 QueueSyntheticPointerAction(params1); | |
1549 SyntheticPointerActionParams params2 = SyntheticPointerActionParams( | |
1550 SyntheticPointerActionParams::PointerActionType::PRESS); | |
1551 params2.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
1552 params2.set_index(1); | |
1553 params2.set_position(gfx::PointF(79, 132)); | |
1554 QueueSyntheticPointerAction(params2); | |
1555 QueueSyntheticPointerAction(process_params); | |
1501 FlushInputUntilComplete(); | 1556 FlushInputUntilComplete(); |
1502 | 1557 |
1503 pointer_touch_target = | 1558 pointer_touch_target = |
1504 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); | 1559 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); |
1505 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); | 1560 EXPECT_EQ(2, num_success_); |
1506 EXPECT_EQ(pointer_touch_target->indexes(1), params.index()); | 1561 EXPECT_EQ(0, num_failure_); |
1507 EXPECT_EQ(pointer_touch_target->positions(1), params.position()); | 1562 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0)); |
1563 EXPECT_EQ(pointer_touch_target->positions(0), params1.position()); | |
1564 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateMoved); | |
1565 EXPECT_EQ(pointer_touch_target->indexes(1), PointerIndex(1)); | |
1566 EXPECT_EQ(pointer_touch_target->positions(1), params2.position()); | |
1508 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed); | 1567 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed); |
1509 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | 1568 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); |
1569 EXPECT_EQ(PROCESSED, action_state_); | |
1510 | 1570 |
1511 params.set_pointer_action_type( | 1571 // Send touch releases for both fingers. |
1512 SyntheticPointerActionParams::PointerActionType::MOVE); | 1572 params1.set_pointer_action_type( |
1513 params.set_position(gfx::PointF(133, 156)); | 1573 SyntheticPointerActionParams::PointerActionType::RELEASE); |
1514 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); | 1574 QueueSyntheticPointerAction(params1); |
1515 QueueSyntheticGesture(std::move(gesture)); | 1575 params2.set_pointer_action_type( |
1576 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
1577 QueueSyntheticPointerAction(params2); | |
1578 QueueSyntheticPointerAction(process_params); | |
1516 FlushInputUntilComplete(); | 1579 FlushInputUntilComplete(); |
1517 | 1580 |
1518 pointer_touch_target = | 1581 EXPECT_EQ(3, num_success_); |
1519 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); | 1582 EXPECT_EQ(0, num_failure_); |
1520 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchMove); | 1583 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0)); |
1521 EXPECT_EQ(pointer_touch_target->positions(1), params.position()); | 1584 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateReleased); |
1522 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateMoved); | 1585 EXPECT_EQ(pointer_touch_target->indexes(1), PointerIndex(1)); |
1586 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased); | |
1523 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | 1587 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); |
1588 EXPECT_EQ(PROCESSED, action_state_); | |
1524 | 1589 |
1525 params.set_pointer_action_type( | 1590 // Send a finish action to notify synthetic gesture controller the whole |
1526 SyntheticPointerActionParams::PointerActionType::RELEASE); | 1591 // pointer action sequence has been handled. |
1527 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); | 1592 SyntheticPointerActionParams finish_params = SyntheticPointerActionParams( |
1528 QueueSyntheticGesture(std::move(gesture)); | 1593 SyntheticPointerActionParams::PointerActionType::FINISH); |
1594 finish_params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
1595 QueueSyntheticPointerAction(finish_params); | |
1529 FlushInputUntilComplete(); | 1596 FlushInputUntilComplete(); |
1530 | 1597 |
1531 pointer_touch_target = | 1598 EXPECT_EQ(4, num_success_); |
1532 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); | 1599 EXPECT_EQ(0, num_failure_); |
1533 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchEnd); | 1600 EXPECT_EQ(FINISHED, action_state_); |
1534 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased); | |
1535 } | 1601 } |
1536 | 1602 |
1537 } // namespace | 1603 } // namespace |
1538 | 1604 |
1539 } // namespace content | 1605 } // namespace content |
OLD | NEW |