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

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

Issue 988863002: [Fallback icons] Change "explicit flow" interface so color hex strings don't use "#". (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment fix. Created 5 years, 9 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/favicon/fallback_icon_url_parser.h" 5 #include "chrome/common/favicon/fallback_icon_url_parser.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "components/favicon_base/fallback_icon_style.h" 8 #include "components/favicon_base/fallback_icon_style.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkColor.h" 10 #include "third_party/skia/include/core/SkColor.h"
(...skipping 24 matching lines...) Expand all
35 FallbackIconUrlParserTest() { 35 FallbackIconUrlParserTest() {
36 } 36 }
37 37
38 bool ParseSpecs(const std::string& specs_str, 38 bool ParseSpecs(const std::string& specs_str,
39 int *size, 39 int *size,
40 favicon_base::FallbackIconStyle* style) { 40 favicon_base::FallbackIconStyle* style) {
41 return ParsedFallbackIconPath::ParseSpecs(specs_str, size, style); 41 return ParsedFallbackIconPath::ParseSpecs(specs_str, size, style);
42 } 42 }
43 43
44 bool ParseColor(const std::string& color_str, SkColor* color) { 44 bool ParseColor(const std::string& color_str, SkColor* color) {
45 int size_dummy; 45 return ParsedFallbackIconPath::ParseColor(color_str, color);
46 favicon_base::FallbackIconStyle style;
47 std::string spec_str = "16," + color_str + ",,,";
48 if (!ParseSpecs(spec_str, &size_dummy, &style))
49 return false;
50 *color = style.background_color;
51 return true;
52 } 46 }
53 47
54 private: 48 private:
55 DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest); 49 DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest);
56 }; 50 };
57 51
58 TEST_F(FallbackIconUrlParserTest, ParseColorSuccess) { 52 TEST_F(FallbackIconUrlParserTest, ParseColorSuccess) {
59 SkColor c; 53 SkColor c;
60 EXPECT_TRUE(ParseColor("#01aBf0f4", &c)); 54 EXPECT_TRUE(ParseColor("31aBf0f4", &c));
61 EXPECT_EQ(SkColorSetARGB(0x01, 0xAB, 0xF0, 0xF4), c); 55 EXPECT_EQ(SkColorSetARGB(0x31, 0xAB, 0xF0, 0xF4), c);
62 EXPECT_TRUE(ParseColor("#01aBf0", &c)); 56 EXPECT_TRUE(ParseColor("01aBf0", &c));
63 EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c); 57 EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c);
64 EXPECT_TRUE(ParseColor("#01a", &c)); 58 EXPECT_TRUE(ParseColor("501a", &c));
59 EXPECT_EQ(SkColorSetARGB(0x55, 0x00, 0x11, 0xAA), c);
60 EXPECT_TRUE(ParseColor("01a", &c));
65 EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c); 61 EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c);
66 EXPECT_TRUE(ParseColor("#000000", &c)); 62 EXPECT_TRUE(ParseColor("000000", &c));
67 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c); 63 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c);
68 EXPECT_TRUE(ParseColor("red", &c)); 64 EXPECT_TRUE(ParseColor("red", &c));
69 EXPECT_EQ(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00), c); 65 EXPECT_EQ(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00), c);
70 } 66 }
71 67
72 TEST_F(FallbackIconUrlParserTest, ParseColorFailure) { 68 TEST_F(FallbackIconUrlParserTest, ParseColorFailure) {
73 const char* test_cases[] = { 69 const char* test_cases[] = {
74 "#00000", 70 "",
75 "#000000000", 71 "00000",
76 " #000000", 72 "000000000",
77 "#ABCDEFG", 73 " 000000",
78 "000000", 74 "ABCDEFG",
79 "#000000 ", 75 "#000",
76 "#000000",
77 "000000 ",
78 "ABCDEFH",
James Hawkins 2015/03/06 22:24:52 Can you add a negative test just for #, e.g., #ABC
huangs 2015/03/06 22:39:48 Done.
80 }; 79 };
81 for (size_t i = 0; i < arraysize(test_cases); ++i) { 80 for (size_t i = 0; i < arraysize(test_cases); ++i) {
82 SkColor c; 81 SkColor c;
83 EXPECT_FALSE(ParseColor(test_cases[i], &c)) 82 EXPECT_FALSE(ParseColor(test_cases[i], &c))
84 << "test_cases[" << i << "]"; 83 << "test_cases[" << i << "]";
85 } 84 }
86 } 85 }
87 86
88 TEST_F(FallbackIconUrlParserTest, ParseSpecsEmpty) { 87 TEST_F(FallbackIconUrlParserTest, ParseSpecsEmpty) {
89 int size; 88 int size;
90 FallbackIconStyle style; 89 FallbackIconStyle style;
91 EXPECT_TRUE(ParseSpecs(",,,,", &size, &style)); 90 EXPECT_TRUE(ParseSpecs(",,,,", &size, &style));
92 EXPECT_EQ(16, size); 91 EXPECT_EQ(16, size);
93 EXPECT_EQ(kDefaultBackgroundColor, style.background_color); 92 EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
94 EXPECT_EQ(kDefaultTextColorLight, style.text_color); 93 EXPECT_EQ(kDefaultTextColorLight, style.text_color);
95 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio); 94 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
96 EXPECT_EQ(kDefaultRoundness, style.roundness); 95 EXPECT_EQ(kDefaultRoundness, style.roundness);
97 } 96 }
98 97
99 TEST_F(FallbackIconUrlParserTest, ParseSpecsPartial) { 98 TEST_F(FallbackIconUrlParserTest, ParseSpecsPartial) {
100 int size; 99 int size;
101 FallbackIconStyle style; 100 FallbackIconStyle style;
102 EXPECT_TRUE(ParseSpecs(",,#aCE,,0.1", &size, &style)); 101 EXPECT_TRUE(ParseSpecs(",,aCE,,0.1", &size, &style));
103 EXPECT_EQ(16, size); 102 EXPECT_EQ(16, size);
104 EXPECT_EQ(kDefaultBackgroundColor, style.background_color); 103 EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
105 EXPECT_EQ(SkColorSetRGB(0xAA, 0xCC, 0xEE), style.text_color); 104 EXPECT_EQ(SkColorSetRGB(0xAA, 0xCC, 0xEE), style.text_color);
106 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio); 105 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
107 EXPECT_EQ(0.1, style.roundness); 106 EXPECT_EQ(0.1, style.roundness);
108 } 107 }
109 108
110 TEST_F(FallbackIconUrlParserTest, ParseSpecsFull) { 109 TEST_F(FallbackIconUrlParserTest, ParseSpecsFull) {
111 int size; 110 int size;
112 111
113 { 112 {
114 FallbackIconStyle style; 113 FallbackIconStyle style;
115 EXPECT_TRUE(ParseSpecs("16,#000,#f01,0.75,0.25", &size, &style)); 114 EXPECT_TRUE(ParseSpecs("16,000,f01,0.75,0.25", &size, &style));
116 EXPECT_EQ(16, size); 115 EXPECT_EQ(16, size);
117 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color); 116 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
118 EXPECT_EQ(SkColorSetRGB(0xff, 0x00, 0x11), style.text_color); 117 EXPECT_EQ(SkColorSetRGB(0xff, 0x00, 0x11), style.text_color);
119 EXPECT_EQ(0.75, style.font_size_ratio); 118 EXPECT_EQ(0.75, style.font_size_ratio);
120 EXPECT_EQ(0.25, style.roundness); 119 EXPECT_EQ(0.25, style.roundness);
121 } 120 }
122 121
123 { 122 {
124 FallbackIconStyle style; 123 FallbackIconStyle style;
125 EXPECT_TRUE(ParseSpecs("48,black,#123456,0.5,0.3", &size, &style)); 124 EXPECT_TRUE(ParseSpecs("48,black,123456,0.5,0.3", &size, &style));
126 EXPECT_EQ(48, size); 125 EXPECT_EQ(48, size);
127 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color); 126 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
128 EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style.text_color); 127 EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style.text_color);
129 EXPECT_EQ(0.5, style.font_size_ratio); 128 EXPECT_EQ(0.5, style.font_size_ratio);
130 EXPECT_EQ(0.3, style.roundness); 129 EXPECT_EQ(0.3, style.roundness);
131 } 130 }
132 131
133 { 132 {
134 FallbackIconStyle style; 133 FallbackIconStyle style;
135 EXPECT_TRUE(ParseSpecs("1,#000,red,0,0", &size, &style)); 134 EXPECT_TRUE(ParseSpecs("1,000,red,0,0", &size, &style));
136 EXPECT_EQ(1, size); 135 EXPECT_EQ(1, size);
137 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color); 136 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
138 EXPECT_EQ(SkColorSetRGB(0xFF, 0x00, 0x00), style.text_color); 137 EXPECT_EQ(SkColorSetRGB(0xFF, 0x00, 0x00), style.text_color);
139 EXPECT_EQ(0, style.font_size_ratio); 138 EXPECT_EQ(0, style.font_size_ratio);
140 EXPECT_EQ(0, style.roundness); 139 EXPECT_EQ(0, style.roundness);
141 } 140 }
142 } 141 }
143 142
144 TEST_F(FallbackIconUrlParserTest, ParseSpecsDefaultTextColor) { 143 TEST_F(FallbackIconUrlParserTest, ParseSpecsDefaultTextColor) {
145 int size; 144 int size;
146 145
147 { 146 {
148 // Dark background -> Light text. 147 // Dark background -> Light text.
149 FallbackIconStyle style; 148 FallbackIconStyle style;
150 EXPECT_TRUE(ParseSpecs(",#000,,,", &size, &style)); 149 EXPECT_TRUE(ParseSpecs(",000,,,", &size, &style));
151 EXPECT_EQ(kDefaultTextColorLight, style.text_color); 150 EXPECT_EQ(kDefaultTextColorLight, style.text_color);
152 } 151 }
153 152
154 { 153 {
155 // Light background -> Dark text. 154 // Light background -> Dark text.
156 FallbackIconStyle style; 155 FallbackIconStyle style;
157 EXPECT_TRUE(ParseSpecs(",#fff,,,", &size, &style)); 156 EXPECT_TRUE(ParseSpecs(",fff,,,", &size, &style));
158 EXPECT_EQ(kDefaultTextColorDark, style.text_color); 157 EXPECT_EQ(kDefaultTextColorDark, style.text_color);
159 } 158 }
160 159
161 { 160 {
162 // Light background -> Dark text, more params don't matter. 161 // Light background -> Dark text, more params don't matter.
163 FallbackIconStyle style; 162 FallbackIconStyle style;
164 EXPECT_TRUE(ParseSpecs("107,#fff,,0.3,0.5", &size, &style)); 163 EXPECT_TRUE(ParseSpecs("107,fff,,0.3,0.5", &size, &style));
165 EXPECT_EQ(kDefaultTextColorDark, style.text_color); 164 EXPECT_EQ(kDefaultTextColorDark, style.text_color);
166 } 165 }
167 } 166 }
168 167
169 TEST_F(FallbackIconUrlParserTest, ParseSpecsFailure) { 168 TEST_F(FallbackIconUrlParserTest, ParseSpecsFailure) {
170 const char* test_cases[] = { 169 const char* test_cases[] = {
171 // Need exactly 5 params. 170 // Need exactly 5 params.
172 "", 171 "",
173 "16", 172 "16",
174 "16,black", 173 "16,black",
175 "16,black,#fff", 174 "16,black,fff",
176 "16,black,#fff,0.75", 175 "16,black,fff,0.75",
177 ",,," 176 ",,,"
178 ",,,,,", 177 ",,,,,",
179 "16,black,#fff,0.75,0.25,junk", 178 "16,black,fff,0.75,0.25,junk",
180 // Don't allow any space. 179 // Don't allow any space.
181 "16,black,#fff, 0.75,0.25", 180 "16,black,fff, 0.75,0.25",
182 "16,black ,#fff,0.75,0.25", 181 "16,black ,fff,0.75,0.25",
183 "16,black,#fff,0.75,0.25 ", 182 "16,black,fff,0.75,0.25 ",
184 // Adding junk text. 183 // Adding junk text.
185 "16,black,#fff,0.75,0.25junk", 184 "16,black,fff,0.75,0.25junk",
186 "junk,black,#fff,0.75,0.25", 185 "junk,black,fff,0.75,0.25",
187 "16,#junk,#fff,0.75,0.25", 186 "16,#junk,fff,0.75,0.25",
188 "16,black,#junk,0.75,0.25", 187 "16,black,junk,0.75,0.25",
189 "16,black,#fff,junk,0.25", 188 "16,black,fff,junk,0.25",
190 "16,black,#fff,0.75,junk", 189 "16,black,fff,0.75,junk",
191 // Out of bound. 190 // Out of bound.
192 "0,black,#fff,0.75,0.25", // size. 191 "0,black,fff,0.75,0.25", // size.
193 "4294967296,black,#fff,0.75,0.25", // size. 192 "4294967296,black,fff,0.75,0.25", // size.
194 "-1,black,#fff,0.75,0.25", // size. 193 "-1,black,fff,0.75,0.25", // size.
195 "16,black,#fff,-0.1,0.25", // font_size_ratio. 194 "16,black,fff,-0.1,0.25", // font_size_ratio.
196 "16,black,#fff,1.1,0.25", // font_size_ratio. 195 "16,black,fff,1.1,0.25", // font_size_ratio.
197 "16,black,#fff,0.75,-0.1", // roundness. 196 "16,black,fff,0.75,-0.1", // roundness.
198 "16,black,#fff,0.75,1.1", // roundness. 197 "16,black,fff,0.75,1.1", // roundness.
199 }; 198 };
200 for (size_t i = 0; i < arraysize(test_cases); ++i) { 199 for (size_t i = 0; i < arraysize(test_cases); ++i) {
201 int size; 200 int size;
202 FallbackIconStyle style; 201 FallbackIconStyle style;
203 EXPECT_FALSE(ParseSpecs(test_cases[i], &size, &style)) 202 EXPECT_FALSE(ParseSpecs(test_cases[i], &size, &style))
204 << "test_cases[" << i << "]"; 203 << "test_cases[" << i << "]";
205 204
206 } 205 }
207 } 206 }
208 207
209 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathSuccess) { 208 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathSuccess) {
210 const std::string specs = "31,black,#fff,0.75,0.25"; 209 const std::string specs = "31,black,fff,0.75,0.25";
211 210
212 // Everything populated. 211 // Everything populated.
213 { 212 {
214 chrome::ParsedFallbackIconPath parsed; 213 chrome::ParsedFallbackIconPath parsed;
215 EXPECT_TRUE(parsed.Parse(specs + "/" + kTestUrlStr)); 214 EXPECT_TRUE(parsed.Parse(specs + "/" + kTestUrlStr));
216 EXPECT_EQ(GURL(kTestUrlStr), parsed.url()); 215 EXPECT_EQ(GURL(kTestUrlStr), parsed.url());
217 EXPECT_EQ(31, parsed.size_in_pixels()); 216 EXPECT_EQ(31, parsed.size_in_pixels());
218 const favicon_base::FallbackIconStyle& style = parsed.style(); 217 const favicon_base::FallbackIconStyle& style = parsed.style();
219 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color); 218 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
220 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color); 219 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color);
(...skipping 24 matching lines...) Expand all
245 EXPECT_EQ(kDefaultBackgroundColor, style.background_color); 244 EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
246 EXPECT_EQ(kDefaultTextColorLight, style.text_color); 245 EXPECT_EQ(kDefaultTextColorLight, style.text_color);
247 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio); 246 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
248 EXPECT_EQ(kDefaultRoundness, style.roundness); 247 EXPECT_EQ(kDefaultRoundness, style.roundness);
249 } 248 }
250 } 249 }
251 250
252 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathFailure) { 251 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathFailure) {
253 const char* test_cases[] = { 252 const char* test_cases[] = {
254 // Bad size. 253 // Bad size.
255 "-1,#000,#fff,0.75,0.25/http://www.google.com/", 254 "-1,000,fff,0.75,0.25/http://www.google.com/",
256 // Bad specs. 255 // Bad specs.
257 "32,#junk,#fff,0.75,0.25/http://www.google.com/", 256 "32,#junk,fff,0.75,0.25/http://www.google.com/",
258 // Bad URL. 257 // Bad URL.
259 "32,#000,#fff,0.75,0.25/NOT A VALID URL", 258 "32,000,fff,0.75,0.25/NOT A VALID URL",
260 }; 259 };
261 for (size_t i = 0; i < arraysize(test_cases); ++i) { 260 for (size_t i = 0; i < arraysize(test_cases); ++i) {
262 chrome::ParsedFallbackIconPath parsed; 261 chrome::ParsedFallbackIconPath parsed;
263 EXPECT_FALSE(parsed.Parse(test_cases[i])) << "test_cases[" << i << "]"; 262 EXPECT_FALSE(parsed.Parse(test_cases[i])) << "test_cases[" << i << "]";
264 } 263 }
265 } 264 }
266 265
267 } // namespace chrome 266 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698