File: md_text_button_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 (169 lines) | stat: -rw-r--r-- 6,571 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
// 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 "ui/views/controls/button/md_text_button.h"

#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/actions/actions.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/views/background.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/test/views_drawing_test_utils.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/test/widget_test.h"

namespace views {

using MdTextButtonTest = ViewsTestBase;

TEST_F(MdTextButtonTest, CustomPadding) {
  const std::u16string text = u"abc";
  auto button = std::make_unique<MdTextButton>(Button::PressedCallback(), text);

  const auto custom_padding = gfx::Insets::VH(10, 20);
  ASSERT_NE(button->GetInsets(), custom_padding);

  button->SetCustomPadding(custom_padding);
  EXPECT_EQ(button->GetInsets(), custom_padding);
}

TEST_F(MdTextButtonTest, BackgroundColorChangesWithWidgetActivation) {
  // Test whether the button's background color changes when its containing
  // widget's activation changes.
  if constexpr (!PlatformStyle::kInactiveWidgetControlsAppearDisabled) {
    GTEST_SKIP() << "Button colors do not change with widget activation here.";
  }

  std::unique_ptr<Widget> widget =
      CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
  auto* button = widget->SetContentsView(
      std::make_unique<MdTextButton>(Button::PressedCallback(), u" "));
  button->SetStyle(ui::ButtonStyle::kProminent);
  button->SetBounds(0, 0, 70, 20);
  widget->LayoutRootViewIfNecessary();

  const ui::ColorProvider* color_provider = button->GetColorProvider();

  test::WidgetTest::SimulateNativeActivate(widget.get());
  EXPECT_TRUE(widget->IsActive());
  SkBitmap active_bitmap = views::test::PaintViewToBitmap(button);

  auto background_color = [button](const SkBitmap& bitmap) {
    return bitmap.getColor(button->size().width() / 2.,
                           button->size().height() / 2.);
  };

  EXPECT_EQ(background_color(active_bitmap),
            color_provider->GetColor(ui::kColorButtonBackgroundProminent));

  // It would be neat to also check the text color here, but the label's text
  // ends up drawn on top of the background with antialiasing, which means there
  // aren't any pixels that are actually *exactly*
  // kColorButtonForegroundProminent. Bummer.

  // Activate another widget to cause the original widget to deactivate.
  std::unique_ptr<Widget> other_widget =
      CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
  test::WidgetTest::SimulateNativeActivate(other_widget.get());
  EXPECT_FALSE(widget->IsActive());
  SkBitmap inactive_bitmap = views::test::PaintViewToBitmap(button);

  EXPECT_EQ(
      background_color(inactive_bitmap),
      color_provider->GetColor(ui::kColorButtonBackgroundProminentDisabled));
}

using MdTextButtonActionViewInterfaceTest = ViewsTestBase;

TEST_F(MdTextButtonActionViewInterfaceTest, TestActionChanged) {
  auto md_text_button = std::make_unique<MdTextButton>();
  const std::u16string test_string = u"test_string";
  std::unique_ptr<actions::ActionItem> action_item =
      actions::ActionItem::Builder()
          .SetText(test_string)
          .SetActionId(0)
          .SetEnabled(false)
          .Build();
  action_item->SetText(test_string);
  md_text_button->GetActionViewInterface()->ActionItemChangedImpl(
      action_item.get());
  // Test some properties to ensure that the right ActionViewInterface is linked
  // to the view.
  EXPECT_EQ(test_string, md_text_button->GetText());
  EXPECT_FALSE(md_text_button->GetEnabled());
}

TEST_F(MdTextButtonActionViewInterfaceTest,
       DefaultCornerRadiusDependsOnButtonSize) {
  auto md_text_button = std::make_unique<MdTextButton>();
  constexpr gfx::Size kSize1(100, 100);
  constexpr gfx::Size kSize2(50, 50);

  const int corner_radius_1 = LayoutProvider::Get()->GetCornerRadiusMetric(
      ShapeContextTokens::kButtonRadius, kSize1);
  const int corner_radius_2 = LayoutProvider::Get()->GetCornerRadiusMetric(
      ShapeContextTokens::kButtonRadius, kSize2);
  ASSERT_NE(corner_radius_1, corner_radius_2);

  md_text_button->SetBoundsRect(gfx::Rect(kSize1));
  EXPECT_EQ(md_text_button->GetCornerRadii(),
            gfx::RoundedCornersF(corner_radius_1));
  EXPECT_EQ(md_text_button->GetFocusRingCornerRadii(),
            gfx::RoundedCornersF(corner_radius_1));

  md_text_button->SetBoundsRect(gfx::Rect(kSize2));
  EXPECT_EQ(md_text_button->GetCornerRadii(),
            gfx::RoundedCornersF(corner_radius_2));
  EXPECT_EQ(md_text_button->GetFocusRingCornerRadii(),
            gfx::RoundedCornersF(corner_radius_2));
}

TEST_F(MdTextButtonActionViewInterfaceTest,
       CustomCornerRadiusIsNotOverriddenOnButtonSizeChange) {
  auto md_text_button = std::make_unique<MdTextButton>();
  md_text_button->SetBoundsRect(gfx::Rect(100, 100));

  constexpr int kCustomCornerRadius = 1234;
  md_text_button->SetCornerRadius(kCustomCornerRadius);
  ASSERT_EQ(md_text_button->GetCornerRadii(),
            gfx::RoundedCornersF(kCustomCornerRadius));

  md_text_button->SetBoundsRect(gfx::Rect(50, 50));
  EXPECT_EQ(md_text_button->GetCornerRadii(),
            gfx::RoundedCornersF(kCustomCornerRadius));
  EXPECT_EQ(md_text_button->GetFocusRingCornerRadii(),
            gfx::RoundedCornersF(kCustomCornerRadius));
}

TEST_F(MdTextButtonActionViewInterfaceTest, CustomCornerRadii) {
  auto md_text_button = std::make_unique<MdTextButton>();
  md_text_button->SetBoundsRect(gfx::Rect(100, 100));

  gfx::RoundedCornersF kCustomCornerRadii(1, 5, 10, 20);
  md_text_button->SetCornerRadii(kCustomCornerRadii);
  EXPECT_EQ(md_text_button->GetCornerRadii(), kCustomCornerRadii);
  EXPECT_EQ(md_text_button->GetFocusRingCornerRadii(), kCustomCornerRadii);
}

TEST_F(MdTextButtonTest, StrokeColorIdOverride) {
  auto button = std::make_unique<MdTextButton>();

  ASSERT_FALSE(button->GetStrokeColorIdOverride().has_value());

  button->SetStrokeColorIdOverride(ui::kColorButtonBorder);
  EXPECT_EQ(ui::kColorButtonBorder, button->GetStrokeColorIdOverride().value());
}

TEST_F(MdTextButtonTest, StrokeColorOverride) {
  auto button = std::make_unique<MdTextButton>();

  ASSERT_FALSE(button->GetStrokeColorOverrideDeprecated().has_value());

  button->SetStrokeColorOverrideDeprecated(SK_ColorBLUE);
  EXPECT_EQ(SK_ColorBLUE, button->GetStrokeColorOverrideDeprecated().value());
}

}  // namespace views