OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "app/app_paths.h" | |
6 #include "app/gfx/icon_util.h" | |
7 #include "base/gfx/size.h" | |
8 #include "base/scoped_ptr.h" | |
9 #include "base/file_util.h" | |
10 #include "base/path_service.h" | |
11 #include "third_party/skia/include/core/SkBitmap.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace { | |
15 | |
16 static const wchar_t* const kSmallIconName = L"icon_util\\16_X_16_icon.ico"; | |
17 static const wchar_t* const kLargeIconName = L"icon_util\\128_X_128_icon.ico"; | |
18 static const wchar_t* const kTempIconFilename = L"temp_test_icon.ico"; | |
19 | |
20 class IconUtilTest : public testing::Test { | |
21 public: | |
22 IconUtilTest() { | |
23 PathService::Get(app::DIR_TEST_DATA, &test_data_directory_); | |
24 } | |
25 ~IconUtilTest() {} | |
26 | |
27 static const int kSmallIconWidth = 16; | |
28 static const int kSmallIconHeight = 16; | |
29 static const int kLargeIconWidth = 128; | |
30 static const int kLargeIconHeight = 128; | |
31 | |
32 // Given a file name for an .ico file and an image dimentions, this | |
33 // function loads the icon and returns an HICON handle. | |
34 HICON LoadIconFromFile(const std::wstring& filename, | |
35 int width, | |
36 int height) { | |
37 HICON icon = | |
38 static_cast<HICON>(LoadImage(NULL, | |
39 filename.c_str(), | |
40 IMAGE_ICON, | |
41 width, | |
42 height, | |
43 LR_LOADTRANSPARENT | LR_LOADFROMFILE)); | |
44 return icon; | |
45 } | |
46 | |
47 protected: | |
48 // The root directory for test files. | |
49 std::wstring test_data_directory_; | |
50 | |
51 private: | |
52 DISALLOW_EVIL_CONSTRUCTORS(IconUtilTest); | |
53 }; | |
54 }; | |
55 | |
56 // The following test case makes sure IconUtil::SkBitmapFromHICON fails | |
57 // gracefully when called with invalid input parameters. | |
58 TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) { | |
59 std::wstring icon_filename(test_data_directory_); | |
60 file_util::AppendToPath(&icon_filename, kSmallIconName); | |
61 gfx::Size icon_size(kSmallIconWidth, kSmallIconHeight); | |
62 HICON icon = LoadIconFromFile(icon_filename, | |
63 icon_size.width(), | |
64 icon_size.height()); | |
65 ASSERT_TRUE(icon != NULL); | |
66 | |
67 // Invalid size parameter. | |
68 gfx::Size invalid_icon_size(kSmallIconHeight, 0); | |
69 EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(icon, invalid_icon_size), | |
70 static_cast<SkBitmap*>(NULL)); | |
71 | |
72 // Invalid icon. | |
73 EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(NULL, icon_size), | |
74 static_cast<SkBitmap*>(NULL)); | |
75 | |
76 // The following code should succeed. | |
77 scoped_ptr<SkBitmap> bitmap; | |
78 bitmap.reset(IconUtil::CreateSkBitmapFromHICON(icon, icon_size)); | |
79 EXPECT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
80 ::DestroyIcon(icon); | |
81 } | |
82 | |
83 // The following test case makes sure IconUtil::CreateHICONFromSkBitmap fails | |
84 // gracefully when called with invalid input parameters. | |
85 TEST_F(IconUtilTest, TestBitmapToIconInvalidParameters) { | |
86 HICON icon = NULL; | |
87 scoped_ptr<SkBitmap> bitmap; | |
88 | |
89 // Wrong bitmap format. | |
90 bitmap.reset(new SkBitmap); | |
91 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
92 bitmap->setConfig(SkBitmap::kA8_Config, kSmallIconWidth, kSmallIconHeight); | |
93 icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); | |
94 EXPECT_EQ(icon, static_cast<HICON>(NULL)); | |
95 | |
96 // Invalid bitmap size. | |
97 bitmap.reset(new SkBitmap); | |
98 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
99 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 0, 0); | |
100 icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); | |
101 EXPECT_EQ(icon, static_cast<HICON>(NULL)); | |
102 | |
103 // Valid bitmap configuration but no pixels allocated. | |
104 bitmap.reset(new SkBitmap); | |
105 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
106 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | |
107 kSmallIconWidth, | |
108 kSmallIconHeight); | |
109 icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); | |
110 EXPECT_TRUE(icon == NULL); | |
111 } | |
112 | |
113 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap | |
114 // fails gracefully when called with invalid input parameters. | |
115 TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) { | |
116 scoped_ptr<SkBitmap> bitmap; | |
117 std::wstring valid_icon_filename(test_data_directory_); | |
118 file_util::AppendToPath(&valid_icon_filename, kSmallIconName); | |
119 std::wstring invalid_icon_filename(L"C:\\<>?.ico"); | |
120 | |
121 // Wrong bitmap format. | |
122 bitmap.reset(new SkBitmap); | |
123 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
124 bitmap->setConfig(SkBitmap::kA8_Config, kSmallIconWidth, kSmallIconHeight); | |
125 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, | |
126 valid_icon_filename)); | |
127 | |
128 // Invalid bitmap size. | |
129 bitmap.reset(new SkBitmap); | |
130 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
131 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 0, 0); | |
132 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, | |
133 valid_icon_filename)); | |
134 | |
135 // Bitmap with no allocated pixels. | |
136 bitmap.reset(new SkBitmap); | |
137 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
138 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | |
139 kSmallIconWidth, | |
140 kSmallIconHeight); | |
141 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, | |
142 valid_icon_filename)); | |
143 | |
144 // Invalid file name. | |
145 bitmap->allocPixels(); | |
146 // Setting the pixels to black. | |
147 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4); | |
148 EXPECT_FALSE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, | |
149 invalid_icon_filename)); | |
150 } | |
151 | |
152 // This test case makes sure that when we load an icon from disk and convert | |
153 // the HICON into a bitmap, the bitmap has the expected format and dimentions. | |
154 TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) { | |
155 scoped_ptr<SkBitmap> bitmap; | |
156 std::wstring small_icon_filename(test_data_directory_); | |
157 file_util::AppendToPath(&small_icon_filename, kSmallIconName); | |
158 gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight); | |
159 HICON small_icon = LoadIconFromFile(small_icon_filename, | |
160 small_icon_size.width(), | |
161 small_icon_size.height()); | |
162 ASSERT_NE(small_icon, static_cast<HICON>(NULL)); | |
163 bitmap.reset(IconUtil::CreateSkBitmapFromHICON(small_icon, small_icon_size)); | |
164 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
165 EXPECT_EQ(bitmap->width(), small_icon_size.width()); | |
166 EXPECT_EQ(bitmap->height(), small_icon_size.height()); | |
167 EXPECT_EQ(bitmap->config(), SkBitmap::kARGB_8888_Config); | |
168 ::DestroyIcon(small_icon); | |
169 | |
170 std::wstring large_icon_filename(test_data_directory_); | |
171 file_util::AppendToPath(&large_icon_filename, kLargeIconName); | |
172 gfx::Size large_icon_size(kLargeIconWidth, kLargeIconHeight); | |
173 HICON large_icon = LoadIconFromFile(large_icon_filename, | |
174 large_icon_size.width(), | |
175 large_icon_size.height()); | |
176 ASSERT_NE(large_icon, static_cast<HICON>(NULL)); | |
177 bitmap.reset(IconUtil::CreateSkBitmapFromHICON(large_icon, large_icon_size)); | |
178 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
179 EXPECT_EQ(bitmap->width(), large_icon_size.width()); | |
180 EXPECT_EQ(bitmap->height(), large_icon_size.height()); | |
181 EXPECT_EQ(bitmap->config(), SkBitmap::kARGB_8888_Config); | |
182 ::DestroyIcon(large_icon); | |
183 } | |
184 | |
185 // This test case makes sure that when an HICON is created from an SkBitmap, | |
186 // the returned handle is valid and refers to an icon with the expected | |
187 // dimentions color depth etc. | |
188 TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) { | |
189 scoped_ptr<SkBitmap> bitmap; | |
190 bitmap.reset(new SkBitmap); | |
191 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
192 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | |
193 kSmallIconWidth, | |
194 kSmallIconHeight); | |
195 bitmap->allocPixels(); | |
196 HICON icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); | |
197 EXPECT_NE(icon, static_cast<HICON>(NULL)); | |
198 ICONINFO icon_info; | |
199 ASSERT_TRUE(::GetIconInfo(icon, &icon_info)); | |
200 EXPECT_TRUE(icon_info.fIcon); | |
201 | |
202 // Now that have the icon information, we should obtain the specification of | |
203 // the icon's bitmap and make sure it matches the specification of the | |
204 // SkBitmap we started with. | |
205 // | |
206 // The bitmap handle contained in the icon information is a handle to a | |
207 // compatible bitmap so we need to call ::GetDIBits() in order to retrieve | |
208 // the bitmap's header information. | |
209 BITMAPINFO bitmap_info; | |
210 ::ZeroMemory(&bitmap_info, sizeof(BITMAPINFO)); | |
211 bitmap_info.bmiHeader.biSize = sizeof(BITMAPINFO); | |
212 HDC hdc = ::GetDC(NULL); | |
213 int result = ::GetDIBits(hdc, | |
214 icon_info.hbmColor, | |
215 0, | |
216 kSmallIconWidth, | |
217 NULL, | |
218 &bitmap_info, | |
219 DIB_RGB_COLORS); | |
220 ASSERT_GT(result, 0); | |
221 EXPECT_EQ(bitmap_info.bmiHeader.biWidth, kSmallIconWidth); | |
222 EXPECT_EQ(bitmap_info.bmiHeader.biHeight, kSmallIconHeight); | |
223 EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1); | |
224 EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32); | |
225 ::ReleaseDC(NULL, hdc); | |
226 ::DestroyIcon(icon); | |
227 } | |
228 | |
229 // The following test case makes sure IconUtil::CreateIconFileFromSkBitmap | |
230 // creates a valid .ico file given an SkBitmap. | |
231 TEST_F(IconUtilTest, TestCreateIconFile) { | |
232 scoped_ptr<SkBitmap> bitmap; | |
233 std::wstring icon_filename(test_data_directory_); | |
234 file_util::AppendToPath(&icon_filename, kTempIconFilename); | |
235 | |
236 // Allocating the bitmap. | |
237 bitmap.reset(new SkBitmap); | |
238 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL)); | |
239 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | |
240 kSmallIconWidth, | |
241 kSmallIconHeight); | |
242 bitmap->allocPixels(); | |
243 | |
244 // Setting the pixels to black. | |
245 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4); | |
246 | |
247 EXPECT_TRUE(IconUtil::CreateIconFileFromSkBitmap(*bitmap, | |
248 icon_filename)); | |
249 | |
250 // We are currently only testing that it is possible to load an icon from | |
251 // the .ico file we just created. We don't really check the additional icon | |
252 // images created by IconUtil::CreateIconFileFromSkBitmap. | |
253 HICON icon = LoadIconFromFile(icon_filename, | |
254 kSmallIconWidth, | |
255 kSmallIconHeight); | |
256 EXPECT_NE(icon, static_cast<HICON>(NULL)); | |
257 if (icon != NULL) { | |
258 ::DestroyIcon(icon); | |
259 } | |
260 } | |
OLD | NEW |