blob: 48eae908486bed2d7e6312e9d87f4d5781c8ca70 [file] [log] [blame]
[email protected]78dfe382013-07-17 18:05:421// 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.
4
blundellee0ade22015-07-08 10:07:455#include "components/favicon_base/favicon_url_parser.h"
[email protected]78dfe382013-07-17 18:05:426
dcheng84c358e2016-04-26 07:05:537#include <memory>
8
avibc5337b2015-12-25 23:16:339#include "base/macros.h"
[email protected]7627e0b42014-04-17 17:20:5310#include "components/favicon_base/favicon_types.h"
[email protected]78dfe382013-07-17 18:05:4211#include "testing/gtest/include/gtest/gtest.h"
12#include "ui/base/layout.h"
13
14class FaviconUrlParserTest : public testing::Test {
15 public:
16 FaviconUrlParserTest() {
17 // Set the supported scale factors because the supported scale factors
18 // affect the result of ParsePathAndScale().
19 std::vector<ui::ScaleFactor> supported_scale_factors;
20 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
21 supported_scale_factors.push_back(ui::SCALE_FACTOR_140P);
22 scoped_set_supported_scale_factors_.reset(
23 new ui::test::ScopedSetSupportedScaleFactors(supported_scale_factors));
24 }
25
dchengd0022752014-10-28 00:09:0626 ~FaviconUrlParserTest() override {}
[email protected]78dfe382013-07-17 18:05:4227
28 private:
dcheng84c358e2016-04-26 07:05:5329 typedef std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors>
[email protected]78dfe382013-07-17 18:05:4230 ScopedSetSupportedScaleFactors;
31 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
32
33 DISALLOW_COPY_AND_ASSIGN(FaviconUrlParserTest);
34};
35
36// Test parsing path with no extra parameters.
37TEST_F(FaviconUrlParserTest, ParsingNoExtraParams) {
38 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
[email protected]78dfe382013-07-17 18:05:4239 chrome::ParsedFaviconPath parsed;
40
41 const std::string path1 = url;
pkotwiczd95aa6592017-05-11 02:26:2142 EXPECT_TRUE(chrome::ParseFaviconPath(path1, &parsed));
[email protected]78dfe382013-07-17 18:05:4243 EXPECT_FALSE(parsed.is_icon_url);
44 EXPECT_EQ(url, parsed.url);
45 EXPECT_EQ(16, parsed.size_in_dip);
[email protected]32730eb2014-06-26 23:49:2146 EXPECT_EQ(1.0f, parsed.device_scale_factor);
[email protected]78dfe382013-07-17 18:05:4247}
48
49// Test parsing path with a 'size' parameter.
50TEST_F(FaviconUrlParserTest, ParsingSizeParam) {
51 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
[email protected]78dfe382013-07-17 18:05:4252 chrome::ParsedFaviconPath parsed;
53
54 // Test that we can still parse the legacy 'size' parameter format.
55 const std::string path2 = "size/32/" + url;
pkotwiczd95aa6592017-05-11 02:26:2156 EXPECT_TRUE(chrome::ParseFaviconPath(path2, &parsed));
[email protected]78dfe382013-07-17 18:05:4257 EXPECT_FALSE(parsed.is_icon_url);
58 EXPECT_EQ(url, parsed.url);
59 EXPECT_EQ(32, parsed.size_in_dip);
[email protected]abe29e2a2014-06-20 23:20:5360 EXPECT_EQ(1.0f, parsed.device_scale_factor);
[email protected]78dfe382013-07-17 18:05:4261
62 // Test parsing current 'size' parameter format.
63 const std::string path3 = "size/[email protected]/" + url;
pkotwiczd95aa6592017-05-11 02:26:2164 EXPECT_TRUE(chrome::ParseFaviconPath(path3, &parsed));
[email protected]78dfe382013-07-17 18:05:4265 EXPECT_FALSE(parsed.is_icon_url);
66 EXPECT_EQ(url, parsed.url);
67 EXPECT_EQ(32, parsed.size_in_dip);
[email protected]abe29e2a2014-06-20 23:20:5368 EXPECT_EQ(1.4f, parsed.device_scale_factor);
[email protected]78dfe382013-07-17 18:05:4269
70 // Test that we pick the ui::ScaleFactor which is closest to the passed in
71 // scale factor.
72 const std::string path4 = "size/[email protected]/" + url;
pkotwiczd95aa6592017-05-11 02:26:2173 EXPECT_TRUE(chrome::ParseFaviconPath(path4, &parsed));
[email protected]78dfe382013-07-17 18:05:4274 EXPECT_FALSE(parsed.is_icon_url);
75 EXPECT_EQ(url, parsed.url);
76 EXPECT_EQ(16, parsed.size_in_dip);
[email protected]abe29e2a2014-06-20 23:20:5377 EXPECT_EQ(1.41f, parsed.device_scale_factor);
[email protected]78dfe382013-07-17 18:05:4278
79 // Invalid cases.
80 const std::string path5 = "size/" + url;
pkotwiczd95aa6592017-05-11 02:26:2181 EXPECT_FALSE(chrome::ParseFaviconPath(path5, &parsed));
[email protected]78dfe382013-07-17 18:05:4282 const std::string path6 = "size/@1x/" + url;
pkotwiczd95aa6592017-05-11 02:26:2183 EXPECT_FALSE(chrome::ParseFaviconPath(path6, &parsed));
[email protected]78dfe382013-07-17 18:05:4284 const std::string path7 = "size/abc@1x/" + url;
pkotwiczd95aa6592017-05-11 02:26:2185 EXPECT_FALSE(chrome::ParseFaviconPath(path7, &parsed));
[email protected]78dfe382013-07-17 18:05:4286
87 // Part of url looks like 'size' parameter.
88 const std::string path8 = "http://www.google.com/size/[email protected]";
pkotwiczd95aa6592017-05-11 02:26:2189 EXPECT_TRUE(chrome::ParseFaviconPath(path8, &parsed));
[email protected]78dfe382013-07-17 18:05:4290 EXPECT_FALSE(parsed.is_icon_url);
91 EXPECT_EQ(path8, parsed.url);
92 EXPECT_EQ(16, parsed.size_in_dip);
[email protected]abe29e2a2014-06-20 23:20:5393 EXPECT_EQ(1.0f, parsed.device_scale_factor);
[email protected]78dfe382013-07-17 18:05:4294}
95
[email protected]78dfe382013-07-17 18:05:4296// Test parsing path with 'iconurl' parameter.
97TEST_F(FaviconUrlParserTest, ParsingIconUrlParam) {
98 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
[email protected]78dfe382013-07-17 18:05:4299 chrome::ParsedFaviconPath parsed;
100
101 const std::string path10 = "iconurl/http://www.google.com/favicon.ico";
pkotwiczd95aa6592017-05-11 02:26:21102 EXPECT_TRUE(chrome::ParseFaviconPath(path10, &parsed));
[email protected]78dfe382013-07-17 18:05:42103 EXPECT_TRUE(parsed.is_icon_url);
104 EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url);
105 EXPECT_EQ(16, parsed.size_in_dip);
[email protected]32730eb2014-06-26 23:49:21106 EXPECT_EQ(1.0f, parsed.device_scale_factor);
[email protected]78dfe382013-07-17 18:05:42107}
108
[email protected]78dfe382013-07-17 18:05:42109// Test parsing paths with both a 'size' parameter and a 'url modifier'
110// parameter.
111TEST_F(FaviconUrlParserTest, ParsingSizeParamAndUrlModifier) {
112 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
[email protected]78dfe382013-07-17 18:05:42113 chrome::ParsedFaviconPath parsed;
114
[email protected]78dfe382013-07-17 18:05:42115 const std::string path14 =
pkotwiczac8799f2017-05-02 17:50:22116 "size/32/iconurl/http://www.google.com/favicon.ico";
pkotwiczd95aa6592017-05-11 02:26:21117 EXPECT_TRUE(chrome::ParseFaviconPath(path14, &parsed));
[email protected]78dfe382013-07-17 18:05:42118 EXPECT_TRUE(parsed.is_icon_url);
119 EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url);
pkotwiczac8799f2017-05-02 17:50:22120 EXPECT_EQ(32, parsed.size_in_dip);
[email protected]78dfe382013-07-17 18:05:42121}