2 // Test Font class testing unit
6 // Jordi Mas i Hernandez, jordi@ximian.com
7 // Peter Dennis Bartok, pbartok@novell.com
9 // (C) 2003 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
34 using System.Drawing.Text;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Runtime.InteropServices;
39 namespace MonoTests.System.Drawing{
42 public class FontTest {
47 public void FixtureSetUp ()
50 using (FontFamily ff = new FontFamily (GenericFontFamilies.Monospace)) {
54 catch (ArgumentException) {
55 Assert.Ignore ("No font family could be found.");
59 // Test basic Font clone, properties and contructor
61 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
62 public void TestClone()
64 Font f = new Font("Arial",12);
65 Font f2 = (Font) f.Clone();
67 Assert.AreEqual (f.Bold, f2.Bold, "Bold");
68 Assert.AreEqual (f.FontFamily, f2.FontFamily, "FontFamily");
69 Assert.AreEqual (f.GdiCharSet, f2.GdiCharSet, "GdiCharSet");
70 Assert.AreEqual (f.GdiVerticalFont, f2.GdiVerticalFont, "GdiVerticalFont");
71 Assert.AreEqual (f.Height, f2.Height, "Height");
72 Assert.AreEqual (f.Italic, f2.Italic, "Italic");
73 Assert.AreEqual (f.Name, f2.Name, "Name");
74 Assert.AreEqual (f.Size, f2.Size, "Size");
75 Assert.AreEqual (f.SizeInPoints, f2.SizeInPoints, "SizeInPoints");
76 Assert.AreEqual (f.Strikeout, f2.Strikeout, "Strikeout");
77 Assert.AreEqual (f.Style, f2.Style, "Style");
78 Assert.AreEqual (f.Underline, f2.Underline, "Underline");
79 Assert.AreEqual (f.Unit, f2.Unit, "Unit");
82 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
86 public int lfEscapement;
87 public int lfOrientation;
90 public byte lfUnderline;
91 public byte lfStrikeOut;
92 public byte lfCharSet;
93 public byte lfOutPrecision;
94 public byte lfClipPrecision;
95 public byte lfQuality;
96 public byte lfPitchAndFamily;
97 [ MarshalAs(UnmanagedType.ByValTStr, SizeConst=32) ]
98 public string lfFaceName;
101 [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Auto)]
102 struct LOGFONT_STRUCT {
105 public int lfEscapement;
106 public int lfOrientation;
108 public byte lfItalic;
109 public byte lfUnderline;
110 public byte lfStrikeOut;
111 public byte lfCharSet;
112 public byte lfOutPrecision;
113 public byte lfClipPrecision;
114 public byte lfQuality;
115 public byte lfPitchAndFamily;
116 [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)]
117 public string lfFaceName;
123 [ExpectedException (typeof (SecurityException))]
124 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
125 public void ToLogFont_DenyUnmanagedCode ()
131 f = new Font("Arial", 10);
137 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
138 public void ToLogFont_AssertUnmanagedCode ()
140 Font f = new Font("Arial", 10);
141 LOGFONT lf = new LOGFONT();
144 Assert.AreEqual (400, lf.lfWeight, "lfWeight");
145 Assert.AreEqual (1, lf.lfCharSet, "lfCharSet");
146 Assert.AreEqual (f.Name, lf.lfFaceName, "lfFaceName");
148 LOGFONT_STRUCT lfs = new LOGFONT_STRUCT ();
150 Assert.AreEqual (0, lfs.lfWeight, "struct-lfWeight");
151 Assert.AreEqual (0, lfs.lfCharSet, "struct-lfCharSet");
152 Assert.AreEqual (0, lfs.lfHeight, "struct-lfHeight");
153 Assert.AreEqual (0, lfs.lfWidth, "struct-lfWidth");
154 Assert.AreEqual (0, lfs.lfEscapement, "struct-lfEscapement");
155 Assert.AreEqual (0, lfs.lfOrientation, "struct-lfOrientation");
156 Assert.AreEqual (0, lfs.lfWeight, "struct-lfWeight");
157 Assert.AreEqual (0, lfs.lfItalic, "struct-lfItalic");
158 Assert.AreEqual (0, lfs.lfUnderline, "struct-lfUnderline");
159 Assert.AreEqual (0, lfs.lfStrikeOut, "struct-lfStrikeOut");
160 Assert.AreEqual (0, lfs.lfCharSet, "struct-lfCharSet");
161 Assert.AreEqual (0, lfs.lfOutPrecision, "struct-lfOutPrecision");
162 Assert.AreEqual (0, lfs.lfClipPrecision, "struct-lfClipPrecision");
163 Assert.AreEqual (0, lfs.lfQuality, "struct-lfQuality");
164 Assert.AreEqual (0, lfs.lfPitchAndFamily, "struct-lfPitchAndFamily");
165 Assert.IsNull (lfs.lfFaceName, "struct-lfFaceName");
169 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
170 [ExpectedException (typeof (ArgumentException))]
171 public void ToLogFont_TooSmall ()
173 Font f = new Font ("Arial", 10);
174 object o = new object ();
176 // no PInvoke conversion exists !?!?
180 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
181 public void ToLogFont_Int ()
183 Font f = new Font ("Arial", 10);
186 Assert.AreEqual (1, i);
190 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
192 [ExpectedException (typeof (AccessViolationException))]
194 [ExpectedException (typeof (NullReferenceException))]
196 public void ToLogFont_Null ()
198 Font f = new Font ("Arial", 10);
204 [ExpectedException (typeof (ArgumentNullException))]
207 [Category ("NotWorking")]
209 public void Font_StringNull_Float ()
211 string family = null;
212 Font f = new Font (family, 12.5f);
213 Assert.AreEqual (FontFamily.GenericSansSerif, f.FontFamily, "FontFamily");
214 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
215 Assert.AreEqual (12.5f, f.Size, "Size");
216 Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
217 Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
222 [Category ("NotWorking")]
224 public void Font_String_Float ()
226 Font f = new Font (name, 12.5f);
227 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
228 Assert.IsFalse (f.Bold, "Bold");
229 Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
230 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
231 Assert.IsTrue (f.Height > 0, "Height");
232 Assert.IsFalse (f.Italic, "Italic");
233 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
234 Assert.AreEqual (12.5f, f.Size, "Size");
235 Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
236 Assert.IsFalse (f.Strikeout, "Strikeout");
237 Assert.IsFalse (f.Underline, "Underline");
238 Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
243 [Category ("NotWorking")]
245 public void Font_String_Float_FontStyle ()
247 Font f = new Font (name, 12.5f, FontStyle.Bold);
248 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
249 Assert.IsTrue (f.Bold, "Bold");
250 Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
251 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
252 Assert.IsTrue (f.Height > 0, "Height");
253 Assert.IsFalse (f.Italic, "Italic");
254 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
255 Assert.AreEqual (12.5f, f.Size, "Size");
256 Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
257 Assert.IsFalse (f.Strikeout, "Strikeout");
258 Assert.IsFalse (f.Underline, "Underline");
259 Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
264 [Category ("NotWorking")]
266 public void Font_String_Float_FontStyle_GraphicsUnit ()
268 Font f = new Font (name, 12.5f, FontStyle.Italic, GraphicsUnit.Pixel);
269 Assert.IsFalse (f.Bold, "Bold");
270 Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
271 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
272 Assert.IsTrue (f.Height > 0, "Height");
273 Assert.IsTrue (f.Italic, "Italic");
274 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
275 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
276 Assert.AreEqual (12.5f, f.Size, "Size");
277 Assert.IsFalse (f.Strikeout, "Strikeout");
278 Assert.IsFalse (f.Underline, "Underline");
279 Assert.AreEqual (GraphicsUnit.Pixel, f.Unit, "Unit");
283 [ExpectedException (typeof (ArgumentException))]
285 [Category ("NotWorking")]
287 public void Font_String_Float_FontStyle_GraphicsUnit_Display ()
289 new Font (name, 12.5f, FontStyle.Italic, GraphicsUnit.Display);
294 [Category ("NotWorking")]
296 public void Font_String_Float_FontStyle_GraphicsUnit_Byte ()
298 Font f = new Font (name, 12.5f, FontStyle.Strikeout, GraphicsUnit.Inch, Byte.MaxValue);
299 Assert.IsFalse (f.Bold, "Bold");
300 Assert.AreEqual (Byte.MaxValue, f.GdiCharSet, "GdiCharSet");
301 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
302 Assert.IsTrue (f.Height > 0, "Height");
303 Assert.IsFalse (f.Italic, "Italic");
304 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
305 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
306 Assert.AreEqual (12.5f, f.Size, "Size");
307 Assert.AreEqual (900f, f.SizeInPoints, "SizeInPoints");
308 Assert.IsTrue (f.Strikeout, "Strikeout");
309 Assert.IsFalse (f.Underline, "Underline");
310 Assert.AreEqual (GraphicsUnit.Inch, f.Unit, "Unit");
315 [Category ("NotWorking")]
317 public void Font_String_Float_FontStyle_GraphicsUnit_Byte_Bool ()
319 Font f = new Font (name, 12.5f, FontStyle.Underline, GraphicsUnit.Document, Byte.MinValue, true);
320 Assert.IsFalse (f.Bold, "Bold");
321 Assert.AreEqual (Byte.MinValue, f.GdiCharSet, "GdiCharSet");
322 Assert.IsTrue (f.GdiVerticalFont, "GdiVerticalFont");
323 Assert.IsTrue (f.Height > 0, "Height");
324 Assert.IsFalse (f.Italic, "Italic");
325 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
326 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
327 Assert.AreEqual (12.5f, f.Size, "Size");
328 Assert.AreEqual (3f, f.SizeInPoints, "SizeInPoints");
329 Assert.IsFalse (f.Strikeout, "Strikeout");
330 Assert.IsTrue (f.Underline, "Underline");
331 Assert.AreEqual (GraphicsUnit.Document, f.Unit, "Unit");
335 [ExpectedException (typeof (ArgumentNullException))]
336 public void Font_FontFamilyNull_Float ()
338 FontFamily ff = null;
339 new Font (ff, 12.5f);
344 [Category ("NotWorking")]
346 public void Font_FontFamily_Float ()
348 Font f = new Font (FontFamily.GenericMonospace, 12.5f);
349 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
350 Assert.IsFalse (f.Bold, "Bold");
351 Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
352 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
353 Assert.IsTrue (f.Height > 0, "Height");
354 Assert.IsFalse (f.Italic, "Italic");
355 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
356 Assert.AreEqual (12.5f, f.Size, "Size");
357 Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
358 Assert.IsFalse (f.Strikeout, "Strikeout");
359 Assert.IsFalse (f.Underline, "Underline");
360 Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
365 [Category ("NotWorking")]
367 public void Font_FontFamily_Float_FontStyle ()
369 Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Bold);
370 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
371 Assert.IsTrue (f.Bold, "Bold");
372 Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
373 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
374 Assert.IsTrue (f.Height > 0, "Height");
375 Assert.IsFalse (f.Italic, "Italic");
376 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
377 Assert.AreEqual (12.5f, f.Size, "Size");
378 Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
379 Assert.IsFalse (f.Strikeout, "Strikeout");
380 Assert.IsFalse (f.Underline, "Underline");
381 Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
386 [Category ("NotWorking")]
388 public void Font_FontFamily_Float_FontStyle_GraphicsUnit ()
390 Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Italic, GraphicsUnit.Millimeter);
391 Assert.IsFalse (f.Bold, "Bold");
392 Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
393 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
394 Assert.IsTrue (f.Height > 0, "Height");
395 Assert.IsTrue (f.Italic, "Italic");
396 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
397 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
398 Assert.AreEqual (12.5f, f.Size, "Size");
399 Assert.AreEqual (35.43307f, f.SizeInPoints, "SizeInPoints");
400 Assert.IsFalse (f.Strikeout, "Strikeout");
401 Assert.IsFalse (f.Underline, "Underline");
402 Assert.AreEqual (GraphicsUnit.Millimeter, f.Unit, "Unit");
406 [ExpectedException (typeof (ArgumentException))]
408 [Category ("NotWorking")]
410 public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Display ()
412 new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Italic, GraphicsUnit.Display);
417 [Category ("NotWorking")]
419 public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Byte ()
421 Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Strikeout, GraphicsUnit.Inch, Byte.MaxValue);
422 Assert.IsFalse (f.Bold, "Bold");
423 Assert.AreEqual (Byte.MaxValue, f.GdiCharSet, "GdiCharSet");
424 Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
425 Assert.IsTrue (f.Height > 0, "Height");
426 Assert.IsFalse (f.Italic, "Italic");
427 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
428 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
429 Assert.AreEqual (12.5f, f.Size, "Size");
430 Assert.AreEqual (900f, f.SizeInPoints, "SizeInPoints");
431 Assert.IsTrue (f.Strikeout, "Strikeout");
432 Assert.IsFalse (f.Underline, "Underline");
433 Assert.AreEqual (GraphicsUnit.Inch, f.Unit, "Unit");
438 [Category ("NotWorking")]
440 public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Byte_Bool ()
442 Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Underline, GraphicsUnit.Document, Byte.MinValue, true);
443 Assert.IsFalse (f.Bold, "Bold");
444 Assert.AreEqual (Byte.MinValue, f.GdiCharSet, "GdiCharSet");
445 Assert.IsTrue (f.GdiVerticalFont, "GdiVerticalFont");
446 Assert.IsTrue (f.Height > 0, "Height");
447 Assert.IsFalse (f.Italic, "Italic");
448 Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
449 Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
450 Assert.AreEqual (12.5f, f.Size, "Size");
451 Assert.AreEqual (3f, f.SizeInPoints, "SizeInPoints");
452 Assert.IsFalse (f.Strikeout, "Strikeout");
453 Assert.IsTrue (f.Underline, "Underline");
454 Assert.AreEqual (GraphicsUnit.Document, f.Unit, "Unit");
458 public void Dispose_Double ()
460 Font f = new Font (name, 12.5f);
466 public void Dispose_UseAfter_Works ()
468 Font f = new Font (name, 12.5f);
469 string fname = f.Name;
471 // most properties don't throw, everything seems to be cached
472 Assert.AreEqual (fname, f.Name, "Name");
473 Assert.AreEqual (12.5f, f.Size, "Size");
477 [ExpectedException (typeof (ArgumentException))]
479 [Category ("NotWorking")]
481 public void Dispose_Height ()
483 Font f = new Font (name, 12.5f);
485 Assert.AreEqual (0, f.Height, "Name");
489 [ExpectedException (typeof (ArgumentException))]
491 [Category ("NotWorking")]
493 public void Dispose_ToLogFont ()
495 Font f = new Font (name, 12.5f);
497 LOGFONT lf = new LOGFONT();
503 [Category ("NotWorking")]
505 public void Dispose_ToLogFont_LoopCharSet ()
507 Font f = new Font (name, 12.5f);
509 LOGFONT lf = new LOGFONT ();
511 for (int i = Byte.MinValue; i < Byte.MaxValue; i++) {
516 lf.lfOrientation = b;
522 lf.lfOutPrecision = b;
523 lf.lfClipPrecision = b;
525 lf.lfPitchAndFamily = b;
526 lf.lfFaceName = b.ToString ();
530 catch (ArgumentException) {
531 Assert.AreEqual (b, lf.lfHeight, "lfHeight");
532 Assert.AreEqual (b, lf.lfWidth, "lfWidth");
533 Assert.AreEqual (b, lf.lfEscapement, "lfEscapement");
534 Assert.AreEqual (b, lf.lfOrientation, "lfOrientation");
535 Assert.AreEqual (b, lf.lfWeight, "lfWeight");
536 Assert.AreEqual (b, lf.lfItalic, "lfItalic");
537 Assert.AreEqual (b, lf.lfUnderline, "lfUnderline");
538 Assert.AreEqual (b, lf.lfStrikeOut, "lfStrikeOut");
539 // special case for 0
540 Assert.AreEqual ((i == 0) ? (byte)1 : b, lf.lfCharSet, "lfCharSet");
541 Assert.AreEqual (b, lf.lfOutPrecision, "lfOutPrecision");
542 Assert.AreEqual (b, lf.lfClipPrecision, "lfClipPrecision");
543 Assert.AreEqual (b, lf.lfQuality, "lfQuality");
544 Assert.AreEqual (b, lf.lfPitchAndFamily, "lfPitchAndFamily");
545 Assert.AreEqual (b.ToString (), lf.lfFaceName, "lfFaceName");
547 catch (Exception e) {
548 Assert.Fail ("Unexcepted exception {0} at iteration {1}", e, i);
554 [ExpectedException (typeof (ArgumentException))]
556 [Category ("NotWorking")]
558 public void Dispose_ToHFont ()
560 Font f = new Font (name, 12.5f);