[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 9974ce1 | 2014-01-30 07:14:13 | [diff] [blame] | 5 | #include "ui/gfx/icon_util.h" |
| 6 | |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/path_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 12 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 13 | #include "ui/gfx/gfx_paths.h" |
[email protected] | 9974ce1 | 2014-01-30 07:14:13 | [diff] [blame] | 14 | #include "ui/gfx/icon_util_unittests_resource.h" |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 15 | #include "ui/gfx/image/image.h" |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 16 | #include "ui/gfx/image/image_family.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 17 | #include "ui/gfx/size.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 21 | static const char kSmallIconName[] = "icon_util/16_X_16_icon.ico"; |
| 22 | static const char kLargeIconName[] = "icon_util/128_X_128_icon.ico"; |
| 23 | static const char kTempIconFilename[] = "temp_test_icon.ico"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 25 | } // namespace |
| 26 | |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 27 | class IconUtilTest : public testing::Test { |
| 28 | public: |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 29 | virtual void SetUp() OVERRIDE { |
[email protected] | 0165aa2 | 2014-08-17 04:41:59 | [diff] [blame] | 30 | gfx::RegisterPathProvider(); |
| 31 | ASSERT_TRUE(PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)); |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 32 | temp_directory_.CreateUniqueTempDir(); |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 33 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 35 | static const int kSmallIconWidth = 16; |
| 36 | static const int kSmallIconHeight = 16; |
| 37 | static const int kLargeIconWidth = 128; |
| 38 | static const int kLargeIconHeight = 128; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 40 | // Given a file name for an .ico file and an image dimensions, this |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 41 | // function loads the icon and returns an HICON handle. |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 42 | HICON LoadIconFromFile(const base::FilePath& filename, |
| 43 | int width, int height) { |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 44 | HICON icon = static_cast<HICON>(LoadImage(NULL, |
| 45 | filename.value().c_str(), |
| 46 | IMAGE_ICON, |
| 47 | width, |
| 48 | height, |
| 49 | LR_LOADTRANSPARENT | LR_LOADFROMFILE)); |
| 50 | return icon; |
| 51 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 53 | SkBitmap CreateBlackSkBitmap(int width, int height) { |
| 54 | SkBitmap bitmap; |
[email protected] | f62e57b | 2014-03-12 20:52:02 | [diff] [blame] | 55 | bitmap.allocN32Pixels(width, height); |
| 56 | // Setting the pixels to transparent-black. |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 57 | memset(bitmap.getPixels(), 0, width * height * 4); |
| 58 | return bitmap; |
| 59 | } |
| 60 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 61 | // Loads an .ico file from |icon_filename| and asserts that it contains all of |
| 62 | // the expected icon sizes up to and including |max_icon_size|, and no other |
| 63 | // icons. If |max_icon_size| >= 256, this tests for a 256x256 PNG icon entry. |
| 64 | void CheckAllIconSizes(const base::FilePath& icon_filename, |
| 65 | int max_icon_size); |
| 66 | |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 67 | protected: |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 68 | // The root directory for test files. This should be treated as read-only. |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 69 | base::FilePath test_data_directory_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 71 | // Directory for creating files by this test. |
| 72 | base::ScopedTempDir temp_directory_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | }; |
| 74 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 75 | void IconUtilTest::CheckAllIconSizes(const base::FilePath& icon_filename, |
| 76 | int max_icon_size) { |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 77 | ASSERT_TRUE(base::PathExists(icon_filename)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 78 | |
| 79 | // Determine how many icons to expect, based on |max_icon_size|. |
| 80 | int expected_num_icons = 0; |
| 81 | for (size_t i = 0; i < IconUtil::kNumIconDimensions; ++i) { |
| 82 | if (IconUtil::kIconDimensions[i] > max_icon_size) |
| 83 | break; |
| 84 | ++expected_num_icons; |
| 85 | } |
| 86 | |
| 87 | // First, use the Windows API to load the icon, a basic validity test. |
| 88 | HICON icon = LoadIconFromFile(icon_filename, kSmallIconWidth, |
| 89 | kSmallIconHeight); |
| 90 | EXPECT_NE(static_cast<HICON>(NULL), icon); |
| 91 | if (icon != NULL) |
| 92 | ::DestroyIcon(icon); |
| 93 | |
| 94 | // Read the file completely into memory. |
| 95 | std::string icon_data; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 96 | ASSERT_TRUE(base::ReadFileToString(icon_filename, &icon_data)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 97 | ASSERT_GE(icon_data.length(), sizeof(IconUtil::ICONDIR)); |
| 98 | |
| 99 | // Ensure that it has exactly the expected number and sizes of icons, in the |
| 100 | // expected order. This matches each entry of the loaded file's icon directory |
| 101 | // with the corresponding element of kIconDimensions. |
| 102 | // Also extracts the 256x256 entry as png_entry. |
| 103 | const IconUtil::ICONDIR* icon_dir = |
| 104 | reinterpret_cast<const IconUtil::ICONDIR*>(icon_data.data()); |
| 105 | EXPECT_EQ(expected_num_icons, icon_dir->idCount); |
| 106 | ASSERT_GE(IconUtil::kNumIconDimensions, icon_dir->idCount); |
| 107 | ASSERT_GE(icon_data.length(), |
| 108 | sizeof(IconUtil::ICONDIR) + |
| 109 | icon_dir->idCount * sizeof(IconUtil::ICONDIRENTRY)); |
| 110 | const IconUtil::ICONDIRENTRY* png_entry = NULL; |
| 111 | for (size_t i = 0; i < icon_dir->idCount; ++i) { |
| 112 | const IconUtil::ICONDIRENTRY* entry = &icon_dir->idEntries[i]; |
| 113 | // Mod 256 because as a special case in ICONDIRENTRY, the value 0 represents |
| 114 | // a width or height of 256. |
| 115 | int expected_size = IconUtil::kIconDimensions[i] % 256; |
| 116 | EXPECT_EQ(expected_size, static_cast<int>(entry->bWidth)); |
| 117 | EXPECT_EQ(expected_size, static_cast<int>(entry->bHeight)); |
| 118 | if (entry->bWidth == 0 && entry->bHeight == 0) { |
| 119 | EXPECT_EQ(NULL, png_entry); |
| 120 | png_entry = entry; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (max_icon_size >= 256) { |
| 125 | ASSERT_TRUE(png_entry); |
| 126 | |
| 127 | // Convert the PNG entry data back to a SkBitmap to ensure it's valid. |
| 128 | ASSERT_GE(icon_data.length(), |
| 129 | png_entry->dwImageOffset + png_entry->dwBytesInRes); |
| 130 | const unsigned char* png_bytes = reinterpret_cast<const unsigned char*>( |
| 131 | icon_data.data() + png_entry->dwImageOffset); |
| 132 | gfx::Image image = gfx::Image::CreateFrom1xPNGBytes( |
| 133 | png_bytes, png_entry->dwBytesInRes); |
| 134 | SkBitmap bitmap = image.AsBitmap(); |
| 135 | EXPECT_EQ(256, bitmap.width()); |
| 136 | EXPECT_EQ(256, bitmap.height()); |
| 137 | } |
| 138 | } |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 139 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | // The following test case makes sure IconUtil::SkBitmapFromHICON fails |
| 141 | // gracefully when called with invalid input parameters. |
| 142 | TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) { |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 143 | base::FilePath icon_filename = |
| 144 | test_data_directory_.AppendASCII(kSmallIconName); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | gfx::Size icon_size(kSmallIconWidth, kSmallIconHeight); |
| 146 | HICON icon = LoadIconFromFile(icon_filename, |
| 147 | icon_size.width(), |
| 148 | icon_size.height()); |
| 149 | ASSERT_TRUE(icon != NULL); |
| 150 | |
| 151 | // Invalid size parameter. |
| 152 | gfx::Size invalid_icon_size(kSmallIconHeight, 0); |
| 153 | EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(icon, invalid_icon_size), |
| 154 | static_cast<SkBitmap*>(NULL)); |
| 155 | |
| 156 | // Invalid icon. |
| 157 | EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(NULL, icon_size), |
| 158 | static_cast<SkBitmap*>(NULL)); |
| 159 | |
| 160 | // The following code should succeed. |
| 161 | scoped_ptr<SkBitmap> bitmap; |
| 162 | bitmap.reset(IconUtil::CreateSkBitmapFromHICON(icon, icon_size)); |
| 163 | EXPECT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
| 164 | ::DestroyIcon(icon); |
| 165 | } |
| 166 | |
| 167 | // The following test case makes sure IconUtil::CreateHICONFromSkBitmap fails |
| 168 | // gracefully when called with invalid input parameters. |
| 169 | TEST_F(IconUtilTest, TestBitmapToIconInvalidParameters) { |
| 170 | HICON icon = NULL; |
| 171 | scoped_ptr<SkBitmap> bitmap; |
| 172 | |
| 173 | // Wrong bitmap format. |
| 174 | bitmap.reset(new SkBitmap); |
| 175 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
[email protected] | 7d3c069e | 2014-06-09 17:11:15 | [diff] [blame] | 176 | bitmap->setInfo(SkImageInfo::MakeA8(kSmallIconWidth, kSmallIconHeight)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); |
| 178 | EXPECT_EQ(icon, static_cast<HICON>(NULL)); |
| 179 | |
| 180 | // Invalid bitmap size. |
| 181 | bitmap.reset(new SkBitmap); |
| 182 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
[email protected] | 7d3c069e | 2014-06-09 17:11:15 | [diff] [blame] | 183 | bitmap->setInfo(SkImageInfo::MakeN32Premul(0, 0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); |
| 185 | EXPECT_EQ(icon, static_cast<HICON>(NULL)); |
| 186 | |
| 187 | // Valid bitmap configuration but no pixels allocated. |
| 188 | bitmap.reset(new SkBitmap); |
| 189 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
[email protected] | 7d3c069e | 2014-06-09 17:11:15 | [diff] [blame] | 190 | bitmap->setInfo(SkImageInfo::MakeN32Premul(kSmallIconWidth, |
| 191 | kSmallIconHeight)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); |
| 193 | EXPECT_TRUE(icon == NULL); |
| 194 | } |
| 195 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 196 | // The following test case makes sure IconUtil::CreateIconFileFromImageFamily |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | // fails gracefully when called with invalid input parameters. |
| 198 | TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) { |
| 199 | scoped_ptr<SkBitmap> bitmap; |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 200 | gfx::ImageFamily image_family; |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 201 | base::FilePath valid_icon_filename = temp_directory_.path().AppendASCII( |
| 202 | kTempIconFilename); |
| 203 | base::FilePath invalid_icon_filename = temp_directory_.path().AppendASCII( |
| 204 | "<>?.ico"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
| 206 | // Wrong bitmap format. |
| 207 | bitmap.reset(new SkBitmap); |
| 208 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 209 | // Must allocate pixels or else ImageSkia will ignore the bitmap and just |
| 210 | // return an empty image. |
[email protected] | 7d3c069e | 2014-06-09 17:11:15 | [diff] [blame] | 211 | bitmap->allocPixels(SkImageInfo::MakeA8(kSmallIconWidth, kSmallIconHeight)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 212 | memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height()); |
| 213 | image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap)); |
| 214 | EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 215 | valid_icon_filename)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 216 | EXPECT_FALSE(base::PathExists(valid_icon_filename)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | |
| 218 | // Invalid bitmap size. |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 219 | image_family.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | bitmap.reset(new SkBitmap); |
| 221 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
[email protected] | 7d3c069e | 2014-06-09 17:11:15 | [diff] [blame] | 222 | bitmap->allocPixels(SkImageInfo::MakeN32Premul(0, 0)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 223 | image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap)); |
| 224 | EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 225 | valid_icon_filename)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 226 | EXPECT_FALSE(base::PathExists(valid_icon_filename)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | |
| 228 | // Bitmap with no allocated pixels. |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 229 | image_family.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | bitmap.reset(new SkBitmap); |
| 231 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
[email protected] | 7d3c069e | 2014-06-09 17:11:15 | [diff] [blame] | 232 | bitmap->setInfo(SkImageInfo::MakeN32Premul(kSmallIconWidth, |
| 233 | kSmallIconHeight)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 234 | image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap)); |
| 235 | EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 236 | valid_icon_filename)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 237 | EXPECT_FALSE(base::PathExists(valid_icon_filename)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | |
| 239 | // Invalid file name. |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 240 | image_family.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | bitmap->allocPixels(); |
[email protected] | 0006be64 | 2008-08-27 16:43:19 | [diff] [blame] | 242 | // Setting the pixels to black. |
| 243 | memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 244 | image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap)); |
| 245 | EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 246 | invalid_icon_filename)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 247 | EXPECT_FALSE(base::PathExists(invalid_icon_filename)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 250 | // This test case makes sure IconUtil::CreateIconFileFromImageFamily fails if |
| 251 | // the image family is empty or invalid. |
| 252 | TEST_F(IconUtilTest, TestCreateIconFileEmptyImageFamily) { |
| 253 | base::FilePath icon_filename = temp_directory_.path().AppendASCII( |
| 254 | kTempIconFilename); |
| 255 | |
| 256 | // Empty image family. |
| 257 | EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(gfx::ImageFamily(), |
| 258 | icon_filename)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 259 | EXPECT_FALSE(base::PathExists(icon_filename)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 260 | |
| 261 | // Image family with only an empty image. |
| 262 | gfx::ImageFamily image_family; |
| 263 | image_family.Add(gfx::Image()); |
| 264 | EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 265 | icon_filename)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 266 | EXPECT_FALSE(base::PathExists(icon_filename)); |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 267 | } |
| 268 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | // This test case makes sure that when we load an icon from disk and convert |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 270 | // the HICON into a bitmap, the bitmap has the expected format and dimensions. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 271 | TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) { |
| 272 | scoped_ptr<SkBitmap> bitmap; |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 273 | base::FilePath small_icon_filename = test_data_directory_.AppendASCII( |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 274 | kSmallIconName); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight); |
| 276 | HICON small_icon = LoadIconFromFile(small_icon_filename, |
| 277 | small_icon_size.width(), |
| 278 | small_icon_size.height()); |
| 279 | ASSERT_NE(small_icon, static_cast<HICON>(NULL)); |
| 280 | bitmap.reset(IconUtil::CreateSkBitmapFromHICON(small_icon, small_icon_size)); |
| 281 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
| 282 | EXPECT_EQ(bitmap->width(), small_icon_size.width()); |
| 283 | EXPECT_EQ(bitmap->height(), small_icon_size.height()); |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 284 | EXPECT_EQ(bitmap->colorType(), kN32_SkColorType); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | ::DestroyIcon(small_icon); |
| 286 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 287 | base::FilePath large_icon_filename = test_data_directory_.AppendASCII( |
[email protected] | 3865810 | 2010-07-30 14:01:59 | [diff] [blame] | 288 | kLargeIconName); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | gfx::Size large_icon_size(kLargeIconWidth, kLargeIconHeight); |
| 290 | HICON large_icon = LoadIconFromFile(large_icon_filename, |
| 291 | large_icon_size.width(), |
| 292 | large_icon_size.height()); |
| 293 | ASSERT_NE(large_icon, static_cast<HICON>(NULL)); |
| 294 | bitmap.reset(IconUtil::CreateSkBitmapFromHICON(large_icon, large_icon_size)); |
| 295 | ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
| 296 | EXPECT_EQ(bitmap->width(), large_icon_size.width()); |
| 297 | EXPECT_EQ(bitmap->height(), large_icon_size.height()); |
[email protected] | c80330da | 2014-07-01 19:26:27 | [diff] [blame] | 298 | EXPECT_EQ(bitmap->colorType(), kN32_SkColorType); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | ::DestroyIcon(large_icon); |
| 300 | } |
| 301 | |
| 302 | // This test case makes sure that when an HICON is created from an SkBitmap, |
| 303 | // the returned handle is valid and refers to an icon with the expected |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 304 | // dimensions color depth etc. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 305 | TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) { |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 306 | SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight); |
| 307 | HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | EXPECT_NE(icon, static_cast<HICON>(NULL)); |
| 309 | ICONINFO icon_info; |
| 310 | ASSERT_TRUE(::GetIconInfo(icon, &icon_info)); |
| 311 | EXPECT_TRUE(icon_info.fIcon); |
| 312 | |
| 313 | // Now that have the icon information, we should obtain the specification of |
| 314 | // the icon's bitmap and make sure it matches the specification of the |
| 315 | // SkBitmap we started with. |
| 316 | // |
| 317 | // The bitmap handle contained in the icon information is a handle to a |
| 318 | // compatible bitmap so we need to call ::GetDIBits() in order to retrieve |
| 319 | // the bitmap's header information. |
| 320 | BITMAPINFO bitmap_info; |
| 321 | ::ZeroMemory(&bitmap_info, sizeof(BITMAPINFO)); |
| 322 | bitmap_info.bmiHeader.biSize = sizeof(BITMAPINFO); |
| 323 | HDC hdc = ::GetDC(NULL); |
| 324 | int result = ::GetDIBits(hdc, |
| 325 | icon_info.hbmColor, |
| 326 | 0, |
| 327 | kSmallIconWidth, |
| 328 | NULL, |
| 329 | &bitmap_info, |
| 330 | DIB_RGB_COLORS); |
| 331 | ASSERT_GT(result, 0); |
| 332 | EXPECT_EQ(bitmap_info.bmiHeader.biWidth, kSmallIconWidth); |
| 333 | EXPECT_EQ(bitmap_info.bmiHeader.biHeight, kSmallIconHeight); |
| 334 | EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1); |
| 335 | EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32); |
| 336 | ::ReleaseDC(NULL, hdc); |
| 337 | ::DestroyIcon(icon); |
| 338 | } |
| 339 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 340 | // This test case makes sure that CreateIconFileFromImageFamily creates a |
| 341 | // valid .ico file given an ImageFamily, and appropriately creates all icon |
| 342 | // sizes from the given input. |
| 343 | TEST_F(IconUtilTest, TestCreateIconFileFromImageFamily) { |
| 344 | gfx::ImageFamily image_family; |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 345 | base::FilePath icon_filename = |
[email protected] | 806a091d | 2013-04-18 06:30:36 | [diff] [blame] | 346 | temp_directory_.path().AppendASCII(kTempIconFilename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 347 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 348 | // Test with only a 16x16 icon. Should only scale up to 48x48. |
| 349 | image_family.Add(gfx::Image::CreateFrom1xBitmap( |
| 350 | CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight))); |
| 351 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 352 | icon_filename)); |
| 353 | CheckAllIconSizes(icon_filename, 48); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 355 | // Test with a 48x48 icon. Should only scale down. |
| 356 | image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(48, 48))); |
| 357 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 358 | icon_filename)); |
| 359 | CheckAllIconSizes(icon_filename, 48); |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 360 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 361 | // Test with a 64x64 icon. Should scale up to 256x256. |
| 362 | image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(64, 64))); |
| 363 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 364 | icon_filename)); |
| 365 | CheckAllIconSizes(icon_filename, 256); |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 366 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 367 | // Test with a 256x256 icon. Should include the 256x256 in the output. |
| 368 | image_family.Add(gfx::Image::CreateFrom1xBitmap( |
| 369 | CreateBlackSkBitmap(256, 256))); |
| 370 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 371 | icon_filename)); |
| 372 | CheckAllIconSizes(icon_filename, 256); |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 373 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 374 | // Test with a 49x49 icon. Should scale up to 256x256, but exclude the |
| 375 | // original 49x49 representation from the output. |
| 376 | image_family.clear(); |
| 377 | image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(49, 49))); |
| 378 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 379 | icon_filename)); |
| 380 | CheckAllIconSizes(icon_filename, 256); |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 381 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 382 | // Test with a non-square 16x32 icon. Should scale up to 48, but exclude the |
| 383 | // original 16x32 representation from the output. |
| 384 | image_family.clear(); |
| 385 | image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(16, 32))); |
| 386 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 387 | icon_filename)); |
| 388 | CheckAllIconSizes(icon_filename, 48); |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 389 | |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 390 | // Test with a non-square 32x49 icon. Should scale up to 256, but exclude the |
| 391 | // original 32x49 representation from the output. |
| 392 | image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(32, 49))); |
| 393 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 394 | icon_filename)); |
| 395 | CheckAllIconSizes(icon_filename, 256); |
| 396 | |
| 397 | // Test with an empty and non-empty image. |
| 398 | // The empty image should be ignored. |
| 399 | image_family.clear(); |
| 400 | image_family.Add(gfx::Image()); |
| 401 | image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(16, 16))); |
| 402 | ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family, |
| 403 | icon_filename)); |
| 404 | CheckAllIconSizes(icon_filename, 48); |
[email protected] | 6ed46c8 | 2013-01-08 19:02:59 | [diff] [blame] | 405 | } |
[email protected] | be74fb5 | 2013-01-14 19:45:34 | [diff] [blame] | 406 | |
| 407 | TEST_F(IconUtilTest, TestCreateSkBitmapFromIconResource48x48) { |
| 408 | HMODULE module = GetModuleHandle(NULL); |
| 409 | scoped_ptr<SkBitmap> bitmap( |
| 410 | IconUtil::CreateSkBitmapFromIconResource(module, IDR_MAINFRAME, 48)); |
| 411 | ASSERT_TRUE(bitmap.get()); |
| 412 | EXPECT_EQ(48, bitmap->width()); |
| 413 | EXPECT_EQ(48, bitmap->height()); |
| 414 | } |
| 415 | |
| 416 | TEST_F(IconUtilTest, TestCreateSkBitmapFromIconResource256x256) { |
| 417 | HMODULE module = GetModuleHandle(NULL); |
| 418 | scoped_ptr<SkBitmap> bitmap( |
| 419 | IconUtil::CreateSkBitmapFromIconResource(module, IDR_MAINFRAME, 256)); |
| 420 | ASSERT_TRUE(bitmap.get()); |
| 421 | EXPECT_EQ(256, bitmap->width()); |
| 422 | EXPECT_EQ(256, bitmap->height()); |
| 423 | } |
[email protected] | 7842b4f | 2013-04-30 04:43:49 | [diff] [blame] | 424 | |
| 425 | // This tests that kNumIconDimensionsUpToMediumSize has the correct value. |
| 426 | TEST_F(IconUtilTest, TestNumIconDimensionsUpToMediumSize) { |
| 427 | ASSERT_LE(IconUtil::kNumIconDimensionsUpToMediumSize, |
| 428 | IconUtil::kNumIconDimensions); |
| 429 | EXPECT_EQ(IconUtil::kMediumIconSize, |
| 430 | IconUtil::kIconDimensions[ |
| 431 | IconUtil::kNumIconDimensionsUpToMediumSize - 1]); |
| 432 | } |