summaryrefslogtreecommitdiff
path: root/chromium/content/browser/renderer_host/input/fling_controller_unittest.cc
blob: d95159d2c7ba9882eb9ea4a1199f49817e03817f (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/input/fling_controller.h"

#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "content/browser/renderer_host/input/gesture_event_queue.h"
#include "content/public/common/content_features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/blink/fling_booster.h"

using blink::WebGestureEvent;
using blink::WebInputEvent;
using blink::WebMouseWheelEvent;

namespace content {

class FakeFlingController : public FlingController {
 public:
  FakeFlingController(GestureEventQueue* gesture_event_queue,
                      TouchpadTapSuppressionControllerClient* touchpad_client,
                      FlingControllerClient* fling_client,
                      const Config& config)
      : FlingController(gesture_event_queue,
                        touchpad_client,
                        fling_client,
                        config) {}

  bool FlingBoosted() const { return fling_booster_->fling_boosted(); }
};

class FlingControllerTest : public testing::Test,
                            public TouchpadTapSuppressionControllerClient,
                            public GestureEventQueueClient,
                            public FlingControllerClient {
 public:
  // testing::Test
  FlingControllerTest()
      : scoped_task_environment_(
            base::test::ScopedTaskEnvironment::MainThreadType::UI) {}

  ~FlingControllerTest() override {}

  void SetUp() override {
    queue_ = std::make_unique<GestureEventQueue>(this, this, this,
                                                 GestureEventQueue::Config());
    fling_controller_ = std::make_unique<FakeFlingController>(
        queue_.get(), this, this, FlingController::Config());
    feature_list_.InitFromCommandLine(
        features::kTouchpadAndWheelScrollLatching.name, "");
  }

  // TouchpadTapSuppressionControllerClient
  void SendMouseEventImmediately(
      const MouseEventWithLatencyInfo& event) override {}

  // GestureEventQueueClient
  void SendGestureEventImmediately(
      const GestureEventWithLatencyInfo& event) override {}
  void OnGestureEventAck(const GestureEventWithLatencyInfo& event,
                         InputEventAckSource ack_source,
                         InputEventAckState ack_result) override {}

  // FlingControllerClient
  void SendGeneratedWheelEvent(
      const MouseWheelEventWithLatencyInfo& wheel_event) override {
    last_sent_wheel_ = wheel_event.event;
  }
  void SendGeneratedGestureScrollEvents(
      const GestureEventWithLatencyInfo& gesture_event) override {
    last_sent_gesture_ = gesture_event.event;
  }
  void SetNeedsBeginFrameForFlingProgress() override {
    DCHECK(!scheduled_next_fling_progress_);
    scheduled_next_fling_progress_ = true;
  }
  void DidStopFlingingOnBrowser() override {
    notified_client_after_fling_stop_ = true;
  }

  void SimulateFlingStart(blink::WebGestureDevice source_device,
                          const gfx::Vector2dF& velocity) {
    scheduled_next_fling_progress_ = false;
    WebGestureEvent fling_start(
        WebInputEvent::kGestureFlingStart, 0,
        ui::EventTimeStampToSeconds(base::TimeTicks::Now()), 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 (!fling_controller_->FilterGestureEvent(fling_start_with_latency))
      fling_controller_->ProcessGestureFlingStart(fling_start_with_latency);
  }

  void SimulateFlingCancel(blink::WebGestureDevice source_device) {
    notified_client_after_fling_stop_ = false;
    WebGestureEvent fling_cancel(
        WebInputEvent::kGestureFlingCancel, 0,
        ui::EventTimeStampToSeconds(base::TimeTicks::Now()), source_device);
    // autoscroll fling cancel doesn't allow fling boosting.
    if (source_device == blink::kWebGestureDeviceSyntheticAutoscroll)
      fling_cancel.data.fling_cancel.prevent_boosting = true;
    GestureEventWithLatencyInfo fling_cancel_with_latency(fling_cancel);
    last_fling_cancel_filtered_ =
        fling_controller_->FilterGestureEvent(fling_cancel_with_latency);
    if (!last_fling_cancel_filtered_)
      fling_controller_->ProcessGestureFlingCancel(fling_cancel_with_latency);
  }

  void ProgressFling(base::TimeTicks current_time) {
    DCHECK(scheduled_next_fling_progress_);
    scheduled_next_fling_progress_ = false;
    fling_controller_->ProgressFling(current_time);
  }

  bool FlingInProgress() { return fling_controller_->fling_in_progress(); }
  bool FlingBoosted() { return fling_controller_->FlingBoosted(); }

 protected:
  std::unique_ptr<FakeFlingController> fling_controller_;
  WebMouseWheelEvent last_sent_wheel_;
  WebGestureEvent last_sent_gesture_;
  bool last_fling_cancel_filtered_;
  bool scheduled_next_fling_progress_;
  bool notified_client_after_fling_stop_;

 private:
  base::test::ScopedTaskEnvironment scoped_task_environment_;
  std::unique_ptr<GestureEventQueue> queue_;
  base::test::ScopedFeatureList feature_list_;
};

TEST_F(FlingControllerTest,
       ControllerSendsWheelEndOnTouchpadFlingWithZeroVelocity) {
  SimulateFlingStart(blink::kWebGestureDeviceTouchpad, 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_F(FlingControllerTest,
       ControllerSendsGSEOnTouchscreenFlingWithZeroVelocity) {
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen, gfx::Vector2dF());
  // The controller doesn't start a fling and sends a GSE immediately.
  EXPECT_FALSE(FlingInProgress());
  EXPECT_EQ(WebInputEvent::kGestureScrollEnd, last_sent_gesture_.GetType());
}

TEST_F(FlingControllerTest, ControllerHandlesTouchpadGestureFling) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchpad, gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  // The first wheel event must have momentum_phase == KPhaseBegan.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, 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.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged, last_sent_wheel_.momentum_phase);
  EXPECT_GT(last_sent_wheel_.delta_x, 0.f);

  // Now cancel the fling. The GFC will end the fling.
  SimulateFlingCancel(blink::kWebGestureDeviceTouchpad);
  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_F(FlingControllerTest, ControllerHandlesTouchscreenGestureFling) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());

  // The fling progress will generate and send GSU events with inertial state.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  ASSERT_EQ(WebInputEvent::kGestureScrollUpdate, last_sent_gesture_.GetType());
  EXPECT_EQ(WebGestureEvent::kMomentumPhase,
            last_sent_gesture_.data.scroll_update.inertial_phase);
  EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);

  // Now cancel the fling. The GFC will get suppressed by fling booster.
  SimulateFlingCancel(blink::kWebGestureDeviceTouchscreen);
  EXPECT_TRUE(last_fling_cancel_filtered_);
  EXPECT_TRUE(FlingInProgress());

  // Wait for the boosting timer to expire. The delayed cancelation must work.
  progress_time += base::TimeDelta::FromMilliseconds(500);
  ProgressFling(progress_time);
  EXPECT_FALSE(FlingInProgress());
  EXPECT_EQ(WebInputEvent::kGestureScrollEnd, last_sent_gesture_.GetType());
}

TEST_F(FlingControllerTest, ControllerSendsWheelEndWhenTouchpadFlingIsOver) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchpad, gfx::Vector2dF(100, 0));
  EXPECT_TRUE(FlingInProgress());
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, last_sent_wheel_.momentum_phase);
  EXPECT_GT(last_sent_wheel_.delta_x, 0.f);

  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  while (FlingInProgress()) {
    EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged,
              last_sent_wheel_.momentum_phase);
    EXPECT_GT(last_sent_wheel_.delta_x, 0.f);
    progress_time += base::TimeDelta::FromMilliseconds(17);
    ProgressFling(progress_time);
  }

  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_F(FlingControllerTest, ControllerSendsGSEWhenTouchscreenFlingIsOver) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(100, 0));
  EXPECT_TRUE(FlingInProgress());

  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  while (FlingInProgress()) {
    ASSERT_EQ(WebInputEvent::kGestureScrollUpdate,
              last_sent_gesture_.GetType());
    EXPECT_EQ(WebGestureEvent::kMomentumPhase,
              last_sent_gesture_.data.scroll_update.inertial_phase);
    EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
    progress_time += base::TimeDelta::FromMilliseconds(17);
    ProgressFling(progress_time);
  }

  EXPECT_EQ(WebInputEvent::kGestureScrollEnd, last_sent_gesture_.GetType());
}

