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

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: Adding new host chrome://fallback-icon. 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/fallback_icon_style_builder.h"
9 #include "components/favicon_base/favicon_types.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/base/layout.h"
12
13 using favicon_base::FallbackIconStyle;
14 using favicon_base::FallbackIconStyleBuilder;
15
16 typedef favicon_base::FallbackIconStyleBuilder Builder;
17
18 class FallbackIconUrlParserTest : public testing::Test {
19 public:
20 FallbackIconUrlParserTest() {
21 // Set the supported scale factors because the supported scale factors
22 // affect the result of ParsePathAndScale().
23 std::vector<ui::ScaleFactor> supported_scale_factors;
24 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
25 supported_scale_factors.push_back(ui::SCALE_FACTOR_140P);
26 scoped_set_supported_scale_factors_.reset(
27 new ui::test::ScopedSetSupportedScaleFactors(supported_scale_factors));
28 }
29
30 ~FallbackIconUrlParserTest() override {}
31
32 private:
33 typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
34 ScopedSetSupportedScaleFactors;
35 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
36
37 DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest);
38 };
39
40 // Test parsing path with no extra parameters.
41 TEST_F(FallbackIconUrlParserTest, ParsingNoExtraParams) {
42 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
43 chrome::ParsedFallbackIconPath parsed;
44
45 const std::string path1 = url;
46 EXPECT_TRUE(chrome::ParseFallbackIconPath(path1, &parsed));
47 EXPECT_EQ(url, parsed.url);
48 EXPECT_EQ(16, parsed.size_in_dip);
49 EXPECT_EQ(1.0f, parsed.device_scale_factor);
50 EXPECT_FALSE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
51 EXPECT_FALSE(parsed.style_builder.Has(Builder::TEXT_COLOR));
52 EXPECT_FALSE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
53 EXPECT_FALSE(parsed.style_builder.Has(Builder::ROUNDNESS));
54 }
55
56 // Test parsing path with a 'size' parameter.
57 TEST_F(FallbackIconUrlParserTest, ParsingSizeParam) {
58 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
59 chrome::ParsedFallbackIconPath parsed;
60
61 // Test that we can still parse the legacy 'size' parameter format.
62 const std::string path2 = "size/32/" + url;
63 EXPECT_TRUE(chrome::ParseFallbackIconPath(path2, &parsed));
64 EXPECT_EQ(url, parsed.url);
65 EXPECT_EQ(32, parsed.size_in_dip);
66 EXPECT_EQ(1.0f, parsed.device_scale_factor);
67
68 // Test parsing current 'size' parameter format.
69 const std::string path3 = "size/32@1.4x/" + url;
70 EXPECT_TRUE(chrome::ParseFallbackIconPath(path3, &parsed));
71 EXPECT_EQ(url, parsed.url);
72 EXPECT_EQ(32, parsed.size_in_dip);
73 EXPECT_EQ(1.4f, parsed.device_scale_factor);
74
75 // Test that we pick the ui::ScaleFactor which is closest to the passed in
76 // scale factor.
77 const std::string path4 = "size/16@1.41x/" + url;
78 EXPECT_TRUE(chrome::ParseFallbackIconPath(path4, &parsed));
79 EXPECT_EQ(url, parsed.url);
80 EXPECT_EQ(16, parsed.size_in_dip);
81 EXPECT_EQ(1.41f, parsed.device_scale_factor);
82
83 // Invalid cases.
84 const std::string path5 = "size/" + url;
85 EXPECT_FALSE(chrome::ParseFallbackIconPath(path5, &parsed));
86 const std::string path6 = "size/@1x/" + url;
87 EXPECT_FALSE(chrome::ParseFallbackIconPath(path6, &parsed));
88 const std::string path7 = "size/abc@1x/" + url;
89 EXPECT_FALSE(chrome::ParseFallbackIconPath(path7, &parsed));
90
91 // Part of url looks like 'size' parameter.
92 const std::string path8 = "http://www.google.com/size/32@1.4x";
93 EXPECT_TRUE(chrome::ParseFallbackIconPath(path8, &parsed));
94 EXPECT_EQ(path8, parsed.url);
95 EXPECT_EQ(16, parsed.size_in_dip);
96 EXPECT_EQ(1.0f, parsed.device_scale_factor);
97 }
98
99 // Test parsing path with a 'style' parameter.
100 TEST_F(FallbackIconUrlParserTest, ParsingStyleParam) {
101 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
102 chrome::ParsedFallbackIconPath parsed;
103 FallbackIconStyle style;
104
105 // Empty is valid.
106 const std::string path9 = "style//" + url;
107 EXPECT_TRUE(chrome::ParseFallbackIconPath(path9, &parsed));
108 EXPECT_FALSE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
109 EXPECT_FALSE(parsed.style_builder.Has(Builder::TEXT_COLOR));
110 EXPECT_FALSE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
111 EXPECT_FALSE(parsed.style_builder.Has(Builder::ROUNDNESS));
112
113 // Everything populated.
114 const std::string path10 = "style/abc,123456,0.5,0.1/" + url;
115 EXPECT_TRUE(chrome::ParseFallbackIconPath(path10, &parsed));
116 EXPECT_TRUE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
117 EXPECT_TRUE(parsed.style_builder.Has(Builder::TEXT_COLOR));
118 EXPECT_TRUE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
119 EXPECT_TRUE(parsed.style_builder.Has(Builder::ROUNDNESS));
120 style = parsed.style_builder.Build();
121 EXPECT_EQ(SkColorSetRGB(0xAA, 0xBB, 0xCC), style.background_color);
122 EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style.text_color);
123 EXPECT_EQ(0.5, style.font_size_ratio);
124 EXPECT_EQ(0.1, style.roundness);
125
126 // Partially populated.
127 const std::string path11 = "style/123abc,,0.6/" + url;
128 EXPECT_TRUE(chrome::ParseFallbackIconPath(path11, &parsed));
129 EXPECT_TRUE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
130 EXPECT_FALSE(parsed.style_builder.Has(Builder::TEXT_COLOR));
131 EXPECT_TRUE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
132 EXPECT_FALSE(parsed.style_builder.Has(Builder::ROUNDNESS));
133 style = parsed.style_builder.Build();
134 EXPECT_EQ(SkColorSetRGB(0x12, 0x3A, 0xBC), style.background_color);
135 EXPECT_EQ(0.6, style.font_size_ratio);
136
137 // Error: font_size_ratio too large.
138 const std::string path12 = "style/fff,000,100/" + url;
139 EXPECT_FALSE(chrome::ParseFallbackIconPath(path12, &parsed));
140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698