1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/input/fling_controller.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "components/input/features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/blink/fling_booster.h"
#include "ui/events/gestures/physics_based_fling_curve.h"
using blink::WebGestureEvent;
using blink::WebInputEvent;
using blink::WebMouseWheelEvent;
using ui::PhysicsBasedFlingCurve;
namespace {
constexpr double kFrameDelta = 1000.0 / 60.0;
} // namespace
namespace input {
class FakeFlingController : public FlingController {
public:
FakeFlingController(FlingControllerEventSenderClient* event_sender_client,
FlingControllerSchedulerClient* scheduler_client,
const Config& config)
: FlingController(event_sender_client, scheduler_client, config) {}
};
class FlingControllerTest : public FlingControllerEventSenderClient,
public FlingControllerSchedulerClient,
public testing::TestWithParam<bool> {
public:
// testing::Test
FlingControllerTest()
: needs_begin_frame_for_fling_progress_(GetParam()),
task_environment_(base::test::TaskEnvironment::MainThreadType::UI) {}
FlingControllerTest(const FlingControllerTest&) = delete;
FlingControllerTest& operator=(const FlingControllerTest&) = delete;
~FlingControllerTest() override = default;
void SetUp() override {
fling_controller_ = std::make_unique<FakeFlingController>(
this, this, FlingController::Config());
fling_controller_->set_clock_for_testing(&mock_clock_);
AdvanceTime();
}
// FlingControllerEventSenderClient
void SendGeneratedWheelEvent(
const MouseWheelEventWithLatencyInfo& wheel_event) override {
wheel_event_count_++;
last_sent_wheel_ = wheel_event.event;
first_wheel_event_sent_ = true;
if (wheel_event.event.momentum_phase == WebMouseWheelEvent::kPhaseEnded)
first_wheel_event_sent_ = false;
}
void SendGeneratedGestureScrollEvents(
const GestureEventWithLatencyInfo& gesture_event) override {
fling_controller_->ObserveAndMaybeConsumeGestureEvent(gesture_event);
sent_scroll_gesture_count_++;
last_sent_gesture_ = gesture_event.event;
}
gfx::Size GetRootWidgetViewportSize() override {
return gfx::Size(1920, 1080);
}
// FlingControllerSchedulerClient
void ScheduleFlingProgress(
base::WeakPtr<FlingController> fling_controller) override {
DCHECK(!scheduled_next_fling_progress_);
scheduled_next_fling_progress_ = true;
}
void DidStopFlingingOnBrowser(
base::WeakPtr<FlingController> fling_controller) override {
notified_client_after_fling_stop_ = true;
}
bool NeedsBeginFrameForFlingProgress() override {
return needs_begin_frame_for_fling_progress_;
}
bool ShouldUseMobileFlingCurve() override {
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
return true;
#else
return false;
#endif
}
gfx::Vector2dF GetPixelsPerInch(
const gfx::PointF& position_in_screen) override {
return gfx::Vector2dF(kDefaultPixelsPerInch, kDefaultPixelsPerInch);
}
void SimulateFlingStart(blink::WebGestureDevice source_device,
const gfx::Vector2dF& velocity,
bool wait_before_processing = true) {
scheduled_next_fling_progress_ = false;
sent_scroll_gesture_count_ = 0;
WebGestureEvent fling_start(WebInputEvent::Type::kGestureFlingStart, 0,
NowTicks(), source_device);
fling_start.data.fling_start.velocity_x = velocity.x();
fling_start.data.fling_start.velocity_y = velocity.y();
GestureEventWithLatencyInfo fling_start_with_latency(fling_start);
if (wait_before_processing) {
// Wait for up to one frame before processing the event.
AdvanceTime(base::RandInt(0, static_cast<int>(kFrameDelta)));
}
fling_controller_->ObserveAndMaybeConsumeGestureEvent(
fling_start_with_latency);
}
void SimulateScrollBegin(blink::WebGestureDevice source_device,
const gfx::Vector2dF& delta) {
WebGestureEvent scroll_begin(WebInputEvent::Type::kGestureScrollBegin, 0,
NowTicks(), source_device);
scroll_begin.data.scroll_begin.delta_x_hint = delta.x();
scroll_begin.data.scroll_begin.delta_y_hint = delta.y();
scroll_begin.data.scroll_begin.inertial_phase =
WebGestureEvent::InertialPhaseState::kNonMomentum;
scroll_begin.data.scroll_begin.delta_hint_units =
ui::ScrollGranularity::kScrollByPrecisePixel;
GestureEventWithLatencyInfo scroll_begin_with_latency(scroll_begin);
fling_controller_->ObserveAndMaybeConsumeGestureEvent(
scroll_begin_with_latency);
}
void SimulateScrollUpdate(blink::WebGestureDevice source_device,
const gfx::Vector2dF& delta) {
WebGestureEvent scroll_update(WebInputEvent::Type::kGestureScrollUpdate, 0,
NowTicks(), source_device);
scroll_update.data.scroll_update.delta_x = delta.x();
scroll_update.data.scroll_update.delta_y = delta.y();
scroll_update.data.scroll_update.inertial_phase =
WebGestureEvent::InertialPhaseState::kNonMomentum;
scroll_update.data.scroll_update.delta_units =
ui::ScrollGranularity::kScrollByPrecisePixel;
GestureEventWithLatencyInfo scroll_update_with_latency(
scroll_update);
fling_controller_->ObserveAndMaybeConsumeGestureEvent(
scroll_update_with_latency);
}
void SimulateFlingCancel(blink::WebGestureDevice source_device) {
notified_client_after_fling_stop_ = false;
WebGestureEvent fling_cancel(WebInputEvent::Type::kGestureFlingCancel, 0,
NowTicks(), source_device);
// autoscroll fling cancel doesn't allow fling boosting.
if (source_device == blink::WebGestureDevice::kSyntheticAutoscroll)
fling_cancel.data.fling_cancel.prevent_boosting = true;
GestureEventWithLatencyInfo fling_cancel_with_latency(fling_cancel);
fling_controller_->ObserveAndMaybeConsumeGestureEvent(
fling_cancel_with_latency);
}
void ProgressFling(base::TimeTicks current_time,
std::optional<base::TimeTicks>
first_coalesced_frame_begin_time = std::nullopt) {
DCHECK(scheduled_next_fling_progress_);
scheduled_next_fling_progress_ = false;
fling_controller_->ProgressFling(current_time,
first_coalesced_frame_begin_time);
}
bool FlingInProgress() { return fling_controller_->fling_in_progress(); }
void AdvanceTime(double time_delta_ms = kFrameDelta) {
mock_clock_.Advance(base::Milliseconds(time_delta_ms));
}
base::TimeTicks NowTicks() const { return mock_clock_.NowTicks(); }
float CompleteFlingAndAccumulateScrollDelta() {
float total_scroll_delta = 0.f;
// Sometimes SendGeneratedGestureScrollEvents is not run because fling is
// not advanced. This is due to the first |FlingScheduler::OnAnimationStep|
// call having the time of the last frame before AddAnimationObserver.
// Please see comment in |FlingController::ProgressFling|. This leaves the
// last_sent_gesture as a GSE. We therefore don't accrue delta in this case
if (last_sent_gesture_.GetType() !=
WebInputEvent::Type::kGestureScrollEnd) {
DCHECK(last_sent_gesture_.GetType() ==
WebInputEvent::Type::kGestureScrollUpdate);
total_scroll_delta += last_sent_gesture_.data.scroll_update.delta_x;
}
while (true) {
AdvanceTime();
ProgressFling(NowTicks());
if (last_sent_gesture_.GetType() ==
WebInputEvent::Type::kGestureScrollEnd) {
break;
} else {
DCHECK(last_sent_gesture_.GetType() ==
WebInputEvent::Type::kGestureScrollUpdate);
total_scroll_delta += last_sent_gesture_.data.scroll_update.delta_x;
}
}
return total_scroll_delta;
}
protected:
std::unique_ptr<FakeFlingController> fling_controller_;
int wheel_event_count_ = 0;
WebMouseWheelEvent last_sent_wheel_;
WebGestureEvent last_sent_gesture_;
bool scheduled_next_fling_progress_ = false;
bool notified_client_after_fling_stop_ = false;
bool first_wheel_event_sent_ = false;
int sent_scroll_gesture_count_ = 0;
private:
base::SimpleTestTickClock mock_clock_;
// This determines whether the platform ticks fling animations using
// SetNeedsBeginFrame (i.e. WebView). If true, we should avoid calling
// ProgressFling immediately after a FlingStart since this will match the
// behavior in FlingController::ProcessGestureFlingStart. See
// https://crrev.com/c/1181521.
bool needs_begin_frame_for_fling_progress_;
base::test::TaskEnvironment task_environment_;
};
INSTANTIATE_TEST_SUITE_P(All, FlingControllerTest, testing::Bool());
TEST_P(FlingControllerTest,
ControllerSendsWheelEndOnTouchpadFlingWithZeroVelocity) {
SimulateFlingStart(blink::WebGestureDevice::kTouchpad, gfx::Vector2dF());
// The controller doesn't start a fling and sends a wheel end event
// immediately.
EXPECT_FALSE(FlingInProgress());
EXPECT_EQ(WebMouseWheelEvent::kPhaseEnded, last_sent_wheel_.momentum_phase);
EXPECT_EQ(0.f, last_sent_wheel_.delta_x);
EXPECT_EQ(0.f, last_sent_wheel_.delta_y);
}
TEST_P(FlingControllerTest,
ControllerSendsGSEOnTouchscreenFlingWithZeroVelocity) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen, gfx::Vector2dF());
// The controller doesn't start a fling and sends a GSE immediately.
EXPECT_FALSE(FlingInProgress());
EXPECT_EQ(WebInputEvent::Type::kGestureScrollEnd,
last_sent_gesture_.GetType());
}
TEST_P(FlingControllerTest, ControllerHandlesTouchpadGestureFling) {
SimulateFlingStart(blink::WebGestureDevice::kTouchpad,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
// Processing GFS will send the first fling progress event if the time delta
// between the timestamp of the GFS and the time that ProcessGestureFlingStart
// is called is large enough.
bool process_GFS_sent_first_event = first_wheel_event_sent_;
AdvanceTime();
ProgressFling(NowTicks());
if (!process_GFS_sent_first_event) {
EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, last_sent_wheel_.momentum_phase);
} else {
EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged,
last_sent_wheel_.momentum_phase);
}
EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
// The rest of the wheel events must have momentum_phase == KPhaseChanged.
AdvanceTime();
ProgressFling(NowTicks());
EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged, last_sent_wheel_.momentum_phase);
EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
// Now cancel the fling.
SimulateFlingCancel(blink::WebGestureDevice::kTouchpad);
EXPECT_FALSE(FlingInProgress());
}
// Ensure that the start time of a fling is measured from the last received
// GSU. This ensures that the first progress fling during FlingStart should
// send significant delta. If we're using the FlingStart as the start time, we
// would send none or very little delta.
TEST_P(FlingControllerTest, FlingStartsAtLastScrollUpdate) {
SimulateScrollUpdate(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
double time_to_advance_ms = 30.0;
AdvanceTime(time_to_advance_ms);
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0), /*wait_before_processing=*/false);
EXPECT_TRUE(FlingInProgress());
if (NeedsBeginFrameForFlingProgress())
ProgressFling(NowTicks());
// We haven't advanced time since the FlingStart. Ensure we still send a
// significant amount of delta (~0.030sec * 1000pixels/sec) since we should
// be measuring the time since the last GSU.
EXPECT_EQ(1, sent_scroll_gesture_count_);
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_NEAR(last_sent_gesture_.data.scroll_update.delta_x, 30.0, 5);
}
// Tests that when a fling is interrupted (e.g. by having reached the end of
// the content), a subsequent fling isn't boosted. An example here would be an
// infinite scroller that loads more content after hitting the scroll extent.
TEST_P(FlingControllerTest, InterruptedFlingIsntBoosted) {
double time_to_advance_ms = 8.0;
// Start an ordinary fling.
{
AdvanceTime(time_to_advance_ms);
SimulateScrollBegin(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(10, 0));
SimulateScrollUpdate(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(10, 0));
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0),
/*wait_before_processing=*/false);
ASSERT_TRUE(FlingInProgress());
if (NeedsBeginFrameForFlingProgress())
ProgressFling(NowTicks());
}
// Stop the fling. This simulates hitting a scroll extent.
{
ASSERT_EQ(fling_controller_->CurrentFlingVelocity().x(), 1000);
fling_controller_->StopFling();
}
// Now perform a second fling (e.g. after an infinite scroller loads more
// content). Ensure it isn't boosted since the previous fling was
// interrupted.
{
AdvanceTime(time_to_advance_ms);
SimulateScrollBegin(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(10, 0));
SimulateScrollUpdate(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(10, 0));
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0),
/*wait_before_processing=*/false);
if (NeedsBeginFrameForFlingProgress())
ProgressFling(NowTicks());
EXPECT_EQ(fling_controller_->CurrentFlingVelocity().x(), 1000)
<< "Fling was boosted but should not have been.";
}
}
TEST_P(FlingControllerTest, ControllerHandlesTouchscreenGestureFling) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
// The fling progress will generate and send GSU events with inertial state.
AdvanceTime();
ProgressFling(NowTicks());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
// Now cancel the fling.
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
EXPECT_FALSE(FlingInProgress());
// Cancellation should send a GSE.
EXPECT_FALSE(FlingInProgress());
EXPECT_EQ(WebInputEvent::Type::kGestureScrollEnd,
last_sent_gesture_.GetType());
}
TEST_P(FlingControllerTest, ControllerSendsWheelEndWhenTouchpadFlingIsOver) {
SimulateFlingStart(blink::WebGestureDevice::kTouchpad,
gfx::Vector2dF(100, 0));
EXPECT_TRUE(FlingInProgress());
// Processing GFS will send the first fling progress event if the time delta
// between the timestamp of the GFS and the time that ProcessGestureFlingStart
// is called is large enough.
bool process_GFS_sent_first_event = first_wheel_event_sent_;
AdvanceTime();
ProgressFling(NowTicks());
if (!process_GFS_sent_first_event) {
EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, last_sent_wheel_.momentum_phase);
} else {
EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged,
last_sent_wheel_.momentum_phase);
}
EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
AdvanceTime();
ProgressFling(NowTicks());
while (FlingInProgress()) {
EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged,
last_sent_wheel_.momentum_phase);
EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
AdvanceTime();
ProgressFling(NowTicks());
}
EXPECT_EQ(WebMouseWheelEvent::kPhaseEnded, last_sent_wheel_.momentum_phase);
EXPECT_EQ(0.f, last_sent_wheel_.delta_x);
EXPECT_EQ(0.f, last_sent_wheel_.delta_y);
}
TEST_P(FlingControllerTest, ControllerSendsGSEWhenTouchscreenFlingIsOver) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(100, 0));
EXPECT_TRUE(FlingInProgress());
AdvanceTime();
ProgressFling(NowTicks());
while (FlingInProgress()) {
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
AdvanceTime();
ProgressFling(NowTicks());
}
EXPECT_EQ(WebInputEvent::Type::kGestureScrollEnd,
last_sent_gesture_.GetType());
}
TEST_P(FlingControllerTest, EarlyTouchpadFlingCancelationOnFlingStop) {
SimulateFlingStart(blink::WebGestureDevice::kTouchpad,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
// Processing GFS will send the first fling progress event if the time delta
// between the timestamp of the GFS and the time that ProcessGestureFlingStart
// is called is large enough.
bool process_GFS_sent_first_event = first_wheel_event_sent_;
AdvanceTime();
ProgressFling(NowTicks());
if (!process_GFS_sent_first_event) {
EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, last_sent_wheel_.momentum_phase);
} else {
EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged,
last_sent_wheel_.momentum_phase);
}
EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
fling_controller_->StopFling();
EXPECT_FALSE(FlingInProgress());
EXPECT_EQ(WebMouseWheelEvent::kPhaseEnded, last_sent_wheel_.momentum_phase);
EXPECT_EQ(0.f, last_sent_wheel_.delta_x);
EXPECT_EQ(0.f, last_sent_wheel_.delta_y);
}
TEST_P(FlingControllerTest, EarlyTouchscreenFlingCancelationOnFlingStop) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
// progress fling must send GSU events.
AdvanceTime();
ProgressFling(NowTicks());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
fling_controller_->StopFling();
EXPECT_FALSE(FlingInProgress());
EXPECT_EQ(WebInputEvent::Type::kGestureScrollEnd,
last_sent_gesture_.GetType());
}
TEST_P(FlingControllerTest, GestureFlingCancelOutsideFling) {
// FlingCancel without a FlingStart doesn't cause issues, doesn't send any
// events.
{
int current_sent_scroll_gesture_count = sent_scroll_gesture_count_;
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
EXPECT_FALSE(FlingInProgress());
EXPECT_EQ(current_sent_scroll_gesture_count, sent_scroll_gesture_count_);
}
// Do a fling and cancel it. Make sure another cancel is also a no-op.
{
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
AdvanceTime();
ProgressFling(NowTicks());
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
int current_sent_scroll_gesture_count = sent_scroll_gesture_count_;
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
EXPECT_EQ(current_sent_scroll_gesture_count, sent_scroll_gesture_count_);
}
}
TEST_P(FlingControllerTest, GestureFlingNotCancelledBySmallTimeDelta) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0), false);
EXPECT_TRUE(FlingInProgress());
int current_sent_scroll_gesture_count = sent_scroll_gesture_count_;
// If we the first progress tick happens too close to the fling_start time,
// the controller won't send any GSU events, but the fling is still active.
ProgressFling(NowTicks());
EXPECT_EQ(current_sent_scroll_gesture_count, sent_scroll_gesture_count_);
EXPECT_TRUE(FlingInProgress());
// The rest of the progress flings must advance the fling normally.
AdvanceTime();
ProgressFling(NowTicks());
EXPECT_EQ(blink::WebGestureDevice::kTouchscreen,
last_sent_gesture_.SourceDevice());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
}
TEST_P(FlingControllerTest, GestureFlingWithNegativeTimeDelta) {
base::TimeTicks initial_time = NowTicks();
AdvanceTime();
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
int current_sent_scroll_gesture_count = sent_scroll_gesture_count_;
// If we get a negative time delta, that is, the Progress tick time happens
// before the fling's start time then we should *not* try progressing the
// fling.
ProgressFling(initial_time);
EXPECT_EQ(current_sent_scroll_gesture_count, sent_scroll_gesture_count_);
// The rest of the progress flings must advance the fling normally.
AdvanceTime();
ProgressFling(NowTicks());
EXPECT_EQ(blink::WebGestureDevice::kTouchscreen,
last_sent_gesture_.SourceDevice());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
}
// Regression test for https://crbug.com/924279
TEST_P(FlingControllerTest, TouchpadFlingWithOldEvent) {
// Only the code path that uses compositor animation observers is affected.
if (NeedsBeginFrameForFlingProgress())
return;
// Create a fling start event.
base::TimeTicks event_time = NowTicks();
WebGestureEvent fling_start(WebInputEvent::Type::kGestureFlingStart, 0,
event_time, blink::WebGestureDevice::kTouchpad);
fling_start.data.fling_start.velocity_x = 0.f;
fling_start.data.fling_start.velocity_y = -1000.f;
GestureEventWithLatencyInfo fling_start_with_latency(fling_start);
// Move time forward. Assume a frame occurs here.
AdvanceTime(1.f);
base::TimeTicks last_frame_time = NowTicks();
// Start the fling animation later, as if there was a delay in event dispatch.
AdvanceTime(1.f);
fling_controller_->ProcessGestureFlingStart(fling_start_with_latency);
EXPECT_TRUE(FlingInProgress());
// Initial scroll was sent.
EXPECT_EQ(1, wheel_event_count_);
wheel_event_count_ = 0;
// Move time forward a little.
AdvanceTime(1.f);
// Simulate the compositor animation observer calling ProgressFling with the
// last frame time. That frame time is after the event time, but before the
// animation start time.
ProgressFling(last_frame_time);
// No scrolls were sent.
EXPECT_EQ(0, wheel_event_count_);
// Additional ProgressFling calls generate scroll events as normal.
AdvanceTime();
ProgressFling(NowTicks());
EXPECT_GT(wheel_event_count_, 0);
}
TEST_P(FlingControllerTest, ControllerBoostsTouchpadFling) {
SimulateFlingStart(blink::WebGestureDevice::kTouchpad,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
// Processing GFS will send the first fling progress event if the time delta
// between the timestamp of the GFS and the time that ProcessGestureFlingStart
// is called is large enough.
bool process_GFS_sent_first_event = first_wheel_event_sent_;
AdvanceTime();
ProgressFling(NowTicks());
if (!process_GFS_sent_first_event) {
EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, last_sent_wheel_.momentum_phase);
} else {
EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged,
last_sent_wheel_.momentum_phase);
}
EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
// The rest of the wheel events must have momentum_phase == KPhaseChanged.
AdvanceTime();
ProgressFling(NowTicks());
EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged, last_sent_wheel_.momentum_phase);
EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
// Now cancel the fling.
SimulateFlingCancel(blink::WebGestureDevice::kTouchpad);
EXPECT_FALSE(FlingInProgress());
// The second GFS will boost the current active fling.
SimulateFlingStart(blink::WebGestureDevice::kTouchpad,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
}
TEST_P(FlingControllerTest, ControllerBoostsTouchscreenFling) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
// Fling progress must send GSU events.
AdvanceTime();
ProgressFling(NowTicks());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
// Now cancel the fling.
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
EXPECT_FALSE(FlingInProgress());
// The second GFS can be boosted so it should boost the just deactivated
// fling.
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
EXPECT_GT(fling_controller_->CurrentFlingVelocity().x(), 1000);
}
// Ensure that once a fling finishes, the next fling doesn't get boosted.
TEST_P(FlingControllerTest, ControllerDoesntBoostFinishedFling) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
AdvanceTime();
ProgressFling(NowTicks());
// Fast forward so that the fling ends.
double time_to_advance_ms = 1000.0;
AdvanceTime(time_to_advance_ms);
ProgressFling(NowTicks());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollEnd,
last_sent_gesture_.GetType())
<< "Unexpected Last Sent Gesture: "
<< WebInputEvent::GetName(last_sent_gesture_.GetType());
EXPECT_EQ(fling_controller_->CurrentFlingVelocity().x(), 0);
EXPECT_FALSE(FlingInProgress());
// Now send a new fling that would have been boosted had it occurred during
// the previous fling. Ensure it isn't boosted.
AdvanceTime();
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0), /*wait_before_processing=*/false);
EXPECT_TRUE(FlingInProgress());
EXPECT_EQ(fling_controller_->CurrentFlingVelocity().x(), 1000);
}
TEST_P(FlingControllerTest, ControllerNotifiesTheClientAfterFlingStart) {
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
// Now cancel the fling.
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
EXPECT_FALSE(FlingInProgress());
EXPECT_TRUE(notified_client_after_fling_stop_);
}
TEST_P(FlingControllerTest, MiddleClickAutoScrollFling) {
SimulateFlingStart(blink::WebGestureDevice::kSyntheticAutoscroll,
gfx::Vector2dF(1000, 0));
EXPECT_TRUE(FlingInProgress());
AdvanceTime();
ProgressFling(NowTicks());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
// Now send a new fling with different velocity and without sending a fling
// cancel event, the new fling should always replace the old one even when
// they are in the same direction.
SimulateFlingStart(blink::WebGestureDevice::kSyntheticAutoscroll,
gfx::Vector2dF(2000, 0));
EXPECT_TRUE(FlingInProgress());
EXPECT_EQ(fling_controller_->CurrentFlingVelocity().x(), 2000);
// Now cancel the fling.
SimulateFlingCancel(blink::WebGestureDevice::kSyntheticAutoscroll);
EXPECT_FALSE(FlingInProgress());
}
// Ensure that the fling controller does not start a fling if the last touchpad
// wheel event was consumed.
TEST_P(FlingControllerTest, NoFlingStartAfterWheelEventConsumed) {
// First ensure that a fling can start after a not consumed wheel event.
fling_controller_->OnWheelEventAck(
MouseWheelEventWithLatencyInfo(),
blink::mojom::InputEventResultSource::kCompositorThread,
blink::mojom::InputEventResultState::kNotConsumed);
SimulateFlingStart(blink::WebGestureDevice::kTouchpad,
gfx::Vector2dF(1000, 0));
ASSERT_TRUE(FlingInProgress());
// Cancel the first fling.
SimulateFlingCancel(blink::WebGestureDevice::kTouchpad);
EXPECT_FALSE(FlingInProgress());
// Now test that a consumed touchpad wheel event results in no fling.
fling_controller_->OnWheelEventAck(
MouseWheelEventWithLatencyInfo(),
blink::mojom::InputEventResultSource::kCompositorThread,
blink::mojom::InputEventResultState::kConsumed);
SimulateFlingStart(blink::WebGestureDevice::kTouchpad,
gfx::Vector2dF(1000, 0));
EXPECT_FALSE(FlingInProgress());
}
TEST_P(FlingControllerTest, SetGenerationTimestampToCurrentTimeWithoutFix) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatureState(
features::kUseFirstCoalescedFrameAsFlingGenerationTimestamp, false);
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
AdvanceTime();
std::optional<base::TimeTicks> first_coalesced_frame_begin_time =
NowTicks() - base::Seconds(3);
ProgressFling(NowTicks(), first_coalesced_frame_begin_time);
EXPECT_EQ(NowTicks(), last_sent_gesture_.TimeStamp());
}
TEST_P(FlingControllerTest,
SetGenerationTimestampToFirstCoalescedFrameBeginTimeWithFix) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatureState(
features::kUseFirstCoalescedFrameAsFlingGenerationTimestamp, true);
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0));
AdvanceTime();
std::optional<base::TimeTicks> first_coalesced_frame_begin_time =
NowTicks() - base::Seconds(3);
ProgressFling(NowTicks(), first_coalesced_frame_begin_time);
EXPECT_EQ(first_coalesced_frame_begin_time, last_sent_gesture_.TimeStamp());
}
class FlingControllerWithPhysicsBasedFlingTest : public FlingControllerTest {
public:
// testing::Test
FlingControllerWithPhysicsBasedFlingTest() {
scoped_feature_list_.InitAndEnableFeature(
::features::kExperimentalFlingAnimation);
}
FlingControllerWithPhysicsBasedFlingTest(
const FlingControllerWithPhysicsBasedFlingTest&) = delete;
FlingControllerWithPhysicsBasedFlingTest& operator=(
const FlingControllerWithPhysicsBasedFlingTest&) = delete;
~FlingControllerWithPhysicsBasedFlingTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All,
FlingControllerWithPhysicsBasedFlingTest,
testing::Bool());
// Ensure the bounding distance for boosted physics based flings is increased
// by a factor of the boost_multiplier and default multiplier
TEST_P(FlingControllerWithPhysicsBasedFlingTest,
ControllerBoostsTouchscreenFling) {
// We use a velocity of 4500 in this test because it yields a scroll delta
// that is greater than viewport * boost_multiplier * kDefaultBoundsMultiplier
// Android, Chromecast and iOS use Mobile fling curve so they are ignored
// for this test
bool use_mobile_fling_curve = false;
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_IOS)
use_mobile_fling_curve = true;
#endif
if (use_mobile_fling_curve)
return;
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(4500, 0));
EXPECT_TRUE(FlingInProgress());
// Fling progress must send GSU events.
AdvanceTime();
ProgressFling(NowTicks());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
last_sent_gesture_.GetType());
EXPECT_EQ(WebGestureEvent::InertialPhaseState::kMomentum,
last_sent_gesture_.data.scroll_update.inertial_phase);
EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
// Now cancel the fling.
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
EXPECT_FALSE(FlingInProgress());
// The second GFS can be boosted so it should boost the just deactivated
// fling. To test that the correct bounds scale is used, the scroll delta
// is accumulated after each frame.
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(4500, 0));
EXPECT_TRUE(FlingInProgress());
if (NeedsBeginFrameForFlingProgress())
ProgressFling(NowTicks());
float total_scroll_delta = CompleteFlingAndAccumulateScrollDelta();
// We expect the scroll delta to be the viewport * [boost_multiplier = 2] *
// multiplier
float expected_delta =
2 * PhysicsBasedFlingCurve::default_bounds_multiplier_for_testing() *
GetRootWidgetViewportSize().width();
EXPECT_EQ(ceilf(total_scroll_delta), roundf(expected_delta));
}
// Ensure that once a fling finishes, the next fling has a boost_multiplier of 1
TEST_P(FlingControllerWithPhysicsBasedFlingTest,
ControllerDoesntBoostFinishedFling) {
// Android, Chromecast and iOS use Mobile fling curve so they are ignored
// for this test
bool use_mobile_fling_curve = false;
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_IOS)
use_mobile_fling_curve = true;
#endif
if (use_mobile_fling_curve)
return;
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(1000, 0), /*wait_before_processing=*/true);
EXPECT_TRUE(FlingInProgress());
AdvanceTime();
ProgressFling(NowTicks());
// Fast forward so that the fling ends.
double time_to_advance_ms = 1000.0;
AdvanceTime(time_to_advance_ms);
ProgressFling(NowTicks());
ASSERT_EQ(WebInputEvent::Type::kGestureScrollEnd,
last_sent_gesture_.GetType())
<< "Unexpected Last Sent Gesture: "
<< WebInputEvent::GetName(last_sent_gesture_.GetType());
EXPECT_EQ(fling_controller_->CurrentFlingVelocity().x(), 0);
EXPECT_FALSE(FlingInProgress());
// Now send a new fling, ensure boost_multiplier is 1
AdvanceTime();
SimulateFlingCancel(blink::WebGestureDevice::kTouchscreen);
SimulateFlingStart(blink::WebGestureDevice::kTouchscreen,
gfx::Vector2dF(10000, 0));
EXPECT_TRUE(FlingInProgress());
if (NeedsBeginFrameForFlingProgress())
ProgressFling(NowTicks());
float total_scroll_delta = CompleteFlingAndAccumulateScrollDelta();
// We expect the scroll delta to be the viewport * [boost_multiplier = 1] *
// multiplier
float expected_delta =
PhysicsBasedFlingCurve::default_bounds_multiplier_for_testing() *
GetRootWidgetViewportSize().width();
EXPECT_EQ(ceilf(total_scroll_delta), roundf(expected_delta));
}
} // namespace input
|