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.
5 #include "chrome/common/favicon/fallback_icon_url_parser.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "components/favicon_base/fallback_icon_style.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkColor.h"
11 #include "ui/gfx/favicon_size.h"
14 using chrome::ParsedFallbackIconPath
;
15 using favicon_base::FallbackIconStyle
;
21 // Default values for FallbackIconStyle, from
22 // /components/favicon_base/fallback_icon_style.h
23 const SkColor kDefaultBackgroundColor
= SkColorSetRGB(0x78, 0x78, 0x78);
24 const SkColor kDefaultTextColorDark
= SK_ColorBLACK
;
25 const SkColor kDefaultTextColorLight
= SK_ColorWHITE
;
26 const double kDefaultFontSizeRatio
= 0.44;
27 const double kDefaultRoundness
= 0;
29 const char kTestUrlStr
[] = "https://www.google.ca/imghp?hl=en&tab=wi";
33 class FallbackIconUrlParserTest
: public testing::Test
{
35 FallbackIconUrlParserTest() {
38 bool ParseSpecs(const std::string
& specs_str
,
40 favicon_base::FallbackIconStyle
* style
) {
41 return ParsedFallbackIconPath::ParseSpecs(specs_str
, size
, style
);
44 bool ParseColor(const std::string
& color_str
, SkColor
* color
) {
45 return ParsedFallbackIconPath::ParseColor(color_str
, color
);
49 DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest
);
52 TEST_F(FallbackIconUrlParserTest
, ParseColorSuccess
) {
54 EXPECT_TRUE(ParseColor("31aBf0f4", &c
));
55 EXPECT_EQ(SkColorSetARGB(0x31, 0xAB, 0xF0, 0xF4), c
);
56 EXPECT_TRUE(ParseColor("01aBf0", &c
));
57 EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c
);
58 EXPECT_TRUE(ParseColor("501a", &c
));
59 EXPECT_EQ(SkColorSetARGB(0x55, 0x00, 0x11, 0xAA), c
);
60 EXPECT_TRUE(ParseColor("01a", &c
));
61 EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c
);
62 EXPECT_TRUE(ParseColor("000000", &c
));
63 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c
);
64 EXPECT_TRUE(ParseColor("red", &c
));
65 EXPECT_EQ(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00), c
);
68 TEST_F(FallbackIconUrlParserTest
, ParseColorFailure
) {
69 const char* test_cases
[] = {
81 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
83 EXPECT_FALSE(ParseColor(test_cases
[i
], &c
))
84 << "test_cases[" << i
<< "]";
88 TEST_F(FallbackIconUrlParserTest
, ParseSpecsEmpty
) {
90 FallbackIconStyle style
;
91 EXPECT_TRUE(ParseSpecs(",,,,", &size
, &style
));
93 EXPECT_EQ(kDefaultBackgroundColor
, style
.background_color
);
94 EXPECT_EQ(kDefaultTextColorLight
, style
.text_color
);
95 EXPECT_EQ(kDefaultFontSizeRatio
, style
.font_size_ratio
);
96 EXPECT_EQ(kDefaultRoundness
, style
.roundness
);
99 TEST_F(FallbackIconUrlParserTest
, ParseSpecsPartial
) {
101 FallbackIconStyle style
;
102 EXPECT_TRUE(ParseSpecs(",,aCE,,0.1", &size
, &style
));
104 EXPECT_EQ(kDefaultBackgroundColor
, style
.background_color
);
105 EXPECT_EQ(SkColorSetRGB(0xAA, 0xCC, 0xEE), style
.text_color
);
106 EXPECT_EQ(kDefaultFontSizeRatio
, style
.font_size_ratio
);
107 EXPECT_EQ(0.1, style
.roundness
);
110 TEST_F(FallbackIconUrlParserTest
, ParseSpecsFull
) {
114 FallbackIconStyle style
;
115 EXPECT_TRUE(ParseSpecs("16,000,f01,0.75,0.25", &size
, &style
));
117 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style
.background_color
);
118 EXPECT_EQ(SkColorSetRGB(0xff, 0x00, 0x11), style
.text_color
);
119 EXPECT_EQ(0.75, style
.font_size_ratio
);
120 EXPECT_EQ(0.25, style
.roundness
);
124 FallbackIconStyle style
;
125 EXPECT_TRUE(ParseSpecs("48,black,123456,0.5,0.3", &size
, &style
));
127 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style
.background_color
);
128 EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style
.text_color
);
129 EXPECT_EQ(0.5, style
.font_size_ratio
);
130 EXPECT_EQ(0.3, style
.roundness
);
134 FallbackIconStyle style
;
135 EXPECT_TRUE(ParseSpecs("1,000,red,0,0", &size
, &style
));
137 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style
.background_color
);
138 EXPECT_EQ(SkColorSetRGB(0xFF, 0x00, 0x00), style
.text_color
);
139 EXPECT_EQ(0, style
.font_size_ratio
);
140 EXPECT_EQ(0, style
.roundness
);
144 TEST_F(FallbackIconUrlParserTest
, ParseSpecsDefaultTextColor
) {
148 // Dark background -> Light text.
149 FallbackIconStyle style
;
150 EXPECT_TRUE(ParseSpecs(",000,,,", &size
, &style
));
151 EXPECT_EQ(kDefaultTextColorLight
, style
.text_color
);
155 // Light background -> Dark text.
156 FallbackIconStyle style
;
157 EXPECT_TRUE(ParseSpecs(",fff,,,", &size
, &style
));
158 EXPECT_EQ(kDefaultTextColorDark
, style
.text_color
);
162 // Light background -> Dark text, more params don't matter.
163 FallbackIconStyle style
;
164 EXPECT_TRUE(ParseSpecs("107,fff,,0.3,0.5", &size
, &style
));
165 EXPECT_EQ(kDefaultTextColorDark
, style
.text_color
);
169 TEST_F(FallbackIconUrlParserTest
, ParseSpecsFailure
) {
170 const char* test_cases
[] = {
171 // Need exactly 5 params.
179 "16,black,fff,0.75,0.25,junk",
180 // Don't allow any space.
181 "16,black,fff, 0.75,0.25",
182 "16,black ,fff,0.75,0.25",
183 "16,black,fff,0.75,0.25 ",
185 "16,black,fff,0.75,0.25junk",
186 "junk,black,fff,0.75,0.25",
187 "16,#junk,fff,0.75,0.25",
188 "16,black,junk,0.75,0.25",
189 "16,black,fff,junk,0.25",
190 "16,black,fff,0.75,junk",
192 "0,black,fff,0.75,0.25", // size.
193 "4294967296,black,fff,0.75,0.25", // size.
194 "-1,black,fff,0.75,0.25", // size.
195 "16,black,fff,-0.1,0.25", // font_size_ratio.
196 "16,black,fff,1.1,0.25", // font_size_ratio.
197 "16,black,fff,0.75,-0.1", // roundness.
198 "16,black,fff,0.75,1.1", // roundness.
200 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
202 FallbackIconStyle style
;
203 EXPECT_FALSE(ParseSpecs(test_cases
[i
], &size
, &style
))
204 << "test_cases[" << i
<< "]";
209 TEST_F(FallbackIconUrlParserTest
, ParseFallbackIconPathSuccess
) {
210 const std::string specs
= "31,black,fff,0.75,0.25";
212 // Everything populated.
214 chrome::ParsedFallbackIconPath parsed
;
215 EXPECT_TRUE(parsed
.Parse(specs
+ "/" + kTestUrlStr
));
216 EXPECT_EQ(31, parsed
.size_in_pixels());
217 const favicon_base::FallbackIconStyle
& style
= parsed
.style();
218 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style
.background_color
);
219 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style
.text_color
);
220 EXPECT_EQ(0.75, style
.font_size_ratio
);
221 EXPECT_EQ(0.25, style
.roundness
);
222 EXPECT_EQ(GURL(kTestUrlStr
), GURL(parsed
.url_string()));
223 EXPECT_EQ(specs
.length() + 1, parsed
.path_index());
228 chrome::ParsedFallbackIconPath parsed
;
229 EXPECT_TRUE(parsed
.Parse(specs
+ "/"));
230 EXPECT_EQ(31, parsed
.size_in_pixels());
231 const favicon_base::FallbackIconStyle
& style
= parsed
.style();
232 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style
.background_color
);
233 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style
.text_color
);
234 EXPECT_EQ(0.75, style
.font_size_ratio
);
235 EXPECT_EQ(0.25, style
.roundness
);
236 EXPECT_EQ(GURL(), GURL(parsed
.url_string()));
237 EXPECT_EQ(specs
.length() + 1, parsed
.path_index());
240 // Tolerate invalid URL.
242 chrome::ParsedFallbackIconPath parsed
;
243 EXPECT_TRUE(parsed
.Parse(specs
+ "/NOT A VALID URL"));
244 EXPECT_EQ(31, parsed
.size_in_pixels());
245 const favicon_base::FallbackIconStyle
& style
= parsed
.style();
246 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style
.background_color
);
247 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style
.text_color
);
248 EXPECT_EQ(0.75, style
.font_size_ratio
);
249 EXPECT_EQ(0.25, style
.roundness
);
250 EXPECT_EQ("NOT A VALID URL", parsed
.url_string());
251 EXPECT_EQ(specs
.length() + 1, parsed
.path_index());
254 // Size and style are default.
256 std::string specs2
= ",,,,";
257 chrome::ParsedFallbackIconPath parsed
;
258 EXPECT_TRUE(parsed
.Parse(specs2
+ "/" + kTestUrlStr
));
259 EXPECT_EQ(gfx::kFaviconSize
, parsed
.size_in_pixels());
260 const favicon_base::FallbackIconStyle
& style
= parsed
.style();
261 EXPECT_EQ(kDefaultBackgroundColor
, style
.background_color
);
262 EXPECT_EQ(kDefaultTextColorLight
, style
.text_color
);
263 EXPECT_EQ(kDefaultFontSizeRatio
, style
.font_size_ratio
);
264 EXPECT_EQ(kDefaultRoundness
, style
.roundness
);
265 EXPECT_EQ(GURL(kTestUrlStr
), GURL(parsed
.url_string()));
266 EXPECT_EQ(specs2
.length() + 1, parsed
.path_index());
270 TEST_F(FallbackIconUrlParserTest
, ParseFallbackIconPathFailure
) {
271 const char* test_cases
[] = {
273 "-1,000,fff,0.75,0.25/http://www.google.com/",
275 "32,#junk,fff,0.75,0.25/http://www.google.com/",
277 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
278 chrome::ParsedFallbackIconPath parsed
;
279 EXPECT_FALSE(parsed
.Parse(test_cases
[i
])) << "test_cases[" << i
<< "]";
283 } // namespace chrome