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