TEST_F(FlingControllerTest,
       EarlyTouchpadFlingCancelationOnInertialGSUAckNotConsumed) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchpad, gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, last_sent_wheel_.momentum_phase);
  EXPECT_GT(last_sent_wheel_.delta_x, 0.f);

  // A non-consumed GSU ack in inertial state cancels out the rest of the fling.
  WebGestureEvent scroll_update(
      WebInputEvent::kGestureScrollUpdate, 0,
      ui::EventTimeStampToSeconds(base::TimeTicks::Now()));
  scroll_update.data.scroll_update.inertial_phase =
      WebGestureEvent::kMomentumPhase;

  fling_controller_->OnGestureEventAck(
      GestureEventWithLatencyInfo(scroll_update),
      INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
  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_F(FlingControllerTest,
       EarlyTouchscreenFlingCancelationOnInertialGSUAckNotConsumed) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  ASSERT_EQ(WebInputEvent::kGestureScrollUpdate, last_sent_gesture_.GetType());
  EXPECT_EQ(WebGestureEvent::kMomentumPhase,
            last_sent_gesture_.data.scroll_update.inertial_phase);
  EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);

  // A non-consumed GSU ack in inertial state cancels out the rest of the fling.
  WebGestureEvent scroll_update(
      WebInputEvent::kGestureScrollUpdate, 0,
      ui::EventTimeStampToSeconds(base::TimeTicks::Now()));
  scroll_update.data.scroll_update.inertial_phase =
      WebGestureEvent::kMomentumPhase;

  fling_controller_->OnGestureEventAck(
      GestureEventWithLatencyInfo(scroll_update),
      INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
  EXPECT_FALSE(FlingInProgress());
  EXPECT_EQ(WebInputEvent::kGestureScrollEnd, last_sent_gesture_.GetType());
}

