Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: ui/gfx/icon_util_unittest.cc

Issue 11742007: Add support for adding 256x256 pngs to Windows .ico files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« ui/gfx/icon_util.cc ('K') | « ui/gfx/icon_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
7 #include "base/path_service.h" 8 #include "base/path_service.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/skia/include/core/SkBitmap.h" 10 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "ui/gfx/gfx_paths.h" 11 #include "ui/gfx/gfx_paths.h"
11 #include "ui/gfx/icon_util.h" 12 #include "ui/gfx/icon_util.h"
13 #include "ui/gfx/image/image.h"
12 #include "ui/gfx/size.h" 14 #include "ui/gfx/size.h"
13 15
14 namespace { 16 namespace {
15 17
16 static const char kSmallIconName[] = "icon_util/16_X_16_icon.ico"; 18 static const char kSmallIconName[] = "icon_util/16_X_16_icon.ico";
17 static const char kLargeIconName[] = "icon_util/128_X_128_icon.ico"; 19 static const char kLargeIconName[] = "icon_util/128_X_128_icon.ico";
18 static const char kTempIconFilename[] = "temp_test_icon.ico"; 20 static const char kTempIconFilename[] = "temp_test_icon.ico";
19 21
20 class IconUtilTest : public testing::Test { 22 class IconUtilTest : public testing::Test {
21 public: 23 public:
(...skipping 12 matching lines...) Expand all
34 HICON LoadIconFromFile(const FilePath& filename, int width, int height) { 36 HICON LoadIconFromFile(const FilePath& filename, int width, int height) {
35 HICON icon = static_cast<HICON>(LoadImage(NULL, 37 HICON icon = static_cast<HICON>(LoadImage(NULL,
36 filename.value().c_str(), 38 filename.value().c_str(),
37 IMAGE_ICON, 39 IMAGE_ICON,
38 width, 40 width,
39 height, 41 height,
40 LR_LOADTRANSPARENT | LR_LOADFROMFILE)); 42 LR_LOADTRANSPARENT | LR_LOADFROMFILE));
41 return icon; 43 return icon;
42 } 44 }
43 45
46 SkBitmap CreateBlackSkBitmap(int width, int height) {
47 SkBitmap bitmap;
48 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
49 bitmap.allocPixels();
50 // Setting the pixels to black.
51 memset(bitmap.getPixels(), 0, width * height * 4);
52 return bitmap;
53 }
54
44 protected: 55 protected:
45 // The root directory for test files. 56 // The root directory for test files.
46 FilePath test_data_directory_; 57 FilePath test_data_directory_;
47 58
59 // Directory for creating files by this test.
60 base::ScopedTempDir temp_directory_;
61
48 private: 62 private:
49 DISALLOW_COPY_AND_ASSIGN(IconUtilTest); 63 DISALLOW_COPY_AND_ASSIGN(IconUtilTest);
50 }; 64 };
51 65
52 } // namespace 66 } // namespace
53 67
54 // The following test case makes sure IconUtil::SkBitmapFromHICON fails 68 // The following test case makes sure IconUtil::SkBitmapFromHICON fails
55 // gracefully when called with invalid input parameters. 69 // gracefully when called with invalid input parameters.
56 TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) { 70 TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) {
57 FilePath icon_filename = test_data_directory_.AppendASCII(kSmallIconName); 71 FilePath icon_filename = test_data_directory_.AppendASCII(kSmallIconName);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) { 126 TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) {
113 scoped_ptr<SkBitmap> bitmap; 127 scoped_ptr<SkBitmap> bitmap;
114 FilePath valid_icon_filename = test_data_directory_.AppendASCII( 128 FilePath valid_icon_filename = test_data_directory_.AppendASCII(
115 kSmallIconName); 129 kSmallIconName);
116 FilePath invalid_icon_filename(FILE_PATH_LITERAL("C:\\<>?.ico")); 130 FilePath invalid_icon_filename(FILE_PATH_LITERAL("C:\\<>?.ico"));
117 131
118 // Wrong bitmap format. 132 // Wrong bitmap format.
119 bitmap.reset(new SkBitmap); 133 bitmap.reset(new SkBitmap);
120 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); 134 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
121 bitmap->setConfig(SkBitmap::kA8_Config, kSmallIconWidth, kSmallIconHeight); 135 bitmap->setConfig(SkBitmap::kA8_Config, kSmallIconWidth, kSmallIconHeight);
122 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, 136 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(),
123 valid_icon_filename)); 137 valid_icon_filename));
124 138
125 // Invalid bitmap size. 139 // Invalid bitmap size.
126 bitmap.reset(new SkBitmap); 140 bitmap.reset(new SkBitmap);
127 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); 141 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
128 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 0, 0); 142 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 0, 0);
129 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, 143 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(),
130 valid_icon_filename)); 144 valid_icon_filename));
131 145
132 // Bitmap with no allocated pixels. 146 // Bitmap with no allocated pixels.
133 bitmap.reset(new SkBitmap); 147 bitmap.reset(new SkBitmap);
134 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); 148 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
135 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 149 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
136 kSmallIconWidth, 150 kSmallIconWidth,
137 kSmallIconHeight); 151 kSmallIconHeight);
138 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, 152 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(),
139 valid_icon_filename)); 153 valid_icon_filename));
140 154
141 // Invalid file name. 155 // Invalid file name.
142 bitmap->allocPixels(); 156 bitmap->allocPixels();
143 // Setting the pixels to black. 157 // Setting the pixels to black.
144 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4); 158 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4);
145 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, 159 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, SkBitmap(),
146 invalid_icon_filename)); 160 invalid_icon_filename));
147 } 161 }
148 162
149 // This test case makes sure that when we load an icon from disk and convert 163 // This test case makes sure that when we load an icon from disk and convert
150 // the HICON into a bitmap, the bitmap has the expected format and dimentions. 164 // the HICON into a bitmap, the bitmap has the expected format and dimentions.
151 TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) { 165 TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) {
152 scoped_ptr<SkBitmap> bitmap; 166 scoped_ptr<SkBitmap> bitmap;
153 FilePath small_icon_filename = test_data_directory_.AppendASCII( 167 FilePath small_icon_filename = test_data_directory_.AppendASCII(
154 kSmallIconName); 168 kSmallIconName);
155 gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight); 169 gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight);
(...skipping 20 matching lines...) Expand all
176 EXPECT_EQ(bitmap->width(), large_icon_size.width()); 190 EXPECT_EQ(bitmap->width(), large_icon_size.width());
177 EXPECT_EQ(bitmap->height(), large_icon_size.height()); 191 EXPECT_EQ(bitmap->height(), large_icon_size.height());
178 EXPECT_EQ(bitmap->config(), SkBitmap::kARGB_8888_Config); 192 EXPECT_EQ(bitmap->config(), SkBitmap::kARGB_8888_Config);
179 ::DestroyIcon(large_icon); 193 ::DestroyIcon(large_icon);
180 } 194 }
181 195
182 // This test case makes sure that when an HICON is created from an SkBitmap, 196 // This test case makes sure that when an HICON is created from an SkBitmap,
183 // the returned handle is valid and refers to an icon with the expected 197 // the returned handle is valid and refers to an icon with the expected
184 // dimentions color depth etc. 198 // dimentions color depth etc.
185 TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) { 199 TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) {
186 scoped_ptr<SkBitmap> bitmap; 200 SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight);
187 bitmap.reset(new SkBitmap); 201 HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap);
188 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
189 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
190 kSmallIconWidth,
191 kSmallIconHeight);
192 bitmap->allocPixels();
193 HICON icon = IconUtil::CreateHICONFromSkBitmap(*bitmap);
194 EXPECT_NE(icon, static_cast<HICON>(NULL)); 202 EXPECT_NE(icon, static_cast<HICON>(NULL));
195 ICONINFO icon_info; 203 ICONINFO icon_info;
196 ASSERT_TRUE(::GetIconInfo(icon, &icon_info)); 204 ASSERT_TRUE(::GetIconInfo(icon, &icon_info));
197 EXPECT_TRUE(icon_info.fIcon); 205 EXPECT_TRUE(icon_info.fIcon);
198 206
199 // Now that have the icon information, we should obtain the specification of 207 // Now that have the icon information, we should obtain the specification of
200 // the icon's bitmap and make sure it matches the specification of the 208 // the icon's bitmap and make sure it matches the specification of the
201 // SkBitmap we started with. 209 // SkBitmap we started with.
202 // 210 //
203 // The bitmap handle contained in the icon information is a handle to a 211 // The bitmap handle contained in the icon information is a handle to a
(...skipping 15 matching lines...) Expand all
219 EXPECT_EQ(bitmap_info.bmiHeader.biHeight, kSmallIconHeight); 227 EXPECT_EQ(bitmap_info.bmiHeader.biHeight, kSmallIconHeight);
220 EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1); 228 EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1);
221 EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32); 229 EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32);
222 ::ReleaseDC(NULL, hdc); 230 ::ReleaseDC(NULL, hdc);
223 ::DestroyIcon(icon); 231 ::DestroyIcon(icon);
224 } 232 }
225 233
226 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap 234 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap
227 // creates a valid .ico file given an SkBitmap. 235 // creates a valid .ico file given an SkBitmap.
228 TEST_F(IconUtilTest, TestCreateIconFile) { 236 TEST_F(IconUtilTest, TestCreateIconFile) {
229 scoped_ptr<SkBitmap> bitmap;
230 FilePath icon_filename = test_data_directory_.AppendASCII(kTempIconFilename); 237 FilePath icon_filename = test_data_directory_.AppendASCII(kTempIconFilename);
231 238
232 // Allocating the bitmap. 239 SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight);
233 bitmap.reset(new SkBitmap); 240 EXPECT_TRUE(IconUtil::CreateIconFileFromSkBitmap(bitmap, SkBitmap(),
234 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
235 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
236 kSmallIconWidth,
237 kSmallIconHeight);
238 bitmap->allocPixels();
239
240 // Setting the pixels to black.
241 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4);
242
243 EXPECT_TRUE(IconUtil::CreateIconFileFromSkBitmap(*bitmap,
244 icon_filename)); 241 icon_filename));
245 242
246 // We are currently only testing that it is possible to load an icon from 243 // We are currently only testing that it is possible to load an icon from
247 // the .ico file we just created. We don't really check the additional icon 244 // the .ico file we just created. We don't really check the additional icon
248 // images created by IconUtil::CreateIconFileFromSkBitmap. 245 // images created by IconUtil::CreateIconFileFromSkBitmap.
249 HICON icon = LoadIconFromFile(icon_filename, 246 HICON icon = LoadIconFromFile(icon_filename,
250 kSmallIconWidth, 247 kSmallIconWidth,
251 kSmallIconHeight); 248 kSmallIconHeight);
252 EXPECT_NE(icon, static_cast<HICON>(NULL)); 249 EXPECT_NE(icon, static_cast<HICON>(NULL));
253 if (icon != NULL) { 250 if (icon != NULL) {
254 ::DestroyIcon(icon); 251 ::DestroyIcon(icon);
255 } 252 }
256 } 253 }
254
255 TEST_F(IconUtilTest, TestCreateIconFileWithLargeBitmap) {
256 const FilePath icon_path(temp_directory_.path().AppendASCII("test.ico"));
257 const SkBitmap bitmap_48 = CreateBlackSkBitmap(48, 48);
258 const SkBitmap bitmap_256 = CreateBlackSkBitmap(256, 256);
259
260 // First, create the icon file.
261 ASSERT_TRUE(IconUtil::CreateIconFileFromSkBitmap(bitmap_48, bitmap_256,
262 icon_path));
263 ASSERT_TRUE(file_util::PathExists(icon_path));
264
265 // Then, read the file and ensure it has a valid 256x256 PNG icon entry.
266 std::string icon_data;
267 ASSERT_TRUE(file_util::ReadFileToString(icon_path, &icon_data));
268 ASSERT_GE(icon_data.length(), sizeof(IconUtil::ICONDIR));
269
270 const IconUtil::ICONDIR* icon_dir =
271 reinterpret_cast<const IconUtil::ICONDIR*>(icon_data.data());
272 ASSERT_GE(icon_data.length(),
273 sizeof(IconUtil::ICONDIR) +
274 icon_dir->idCount * sizeof(IconUtil::ICONDIRENTRY));
275 const IconUtil::ICONDIRENTRY* png_entry = NULL;
276 for (size_t i = 0; i < icon_dir->idCount; ++i) {
277 const IconUtil::ICONDIRENTRY* entry = &icon_dir->idEntries[i];
278 if (entry->bWidth == 0 && entry->bHeight == 0) {
279 EXPECT_EQ(NULL, png_entry);
280 png_entry = entry;
281 }
282 }
283 ASSERT_TRUE(png_entry);
284
285 // Convert the PNG entry data back to a SkBitmap to ensure it's valid.
286 ASSERT_GE(icon_data.length(),
287 png_entry->dwImageOffset + png_entry->dwBytesInRes);
288 const unsigned char* png_bytes = reinterpret_cast<const unsigned char*>(
289 icon_data.data() + png_entry->dwImageOffset);
290 gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(png_bytes,
291 png_entry->dwBytesInRes);
292 SkBitmap bitmap = image.AsBitmap();
293 EXPECT_EQ(256, bitmap.width());
294 EXPECT_EQ(256, bitmap.height());
295 }
OLDNEW
« ui/gfx/icon_util.cc ('K') | « ui/gfx/icon_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698