| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 } | 27 } |
| 28 ~IconUtilTest() {} | 28 ~IconUtilTest() {} |
| 29 | 29 |
| 30 static const int kSmallIconWidth = 16; | 30 static const int kSmallIconWidth = 16; |
| 31 static const int kSmallIconHeight = 16; | 31 static const int kSmallIconHeight = 16; |
| 32 static const int kLargeIconWidth = 128; | 32 static const int kLargeIconWidth = 128; |
| 33 static const int kLargeIconHeight = 128; | 33 static const int kLargeIconHeight = 128; |
| 34 | 34 |
| 35 // Given a file name for an .ico file and an image dimentions, this | 35 // Given a file name for an .ico file and an image dimentions, this |
| 36 // function loads the icon and returns an HICON handle. | 36 // function loads the icon and returns an HICON handle. |
| 37 HICON LoadIconFromFile(const FilePath& filename, int width, int height) { | 37 HICON LoadIconFromFile(const base::FilePath& filename, |
| 38 int width, int height) { |
| 38 HICON icon = static_cast<HICON>(LoadImage(NULL, | 39 HICON icon = static_cast<HICON>(LoadImage(NULL, |
| 39 filename.value().c_str(), | 40 filename.value().c_str(), |
| 40 IMAGE_ICON, | 41 IMAGE_ICON, |
| 41 width, | 42 width, |
| 42 height, | 43 height, |
| 43 LR_LOADTRANSPARENT | LR_LOADFROMFILE)); | 44 LR_LOADTRANSPARENT | LR_LOADFROMFILE)); |
| 44 return icon; | 45 return icon; |
| 45 } | 46 } |
| 46 | 47 |
| 47 SkBitmap CreateBlackSkBitmap(int width, int height) { | 48 SkBitmap CreateBlackSkBitmap(int width, int height) { |
| 48 SkBitmap bitmap; | 49 SkBitmap bitmap; |
| 49 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 50 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 50 bitmap.allocPixels(); | 51 bitmap.allocPixels(); |
| 51 // Setting the pixels to black. | 52 // Setting the pixels to black. |
| 52 memset(bitmap.getPixels(), 0, width * height * 4); | 53 memset(bitmap.getPixels(), 0, width * height * 4); |
| 53 return bitmap; | 54 return bitmap; |
| 54 } | 55 } |
| 55 | 56 |
| 56 protected: | 57 protected: |
| 57 // The root directory for test files. | 58 // The root directory for test files. |
| 58 FilePath test_data_directory_; | 59 base::FilePath test_data_directory_; |
| 59 | 60 |
| 60 // Directory for creating files by this test. | 61 // Directory for creating files by this test. |
| 61 base::ScopedTempDir temp_directory_; | 62 base::ScopedTempDir temp_directory_; |
| 62 | 63 |
| 63 private: | 64 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(IconUtilTest); | 65 DISALLOW_COPY_AND_ASSIGN(IconUtilTest); |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 } // namespace | 68 } // namespace |
| 68 | 69 |
| 69 // The following test case makes sure IconUtil::SkBitmapFromHICON fails | 70 // The following test case makes sure IconUtil::SkBitmapFromHICON fails |
| 70 // gracefully when called with invalid input parameters. | 71 // gracefully when called with invalid input parameters. |
| 71 TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) { | 72 TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) { |
| 72 FilePath icon_filename = test_data_directory_.AppendASCII(kSmallIconName); | 73 base::FilePath icon_filename = |
| 74 test_data_directory_.AppendASCII(kSmallIconName); |
| 73 gfx::Size icon_size(kSmallIconWidth, kSmallIconHeight); | 75 gfx::Size icon_size(kSmallIconWidth, kSmallIconHeight); |
| 74 HICON icon = LoadIconFromFile(icon_filename, | 76 HICON icon = LoadIconFromFile(icon_filename, |
| 75 icon_size.width(), | 77 icon_size.width(), |
| 76 icon_size.height()); | 78 icon_size.height()); |
| 77 ASSERT_TRUE(icon != NULL); | 79 ASSERT_TRUE(icon != NULL); |
| 78 | 80 |
| 79 // Invalid size parameter. | 81 // Invalid size parameter. |
| 80 gfx::Size invalid_icon_size(kSmallIconHeight, 0); | 82 gfx::Size invalid_icon_size(kSmallIconHeight, 0); |
| 81 EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(icon, invalid_icon_size), | 83 EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(icon, invalid_icon_size), |
| 82 static_cast<SkBitmap*>(NULL)); | 84 static_cast<SkBitmap*>(NULL)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 kSmallIconWidth, | 121 kSmallIconWidth, |
| 120 kSmallIconHeight); | 122 kSmallIconHeight); |
| 121 icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); | 123 icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); |
| 122 EXPECT_TRUE(icon == NULL); | 124 EXPECT_TRUE(icon == NULL); |
| 123 } | 125 } |
| 124 | 126 |
| 125 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap | 127 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap |
| 126 // fails gracefully when called with invalid input parameters. | 128 // fails gracefully when called with invalid input parameters. |
| 127 TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) { | 129 TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) { |
| 128 scoped_ptr<SkBitmap> bitmap; | 130 scoped_ptr<SkBitmap> bitmap; |
| 129 FilePath valid_icon_filename = test_data_directory_.AppendASCII( | 131 base::FilePath valid_icon_filename = test_data_directory_.AppendASCII( |
| 130 kSmallIconName); | 132 kSmallIconName); |
| 131 FilePath invalid_icon_filename(FILE_PATH_LITERAL("C:\\<>?.ico")); | 133 base::FilePath invalid_icon_filename(FILE_PATH_LITERAL("C:\\<>?.ico")); |
| 132 | 134 |
| 133 // Wrong bitmap format. | 135 // Wrong bitmap format. |
| 134 bitmap.reset(new SkBitmap); | 136 bitmap.reset(new SkBitmap); |
| 135 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | 137 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
| 136 bitmap->setConfig(SkBitmap::kA8_Config, kSmallIconWidth, kSmallIconHeight); | 138 bitmap->setConfig(SkBitmap::kA8_Config, kSmallIconWidth, kSmallIconHeight); |
| 137 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(), | 139 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(), |
| 138 valid_icon_filename)); | 140 valid_icon_filename)); |
| 139 | 141 |
| 140 // Invalid bitmap size. | 142 // Invalid bitmap size. |
| 141 bitmap.reset(new SkBitmap); | 143 bitmap.reset(new SkBitmap); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 158 // Setting the pixels to black. | 160 // Setting the pixels to black. |
| 159 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4); | 161 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4); |
| 160 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(), | 162 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(), |
| 161 invalid_icon_filename)); | 163 invalid_icon_filename)); |
| 162 } | 164 } |
| 163 | 165 |
| 164 // This test case makes sure that when we load an icon from disk and convert | 166 // This test case makes sure that when we load an icon from disk and convert |
| 165 // the HICON into a bitmap, the bitmap has the expected format and dimentions. | 167 // the HICON into a bitmap, the bitmap has the expected format and dimentions. |
| 166 TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) { | 168 TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) { |
| 167 scoped_ptr<SkBitmap> bitmap; | 169 scoped_ptr<SkBitmap> bitmap; |
| 168 FilePath small_icon_filename = test_data_directory_.AppendASCII( | 170 base::FilePath small_icon_filename = test_data_directory_.AppendASCII( |
| 169 kSmallIconName); | 171 kSmallIconName); |
| 170 gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight); | 172 gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight); |
| 171 HICON small_icon = LoadIconFromFile(small_icon_filename, | 173 HICON small_icon = LoadIconFromFile(small_icon_filename, |
| 172 small_icon_size.width(), | 174 small_icon_size.width(), |
| 173 small_icon_size.height()); | 175 small_icon_size.height()); |
| 174 ASSERT_NE(small_icon, static_cast<HICON>(NULL)); | 176 ASSERT_NE(small_icon, static_cast<HICON>(NULL)); |
| 175 bitmap.reset(IconUtil::CreateSkBitmapFromHICON(small_icon, small_icon_size)); | 177 bitmap.reset(IconUtil::CreateSkBitmapFromHICON(small_icon, small_icon_size)); |
| 176 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | 178 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
| 177 EXPECT_EQ(bitmap->width(), small_icon_size.width()); | 179 EXPECT_EQ(bitmap->width(), small_icon_size.width()); |
| 178 EXPECT_EQ(bitmap->height(), small_icon_size.height()); | 180 EXPECT_EQ(bitmap->height(), small_icon_size.height()); |
| 179 EXPECT_EQ(bitmap->config(), SkBitmap::kARGB_8888_Config); | 181 EXPECT_EQ(bitmap->config(), SkBitmap::kARGB_8888_Config); |
| 180 ::DestroyIcon(small_icon); | 182 ::DestroyIcon(small_icon); |
| 181 | 183 |
| 182 FilePath large_icon_filename = test_data_directory_.AppendASCII( | 184 base::FilePath large_icon_filename = test_data_directory_.AppendASCII( |
| 183 kLargeIconName); | 185 kLargeIconName); |
| 184 gfx::Size large_icon_size(kLargeIconWidth, kLargeIconHeight); | 186 gfx::Size large_icon_size(kLargeIconWidth, kLargeIconHeight); |
| 185 HICON large_icon = LoadIconFromFile(large_icon_filename, | 187 HICON large_icon = LoadIconFromFile(large_icon_filename, |
| 186 large_icon_size.width(), | 188 large_icon_size.width(), |
| 187 large_icon_size.height()); | 189 large_icon_size.height()); |
| 188 ASSERT_NE(large_icon, static_cast<HICON>(NULL)); | 190 ASSERT_NE(large_icon, static_cast<HICON>(NULL)); |
| 189 bitmap.reset(IconUtil::CreateSkBitmapFromHICON(large_icon, large_icon_size)); | 191 bitmap.reset(IconUtil::CreateSkBitmapFromHICON(large_icon, large_icon_size)); |
| 190 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | 192 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); |
| 191 EXPECT_EQ(bitmap->width(), large_icon_size.width()); | 193 EXPECT_EQ(bitmap->width(), large_icon_size.width()); |
| 192 EXPECT_EQ(bitmap->height(), large_icon_size.height()); | 194 EXPECT_EQ(bitmap->height(), large_icon_size.height()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 EXPECT_EQ(bitmap_info.bmiHeader.biHeight, kSmallIconHeight); | 230 EXPECT_EQ(bitmap_info.bmiHeader.biHeight, kSmallIconHeight); |
| 229 EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1); | 231 EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1); |
| 230 EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32); | 232 EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32); |
| 231 ::ReleaseDC(NULL, hdc); | 233 ::ReleaseDC(NULL, hdc); |
| 232 ::DestroyIcon(icon); | 234 ::DestroyIcon(icon); |
| 233 } | 235 } |
| 234 | 236 |
| 235 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap | 237 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap |
| 236 // creates a valid .ico file given an SkBitmap. | 238 // creates a valid .ico file given an SkBitmap. |
| 237 TEST_F(IconUtilTest, TestCreateIconFile) { | 239 TEST_F(IconUtilTest, TestCreateIconFile) { |
| 238 FilePath icon_filename = test_data_directory_.AppendASCII(kTempIconFilename); | 240 base::FilePath icon_filename = |
| 241 test_data_directory_.AppendASCII(kTempIconFilename); |
| 239 | 242 |
| 240 SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight); | 243 SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight); |
| 241 EXPECT_TRUE(IconUtil::CreateIconFileFromSkBitmap(bitmap, SkBitmap(), | 244 EXPECT_TRUE(IconUtil::CreateIconFileFromSkBitmap(bitmap, SkBitmap(), |
| 242 icon_filename)); | 245 icon_filename)); |
| 243 | 246 |
| 244 // We are currently only testing that it is possible to load an icon from | 247 // We are currently only testing that it is possible to load an icon from |
| 245 // the .ico file we just created. We don't really check the additional icon | 248 // the .ico file we just created. We don't really check the additional icon |
| 246 // images created by IconUtil::CreateIconFileFromSkBitmap. | 249 // images created by IconUtil::CreateIconFileFromSkBitmap. |
| 247 HICON icon = LoadIconFromFile(icon_filename, | 250 HICON icon = LoadIconFromFile(icon_filename, |
| 248 kSmallIconWidth, | 251 kSmallIconWidth, |
| 249 kSmallIconHeight); | 252 kSmallIconHeight); |
| 250 EXPECT_NE(icon, static_cast<HICON>(NULL)); | 253 EXPECT_NE(icon, static_cast<HICON>(NULL)); |
| 251 if (icon != NULL) { | 254 if (icon != NULL) { |
| 252 ::DestroyIcon(icon); | 255 ::DestroyIcon(icon); |
| 253 } | 256 } |
| 254 } | 257 } |
| 255 | 258 |
| 256 TEST_F(IconUtilTest, TestCreateIconFileWithLargeBitmap) { | 259 TEST_F(IconUtilTest, TestCreateIconFileWithLargeBitmap) { |
| 257 const FilePath icon_path(temp_directory_.path().AppendASCII("test.ico")); | 260 const base::FilePath icon_path( |
| 261 temp_directory_.path().AppendASCII("test.ico")); |
| 258 const SkBitmap bitmap_48 = CreateBlackSkBitmap(48, 48); | 262 const SkBitmap bitmap_48 = CreateBlackSkBitmap(48, 48); |
| 259 const SkBitmap bitmap_256 = CreateBlackSkBitmap(256, 256); | 263 const SkBitmap bitmap_256 = CreateBlackSkBitmap(256, 256); |
| 260 | 264 |
| 261 // First, create the icon file. | 265 // First, create the icon file. |
| 262 ASSERT_TRUE(IconUtil::CreateIconFileFromSkBitmap(bitmap_48, bitmap_256, | 266 ASSERT_TRUE(IconUtil::CreateIconFileFromSkBitmap(bitmap_48, bitmap_256, |
| 263 icon_path)); | 267 icon_path)); |
| 264 ASSERT_TRUE(file_util::PathExists(icon_path)); | 268 ASSERT_TRUE(file_util::PathExists(icon_path)); |
| 265 | 269 |
| 266 // Then, read the file and ensure it has a valid 256x256 PNG icon entry. | 270 // Then, read the file and ensure it has a valid 256x256 PNG icon entry. |
| 267 std::string icon_data; | 271 std::string icon_data; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 } | 309 } |
| 306 | 310 |
| 307 TEST_F(IconUtilTest, TestCreateSkBitmapFromIconResource256x256) { | 311 TEST_F(IconUtilTest, TestCreateSkBitmapFromIconResource256x256) { |
| 308 HMODULE module = GetModuleHandle(NULL); | 312 HMODULE module = GetModuleHandle(NULL); |
| 309 scoped_ptr<SkBitmap> bitmap( | 313 scoped_ptr<SkBitmap> bitmap( |
| 310 IconUtil::CreateSkBitmapFromIconResource(module, IDR_MAINFRAME, 256)); | 314 IconUtil::CreateSkBitmapFromIconResource(module, IDR_MAINFRAME, 256)); |
| 311 ASSERT_TRUE(bitmap.get()); | 315 ASSERT_TRUE(bitmap.get()); |
| 312 EXPECT_EQ(256, bitmap->width()); | 316 EXPECT_EQ(256, bitmap->width()); |
| 313 EXPECT_EQ(256, bitmap->height()); | 317 EXPECT_EQ(256, bitmap->height()); |
| 314 } | 318 } |
| OLD | NEW |