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
|
// Copyright 2017 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/label_button_label.h"
#include <map>
#include <memory>
#include <optional>
#include "base/memory/raw_ptr.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_variant.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/test/views_test_base.h"
namespace views {
namespace {
// LabelButtonLabel subclass that reports its text color whenever a paint is
// scheduled.
class TestLabel : public internal::LabelButtonLabel {
public:
explicit TestLabel(SkColor* last_color,
std::optional<ui::ColorVariant>* last_requested_color)
: LabelButtonLabel(std::u16string(), views::style::CONTEXT_BUTTON),
last_color_(last_color),
last_requested_color_(last_requested_color) {}
TestLabel(const TestLabel&) = delete;
TestLabel& operator=(const TestLabel&) = delete;
// LabelButtonLabel:
void OnDidSchedulePaint(const gfx::Rect& r) override {
LabelButtonLabel::OnDidSchedulePaint(r);
*last_color_ = Label::GetEnabledColor();
*last_requested_color_ = Label::GetRequestedEnabledColor();
}
private:
raw_ptr<SkColor> last_color_;
raw_ptr<std::optional<ui::ColorVariant>> last_requested_color_;
};
} // namespace
class LabelButtonLabelTest : public ViewsTestBase {
public:
LabelButtonLabelTest() = default;
LabelButtonLabelTest(const LabelButtonLabelTest&) = delete;
LabelButtonLabelTest& operator=(const LabelButtonLabelTest&) = delete;
void SetUp() override {
ViewsTestBase::SetUp();
widget_ = CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
widget_->GetNativeTheme()->set_use_dark_colors(false);
widget_->SetContentsView(
std::make_unique<TestLabel>(&last_color_, &last_requested_color_));
label()->SetAutoColorReadabilityEnabled(false);
}
void TearDown() override {
widget_.reset();
ViewsTestBase::TearDown();
}
void SetUseDarkColors(bool use_dark_colors) {
ui::NativeTheme* native_theme = widget_->GetNativeTheme();
native_theme->set_use_dark_colors(use_dark_colors);
native_theme->NotifyOnNativeThemeUpdated();
}
TestLabel* label() {
return static_cast<TestLabel*>(widget_->GetContentsView());
}
protected:
SkColor last_color_ = gfx::kPlaceholderColor;
std::optional<ui::ColorVariant> last_requested_color_;
std::unique_ptr<views::Widget> widget_;
};
// Test that LabelButtonLabel reacts properly to themed and overridden colors.
TEST_F(LabelButtonLabelTest, Colors) {
// First one comes from the default theme. This check ensures the SK_ColorRED
// placeholder initializers were replaced.
SkColor default_theme_enabled_color =
label()->GetColorProvider()->GetColor(ui::kColorLabelForeground);
EXPECT_EQ(default_theme_enabled_color, last_color_);
label()->SetEnabled(false);
SkColor default_theme_disabled_color =
label()->GetColorProvider()->GetColor(ui::kColorLabelForegroundDisabled);
EXPECT_EQ(default_theme_disabled_color, last_color_);
SetUseDarkColors(true);
SkColor dark_theme_disabled_color =
label()->GetColorProvider()->GetColor(ui::kColorLabelForegroundDisabled);
EXPECT_NE(default_theme_disabled_color, dark_theme_disabled_color);
EXPECT_EQ(dark_theme_disabled_color, last_color_);
label()->SetEnabled(true);
SkColor dark_theme_enabled_color =
label()->GetColorProvider()->GetColor(ui::kColorLabelForeground);
EXPECT_NE(default_theme_enabled_color, dark_theme_enabled_color);
EXPECT_EQ(dark_theme_enabled_color, last_color_);
// Override the theme for the disabled color.
label()->SetDisabledColor(SK_ColorRED);
EXPECT_NE(SK_ColorRED, dark_theme_disabled_color);
// Still enabled, so not RED yet.
EXPECT_EQ(dark_theme_enabled_color, last_color_);
label()->SetEnabled(false);
EXPECT_EQ(SK_ColorRED, last_color_);
label()->SetDisabledColor(SK_ColorMAGENTA);
EXPECT_EQ(SK_ColorMAGENTA, last_color_);
// Disabled still overridden after a theme change.
SetUseDarkColors(false);
EXPECT_EQ(SK_ColorMAGENTA, last_color_);
// The enabled color still gets its value from the theme.
label()->SetEnabled(true);
EXPECT_EQ(default_theme_enabled_color, last_color_);
label()->SetEnabledColor(SK_ColorYELLOW);
label()->SetDisabledColor(SK_ColorCYAN);
EXPECT_EQ(SK_ColorYELLOW, last_color_);
label()->SetEnabled(false);
EXPECT_EQ(SK_ColorCYAN, last_color_);
}
// Test that LabelButtonLabel reacts properly to themed and overridden color
// ids.
TEST_F(LabelButtonLabelTest, ColorIds) {
// Default color id was set.
EXPECT_TRUE(last_requested_color_.has_value());
// Override the theme for the enabled color.
label()->SetEnabledColor(ui::kColorAccent);
EXPECT_EQ(last_requested_color_, ui::kColorAccent);
EXPECT_EQ(last_color_,
label()->GetColorProvider()->GetColor(ui::kColorAccent));
label()->SetEnabled(false);
label()->SetDisabledColor(ui::kColorBadgeBackground);
EXPECT_EQ(last_requested_color_, ui::kColorBadgeBackground);
EXPECT_EQ(last_color_,
label()->GetColorProvider()->GetColor(ui::kColorBadgeBackground));
// Still overridden after a theme change.
SetUseDarkColors(false);
EXPECT_EQ(last_requested_color_, ui::kColorBadgeBackground);
EXPECT_EQ(last_color_,
label()->GetColorProvider()->GetColor(ui::kColorBadgeBackground));
label()->SetEnabled(true);
EXPECT_EQ(last_requested_color_, ui::kColorAccent);
EXPECT_EQ(last_color_,
label()->GetColorProvider()->GetColor(ui::kColorAccent));
}
} // namespace views
|