TEST_F(FlingControllerTest, EarlyTouchpadFlingCancelationOnFlingStop) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchpad, gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, 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_F(FlingControllerTest, EarlyTouchscreenFlingCancelationOnFlingStop) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());

  // progress fling must send GSU events.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  ASSERT_EQ(WebInputEvent::kGestureScrollUpdate, last_sent_gesture_.GetType());
  EXPECT_EQ(WebGestureEvent::kMomentumPhase,
            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::kGestureScrollEnd, last_sent_gesture_.GetType());
}

TEST_F(FlingControllerTest, GestureFlingCancelsFiltered) {
  // GFC without previous GFS is dropped.
  SimulateFlingCancel(blink::kWebGestureDeviceTouchscreen);
  EXPECT_TRUE(last_fling_cancel_filtered_);

  // GFC after previous GFS is filtered by fling booster.
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  SimulateFlingCancel(blink::kWebGestureDeviceTouchscreen);
  EXPECT_TRUE(last_fling_cancel_filtered_);
  EXPECT_TRUE(FlingInProgress());

  // Any other GFC while the fling cancelation is deferred gets filtered.
  SimulateFlingCancel(blink::kWebGestureDeviceTouchscreen);
  EXPECT_TRUE(last_fling_cancel_filtered_);
}

TEST_F(FlingControllerTest, GestureFlingNotCancelledBySmallTimeDelta) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());

  // 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.
  // progress_time += base::TimeDelta::FromMilliseconds(1);
  ProgressFling(progress_time);
  EXPECT_EQ(blink::kWebGestureDeviceUninitialized,
            last_sent_gesture_.SourceDevice());
  EXPECT_TRUE(FlingInProgress());

  // The rest of the progress flings must advance the fling normally.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(blink::kWebGestureDeviceTouchscreen,
            last_sent_gesture_.SourceDevice());
  ASSERT_EQ(WebInputEvent::kGestureScrollUpdate, last_sent_gesture_.GetType());
  EXPECT_EQ(WebGestureEvent::kMomentumPhase,
            last_sent_gesture_.data.scroll_update.inertial_phase);
  EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
}

TEST_F(FlingControllerTest, GestureFlingWithNegativeTimeDelta) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());

  // 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 and instead reset the fling start time.
  progress_time -= base::TimeDelta::FromMilliseconds(5);
  ProgressFling(progress_time);
  EXPECT_EQ(blink::kWebGestureDeviceUninitialized,
            last_sent_gesture_.SourceDevice());

  // The rest of the progress flings must advance the fling normally.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(blink::kWebGestureDeviceTouchscreen,
            last_sent_gesture_.SourceDevice());
  ASSERT_EQ(WebInputEvent::kGestureScrollUpdate, last_sent_gesture_.GetType());
  EXPECT_EQ(WebGestureEvent::kMomentumPhase,
            last_sent_gesture_.data.scroll_update.inertial_phase);
  EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);
}

