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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/favicon_base/fallback_icon_url_parser.h"
#include "base/memory/scoped_ptr.h"
#include "components/favicon_base/fallback_icon_style.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/favicon_size.h"
#include "url/gurl.h"
using chrome::ParsedFallbackIconPath;
using favicon_base::FallbackIconStyle;
namespace chrome {
namespace {
// Default values for FallbackIconStyle, from
// /components/favicon_base/fallback_icon_style.h
const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78);
const SkColor kDefaultTextColorDark = SK_ColorBLACK;
const SkColor kDefaultTextColorLight = SK_ColorWHITE;
const double kDefaultFontSizeRatio = 0.44;
const double kDefaultRoundness = 0;
const char kTestUrlStr[] = "https://www.google.ca/imghp?hl=en&tab=wi";
} // namespace
class FallbackIconUrlParserTest : public testing::Test {
public:
FallbackIconUrlParserTest() {
}
bool ParseSpecs(const std::string& specs_str,
int *size,
favicon_base::FallbackIconStyle* style) {
return ParsedFallbackIconPath::ParseSpecs(specs_str, size, style);
}
bool ParseColor(const std::string& color_str, SkColor* color) {
return ParsedFallbackIconPath::ParseColor(color_str, color);
}
private:
DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest);
};
TEST_F(FallbackIconUrlParserTest, ParseColorSuccess) {
SkColor c;
EXPECT_TRUE(ParseColor("31aBf0f4", &c));
EXPECT_EQ(SkColorSetARGB(0x31, 0xAB, 0xF0, 0xF4), c);
EXPECT_TRUE(ParseColor("01aBf0", &c));
EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c);
EXPECT_TRUE(ParseColor("501a", &c));
EXPECT_EQ(SkColorSetARGB(0x55, 0x00, 0x11, 0xAA), c);
EXPECT_TRUE(ParseColor("01a", &c));
EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c);
EXPECT_TRUE(ParseColor("000000", &c));
EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c);
EXPECT_TRUE(ParseColor("red", &c));
EXPECT_EQ(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00), c);
}
TEST_F(FallbackIconUrlParserTest, ParseColorFailure) {
const char* test_cases[] = {
"",
"00000",
"000000000",
" 000000",
"ABCDEFG",
"#000",
"#000000",
"000000 ",
"ABCDEFH",
"#ABCDEF",
};
for (size_t i = 0; i < arraysize(test_cases); ++i) {
SkColor c;
EXPECT_FALSE(ParseColor(test_cases[i], &c))
<< "test_cases[" << i << "]";
}
}
TEST_F(FallbackIconUrlParserTest, ParseSpecsEmpty) {
int size;
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs(",,,,", &size, &style));
EXPECT_EQ(16, size);
EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
EXPECT_EQ(kDefaultTextColorLight, style.text_color);
EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
EXPECT_EQ(kDefaultRoundness, style.roundness);
}
TEST_F(FallbackIconUrlParserTest, ParseSpecsPartial) {
int size;
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs(",,aCE,,0.1", &size, &style));
EXPECT_EQ(16, size);
EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
EXPECT_EQ(SkColorSetRGB(0xAA, 0xCC, 0xEE), style.text_color);
EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
EXPECT_EQ(0.1, style.roundness);
}
TEST_F(FallbackIconUrlParserTest, ParseSpecsFull) {
int size;
{
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs("16,000,f01,0.75,0.25", &size, &style));
EXPECT_EQ(16, size);
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xff, 0x00, 0x11), style.text_color);
EXPECT_EQ(0.75, style.font_size_ratio);
EXPECT_EQ(0.25, style.roundness);
}
{
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs("48,black,123456,0.5,0.3", &size, &style));
EXPECT_EQ(48, size);
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style.text_color);
EXPECT_EQ(0.5, style.font_size_ratio);
EXPECT_EQ(0.3, style.roundness);
}
{
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs("1,000,red,0,0", &size, &style));
EXPECT_EQ(1, size);
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xFF, 0x00, 0x00), style.text_color);
EXPECT_EQ(0, style.font_size_ratio);
EXPECT_EQ(0, style.roundness);
}
}
TEST_F(FallbackIconUrlParserTest, ParseSpecsDefaultTextColor) {
int size;
{
// Dark background -> Light text.
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs(",000,,,", &size, &style));
EXPECT_EQ(kDefaultTextColorLight, style.text_color);
}
{
// Light background -> Dark text.
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs(",fff,,,", &size, &style));
EXPECT_EQ(kDefaultTextColorDark, style.text_color);
}
{
// Light background -> Dark text, more params don't matter.
FallbackIconStyle style;
EXPECT_TRUE(ParseSpecs("107,fff,,0.3,0.5", &size, &style));
EXPECT_EQ(kDefaultTextColorDark, style.text_color);
}
}
TEST_F(FallbackIconUrlParserTest, ParseSpecsFailure) {
const char* test_cases[] = {
// Need exactly 5 params.
"",
"16",
"16,black",
"16,black,fff",
"16,black,fff,0.75",
",,,"
",,,,,",
"16,black,fff,0.75,0.25,junk",
// Don't allow any space.
"16,black,fff, 0.75,0.25",
"16,black ,fff,0.75,0.25",
"16,black,fff,0.75,0.25 ",
// Adding junk text.
"16,black,fff,0.75,0.25junk",
"junk,black,fff,0.75,0.25",
"16,#junk,fff,0.75,0.25",
"16,black,junk,0.75,0.25",
"16,black,fff,junk,0.25",
"16,black,fff,0.75,junk",
// Out of bound.
"0,black,fff,0.75,0.25", // size.
"4294967296,black,fff,0.75,0.25", // size.
"-1,black,fff,0.75,0.25", // size.
"16,black,fff,-0.1,0.25", // font_size_ratio.
"16,black,fff,1.1,0.25", // font_size_ratio.
"16,black,fff,0.75,-0.1", // roundness.
"16,black,fff,0.75,1.1", // roundness.
};
for (size_t i = 0; i < arraysize(test_cases); ++i) {
int size;
FallbackIconStyle style;
EXPECT_FALSE(ParseSpecs(test_cases[i], &size, &style))
<< "test_cases[" << i << "]";
}
}
TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathSuccess) {
const std::string specs = "31,black,fff,0.75,0.25";
// Everything populated.
{
chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(specs + "/" + kTestUrlStr));
EXPECT_EQ(31, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color);
EXPECT_EQ(0.75, style.font_size_ratio);
EXPECT_EQ(0.25, style.roundness);
EXPECT_EQ(GURL(kTestUrlStr), GURL(parsed.url_string()));
EXPECT_EQ(specs.length() + 1, parsed.path_index());
}
// Empty URL.
{
chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(specs + "/"));
EXPECT_EQ(31, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color);
EXPECT_EQ(0.75, style.font_size_ratio);
EXPECT_EQ(0.25, style.roundness);
EXPECT_EQ(GURL(), GURL(parsed.url_string()));
EXPECT_EQ(specs.length() + 1, parsed.path_index());
}
// Tolerate invalid URL.
{
chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(specs + "/NOT A VALID URL"));
EXPECT_EQ(31, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color);
EXPECT_EQ(0.75, style.font_size_ratio);
EXPECT_EQ(0.25, style.roundness);
EXPECT_EQ("NOT A VALID URL", parsed.url_string());
EXPECT_EQ(specs.length() + 1, parsed.path_index());
}
// Size and style are default.
{
std::string specs2 = ",,,,";
chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(specs2 + "/" + kTestUrlStr));
EXPECT_EQ(gfx::kFaviconSize, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
EXPECT_EQ(kDefaultTextColorLight, style.text_color);
EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
EXPECT_EQ(kDefaultRoundness, style.roundness);
EXPECT_EQ(GURL(kTestUrlStr), GURL(parsed.url_string()));
EXPECT_EQ(specs2.length() + 1, parsed.path_index());
}
}
TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathFailure) {
const char* test_cases[] = {
// Bad size.
"-1,000,fff,0.75,0.25/http://www.google.com/",
// Bad specs.
"32,#junk,fff,0.75,0.25/http://www.google.com/",
};
for (size_t i = 0; i < arraysize(test_cases); ++i) {
chrome::ParsedFallbackIconPath parsed;
EXPECT_FALSE(parsed.Parse(test_cases[i])) << "test_cases[" << i << "]";
}
}
} // namespace chrome
|