123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- // Copyright 2019 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ui/gfx/system_fonts_win.h"
- #include <windows.h>
- #include "base/strings/utf_string_conversions.h"
- #include "build/build_config.h"
- #include "testing/gtest/include/gtest/gtest.h"
- namespace gfx {
- namespace win {
- namespace {
- class SystemFontsWinTest : public testing::Test {
- public:
- SystemFontsWinTest() = default;
- SystemFontsWinTest(const SystemFontsWinTest&) = delete;
- SystemFontsWinTest& operator=(const SystemFontsWinTest&) = delete;
- protected:
- void SetUp() override {
- #if BUILDFLAG(IS_WIN)
- // System fonts is keeping a cache of loaded system fonts. These fonts are
- // scaled based on global callbacks configured on startup. The tests in this
- // file are testing these callbacks and need to be sure we cleared the
- // global state to avoid flaky tests.
- win::ResetSystemFontsForTesting();
- #endif
- }
- };
- LOGFONT CreateLOGFONT(const wchar_t* name, LONG height) {
- LOGFONT logfont = {};
- logfont.lfHeight = height;
- auto result = wcscpy_s(logfont.lfFaceName, name);
- DCHECK_EQ(0, result);
- return logfont;
- }
- const wchar_t kSegoeUI[] = L"Segoe UI";
- const wchar_t kArial[] = L"Arial";
- } // namespace
- TEST_F(SystemFontsWinTest, AdjustFontSize) {
- EXPECT_EQ(10, gfx::win::AdjustFontSize(10, 0));
- EXPECT_EQ(-10, gfx::win::AdjustFontSize(-10, 0));
- EXPECT_EQ(8, gfx::win::AdjustFontSize(10, -2));
- EXPECT_EQ(-8, gfx::win::AdjustFontSize(-10, -2));
- EXPECT_EQ(13, gfx::win::AdjustFontSize(10, 3));
- EXPECT_EQ(-13, gfx::win::AdjustFontSize(-10, 3));
- EXPECT_EQ(1, gfx::win::AdjustFontSize(10, -9));
- EXPECT_EQ(-1, gfx::win::AdjustFontSize(-10, -9));
- EXPECT_EQ(0, gfx::win::AdjustFontSize(10, -12));
- EXPECT_EQ(0, gfx::win::AdjustFontSize(-10, -12));
- }
- TEST_F(SystemFontsWinTest, AdjustFontSize_MinimumSizeSpecified) {
- gfx::win::SetGetMinimumFontSizeCallback([] { return 1; });
- EXPECT_EQ(10, gfx::win::AdjustFontSize(10, 0));
- EXPECT_EQ(-10, gfx::win::AdjustFontSize(-10, 0));
- EXPECT_EQ(8, gfx::win::AdjustFontSize(10, -2));
- EXPECT_EQ(-8, gfx::win::AdjustFontSize(-10, -2));
- EXPECT_EQ(13, gfx::win::AdjustFontSize(10, 3));
- EXPECT_EQ(-13, gfx::win::AdjustFontSize(-10, 3));
- EXPECT_EQ(1, gfx::win::AdjustFontSize(10, -9));
- EXPECT_EQ(-1, gfx::win::AdjustFontSize(-10, -9));
- EXPECT_EQ(1, gfx::win::AdjustFontSize(10, -12));
- EXPECT_EQ(-1, gfx::win::AdjustFontSize(-10, -12));
- }
- TEST_F(SystemFontsWinTest, AdjustLOGFONT_NoAdjustment) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
- FontAdjustment adjustment;
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(-12, logfont.lfHeight);
- EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
- }
- TEST_F(SystemFontsWinTest, AdjustLOGFONT_ChangeFace) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
- FontAdjustment adjustment{kArial, 1.0};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(-12, logfont.lfHeight);
- EXPECT_STREQ(kArial, logfont.lfFaceName);
- }
- TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleDown) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
- FontAdjustment adjustment{L"", 0.5};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(-6, logfont.lfHeight);
- EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
- logfont = CreateLOGFONT(kSegoeUI, 12);
- adjustment = {L"", 0.5};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(6, logfont.lfHeight);
- EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
- }
- TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleDownWithRounding) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
- FontAdjustment adjustment{L"", 0.85};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(-9, logfont.lfHeight);
- EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
- logfont = CreateLOGFONT(kSegoeUI, 10);
- adjustment = {L"", 0.85};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(9, logfont.lfHeight);
- EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
- }
- TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleUpWithFaceChange) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
- FontAdjustment adjustment{kArial, 1.5};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(-18, logfont.lfHeight);
- EXPECT_STREQ(kArial, logfont.lfFaceName);
- logfont = CreateLOGFONT(kSegoeUI, 12);
- adjustment = {kArial, 1.5};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(18, logfont.lfHeight);
- EXPECT_STREQ(kArial, logfont.lfFaceName);
- }
- TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleUpWithRounding) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
- FontAdjustment adjustment{L"", 1.111};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(-11, logfont.lfHeight);
- EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
- logfont = CreateLOGFONT(kSegoeUI, 10);
- adjustment = {L"", 1.11};
- AdjustLOGFONTForTesting(adjustment, &logfont);
- EXPECT_EQ(11, logfont.lfHeight);
- EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
- }
- TEST_F(SystemFontsWinTest, GetFontFromLOGFONT) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
- Font font = GetFontFromLOGFONTForTesting(logfont);
- EXPECT_EQ(font.GetStyle(), Font::FontStyle::NORMAL);
- EXPECT_EQ(font.GetWeight(), Font::Weight::NORMAL);
- }
- TEST_F(SystemFontsWinTest, GetFontFromLOGFONT_WithStyle) {
- LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
- logfont.lfItalic = 1;
- logfont.lfWeight = 700;
- Font font = GetFontFromLOGFONTForTesting(logfont);
- EXPECT_EQ(font.GetStyle(), Font::FontStyle::ITALIC);
- EXPECT_EQ(font.GetWeight(), Font::Weight::BOLD);
- }
- TEST_F(SystemFontsWinTest, GetDefaultSystemFont) {
- Font system_font = GetDefaultSystemFont();
- EXPECT_EQ(base::WideToUTF8(kSegoeUI), system_font.GetFontName());
- }
- } // namespace win
- } // namespace gfx
|