This source file includes following definitions.
- SetUp
- LoadIconFromFile
- CreateBlackSkBitmap
- CheckAllIconSizes
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "ui/gfx/icon_util.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/gfx_paths.h"
#include "ui/gfx/icon_util_unittests_resource.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_family.h"
#include "ui/gfx/size.h"
namespace {
static const char kSmallIconName[] = "icon_util/16_X_16_icon.ico";
static const char kLargeIconName[] = "icon_util/128_X_128_icon.ico";
static const char kTempIconFilename[] = "temp_test_icon.ico";
}
class IconUtilTest : public testing::Test {
public:
virtual void SetUp() OVERRIDE {
PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_);
temp_directory_.CreateUniqueTempDir();
}
static const int kSmallIconWidth = 16;
static const int kSmallIconHeight = 16;
static const int kLargeIconWidth = 128;
static const int kLargeIconHeight = 128;
HICON LoadIconFromFile(const base::FilePath& filename,
int width, int height) {
HICON icon = static_cast<HICON>(LoadImage(NULL,
filename.value().c_str(),
IMAGE_ICON,
width,
height,
LR_LOADTRANSPARENT | LR_LOADFROMFILE));
return icon;
}
SkBitmap CreateBlackSkBitmap(int width, int height) {
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
memset(bitmap.getPixels(), 0, width * height * 4);
return bitmap;
}
void CheckAllIconSizes(const base::FilePath& icon_filename,
int max_icon_size);
protected:
base::FilePath test_data_directory_;
base::ScopedTempDir temp_directory_;
};
void IconUtilTest::CheckAllIconSizes(const base::FilePath& icon_filename,
int max_icon_size) {
ASSERT_TRUE(base::PathExists(icon_filename));
int expected_num_icons = 0;
for (size_t i = 0; i < IconUtil::kNumIconDimensions; ++i) {
if (IconUtil::kIconDimensions[i] > max_icon_size)
break;
++expected_num_icons;
}
HICON icon = LoadIconFromFile(icon_filename, kSmallIconWidth,
kSmallIconHeight);
EXPECT_NE(static_cast<HICON>(NULL), icon);
if (icon != NULL)
::DestroyIcon(icon);
std::string icon_data;
ASSERT_TRUE(base::ReadFileToString(icon_filename, &icon_data));
ASSERT_GE(icon_data.length(), sizeof(IconUtil::ICONDIR));
const IconUtil::ICONDIR* icon_dir =
reinterpret_cast<const IconUtil::ICONDIR*>(icon_data.data());
EXPECT_EQ(expected_num_icons, icon_dir->idCount);
ASSERT_GE(IconUtil::kNumIconDimensions, icon_dir->idCount);
ASSERT_GE(icon_data.length(),
sizeof(IconUtil::ICONDIR) +
icon_dir->idCount * sizeof(IconUtil::ICONDIRENTRY));
const IconUtil::ICONDIRENTRY* png_entry = NULL;
for (size_t i = 0; i < icon_dir->idCount; ++i) {
const IconUtil::ICONDIRENTRY* entry = &icon_dir->idEntries[i];
int expected_size = IconUtil::kIconDimensions[i] % 256;
EXPECT_EQ(expected_size, static_cast<int>(entry->bWidth));
EXPECT_EQ(expected_size, static_cast<int>(entry->bHeight));
if (entry->bWidth == 0 && entry->bHeight == 0) {
EXPECT_EQ(NULL, png_entry);
png_entry = entry;
}
}
if (max_icon_size >= 256) {
ASSERT_TRUE(png_entry);
ASSERT_GE(icon_data.length(),
png_entry->dwImageOffset + png_entry->dwBytesInRes);
const unsigned char* png_bytes = reinterpret_cast<const unsigned char*>(
icon_data.data() + png_entry->dwImageOffset);
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
png_bytes, png_entry->dwBytesInRes);
SkBitmap bitmap = image.AsBitmap();
EXPECT_EQ(256, bitmap.width());
EXPECT_EQ(256, bitmap.height());
}
}
TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) {
base::FilePath icon_filename =
test_data_directory_.AppendASCII(kSmallIconName);
gfx::Size icon_size(kSmallIconWidth, kSmallIconHeight);
HICON icon = LoadIconFromFile(icon_filename,
icon_size.width(),
icon_size.height());
ASSERT_TRUE(icon != NULL);
gfx::Size invalid_icon_size(kSmallIconHeight, 0);
EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(icon, invalid_icon_size),
static_cast<SkBitmap*>(NULL));
EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(NULL, icon_size),
static_cast<SkBitmap*>(NULL));
scoped_ptr<SkBitmap> bitmap;
bitmap.reset(IconUtil::CreateSkBitmapFromHICON(icon, icon_size));
EXPECT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
::DestroyIcon(icon);
}
TEST_F(IconUtilTest, TestBitmapToIconInvalidParameters) {
HICON icon = NULL;
scoped_ptr<SkBitmap> bitmap;
bitmap.reset(new SkBitmap);
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
bitmap->setConfig(SkImageInfo::MakeA8(kSmallIconWidth, kSmallIconHeight));
icon = IconUtil::CreateHICONFromSkBitmap(*bitmap);
EXPECT_EQ(icon, static_cast<HICON>(NULL));
bitmap.reset(new SkBitmap);
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
bitmap->setConfig(SkImageInfo::MakeN32Premul(0, 0));
icon = IconUtil::CreateHICONFromSkBitmap(*bitmap);
EXPECT_EQ(icon, static_cast<HICON>(NULL));
bitmap.reset(new SkBitmap);
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
bitmap->setConfig(SkImageInfo::MakeN32Premul(kSmallIconWidth,
kSmallIconHeight));
icon = IconUtil::CreateHICONFromSkBitmap(*bitmap);
EXPECT_TRUE(icon == NULL);
}
TEST_F(IconUtilTest, TestCreateIconFileInvalidParameters) {
scoped_ptr<SkBitmap> bitmap;
gfx::ImageFamily image_family;
base::FilePath valid_icon_filename = temp_directory_.path().AppendASCII(
kTempIconFilename);
base::FilePath invalid_icon_filename = temp_directory_.path().AppendASCII(
"<>?.ico");
bitmap.reset(new SkBitmap);
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
bitmap->setConfig(SkImageInfo::MakeA8(kSmallIconWidth, kSmallIconHeight));
bitmap->allocPixels();
memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height());
image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap));
EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
valid_icon_filename));
EXPECT_FALSE(base::PathExists(valid_icon_filename));
image_family.clear();
bitmap.reset(new SkBitmap);
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
bitmap->setConfig(SkImageInfo::MakeN32Premul(0, 0));
bitmap->allocPixels();
image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap));
EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
valid_icon_filename));
EXPECT_FALSE(base::PathExists(valid_icon_filename));
image_family.clear();
bitmap.reset(new SkBitmap);
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
bitmap->setConfig(SkImageInfo::MakeN32Premul(kSmallIconWidth,
kSmallIconHeight));
image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap));
EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
valid_icon_filename));
EXPECT_FALSE(base::PathExists(valid_icon_filename));
image_family.clear();
bitmap->allocPixels();
memset(bitmap->getPixels(), 0, bitmap->width() * bitmap->height() * 4);
image_family.Add(gfx::Image::CreateFrom1xBitmap(*bitmap));
EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
invalid_icon_filename));
EXPECT_FALSE(base::PathExists(invalid_icon_filename));
}
TEST_F(IconUtilTest, TestCreateIconFileEmptyImageFamily) {
base::FilePath icon_filename = temp_directory_.path().AppendASCII(
kTempIconFilename);
EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(gfx::ImageFamily(),
icon_filename));
EXPECT_FALSE(base::PathExists(icon_filename));
gfx::ImageFamily image_family;
image_family.Add(gfx::Image());
EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
EXPECT_FALSE(base::PathExists(icon_filename));
}
TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) {
scoped_ptr<SkBitmap> bitmap;
base::FilePath small_icon_filename = test_data_directory_.AppendASCII(
kSmallIconName);
gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight);
HICON small_icon = LoadIconFromFile(small_icon_filename,
small_icon_size.width(),
small_icon_size.height());
ASSERT_NE(small_icon, static_cast<HICON>(NULL));
bitmap.reset(IconUtil::CreateSkBitmapFromHICON(small_icon, small_icon_size));
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
EXPECT_EQ(bitmap->width(), small_icon_size.width());
EXPECT_EQ(bitmap->height(), small_icon_size.height());
EXPECT_EQ(bitmap->colorType(), kPMColor_SkColorType);
::DestroyIcon(small_icon);
base::FilePath large_icon_filename = test_data_directory_.AppendASCII(
kLargeIconName);
gfx::Size large_icon_size(kLargeIconWidth, kLargeIconHeight);
HICON large_icon = LoadIconFromFile(large_icon_filename,
large_icon_size.width(),
large_icon_size.height());
ASSERT_NE(large_icon, static_cast<HICON>(NULL));
bitmap.reset(IconUtil::CreateSkBitmapFromHICON(large_icon, large_icon_size));
ASSERT_NE(bitmap.get(), static_cast<SkBitmap*>(NULL));
EXPECT_EQ(bitmap->width(), large_icon_size.width());
EXPECT_EQ(bitmap->height(), large_icon_size.height());
EXPECT_EQ(bitmap->colorType(), kPMColor_SkColorType);
::DestroyIcon(large_icon);
}
TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) {
SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight);
HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap);
EXPECT_NE(icon, static_cast<HICON>(NULL));
ICONINFO icon_info;
ASSERT_TRUE(::GetIconInfo(icon, &icon_info));
EXPECT_TRUE(icon_info.fIcon);
BITMAPINFO bitmap_info;
::ZeroMemory(&bitmap_info, sizeof(BITMAPINFO));
bitmap_info.bmiHeader.biSize = sizeof(BITMAPINFO);
HDC hdc = ::GetDC(NULL);
int result = ::GetDIBits(hdc,
icon_info.hbmColor,
0,
kSmallIconWidth,
NULL,
&bitmap_info,
DIB_RGB_COLORS);
ASSERT_GT(result, 0);
EXPECT_EQ(bitmap_info.bmiHeader.biWidth, kSmallIconWidth);
EXPECT_EQ(bitmap_info.bmiHeader.biHeight, kSmallIconHeight);
EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1);
EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32);
::ReleaseDC(NULL, hdc);
::DestroyIcon(icon);
}
TEST_F(IconUtilTest, TestCreateIconFileFromImageFamily) {
gfx::ImageFamily image_family;
base::FilePath icon_filename =
temp_directory_.path().AppendASCII(kTempIconFilename);
image_family.Add(gfx::Image::CreateFrom1xBitmap(
CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 48);
image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(48, 48)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 48);
image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(64, 64)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 256);
image_family.Add(gfx::Image::CreateFrom1xBitmap(
CreateBlackSkBitmap(256, 256)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 256);
image_family.clear();
image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(49, 49)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 256);
image_family.clear();
image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(16, 32)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 48);
image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(32, 49)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 256);
image_family.clear();
image_family.Add(gfx::Image());
image_family.Add(gfx::Image::CreateFrom1xBitmap(CreateBlackSkBitmap(16, 16)));
ASSERT_TRUE(IconUtil::CreateIconFileFromImageFamily(image_family,
icon_filename));
CheckAllIconSizes(icon_filename, 48);
}
TEST_F(IconUtilTest, TestCreateSkBitmapFromIconResource48x48) {
HMODULE module = GetModuleHandle(NULL);
scoped_ptr<SkBitmap> bitmap(
IconUtil::CreateSkBitmapFromIconResource(module, IDR_MAINFRAME, 48));
ASSERT_TRUE(bitmap.get());
EXPECT_EQ(48, bitmap->width());
EXPECT_EQ(48, bitmap->height());
}
TEST_F(IconUtilTest, TestCreateSkBitmapFromIconResource256x256) {
HMODULE module = GetModuleHandle(NULL);
scoped_ptr<SkBitmap> bitmap(
IconUtil::CreateSkBitmapFromIconResource(module, IDR_MAINFRAME, 256));
ASSERT_TRUE(bitmap.get());
EXPECT_EQ(256, bitmap->width());
EXPECT_EQ(256, bitmap->height());
}
TEST_F(IconUtilTest, TestNumIconDimensionsUpToMediumSize) {
ASSERT_LE(IconUtil::kNumIconDimensionsUpToMediumSize,
IconUtil::kNumIconDimensions);
EXPECT_EQ(IconUtil::kMediumIconSize,
IconUtil::kIconDimensions[
IconUtil::kNumIconDimensionsUpToMediumSize - 1]);
}