blob: db2f3d47f6869106951fba3e72f4a0534d8aa3d0 [file] [log] [blame]
Mohsen Izadi9830436c2017-12-21 18:02:461// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/renderer_host/overscroll_controller.h"
6
7#include <memory>
8
9#include "base/containers/queue.h"
10#include "content/browser/renderer_host/overscroll_controller_delegate.h"
11#include "content/common/input/synthetic_web_input_event_builders.h"
12#include "content/test/test_overscroll_delegate.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace content {
16
17class OverscrollControllerTest : public ::testing::Test {
18 protected:
19 OverscrollControllerTest() {}
20 ~OverscrollControllerTest() override {}
21
22 void SetUp() override {
23 delegate_ = std::make_unique<TestOverscrollDelegate>(gfx::Size(400, 300));
24 controller_ = std::make_unique<OverscrollController>();
25 controller_->set_delegate(delegate_.get());
26 }
27
28 void TearDown() override {
29 controller_ = nullptr;
30 delegate_ = nullptr;
31 }
32
33 // Creates and sends a mouse-wheel event to the overscroll controller. Returns
34 // |true| if the event is consumed by the overscroll controller.
35 bool SimulateMouseWheel(float dx, float dy) {
36 DCHECK(!current_event_);
37 current_event_ = std::make_unique<blink::WebMouseWheelEvent>(
38 SyntheticWebMouseWheelEventBuilder::Build(0, 0, dx, dy, 0, true));
39 return controller_->WillHandleEvent(*current_event_);
40 }
41
42 // Creates and sends a gesture-scroll-update event to the overscroll
43 // controller. Returns |true| if the event is consumed by the overscroll
44 // controller.
45 bool SimulateGestureScrollUpdate(float dx,
46 float dy,
47 blink::WebGestureDevice device) {
48 DCHECK(!current_event_);
49 current_event_ = std::make_unique<blink::WebGestureEvent>(
50 SyntheticWebGestureEventBuilder::BuildScrollUpdate(dx, dy, 0, device));
51 return controller_->WillHandleEvent(*current_event_);
52 }
53
54 // Notifies the overscroll controller that the current event is ACKed.
55 void SimulateAck(bool processed) {
56 DCHECK(current_event_);
57 controller_->ReceivedEventACK(*current_event_, processed);
58 current_event_ = nullptr;
59 }
60
61 TestOverscrollDelegate* delegate() const { return delegate_.get(); }
62
63 OverscrollMode controller_mode() const {
64 return controller_->overscroll_mode_;
65 }
66
67 OverscrollSource controller_source() const {
68 return controller_->overscroll_source_;
69 }
70
71 private:
72 std::unique_ptr<TestOverscrollDelegate> delegate_;
73 std::unique_ptr<OverscrollController> controller_;
74
75 // Keeps track of the last event that has been processed by the overscroll
76 // controller which is not yet ACKed. Will be null if no event is processed or
77 // the last event is ACKed.
78 std::unique_ptr<blink::WebInputEvent> current_event_;
79
80 DISALLOW_COPY_AND_ASSIGN(OverscrollControllerTest);
81};
82
83// Tests that if a mouse-wheel is consumed by content before overscroll is
84// initiated, overscroll will not initiate anymore.
85TEST_F(OverscrollControllerTest, MouseWheelConsumedPreventsOverscroll) {
86 // Simulate a mouse-wheel, ACK it as not processed, simulate the corresponding
87 // gesture scroll-update event, and ACK it is not processed. Since it is not
88 // passing the start threshold, no overscroll should happen.
89 EXPECT_FALSE(SimulateMouseWheel(10, 0));
90 SimulateAck(false);
91 EXPECT_FALSE(
92 SimulateGestureScrollUpdate(10, 0, blink::kWebGestureDeviceTouchpad));
93 SimulateAck(false);
94 EXPECT_EQ(OVERSCROLL_NONE, controller_mode());
95 EXPECT_EQ(OverscrollSource::NONE, controller_source());
96 EXPECT_EQ(OVERSCROLL_NONE, delegate()->current_mode());
97 EXPECT_EQ(OVERSCROLL_NONE, delegate()->completed_mode());
98
99 // Simulate a mouse-wheel and ACK it as processed. No gesture scroll-update
100 // needs to be simulated. Still no overscroll.
101 EXPECT_FALSE(SimulateMouseWheel(10, 0));
102 SimulateAck(true);
103 EXPECT_EQ(OVERSCROLL_NONE, controller_mode());
104 EXPECT_EQ(OverscrollSource::NONE, controller_source());
105 EXPECT_EQ(OVERSCROLL_NONE, delegate()->current_mode());
106 EXPECT_EQ(OVERSCROLL_NONE, delegate()->completed_mode());
107
108 // Simulate a mouse-wheel and the corresponding gesture scroll-update both
109 // ACKed as not processed. Although the scroll passes overscroll start
110 // threshold, no overscroll should happen since the previous mouse-wheel was
111 // marked as processed.
112 EXPECT_FALSE(SimulateMouseWheel(100, 0));
113 SimulateAck(false);
114 EXPECT_FALSE(
115 SimulateGestureScrollUpdate(100, 0, blink::kWebGestureDeviceTouchpad));
116 SimulateAck(false);
117 EXPECT_EQ(OVERSCROLL_NONE, controller_mode());
118 EXPECT_EQ(OverscrollSource::NONE, controller_source());
119 EXPECT_EQ(OVERSCROLL_NONE, delegate()->current_mode());
120 EXPECT_EQ(OVERSCROLL_NONE, delegate()->completed_mode());
121}
122
123} // namespace content