File: page_action_icon_view_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (276 lines) | stat: -rw-r--r-- 9,439 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"

#include "base/memory/raw_ptr.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/command_updater_impl.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/widget/widget_utils.h"

namespace {

constexpr int kPageActionIconSize = 20;

class TestPageActionIconDelegate : public IconLabelBubbleView::Delegate,
                                   public PageActionIconView::Delegate {
 public:
  TestPageActionIconDelegate() = default;
  virtual ~TestPageActionIconDelegate() = default;

  // IconLabelBubbleView::Delegate:
  SkColor GetIconLabelBubbleSurroundingForegroundColor() const override {
    return gfx::kPlaceholderColor;
  }
  SkColor GetIconLabelBubbleBackgroundColor() const override {
    return gfx::kPlaceholderColor;
  }

  // PageActionIconView::Delegate:
  content::WebContents* GetWebContentsForPageActionIconView() override {
    return nullptr;
  }
  int GetPageActionIconSize() const override { return kPageActionIconSize; }
  bool ShouldHidePageActionIcons() const override {
    return should_hide_page_action_icons_;
  }

  void set_should_hide_page_action_icons(bool should_hide_page_action_icons) {
    should_hide_page_action_icons_ = should_hide_page_action_icons;
  }

 private:
  bool should_hide_page_action_icons_ = false;
};

}  // namespace

namespace {

class TestPageActionIconView : public PageActionIconView {
 public:
  using PageActionIconView::AnimateIn;

  explicit TestPageActionIconView(
      CommandUpdater* command_updater,
      int command_id,
      IconLabelBubbleView::Delegate* parent_delegate,
      PageActionIconView::Delegate* delegate,
      const gfx::FontList& font_list)
      : PageActionIconView(command_updater,
                           command_id,
                           parent_delegate,
                           delegate,
                           "TestName",
                           0,
                           nullptr,
                           true,
                           font_list) {
    SetUpForInOutAnimation();
    GetViewAccessibility().SetName(u"TestTooltip");
  }

  views::BubbleDialogDelegate* GetBubble() const override { return nullptr; }

  bool IsBubbleShowing() const override {
    return is_bubble_showing_override_ ? true
                                       : PageActionIconView::IsBubbleShowing();
  }

  bool IsLabelVisible() const { return label()->GetVisible(); }

  void SetIsBubbleShowingOverride(bool is_bubble_showing_override) {
    is_bubble_showing_override_ = is_bubble_showing_override;
    Update();
  }

 protected:
  // PageActionIconView:
  void OnExecuting(ExecuteSource execute_source) override {}
  const gfx::VectorIcon& GetVectorIcon() const override {
    return gfx::VectorIcon::EmptyIcon();
  }
  void UpdateImpl() override {}

 private:
  bool is_bubble_showing_override_ = false;
};

class TestPageActionIconViewWithIconImage : public TestPageActionIconView {
 public:
  using TestPageActionIconView::TestPageActionIconView;

  void SetIconImageColorForTesting(SkColor icon_image_color) {
    icon_image_color_ = icon_image_color;
  }

  void UpdateIconImageForTesting() { UpdateIconImage(); }

  // TestPageActionIconView:
  ui::ImageModel GetSizedIconImage(int size) const override {
    SkBitmap bitmap;
    bitmap.allocN32Pixels(size, size);
    bitmap.eraseColor(icon_image_color_);
    return ui::ImageModel::FromImageSkia(
        gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
  }

 private:
  SkColor icon_image_color_ = gfx::kPlaceholderColor;
};

}  // namespace

class PageActionIconViewTest : public ChromeViewsTestBase {
 protected:
  // ChromeViewsTestBase:
  void SetUp() override {
    ChromeViewsTestBase::SetUp();

    widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    delegate_ = TestPageActionIconDelegate();
    view_ = widget_->SetContentsView(std::make_unique<TestPageActionIconView>(
        /*command_updater=*/nullptr,
        /*command_id=*/0, delegate(), delegate(), gfx::FontList()));

    widget_->Show();
  }
  void TearDown() override {
    ClearView();
    widget_.reset();
    ChromeViewsTestBase::TearDown();
  }

  void ClearView() { view_ = nullptr; }

  TestPageActionIconView* view() { return view_; }
  views::Widget* widget() { return widget_.get(); }
  TestPageActionIconDelegate* delegate() { return &delegate_; }

