system_fonts_win_unittest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2019 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. #include "ui/gfx/system_fonts_win.h"
  5. #include <windows.h>
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "build/build_config.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace gfx {
  10. namespace win {
  11. namespace {
  12. class SystemFontsWinTest : public testing::Test {
  13. public:
  14. SystemFontsWinTest() = default;
  15. SystemFontsWinTest(const SystemFontsWinTest&) = delete;
  16. SystemFontsWinTest& operator=(const SystemFontsWinTest&) = delete;
  17. protected:
  18. void SetUp() override {
  19. #if BUILDFLAG(IS_WIN)
  20. // System fonts is keeping a cache of loaded system fonts. These fonts are
  21. // scaled based on global callbacks configured on startup. The tests in this
  22. // file are testing these callbacks and need to be sure we cleared the
  23. // global state to avoid flaky tests.
  24. win::ResetSystemFontsForTesting();
  25. #endif
  26. }
  27. };
  28. LOGFONT CreateLOGFONT(const wchar_t* name, LONG height) {
  29. LOGFONT logfont = {};
  30. logfont.lfHeight = height;
  31. auto result = wcscpy_s(logfont.lfFaceName, name);
  32. DCHECK_EQ(0, result);
  33. return logfont;
  34. }
  35. const wchar_t kSegoeUI[] = L"Segoe UI";
  36. const wchar_t kArial[] = L"Arial";
  37. } // namespace
  38. TEST_F(SystemFontsWinTest, AdjustFontSize) {
  39. EXPECT_EQ(10, gfx::win::AdjustFontSize(10, 0));
  40. EXPECT_EQ(-10, gfx::win::AdjustFontSize(-10, 0));
  41. EXPECT_EQ(8, gfx::win::AdjustFontSize(10, -2));
  42. EXPECT_EQ(-8, gfx::win::AdjustFontSize(-10, -2));
  43. EXPECT_EQ(13, gfx::win::AdjustFontSize(10, 3));
  44. EXPECT_EQ(-13, gfx::win::AdjustFontSize(-10, 3));
  45. EXPECT_EQ(1, gfx::win::AdjustFontSize(10, -9));
  46. EXPECT_EQ(-1, gfx::win::AdjustFontSize(-10, -9));
  47. EXPECT_EQ(0, gfx::win::AdjustFontSize(10, -12));
  48. EXPECT_EQ(0, gfx::win::AdjustFontSize(-10, -12));
  49. }
  50. TEST_F(SystemFontsWinTest, AdjustFontSize_MinimumSizeSpecified) {
  51. gfx::win::SetGetMinimumFontSizeCallback([] { return 1; });
  52. EXPECT_EQ(10, gfx::win::AdjustFontSize(10, 0));
  53. EXPECT_EQ(-10, gfx::win::AdjustFontSize(-10, 0));
  54. EXPECT_EQ(8, gfx::win::AdjustFontSize(10, -2));
  55. EXPECT_EQ(-8, gfx::win::AdjustFontSize(-10, -2));
  56. EXPECT_EQ(13, gfx::win::AdjustFontSize(10, 3));
  57. EXPECT_EQ(-13, gfx::win::AdjustFontSize(-10, 3));
  58. EXPECT_EQ(1, gfx::win::AdjustFontSize(10, -9));
  59. EXPECT_EQ(-1, gfx::win::AdjustFontSize(-10, -9));
  60. EXPECT_EQ(1, gfx::win::AdjustFontSize(10, -12));
  61. EXPECT_EQ(-1, gfx::win::AdjustFontSize(-10, -12));
  62. }
  63. TEST_F(SystemFontsWinTest, AdjustLOGFONT_NoAdjustment) {
  64. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
  65. FontAdjustment adjustment;
  66. AdjustLOGFONTForTesting(adjustment, &logfont);
  67. EXPECT_EQ(-12, logfont.lfHeight);
  68. EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
  69. }
  70. TEST_F(SystemFontsWinTest, AdjustLOGFONT_ChangeFace) {
  71. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
  72. FontAdjustment adjustment{kArial, 1.0};
  73. AdjustLOGFONTForTesting(adjustment, &logfont);
  74. EXPECT_EQ(-12, logfont.lfHeight);
  75. EXPECT_STREQ(kArial, logfont.lfFaceName);
  76. }
  77. TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleDown) {
  78. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
  79. FontAdjustment adjustment{L"", 0.5};
  80. AdjustLOGFONTForTesting(adjustment, &logfont);
  81. EXPECT_EQ(-6, logfont.lfHeight);
  82. EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
  83. logfont = CreateLOGFONT(kSegoeUI, 12);
  84. adjustment = {L"", 0.5};
  85. AdjustLOGFONTForTesting(adjustment, &logfont);
  86. EXPECT_EQ(6, logfont.lfHeight);
  87. EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
  88. }
  89. TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleDownWithRounding) {
  90. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
  91. FontAdjustment adjustment{L"", 0.85};
  92. AdjustLOGFONTForTesting(adjustment, &logfont);
  93. EXPECT_EQ(-9, logfont.lfHeight);
  94. EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
  95. logfont = CreateLOGFONT(kSegoeUI, 10);
  96. adjustment = {L"", 0.85};
  97. AdjustLOGFONTForTesting(adjustment, &logfont);
  98. EXPECT_EQ(9, logfont.lfHeight);
  99. EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
  100. }
  101. TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleUpWithFaceChange) {
  102. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -12);
  103. FontAdjustment adjustment{kArial, 1.5};
  104. AdjustLOGFONTForTesting(adjustment, &logfont);
  105. EXPECT_EQ(-18, logfont.lfHeight);
  106. EXPECT_STREQ(kArial, logfont.lfFaceName);
  107. logfont = CreateLOGFONT(kSegoeUI, 12);
  108. adjustment = {kArial, 1.5};
  109. AdjustLOGFONTForTesting(adjustment, &logfont);
  110. EXPECT_EQ(18, logfont.lfHeight);
  111. EXPECT_STREQ(kArial, logfont.lfFaceName);
  112. }
  113. TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleUpWithRounding) {
  114. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
  115. FontAdjustment adjustment{L"", 1.111};
  116. AdjustLOGFONTForTesting(adjustment, &logfont);
  117. EXPECT_EQ(-11, logfont.lfHeight);
  118. EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
  119. logfont = CreateLOGFONT(kSegoeUI, 10);
  120. adjustment = {L"", 1.11};
  121. AdjustLOGFONTForTesting(adjustment, &logfont);
  122. EXPECT_EQ(11, logfont.lfHeight);
  123. EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
  124. }
  125. TEST_F(SystemFontsWinTest, GetFontFromLOGFONT) {
  126. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
  127. Font font = GetFontFromLOGFONTForTesting(logfont);
  128. EXPECT_EQ(font.GetStyle(), Font::FontStyle::NORMAL);
  129. EXPECT_EQ(font.GetWeight(), Font::Weight::NORMAL);
  130. }
  131. TEST_F(SystemFontsWinTest, GetFontFromLOGFONT_WithStyle) {
  132. LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
  133. logfont.lfItalic = 1;
  134. logfont.lfWeight = 700;
  135. Font font = GetFontFromLOGFONTForTesting(logfont);
  136. EXPECT_EQ(font.GetStyle(), Font::FontStyle::ITALIC);
  137. EXPECT_EQ(font.GetWeight(), Font::Weight::BOLD);
  138. }
  139. TEST_F(SystemFontsWinTest, GetDefaultSystemFont) {
  140. Font system_font = GetDefaultSystemFont();
  141. EXPECT_EQ(base::WideToUTF8(kSegoeUI), system_font.GetFontName());
  142. }
  143. } // namespace win
  144. } // namespace gfx