File: scrollbar_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (318 lines) | stat: -rw-r--r-- 11,238 bytes parent folder | download | duplicates (6)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"
#include "ui/views/controls/scrollbar/scroll_bar.h"
#include "ui/views/controls/scrollbar/scroll_bar_views.h"
#include "ui/views/test/view_metadata_test_utils.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_utils.h"

namespace views {

namespace {

// The Scrollbar controller. This is the widget that should do the real
// scrolling of contents.
class TestScrollBarController : public ScrollBarController {
 public:
  virtual ~TestScrollBarController() = default;

  void ScrollToPosition(ScrollBar* source, int position) override {
    last_source = source;
    last_position = position;
  }

  int GetScrollIncrement(ScrollBar* source,
                         bool is_page,
                         bool is_positive) override {
    last_source = source;
    last_is_page = is_page;
    last_is_positive = is_positive;

    if (is_page) {
      return 20;
    }
    return 10;
  }

  // We save the last values in order to assert the correctness of the scroll
  // operation.
  raw_ptr<ScrollBar> last_source = nullptr;
  bool last_is_positive = false;
  bool last_is_page = false;
  int last_position = 0;
};

// This container is used to forward gesture events to the scrollbar for
// testing fling and other gestures.
class TestScrollbarContainer : public View {
 public:
  TestScrollbarContainer() = default;
  ~TestScrollbarContainer() override = default;
  TestScrollbarContainer(const TestScrollbarContainer&) = delete;
  TestScrollbarContainer& operator=(const TestScrollbarContainer&) = delete;

  void OnGestureEvent(ui::GestureEvent* event) override {
    children()[0]->OnGestureEvent(event);
  }
};

}  // namespace

class ScrollBarViewsTest : public ViewsTestBase {
 public:
  static constexpr int kScrollBarID = 123;

  ScrollBarViewsTest() = default;

  void SetUp() override {
    ViewsTestBase::SetUp();
    controller_ = std::make_unique<TestScrollBarController>();

    widget_ = std::make_unique<Widget>();
    Widget::InitParams params = CreateParams(
        Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_POPUP);
    params.bounds = gfx::Rect(0, 0, 100, 300);
    widget_->Init(std::move(params));
    widget_->Show();
    auto* container =
        widget_->SetContentsView(std::make_unique<TestScrollbarContainer>());

    auto* bar = container->AddChildView(std::make_unique<ScrollBarViews>());
    bar->SetBounds(0, 0, 100, 100);
    bar->Update(100, 1000, 0);
    bar->set_controller(controller_.get());
    bar->SetID(kScrollBarID);

    track_size_ = scrollbar()->GetTrackBounds().width();
  }

  void TearDown() override {
    scrollbar()->set_controller(nullptr);
    controller_.reset();
    widget_.reset();
    ViewsTestBase::TearDown();
  }

 protected:
  ScrollBar* scrollbar() {
    return static_cast<ScrollBar*>(
        widget_->GetContentsView()->GetViewByID(kScrollBarID));
  }

  UniqueWidgetPtr widget_;

  // Keep track of the size of the track. This is how we can tell when we
  // scroll to the middle.
  int track_size_;

