Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: ui/views/controls/button/label_button_label_unittest.cc

Issue 2913933002: Move views::Label DisabledColor logic into views::LabelButtonLabel (Closed)
Patch Set: Back to PatchSet 11 Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/views/controls/button/label_button_label.h"
6
7 #include <memory>
8
9 #include "third_party/skia/include/core/SkColor.h"
10 #include "ui/native_theme/native_theme_base.h"
11 #include "ui/views/test/views_test_base.h"
12
13 namespace views {
14 namespace {
15
16 // Basic NativeTheme that can customize colors.
17 class TestNativeTheme : public ui::NativeThemeBase {
18 public:
19 TestNativeTheme() {}
20
21 void Set(ColorId id, SkColor color) { colors_[id] = color; }
22
23 // NativeThemeBase:
24 SkColor GetSystemColor(ColorId color_id) const override {
25 return colors_.count(color_id) ? colors_.find(color_id)->second
26 : SK_ColorMAGENTA;
27 }
28
29 private:
30 std::map<ColorId, SkColor> colors_;
31
32 DISALLOW_COPY_AND_ASSIGN(TestNativeTheme);
33 };
34
35 // LabelButtonLabel subclass that reports its text color whenever a paint is
36 // scheduled.
37 class TestLabel : public LabelButtonLabel {
38 public:
39 explicit TestLabel(SkColor* last_color)
40 : LabelButtonLabel(base::string16(), views::style::CONTEXT_BUTTON),
41 last_color_(last_color) {}
42
43 // LabelButtonLabel:
44 void SchedulePaintInRect(const gfx::Rect& r) override {
45 LabelButtonLabel::SchedulePaintInRect(r);
46 *last_color_ = enabled_color();
47 }
48
49 private:
50 SkColor* last_color_;
51
52 DISALLOW_COPY_AND_ASSIGN(TestLabel);
53 };
54
55 } // namespace
56
57 class LabelButtonLabelTest : public ViewsTestBase {
58 public:
59 LabelButtonLabelTest() {}
60
61 void SetUp() override {
62 ViewsTestBase::SetUp();
63 label_ = base::MakeUnique<TestLabel>(&last_color_);
64 }
65
66 protected:
67 SkColor last_color_ = SK_ColorCYAN;
68 std::unique_ptr<TestLabel> label_;
69 TestNativeTheme theme1_;
70 TestNativeTheme theme2_;
71
72 private:
73 DISALLOW_COPY_AND_ASSIGN(LabelButtonLabelTest);
74 };
75
76 // Test that LabelButtonLabel reacts properly to themed and overridden colors.
77 TEST_F(LabelButtonLabelTest, Colors) {
78 // The SchedulePaintInRect override won't be called while the base class is
79 // initialized. Not much we can do about that, so give it the first for free.
80 EXPECT_EQ(SK_ColorCYAN, last_color_); // Sanity check.
81
82 // At the same time we can check that changing the auto color readability
83 // schedules a paint. Currently it does. Although it technically doesn't need
84 // to since the color isn't actually changing.
85 label_->SetAutoColorReadabilityEnabled(false);
86
87 // First one comes from the default theme. This check ensures the SK_ColorRED
88 // placeholder initializers were replaced.
89 SkColor default_theme_enabled_color =
90 label_->GetNativeTheme()->GetSystemColor(
91 ui::NativeTheme::kColorId_LabelEnabledColor);
92 EXPECT_EQ(default_theme_enabled_color, last_color_);
93
94 // Note these are not kColorId_Button{Enabled,Disabled}Color because "Button
95 // colors are used only for STYLE_BUTTON, otherwise we use label colors." See
96 // LabelButton::ResetColorsFromNativeTheme().
97 theme1_.Set(ui::NativeTheme::kColorId_LabelEnabledColor, SK_ColorGREEN);
98 theme1_.Set(ui::NativeTheme::kColorId_LabelDisabledColor, SK_ColorYELLOW);
99 label_->SetNativeTheme(&theme1_);
100
101 // Setting the theme should paint.
102 EXPECT_EQ(SK_ColorGREEN, last_color_);
103
104 label_->SetEnabled(false);
105 EXPECT_EQ(SK_ColorYELLOW, last_color_);
106
107 // Set up a second theme. View::SetNativeTheme() makes the reasonable
108 // assumption that NativeTheme doesn't change its mind about things unless a
109 // Widget triggers it (which it can do as a friend of RootView).
110 theme2_.Set(ui::NativeTheme::kColorId_LabelEnabledColor, SK_ColorBLUE);
111 theme2_.Set(ui::NativeTheme::kColorId_LabelDisabledColor, SK_ColorGRAY);
112 label_->SetNativeTheme(&theme2_);
113
114 EXPECT_EQ(SK_ColorGRAY, last_color_);
115
116 label_->SetEnabled(true);
117 EXPECT_EQ(SK_ColorBLUE, last_color_);
118
119 // Override the theme for the disabled color.
120 label_->SetDisabledColor(SK_ColorRED);
121
122 // Still enabled, so not RED yet.
123 EXPECT_EQ(SK_ColorBLUE, last_color_);
124
125 label_->SetEnabled(false);
126 EXPECT_EQ(SK_ColorRED, last_color_);
127
128 label_->SetDisabledColor(SK_ColorMAGENTA);
129 EXPECT_EQ(SK_ColorMAGENTA, last_color_);
130
131 // Disabled still overridden after a theme change.
132 label_->SetNativeTheme(&theme1_);
133 EXPECT_EQ(SK_ColorMAGENTA, last_color_);
134
135 // The enabled color still gets its value from the theme.
136 label_->SetEnabled(true);
137 EXPECT_EQ(SK_ColorGREEN, last_color_);
138
139 label_->SetEnabledColor(SK_ColorYELLOW);
140 label_->SetDisabledColor(SK_ColorCYAN);
141 EXPECT_EQ(SK_ColorYELLOW, last_color_);
142 label_->SetEnabled(false);
143 EXPECT_EQ(SK_ColorCYAN, last_color_);
144 }
145
146 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698