blob: f0f2cde0c34c64e401e1703fdbd9fd013af290ea [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]9974ce12014-01-30 07:14:135#include "ui/gfx/icon_util.h"
6
thestig22dfc4012014-09-05 08:29:447#include "base/files/file_util.h"
[email protected]6ed46c82013-01-08 19:02:598#include "base/files/scoped_temp_dir.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
initial.commit09911bf2008-07-26 23:55:2911#include "testing/gtest/include/gtest/gtest.h"
[email protected]38658102010-07-30 14:01:5912#include "third_party/skia/include/core/SkBitmap.h"
[email protected]08397d52011-02-05 01:53:3813#include "ui/gfx/gfx_paths.h"
[email protected]9974ce12014-01-30 07:14:1314#include "ui/gfx/icon_util_unittests_resource.h"
[email protected]6ed46c82013-01-08 19:02:5915#include "ui/gfx/image/image.h"
[email protected]7842b4f2013-04-30 04:43:4916#include "ui/gfx/image/image_family.h"
[email protected]08397d52011-02-05 01:53:3817#include "ui/gfx/size.h"
initial.commit09911bf2008-07-26 23:55:2918
19namespace {
20
[email protected]38658102010-07-30 14:01:5921static const char kSmallIconName[] = "icon_util/16_X_16_icon.ico";
22static const char kLargeIconName[] = "icon_util/128_X_128_icon.ico";
23static const char kTempIconFilename[] = "temp_test_icon.ico";
initial.commit09911bf2008-07-26 23:55:2924
[email protected]7842b4f2013-04-30 04:43:4925} // namespace
26
[email protected]38658102010-07-30 14:01:5927class IconUtilTest : public testing::Test {
28 public:
[email protected]806a091d2013-04-18 06:30:3629 virtual void SetUp() OVERRIDE {
[email protected]0165aa22014-08-17 04:41:5930 gfx::RegisterPathProvider();
31 ASSERT_TRUE(PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_));
[email protected]806a091d2013-04-18 06:30:3632 temp_directory_.CreateUniqueTempDir();
[email protected]38658102010-07-30 14:01:5933 }
initial.commit09911bf2008-07-26 23:55:2934
[email protected]38658102010-07-30 14:01:5935 static const int kSmallIconWidth = 16;
36 static const int kSmallIconHeight = 16;
37 static const int kLargeIconWidth = 128;
38 static const int kLargeIconHeight = 128;
initial.commit09911bf2008-07-26 23:55:2939
[email protected]806a091d2013-04-18 06:30:3640 // Given a file name for an .ico file and an image dimensions, this
[email protected]38658102010-07-30 14:01:5941 // function loads the icon and returns an HICON handle.
[email protected]79f63882013-02-10 05:15:4542 HICON LoadIconFromFile(const base::FilePath& filename,
43 int width, int height) {
[email protected]38658102010-07-30 14:01:5944 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.commit09911bf2008-07-26 23:55:2952
[email protected]6ed46c82013-01-08 19:02:5953 SkBitmap CreateBlackSkBitmap(int width, int height) {
54 SkBitmap bitmap;
[email protected]f62e57b2014-03-12 20:52:0255 bitmap.allocN32Pixels(width, height);
56 // Setting the pixels to transparent-black.
[email protected]6ed46c82013-01-08 19:02:5957 memset(bitmap.getPixels(), 0, width * height * 4);
58 return bitmap;
59 }
60
[email protected]7842b4f2013-04-30 04:43:4961 // 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]38658102010-07-30 14:01:5967 protected:
[email protected]806a091d2013-04-18 06:30:3668 // The root directory for test files. This should be treated as read-only.
[email protected]79f63882013-02-10 05:15:4569 base::FilePath test_data_directory_;
initial.commit09911bf2008-07-26 23:55:2970
[email protected]6ed46c82013-01-08 19:02:5971 // Directory for creating files by this test.
72 base::ScopedTempDir temp_directory_;
initial.commit09911bf2008-07-26 23:55:2973};
74
[email protected]7842b4f2013-04-30 04:43:4975void IconUtilTest::CheckAllIconSizes(const base::FilePath& icon_filename,
76 int max_icon_size) {
[email protected]7567484142013-07-11 17:36:0777 ASSERT_TRUE(base::PathExists(icon_filename));
[email protected]7842b4f2013-04-30 04:43:4978
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]82f84b92013-08-30 18:23:5096 ASSERT_TRUE(base::ReadFileToString(icon_filename, &icon_data));
[email protected]7842b4f2013-04-30 04:43:4997 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]38658102010-07-30 14:01:59139
initial.commit09911bf2008-07-26 23:55:29140// The following test case makes sure IconUtil::SkBitmapFromHICON fails
141// gracefully when called with invalid input parameters.
142TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) {
[email protected]79f63882013-02-10 05:15:45143 base::FilePath icon_filename =
144 test_data_directory_.AppendASCII(kSmallIconName);
initial.commit09911bf2008-07-26 23:55:29145 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.
169TEST_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]7d3c069e2014-06-09 17:11:15176 bitmap->setInfo(SkImageInfo::MakeA8(kSmallIconWidth, kSmallIconHeight));
initial.commit09911bf2008-07-26 23:55:29177 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]7d3c069e2014-06-09 17:11:15183 bitmap->setInfo(SkImageInfo::MakeN32Premul(0, 0));
initial.commit09911bf2008-07-26 23:55:29184 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]7d3c069e2014-06-09 17:11:15190 bitmap->setInfo(SkImageInfo::MakeN32Premul(kSmallIconWidth,
191 kSmallIconHeight));
initial.commit09911bf2008-07-26 23:55:29192 icon = IconUtil::CreateHICONFromSkBitmap(*bitmap);
193 EXPECT_TRUE(icon == NULL);
194}
195
[email protected]7842b4f2013-04-30 04:43:49196// The following test case makes sure IconUtil::CreateIconFileFromImageFamily
initial.commit09911bf2008-07-26 23:55:29197// fails gracefully when called with invalid input parameters.
198TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) {
199 scoped_ptr<SkBitmap> bitmap;
[email protected]7842b4f2013-04-30 04:43:49200 gfx::ImageFamily image_family;
[email protected]806a091d2013-04-18 06:30:36201 base::FilePath valid_icon_filename = temp_directory_.path().AppendASCII(
202 kTempIconFilename);
203 base::FilePath invalid_icon_filename = temp_directory_.path().AppendASCII(
204 "<>?.ico");
initial.commit09911bf2008-07-26 23:55:29205
206 // Wrong bitmap format.
207 bitmap.reset(new SkBitmap);
208 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
[email protected]7842b4f2013-04-30 04:43:49209 // Must allocate pixels or else ImageSkia will ignore the bitmap and just
210 // return an empty image.
[email protected]7d3c069e2014-06-09 17:11:15211 bitmap->allocPixels(SkImageInfo::MakeA8(kSmallIconWidth, kSmallIconHeight));
[email protected]7842b4f2013-04-30 04:43:49212 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]7567484142013-07-11 17:36:07216 EXPECT_FALSE(base::PathExists(valid_icon_filename));
initial.commit09911bf2008-07-26 23:55:29217
218 // Invalid bitmap size.
[email protected]7842b4f2013-04-30 04:43:49219 image_family.clear();
initial.commit09911bf2008-07-26 23:55:29220 bitmap.reset(new SkBitmap);
221 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
[email protected]7d3c069e2014-06-09 17:11:15222 bitmap->allocPixels(SkImageInfo::MakeN32Premul(0, 0));
[email protected]7842b4f2013-04-30 04:43:49223 image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap));
224 EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
225 valid_icon_filename));
[email protected]7567484142013-07-11 17:36:07226 EXPECT_FALSE(base::PathExists(valid_icon_filename));
initial.commit09911bf2008-07-26 23:55:29227
228 // Bitmap with no allocated pixels.
[email protected]7842b4f2013-04-30 04:43:49229 image_family.clear();
initial.commit09911bf2008-07-26 23:55:29230 bitmap.reset(new SkBitmap);
231 ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
[email protected]7d3c069e2014-06-09 17:11:15232 bitmap->setInfo(SkImageInfo::MakeN32Premul(kSmallIconWidth,
233 kSmallIconHeight));
[email protected]7842b4f2013-04-30 04:43:49234 image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap));
235 EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
236 valid_icon_filename));
[email protected]7567484142013-07-11 17:36:07237 EXPECT_FALSE(base::PathExists(valid_icon_filename));
initial.commit09911bf2008-07-26 23:55:29238
239 // Invalid file name.
[email protected]7842b4f2013-04-30 04:43:49240 image_family.clear();
initial.commit09911bf2008-07-26 23:55:29241 bitmap->allocPixels();
[email protected]0006be642008-08-27 16:43:19242 // Setting the pixels to black.
243 memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4);
[email protected]7842b4f2013-04-30 04:43:49244 image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap));
245 EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
246 invalid_icon_filename));
[email protected]7567484142013-07-11 17:36:07247 EXPECT_FALSE(base::PathExists(invalid_icon_filename));
initial.commit09911bf2008-07-26 23:55:29248}
249
[email protected]7842b4f2013-04-30 04:43:49250// This test case makes sure IconUtil::CreateIconFileFromImageFamily fails if
251// the image family is empty or invalid.
252TEST_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]7567484142013-07-11 17:36:07259 EXPECT_FALSE(base::PathExists(icon_filename));
[email protected]7842b4f2013-04-30 04:43:49260
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]7567484142013-07-11 17:36:07266 EXPECT_FALSE(base::PathExists(icon_filename));
[email protected]7842b4f2013-04-30 04:43:49267}
268
initial.commit09911bf2008-07-26 23:55:29269// This test case makes sure that when we load an icon from disk and convert
[email protected]806a091d2013-04-18 06:30:36270// the HICON into a bitmap, the bitmap has the expected format and dimensions.
initial.commit09911bf2008-07-26 23:55:29271TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) {
272 scoped_ptr<SkBitmap> bitmap;
[email protected]79f63882013-02-10 05:15:45273 base::FilePath small_icon_filename = test_data_directory_.AppendASCII(
[email protected]38658102010-07-30 14:01:59274 kSmallIconName);
initial.commit09911bf2008-07-26 23:55:29275 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]c80330da2014-07-01 19:26:27284 EXPECT_EQ(bitmap->colorType(), kN32_SkColorType);
initial.commit09911bf2008-07-26 23:55:29285 ::DestroyIcon(small_icon);
286
[email protected]79f63882013-02-10 05:15:45287 base::FilePath large_icon_filename = test_data_directory_.AppendASCII(
[email protected]38658102010-07-30 14:01:59288 kLargeIconName);
initial.commit09911bf2008-07-26 23:55:29289 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]c80330da2014-07-01 19:26:27298 EXPECT_EQ(bitmap->colorType(), kN32_SkColorType);
initial.commit09911bf2008-07-26 23:55:29299 ::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]806a091d2013-04-18 06:30:36304// dimensions color depth etc.
initial.commit09911bf2008-07-26 23:55:29305TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) {
[email protected]6ed46c82013-01-08 19:02:59306 SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight);
307 HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap);
initial.commit09911bf2008-07-26 23:55:29308 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]7842b4f2013-04-30 04:43:49340// 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.
343TEST_F(IconUtilTest, TestCreateIconFileFromImageFamily) {
344 gfx::ImageFamily image_family;
[email protected]79f63882013-02-10 05:15:45345 base::FilePath icon_filename =
[email protected]806a091d2013-04-18 06:30:36346 temp_directory_.path().AppendASCII(kTempIconFilename);
initial.commit09911bf2008-07-26 23:55:29347
[email protected]7842b4f2013-04-30 04:43:49348 // 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.commit09911bf2008-07-26 23:55:29354
[email protected]7842b4f2013-04-30 04:43:49355 // 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]6ed46c82013-01-08 19:02:59360
[email protected]7842b4f2013-04-30 04:43:49361 // 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]6ed46c82013-01-08 19:02:59366
[email protected]7842b4f2013-04-30 04:43:49367 // 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]6ed46c82013-01-08 19:02:59373
[email protected]7842b4f2013-04-30 04:43:49374 // 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]6ed46c82013-01-08 19:02:59381
[email protected]7842b4f2013-04-30 04:43:49382 // 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]6ed46c82013-01-08 19:02:59389
[email protected]7842b4f2013-04-30 04:43:49390 // 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]6ed46c82013-01-08 19:02:59405}
[email protected]be74fb52013-01-14 19:45:34406
407TEST_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
416TEST_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]7842b4f2013-04-30 04:43:49424
425// This tests that kNumIconDimensionsUpToMediumSize has the correct value.
426TEST_F(IconUtilTest, TestNumIconDimensionsUpToMediumSize) {
427 ASSERT_LE(IconUtil::kNumIconDimensionsUpToMediumSize,
428 IconUtil::kNumIconDimensions);
429 EXPECT_EQ(IconUtil::kMediumIconSize,
430 IconUtil::kIconDimensions[
431 IconUtil::kNumIconDimensionsUpToMediumSize - 1]);
432}