File: scroll_view_gradient_helper_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (157 lines) | stat: -rw-r--r-- 5,420 bytes parent folder | download
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
// Copyright 2021 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 "ash/controls/gradient_layer_delegate.h"
#include "ash/controls/scroll_view_gradient_helper.h"
#include "base/memory/raw_ptr.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_type.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_utils.h"

namespace ash {
namespace {

constexpr int kWidgetHeight = 100;
constexpr int kWidgetWidth = 100;
constexpr int kGradientSize = 16;

// Uses ViewsTestBase because we may want to move this helper into //ui/views
// in the future.
class ScrollViewGradientHelperTest : public views::ViewsTestBase {
 public:
  ScrollViewGradientHelperTest() = default;
  ~ScrollViewGradientHelperTest() override = default;

  // testing::Test:
  void SetUp() override {
    ViewsTestBase::SetUp();

    // Create a small widget.
    widget_ = std::make_unique<views::Widget>();
    views::Widget::InitParams params =
        CreateParams(views::Widget::InitParams::TYPE_POPUP);
    params.bounds = gfx::Rect(10, 10, kWidgetWidth, kWidgetHeight);
    widget_->Init(std::move(params));
    widget_->Show();

    // Add a scroll view and gradient helper.
    auto* contents = widget_->SetContentsView(std::make_unique<views::View>());
    scroll_view_ =
        contents->AddChildView(std::make_unique<views::ScrollView>());
    scroll_view_->SetBounds(0, 0, kWidgetWidth, kWidgetHeight);
    scroll_view_->SetPaintToLayer(ui::LAYER_NOT_DRAWN);
    gradient_helper_ =
        std::make_unique<ScrollViewGradientHelper>(scroll_view_, kGradientSize);
  }

  void TearDown() override {
    gradient_helper_.reset();
    widget_.reset();
    ViewsTestBase::TearDown();
  }

  void AddScrollViewContentsWithHeight(int height) {
    auto contents = std::make_unique<views::View>();
    contents->SetSize({kWidgetWidth, height});
    scroll_view_->SetContents(std::move(contents));
    views::test::RunScheduledLayout(scroll_view_);
  }

  bool HasGradientAtTop() {
    const auto& gradient_mask = gradient_helper_->gradient_mask_for_test();
    EXPECT_FALSE(gradient_mask.IsEmpty());
    return cc::MathUtil::IsWithinEpsilon(gradient_mask.steps()[0].fraction,
                                         0.f);
  }

  bool HasGradientAtBottom() {
    const auto& gradient_mask = gradient_helper_->gradient_mask_for_test();
    EXPECT_FALSE(gradient_mask.IsEmpty());
    return cc::MathUtil::IsWithinEpsilon(
        gradient_mask.steps()[gradient_mask.step_count() - 1].fraction, 1.f);
  }

  bool HasGradientMask(const ui::Layer* layer) {
    return !layer->gradient_mask().IsEmpty();
  }

  views::UniqueWidgetPtr widget_;
  raw_ptr<views::ScrollView, DanglingUntriaged | ExperimentalAsh> scroll_view_ =
      nullptr;
  std::unique_ptr<ScrollViewGradientHelper> gradient_helper_;
};

TEST_F(ScrollViewGradientHelperTest, NoGradientForViewThatDoesNotScroll) {
  // Create a short contents view, so the scroll view won't scroll.
  AddScrollViewContentsWithHeight(10);
  gradient_helper_->UpdateGradientMask();

  EXPECT_FALSE(HasGradientMask(scroll_view_->layer()));
  EXPECT_TRUE(gradient_helper_->gradient_mask_for_test().IsEmpty());
}

TEST_F(ScrollViewGradientHelperTest, HasGradientForViewThatScrolls) {
  // Create a tall contents view.
  AddScrollViewContentsWithHeight(500);
  gradient_helper_->UpdateGradientMask();

  // Gradient is shown.
  EXPECT_TRUE(HasGradientMask(scroll_view_->layer()));
  EXPECT_FALSE(gradient_helper_->gradient_mask_for_test().IsEmpty());

  // Shrink the contents view.
  scroll_view_->contents()->SetSize({kWidgetWidth, 10});
  views::test::RunScheduledLayout(scroll_view_);
  gradient_helper_->UpdateGradientMask();

  // Gradient is removed.
  EXPECT_FALSE(HasGradientMask(scroll_view_->layer()));
  EXPECT_TRUE(gradient_helper_->gradient_mask_for_test().IsEmpty());
}

TEST_F(ScrollViewGradientHelperTest, ShowsGradientsBasedOnScrollPosition) {
  // Create a tall contents view.
  AddScrollViewContentsWithHeight(500);
  ASSERT_TRUE(scroll_view_->vertical_scroll_bar());
  gradient_helper_->UpdateGradientMask();

  // Because the scroll position is at the top, only the bottom gradient shows.
  EXPECT_FALSE(HasGradientAtTop());
  EXPECT_TRUE(HasGradientAtBottom());

  // Scroll down. Now both top and bottom should have gradients.
  scroll_view_->vertical_scroll_bar()->ScrollByAmount(
      views::ScrollBar::ScrollAmount::kNextLine);
  EXPECT_TRUE(HasGradientAtTop());
  EXPECT_TRUE(HasGradientAtBottom());

  // Scroll to end. Now only the top should have a gradient.
  scroll_view_->vertical_scroll_bar()->ScrollByAmount(
      views::ScrollBar::ScrollAmount::kEnd);
  EXPECT_TRUE(HasGradientAtTop());
  EXPECT_FALSE(HasGradientAtBottom());
}

TEST_F(ScrollViewGradientHelperTest, DeletingHelperRemovesMaskLayer) {
  // Create a tall contents view.
  AddScrollViewContentsWithHeight(500);
  gradient_helper_->UpdateGradientMask();

  // Precondition: Mask exists.
  ASSERT_TRUE(HasGradientMask(scroll_view_->layer()));

  gradient_helper_.reset();
  ASSERT_FALSE(HasGradientMask(scroll_view_->layer()));
}

}  // namespace
}  // namespace ash