summaryrefslogtreecommitdiffstats
path: root/content/browser/web_contents/aura/overscroll_window_delegate_unittest.cc
blob: 13382a8656fb72791ec3bf19bfb286492285d691 (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
// Copyright 2015 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/web_contents/aura/overscroll_window_delegate.h"

#include "content/browser/renderer_host/overscroll_controller_delegate.h"
#include "content/public/browser/overscroll_configuration.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/window.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
#include "ui/events/test/event_generator.h"

namespace content {

namespace {
const int kTestWindowWidth = 600;
}

class OverscrollWindowDelegateTest : public aura::test::AuraTestBase,
                                     public OverscrollControllerDelegate {
 public:
  OverscrollWindowDelegateTest()
      : window_(nullptr),
        overscroll_complete_(false),
        overscroll_started_(false),
        mode_changed_(false),
        current_mode_(OVERSCROLL_NONE),
        touch_start_threshold_(content::GetOverscrollConfig(
            content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN)),
        touch_complete_threshold_(content::GetOverscrollConfig(
            content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE)) {
  }

  ~OverscrollWindowDelegateTest() override {}

  void Reset() {
    overscroll_complete_ = false;
    overscroll_started_ = false;
    mode_changed_ = false;
    current_mode_ = OVERSCROLL_NONE;
    window_.reset(CreateNormalWindow(
        0, root_window(), new OverscrollWindowDelegate(this, gfx::Image())));
    window_->SetBounds(gfx::Rect(0, 0, kTestWindowWidth, kTestWindowWidth));
  }

  // Accessors.
  aura::Window* window() { return window_.get(); }

  bool overscroll_complete() { return overscroll_complete_; }
  bool overscroll_started() { return overscroll_started_; }
  bool mode_changed() { return mode_changed_; }

  OverscrollMode current_mode() { return current_mode_; }

  const float touch_start_threshold() {
    return touch_start_threshold_;
  }

  const float touch_complete_threshold() {
    return kTestWindowWidth * touch_complete_threshold_;
  }

 protected:
  // aura::test::AuraTestBase:
  void SetUp() override {
    aura::test::AuraTestBase::SetUp();
    Reset();
    ui::GestureConfiguration::GetInstance()
        ->set_max_touch_move_in_pixels_for_click(0);
  }

  void TearDown() override {
    window_.reset();
    aura::test::AuraTestBase::TearDown();
  }

 private:
  // OverscrollControllerDelegate:
  gfx::Rect GetVisibleBounds() const override {
    return gfx::Rect(kTestWindowWidth, kTestWindowWidth);
  }

  bool OnOverscrollUpdate(float delta_x, float delta_y) override {
    return true;
  }

  void OnOverscrollComplete(OverscrollMode overscroll_mode) override {
    overscroll_complete_ = true;
  }

  void OnOverscrollModeChange(OverscrollMode old_mode,
                              OverscrollMode new_mode) override {
    mode_changed_ = true;
    current_mode_ = new_mode;
    if (current_mode_ != OVERSCROLL_NONE)
      overscroll_started_ = true;
  }

  // Window in which the overscroll window delegate is installed.
  scoped_ptr<aura::Window> window_;

  // State flags.
  bool overscroll_complete_;
  bool overscroll_started_;
  bool mode_changed_;
  OverscrollMode current_mode_;

  // Config defined constants.
  const float touch_start_threshold_;
  const float touch_complete_threshold_;

  DISALLOW_COPY_AND_ASSIGN(OverscrollWindowDelegateTest);
};

// Tests that the basic overscroll gesture works and sends updates to the
// delegate.
TEST_F(OverscrollWindowDelegateTest, BasicOverscroll) {
  ui::test::EventGenerator generator(root_window());

  // Start an OVERSCROLL_EAST gesture.
  generator.GestureScrollSequence(
      gfx::Point(0, 0), gfx::Point(touch_complete_threshold() + 1, 10),
      base::TimeDelta::FromMilliseconds(10), 10);
  EXPECT_TRUE(overscroll_started());
  EXPECT_EQ(current_mode(), OVERSCROLL_EAST);
  EXPECT_TRUE(overscroll_complete());

  Reset();
  // Start an OVERSCROLL_WEST gesture.
  generator.GestureScrollSequence(gfx::Point(touch_complete_threshold() + 1, 0),
                                  gfx::Point(0, 0),
                                  base::TimeDelta::FromMilliseconds(10), 10);
  EXPECT_TRUE(overscroll_started());
  EXPECT_EQ(current_mode(), OVERSCROLL_WEST);
  EXPECT_TRUE(overscroll_complete());
}

// Verifies that the OverscrollWindowDelegate direction is set correctly during
// an overscroll.
TEST_F(OverscrollWindowDelegateTest, BasicOverscrollModes) {
  ui::test::EventGenerator generator(root_window());
  OverscrollWindowDelegate* delegate =
      static_cast<OverscrollWindowDelegate*>(window()->delegate());

  // Start pressing a touch, but do not start the gesture yet.
  generator.MoveTouch(gfx::Point(0, 0));
  generator.PressTouch();
  EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE);

  // Slide the touch to the right.
  generator.MoveTouch(gfx::Point(touch_complete_threshold() + 1, 0));
  EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_EAST);

  // Complete the gesture.
  generator.ReleaseTouch();
  EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE);
  EXPECT_TRUE(overscroll_complete());

  // Start another overscroll.
  generator.MoveTouch(gfx::Point(touch_complete_threshold() + 1, 0));
  generator.PressTouch();
  EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE);

  // Slide the touch to the left.
  generator.MoveTouch(gfx::Point(0, 0));
  EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_WEST);

  // Complete the gesture.
  generator.ReleaseTouch();
  EXPECT_EQ(delegate->overscroll_mode_, OVERSCROLL_NONE);
  EXPECT_TRUE(overscroll_complete());

  // Generate a mouse events which normally cancel the overscroll. Confirm
  // that superfluous mode changed events are not dispatched.
  Reset();
  generator.PressLeftButton();
  generator.MoveMouseTo(gfx::Point(10, 10));
  EXPECT_FALSE(mode_changed());
}

