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

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: Move logic to controller 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 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 475
476 class MockSyntheticPointerActionTarget : public MockSyntheticGestureTarget { 476 class MockSyntheticPointerActionTarget : public MockSyntheticGestureTarget {
477 public: 477 public:
478 MockSyntheticPointerActionTarget() {} 478 MockSyntheticPointerActionTarget() {}
479 ~MockSyntheticPointerActionTarget() override {} 479 ~MockSyntheticPointerActionTarget() override {}
480 480
481 gfx::PointF positions(int index) const { return positions_[index]; } 481 gfx::PointF positions(int index) const { return positions_[index]; }
482 int indexes(int index) const { return indexes_[index]; } 482 int indexes(int index) const { return indexes_[index]; }
483 WebTouchPoint::State states(int index) { return states_[index]; } 483 WebTouchPoint::State states(int index) { return states_[index]; }
484 unsigned touch_length() const { return touch_length_; } 484 unsigned touch_length() const { return touch_length_; }
485 WebInputEvent::Type type() const { return type_; }
486 485
487 protected: 486 protected:
488 gfx::PointF positions_[kTouchPointersLength]; 487 gfx::PointF positions_[kTouchPointersLength];
489 unsigned touch_length_; 488 unsigned touch_length_;
490 int indexes_[kTouchPointersLength]; 489 int indexes_[kTouchPointersLength];
491 WebTouchPoint::State states_[kTouchPointersLength]; 490 WebTouchPoint::State states_[kTouchPointersLength];
492 WebInputEvent::Type type_;
493 }; 491 };
494 492
495 class MockSyntheticPointerTouchActionTarget 493 class MockSyntheticPointerTouchActionTarget
496 : public MockSyntheticPointerActionTarget { 494 : public MockSyntheticPointerActionTarget {
497 public: 495 public:
498 MockSyntheticPointerTouchActionTarget() {} 496 MockSyntheticPointerTouchActionTarget() {}
499 ~MockSyntheticPointerTouchActionTarget() override {} 497 ~MockSyntheticPointerTouchActionTarget() override {}
500 498
501 void DispatchInputEventToPlatform(const WebInputEvent& event) override { 499 void DispatchInputEventToPlatform(const WebInputEvent& event) override {
502 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); 500 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type));
503 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); 501 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) { 502 for (size_t i = 0; i < touch_event.touchesLength; ++i) {
506 indexes_[i] = touch_event.touches[i].id; 503 indexes_[i] = touch_event.touches[i].id;
507 positions_[i] = gfx::PointF(touch_event.touches[i].position); 504 positions_[i] = gfx::PointF(touch_event.touches[i].position);
508 states_[i] = touch_event.touches[i].state; 505 states_[i] = touch_event.touches[i].state;
509 } 506 }
510 touch_length_ = touch_event.touchesLength; 507 touch_length_ = touch_event.touchesLength;
511 } 508 }
512 }; 509 };
513 510
514 class SyntheticGestureControllerTestBase { 511 class SyntheticGestureControllerTestBase {
(...skipping 10 matching lines...) Expand all
525 } 522 }
526 523
527 void QueueSyntheticGesture(std::unique_ptr<SyntheticGesture> gesture) { 524 void QueueSyntheticGesture(std::unique_ptr<SyntheticGesture> gesture) {
528 controller_->QueueSyntheticGesture( 525 controller_->QueueSyntheticGesture(
529 std::move(gesture), 526 std::move(gesture),
530 base::Bind( 527 base::Bind(
531 &SyntheticGestureControllerTestBase::OnSyntheticGestureCompleted, 528 &SyntheticGestureControllerTestBase::OnSyntheticGestureCompleted,
532 base::Unretained(this))); 529 base::Unretained(this)));
533 } 530 }
534 531
532 void QueueSyntheticPointerAction(
533 const SyntheticPointerActionParams& gesture_params) {
534 controller_->QueueSyntheticPointerAction(
535 gesture_params, base::Bind(&SyntheticGestureControllerTestBase::
536 OnSyntheticPointerActionCompleted,
537 base::Unretained(this)));
538 }
539
535 void FlushInputUntilComplete() { 540 void FlushInputUntilComplete() {
536 while (target_->flush_requested()) { 541 while (target_->flush_requested()) {
537 while (target_->flush_requested()) { 542 while (target_->flush_requested()) {
538 target_->ClearFlushRequest(); 543 target_->ClearFlushRequest();
539 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs); 544 time_ += base::TimeDelta::FromMilliseconds(kFlushInputRateInMs);
540 controller_->Flush(time_); 545 controller_->Flush(time_);
541 } 546 }
542 controller_->OnDidFlushInput(); 547 controller_->OnDidFlushInput();
543 } 548 }
544 } 549 }
545 550
546 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { 551 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) {
547 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); 552 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING);
548 if (result == SyntheticGesture::GESTURE_FINISHED) 553 if (result == SyntheticGesture::GESTURE_FINISHED)
549 num_success_++; 554 num_success_++;
550 else 555 else
551 num_failure_++; 556 num_failure_++;
552 } 557 }
553 558
559 void OnSyntheticPointerActionCompleted(SyntheticGesture::Result result) {
560 if (result == SyntheticGesture::POINTER_ACTION_PROCESSED) {
561 num_success_++;
562 action_state_ = PROCESSED;
563 } else if (result == SyntheticGesture::POINTER_ACTION_FINISHED) {
564 num_success_++;
565 action_state_ = FINISHED;
566 } else {
567 num_failure_++;
568 action_state_ = OTHER;
569 }
570 }
571
554 base::TimeDelta GetTotalTime() const { return time_ - start_time_; } 572 base::TimeDelta GetTotalTime() const { return time_ - start_time_; }
555 573
574 int PointerIndex(int index) const { return controller_->PointerIndex(index); }
575
576 enum PointerActionState { PROCESSED, FINISHED, OTHER };
577
556 MockSyntheticGestureTarget* target_; 578 MockSyntheticGestureTarget* target_;
557 std::unique_ptr<SyntheticGestureController> controller_; 579 std::unique_ptr<SyntheticGestureController> controller_;
558 base::TimeTicks start_time_; 580 base::TimeTicks start_time_;
559 base::TimeTicks time_; 581 base::TimeTicks time_;
560 int num_success_; 582 int num_success_;
561 int num_failure_; 583 int num_failure_;
584 PointerActionState action_state_;
562 }; 585 };
563 586
564 class SyntheticGestureControllerTest 587 class SyntheticGestureControllerTest
565 : public SyntheticGestureControllerTestBase, 588 : public SyntheticGestureControllerTestBase,
566 public testing::Test { 589 public testing::Test {
567 protected: 590 protected:
568 void SetUp() override { 591 void SetUp() override {
569 start_time_ = base::TimeTicks::Now(); 592 start_time_ = base::TimeTicks::Now();
570 time_ = start_time_; 593 time_ = start_time_;
571 num_success_ = 0; 594 num_success_ = 0;
572 num_failure_ = 0; 595 num_failure_ = 0;
596 action_state_ = OTHER;
573 } 597 }
574 598
575 void TearDown() override { 599 void TearDown() override {
576 controller_.reset(); 600 controller_.reset();
577 target_ = nullptr; 601 target_ = nullptr;
578 time_ = base::TimeTicks(); 602 time_ = base::TimeTicks();
579 } 603 }
580 }; 604 };
581 605
582 class SyntheticGestureControllerTestWithParam 606 class SyntheticGestureControllerTestWithParam
583 : public SyntheticGestureControllerTestBase, 607 : public SyntheticGestureControllerTestBase,
584 public testing::TestWithParam<bool> { 608 public testing::TestWithParam<bool> {
585 protected: 609 protected:
586 void SetUp() override { 610 void SetUp() override {
587 start_time_ = base::TimeTicks::Now(); 611 start_time_ = base::TimeTicks::Now();
588 time_ = start_time_; 612 time_ = start_time_;
589 num_success_ = 0; 613 num_success_ = 0;
590 num_failure_ = 0; 614 num_failure_ = 0;
615 action_state_ = OTHER;
591 } 616 }
592 617
593 void TearDown() override { 618 void TearDown() override {
594 controller_.reset(); 619 controller_.reset();
595 target_ = nullptr; 620 target_ = nullptr;
596 time_ = base::TimeTicks(); 621 time_ = base::TimeTicks();
597 } 622 }
598 }; 623 };
599 624
600 TEST_F(SyntheticGestureControllerTest, SingleGesture) { 625 TEST_F(SyntheticGestureControllerTest, SingleGesture) {
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after
1468 EXPECT_TRUE(tap_target->GestureFinished()); 1493 EXPECT_TRUE(tap_target->GestureFinished());
1469 EXPECT_EQ(tap_target->position(), params.position); 1494 EXPECT_EQ(tap_target->position(), params.position);
1470 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms); 1495 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms);
1471 EXPECT_GE(GetTotalTime(), 1496 EXPECT_GE(GetTotalTime(),
1472 base::TimeDelta::FromMilliseconds(params.duration_ms)); 1497 base::TimeDelta::FromMilliseconds(params.duration_ms));
1473 } 1498 }
1474 1499
1475 TEST_F(SyntheticGestureControllerTest, PointerTouchAction) { 1500 TEST_F(SyntheticGestureControllerTest, PointerTouchAction) {
1476 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); 1501 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>();
1477 1502
1478 SyntheticPointerActionParams params = SyntheticPointerActionParams( 1503 SyntheticPointerActionParams params1 = SyntheticPointerActionParams(
1479 SyntheticPointerActionParams::PointerActionType::PRESS); 1504 SyntheticPointerActionParams::PointerActionType::PRESS);
1480 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; 1505 params1.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1481 params.set_index(0); 1506 params1.set_index(0);
1482 params.set_position(gfx::PointF(54, 89)); 1507 params1.set_position(gfx::PointF(54, 89));
1483 SyntheticTouchPointer synthetic_pointer; 1508 SyntheticTouchPointer synthetic_pointer;
1484 1509
1485 std::unique_ptr<SyntheticPointerAction> gesture( 1510 // Send a touch press for one finger.
1486 new SyntheticPointerAction(params, &synthetic_pointer)); 1511 SyntheticPointerActionParams process_params = SyntheticPointerActionParams(
1487 QueueSyntheticGesture(std::move(gesture)); 1512 SyntheticPointerActionParams::PointerActionType::PROCESS);
1513 process_params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1514 QueueSyntheticPointerAction(params1);
1515 QueueSyntheticPointerAction(process_params);
1488 FlushInputUntilComplete(); 1516 FlushInputUntilComplete();
1489 1517
1490 MockSyntheticPointerTouchActionTarget* pointer_touch_target = 1518 MockSyntheticPointerTouchActionTarget* pointer_touch_target =
1491 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1519 static_cast<MockSyntheticPointerTouchActionTarget*>(target_);
1492 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); 1520 EXPECT_EQ(1, num_success_);
1493 EXPECT_EQ(pointer_touch_target->positions(0), params.position()); 1521 EXPECT_EQ(0, num_failure_);
1522 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0));
1523 EXPECT_EQ(pointer_touch_target->positions(0), params1.position());
1494 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); 1524 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed);
1495 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); 1525 ASSERT_EQ(pointer_touch_target->touch_length(), 1U);
1526 EXPECT_EQ(PROCESSED, action_state_);
1496 1527
1497 params.set_index(1); 1528 // Send a touch move for the first finger and a touch press for the second
1498 params.set_position(gfx::PointF(79, 132)); 1529 // finger.
1499 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); 1530 params1.set_pointer_action_type(
1500 QueueSyntheticGesture(std::move(gesture)); 1531 SyntheticPointerActionParams::PointerActionType::MOVE);
1532 params1.set_position(gfx::PointF(133, 156));
1533 QueueSyntheticPointerAction(params1);
1534 SyntheticPointerActionParams params2 = SyntheticPointerActionParams(
1535 SyntheticPointerActionParams::PointerActionType::PRESS);
1536 params2.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1537 params2.set_index(1);
1538 params2.set_position(gfx::PointF(79, 132));
1539 QueueSyntheticPointerAction(params2);
1540 QueueSyntheticPointerAction(process_params);
1501 FlushInputUntilComplete(); 1541 FlushInputUntilComplete();
1502 1542
1503 pointer_touch_target = 1543 pointer_touch_target =
1504 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1544 static_cast<MockSyntheticPointerTouchActionTarget*>(target_);
1505 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); 1545 EXPECT_EQ(2, num_success_);
1506 EXPECT_EQ(pointer_touch_target->indexes(1), params.index()); 1546 EXPECT_EQ(0, num_failure_);
1507 EXPECT_EQ(pointer_touch_target->positions(1), params.position()); 1547 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0));
1548 EXPECT_EQ(pointer_touch_target->positions(0), params1.position());
1549 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateMoved);
1550 EXPECT_EQ(pointer_touch_target->indexes(1), PointerIndex(1));
1551 EXPECT_EQ(pointer_touch_target->positions(1), params2.position());
1508 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed); 1552 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed);
1509 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); 1553 ASSERT_EQ(pointer_touch_target->touch_length(), 2U);
1554 EXPECT_EQ(PROCESSED, action_state_);
1510 1555
1511 params.set_pointer_action_type( 1556 // Send touch releases for both fingers.
1512 SyntheticPointerActionParams::PointerActionType::MOVE); 1557 params1.set_pointer_action_type(
1513 params.set_position(gfx::PointF(133, 156)); 1558 SyntheticPointerActionParams::PointerActionType::RELEASE);
1514 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); 1559 QueueSyntheticPointerAction(params1);
1515 QueueSyntheticGesture(std::move(gesture)); 1560 params2.set_pointer_action_type(
1561 SyntheticPointerActionParams::PointerActionType::RELEASE);
1562 QueueSyntheticPointerAction(params2);
1563 QueueSyntheticPointerAction(process_params);
1516 FlushInputUntilComplete(); 1564 FlushInputUntilComplete();
1517 1565
1518 pointer_touch_target = 1566 EXPECT_EQ(3, num_success_);
1519 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1567 EXPECT_EQ(0, num_failure_);
1520 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchMove); 1568 EXPECT_EQ(pointer_touch_target->indexes(0), PointerIndex(0));
1521 EXPECT_EQ(pointer_touch_target->positions(1), params.position()); 1569 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateReleased);
1522 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateMoved); 1570 EXPECT_EQ(pointer_touch_target->indexes(1), PointerIndex(1));
1571 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased);
1523 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); 1572 ASSERT_EQ(pointer_touch_target->touch_length(), 2U);
1573 EXPECT_EQ(PROCESSED, action_state_);
1524 1574
1525 params.set_pointer_action_type( 1575 // Send a finish action to notify synthetic gesture controller the whole
1526 SyntheticPointerActionParams::PointerActionType::RELEASE); 1576 // pointer action sequence has been handled.
1527 gesture.reset(new SyntheticPointerAction(params, &synthetic_pointer)); 1577 SyntheticPointerActionParams finish_params = SyntheticPointerActionParams(
1528 QueueSyntheticGesture(std::move(gesture)); 1578 SyntheticPointerActionParams::PointerActionType::FINISH);
1579 finish_params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
1580 QueueSyntheticPointerAction(finish_params);
1529 FlushInputUntilComplete(); 1581 FlushInputUntilComplete();
1530 1582
1531 pointer_touch_target = 1583 EXPECT_EQ(4, num_success_);
1532 static_cast<MockSyntheticPointerTouchActionTarget*>(target_); 1584 EXPECT_EQ(0, num_failure_);
1533 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchEnd); 1585 EXPECT_EQ(FINISHED, action_state_);
1534 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased);
1535 } 1586 }
1536 1587
1537 } // namespace 1588 } // namespace
1538 1589
1539 } // namespace content 1590 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698