| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/favicon_source.h" | 5 #include "chrome/browser/ui/webui/favicon_source.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "chrome/browser/search/instant_service.h" | 9 #include "chrome/browser/search/instant_service.h" |
| 10 #include "chrome/browser/search/instant_service_factory.h" | 10 #include "chrome/browser/search/instant_service_factory.h" |
| 11 #include "chrome/common/favicon_url_parser.h" |
| 11 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/base/layout.h" | 15 #include "ui/base/layout.h" |
| 15 | 16 |
| 16 class FaviconSourceTest : public testing::Test { | 17 class FaviconSourceTest : public testing::Test { |
| 17 public: | 18 public: |
| 18 FaviconSourceTest() | 19 FaviconSourceTest() |
| 19 : loop_(base::MessageLoop::TYPE_UI), | 20 : loop_(base::MessageLoop::TYPE_UI), |
| 20 ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()), | 21 ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 45 | 46 |
| 46 typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors> | 47 typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors> |
| 47 ScopedSetSupportedScaleFactors; | 48 ScopedSetSupportedScaleFactors; |
| 48 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_; | 49 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_; |
| 49 | 50 |
| 50 scoped_ptr<FaviconSource> favicon_source_; | 51 scoped_ptr<FaviconSource> favicon_source_; |
| 51 | 52 |
| 52 DISALLOW_COPY_AND_ASSIGN(FaviconSourceTest); | 53 DISALLOW_COPY_AND_ASSIGN(FaviconSourceTest); |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 // Test parsing the chrome-search://favicon/ URLs | |
| 56 TEST_F(FaviconSourceTest, InstantParsing) { | |
| 57 const std::string path("chrome-search://favicon/http://www.google.com"); | |
| 58 bool is_icon_url; | |
| 59 GURL url; | |
| 60 int size_in_dip; | |
| 61 ui::ScaleFactor scale_factor; | |
| 62 | |
| 63 EXPECT_TRUE(favicon_source()->ParsePath(path, &is_icon_url, &url, | |
| 64 &size_in_dip, &scale_factor)); | |
| 65 EXPECT_FALSE(is_icon_url); | |
| 66 EXPECT_EQ(GURL(path), url); | |
| 67 EXPECT_EQ(16, size_in_dip); | |
| 68 EXPECT_EQ(ui::SCALE_FACTOR_100P, scale_factor); | |
| 69 } | |
| 70 | |
| 71 // Test parsing the chrome://favicon URLs | 56 // Test parsing the chrome://favicon URLs |
| 72 TEST_F(FaviconSourceTest, Parsing) { | 57 TEST_F(FaviconSourceTest, Parsing) { |
| 73 const GURL kUrl("https://www.google.ca/imghp?hl=en&tab=wi"); | 58 const std::string url("https://www.google.ca/imghp?hl=en&tab=wi"); |
| 74 | 59 int icon_types = chrome::TOUCH_PRECOMPOSED_ICON; |
| 75 bool is_icon_url; | 60 chrome::ParsedFaviconPath parsed; |
| 76 GURL url; | |
| 77 int size_in_dip; | |
| 78 ui::ScaleFactor scale_factor; | |
| 79 | 61 |
| 80 // 1) Test parsing path with no extra parameters. | 62 // 1) Test parsing path with no extra parameters. |
| 81 const std::string path1 = kUrl.spec(); | 63 const std::string path1 = url; |
| 82 EXPECT_TRUE(favicon_source()->ParsePath(path1, &is_icon_url, &url, | 64 EXPECT_TRUE(chrome::ParseFaviconPath(path1, icon_types, &parsed)); |
| 83 &size_in_dip, &scale_factor)); | 65 EXPECT_FALSE(parsed.is_icon_url); |
| 84 EXPECT_FALSE(is_icon_url); | 66 EXPECT_EQ(url, parsed.url); |
| 85 EXPECT_EQ(kUrl, url); | 67 EXPECT_EQ(16, parsed.size_in_dip); |
| 86 EXPECT_EQ(16, size_in_dip); | 68 EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); |
| 87 EXPECT_EQ(ui::SCALE_FACTOR_100P, scale_factor); | |
| 88 | 69 |
| 89 // 2) Test parsing path with a 'size' parameter. | 70 // 2) Test parsing path with a 'size' parameter. |
| 90 // | 71 // |
| 91 // Test that we can still parse the legacy 'size' parameter format. | 72 // Test that we can still parse the legacy 'size' parameter format. |
| 92 const std::string path2 = "size/32/" + kUrl.spec(); | 73 const std::string path2 = "size/32/" + url; |
| 93 EXPECT_TRUE(favicon_source()->ParsePath(path2, &is_icon_url, &url, | 74 EXPECT_TRUE(chrome::ParseFaviconPath(path2, icon_types, &parsed)); |
| 94 &size_in_dip, &scale_factor)); | 75 EXPECT_FALSE(parsed.is_icon_url); |
| 95 EXPECT_FALSE(is_icon_url); | 76 EXPECT_EQ(url, parsed.url); |
| 96 EXPECT_EQ(kUrl, url); | 77 EXPECT_EQ(32, parsed.size_in_dip); |
| 97 EXPECT_EQ(32, size_in_dip); | 78 EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); |
| 98 EXPECT_EQ(ui::SCALE_FACTOR_100P, scale_factor); | |
| 99 | 79 |
| 100 // Test parsing current 'size' parameter format. | 80 // Test parsing current 'size' parameter format. |
| 101 const std::string path3 = "size/32@1.4x/" + kUrl.spec(); | 81 const std::string path3 = "size/32@1.4x/" + url; |
| 102 EXPECT_TRUE(favicon_source()->ParsePath(path3, &is_icon_url, &url, | 82 EXPECT_TRUE(chrome::ParseFaviconPath(path3, icon_types, &parsed)); |
| 103 &size_in_dip, &scale_factor)); | 83 EXPECT_FALSE(parsed.is_icon_url); |
| 104 EXPECT_FALSE(is_icon_url); | 84 EXPECT_EQ(url, parsed.url); |
| 105 EXPECT_EQ(kUrl, url); | 85 EXPECT_EQ(32, parsed.size_in_dip); |
| 106 EXPECT_EQ(32, size_in_dip); | 86 EXPECT_EQ(ui::SCALE_FACTOR_140P, parsed.scale_factor); |
| 107 EXPECT_EQ(ui::SCALE_FACTOR_140P, scale_factor); | |
| 108 | 87 |
| 109 // Test that we pick the ui::ScaleFactor which is closest to the passed in | 88 // Test that we pick the ui::ScaleFactor which is closest to the passed in |
| 110 // scale factor. | 89 // scale factor. |
| 111 const std::string path4 = "size/16@1.41x/" + kUrl.spec(); | 90 const std::string path4 = "size/16@1.41x/" + url; |
| 112 EXPECT_TRUE(favicon_source()->ParsePath(path4, &is_icon_url, &url, | 91 EXPECT_TRUE(chrome::ParseFaviconPath(path4, icon_types, &parsed)); |
| 113 &size_in_dip, &scale_factor)); | 92 EXPECT_FALSE(parsed.is_icon_url); |
| 114 EXPECT_FALSE(is_icon_url); | 93 EXPECT_EQ(url, parsed.url); |
| 115 EXPECT_EQ(kUrl, url); | 94 EXPECT_EQ(16, parsed.size_in_dip); |
| 116 EXPECT_EQ(16, size_in_dip); | 95 EXPECT_EQ(ui::SCALE_FACTOR_140P, parsed.scale_factor); |
| 117 EXPECT_EQ(ui::SCALE_FACTOR_140P, scale_factor); | |
| 118 | 96 |
| 119 // Invalid cases. | 97 // Invalid cases. |
| 120 const std::string path5 = "size/" + kUrl.spec(); | 98 const std::string path5 = "size/" + url; |
| 121 EXPECT_FALSE(favicon_source()->ParsePath(path5, &is_icon_url, &url, | 99 EXPECT_FALSE(chrome::ParseFaviconPath(path5, icon_types, &parsed)); |
| 122 &size_in_dip, &scale_factor)); | 100 const std::string path6 = "size/@1x/" + url; |
| 123 const std::string path6 = "size/@1x/" + kUrl.spec(); | 101 EXPECT_FALSE(chrome::ParseFaviconPath(path6, icon_types, &parsed)); |
| 124 EXPECT_FALSE(favicon_source()->ParsePath(path6, &is_icon_url, &url, | 102 const std::string path7 = "size/abc@1x/" + url; |
| 125 &size_in_dip, &scale_factor)); | 103 EXPECT_FALSE(chrome::ParseFaviconPath(path7, icon_types, &parsed)); |
| 126 const std::string path7 = "size/abc@1x/" + kUrl.spec(); | |
| 127 EXPECT_FALSE(favicon_source()->ParsePath(path7, &is_icon_url, &url, | |
| 128 &size_in_dip, &scale_factor)); | |
| 129 | 104 |
| 130 // Part of url looks like 'size' parameter. | 105 // Part of url looks like 'size' parameter. |
| 131 const std::string path8 = "http://www.google.com/size/32@1.4x"; | 106 const std::string path8 = "http://www.google.com/size/32@1.4x"; |
| 132 EXPECT_TRUE(favicon_source()->ParsePath(path8, &is_icon_url, &url, | 107 EXPECT_TRUE(chrome::ParseFaviconPath(path8, icon_types, &parsed)); |
| 133 &size_in_dip, &scale_factor)); | 108 EXPECT_FALSE(parsed.is_icon_url); |
| 134 EXPECT_FALSE(is_icon_url); | 109 EXPECT_EQ(path8, parsed.url); |
| 135 EXPECT_EQ(path8, url.spec()); | 110 EXPECT_EQ(16, parsed.size_in_dip); |
| 136 EXPECT_EQ(16, size_in_dip); | 111 EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); |
| 137 EXPECT_EQ(ui::SCALE_FACTOR_100P, scale_factor); | |
| 138 | 112 |
| 139 // 3) Test parsing path with the 'largest' parameter. | 113 // 3) Test parsing path with the 'largest' parameter. |
| 140 const std::string path9 = "largest/" + kUrl.spec(); | 114 const std::string path9 = "largest/" + url; |
| 141 EXPECT_TRUE(favicon_source()->ParsePath(path9, &is_icon_url, &url, | 115 EXPECT_TRUE(chrome::ParseFaviconPath(path9, icon_types, &parsed)); |
| 142 &size_in_dip, &scale_factor)); | 116 EXPECT_FALSE(parsed.is_icon_url); |
| 143 EXPECT_FALSE(is_icon_url); | 117 EXPECT_EQ(url, parsed.url); |
| 144 EXPECT_EQ(kUrl, url); | 118 EXPECT_EQ(0, parsed.size_in_dip); |
| 145 EXPECT_EQ(0, size_in_dip); | |
| 146 // The scale factor is meaningless when requesting the largest favicon. | 119 // The scale factor is meaningless when requesting the largest favicon. |
| 147 | 120 |
| 148 // 4) Test parsing path with 'iconurl' parameter. | 121 // 4) Test parsing path with 'iconurl' parameter. |
| 149 const std::string path10 = "iconurl/http://www.google.com/favicon.ico"; | 122 const std::string path10 = "iconurl/http://www.google.com/favicon.ico"; |
| 150 EXPECT_TRUE(favicon_source()->ParsePath(path10, &is_icon_url, &url, | 123 EXPECT_TRUE(chrome::ParseFaviconPath(path10, icon_types, &parsed)); |
| 151 &size_in_dip, &scale_factor)); | 124 EXPECT_TRUE(parsed.is_icon_url); |
| 152 EXPECT_TRUE(is_icon_url); | 125 EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url); |
| 153 EXPECT_EQ("http://www.google.com/favicon.ico", url.spec()); | 126 EXPECT_EQ(16, parsed.size_in_dip); |
| 154 EXPECT_EQ(16, size_in_dip); | 127 EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); |
| 155 EXPECT_EQ(ui::SCALE_FACTOR_100P, scale_factor); | |
| 156 | 128 |
| 157 // 5) Test parsing path with 'origin' parameter. | 129 // 5) Test parsing path with 'origin' parameter. |
| 158 const std::string path11 = "origin/" + kUrl.spec(); | 130 const std::string path11 = "origin/" + url; |
| 159 EXPECT_TRUE(favicon_source()->ParsePath(path11, &is_icon_url, &url, | 131 EXPECT_TRUE(chrome::ParseFaviconPath(path11, icon_types, &parsed)); |
| 160 &size_in_dip, &scale_factor)); | 132 EXPECT_FALSE(parsed.is_icon_url); |
| 161 EXPECT_FALSE(is_icon_url); | 133 EXPECT_EQ("https://www.google.ca/", parsed.url); |
| 162 EXPECT_EQ("https://www.google.ca/", url.spec()); | 134 EXPECT_EQ(16, parsed.size_in_dip); |
| 163 EXPECT_EQ(16, size_in_dip); | 135 EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); |
| 164 EXPECT_EQ(ui::SCALE_FACTOR_100P, scale_factor); | |
| 165 | 136 |
| 166 const std::string path12 = "origin/google.com"; | 137 const std::string path12 = "origin/google.com"; |
| 167 EXPECT_TRUE(favicon_source()->ParsePath(path12, &is_icon_url, &url, | 138 EXPECT_TRUE(chrome::ParseFaviconPath(path12, icon_types, &parsed)); |
| 168 &size_in_dip, &scale_factor)); | 139 EXPECT_FALSE(parsed.is_icon_url); |
| 169 EXPECT_FALSE(is_icon_url); | 140 EXPECT_EQ("http://google.com/", parsed.url); |
| 170 EXPECT_EQ("http://google.com/", url.spec()); | 141 EXPECT_EQ(16, parsed.size_in_dip); |
| 171 EXPECT_EQ(16, size_in_dip); | 142 EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); |
| 172 EXPECT_EQ(ui::SCALE_FACTOR_100P, scale_factor); | |
| 173 | 143 |
| 174 // 6) Test parsing paths with both a 'size' parameter and a 'url modifier' | 144 // 6) Test parsing paths with both a 'size' parameter and a 'url modifier' |
| 175 // parameter. | 145 // parameter. |
| 176 const std::string path13 = "size/32@1.4x/origin/" + kUrl.spec(); | 146 const std::string path13 = "size/32@1.4x/origin/" + url; |
| 177 EXPECT_TRUE(favicon_source()->ParsePath(path13, &is_icon_url, &url, | 147 EXPECT_TRUE(chrome::ParseFaviconPath(path13, icon_types, &parsed)); |
| 178 &size_in_dip, &scale_factor)); | 148 EXPECT_FALSE(parsed.is_icon_url); |
| 179 EXPECT_FALSE(is_icon_url); | 149 EXPECT_EQ("https://www.google.ca/", parsed.url); |
| 180 EXPECT_EQ("https://www.google.ca/", url.spec()); | 150 EXPECT_EQ(32, parsed.size_in_dip); |
| 181 EXPECT_EQ(32, size_in_dip); | 151 EXPECT_EQ(ui::SCALE_FACTOR_140P, parsed.scale_factor); |
| 182 EXPECT_EQ(ui::SCALE_FACTOR_140P, scale_factor); | |
| 183 | 152 |
| 184 const std::string path14 = | 153 const std::string path14 = |
| 185 "largest/iconurl/http://www.google.com/favicon.ico"; | 154 "largest/iconurl/http://www.google.com/favicon.ico"; |
| 186 EXPECT_TRUE(favicon_source()->ParsePath(path14, &is_icon_url, &url, | 155 EXPECT_TRUE(chrome::ParseFaviconPath(path14, icon_types, &parsed)); |
| 187 &size_in_dip, &scale_factor)); | 156 EXPECT_TRUE(parsed.is_icon_url); |
| 188 EXPECT_TRUE(is_icon_url); | 157 EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url); |
| 189 EXPECT_EQ("http://www.google.com/favicon.ico", url.spec()); | 158 EXPECT_EQ(0, parsed.size_in_dip); |
| 190 EXPECT_EQ(0, size_in_dip); | |
| 191 } | 159 } |
| OLD | NEW |