| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/manifest/manifest_icon_selector.h" | 5 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 using IconPurpose = content::Manifest::Icon::IconPurpose; |
| 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 const int kDefaultIconSize = 144; | 18 const int kIdealIconSize = 144; |
| 17 | 19 const int kMinimumIconSize = 0; |
| 18 GURL FindBestMatchingIconWithMinimum( | |
| 19 const std::vector<content::Manifest::Icon>& icons, | |
| 20 int ideal_icon_size_in_px, | |
| 21 int minimum_icon_size_in_px) { | |
| 22 return ManifestIconSelector::FindBestMatchingIcon( | |
| 23 icons, ideal_icon_size_in_px, minimum_icon_size_in_px); | |
| 24 } | |
| 25 | |
| 26 GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons, | |
| 27 int ideal_icon_size_in_px) { | |
| 28 return FindBestMatchingIconWithMinimum(icons, ideal_icon_size_in_px, 0); | |
| 29 } | |
| 30 | 20 |
| 31 static content::Manifest::Icon CreateIcon(const std::string& url, | 21 static content::Manifest::Icon CreateIcon(const std::string& url, |
| 32 const std::string& type, | 22 const std::string& type, |
| 33 const std::vector<gfx::Size> sizes) { | 23 const std::vector<gfx::Size> sizes, |
| 24 IconPurpose purpose) { |
| 34 content::Manifest::Icon icon; | 25 content::Manifest::Icon icon; |
| 35 icon.src = GURL(url); | 26 icon.src = GURL(url); |
| 36 icon.type = base::UTF8ToUTF16(type); | 27 icon.type = base::UTF8ToUTF16(type); |
| 37 icon.sizes = sizes; | 28 icon.sizes = sizes; |
| 29 icon.purpose.push_back(purpose); |
| 38 | 30 |
| 39 return icon; | 31 return icon; |
| 40 } | 32 } |
| 41 | 33 |
| 42 } // anonymous namespace | 34 } // anonymous namespace |
| 43 | 35 |
| 44 TEST(ManifestIconSelector, NoIcons) { | 36 TEST(ManifestIconSelector, NoIcons) { |
| 45 // No icons should return the empty URL. | 37 // No icons should return the empty URL. |
| 46 std::vector<content::Manifest::Icon> icons; | 38 std::vector<content::Manifest::Icon> icons; |
| 47 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 39 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 40 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 48 EXPECT_TRUE(url.is_empty()); | 41 EXPECT_TRUE(url.is_empty()); |
| 49 } | 42 } |
| 50 | 43 |
| 51 TEST(ManifestIconSelector, NoSizes) { | 44 TEST(ManifestIconSelector, NoSizes) { |
| 52 // Icon with no sizes are ignored. | 45 // Icon with no sizes are ignored. |
| 53 std::vector<content::Manifest::Icon> icons; | 46 std::vector<content::Manifest::Icon> icons; |
| 54 icons.push_back( | 47 icons.push_back(CreateIcon("http://foo.com/icon.png", "", |
| 55 CreateIcon("http://foo.com/icon.png", "", std::vector<gfx::Size>())); | 48 std::vector<gfx::Size>(), IconPurpose::ANY)); |
| 56 | 49 |
| 57 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 50 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 51 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 58 EXPECT_TRUE(url.is_empty()); | 52 EXPECT_TRUE(url.is_empty()); |
| 59 } | 53 } |
| 60 | 54 |
| 61 TEST(ManifestIconSelector, MIMETypeFiltering) { | 55 TEST(ManifestIconSelector, MIMETypeFiltering) { |
| 62 // Icons with type specified to a MIME type that isn't a valid image MIME type | 56 // Icons with type specified to a MIME type that isn't a valid image MIME type |
| 63 // are ignored. | 57 // are ignored. |
| 64 std::vector<gfx::Size> sizes; | 58 std::vector<gfx::Size> sizes; |
| 65 sizes.push_back(gfx::Size(1024, 1024)); | 59 sizes.push_back(gfx::Size(1024, 1024)); |
| 66 | 60 |
| 67 std::vector<content::Manifest::Icon> icons; | 61 std::vector<content::Manifest::Icon> icons; |
| 62 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/foo_bar", sizes, |
| 63 IconPurpose::ANY)); |
| 68 icons.push_back( | 64 icons.push_back( |
| 69 CreateIcon("http://foo.com/icon.png", "image/foo_bar", sizes)); | 65 CreateIcon("http://foo.com/icon.png", "image/", sizes, IconPurpose::ANY)); |
| 70 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", sizes)); | 66 icons.push_back( |
| 71 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", sizes)); | 67 CreateIcon("http://foo.com/icon.png", "image/", sizes, IconPurpose::ANY)); |
| 72 icons.push_back(CreateIcon("http://foo.com/icon.png", "video/mp4", sizes)); | 68 icons.push_back(CreateIcon("http://foo.com/icon.png", "video/mp4", sizes, |
| 69 IconPurpose::ANY)); |
| 73 | 70 |
| 74 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 71 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 72 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 75 EXPECT_TRUE(url.is_empty()); | 73 EXPECT_TRUE(url.is_empty()); |
| 76 | 74 |
| 77 icons.clear(); | 75 icons.clear(); |
| 78 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/png", sizes)); | 76 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/png", sizes, |
| 79 url = FindBestMatchingIcon(icons, kDefaultIconSize); | 77 IconPurpose::ANY)); |
| 78 url = ManifestIconSelector::FindBestMatchingIcon( |
| 79 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 80 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 80 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 81 | 81 |
| 82 icons.clear(); | 82 icons.clear(); |
| 83 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/gif", sizes)); | 83 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/gif", sizes, |
| 84 url = FindBestMatchingIcon(icons, kDefaultIconSize); | 84 IconPurpose::ANY)); |
| 85 url = ManifestIconSelector::FindBestMatchingIcon( |
| 86 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 85 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 87 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 86 | 88 |
| 87 icons.clear(); | 89 icons.clear(); |
| 88 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/jpeg", sizes)); | 90 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/jpeg", sizes, |
| 89 url = FindBestMatchingIcon(icons, kDefaultIconSize); | 91 IconPurpose::ANY)); |
| 92 url = ManifestIconSelector::FindBestMatchingIcon( |
| 93 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 90 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 94 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 91 } | 95 } |
| 92 | 96 |
| 93 TEST(ManifestIconSelector, PreferredSizeIsUsedFirst) { | 97 TEST(ManifestIconSelector, PurposeFiltering) { |
| 94 // Each icon is marked with sizes that match the preferred icon size. | 98 // Icons with purpose specified to non-matching purpose are ignored. |
| 95 std::vector<gfx::Size> sizes_48; | 99 std::vector<gfx::Size> sizes_48; |
| 96 sizes_48.push_back(gfx::Size(48, 48)); | 100 sizes_48.push_back(gfx::Size(48, 48)); |
| 97 | 101 |
| 102 std::vector<gfx::Size> sizes_96; |
| 103 sizes_96.push_back(gfx::Size(96, 96)); |
| 104 |
| 105 std::vector<gfx::Size> sizes_144; |
| 106 sizes_144.push_back(gfx::Size(144, 144)); |
| 107 |
| 108 std::vector<content::Manifest::Icon> icons; |
| 109 icons.push_back( |
| 110 CreateIcon("http://foo.com/icon_48.png", "", sizes_48, IconPurpose::BADGE)
); |
| 111 icons.push_back( |
| 112 CreateIcon("http://foo.com/icon_96.png", "", sizes_96, IconPurpose::ANY)); |
| 113 icons.push_back(CreateIcon("http://foo.com/icon_144.png", "", sizes_144, |
| 114 IconPurpose::ANY)); |
| 115 |
| 116 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 117 icons, 48, kMinimumIconSize, IconPurpose::BADGE); |
| 118 EXPECT_EQ("http://foo.com/icon_48.png", url.spec()); |
| 119 |
| 120 url = ManifestIconSelector::FindBestMatchingIcon( |
| 121 icons, 48, kMinimumIconSize, IconPurpose::ANY); |
| 122 EXPECT_EQ("http://foo.com/icon_96.png", url.spec()); |
| 123 |
| 124 url = ManifestIconSelector::FindBestMatchingIcon(icons, 96, kMinimumIconSize, |
| 125 IconPurpose::BADGE); |
| 126 EXPECT_EQ("http://foo.com/icon_48.png", url.spec()); |
| 127 |
| 128 url = ManifestIconSelector::FindBestMatchingIcon(icons, 96, 96, |
| 129 IconPurpose::BADGE); |
| 130 EXPECT_TRUE(url.is_empty()); |
| 131 |
| 132 url = ManifestIconSelector::FindBestMatchingIcon(icons, 144, kMinimumIconSize, |
| 133 IconPurpose::ANY); |
| 134 EXPECT_EQ("http://foo.com/icon_144.png", url.spec()); |
| 135 } |
| 136 |
| 137 TEST(ManifestIconSelector, IdealSizeIsUsedFirst) { |
| 138 // Each icon is marked with sizes that match the ideal icon size. |
| 139 std::vector<gfx::Size> sizes_48; |
| 140 sizes_48.push_back(gfx::Size(48, 48)); |
| 141 |
| 98 std::vector<gfx::Size> sizes_96; | 142 std::vector<gfx::Size> sizes_96; |
| 99 sizes_96.push_back(gfx::Size(96, 96)); | 143 sizes_96.push_back(gfx::Size(96, 96)); |
| 100 | 144 |
| 101 std::vector<gfx::Size> sizes_144; | 145 std::vector<gfx::Size> sizes_144; |
| 102 sizes_144.push_back(gfx::Size(144, 144)); | 146 sizes_144.push_back(gfx::Size(144, 144)); |
| 103 | 147 |
| 104 std::vector<content::Manifest::Icon> icons; | 148 std::vector<content::Manifest::Icon> icons; |
| 105 icons.push_back(CreateIcon("http://foo.com/icon_48.png", "", sizes_48)); | 149 icons.push_back( |
| 106 icons.push_back(CreateIcon("http://foo.com/icon_96.png", "", sizes_96)); | 150 CreateIcon("http://foo.com/icon_48.png", "", sizes_48, IconPurpose::ANY)); |
| 107 icons.push_back(CreateIcon("http://foo.com/icon_144.png", "", sizes_144)); | 151 icons.push_back( |
| 152 CreateIcon("http://foo.com/icon_96.png", "", sizes_96, IconPurpose::ANY)); |
| 153 icons.push_back(CreateIcon("http://foo.com/icon_144.png", "", sizes_144, |
| 154 IconPurpose::ANY)); |
| 108 | 155 |
| 109 GURL url = FindBestMatchingIcon(icons, 48); | 156 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 157 icons, 48, kMinimumIconSize, IconPurpose::ANY); |
| 110 EXPECT_EQ("http://foo.com/icon_48.png", url.spec()); | 158 EXPECT_EQ("http://foo.com/icon_48.png", url.spec()); |
| 111 | 159 |
| 112 url = FindBestMatchingIcon(icons, 96); | 160 url = ManifestIconSelector::FindBestMatchingIcon(icons, 96, kMinimumIconSize, |
| 161 IconPurpose::ANY); |
| 113 EXPECT_EQ("http://foo.com/icon_96.png", url.spec()); | 162 EXPECT_EQ("http://foo.com/icon_96.png", url.spec()); |
| 114 | 163 |
| 115 url = FindBestMatchingIcon(icons, 144); | 164 url = ManifestIconSelector::FindBestMatchingIcon(icons, 144, kMinimumIconSize, |
| 165 IconPurpose::ANY); |
| 116 EXPECT_EQ("http://foo.com/icon_144.png", url.spec()); | 166 EXPECT_EQ("http://foo.com/icon_144.png", url.spec()); |
| 117 } | 167 } |
| 118 | 168 |
| 119 TEST(ManifestIconSelector, FirstIconWithPreferredSizeIsUsedFirst) { | 169 TEST(ManifestIconSelector, FirstIconWithIdealSizeIsUsedFirst) { |
| 120 // This test has three icons. The first icon is going to be used because it | 170 // This test has three icons. The first icon is going to be used because it |
| 121 // contains the preferred size. | 171 // contains the ideal size. |
| 122 std::vector<gfx::Size> sizes_1; | 172 std::vector<gfx::Size> sizes_1; |
| 123 sizes_1.push_back(gfx::Size(kDefaultIconSize, kDefaultIconSize)); | 173 sizes_1.push_back(gfx::Size(kIdealIconSize, kIdealIconSize)); |
| 124 sizes_1.push_back(gfx::Size(kDefaultIconSize * 2, kDefaultIconSize * 2)); | 174 sizes_1.push_back(gfx::Size(kIdealIconSize * 2, kIdealIconSize * 2)); |
| 125 sizes_1.push_back(gfx::Size(kDefaultIconSize * 3, kDefaultIconSize * 3)); | 175 sizes_1.push_back(gfx::Size(kIdealIconSize * 3, kIdealIconSize * 3)); |
| 126 | 176 |
| 127 std::vector<gfx::Size> sizes_2; | 177 std::vector<gfx::Size> sizes_2; |
| 128 sizes_2.push_back(gfx::Size(1024, 1024)); | 178 sizes_2.push_back(gfx::Size(1024, 1024)); |
| 129 | 179 |
| 130 std::vector<gfx::Size> sizes_3; | 180 std::vector<gfx::Size> sizes_3; |
| 131 sizes_3.push_back(gfx::Size(1024, 1024)); | 181 sizes_3.push_back(gfx::Size(1024, 1024)); |
| 132 | 182 |
| 133 std::vector<content::Manifest::Icon> icons; | 183 std::vector<content::Manifest::Icon> icons; |
| 134 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1)); | 184 icons.push_back( |
| 135 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_2)); | 185 CreateIcon("http://foo.com/icon_x1.png", "", sizes_1, IconPurpose::ANY)); |
| 136 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", sizes_3)); | 186 icons.push_back( |
| 187 CreateIcon("http://foo.com/icon_x2.png", "", sizes_2, IconPurpose::ANY)); |
| 188 icons.push_back( |
| 189 CreateIcon("http://foo.com/icon_x3.png", "", sizes_3, IconPurpose::ANY)); |
| 137 | 190 |
| 138 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 191 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 192 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 139 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 193 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
| 140 | 194 |
| 141 url = FindBestMatchingIcon(icons, kDefaultIconSize * 2); | 195 url = ManifestIconSelector::FindBestMatchingIcon( |
| 196 icons, kIdealIconSize * 2, kMinimumIconSize, IconPurpose::ANY); |
| 142 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 197 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
| 143 | 198 |
| 144 url = FindBestMatchingIcon(icons, kDefaultIconSize * 3); | 199 url = ManifestIconSelector::FindBestMatchingIcon( |
| 200 icons, kIdealIconSize * 3, kMinimumIconSize, IconPurpose::ANY); |
| 145 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 201 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
| 146 } | 202 } |
| 147 | 203 |
| 148 TEST(ManifestIconSelector, FallbackToSmallestLargerIcon) { | 204 TEST(ManifestIconSelector, FallbackToSmallestLargerIcon) { |
| 149 // If there is no perfect icon, the smallest larger icon will be chosen. | 205 // If there is no perfect icon, the smallest larger icon will be chosen. |
| 150 std::vector<gfx::Size> sizes_1; | 206 std::vector<gfx::Size> sizes_1; |
| 151 sizes_1.push_back(gfx::Size(90, 90)); | 207 sizes_1.push_back(gfx::Size(90, 90)); |
| 152 | 208 |
| 153 std::vector<gfx::Size> sizes_2; | 209 std::vector<gfx::Size> sizes_2; |
| 154 sizes_2.push_back(gfx::Size(128, 128)); | 210 sizes_2.push_back(gfx::Size(128, 128)); |
| 155 | 211 |
| 156 std::vector<gfx::Size> sizes_3; | 212 std::vector<gfx::Size> sizes_3; |
| 157 sizes_3.push_back(gfx::Size(192, 192)); | 213 sizes_3.push_back(gfx::Size(192, 192)); |
| 158 | 214 |
| 159 std::vector<content::Manifest::Icon> icons; | 215 std::vector<content::Manifest::Icon> icons; |
| 160 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1)); | 216 icons.push_back( |
| 161 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_2)); | 217 CreateIcon("http://foo.com/icon_x1.png", "", sizes_1, IconPurpose::ANY)); |
| 162 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", sizes_3)); | 218 icons.push_back( |
| 219 CreateIcon("http://foo.com/icon_x2.png", "", sizes_2, IconPurpose::ANY)); |
| 220 icons.push_back( |
| 221 CreateIcon("http://foo.com/icon_x3.png", "", sizes_3, IconPurpose::ANY)); |
| 163 | 222 |
| 164 GURL url = FindBestMatchingIcon(icons, 48); | 223 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 224 icons, 48, kMinimumIconSize, IconPurpose::ANY); |
| 165 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 225 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
| 166 | 226 |
| 167 url = FindBestMatchingIcon(icons, 96); | 227 url = ManifestIconSelector::FindBestMatchingIcon(icons, 96, kMinimumIconSize, |
| 228 IconPurpose::ANY); |
| 168 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec()); | 229 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec()); |
| 169 | 230 |
| 170 url = FindBestMatchingIcon(icons, 144); | 231 url = ManifestIconSelector::FindBestMatchingIcon(icons, 144, kMinimumIconSize, |
| 232 IconPurpose::ANY); |
| 171 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); | 233 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); |
| 172 } | 234 } |
| 173 | 235 |
| 174 TEST(ManifestIconSelector, FallbackToLargestIconLargerThanMinimum) { | 236 TEST(ManifestIconSelector, FallbackToLargestIconLargerThanMinimum) { |
| 175 // When an icon of the correct size has not been found, we fall back to the | 237 // When an icon of the correct size has not been found, we fall back to the |
| 176 // closest non-matching sizes. Make sure that the minimum passed is enforced. | 238 // closest non-matching sizes. Make sure that the minimum passed is enforced. |
| 177 std::vector<gfx::Size> sizes_1_2; | 239 std::vector<gfx::Size> sizes_1_2; |
| 178 std::vector<gfx::Size> sizes_3; | 240 std::vector<gfx::Size> sizes_3; |
| 179 | 241 |
| 180 sizes_1_2.push_back(gfx::Size(47, 47)); | 242 sizes_1_2.push_back(gfx::Size(47, 47)); |
| 181 sizes_3.push_back(gfx::Size(95, 95)); | 243 sizes_3.push_back(gfx::Size(95, 95)); |
| 182 | 244 |
| 183 std::vector<content::Manifest::Icon> icons; | 245 std::vector<content::Manifest::Icon> icons; |
| 184 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1_2)); | 246 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1_2, |
| 185 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_1_2)); | 247 IconPurpose::ANY)); |
| 186 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", sizes_3)); | 248 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_1_2, |
| 249 IconPurpose::ANY)); |
| 250 icons.push_back( |
| 251 CreateIcon("http://foo.com/icon_x3.png", "", sizes_3, IconPurpose::ANY)); |
| 187 | 252 |
| 188 // Icon 3 should match. | 253 // Icon 3 should match. |
| 189 GURL url = FindBestMatchingIconWithMinimum(icons, 1024, 48); | 254 GURL url = ManifestIconSelector::FindBestMatchingIcon(icons, 1024, 48, |
| 255 IconPurpose::ANY); |
| 190 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); | 256 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); |
| 191 | 257 |
| 192 // Nothing matches here as the minimum is 96. | 258 // Nothing matches here as the minimum is 96. |
| 193 url = FindBestMatchingIconWithMinimum(icons, 1024, 96); | 259 url = ManifestIconSelector::FindBestMatchingIcon(icons, 1024, 96, |
| 260 IconPurpose::ANY); |
| 194 EXPECT_TRUE(url.is_empty()); | 261 EXPECT_TRUE(url.is_empty()); |
| 195 } | 262 } |
| 196 | 263 |
| 197 TEST(ManifestIconSelector, IdealVeryCloseToMinimumMatches) { | 264 TEST(ManifestIconSelector, IdealVeryCloseToMinimumMatches) { |
| 198 std::vector<gfx::Size> sizes; | 265 std::vector<gfx::Size> sizes; |
| 199 sizes.push_back(gfx::Size(2, 2)); | 266 sizes.push_back(gfx::Size(2, 2)); |
| 200 | 267 |
| 201 std::vector<content::Manifest::Icon> icons; | 268 std::vector<content::Manifest::Icon> icons; |
| 202 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes)); | 269 icons.push_back( |
| 270 CreateIcon("http://foo.com/icon_x1.png", "", sizes, IconPurpose::ANY)); |
| 203 | 271 |
| 204 GURL url = FindBestMatchingIconWithMinimum(icons, 2, 1); | 272 GURL url = |
| 273 ManifestIconSelector::FindBestMatchingIcon(icons, 2, 1, IconPurpose::ANY); |
| 205 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 274 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
| 206 } | 275 } |
| 207 | 276 |
| 208 TEST(ManifestIconSelector, SizeVeryCloseToMinimumMatches) { | 277 TEST(ManifestIconSelector, SizeVeryCloseToMinimumMatches) { |
| 209 std::vector<gfx::Size> sizes; | 278 std::vector<gfx::Size> sizes; |
| 210 sizes.push_back(gfx::Size(2, 2)); | 279 sizes.push_back(gfx::Size(2, 2)); |
| 211 | 280 |
| 212 std::vector<content::Manifest::Icon> icons; | 281 std::vector<content::Manifest::Icon> icons; |
| 213 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes)); | 282 icons.push_back( |
| 283 CreateIcon("http://foo.com/icon_x1.png", "", sizes, IconPurpose::ANY)); |
| 214 | 284 |
| 215 GURL url = FindBestMatchingIconWithMinimum(icons, 200, 1); | 285 GURL url = ManifestIconSelector::FindBestMatchingIcon(icons, 200, 1, |
| 286 IconPurpose::ANY); |
| 216 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 287 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
| 217 } | 288 } |
| 218 | 289 |
| 219 TEST(ManifestIconSelector, NotSquareIconsAreIgnored) { | 290 TEST(ManifestIconSelector, NotSquareIconsAreIgnored) { |
| 220 std::vector<gfx::Size> sizes; | 291 std::vector<gfx::Size> sizes; |
| 221 sizes.push_back(gfx::Size(1024, 1023)); | 292 sizes.push_back(gfx::Size(1024, 1023)); |
| 222 | 293 |
| 223 std::vector<content::Manifest::Icon> icons; | 294 std::vector<content::Manifest::Icon> icons; |
| 224 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes)); | 295 icons.push_back( |
| 296 CreateIcon("http://foo.com/icon.png", "", sizes, IconPurpose::ANY)); |
| 225 | 297 |
| 226 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 298 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 299 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 227 EXPECT_TRUE(url.is_empty()); | 300 EXPECT_TRUE(url.is_empty()); |
| 228 } | 301 } |
| 229 | 302 |
| 230 TEST(ManifestIconSelector, ClosestIconToPreferred) { | 303 TEST(ManifestIconSelector, ClosestIconToIdeal) { |
| 231 // Ensure ManifestIconSelector::FindBestMatchingIcon selects the closest icon | 304 // Ensure ManifestIconSelector::FindBestMatchingIcon selects the closest icon |
| 232 // to the preferred size when presented with a number of options. | 305 // to the ideal size when presented with a number of options. |
| 233 int very_small = kDefaultIconSize / 4; | 306 int very_small = kIdealIconSize / 4; |
| 234 int small_size = kDefaultIconSize / 2; | 307 int small_size = kIdealIconSize / 2; |
| 235 int bit_small = kDefaultIconSize - 1; | 308 int bit_small = kIdealIconSize - 1; |
| 236 int bit_big = kDefaultIconSize + 1; | 309 int bit_big = kIdealIconSize + 1; |
| 237 int big = kDefaultIconSize * 2; | 310 int big = kIdealIconSize * 2; |
| 238 int very_big = kDefaultIconSize * 4; | 311 int very_big = kIdealIconSize * 4; |
| 239 | 312 |
| 240 // (very_small, bit_small) => bit_small | 313 // (very_small, bit_small) => bit_small |
| 241 { | 314 { |
| 242 std::vector<gfx::Size> sizes_1; | 315 std::vector<gfx::Size> sizes_1; |
| 243 sizes_1.push_back(gfx::Size(very_small, very_small)); | 316 sizes_1.push_back(gfx::Size(very_small, very_small)); |
| 244 | 317 |
| 245 std::vector<gfx::Size> sizes_2; | 318 std::vector<gfx::Size> sizes_2; |
| 246 sizes_2.push_back(gfx::Size(bit_small, bit_small)); | 319 sizes_2.push_back(gfx::Size(bit_small, bit_small)); |
| 247 | 320 |
| 248 std::vector<content::Manifest::Icon> icons; | 321 std::vector<content::Manifest::Icon> icons; |
| 249 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); | 322 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1, |
| 250 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); | 323 IconPurpose::ANY)); |
| 324 icons.push_back( |
| 325 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY)); |
| 251 | 326 |
| 252 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 327 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 328 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 253 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 329 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 254 } | 330 } |
| 255 | 331 |
| 256 // (very_small, bit_small, small_size) => bit_small | 332 // (very_small, bit_small, small_size) => bit_small |
| 257 { | 333 { |
| 258 std::vector<gfx::Size> sizes_1; | 334 std::vector<gfx::Size> sizes_1; |
| 259 sizes_1.push_back(gfx::Size(very_small, very_small)); | 335 sizes_1.push_back(gfx::Size(very_small, very_small)); |
| 260 | 336 |
| 261 std::vector<gfx::Size> sizes_2; | 337 std::vector<gfx::Size> sizes_2; |
| 262 sizes_2.push_back(gfx::Size(bit_small, bit_small)); | 338 sizes_2.push_back(gfx::Size(bit_small, bit_small)); |
| 263 | 339 |
| 264 std::vector<gfx::Size> sizes_3; | 340 std::vector<gfx::Size> sizes_3; |
| 265 sizes_3.push_back(gfx::Size(small_size, small_size)); | 341 sizes_3.push_back(gfx::Size(small_size, small_size)); |
| 266 | 342 |
| 267 std::vector<content::Manifest::Icon> icons; | 343 std::vector<content::Manifest::Icon> icons; |
| 268 icons.push_back(CreateIcon("http://foo.com/icon_no_1.png", "", sizes_1)); | 344 icons.push_back(CreateIcon("http://foo.com/icon_no_1.png", "", sizes_1, |
| 269 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); | 345 IconPurpose::ANY)); |
| 270 icons.push_back(CreateIcon("http://foo.com/icon_no_2.png", "", sizes_3)); | 346 icons.push_back( |
| 347 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY)); |
| 348 icons.push_back(CreateIcon("http://foo.com/icon_no_2.png", "", sizes_3, |
| 349 IconPurpose::ANY)); |
| 271 | 350 |
| 272 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 351 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 352 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 273 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 353 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 274 } | 354 } |
| 275 | 355 |
| 276 // (very_big, big) => big | 356 // (very_big, big) => big |
| 277 { | 357 { |
| 278 std::vector<gfx::Size> sizes_1; | 358 std::vector<gfx::Size> sizes_1; |
| 279 sizes_1.push_back(gfx::Size(very_big, very_big)); | 359 sizes_1.push_back(gfx::Size(very_big, very_big)); |
| 280 | 360 |
| 281 std::vector<gfx::Size> sizes_2; | 361 std::vector<gfx::Size> sizes_2; |
| 282 sizes_2.push_back(gfx::Size(big, big)); | 362 sizes_2.push_back(gfx::Size(big, big)); |
| 283 | 363 |
| 284 std::vector<content::Manifest::Icon> icons; | 364 std::vector<content::Manifest::Icon> icons; |
| 285 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); | 365 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1, |
| 286 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); | 366 IconPurpose::ANY)); |
| 367 icons.push_back( |
| 368 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY)); |
| 287 | 369 |
| 288 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 370 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 371 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 289 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 372 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 290 } | 373 } |
| 291 | 374 |
| 292 // (very_big, big, bit_big) => bit_big | 375 // (very_big, big, bit_big) => bit_big |
| 293 { | 376 { |
| 294 std::vector<gfx::Size> sizes_1; | 377 std::vector<gfx::Size> sizes_1; |
| 295 sizes_1.push_back(gfx::Size(very_big, very_big)); | 378 sizes_1.push_back(gfx::Size(very_big, very_big)); |
| 296 | 379 |
| 297 std::vector<gfx::Size> sizes_2; | 380 std::vector<gfx::Size> sizes_2; |
| 298 sizes_2.push_back(gfx::Size(big, big)); | 381 sizes_2.push_back(gfx::Size(big, big)); |
| 299 | 382 |
| 300 std::vector<gfx::Size> sizes_3; | 383 std::vector<gfx::Size> sizes_3; |
| 301 sizes_3.push_back(gfx::Size(bit_big, bit_big)); | 384 sizes_3.push_back(gfx::Size(bit_big, bit_big)); |
| 302 | 385 |
| 303 std::vector<content::Manifest::Icon> icons; | 386 std::vector<content::Manifest::Icon> icons; |
| 304 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); | 387 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1, |
| 305 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2)); | 388 IconPurpose::ANY)); |
| 306 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_3)); | 389 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2, |
| 390 IconPurpose::ANY)); |
| 391 icons.push_back( |
| 392 CreateIcon("http://foo.com/icon.png", "", sizes_3, IconPurpose::ANY)); |
| 307 | 393 |
| 308 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 394 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 395 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 309 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 396 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 310 } | 397 } |
| 311 | 398 |
| 312 // (bit_small, very_big) => very_big | 399 // (bit_small, very_big) => very_big |
| 313 { | 400 { |
| 314 std::vector<gfx::Size> sizes_1; | 401 std::vector<gfx::Size> sizes_1; |
| 315 sizes_1.push_back(gfx::Size(bit_small, bit_small)); | 402 sizes_1.push_back(gfx::Size(bit_small, bit_small)); |
| 316 | 403 |
| 317 std::vector<gfx::Size> sizes_2; | 404 std::vector<gfx::Size> sizes_2; |
| 318 sizes_2.push_back(gfx::Size(very_big, very_big)); | 405 sizes_2.push_back(gfx::Size(very_big, very_big)); |
| 319 | 406 |
| 320 std::vector<content::Manifest::Icon> icons; | 407 std::vector<content::Manifest::Icon> icons; |
| 321 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); | 408 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1, |
| 322 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); | 409 IconPurpose::ANY)); |
| 410 icons.push_back( |
| 411 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY)); |
| 323 | 412 |
| 324 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 413 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 414 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 325 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 415 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 326 } | 416 } |
| 327 | 417 |
| 328 // (bit_small, bit_big) => bit_big | 418 // (bit_small, bit_big) => bit_big |
| 329 { | 419 { |
| 330 std::vector<gfx::Size> sizes_1; | 420 std::vector<gfx::Size> sizes_1; |
| 331 sizes_1.push_back(gfx::Size(bit_small, bit_small)); | 421 sizes_1.push_back(gfx::Size(bit_small, bit_small)); |
| 332 | 422 |
| 333 std::vector<gfx::Size> sizes_2; | 423 std::vector<gfx::Size> sizes_2; |
| 334 sizes_2.push_back(gfx::Size(bit_big, bit_big)); | 424 sizes_2.push_back(gfx::Size(bit_big, bit_big)); |
| 335 | 425 |
| 336 std::vector<content::Manifest::Icon> icons; | 426 std::vector<content::Manifest::Icon> icons; |
| 337 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); | 427 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1, |
| 338 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); | 428 IconPurpose::ANY)); |
| 429 icons.push_back( |
| 430 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY)); |
| 339 | 431 |
| 340 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 432 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 433 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 341 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 434 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 342 } | 435 } |
| 343 } | 436 } |
| 344 | 437 |
| 345 TEST(ManifestIconSelector, UseAnyIfNoPreferredSize) { | 438 TEST(ManifestIconSelector, UseAnyIfNoIdealSize) { |
| 346 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a | 439 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a |
| 347 // preferred size. | 440 // ideal size. |
| 348 | 441 |
| 349 // Icon with 'any' and icon with preferred size => preferred size is chosen. | 442 // Icon with 'any' and icon with ideal size => ideal size is chosen. |
| 350 { | 443 { |
| 351 std::vector<gfx::Size> sizes_1; | 444 std::vector<gfx::Size> sizes_1; |
| 352 sizes_1.push_back(gfx::Size(kDefaultIconSize, kDefaultIconSize)); | 445 sizes_1.push_back(gfx::Size(kIdealIconSize, kIdealIconSize)); |
| 353 std::vector<gfx::Size> sizes_2; | 446 std::vector<gfx::Size> sizes_2; |
| 354 sizes_2.push_back(gfx::Size(0, 0)); | 447 sizes_2.push_back(gfx::Size(0, 0)); |
| 355 | 448 |
| 356 std::vector<content::Manifest::Icon> icons; | 449 std::vector<content::Manifest::Icon> icons; |
| 357 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_1)); | 450 icons.push_back( |
| 358 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2)); | 451 CreateIcon("http://foo.com/icon.png", "", sizes_1, IconPurpose::ANY)); |
| 452 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2, |
| 453 IconPurpose::ANY)); |
| 359 | 454 |
| 360 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 455 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 456 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 361 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 457 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 362 } | 458 } |
| 363 | 459 |
| 364 // Icon with 'any' and icon larger than preferred size => any is chosen. | 460 // Icon with 'any' and icon larger than ideal size => any is chosen. |
| 365 { | 461 { |
| 366 std::vector<gfx::Size> sizes_1; | 462 std::vector<gfx::Size> sizes_1; |
| 367 sizes_1.push_back(gfx::Size(kDefaultIconSize + 1, kDefaultIconSize + 1)); | 463 sizes_1.push_back(gfx::Size(kIdealIconSize + 1, kIdealIconSize + 1)); |
| 368 std::vector<gfx::Size> sizes_2; | 464 std::vector<gfx::Size> sizes_2; |
| 369 sizes_2.push_back(gfx::Size(0, 0)); | 465 sizes_2.push_back(gfx::Size(0, 0)); |
| 370 | 466 |
| 371 std::vector<content::Manifest::Icon> icons; | 467 std::vector<content::Manifest::Icon> icons; |
| 372 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); | 468 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1, |
| 373 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); | 469 IconPurpose::ANY)); |
| 470 icons.push_back( |
| 471 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY)); |
| 374 | 472 |
| 375 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); | 473 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 474 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY); |
| 376 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 475 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 377 } | 476 } |
| 378 | 477 |
| 379 // Multiple icons with 'any' => the last one is chosen. | 478 // Multiple icons with 'any' => the last one is chosen. |
| 380 { | 479 { |
| 381 std::vector<gfx::Size> sizes; | 480 std::vector<gfx::Size> sizes; |
| 382 sizes.push_back(gfx::Size(0, 0)); | 481 sizes.push_back(gfx::Size(0, 0)); |
| 383 | 482 |
| 384 std::vector<content::Manifest::Icon> icons; | 483 std::vector<content::Manifest::Icon> icons; |
| 385 icons.push_back(CreateIcon("http://foo.com/icon_no1.png", "", sizes)); | 484 icons.push_back( |
| 386 icons.push_back(CreateIcon("http://foo.com/icon_no2.png", "", sizes)); | 485 CreateIcon("http://foo.com/icon_no1.png", "", sizes, IconPurpose::ANY)); |
| 387 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes)); | 486 icons.push_back( |
| 487 CreateIcon("http://foo.com/icon_no2.png", "", sizes, IconPurpose::ANY)); |
| 488 icons.push_back( |
| 489 CreateIcon("http://foo.com/icon.png", "", sizes, IconPurpose::ANY)); |
| 388 | 490 |
| 389 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize * 3); | 491 GURL url = ManifestIconSelector::FindBestMatchingIcon( |
| 492 icons, kIdealIconSize * 3, kMinimumIconSize, IconPurpose::ANY); |
| 390 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 493 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 391 } | 494 } |
| 392 } | 495 } |
| OLD | NEW |