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

Side by Side Diff: chrome/common/gfx/icon_util_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698