[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 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. |
| 4 | |
blundell | ee0ade2 | 2015-07-08 10:07:45 | [diff] [blame] | 5 | #include "components/favicon_base/favicon_url_parser.h" |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 6 | |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 9 | #include "base/macros.h" |
[email protected] | 7627e0b4 | 2014-04-17 17:20:53 | [diff] [blame] | 10 | #include "components/favicon_base/favicon_types.h" |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "ui/base/layout.h" |
| 13 | |
| 14 | class 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 | |
dcheng | d002275 | 2014-10-28 00:09:06 | [diff] [blame] | 26 | ~FaviconUrlParserTest() override {} |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 27 | |
| 28 | private: |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 29 | typedef std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 30 | 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. |
| 37 | TEST_F(FaviconUrlParserTest, ParsingNoExtraParams) { |
| 38 | const std::string url("https://www.google.ca/imghp?hl=en&tab=wi"); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 39 | chrome::ParsedFaviconPath parsed; |
| 40 | |
| 41 | const std::string path1 = url; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 42 | EXPECT_TRUE(chrome::ParseFaviconPath(path1, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 43 | EXPECT_FALSE(parsed.is_icon_url); |
| 44 | EXPECT_EQ(url, parsed.url); |
| 45 | EXPECT_EQ(16, parsed.size_in_dip); |
[email protected] | 32730eb | 2014-06-26 23:49:21 | [diff] [blame] | 46 | EXPECT_EQ(1.0f, parsed.device_scale_factor); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Test parsing path with a 'size' parameter. |
| 50 | TEST_F(FaviconUrlParserTest, ParsingSizeParam) { |
| 51 | const std::string url("https://www.google.ca/imghp?hl=en&tab=wi"); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 52 | chrome::ParsedFaviconPath parsed; |
| 53 | |
| 54 | // Test that we can still parse the legacy 'size' parameter format. |
| 55 | const std::string path2 = "size/32/" + url; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 56 | EXPECT_TRUE(chrome::ParseFaviconPath(path2, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 57 | EXPECT_FALSE(parsed.is_icon_url); |
| 58 | EXPECT_EQ(url, parsed.url); |
| 59 | EXPECT_EQ(32, parsed.size_in_dip); |
[email protected] | abe29e2a | 2014-06-20 23:20:53 | [diff] [blame] | 60 | EXPECT_EQ(1.0f, parsed.device_scale_factor); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 61 | |
| 62 | // Test parsing current 'size' parameter format. |
| 63 | const std::string path3 = "size/[email protected]/" + url; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 64 | EXPECT_TRUE(chrome::ParseFaviconPath(path3, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 65 | EXPECT_FALSE(parsed.is_icon_url); |
| 66 | EXPECT_EQ(url, parsed.url); |
| 67 | EXPECT_EQ(32, parsed.size_in_dip); |
[email protected] | abe29e2a | 2014-06-20 23:20:53 | [diff] [blame] | 68 | EXPECT_EQ(1.4f, parsed.device_scale_factor); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 69 | |
| 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; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 73 | EXPECT_TRUE(chrome::ParseFaviconPath(path4, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 74 | EXPECT_FALSE(parsed.is_icon_url); |
| 75 | EXPECT_EQ(url, parsed.url); |
| 76 | EXPECT_EQ(16, parsed.size_in_dip); |
[email protected] | abe29e2a | 2014-06-20 23:20:53 | [diff] [blame] | 77 | EXPECT_EQ(1.41f, parsed.device_scale_factor); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 78 | |
| 79 | // Invalid cases. |
| 80 | const std::string path5 = "size/" + url; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 81 | EXPECT_FALSE(chrome::ParseFaviconPath(path5, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 82 | const std::string path6 = "size/@1x/" + url; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 83 | EXPECT_FALSE(chrome::ParseFaviconPath(path6, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 84 | const std::string path7 = "size/abc@1x/" + url; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 85 | EXPECT_FALSE(chrome::ParseFaviconPath(path7, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 86 | |
| 87 | // Part of url looks like 'size' parameter. |
| 88 | const std::string path8 = "http://www.google.com/size/[email protected]"; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 89 | EXPECT_TRUE(chrome::ParseFaviconPath(path8, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 90 | EXPECT_FALSE(parsed.is_icon_url); |
| 91 | EXPECT_EQ(path8, parsed.url); |
| 92 | EXPECT_EQ(16, parsed.size_in_dip); |
[email protected] | abe29e2a | 2014-06-20 23:20:53 | [diff] [blame] | 93 | EXPECT_EQ(1.0f, parsed.device_scale_factor); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 96 | // Test parsing path with 'iconurl' parameter. |
| 97 | TEST_F(FaviconUrlParserTest, ParsingIconUrlParam) { |
| 98 | const std::string url("https://www.google.ca/imghp?hl=en&tab=wi"); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 99 | chrome::ParsedFaviconPath parsed; |
| 100 | |
| 101 | const std::string path10 = "iconurl/http://www.google.com/favicon.ico"; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 102 | EXPECT_TRUE(chrome::ParseFaviconPath(path10, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 103 | 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] | 32730eb | 2014-06-26 23:49:21 | [diff] [blame] | 106 | EXPECT_EQ(1.0f, parsed.device_scale_factor); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 109 | // Test parsing paths with both a 'size' parameter and a 'url modifier' |
| 110 | // parameter. |
| 111 | TEST_F(FaviconUrlParserTest, ParsingSizeParamAndUrlModifier) { |
| 112 | const std::string url("https://www.google.ca/imghp?hl=en&tab=wi"); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 113 | chrome::ParsedFaviconPath parsed; |
| 114 | |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 115 | const std::string path14 = |
pkotwicz | ac8799f | 2017-05-02 17:50:22 | [diff] [blame] | 116 | "size/32/iconurl/http://www.google.com/favicon.ico"; |
pkotwicz | d95aa659 | 2017-05-11 02:26:21 | [diff] [blame] | 117 | EXPECT_TRUE(chrome::ParseFaviconPath(path14, &parsed)); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 118 | EXPECT_TRUE(parsed.is_icon_url); |
| 119 | EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url); |
pkotwicz | ac8799f | 2017-05-02 17:50:22 | [diff] [blame] | 120 | EXPECT_EQ(32, parsed.size_in_dip); |
[email protected] | 78dfe38 | 2013-07-17 18:05:42 | [diff] [blame] | 121 | } |