forked from dotnet/winforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemFontsTests.cs
More file actions
175 lines (160 loc) · 7.67 KB
/
Copy pathSystemFontsTests.cs
File metadata and controls
175 lines (160 loc) · 7.67 KB
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
175
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Xunit;
namespace System.Drawing.Tests
{
public class SystemFontsTests
{
public static IEnumerable<object[]> SystemFonts_TestData()
{
yield return new object[] { (Func<Font>)(() => SystemFonts.CaptionFont) };
yield return new object[] { (Func<Font>)(() => SystemFonts.IconTitleFont) };
yield return new object[] { (Func<Font>)(() => SystemFonts.MenuFont) };
yield return new object[] { (Func<Font>)(() => SystemFonts.MessageBoxFont) };
yield return new object[] { (Func<Font>)(() => SystemFonts.SmallCaptionFont) };
yield return new object[] { (Func<Font>)(() => SystemFonts.StatusFont) };
}
[ConditionalTheory(Helpers.IsDrawingSupported)]
[MemberData(nameof(SystemFonts_TestData))]
public void SystemFont_Get_ReturnsExpected(Func<Font> getFont)
{
using (Font font = getFont())
using (Font otherFont = getFont())
{
Assert.NotNull(font);
Assert.NotNull(otherFont);
Assert.NotSame(font, otherFont);
// Assert.Equal on a font will use the native handle to assert equality, which is not always guaranteed.
Assert.Equal(font.Name, otherFont.Name);
}
}
public static IEnumerable<object[]> SystemFonts_WindowsNames_TestData()
{
int userLangId = GetUserDefaultLCID();
SystemFontList fonts;
switch (userLangId & 0x3ff)
{
case 0x11: // ja-JP (Japanese)
fonts = new SystemFontList("Yu Gothic UI");
break;
case 0x5C: // chr-Cher-US (Cherokee)
fonts = new SystemFontList("Gadugi");
break;
case 0x12: // ko-KR (Korean)
fonts = new SystemFontList("\ub9d1\uc740\x20\uace0\ub515");
break;
case 0x4: // zh-TW (Traditional Chinese, Taiwan) or zh-CN (Simplified Chinese, PRC)
// Although the primary language ID is the same, the fonts are different
// So we have to determine by the full language ID
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-56532684c37f
// Assuming this doc is correct AND the font only differs by whether it's traditional or not it should work
switch (userLangId & 0xFFFF)
{
case 0x0004: // zh-Hans
case 0x7804: // zh
case 0x0804: // zh-CN
case 0x1004: // zh-SG
fonts = new SystemFontList("Microsoft JhengHei UI");
break;
case 0x7C04: // zh-Hant
case 0x0C04: // zh-HK
case 0x1404: // zh-MO
case 0x0404: // zh-TW
fonts = new SystemFontList("Microsoft YaHei UI");
break;
default:
throw new InvalidOperationException("The primary language ID is Chinese, however it was not able to" +
$" determine the user locale from the LCID with value: {userLangId & 0xFFFF:X4}.");
}
break;
case 0x1E: // th-TH
case 0x54: // lo-LA
case 0x53: // km-KH
fonts = new SystemFontList("Leelawadee UI");
break;
case 0x4A: // te-IN
case 0x49: // ta-IN
case 0x5B: // si-LK
case 0x48: // or-IN
case 0x4E: // mr-IN
case 0x4C: // ml-IN
case 0x57: // kok-IN
case 0x45: // bn-BD
case 0x4D: // as-IN
fonts = new SystemFontList("Nirmala UI");
break;
case 0x5E: // am-ET
fonts = new SystemFontList("Ebrima");
break;
default: // For now we assume everything else uses Segoe UI
// If there's other failure reported we can add it
fonts = new SystemFontList("Segoe UI");
break;
}
return fonts.ToTestData();
}
[ConditionalTheory(Helpers.IsDrawingSupported)]
[MemberData(nameof(SystemFonts_WindowsNames_TestData))]
public void SystemFont_Get_ReturnsExpected_WindowsNames(Func<Font> getFont, string systemFontName, string windowsFontName)
{
using (Font font = getFont())
using (Font otherFont = getFont())
using (Font fontFromName = SystemFonts.GetFontByName(systemFontName))
{
Assert.NotSame(font, otherFont);
Assert.Equal(font, otherFont);
Assert.Equal(font, fontFromName);
Assert.Equal(systemFontName, font.SystemFontName);
// Windows 8 updated some system fonts.
if (!PlatformDetection.IsWindows7)
{
Assert.Equal(windowsFontName, font.Name);
}
}
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("captionfont")]
public void GetFontByName_NoSuchName_ReturnsNull(string systemFontName)
{
Assert.Null(SystemFonts.GetFontByName(systemFontName));
}
[DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
internal static extern int GetUserDefaultLCID();
// Do not test DefaultFont and DialogFont, as we can't reliably determine from LCID
// https://github.com/dotnet/runtime/issues/28830#issuecomment-473556522
class SystemFontList
{
public SystemFontList(string c_it_m_mb_scFonts)
{
CaptionFont = c_it_m_mb_scFonts;
IconTitleFont = c_it_m_mb_scFonts;
MenuFont = c_it_m_mb_scFonts;
MessageBoxFont = c_it_m_mb_scFonts;
SmallCaptionFont = c_it_m_mb_scFonts;
StatusFont = c_it_m_mb_scFonts;
}
public string CaptionFont { get; set; }
public string IconTitleFont { get; set; }
public string MenuFont { get; set; }
public string MessageBoxFont { get; set; }
public string SmallCaptionFont { get; set; }
public string StatusFont { get; set; }
public IEnumerable<object[]> ToTestData()
{
return new []
{
new object[] {(Func<Font>)(() => SystemFonts.CaptionFont), nameof(CaptionFont), CaptionFont},
new object[] {(Func<Font>)(() => SystemFonts.IconTitleFont), nameof(IconTitleFont), IconTitleFont},
new object[] {(Func<Font>)(() => SystemFonts.MenuFont), nameof(MenuFont), MenuFont},
new object[] {(Func<Font>)(() => SystemFonts.MessageBoxFont), nameof(MessageBoxFont), MessageBoxFont},
new object[] {(Func<Font>)(() => SystemFonts.SmallCaptionFont), nameof(SmallCaptionFont), SmallCaptionFont},
new object[] {(Func<Font>)(() => SystemFonts.StatusFont), nameof(StatusFont), StatusFont}
};
}
}
}
}