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

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

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removing FallbackIconStyleBuilder; trimming down chrome://fallback-icon syntax. Created 5 years, 11 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/favicon_types.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkColor.h"
11 #include "ui/base/layout.h"
12 #include "ui/gfx/favicon_size.h"
13
14 using favicon_base::FallbackIconStyle;
15
16 namespace {
17
18 // Default values for FallbackIconStyle, from
19 // /components/favicon_base/favicon_types.h
20 SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80);
21 SkColor kDefaultTextColorDark = SK_ColorBLACK;
22 SkColor kDefaultTextColorLight = SK_ColorWHITE;
23 double kDefaultFontSizeRatio = 0.8;
24 double kDefaultRoundness = 0.125; // 1 / 8.
25
26 const char kTestUrl[] = "https://www.google.ca/imghp?hl=en&tab=wi";
27
28 } // namespace
29
30 namespace chrome {
31
32 class FallbackIconUrlParserTest : public testing::Test {
33 public:
34 FallbackIconUrlParserTest() {
pkotwicz 2015/01/23 16:00:47 Setting the supported scale factors should no long
huangs 2015/01/23 19:47:51 Removed, also got rid of #include "ui/base/layout.
35 // Set the supported scale factors because the supported scale factors
36 // affect the result of ParsePathAndScale().
37 std::vector<ui::ScaleFactor> supported_scale_factors;
38 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
39 supported_scale_factors.push_back(ui::SCALE_FACTOR_140P);
40 scoped_set_supported_scale_factors_.reset(
41 new ui::test::ScopedSetSupportedScaleFactors(supported_scale_factors));
42 }
43
44 ~FallbackIconUrlParserTest() override {}
45
46 private:
47 typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
48 ScopedSetSupportedScaleFactors;
49 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
50
51 DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest);
52 };
53
54 TEST_F(FallbackIconUrlParserTest, ParseIconColorSuccess) {
55 SkColor c;
56 EXPECT_TRUE(ParseIconColor("01aBf0f4", &c));
57 EXPECT_EQ(SkColorSetARGB(0xF4, 0x01, 0xAB, 0xF0), c);
58 EXPECT_TRUE(ParseIconColor("01aBf0", &c));
59 EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c);
60 EXPECT_TRUE(ParseIconColor("01a", &c));
61 EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c);
62 EXPECT_TRUE(ParseIconColor("000000", &c));
63 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c);
64 }
65
66 TEST_F(FallbackIconUrlParserTest, ParseIconColorFailure) {
67 const char* test_cases[] = {
68 "",
69 "00000",
70 "000000000",
71 "0 1 2 3 ",
72 " 0 1 2 3",
73 "0ABCDEFG",
74 "#000000",
75 "000000 ",
76 "red"
77 };
78 for (int i = 0; i < arraysize(test_cases); ++i) {
79 SkColor c;
80 EXPECT_FALSE(ParseIconColor(test_cases[i], &c))
81 << "test_cases[" << i << "]";
82 }
83 }
84
85 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsEmpty) {
86 int size;
87 FallbackIconStyle style;
88 EXPECT_TRUE(ParseFallbackIconSpecs(",,,,", &size, &style));
89 EXPECT_EQ(16, size);
90 EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
91 EXPECT_EQ(kDefaultTextColorLight, style.text_color);
92 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
93 EXPECT_EQ(kDefaultRoundness, style.roundness);
94 }
95
96 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsPartial) {
97 int size;
98 FallbackIconStyle style;
99 EXPECT_TRUE(ParseFallbackIconSpecs(",,aCE,,0.1", &size, &style));
100 EXPECT_EQ(16, size);
101 EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
102 EXPECT_EQ(SkColorSetRGB(0xAA, 0xCC, 0xEE), style.text_color);
103 EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
104 EXPECT_EQ(0.1, style.roundness);
105 }
106
107 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsFull) {
108 int size1;
109 FallbackIconStyle style1;
110 EXPECT_TRUE(ParseFallbackIconSpecs("16,000,fff,0.75,0.25", &size1, &style1));
111 EXPECT_EQ(16, size1);
112 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style1.background_color);
113 EXPECT_EQ(SkColorSetRGB(0xff, 0xff, 0xff), style1.text_color);
114 EXPECT_EQ(0.75, style1.font_size_ratio);
115 EXPECT_EQ(0.25, style1.roundness);
116
117 int size2;
118 FallbackIconStyle style2;
119 EXPECT_TRUE(ParseFallbackIconSpecs("48,f05,123456,0.5,0.3", &size2, &style2));
120 EXPECT_EQ(48, size2);
121 EXPECT_EQ(SkColorSetRGB(0xff, 0x00, 0x55), style2.background_color);
122 EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style2.text_color);
123 EXPECT_EQ(0.5, style2.font_size_ratio);
124 EXPECT_EQ(0.3, style2.roundness);
125
126 int size3;
127 FallbackIconStyle style3;
128 EXPECT_TRUE(ParseFallbackIconSpecs("1,000,000,0,0", &size3, &style3));
129 EXPECT_EQ(1, size3);
130 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style3.background_color);
131 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style3.text_color);
132 EXPECT_EQ(0, style3.font_size_ratio);
133 EXPECT_EQ(0, style3.roundness);
134 }
135
136 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsDefaultTextColor) {
137 int size;
138
139 // Dark background -> Light text.
140 FallbackIconStyle style1;
141 EXPECT_TRUE(ParseFallbackIconSpecs(",000,,,", &size, &style1));
142 EXPECT_EQ(kDefaultTextColorLight, style1.text_color);
143
144 // Light background -> Dark text.
145 FallbackIconStyle style2;
146 EXPECT_TRUE(ParseFallbackIconSpecs(",fff,,,", &size, &style2));
147 EXPECT_EQ(kDefaultTextColorDark, style2.text_color);
148
149 // Light background -> Dark text, more params don't matter.
150 FallbackIconStyle style3;
151 EXPECT_TRUE(ParseFallbackIconSpecs("107,fff,,0.3,0.5", &size, &style3));
152 EXPECT_EQ(kDefaultTextColorDark, style2.text_color);
153 }
154
155 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsFailure) {
156 const char* test_cases[] = {
157 // Need exactly 5 params.
158 "",
159 "16",
160 "16,000",
161 "16,000,fff",
162 "16,000,fff,0.75",
163 ",,,"
164 ",,,,,",
165 // Don't allow any space.
166 "16,000,fff, 0.75,0.25",
167 "16,000 ,fff,0.75,0.25",
168 "16,000,fff,0.75,0.25 ",
169 // Adding junk text.
170 "16,000,fff,0.75,0.25junk",
171 "16,000,fff,0.75,0.25,junk",
pkotwicz 2015/01/23 16:00:48 The above test case belongs in the "exactly 5 para
huangs 2015/01/23 19:47:51 Done.
172 "junk,000,fff,0.75,0.25",
173 "16,junk,fff,0.75,0.25",
174 "16,000,junk,0.75,0.25",
175 "16,000,fff,junk,0.25",
176 "16,000,fff,0.75,junk",
177 // Out of bound.
178 "0,000,fff,0.75,0.25", // size.
179 "4294967296,000,fff,0.75,0.25", // size.
180 "-1,000,fff,0.75,0.25", // size.
181 "16,000,fff,-0.1,0.25", // font_size_ratio.
182 "16,000,fff,1.1,0.25", // font_size_ratio.
183 "16,000,fff,0.75,-0.1", // roundness.
184 "16,000,fff,0.75,1.1", // roundness.
185 };
186 for (int i = 0; i < arraysize(test_cases); ++i) {
187 int size;
188 FallbackIconStyle style;
189 EXPECT_FALSE(ParseFallbackIconSpecs(test_cases[i], &size, &style))
190 << "test_cases[" << i << "]";
191
192 }
193 }
194
195 TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPath) {
196 const std::string specs = "31,000,fff,0.75,0.25";
197 const std::string url = kTestUrl;
198 chrome::ParsedFallbackIconPath parsed;
199
200 // Everything populated.
201 EXPECT_TRUE(chrome::ParseFallbackIconPath(specs + "/" + url, &parsed));
202 EXPECT_EQ(kTestUrl, parsed.url);
203 EXPECT_EQ(31, parsed.size_in_pixels);
204 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), parsed.style.background_color);
205 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), parsed.style.text_color);
206 EXPECT_EQ(0.75, parsed.style.font_size_ratio);
207 EXPECT_EQ(0.25, parsed.style.roundness);
208
pkotwicz 2015/01/23 16:00:48 Nit: Remove extra blank line
huangs 2015/01/23 19:47:51 Done.
209
210 // Empty URL..
pkotwicz 2015/01/23 16:00:48 Nit: Remove extra period
huangs 2015/01/23 19:47:51 Done.
211 EXPECT_TRUE(chrome::ParseFallbackIconPath(specs + "/", &parsed));
212 EXPECT_EQ(kTestUrl, "");
213 EXPECT_EQ(31, parsed.size_in_pixels);
214 EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), parsed.style.background_color);
215 EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), parsed.style.text_color);
216 EXPECT_EQ(0.75, parsed.style.font_size_ratio);
217 EXPECT_EQ(0.25, parsed.style.roundness);
218
219 // Size and style are default.
220 EXPECT_TRUE(chrome::ParseFallbackIconPath(",,,,/" + url, &parsed));
221 EXPECT_EQ(kTestUrl, parsed.url);
222 EXPECT_EQ(gfx::kFaviconSize, parsed.size_in_pixels);
223 EXPECT_EQ(kDefaultBackgroundColor, parsed.style.background_color);
224 EXPECT_EQ(kDefaultTextColorLight, parsed.style.text_color);
225 EXPECT_EQ(kDefaultFontSizeRatio, parsed.style.font_size_ratio);
226 EXPECT_EQ(kDefaultRoundness, parsed.style.roundness);
227 }
228
229 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698