  std::unique_ptr<TestScrollBarController> controller_;
};

// Verify properties are accessible via metadata.
TEST_F(ScrollBarViewsTest, MetaDataTest) {
  test::TestViewMetadata(scrollbar());
}

TEST_F(ScrollBarViewsTest, Scrolling) {
  EXPECT_EQ(0, scrollbar()->GetPosition());
  EXPECT_EQ(900, scrollbar()->GetMaxPosition());
  EXPECT_EQ(0, scrollbar()->GetMinPosition());

  // Scroll to middle.
  scrollbar()->ScrollToThumbPosition(track_size_ / 2, true);
  EXPECT_EQ(450, controller_->last_position);
  EXPECT_EQ(scrollbar(), controller_->last_source);

  // Scroll to the end.
  scrollbar()->ScrollToThumbPosition(track_size_, true);
  EXPECT_EQ(900, controller_->last_position);

  // Overscroll. Last position should be the maximum position.
  scrollbar()->ScrollToThumbPosition(track_size_ + 100, true);
  EXPECT_EQ(900, controller_->last_position);

  // Underscroll. Last position should be the minimum position.
  scrollbar()->ScrollToThumbPosition(-10, false);
  EXPECT_EQ(0, controller_->last_position);

  // Test the different fixed scrolling amounts. Generally used by buttons,
  // or click on track.
  scrollbar()->ScrollToThumbPosition(0, false);
  scrollbar()->ScrollByAmount(ScrollBar::ScrollAmount::kNextLine);
  EXPECT_EQ(10, controller_->last_position);

  scrollbar()->ScrollByAmount(ScrollBar::ScrollAmount::kPrevLine);
  EXPECT_EQ(0, controller_->last_position);

  scrollbar()->ScrollByAmount(ScrollBar::ScrollAmount::kNextPage);
  EXPECT_EQ(20, controller_->last_position);

  scrollbar()->ScrollByAmount(ScrollBar::ScrollAmount::kPrevPage);
  EXPECT_EQ(0, controller_->last_position);
}

TEST_F(ScrollBarViewsTest, ScrollBarFitsToBottom) {
  scrollbar()->Update(100, 1999, 0);
  EXPECT_EQ(0, scrollbar()->GetPosition());
  EXPECT_EQ(1899, scrollbar()->GetMaxPosition());
  EXPECT_EQ(0, scrollbar()->GetMinPosition());

  // Scroll to the midpoint of the document.
  scrollbar()->Update(100, 1999, 950);
  EXPECT_EQ((scrollbar()->GetTrackBounds().width() -
             scrollbar()->GetThumbLengthForTesting()) /
                2,
            scrollbar()->GetPosition());

  // Scroll to the end of the document.
  scrollbar()->Update(100, 1999, 1899);
  EXPECT_EQ(scrollbar()->GetTrackBounds().width() -
                scrollbar()->GetThumbLengthForTesting(),
            scrollbar()->GetPosition());
}

TEST_F(ScrollBarViewsTest, ScrollToEndAfterShrinkAndExpand) {
  // Scroll to the end of the content.
  scrollbar()->Update(100, 1001, 0);
  EXPECT_TRUE(scrollbar()->ScrollByContentsOffset(-1));
  // Shrink and then re-expand the content.
  scrollbar()->Update(100, 1000, 0);
  scrollbar()->Update(100, 1001, 0);
  // Ensure the scrollbar allows scrolling to the end.
  EXPECT_TRUE(scrollbar()->ScrollByContentsOffset(-1));
}

TEST_F(ScrollBarViewsTest, ThumbFullLengthOfTrack) {
  // Shrink content so that it fits within the viewport.
  scrollbar()->Update(100, 10, 0);
  EXPECT_EQ(scrollbar()->GetTrackBounds().width(),
            scrollbar()->GetThumbLengthForTesting());
  // Emulate a click on the full size scroll bar.
  scrollbar()->ScrollToThumbPosition(0, false);
  EXPECT_EQ(0, scrollbar()->GetPosition());
  // Emulate a key down.
  scrollbar()->ScrollByAmount(ScrollBar::ScrollAmount::kNextLine);
  EXPECT_EQ(0, scrollbar()->GetPosition());

  // Expand content so that it fits *exactly* within the viewport.
  scrollbar()->Update(100, 100, 0);
  EXPECT_EQ(scrollbar()->GetTrackBounds().width(),
            scrollbar()->GetThumbLengthForTesting());
  // Emulate a click on the full size scroll bar.
  scrollbar()->ScrollToThumbPosition(0, false);
  EXPECT_EQ(0, scrollbar()->GetPosition());
  // Emulate a key down.
  scrollbar()->ScrollByAmount(ScrollBar::ScrollAmount::kNextLine);
  EXPECT_EQ(0, scrollbar()->GetPosition());
  // Emulate a programmatic scroll to a non-zero offset. See crbug.com/1447967.
  scrollbar()->Update(100, 100, 10);
  EXPECT_EQ(scrollbar()->GetTrackBounds().width(),
            scrollbar()->GetThumbLengthForTesting());
}

TEST_F(ScrollBarViewsTest, AccessibleRole) {
  ui::AXNodeData data;
  scrollbar()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.role, ax::mojom::Role::kScrollBar);
  EXPECT_EQ(scrollbar()->GetViewAccessibility().GetCachedRole(),
            ax::mojom::Role::kScrollBar);

