Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: content/browser/renderer_host/input/synthetic_gesture_controller_unittest.cc

Issue 1884883005: Prepare SyntheticPointerAction to handle touch actions for multiple fingers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make a new class SyntheticPointerActionController Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 MockSyntheticGestureController : public SyntheticGestureController {
tdresser 2016/05/19 17:57:03 Technically this isn't a Mock. I can never keep tr
lanwei 2016/05/20 01:48:16 Thanks for explaining what a mock is doing!
50 public:
51 MockSyntheticGestureController(
52 std::unique_ptr<SyntheticGestureTarget> gesture_target)
53 : SyntheticGestureController(std::move(gesture_target)) {}
54 ~MockSyntheticGestureController() override {}
55
56 int PointerIndex(int index) const {
57 return pointer_action_controller_.PointerIndex(index);
58 }
59 };
60
49 namespace { 61 namespace {
50 62
51 const int kFlushInputRateInMs = 16; 63 const int kFlushInputRateInMs = 16;
52 const int kPointerAssumedStoppedTimeMs = 43; 64 const int kPointerAssumedStoppedTimeMs = 43;
53 const float kTouchSlopInDips = 7.0f; 65 const float kTouchSlopInDips = 7.0f;
54 const float kMinScalingSpanInDips = 27.5f; 66 const float kMinScalingSpanInDips = 27.5f;
55 const int kTouchPointersLength = 16; 67 const int kTouchPointersLength = 16;
56 68
57 enum TouchGestureType { TOUCH_SCROLL, TOUCH_DRAG }; 69 enum TouchGestureType { TOUCH_SCROLL, TOUCH_DRAG };
58 70
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 487
476 class MockSyntheticPointerActionTarget : public MockSyntheticGestureTarget { 488 class MockSyntheticPointerActionTarget : public MockSyntheticGestureTarget {
477 public: 489 public:
478 MockSyntheticPointerActionTarget() {} 490 MockSyntheticPointerActionTarget() {}
479 ~MockSyntheticPointerActionTarget() override {} 491 ~MockSyntheticPointerActionTarget() override {}
480 492
481 gfx::PointF positions(int index) const { return positions_[index]; } 493 gfx::PointF positions(int index) const { return positions_[index]; }
482 int indexes(int index) const { return indexes_[index]; } 494 int indexes(int index) const { return indexes_[index]; }
483 WebTouchPoint::State states(int index) { return states_[index]; } 495 WebTouchPoint::State states(int index) { return states_[index]; }
484 unsigned touch_length() const { return touch_length_; } 496 unsigned touch_length() const { return touch_length_; }
485 WebInputEvent::Type type() const { return type_; }
486 497
487 protected: 498 protected:
488 gfx::PointF positions_[kTouchPointersLength]; 499 gfx::PointF positions_[kTouchPointersLength];
489 unsigned touch_length_; 500 unsigned touch_length_;
490 int indexes_[kTouchPointersLength]; 501 int indexes_[kTouchPointersLength];
491 WebTouchPoint::State states_[kTouchPointersLength]; 502 WebTouchPoint::State states_[kTouchPointersLength];
492 WebInputEvent::Type type_;
493 }; 503 };
494 504
495 class MockSyntheticPointerTouchActionTarget 505 class MockSyntheticPointerTouchActionTarget
496 : public MockSyntheticPointerActionTarget { 506 : public MockSyntheticPointerActionTarget {
497 public: 507 public:
498 MockSyntheticPointerTouchActionTarget() {} 508 MockSyntheticPointerTouchActionTarget() {}
499 ~MockSyntheticPointerTouchActionTarget() override {} 509 ~MockSyntheticPointerTouchActionTarget() override {}
500 510
501 void DispatchInputEventToPlatform(const WebInputEvent& event) override { 511 void DispatchInputEventToPlatform(const WebInputEvent& event) override {
502 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); 512 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type));
503 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); 513 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) { 514 for (size_t i = 0; i < touch_event.touchesLength; ++i) {
506 indexes_[i] = touch_event.touches[i].id; 515 indexes_[i] = touch_event.touches[i].id;
507 positions_[i] = gfx::PointF(touch_event.touches[i].position); 516 positions_[i] = gfx::PointF(touch_event.touches[i].position);
508 states_[i] = touch_event.touches[i].state; 517 states_[i] = touch_event.touches[i].state;
509 } 518 }
510 touch_length_ = touch_event.touchesLength; 519 touch_length_ = touch_event.touchesLength;
511 } 520 }
512 }; 521 };
513 522
514 class SyntheticGestureControllerTestBase { 523 class SyntheticGestureControllerTestBase {
515 public: 524 public:
516 SyntheticGestureControllerTestBase() {} 525 SyntheticGestureControllerTestBase() {}
517 ~SyntheticGestureControllerTestBase() {} 526 ~SyntheticGestureControllerTestBase() {}
518 527
519 protected: 528 protected:
520 template<typename MockGestureTarget> 529 template<typename MockGestureTarget>
521 void CreateControllerAndTarget() { 530 void CreateControllerAndTarget() {
522 target_ = new MockGestureTarget(); 531 target_ = new MockGestureTarget();
523 controller_.reset(new SyntheticGestureController( 532 controller_.reset(new MockSyntheticGestureController(
524 std::unique_ptr<SyntheticGestureTarget>(target_))); 533 std::unique_ptr<SyntheticGestureTarget>(target_)));
525 } 534 }
526 535
527 void QueueSyntheticGesture(std::unique_ptr<SyntheticGesture> gesture) { 536 void QueueSyntheticGesture(std::unique_ptr<SyntheticGesture> gesture) {
528 controller_->QueueSyntheticGesture( 537 controller_->QueueSyntheticGesture(
529 std::move(gesture), 538 std::move(gesture),
530 base::Bind( 539 base::Bind(
531 &SyntheticGestureControllerTestBase::OnSyntheticGestureCompleted, 540 &SyntheticGestureControllerTestBase::OnSyntheticGestureCompleted,
532 base::Unretained(this))); 541 base::Unretained(this)));
533 } 542 }
534 543
544 void QueueSyntheticPointerAction(
545 const SyntheticPointerActionParams& gesture_params) {
546 controller_->QueueSyntheticPointerAction(
547 gesture_params, base::Bind(&SyntheticGestureControllerTestBase::
548 OnSyntheticPointerActionCompleted,
549 base::Unretained(this)));
550 }
551
535 void FlushInputUntilComplete() { 552 void FlushInputUntilComplete() {
536 while (target_->flush_requested()) { 553 while (target_->flush_requested()) {
537 while (target_->flush_requested()) { 554 while (target_->flush_requested()) {
538 target_->ClearFlushRequest(); 555 target_->ClearFlushRequest();
539 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs); 556 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs);
540 controller_->Flush(time_); 557 controller_->Flush(time_);
541 } 558 }
542 controller_->OnDidFlushInput(); 559 controller_->OnDidFlushInput();
543 } 560 }
544 } 561 }
545 562
546 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { 563 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) {
547 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); 564 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING);
548 if (result == SyntheticGesture::GESTURE_FINISHED) 565 if (result == SyntheticGesture::GESTURE_FINISHED)
549 num_success_++; 566 num_success_++;
550 else 567 else
551 num_failure_++; 568 num_failure_++;
552 } 569 }
553 570
571 void OnSyntheticPointerActionCompleted(SyntheticGesture::Result result) {
572 if (result == SyntheticGesture::POINTER_ACTION_PROCESSED) {
573 num_success_++;
574 action_state_ = PROCESSED;
575 } else if (result == SyntheticGesture::POINTER_ACTION_FINISHED) {
576 num_success_++;
577 action_state_ = FINISHED;
578 } else {
579 num_failure_++;
580 action_state_ = OTHER;
581 }
582 }
583
554 base::TimeDelta GetTotalTime() const { return time_ - start_time_; } 584 base::TimeDelta GetTotalTime() const { return time_ - start_time_; }
555 585
586 int PointerIndex(int index) const { return controller_->PointerIndex(index); }
587
588 enum PointerActionState { PROCESSED, FINISHED, OTHER };
589
556 MockSyntheticGestureTarget* target_; 590 MockSyntheticGestureTarget* target_;
557 std::unique_ptr<SyntheticGestureController> controller_; 591 std::unique_ptr<MockSyntheticGestureController> controller_;
558 base::TimeTicks start_time_; 592 base::TimeTicks start_time_;
559 base::TimeTicks time_; 593 base::TimeTicks time_;
560 int num_success_; 594 int num_success_;
561 int num_failure_; 595 int num_failure_;
596 PointerActionState action_state_;
562 }; 597 };
563 598
564 class SyntheticGestureControllerTest 599 class SyntheticGestureControllerTest
565 : public SyntheticGestureControllerTestBase, 600 : public SyntheticGestureControllerTestBase,
566 public testing::Test { 601 public testing::Test {
567 protected: 602 protected:
568 void SetUp() override { 603 void SetUp() override {
569 start_time_ = base::TimeTicks::Now(); 604 start_time_ = base::TimeTicks::Now();
570 time_ = start_time_; 605 time_ = start_time_;
571 num_success_ = 0; 606 num_success_ = 0;
572 num_failure_ = 0; 607 num_failure_ = 0;
608 action_state_ = OTHER;
573 } 609 }
574 610
575 void TearDown() override { 611 void TearDown() override {
576 controller_.reset(); 612 controller_.reset();
577 target_ = nullptr; 613 target_ = nullptr;
578 time_ = base::TimeTicks(); 614 time_ = base::TimeTicks();
579 } 615 }
580 }; 616 };
581 617
582 class SyntheticGestureControllerTestWithParam 618 class SyntheticGestureControllerTestWithParam
583 : public SyntheticGestureControllerTestBase, 619 : public SyntheticGestureControllerTestBase,
584 public testing::TestWithParam<bool> { 620 public testing::TestWithParam<bool> {
585 protected: 621 protected:
586 void SetUp() override { 622 void SetUp() override {
587 start_time_ = base::TimeTicks::Now(); 623 start_time_ = base::TimeTicks::Now();
588 time_ = start_time_; 624 time_ = start_time_;
589 num_success_ = 0; 625 num_success_ = 0;
590 num_failure_ = 0; 626 num_failure_ = 0;
627 action_state_ = OTHER;
591 } 628 }
592 629
593 void TearDown() override { 630 void TearDown() override {
594 controller_.reset(); 631 controller_.reset();
595 target_ = nullptr; 632 target_ = nullptr;
596 time_ = base::TimeTicks(); 633 time_ = base::TimeTicks();
597 } 634 }
598 }; 635 };
599 636
600 TEST_F(SyntheticGestureControllerTest, SingleGesture) { 637 TEST_F(SyntheticGestureControllerTest, SingleGesture) {
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after
1468 EXPECT_TRUE(tap_target->GestureFinished()); 1505 EXPECT_TRUE(tap_target->GestureFinished());
1469 EXPECT_EQ(tap_target->position(), params.position); 1506 EXPECT_EQ(tap_target->position(), params.position);
1470 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms); 1507 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms);
1471 EXPECT_GE(GetTotalTime(), 1508 EXPECT_GE(GetTotalTime(),
1472 base::TimeDelta::FromMilliseconds(params.duration_ms)); 1509 base::TimeDelta::FromMilliseconds(params.duration_ms));
1473 } 1510 }
1474 1511
1475 TEST_F(SyntheticGestureControllerTest, PointerTouchAction) { 1512 TEST_F(SyntheticGestureControllerTest, PointerTouchAction) {
1476 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); 1513 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>();
1477 1514
1478 SyntheticPointerActionParams params = SyntheticPointerActionParams( 1515 SyntheticPointerActionParams params1 = SyntheticPointerActionParams(
1479 SyntheticPointerActionParams::PointerActionType::PRESS); 1516 SyntheticPointerActionParams::PointerActionType::PRESS);
1480 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; 1517 params1.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1481 params.set_index(0); 1518 params1.set_index(0);
1482 params.set_position(gfx::PointF(54, 89)); 1519 params1.set_position(gfx::PointF(54, 89));
1483 SyntheticTouchPointer synthetic_pointer; 1520 SyntheticTouchPointer synthetic_pointer;
1484 1521
1485 std::unique_ptr<SyntheticPointerAction> gesture( 1522 // Send a touch press for one finger.
1486 new SyntheticPointerAction(params, &synthetic_pointer)); 1523 SyntheticPointerActionParams process_params = SyntheticPointerActionParams(
1487 QueueSyntheticGesture(std::move(gesture)); 1524 SyntheticPointerActionParams::PointerActionType::PROCESS);
1525 process_params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1526 QueueSyntheticPointerAction(params1);
1527 QueueSyntheticPointerAction(process_params);
1488 FlushInputUntilComplete(); 1528 FlushInputUntilComplete();
1489 1529
1490 MockSyntheticPointerTouchActionTarget* pointer_touch_target = 1530 MockSyntheticPointerTouchActionTarget* pointer_touch_target =
1491 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1531 static_cast<MockSyntheticPointerTouchActionTarget*>(target_);
1492 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); 1532 EXPECT_EQ(1, num_success_);
1493 EXPECT_EQ(pointer_touch_target->positions(0), params.position()); 1533 EXPECT_EQ(0, num_failure_);
1534 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0));
1535 EXPECT_EQ(pointer_touch_target->positions(0), params1.position());
1494 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); 1536 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed);
1495 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); 1537 ASSERT_EQ(pointer_touch_target->touch_length(), 1U);
1538 EXPECT_EQ(PROCESSED, action_state_);
1496 1539
1497 params.set_index(1); 1540 // Send a touch move for the first finger and a touch press for the second
1498 params.set_position(gfx::PointF(79, 132)); 1541 // finger.
1499 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); 1542 params1.set_pointer_action_type(
1500 QueueSyntheticGesture(std::move(gesture)); 1543 SyntheticPointerActionParams::PointerActionType::MOVE);
1544 params1.set_position(gfx::PointF(133, 156));
1545 QueueSyntheticPointerAction(params1);
1546 SyntheticPointerActionParams params2 = SyntheticPointerActionParams(
1547 SyntheticPointerActionParams::PointerActionType::PRESS);
1548 params2.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1549 params2.set_index(1);
1550 params2.set_position(gfx::PointF(79, 132));
1551 QueueSyntheticPointerAction(params2);
1552 QueueSyntheticPointerAction(process_params);
1501 FlushInputUntilComplete(); 1553 FlushInputUntilComplete();
1502 1554
1503 pointer_touch_target = 1555 pointer_touch_target =
1504 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1556 static_cast<MockSyntheticPointerTouchActionTarget*>(target_);
1505 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); 1557 EXPECT_EQ(2, num_success_);
1506 EXPECT_EQ(pointer_touch_target->indexes(1), params.index()); 1558 EXPECT_EQ(0, num_failure_);
1507 EXPECT_EQ(pointer_touch_target->positions(1), params.position()); 1559 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0));
1560 EXPECT_EQ(pointer_touch_target->positions(0), params1.position());
1561 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateMoved);
1562 EXPECT_EQ(pointer_touch_target->indexes(1), PointerIndex(1));
1563 EXPECT_EQ(pointer_touch_target->positions(1), params2.position());
1508 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed); 1564 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed);
1509 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); 1565 ASSERT_EQ(pointer_touch_target->touch_length(), 2U);
1566 EXPECT_EQ(PROCESSED, action_state_);
1510 1567
1511 params.set_pointer_action_type( 1568 // Send touch releases for both fingers.
1512 SyntheticPointerActionParams::PointerActionType::MOVE); 1569 params1.set_pointer_action_type(
1513 params.set_position(gfx::PointF(133, 156)); 1570 SyntheticPointerActionParams::PointerActionType::RELEASE);
1514 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); 1571 QueueSyntheticPointerAction(params1);
1515 QueueSyntheticGesture(std::move(gesture)); 1572 params2.set_pointer_action_type(
1573 SyntheticPointerActionParams::PointerActionType::RELEASE);
1574 QueueSyntheticPointerAction(params2);
1575 QueueSyntheticPointerAction(process_params);
1516 FlushInputUntilComplete(); 1576 FlushInputUntilComplete();
1517 1577
1518 pointer_touch_target = 1578 EXPECT_EQ(3, num_success_);
1519 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1579 EXPECT_EQ(0, num_failure_);
1520 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchMove); 1580 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0));
1521 EXPECT_EQ(pointer_touch_target->positions(1), params.position()); 1581 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateReleased);
1522 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateMoved); 1582 EXPECT_EQ(pointer_touch_target->indexes(1), PointerIndex(1));
1583 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased);
1523 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); 1584 ASSERT_EQ(pointer_touch_target->touch_length(), 2U);
1585 EXPECT_EQ(PROCESSED, action_state_);
1524 1586
1525 params.set_pointer_action_type( 1587 // Send a finish action to notify synthetic gesture controller the whole
1526 SyntheticPointerActionParams::PointerActionType::RELEASE); 1588 // pointer action sequence has been handled.
1527 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); 1589 SyntheticPointerActionParams finish_params = SyntheticPointerActionParams(
1528 QueueSyntheticGesture(std::move(gesture)); 1590 SyntheticPointerActionParams::PointerActionType::FINISH);
1591 finish_params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1592 QueueSyntheticPointerAction(finish_params);
1529 FlushInputUntilComplete(); 1593 FlushInputUntilComplete();
1530 1594
1531 pointer_touch_target = 1595 EXPECT_EQ(4, num_success_);
1532 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1596 EXPECT_EQ(0, num_failure_);
1533 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchEnd); 1597 EXPECT_EQ(FINISHED, action_state_);
1534 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased);
1535 } 1598 }
1536 1599
1537 } // namespace 1600 } // namespace
1538 1601
1539 } // namespace content 1602 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698