// Tests that the overscroll does not start until the gesture gets past a
// particular threshold.
TEST_F(OverscrollWindowDelegateTest, OverscrollThreshold) {
  ui::test::EventGenerator generator(root_window());

  // Start an OVERSCROLL_EAST gesture.
  generator.GestureScrollSequence(gfx::Point(0, 0),
                                  gfx::Point(touch_start_threshold(), 0),
                                  base::TimeDelta::FromMilliseconds(10), 10);
  EXPECT_FALSE(overscroll_started());
  EXPECT_EQ(current_mode(), OVERSCROLL_NONE);
  EXPECT_FALSE(overscroll_complete());

  Reset();
  // Start an OVERSCROLL_WEST gesture.
  generator.GestureScrollSequence(gfx::Point(touch_start_threshold(), 0),
                                  gfx::Point(0, 0),
                                  base::TimeDelta::FromMilliseconds(10), 10);
  EXPECT_FALSE(overscroll_started());
  EXPECT_EQ(current_mode(), OVERSCROLL_NONE);
  EXPECT_FALSE(overscroll_complete());
}

// Tests that the overscroll is aborted if the gesture does not get past the
// completion threshold.
TEST_F(OverscrollWindowDelegateTest, AbortOverscrollThreshold) {
  ui::test::EventGenerator generator(root_window());

  // Start an OVERSCROLL_EAST gesture.
  generator.GestureScrollSequence(gfx::Point(0, 0),
                                  gfx::Point(touch_start_threshold() + 1, 0),
                                  base::TimeDelta::FromMilliseconds(10), 10);
  EXPECT_TRUE(overscroll_started());
  EXPECT_EQ(current_mode(), OVERSCROLL_NONE);
  EXPECT_FALSE(overscroll_complete());

  Reset();
  // Start an OVERSCROLL_WEST gesture.
  generator.GestureScrollSequence(gfx::Point(touch_start_threshold() + 1, 0),
                                  gfx::Point(0, 0),
                                  base::TimeDelta::FromMilliseconds(10), 10);
  EXPECT_TRUE(overscroll_started());
  EXPECT_EQ(current_mode(), OVERSCROLL_NONE);
  EXPECT_FALSE(overscroll_complete());
}

// Tests that the overscroll is aborted if the delegate receives some other
// event.
TEST_F(OverscrollWindowDelegateTest, EventAbortsOverscroll) {
  ui::test::EventGenerator generator(root_window());
  // Start an OVERSCROLL_EAST gesture, without releasing touch.
  generator.set_current_location(gfx::Point(0, 0));
  generator.PressTouch();
  int touch_x = touch_start_threshold() + 1;
  generator.MoveTouch(gfx::Point(touch_x, 0));
  EXPECT_TRUE(overscroll_started());
  EXPECT_EQ(current_mode(), OVERSCROLL_EAST);
  EXPECT_FALSE(overscroll_complete());

  // Dispatch a mouse event, the overscroll should be cancelled.
  generator.PressLeftButton();
  EXPECT_EQ(current_mode(), OVERSCROLL_NONE);
  EXPECT_FALSE(overscroll_complete());

  // We should be able to restart the overscroll without lifting the finger.
  generator.MoveTouch(gfx::Point(touch_x + touch_start_threshold() + 1, 0));
  EXPECT_EQ(current_mode(), OVERSCROLL_EAST);
  EXPECT_FALSE(overscroll_complete());
}

}  // namespace content