  data = ui::AXNodeData();
  scrollbar()->GetViewAccessibility().SetRole(ax::mojom::Role::kButton);
  scrollbar()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.role, ax::mojom::Role::kButton);
  EXPECT_EQ(scrollbar()->GetViewAccessibility().GetCachedRole(),
            ax::mojom::Role::kButton);
}

#if !BUILDFLAG(IS_MAC)
TEST_F(ScrollBarViewsTest, RightClickOpensMenu) {
  EXPECT_EQ(nullptr, scrollbar()->menu_model_);
  EXPECT_EQ(nullptr, scrollbar()->menu_runner_);
  scrollbar()->set_context_menu_controller(scrollbar());
  // Disabled on Mac because Mac's native menu is synchronous.
  scrollbar()->ShowContextMenu(scrollbar()->GetBoundsInScreen().CenterPoint(),
                               ui::mojom::MenuSourceType::kMouse);
  EXPECT_NE(nullptr, scrollbar()->menu_model_);
  EXPECT_NE(nullptr, scrollbar()->menu_runner_);
}

TEST_F(ScrollBarViewsTest, TestPageScrollingByPress) {
  ui::test::EventGenerator generator(GetRootWindow(widget_.get()));
  EXPECT_EQ(0, scrollbar()->GetPosition());
  generator.MoveMouseTo(
      scrollbar()->GetThumb()->GetBoundsInScreen().right_center() +
      gfx::Vector2d(4, 0));
  generator.ClickLeftButton();
  generator.ClickLeftButton();
  EXPECT_GT(scrollbar()->GetPosition(), 0);
}

TEST_F(ScrollBarViewsTest, DragThumbScrollsContent) {
  ui::test::EventGenerator generator(GetRootWindow(widget_.get()));
  EXPECT_EQ(0, scrollbar()->GetPosition());
  generator.MoveMouseTo(
      scrollbar()->GetThumb()->GetBoundsInScreen().CenterPoint());
  generator.PressLeftButton();
  generator.MoveMouseBy(15, 0);
  EXPECT_GE(scrollbar()->GetPosition(), 10);

  // Dragging the mouse somewhat outside the thumb maintains scroll.
  generator.MoveMouseBy(0, 100);
  EXPECT_GE(scrollbar()->GetPosition(), 10);

  // Dragging the mouse far outside the thumb snaps back to the initial
  // scroll position.
  generator.MoveMouseBy(0, 100);
  EXPECT_EQ(0, scrollbar()->GetPosition());
}

TEST_F(ScrollBarViewsTest, DragThumbScrollsContentWhenSnapBackDisabled) {
  scrollbar()->GetThumb()->SetSnapBackOnDragOutside(false);
  ui::test::EventGenerator generator(GetRootWindow(widget_.get()));
  EXPECT_EQ(0, scrollbar()->GetPosition());
  generator.MoveMouseTo(
      scrollbar()->GetThumb()->GetBoundsInScreen().CenterPoint());
  generator.PressLeftButton();
  generator.MoveMouseBy(10, 0);
  EXPECT_GT(scrollbar()->GetPosition(), 0);
  // Move the mouse far down, outside the thumb.
  generator.MoveMouseBy(0, 200);
  // Position does not snap back to zero.
  EXPECT_GT(scrollbar()->GetPosition(), 0);
}

TEST_F(ScrollBarViewsTest, FlingGestureScrollsView) {
  constexpr int kNumScrollSteps = 100;
  constexpr int kScrollVelocity = 10;
  ui::test::EventGenerator generator(GetRootWindow(widget_.get()));
  EXPECT_EQ(0, scrollbar()->GetPosition());
  const gfx::Point start_pos =
      widget_->GetContentsView()->GetBoundsInScreen().CenterPoint();
  const gfx::Point end_pos = start_pos + gfx::Vector2d(-100, 0);
  generator.GestureScrollSequence(
      start_pos, end_pos,
      generator.CalculateScrollDurationForFlingVelocity(
          start_pos, end_pos, kScrollVelocity, kNumScrollSteps),
      kNumScrollSteps);
  // Just make sure the view scrolled
  EXPECT_GT(scrollbar()->GetPosition(), 0);
}
#endif

}  // namespace views