// TODO(sahel): Enable the test once boosting is enabled for touchpad fling.
// https://crbug.com/249063
TEST_F(FlingControllerTest, DISABLED_ControllerBoostsTouchpadFling) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchpad, gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  // The first wheel event must have momentum_phase == KPhaseBegan.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, 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.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  EXPECT_EQ(WebMouseWheelEvent::kPhaseChanged, last_sent_wheel_.momentum_phase);
  EXPECT_GT(last_sent_wheel_.delta_x, 0.f);

  // Now cancel the fling. The GFC will get suppressed by fling booster.
  SimulateFlingCancel(blink::kWebGestureDeviceTouchpad);
  EXPECT_TRUE(last_fling_cancel_filtered_);
  EXPECT_TRUE(FlingInProgress());

  // The second GFS will boost the current active fling.
  SimulateFlingStart(blink::kWebGestureDeviceTouchpad, gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  EXPECT_TRUE(FlingBoosted());
}

TEST_F(FlingControllerTest, ControllerBoostsTouchscreenFling) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());

  // Fling progress must send GSU events.
  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  ASSERT_EQ(WebInputEvent::kGestureScrollUpdate, last_sent_gesture_.GetType());
  EXPECT_EQ(WebGestureEvent::kMomentumPhase,
            last_sent_gesture_.data.scroll_update.inertial_phase);
  EXPECT_GT(last_sent_gesture_.data.scroll_update.delta_x, 0.f);

  // Now cancel the fling. The GFC will get suppressed by fling booster.
  SimulateFlingCancel(blink::kWebGestureDeviceTouchscreen);
  EXPECT_TRUE(last_fling_cancel_filtered_);
  EXPECT_TRUE(FlingInProgress());

  // The second GFS will boost the current active fling.
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());
  EXPECT_TRUE(FlingBoosted());
}

TEST_F(FlingControllerTest, ControllerNotifiesTheClientAfterFlingStart) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceTouchscreen,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());

  // Now cancel the fling. The GFC will get suppressed by fling booster.
  SimulateFlingCancel(blink::kWebGestureDeviceTouchscreen);
  EXPECT_TRUE(last_fling_cancel_filtered_);
  EXPECT_TRUE(FlingInProgress());

  // Wait for the boosting timer to expire. The delayed cancelation must work
  // and the client must be notified after fling cancelation.
  progress_time += base::TimeDelta::FromMilliseconds(500);
  ProgressFling(progress_time);
  EXPECT_FALSE(FlingInProgress());
  EXPECT_EQ(WebInputEvent::kGestureScrollEnd, last_sent_gesture_.GetType());
  EXPECT_TRUE(notified_client_after_fling_stop_);
}

TEST_F(FlingControllerTest, MiddleClickAutoScrollFling) {
  base::TimeTicks progress_time = base::TimeTicks::Now();
  SimulateFlingStart(blink::kWebGestureDeviceSyntheticAutoscroll,
                     gfx::Vector2dF(1000, 0));
  EXPECT_TRUE(FlingInProgress());

  progress_time += base::TimeDelta::FromMilliseconds(17);
  ProgressFling(progress_time);
  ASSERT_EQ(WebInputEvent::kGestureScrollUpdate, last_sent_gesture_.GetType());
  EXPECT_EQ(WebGestureEvent::kMomentumPhase,
            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::kWebGestureDeviceSyntheticAutoscroll,
                     gfx::Vector2dF(2000, 0));
  EXPECT_TRUE(FlingInProgress());
  EXPECT_FALSE(FlingBoosted());

  // Now cancel the fling. The GFC won't get suppressed by fling booster since
  // autoscroll fling doesn't have boosting.
  SimulateFlingCancel(blink::kWebGestureDeviceSyntheticAutoscroll);
  EXPECT_FALSE(last_fling_cancel_filtered_);
  EXPECT_FALSE(FlingInProgress());
}

}  // namespace content