/
PrivateFontCollectionTests.cs
259 lines (226 loc) · 9.67 KB
/
PrivateFontCollectionTests.cs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Licensed to the .NET Foundation under one or more agreements.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Drawing.Tests;
using System.IO;
using System.Runtime.InteropServices;
using Xunit;
namespace System.Drawing.Text.Tests
{
public class PrivateFontCollectionTests
{
[ConditionalFact(Helpers.IsDrawingSupported)]
public void Ctor_Default()
{
using (var fontCollection = new PrivateFontCollection())
{
Assert.Empty(fontCollection.Families);
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_AbsolutePath_Success()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
{
return;
}
using (var fontCollection = new PrivateFontCollection())
{
fontCollection.AddFontFile(Helpers.GetTestBitmapPath("empty.file"));
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.otf"));
FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_RelativePath_Success()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
{
return;
}
using (var fontCollection = new PrivateFontCollection())
{
string relativePath = Path.Combine("fonts", "CodeNewRoman.ttf");
fontCollection.AddFontFile(relativePath);
FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_SamePathMultipleTimes_FamiliesContainsOnlyOneFont()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
{
return;
}
using (var fontCollection = new PrivateFontCollection())
{
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));
FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
}
}
[ActiveIssue(20884, TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_SameNameMultipleTimes_FamiliesContainsFirstFontOnly()
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows7)
{
return;
}
using (var fontCollection = new PrivateFontCollection())
{
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.ttf"));
fontCollection.AddFontFile(Helpers.GetTestFontPath("CodeNewRoman.otf"));
// Verify that the first file is used by checking that it contains metadata
// associated with CodeNewRoman.ttf.
const int FrenchLCID = 1036;
FontFamily fontFamily = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", fontFamily.Name);
Assert.Equal("Bonjour", fontFamily.GetName(FrenchLCID));
}
}
[ActiveIssue(20884, TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_NullFileName_ThrowsArgumentNullException()
{
using (var fontCollection = new PrivateFontCollection())
{
AssertExtensions.Throws<ArgumentNullException>("path", () => fontCollection.AddFontFile(null));
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_InvalidPath_ThrowsArgumentException()
{
using (var fontCollection = new PrivateFontCollection())
{
AssertExtensions.Throws<ArgumentException>("path", null, () => fontCollection.AddFontFile(string.Empty));
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_NoSuchFilePath_ThrowsFileNotFoundException()
{
using (var fontCollection = new PrivateFontCollection())
{
Assert.Throws<FileNotFoundException>(() => fontCollection.AddFontFile("fileName"));
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
[ActiveIssue("https://github.com/dotnet/corefx/issues/8655")]
public void AddFontFile_LongFilePath_ThrowsException()
{
using (var fontCollection = new PrivateFontCollection())
{
// Throws PathTooLongException on Desktop and FileNotFoundException elsewhere.
if (PlatformDetection.IsFullFramework)
{
Assert.Throws<PathTooLongException>(
() => fontCollection.AddFontFile(new string('a', 261)));
}
else
{
Assert.Throws<FileNotFoundException>(
() => fontCollection.AddFontFile(new string('a', 261)));
}
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_Directory_ThrowsFileNotFoundException()
{
using (var fontCollection = new PrivateFontCollection())
{
AssertExtensions.Throws<FileNotFoundException, ExternalException>(() => fontCollection.AddFontFile(AppContext.BaseDirectory));
}
}
[ActiveIssue(20884, TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddFontFile_Disposed_ThrowsArgumentException()
{
var fontCollection = new PrivateFontCollection();
fontCollection.Dispose();
AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddFontFile("fileName"));
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddMemoryFont_ValidMemory_Success()
{
using (var fontCollection = new PrivateFontCollection())
{
byte[] data = File.ReadAllBytes(Helpers.GetTestFontPath("CodeNewRoman.otf"));
IntPtr fontBuffer = Marshal.AllocCoTaskMem(data.Length);
try
{
Marshal.Copy(data, 0, fontBuffer, data.Length);
fontCollection.AddMemoryFont(fontBuffer, data.Length);
FontFamily font = Assert.Single(fontCollection.Families);
Assert.Equal("Code New Roman", font.Name);
}
finally
{
Marshal.FreeCoTaskMem(fontBuffer);
}
}
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddMemoryFont_ZeroMemory_ThrowsArgumentException()
{
using (var fontCollection = new PrivateFontCollection())
{
AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddMemoryFont(IntPtr.Zero, 100));
}
}
[ActiveIssue(20884, TestPlatforms.AnyUnix)]
[ConditionalTheory(Helpers.IsDrawingSupported)]
[InlineData(0)]
[InlineData(-1)]
public void AddMemoryFont_InvalidLength_ThrowsArgumentException(int length)
{
// GDI+ on Windows 7 incorrectly throws a FileNotFoundException.
if (PlatformDetection.IsWindows)
{
return;
}
using (var fontCollection = new PrivateFontCollection())
{
byte[] data = File.ReadAllBytes(Helpers.GetTestFontPath("CodeNewRoman.otf"));
IntPtr fontBuffer = Marshal.AllocCoTaskMem(data.Length);
try
{
Marshal.Copy(data, 0, fontBuffer, data.Length);
AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddMemoryFont(fontBuffer, length));
}
finally
{
Marshal.FreeCoTaskMem(fontBuffer);
}
}
}
[ActiveIssue(20884, TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddMemoryFont_Disposed_ThrowsArgumentException()
{
var fontCollection = new PrivateFontCollection();
fontCollection.Dispose();
AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.AddMemoryFont((IntPtr)10, 100));
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void Families_GetWhenDisposed_ThrowsArgumentException()
{
var fontCollection = new PrivateFontCollection();
fontCollection.Dispose();
AssertExtensions.Throws<ArgumentException>(null, () => fontCollection.Families);
}
[ConditionalFact(Helpers.IsDrawingSupported)]
public void Dispose_MultipleTimes_Nop()
{
var fontCollection = new PrivateFontCollection();
fontCollection.Dispose();
fontCollection.Dispose();
}
}
}