 private:
  TestPageActionIconDelegate delegate_;
  raw_ptr<TestPageActionIconView> view_ = nullptr;
  std::unique_ptr<views::Widget> widget_;
};

TEST_F(PageActionIconViewTest, ShouldResetSlideAnimationWhenHideIcons) {
  view()->AnimateIn(std::nullopt);
  EXPECT_TRUE(view()->IsLabelVisible());
  EXPECT_TRUE(view()->is_animating_label());

  delegate()->set_should_hide_page_action_icons(true);
  view()->Update();
  EXPECT_FALSE(view()->is_animating_label());
  EXPECT_FALSE(view()->ShouldShowLabel());
  EXPECT_FALSE(view()->IsLabelVisible());
}

TEST_F(PageActionIconViewTest, TooltipText) {
  view()->AnimateIn(std::nullopt);
  EXPECT_FALSE(view()->IsBubbleShowing());
  EXPECT_EQ(view()->GetRenderedTooltipText(gfx::Point()),
            view()->GetTextForTooltipAndAccessibleName());
  EXPECT_EQ(view()->GetRenderedTooltipText(gfx::Point()), u"TestTooltip");

  view()->GetViewAccessibility().SetName(u"NewTooltip");

  EXPECT_FALSE(view()->IsBubbleShowing());
  EXPECT_EQ(view()->GetRenderedTooltipText(gfx::Point()),
            view()->GetTextForTooltipAndAccessibleName());
  EXPECT_EQ(view()->GetRenderedTooltipText(gfx::Point()), u"NewTooltip");

  view()->SetIsBubbleShowingOverride(true);

  EXPECT_TRUE(view()->IsBubbleShowing());
  EXPECT_EQ(view()->GetRenderedTooltipText(gfx::Point()), u"");
}

TEST_F(PageActionIconViewTest, TooltipTextAccessibility) {
  view()->AnimateIn(std::nullopt);

  ui::AXNodeData data;
  EXPECT_FALSE(view()->IsBubbleShowing());
  view()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetString16Attribute(ax::mojom::StringAttribute::kName),
            view()->GetTooltipText());
  EXPECT_NE(data.GetString16Attribute(ax::mojom::StringAttribute::kDescription),
            view()->GetTooltipText());

  view()->GetViewAccessibility().SetName(u"NewTooltip");

  data = ui::AXNodeData();
  EXPECT_FALSE(view()->IsBubbleShowing());
  view()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetString16Attribute(ax::mojom::StringAttribute::kName),
            view()->GetTooltipText());
  EXPECT_NE(data.GetString16Attribute(ax::mojom::StringAttribute::kDescription),
            view()->GetTooltipText());

  view()->SetIsBubbleShowingOverride(true);

  data = ui::AXNodeData();
  EXPECT_TRUE(view()->IsBubbleShowing());
  view()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_NE(data.GetString16Attribute(ax::mojom::StringAttribute::kName),
            view()->GetTooltipText());
  EXPECT_EQ(data.GetString16Attribute(ax::mojom::StringAttribute::kDescription),
            view()->GetTooltipText());
}

TEST_F(PageActionIconViewTest, ShouldNotResetSlideAnimationWhenShowIcons) {
  delegate()->set_should_hide_page_action_icons(true);
  view()->AnimateIn(std::nullopt);
  EXPECT_TRUE(view()->IsLabelVisible());
  EXPECT_TRUE(view()->is_animating_label());

  delegate()->set_should_hide_page_action_icons(false);
  view()->Update();
  EXPECT_TRUE(view()->is_animating_label());
  EXPECT_TRUE(view()->ShouldShowLabel());
  EXPECT_TRUE(view()->IsLabelVisible());
}

TEST_F(PageActionIconViewTest, UsesIconImageIfAvailable) {
  auto delegate = TestPageActionIconDelegate();

  // We're about to reset the 'ContentsView' of the Widget. As such
  // we need to clear the reference to |view_| beforehand, otherwise
  // it will become dangling.
  ClearView();
  auto* icon_view = widget()->SetContentsView(
      std::make_unique<TestPageActionIconViewWithIconImage>(
          /*command_updater=*/nullptr,
          /*command_id=*/0, &delegate, &delegate, gfx::FontList()));

  // Get the initial icon view image.
  auto image_previous = icon_view->GetImage(views::Button::STATE_NORMAL);

  // Update the page action icon view to host a green image. This should
  // override the initially set image.
  icon_view->SetIconImageColorForTesting(SK_ColorGREEN);
  icon_view->UpdateIconImageForTesting();
  EXPECT_FALSE(image_previous.BackedBySameObjectAs(
      icon_view->GetImage(views::Button::STATE_NORMAL)));

  // Update the page action icon view to host a red image. This should override
  // the green image.
  image_previous = icon_view->GetImage(views::Button::STATE_NORMAL);
  icon_view->SetIconImageColorForTesting(SK_ColorRED);
  icon_view->UpdateIconImageForTesting();
  EXPECT_FALSE(image_previous.BackedBySameObjectAs(
      icon_view->GetImage(views::Button::STATE_NORMAL)));
}

TEST_F(PageActionIconViewTest, IconViewAccessibleName) {
  EXPECT_EQ(view()->GetViewAccessibility().GetCachedName(),
            view()->GetTextForTooltipAndAccessibleName());
}