Skip to content

Commit

Permalink
Remove the dependency on WTL from text_icon_test.cc
Browse files Browse the repository at this point in the history
This commit updates

  text_icon_test.cc

with WIL so as not to depend on WTL (#861).

This is a mechanical code change. There must be no observable behavior
change.

#codehealth

PiperOrigin-RevId: 599687642
  • Loading branch information
yukawa authored and hiroyuki-komatsu committed Jan 19, 2024
1 parent ca3d136 commit f9bdad2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
8 changes: 3 additions & 5 deletions src/win32/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,16 @@ mozc_cc_library(
mozc_cc_test(
name = "text_icon_test",
srcs = ["text_icon_test.cc"],
copts = copts_wtl(),
features = features_gdi(),
tags = MOZC_TAGS.WIN_ONLY,
target_compatible_with = ["@platforms//os:windows"],
deps = [
":text_icon",
"//base:logging",
"//base:mmap",
"//base:util",
"//base/win32:win_font_test_helper",
"//bazel/win32:gdi32",
"//bazel/win32:user32",
"//testing:gunit_main",
"//third_party/wtl",
"@com_microsoft_wil//:wil",
],
)

Expand Down
53 changes: 21 additions & 32 deletions src/win32/base/text_icon_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@

#include "win32/base/text_icon.h"

// clang-format off
#include <atlbase.h>
#include <atlapp.h>
#include <atlmisc.h>
#include <atlgdi.h>
// clang-format on

#include "base/logging.h"
#include "base/mmap.h"
#include "base/util.h"
#include <windows.h>
#include <wil/resource.h>

#include <cstddef>
#include <string>

#include "base/win32/win_font_test_helper.h"
#include "testing/gunit.h"

Expand All @@ -48,8 +44,6 @@ namespace {

using ::testing::AssertionFailure;
using ::testing::AssertionSuccess;
using ::WTL::CBitmap;
using ::WTL::CIcon;

class TextIconTest : public testing::Test {
public:
Expand All @@ -72,34 +66,34 @@ class TextIconTest : public testing::Test {
}
};

::testing::AssertionResult ExpectMonochromeIcon(const CIcon &icon,
::testing::AssertionResult ExpectMonochromeIcon(const wil::unique_hicon &icon,
size_t size) {
if (icon.IsNull()) {
if (!icon) {
return AssertionFailure() << "|icon| is nullptr.";
}

ICONINFO info = {};
if (!icon.GetIconInfo(&info)) {
if (::GetIconInfo(icon.get(), &info) == 0) {
return AssertionFailure() << "GetIconInfo failed.";
}
CBitmap xor_bmp = info.hbmColor;
CBitmap and_bmp = info.hbmMask;
wil::unique_hbitmap xor_bmp(info.hbmColor);
wil::unique_hbitmap and_bmp(info.hbmMask);

if (xor_bmp.IsNull()) {
if (!xor_bmp) {
return AssertionFailure()
<< "XOR bitmap (hbmColor) should not be nullptr. This icon causes a "
"GDI "
"handle leak when it is passed to ITfLangBarItemButton::GetIcon.";
}

if (and_bmp.IsNull()) {
if (!and_bmp) {
return AssertionFailure() << "AND bitmap (hbmMask) should not be nullptr.";
}

{
BITMAP xor_bmp_info = {};
if (!xor_bmp.GetBitmap(xor_bmp_info)) {
return AssertionFailure() << "GetBitmap failed.";
if (::GetObject(xor_bmp.get(), sizeof(xor_bmp_info), &xor_bmp_info) == 0) {
return AssertionFailure() << "GetObject for xor_bmp failed.";
}

// There seems no way to retrieve the true color depth of given icon object.
Expand All @@ -122,8 +116,8 @@ ::testing::AssertionResult ExpectMonochromeIcon(const CIcon &icon,

{
BITMAP and_bmp_info = {};
if (!and_bmp.GetBitmap(and_bmp_info)) {
return AssertionFailure() << "GetBitmap failed.";
if (::GetObject(and_bmp.get(), sizeof(and_bmp_info), &and_bmp_info) == 0) {
return AssertionFailure() << "GetObject for and_bmp failed.";
}

if (and_bmp_info.bmBitsPixel != 1) {
Expand All @@ -148,16 +142,11 @@ ::testing::AssertionResult ExpectMonochromeIcon(const CIcon &icon,
return AssertionSuccess();
}

#define EXPECT_MONOCHROME_ICON(icon, icon_size) \
EXPECT_TRUE(ExpectMonochromeIcon((icon), (icon_size)))

TEST_F(TextIconTest, CreateMonochromeIcon) {
{
constexpr size_t kIconSize = 20;
CIcon icon = TextIcon::CreateMonochromeIcon(
kIconSize, kIconSize, "A", GetTestFontName(), RGB(0xff, 0x00, 0xff));
EXPECT_MONOCHROME_ICON(icon, kIconSize);
}
constexpr size_t kIconSize = 20;
wil::unique_hicon icon(TextIcon::CreateMonochromeIcon(
kIconSize, kIconSize, "A", GetTestFontName(), RGB(0xff, 0x00, 0xff)));
EXPECT_TRUE(ExpectMonochromeIcon(icon, kIconSize));
}

} // namespace
Expand Down

0 comments on commit f9bdad2

Please sign in to comment.