File: system_fonts_win_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (174 lines) | stat: -rw-r--r-- 5,852 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2019 The Chromium Authors
// 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/check_op.h"
#include "base/compiler_specific.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 = UNSAFE_TODO(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