Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / favicon_base / fallback_icon_url_parser_unittest.cc
blobaf36304dc4c9666798060077c7d9bfa8ac56f5ee
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 "components/favicon_base/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"
12 #include "url/gurl.h"
14 using chrome::ParsedFallbackIconPath;
15 using favicon_base::FallbackIconStyle;
17 namespace chrome {
19 namespace {
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";
31 } // namespace
33 class FallbackIconUrlParserTest : public testing::Test {
34 public:
35 FallbackIconUrlParserTest() {
38 bool ParseSpecs(const std::string& specs_str,
39 int *size,
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);
48 private:
49 DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest);
52 TEST_F(FallbackIconUrlParserTest, ParseColorSuccess) {
53 SkColor c;
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[] = {
70 "",
71 "00000",
72 "000000000",
73 " 000000",
74 "ABCDEFG",
75 "#000",
76 "#000000",
77 "000000 ",
78 "ABCDEFH",
79 "#ABCDEF",
81 for (size_t i = 0; i < arraysize(test_cases); ++i) {
82 SkColor c;
83 EXPECT_FALSE(ParseColor(test_cases[i], &c))
84 << "test_cases[" << i << "]";
88 TEST_F(FallbackIconUrlParserTest, ParseSpecsEmpty) {
89 int size;
90 FallbackIconStyle style;
91 EXPECT_TRUE(ParseSpecs(",,,,", &size, &style));
92 EXPECT_EQ(16, size);
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) {
100 int size;
101 FallbackIconStyle style;
102 EXPECT_TRUE(ParseSpecs(",,aCE,,0.1", &size, &style));
103 EXPECT_EQ(16, size);
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) {
111 int size;
114 FallbackIconStyle style;
115 EXPECT_TRUE(ParseSpecs("16,000,f01,0.75,0.25", &size, &style));
116 EXPECT_EQ(16, size);
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));
126 EXPECT_EQ(48, size);
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));
136 EXPECT_EQ(1, size);
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) {
145 int size;
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.
173 "16",
174 "16,black",
175 "16,black,fff",
176 "16,black,fff,0.75",
177 ",,,"
178 ",,,,,",
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 ",
184 // Adding junk text.
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",
191 // Out of bound.
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) {
201 int size;
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());
226 // Empty URL.
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[] = {
272 // Bad size.
273 "-1,000,fff,0.75,0.25/http://www.google.com/",
274 // Bad specs.
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