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

Side by Side Diff: chrome/common/favicon/fallback_icon_url_parser_unittest.cc

Issue 924063003: [Favicon] Adding FallbackIconUrlParser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unsigned int compare issue in test. Created 5 years, 10 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 "chrome/common/favicon/fallback_icon_url_parser.h"
6
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
13 using favicon_base::FallbackIconStyle;
14
15 namespace {
16
17 // Default values for FallbackIconStyle, from
18 // /components/favicon_base/favicon_types.h
19 SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80);
20 SkColor kDefaultTextColorDark = SK_ColorBLACK;
21 SkColor kDefaultTextColorLight = SK_ColorWHITE;
22 double kDefaultFontSizeRatio = 0.8;
23 double kDefaultRoundness = 0.125; // 1 / 8.
24
25 const char kTestUrl[] = "https://www.google.ca/imghp?hl=en&tab=wi";
26
27 } // namespace
28
29 namespace chrome {
30
31 TEST(FallbackIconUrlParserTest, ParseIconColorSuccess) {
32 SkColor c;
33 EXPECT_TRUE(ParseIconColor("01aBf0f4", &c));
34 EXPECT_EQ(SkColorSetARGB(0xF4, 0x01, 0xAB, 0xF0), c);
35 EXPECT_TRUE(ParseIconColor("01aBf0", &c));
36 EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c);
37 EXPECT_TRUE(ParseIconColor("01a", &c));
38 EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c);
39 EXPECT_TRUE(ParseIconColor("000000", &c));
40 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c);
41 }
42
43 TEST(FallbackIconUrlParserTest, ParseIconColorFailure) {
44 const char* test_cases[] = {
45 "",
46 "00000",
47 "000000000",
48 "0 1 2 3 ",
49 " 0 1 2 3",
50 "0ABCDEFG",
51 "#000000",
52 "000000 ",
53 "red"
54 };
55 for (size_t i = 0; i < arraysize(test_cases); ++i) {
56 SkColor c;
57 EXPECT_FALSE(ParseIconColor(test_cases[i], &c))
58 << "test_cases[" << i << "]";
59 }
60 }
61
62 TEST(FallbackIconUrlParserTest, ParseFallbackIconSpecsEmpty) {
63 int size;
64 FallbackIconStyle style;
65 EXPECT_TRUE(ParseFallbackIconSpecs(",,,,", &size, &style));
66 EXPECT_EQ(16, size);
67 EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
68 EXPECT_EQ(kDefaultTextColorLight, style.text_color);
69 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
70 EXPECT_EQ(kDefaultRoundness, style.roundness);
71 }
72
73 TEST(FallbackIconUrlParserTest, ParseFallbackIconSpecsPartial) {
74 int size;
75 FallbackIconStyle style;
76 EXPECT_TRUE(ParseFallbackIconSpecs(",,aCE,,0.1", &size, &style));
77 EXPECT_EQ(16, size);
78 EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
79 EXPECT_EQ(SkColorSetRGB(0xAA, 0xCC, 0xEE), style.text_color);
80 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
81 EXPECT_EQ(0.1, style.roundness);
82 }
83
84 TEST(FallbackIconUrlParserTest, ParseFallbackIconSpecsFull) {
85 int size1;
86 FallbackIconStyle style1;
87 EXPECT_TRUE(ParseFallbackIconSpecs("16,000,fff,0.75,0.25", &size1, &style1));
88 EXPECT_EQ(16, size1);
89 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style1.background_color);
90 EXPECT_EQ(SkColorSetRGB(0xff, 0xff, 0xff), style1.text_color);
91 EXPECT_EQ(0.75, style1.font_size_ratio);
92 EXPECT_EQ(0.25, style1.roundness);
93
94 int size2;
95 FallbackIconStyle style2;
96 EXPECT_TRUE(ParseFallbackIconSpecs("48,f05,123456,0.5,0.3", &size2, &style2));
97 EXPECT_EQ(48, size2);
98 EXPECT_EQ(SkColorSetRGB(0xff, 0x00, 0x55), style2.background_color);
99 EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style2.text_color);
100 EXPECT_EQ(0.5, style2.font_size_ratio);
101 EXPECT_EQ(0.3, style2.roundness);
102
103 int size3;
104 FallbackIconStyle style3;
105 EXPECT_TRUE(ParseFallbackIconSpecs("1,000,000,0,0", &size3, &style3));
106 EXPECT_EQ(1, size3);
107 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style3.background_color);
108 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style3.text_color);
109 EXPECT_EQ(0, style3.font_size_ratio);
110 EXPECT_EQ(0, style3.roundness);
111 }
112
113 TEST(FallbackIconUrlParserTest, ParseFallbackIconSpecsDefaultTextColor) {
114 int size;
115
116 // Dark background -> Light text.
117 FallbackIconStyle style1;
118 EXPECT_TRUE(ParseFallbackIconSpecs(",000,,,", &size, &style1));
119 EXPECT_EQ(kDefaultTextColorLight, style1.text_color);
120
121 // Light background -> Dark text.
122 FallbackIconStyle style2;
123 EXPECT_TRUE(ParseFallbackIconSpecs(",fff,,,", &size, &style2));
124 EXPECT_EQ(kDefaultTextColorDark, style2.text_color);
125
126 // Light background -> Dark text, more params don't matter.
127 FallbackIconStyle style3;
128 EXPECT_TRUE(ParseFallbackIconSpecs("107,fff,,0.3,0.5", &size, &style3));
129 EXPECT_EQ(kDefaultTextColorDark, style2.text_color);
130 }
131
132 TEST(FallbackIconUrlParserTest, ParseFallbackIconSpecsFailure) {
133 const char* test_cases[] = {
134 // Need exactly 5 params.
135 "",
136 "16",
137 "16,000",
138 "16,000,fff",
139 "16,000,fff,0.75",
140 ",,,"
141 ",,,,,",
142 "16,000,fff,0.75,0.25,junk",
143 // Don't allow any space.
144 "16,000,fff, 0.75,0.25",
145 "16,000 ,fff,0.75,0.25",
146 "16,000,fff,0.75,0.25 ",
147 // Adding junk text.
148 "16,000,fff,0.75,0.25junk",
149 "junk,000,fff,0.75,0.25",
150 "16,junk,fff,0.75,0.25",
151 "16,000,junk,0.75,0.25",
152 "16,000,fff,junk,0.25",
153 "16,000,fff,0.75,junk",
154 // Out of bound.
155 "0,000,fff,0.75,0.25", // size.
156 "4294967296,000,fff,0.75,0.25", // size.
157 "-1,000,fff,0.75,0.25", // size.
158 "16,000,fff,-0.1,0.25", // font_size_ratio.
159 "16,000,fff,1.1,0.25", // font_size_ratio.
160 "16,000,fff,0.75,-0.1", // roundness.
161 "16,000,fff,0.75,1.1", // roundness.
162 };
163 for (size_t i = 0; i < arraysize(test_cases); ++i) {
164 int size;
165 FallbackIconStyle style;
166 EXPECT_FALSE(ParseFallbackIconSpecs(test_cases[i], &size, &style))
167 << "test_cases[" << i << "]";
168
169 }
170 }
171
172 TEST(FallbackIconUrlParserTest, ParseFallbackIconPath) {
173 const std::string specs = "31,000,fff,0.75,0.25";
174 const std::string url = kTestUrl;
175 chrome::ParsedFallbackIconPath parsed;
176
177 // Everything populated.
178 EXPECT_TRUE(chrome::ParseFallbackIconPath(specs + "/" + url, &parsed));
179 EXPECT_EQ(kTestUrl, parsed.url);
180 EXPECT_EQ(31, parsed.size_in_pixels);
181 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), parsed.style.background_color);
182 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), parsed.style.text_color);
183 EXPECT_EQ(0.75, parsed.style.font_size_ratio);
184 EXPECT_EQ(0.25, parsed.style.roundness);
185
186 // Empty URL.
187 EXPECT_TRUE(chrome::ParseFallbackIconPath(specs + "/", &parsed));
188 EXPECT_EQ("", parsed.url);
189 EXPECT_EQ(31, parsed.size_in_pixels);
190 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), parsed.style.background_color);
191 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), parsed.style.text_color);
192 EXPECT_EQ(0.75, parsed.style.font_size_ratio);
193 EXPECT_EQ(0.25, parsed.style.roundness);
194
195 // Size and style are default.
196 EXPECT_TRUE(chrome::ParseFallbackIconPath(",,,,/" + url, &parsed));
197 EXPECT_EQ(kTestUrl, parsed.url);
198 EXPECT_EQ(gfx::kFaviconSize, parsed.size_in_pixels);
199 EXPECT_EQ(kDefaultBackgroundColor, parsed.style.background_color);
200 EXPECT_EQ(kDefaultTextColorLight, parsed.style.text_color);
201 EXPECT_EQ(kDefaultFontSizeRatio, parsed.style.font_size_ratio);
202 EXPECT_EQ(kDefaultRoundness, parsed.style.roundness);
203 }
204
205 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698