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

Side by Side Diff: components/favicon_base/fallback_icon_style_builder_unittest.cc

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding new host chrome://fallback-icon. Created 5 years, 11 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 2015 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 "components/favicon_base/fallback_icon_style_builder.h"
6
7 #include <string>
8
9 #include "base/macros.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace favicon_base {
13 namespace test {
14
15 class FallbackIconStyleBuilderTest : public testing::Test {
16 public:
17 FallbackIconStyleBuilderTest() {
18 }
19 ~FallbackIconStyleBuilderTest() override {
20 }
21
22 private:
23 DISALLOW_COPY_AND_ASSIGN(FallbackIconStyleBuilderTest);
24 };
25
26 TEST_F(FallbackIconStyleBuilderTest, ParseColorSuccess) {
27 SkColor c;
28 EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("01aBf0f4", &c));
29 EXPECT_EQ(SkColorSetARGB(0xF4, 0x01, 0xAB, 0xF0), c);
30 EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("01aBf0", &c));
31 EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c);
32 EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("01a", &c));
33 EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c);
34 EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("000000", &c));
35 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c);
36 }
37
38 TEST_F(FallbackIconStyleBuilderTest, ParseColorFailure) {
39 const char* test_cases[] = {
40 "",
41 "00000",
42 "000000000",
43 "0 1 2 3 ",
44 " 0 1 2 3",
45 "0ABCDEFG",
46 "#000000",
47 "000000 ",
48 "red"
49 };
50 for (int i = 0; i < arraysize(test_cases); ++i) {
51 SkColor c;
52 EXPECT_FALSE(FallbackIconStyleBuilder::ParseColor(test_cases[i], &c))
53 << "test_cases[" << i << "]";
54 }
55 }
56
57 void CheckHas(const FallbackIconStyleBuilder& builder,
58 bool has_background_color,
59 bool has_text_color,
60 bool has_font_size_ratio,
61 bool has_roundness,
62 const std::string& msg) {
63 EXPECT_EQ(has_background_color,
64 builder.Has(FallbackIconStyleBuilder::BACKGROUND_COLOR)) << msg;
65 EXPECT_EQ(has_text_color,
66 builder.Has(FallbackIconStyleBuilder::TEXT_COLOR))<< msg;
67 EXPECT_EQ(has_font_size_ratio,
68 builder.Has(FallbackIconStyleBuilder::FONT_SIZE_RATIO)) << msg;
69 EXPECT_EQ(has_roundness,
70 builder.Has(FallbackIconStyleBuilder::ROUNDNESS)) << msg;
71 }
72
73 TEST_F(FallbackIconStyleBuilderTest, SettersAndGetters) {
74 FallbackIconStyleBuilder builder;
75 CheckHas(builder, false, false, false, false, "Case 0");
76
77 builder.SetTextColor(SK_ColorBLACK);
78 CheckHas(builder, false, true, false, false, "Case 1");
79
80 builder.SetRoundness(0.25);
81 CheckHas(builder, false, true, false, true, "Case 2");
82
83 builder.SetBackgroundColor(SK_ColorWHITE);
84 CheckHas(builder, true, true, false, true, "Case 3");
85
86 builder.SetFontSizeRatio(0.75);
87 CheckHas(builder, true, true, true, true, "Case 4");
88
89 FallbackIconStyle style = builder.Build();
90 EXPECT_EQ(SK_ColorBLACK, style.text_color);
91 EXPECT_EQ(0.25, style.roundness);
92 EXPECT_EQ(SK_ColorWHITE, style.background_color);
93 EXPECT_EQ(0.75, style.font_size_ratio);
94
95 builder.Reset();
96 CheckHas(builder, false, false, false, false, "Case 5");
97 }
98
99 TEST_F(FallbackIconStyleBuilderTest, DefaultAll) {
100 FallbackIconStyleBuilder builder;
101 // Get defaults.
102 builder.AssignDefaults();
103 CheckHas(builder, true, true, true, true, "Case 0");
104
105 FallbackIconStyle style = builder.Build();
106 // kDefaultBackgroundColor
107 EXPECT_EQ(SkColorSetRGB(0x80, 0x80, 0x80), style.background_color);
108 // kDefaultTextColorLight
109 EXPECT_EQ(SK_ColorWHITE, style.text_color);
110 // kDefaultFontSizeRatio
111 EXPECT_EQ(0.8, style.font_size_ratio);
112 // kDefaultRoundness
113 EXPECT_EQ(0.125, style.roundness);
114 }
115
116 TEST_F(FallbackIconStyleBuilderTest, DefaultTextColor) {
117 {
118 // Dark background -> Light default text.
119 FallbackIconStyleBuilder builder;
120 builder.SetBackgroundColor(SkColorSetRGB(0x00, 0x00, 0x00));
121 FallbackIconStyle style = builder.Build();
122 EXPECT_EQ(SK_ColorWHITE, style.text_color); // kDefaultTextColorLight
123 }
124 {
125 FallbackIconStyleBuilder builder;
126 // Light background -> Dark default text.
127 builder.SetBackgroundColor(SkColorSetRGB(0xFF, 0xFF, 0xFF));
128 FallbackIconStyle style = builder.Build();
129 EXPECT_EQ(SK_ColorBLACK, style.text_color); // kDefaultTextColorDark
130 }
131 }
132
133 TEST_F(FallbackIconStyleBuilderTest, ParseEmpty) {
134 FallbackIconStyleBuilder builder;
135 // Empty string is valid, although nothing gets set.
136 EXPECT_TRUE(builder.Parse(""));
137 CheckHas(builder, false, false, false, false, "Case 0");
138
139 // Empty fields also valid valid, and nothing gets set.
140 EXPECT_TRUE(builder.Parse(",,,"));
141 CheckHas(builder, false, false, false, false, "Case 1");
142 }
143
144 TEST_F(FallbackIconStyleBuilderTest, ParseBasic) {
145 FallbackIconStyleBuilder builder;
146 EXPECT_TRUE(builder.Parse("000,fff,0.75,0.25"));
147 CheckHas(builder, true, true, true, true, "Case 0");
148 FallbackIconStyle style = builder.Build();
149 EXPECT_EQ(SK_ColorBLACK, style.background_color);
150 EXPECT_EQ(SK_ColorWHITE, style.text_color);
151 EXPECT_EQ(0.75, style.font_size_ratio);
152 EXPECT_EQ(0.25, style.roundness);
153 }
154
155 TEST_F(FallbackIconStyleBuilderTest, ParseWithMissingFields) {
156 {
157 FallbackIconStyleBuilder builder;
158 EXPECT_TRUE(builder.Parse("000,fff,0.75"));
159 CheckHas(builder, true, true, true, false, "Case 0");
160 FallbackIconStyle style = builder.Build();
161 EXPECT_EQ(SK_ColorBLACK, style.background_color);
162 EXPECT_EQ(SK_ColorWHITE, style.text_color);
163 EXPECT_EQ(0.75, style.font_size_ratio);
164 EXPECT_EQ(0.125, style.roundness); // default
165 }
166
167 SkColor default_background_color = SkColorSetRGB(0x80, 0x80, 0x80);
168 {
169 FallbackIconStyleBuilder builder;
170 EXPECT_TRUE(builder.Parse(",f00,0.8,0"));
171 CheckHas(builder, false, true, true, true, "Case 1");
172 FallbackIconStyle style = builder.Build();
173 EXPECT_EQ(default_background_color, style.background_color); // default
174 EXPECT_EQ(SK_ColorRED, style.text_color);
175 EXPECT_EQ(0.8, style.font_size_ratio);
176 EXPECT_EQ(0, style.roundness);
177 }
178
179 {
180 FallbackIconStyleBuilder builder;
181 EXPECT_TRUE(builder.Parse(",0000FfFf"));
182 CheckHas(builder, false, true, false, false, "Case 2");
183 FallbackIconStyle style = builder.Build();
184 EXPECT_EQ(default_background_color, style.background_color); // default
185 EXPECT_EQ(SK_ColorBLUE, style.text_color);
186 EXPECT_EQ(0.8, style.font_size_ratio); // default
187 EXPECT_EQ(0.125, style.roundness); // default
188 }
189
190 {
191 FallbackIconStyleBuilder builder;
192 EXPECT_TRUE(builder.Parse("00FF00,,,0.1"));
193 CheckHas(builder, true, false, false, true, "Case 3");
194 FallbackIconStyle style = builder.Build();
195 EXPECT_EQ(SK_ColorGREEN, style.background_color);
196 EXPECT_EQ(SK_ColorWHITE, style.text_color); // default
197 EXPECT_EQ(0.8, style.font_size_ratio); // default
198 EXPECT_EQ(0.1, style.roundness);
199 }
200 }
201
202 TEST_F(FallbackIconStyleBuilderTest, ParseFailure) {
203 const char* test_cases[] = {
204 " ",
205 ",,,,",
206 "000,fff, 0.75,0.25", // Don't allow any space.
207 "000 ,fff,0.75,0.25",
208 "000,fff,0.75,0.25 ",
209 "000,fff,0.75,0.25,junk",
210 "junk,fff,0.75,0.25",
211 "000,junk,0.75,0.25",
212 "000,fff,junk,0.25",
213 "000,fff,0.75,junk",
214 "000,fff,-0,1.75", // FontSizeRatio out of bound.
215 "000,fff,1.1,0.75", // FontSizeRatio out of bound.
216 "000,fff,0.75,-0.1", // Roundness out of bound.
217 "000,fff,0.75,1.1", // Roundness out of bound.
218 };
219 for (int i = 0; i < arraysize(test_cases); ++i) {
220 FallbackIconStyleBuilder builder;
221 EXPECT_FALSE(builder.Parse(test_cases[i])) << "test_cases[" << i << "]";
222 }
223 }
224
225 } // namespace test
226 } // namespace favicon_base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698