1 // Copyright 2013 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.
5 #include "chrome/common/favicon/favicon_url_parser.h"
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 "ui/base/layout.h"
12 class FaviconUrlParserTest
: public testing::Test
{
14 FaviconUrlParserTest() {
15 // Set the supported scale factors because the supported scale factors
16 // affect the result of ParsePathAndScale().
17 std::vector
<ui::ScaleFactor
> supported_scale_factors
;
18 supported_scale_factors
.push_back(ui::SCALE_FACTOR_100P
);
19 supported_scale_factors
.push_back(ui::SCALE_FACTOR_140P
);
20 scoped_set_supported_scale_factors_
.reset(
21 new ui::test::ScopedSetSupportedScaleFactors(supported_scale_factors
));
24 ~FaviconUrlParserTest() override
{}
27 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
28 ScopedSetSupportedScaleFactors
;
29 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
31 DISALLOW_COPY_AND_ASSIGN(FaviconUrlParserTest
);
34 // Test parsing path with no extra parameters.
35 TEST_F(FaviconUrlParserTest
, ParsingNoExtraParams
) {
36 const std::string
url("https://www.google.ca/imghp?hl=en&tab=wi");
37 int icon_types
= favicon_base::TOUCH_PRECOMPOSED_ICON
;
38 chrome::ParsedFaviconPath parsed
;
40 const std::string path1
= url
;
41 EXPECT_TRUE(chrome::ParseFaviconPath(path1
, icon_types
, &parsed
));
42 EXPECT_FALSE(parsed
.is_icon_url
);
43 EXPECT_EQ(url
, parsed
.url
);
44 EXPECT_EQ(16, parsed
.size_in_dip
);
45 EXPECT_EQ(1.0f
, parsed
.device_scale_factor
);
48 // Test parsing path with a 'size' parameter.
49 TEST_F(FaviconUrlParserTest
, ParsingSizeParam
) {
50 const std::string
url("https://www.google.ca/imghp?hl=en&tab=wi");
51 int icon_types
= favicon_base::TOUCH_PRECOMPOSED_ICON
;
52 chrome::ParsedFaviconPath parsed
;
54 // Test that we can still parse the legacy 'size' parameter format.
55 const std::string path2
= "size/32/" + url
;
56 EXPECT_TRUE(chrome::ParseFaviconPath(path2
, icon_types
, &parsed
));
57 EXPECT_FALSE(parsed
.is_icon_url
);
58 EXPECT_EQ(url
, parsed
.url
);
59 EXPECT_EQ(32, parsed
.size_in_dip
);
60 EXPECT_EQ(1.0f
, parsed
.device_scale_factor
);
62 // Test parsing current 'size' parameter format.
63 const std::string path3
= "size/32@1.4x/" + url
;
64 EXPECT_TRUE(chrome::ParseFaviconPath(path3
, icon_types
, &parsed
));
65 EXPECT_FALSE(parsed
.is_icon_url
);
66 EXPECT_EQ(url
, parsed
.url
);
67 EXPECT_EQ(32, parsed
.size_in_dip
);
68 EXPECT_EQ(1.4f
, parsed
.device_scale_factor
);
70 // Test that we pick the ui::ScaleFactor which is closest to the passed in
72 const std::string path4
= "size/16@1.41x/" + url
;
73 EXPECT_TRUE(chrome::ParseFaviconPath(path4
, icon_types
, &parsed
));
74 EXPECT_FALSE(parsed
.is_icon_url
);
75 EXPECT_EQ(url
, parsed
.url
);
76 EXPECT_EQ(16, parsed
.size_in_dip
);
77 EXPECT_EQ(1.41f
, parsed
.device_scale_factor
);
80 const std::string path5
= "size/" + url
;
81 EXPECT_FALSE(chrome::ParseFaviconPath(path5
, icon_types
, &parsed
));
82 const std::string path6
= "size/@1x/" + url
;
83 EXPECT_FALSE(chrome::ParseFaviconPath(path6
, icon_types
, &parsed
));
84 const std::string path7
= "size/abc@1x/" + url
;
85 EXPECT_FALSE(chrome::ParseFaviconPath(path7
, icon_types
, &parsed
));
87 // Part of url looks like 'size' parameter.
88 const std::string path8
= "http://www.google.com/size/32@1.4x";
89 EXPECT_TRUE(chrome::ParseFaviconPath(path8
, icon_types
, &parsed
));
90 EXPECT_FALSE(parsed
.is_icon_url
);
91 EXPECT_EQ(path8
, parsed
.url
);
92 EXPECT_EQ(16, parsed
.size_in_dip
);
93 EXPECT_EQ(1.0f
, parsed
.device_scale_factor
);
96 // Test parsing path with the 'largest' parameter.
97 TEST_F(FaviconUrlParserTest
, ParsingLargestParam
) {
98 const std::string
url("https://www.google.ca/imghp?hl=en&tab=wi");
99 int icon_types
= favicon_base::TOUCH_PRECOMPOSED_ICON
;
100 chrome::ParsedFaviconPath parsed
;
102 const std::string path9
= "largest/" + url
;
103 EXPECT_TRUE(chrome::ParseFaviconPath(path9
, icon_types
, &parsed
));
104 EXPECT_FALSE(parsed
.is_icon_url
);
105 EXPECT_EQ(url
, parsed
.url
);
106 EXPECT_EQ(0, parsed
.size_in_dip
);
107 // The scale factor is meaningless when requesting the largest favicon.
110 // Test parsing path with 'iconurl' parameter.
111 TEST_F(FaviconUrlParserTest
, ParsingIconUrlParam
) {
112 const std::string
url("https://www.google.ca/imghp?hl=en&tab=wi");
113 int icon_types
= favicon_base::TOUCH_PRECOMPOSED_ICON
;
114 chrome::ParsedFaviconPath parsed
;
116 const std::string path10
= "iconurl/http://www.google.com/favicon.ico";
117 EXPECT_TRUE(chrome::ParseFaviconPath(path10
, icon_types
, &parsed
));
118 EXPECT_TRUE(parsed
.is_icon_url
);
119 EXPECT_EQ("http://www.google.com/favicon.ico", parsed
.url
);
120 EXPECT_EQ(16, parsed
.size_in_dip
);
121 EXPECT_EQ(1.0f
, parsed
.device_scale_factor
);
124 // Test parsing path with 'origin' parameter.
125 TEST_F(FaviconUrlParserTest
, ParsingOriginParam
) {
126 const std::string
url("https://www.google.ca/imghp?hl=en&tab=wi");
127 int icon_types
= favicon_base::TOUCH_PRECOMPOSED_ICON
;
128 chrome::ParsedFaviconPath parsed
;
130 const std::string path11
= "origin/" + url
;
131 EXPECT_TRUE(chrome::ParseFaviconPath(path11
, icon_types
, &parsed
));
132 EXPECT_FALSE(parsed
.is_icon_url
);
133 EXPECT_EQ("https://www.google.ca/", parsed
.url
);
134 EXPECT_EQ(16, parsed
.size_in_dip
);
135 EXPECT_EQ(1.0f
, parsed
.device_scale_factor
);
137 const std::string path12
= "origin/google.com";
138 EXPECT_TRUE(chrome::ParseFaviconPath(path12
, icon_types
, &parsed
));
139 EXPECT_FALSE(parsed
.is_icon_url
);
140 EXPECT_EQ("http://google.com/", parsed
.url
);
141 EXPECT_EQ(16, parsed
.size_in_dip
);
142 EXPECT_EQ(1.0f
, parsed
.device_scale_factor
);
145 // Test parsing paths with both a 'size' parameter and a 'url modifier'
147 TEST_F(FaviconUrlParserTest
, ParsingSizeParamAndUrlModifier
) {
148 const std::string
url("https://www.google.ca/imghp?hl=en&tab=wi");
149 int icon_types
= favicon_base::TOUCH_PRECOMPOSED_ICON
;
150 chrome::ParsedFaviconPath parsed
;
152 const std::string path13
= "size/32@1.4x/origin/" + url
;
153 EXPECT_TRUE(chrome::ParseFaviconPath(path13
, icon_types
, &parsed
));
154 EXPECT_FALSE(parsed
.is_icon_url
);
155 EXPECT_EQ("https://www.google.ca/", parsed
.url
);
156 EXPECT_EQ(32, parsed
.size_in_dip
);
157 EXPECT_EQ(1.4f
, parsed
.device_scale_factor
);
159 const std::string path14
=
160 "largest/iconurl/http://www.google.com/favicon.ico";
161 EXPECT_TRUE(chrome::ParseFaviconPath(path14
, icon_types
, &parsed
));
162 EXPECT_TRUE(parsed
.is_icon_url
);
163 EXPECT_EQ("http://www.google.com/favicon.ico", parsed
.url
);
164 EXPECT_EQ(0, parsed
.size_in_dip
);