Merge pull request #1242 from esdrubal/xelement
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestFont.cs
1 //
2 // Test Font class testing unit
3 //
4 // Authors:
5 //      Jordi Mas i Hernandez, jordi@ximian.com
6 //      Peter Dennis Bartok, pbartok@novell.com
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2003 Ximian, Inc.  http://www.ximian.com
10 // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Drawing;
35 using System.Drawing.Text;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Runtime.InteropServices;
39
40 namespace MonoTests.System.Drawing{
41
42         [TestFixture]
43         public class FontTest {
44
45                 private string name;
46
47                 [TestFixtureSetUp]
48                 public void FixtureSetUp ()
49                 {
50                         try {
51                                 using (FontFamily ff = new FontFamily (GenericFontFamilies.Monospace)) {
52                                         name = ff.Name;
53                                 }
54                         }
55                         catch (ArgumentException) {
56                                 Assert.Ignore ("No font family could be found.");
57                         }
58                 }
59
60                 // Test basic Font clone, properties and contructor
61                 [Test]
62                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
63                 public void TestClone()
64                 {               
65                         Font f = new Font("Arial",12);  
66                         Font f2 = (Font) f.Clone();
67                         
68                         Assert.AreEqual (f.Bold, f2.Bold, "Bold");
69                         Assert.AreEqual (f.FontFamily, f2.FontFamily, "FontFamily");
70                         Assert.AreEqual (f.GdiCharSet, f2.GdiCharSet, "GdiCharSet");
71                         Assert.AreEqual (f.GdiVerticalFont, f2.GdiVerticalFont, "GdiVerticalFont");
72                         Assert.AreEqual (f.Height, f2.Height, "Height");
73                         Assert.AreEqual (f.Italic, f2.Italic, "Italic");
74                         Assert.AreEqual (f.Name, f2.Name, "Name");
75                         Assert.AreEqual (f.Size, f2.Size, "Size");
76                         Assert.AreEqual (f.SizeInPoints, f2.SizeInPoints, "SizeInPoints");
77                         Assert.AreEqual (f.Strikeout, f2.Strikeout, "Strikeout");
78                         Assert.AreEqual (f.Style, f2.Style, "Style");
79                         Assert.AreEqual (f.Underline, f2.Underline, "Underline");
80                         Assert.AreEqual (f.Unit, f2.Unit, "Unit");
81                 }
82
83                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
84                 class LOGFONT {
85                         public int lfHeight;
86                         public int lfWidth;
87                         public int lfEscapement;
88                         public int lfOrientation;
89                         public int lfWeight;
90                         public byte lfItalic;
91                         public byte lfUnderline;
92                         public byte lfStrikeOut;
93                         public byte lfCharSet;
94                         public byte lfOutPrecision;
95                         public byte lfClipPrecision;
96                         public byte lfQuality;
97                         public byte lfPitchAndFamily;
98                         [ MarshalAs(UnmanagedType.ByValTStr, SizeConst=32) ]
99                         public string lfFaceName;
100                 }
101
102                 [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Auto)]
103                 struct LOGFONT_STRUCT {
104                         public int lfHeight;
105                         public int lfWidth;
106                         public int lfEscapement;
107                         public int lfOrientation;
108                         public int lfWeight;
109                         public byte lfItalic;
110                         public byte lfUnderline;
111                         public byte lfStrikeOut;
112                         public byte lfCharSet;
113                         public byte lfOutPrecision;
114                         public byte lfClipPrecision;
115                         public byte lfQuality;
116                         public byte lfPitchAndFamily;
117                         [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)]
118                         public string lfFaceName;
119                 }
120
121                 [Test]
122                 [Category ("CAS")]
123                 [ExpectedException (typeof (SecurityException))]
124                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
125                 public void ToLogFont_DenyUnmanagedCode ()
126                 {
127                         Font f;
128                         LOGFONT lf;
129
130                         lf = new LOGFONT();
131                         f = new Font("Arial", 10);
132
133                         f.ToLogFont(lf);
134                 }
135
136                 [Test]
137                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
138                 public void ToLogFont_AssertUnmanagedCode ()
139                 {
140                         Font f = new Font("Arial", 10);
141                         LOGFONT lf = new LOGFONT();
142
143                         f.ToLogFont (lf);
144                         Assert.AreEqual (400, lf.lfWeight, "lfWeight");
145                         Assert.AreEqual (1, lf.lfCharSet, "lfCharSet");
146                         Assert.AreEqual (f.Name, lf.lfFaceName, "lfFaceName");
147
148                         LOGFONT_STRUCT lfs = new LOGFONT_STRUCT ();
149                         f.ToLogFont (lfs);
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");
166                 }
167
168                 [Test]
169                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
170                 [ExpectedException (typeof (ArgumentException))]
171                 public void ToLogFont_TooSmall ()
172                 {
173                         Font f = new Font ("Arial", 10);
174                         object o = new object ();
175                         f.ToLogFont (o);
176                         // no PInvoke conversion exists !?!?
177                 }
178
179                 [Test]
180                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
181                 public void ToLogFont_Int ()
182                 {
183                         Font f = new Font ("Arial", 10);
184                         int i = 1;
185                         f.ToLogFont (i);
186                         Assert.AreEqual (1, i);
187                 }
188
189                 [Test]
190                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
191         #if NET_2_0
192                 [ExpectedException (typeof (AccessViolationException))]
193         #else
194                 [ExpectedException (typeof (NullReferenceException))]
195         #endif
196                 public void ToLogFont_Null ()
197                 {
198                         Font f = new Font ("Arial", 10);
199                         f.ToLogFont (null);
200                 }
201                 [Test]
202 #if ONLY_1_1
203                 [ExpectedException (typeof (ArgumentNullException))]
204 #endif
205                 public void Font_StringNull_Float ()
206                 {
207                         string family = null;
208                         Font f = new Font (family, 12.5f);
209                         Assert.AreEqual (FontFamily.GenericSansSerif, f.FontFamily, "FontFamily");
210                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
211                         Assert.AreEqual (12.5f, f.Size, "Size");
212                         Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
213                         Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
214                 }
215
216                 [Test]
217                 public void Font_String_Float ()
218                 {
219                         Font f = new Font (name, 12.5f);
220                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
221                         Assert.IsFalse (f.Bold, "Bold");
222                         Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
223                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
224                         Assert.IsTrue (f.Height > 0, "Height");
225                         Assert.IsFalse (f.Italic, "Italic");
226                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
227                         Assert.AreEqual (12.5f, f.Size, "Size");
228                         Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
229                         Assert.IsFalse (f.Strikeout, "Strikeout");
230                         Assert.IsFalse (f.Underline, "Underline");
231                         Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
232                 }
233
234                 [Test]
235                 public void Font_String_Float_FontStyle ()
236                 {
237                         Font f = new Font (name, 12.5f, FontStyle.Bold);
238                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
239                         Assert.IsTrue (f.Bold, "Bold");
240                         Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
241                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
242                         Assert.IsTrue (f.Height > 0, "Height");
243                         Assert.IsFalse (f.Italic, "Italic");
244                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
245                         Assert.AreEqual (12.5f, f.Size, "Size");
246                         Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
247                         Assert.IsFalse (f.Strikeout, "Strikeout");
248                         Assert.IsFalse (f.Underline, "Underline");
249                         Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
250                 }
251
252                 [Test]
253                 public void Font_String_Float_FontStyle_GraphicsUnit ()
254                 {
255                         Font f = new Font (name, 12.5f, FontStyle.Italic, GraphicsUnit.Pixel);
256                         Assert.IsFalse (f.Bold, "Bold");
257                         Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
258                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
259                         Assert.IsTrue (f.Height > 0, "Height");
260                         Assert.IsTrue (f.Italic, "Italic");
261                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
262                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
263                         Assert.AreEqual (12.5f, f.Size, "Size");
264                         Assert.IsFalse (f.Strikeout, "Strikeout");
265                         Assert.IsFalse (f.Underline, "Underline");
266                         Assert.AreEqual (GraphicsUnit.Pixel, f.Unit, "Unit");
267                 }
268
269                 [Test]
270                 [ExpectedException (typeof (ArgumentException))]
271                 public void Font_String_Float_FontStyle_GraphicsUnit_Display ()
272                 {
273                         new Font (name, 12.5f, FontStyle.Italic, GraphicsUnit.Display);
274                 }
275
276                 [Test]
277                 public void Font_String_Float_FontStyle_GraphicsUnit_Byte ()
278                 {
279                         Font f = new Font (name, 12.5f, FontStyle.Strikeout, GraphicsUnit.Inch, Byte.MaxValue);
280                         Assert.IsFalse (f.Bold, "Bold");
281                         Assert.AreEqual (Byte.MaxValue, f.GdiCharSet, "GdiCharSet");
282                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
283                         Assert.IsTrue (f.Height > 0, "Height");
284                         Assert.IsFalse (f.Italic, "Italic");
285                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
286                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
287                         Assert.AreEqual (12.5f, f.Size, "Size");
288                         Assert.AreEqual (900f, f.SizeInPoints, "SizeInPoints");
289                         Assert.IsTrue (f.Strikeout, "Strikeout");
290                         Assert.IsFalse (f.Underline, "Underline");
291                         Assert.AreEqual (GraphicsUnit.Inch, f.Unit, "Unit");
292                 }
293
294                 [Test]
295                 public void Font_String_Float_FontStyle_GraphicsUnit_Byte_Bool ()
296                 {
297                         Font f = new Font (name, 12.5f, FontStyle.Underline, GraphicsUnit.Document, Byte.MinValue, true);
298                         Assert.IsFalse (f.Bold, "Bold");
299                         Assert.AreEqual (Byte.MinValue, f.GdiCharSet, "GdiCharSet");
300                         Assert.IsTrue (f.GdiVerticalFont, "GdiVerticalFont");
301                         Assert.IsTrue (f.Height > 0, "Height");
302                         Assert.IsFalse (f.Italic, "Italic");
303                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
304                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
305                         Assert.AreEqual (12.5f, f.Size, "Size");
306                         Assert.AreEqual (3f, f.SizeInPoints, "SizeInPoints");
307                         Assert.IsFalse (f.Strikeout, "Strikeout");
308                         Assert.IsTrue (f.Underline, "Underline");
309                         Assert.AreEqual (GraphicsUnit.Document, f.Unit, "Unit");
310                 }
311
312                 [Test]
313                 [ExpectedException (typeof (ArgumentNullException))]
314                 public void Font_FontFamilyNull_Float ()
315                 {
316                         FontFamily ff = null;
317                         new Font (ff, 12.5f);
318                 }
319
320                 [Test]
321                 [ExpectedException (typeof (NullReferenceException))]
322                 public void Font_FontNull_FontStyle ()
323                 {
324                         Font f = null;
325                         new Font (f, FontStyle.Bold);
326                 }
327
328                 [Test]
329                 public void Font_FontFamily_Float ()
330                 {
331                         Font f = new Font (FontFamily.GenericMonospace, 12.5f);
332                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
333                         Assert.IsFalse (f.Bold, "Bold");
334                         Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
335                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
336                         Assert.IsTrue (f.Height > 0, "Height");
337                         Assert.IsFalse (f.Italic, "Italic");
338                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
339                         Assert.AreEqual (12.5f, f.Size, "Size");
340                         Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
341                         Assert.IsFalse (f.Strikeout, "Strikeout");
342                         Assert.IsFalse (f.Underline, "Underline");
343                         Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
344                 }
345
346                 [Test]
347                 public void Font_FontFamily_Float_FontStyle ()
348                 {
349                         Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Bold);
350                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
351                         Assert.IsTrue (f.Bold, "Bold");
352                         Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
353                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
354                         Assert.IsTrue (f.Height > 0, "Height");
355                         Assert.IsFalse (f.Italic, "Italic");
356                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
357                         Assert.AreEqual (12.5f, f.Size, "Size");
358                         Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
359                         Assert.IsFalse (f.Strikeout, "Strikeout");
360                         Assert.IsFalse (f.Underline, "Underline");
361                         Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
362                 }
363
364                 [Test]
365                 public void Font_FontFamily_Float_FontStyle_GraphicsUnit ()
366                 {
367                         Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Italic, GraphicsUnit.Millimeter);
368                         Assert.IsFalse (f.Bold, "Bold");
369                         Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
370                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
371                         Assert.IsTrue (f.Height > 0, "Height");
372                         Assert.IsTrue (f.Italic, "Italic");
373                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
374                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
375                         Assert.AreEqual (12.5f, f.Size, "Size");
376                         Assert.AreEqual (35.43307f, f.SizeInPoints, "SizeInPoints");
377                         Assert.IsFalse (f.Strikeout, "Strikeout");
378                         Assert.IsFalse (f.Underline, "Underline");
379                         Assert.AreEqual (GraphicsUnit.Millimeter, f.Unit, "Unit");
380                 }
381
382                 [Test]
383                 [ExpectedException (typeof (ArgumentException))]
384                 public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Display ()
385                 {
386                         new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Italic, GraphicsUnit.Display);
387                 }
388
389                 [Test]
390                 public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Byte ()
391                 {
392                         Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Strikeout, GraphicsUnit.Inch, Byte.MaxValue);
393                         Assert.IsFalse (f.Bold, "Bold");
394                         Assert.AreEqual (Byte.MaxValue, f.GdiCharSet, "GdiCharSet");
395                         Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
396                         Assert.IsTrue (f.Height > 0, "Height");
397                         Assert.IsFalse (f.Italic, "Italic");
398                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
399                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
400                         Assert.AreEqual (12.5f, f.Size, "Size");
401                         Assert.AreEqual (900f, f.SizeInPoints, "SizeInPoints");
402                         Assert.IsTrue (f.Strikeout, "Strikeout");
403                         Assert.IsFalse (f.Underline, "Underline");
404                         Assert.AreEqual (GraphicsUnit.Inch, f.Unit, "Unit");
405                 }
406
407                 [Test]
408                 public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Byte_Bool ()
409                 {
410                         Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Underline, GraphicsUnit.Document, Byte.MinValue, true);
411                         Assert.IsFalse (f.Bold, "Bold");
412                         Assert.AreEqual (Byte.MinValue, f.GdiCharSet, "GdiCharSet");
413                         Assert.IsTrue (f.GdiVerticalFont, "GdiVerticalFont");
414                         Assert.IsTrue (f.Height > 0, "Height");
415                         Assert.IsFalse (f.Italic, "Italic");
416                         Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
417                         Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
418                         Assert.AreEqual (12.5f, f.Size, "Size");
419                         Assert.AreEqual (3f, f.SizeInPoints, "SizeInPoints");
420                         Assert.IsFalse (f.Strikeout, "Strikeout");
421                         Assert.IsTrue (f.Underline, "Underline");
422                         Assert.AreEqual (GraphicsUnit.Document, f.Unit, "Unit");
423                 }
424
425                 [Test]
426                 public void Dispose_Double ()
427                 {
428                         Font f = new Font (name, 12.5f);
429                         f.Dispose ();
430                         f.Dispose ();
431                 }
432
433                 [Test]
434                 public void Dispose_UseAfter_Works ()
435                 {
436                         Font f = new Font (name, 12.5f);
437                         string fname = f.Name;
438                         f.Dispose ();
439                         // most properties don't throw, everything seems to be cached
440                         Assert.AreEqual (fname, f.Name, "Name");
441                         Assert.AreEqual (12.5f, f.Size, "Size");
442                 }
443
444                 [Test]
445                 [ExpectedException (typeof (ArgumentException))]
446                 public void Dispose_Height ()
447                 {
448                         Font f = new Font (name, 12.5f);
449                         f.Dispose ();
450                         Assert.AreEqual (0, f.Height, "Name");
451                 }
452
453                 [Test]
454                 [ExpectedException (typeof (ArgumentException))]
455                 public void Dispose_ToLogFont ()
456                 {
457                         Font f = new Font (name, 12.5f);
458                         f.Dispose ();
459                         LOGFONT lf = new LOGFONT();
460                         f.ToLogFont (lf);
461                 }
462
463                 [Test]
464                 public void Dispose_ToLogFont_LoopCharSet ()
465                 {
466                         Font f = new Font (name, 12.5f);
467                         f.Dispose ();
468                         LOGFONT lf = new LOGFONT ();
469
470                         for (int i = Byte.MinValue; i < Byte.MaxValue; i++) {
471                                 byte b = (byte) i;
472                                 lf.lfHeight = b;
473                                 lf.lfWidth = b;
474                                 lf.lfEscapement = b;
475                                 lf.lfOrientation = b;
476                                 lf.lfWeight = b;
477                                 lf.lfItalic = b;
478                                 lf.lfUnderline = b;
479                                 lf.lfStrikeOut = b;
480                                 lf.lfCharSet = b;
481                                 lf.lfOutPrecision = b;
482                                 lf.lfClipPrecision = b;
483                                 lf.lfQuality = b;
484                                 lf.lfPitchAndFamily = b;
485                                 lf.lfFaceName = b.ToString ();
486                                 try {
487                                         f.ToLogFont (lf);
488                                 }
489                                 catch (ArgumentException) {
490                                         Assert.AreEqual (b, lf.lfHeight, "lfHeight");
491                                         Assert.AreEqual (b, lf.lfWidth, "lfWidth");
492                                         Assert.AreEqual (b, lf.lfEscapement, "lfEscapement");
493                                         Assert.AreEqual (b, lf.lfOrientation, "lfOrientation");
494                                         Assert.AreEqual (b, lf.lfWeight, "lfWeight");
495                                         Assert.AreEqual (b, lf.lfItalic, "lfItalic");
496                                         Assert.AreEqual (b, lf.lfUnderline, "lfUnderline");
497                                         Assert.AreEqual (b, lf.lfStrikeOut, "lfStrikeOut");
498                                         // special case for 0
499                                         Assert.AreEqual ((i == 0) ? (byte)1 : b, lf.lfCharSet, "lfCharSet");
500                                         Assert.AreEqual (b, lf.lfOutPrecision, "lfOutPrecision");
501                                         Assert.AreEqual (b, lf.lfClipPrecision, "lfClipPrecision");
502                                         Assert.AreEqual (b, lf.lfQuality, "lfQuality");
503                                         Assert.AreEqual (b, lf.lfPitchAndFamily, "lfPitchAndFamily");
504                                         Assert.AreEqual (b.ToString (), lf.lfFaceName, "lfFaceName");
505                                 }
506                                 catch (Exception e) {
507                                         Assert.Fail ("Unexcepted exception {0} at iteration {1}", e, i);
508                                 }
509                         }
510                 }
511
512                 [Test]
513                 [ExpectedException (typeof (ArgumentException))]
514                 public void Dispose_ToHFont ()
515                 {
516                         Font f = new Font (name, 12.5f);
517                         f.Dispose ();
518                         f.ToHfont ();
519                 }
520                 
521                 [Test]
522                 [ExpectedException (typeof (ArgumentException))]
523                 [Category ("NotWorking")]
524                 public void UnavailableStyleException ()
525                 {
526                         // Marked NotWorking because it is dependent on what fonts/styles are available
527                         // on the OS.  This test is written for Windows.
528                         Font f = new Font ("Monotype Corsiva", 8, FontStyle.Regular);
529                 }
530
531                 [Test]
532                 public void GetHeight_Float ()
533                 {
534                         using (Font f = new Font (name, 12.5f)) {
535                                 Assert.AreEqual (0, f.GetHeight (0), "0");
536                         }
537                 }
538
539                 [Test]
540                 public void GetHeight_Graphics ()
541                 {
542                         using (Bitmap bmp = new Bitmap (10, 10)) {
543                                 using (Graphics g = Graphics.FromImage (bmp)) {
544                                         using (Font f = new Font (name, 12.5f)) {
545                                                 float expected = f.GetHeight (g.DpiY);
546                                                 Assert.AreEqual (expected, f.GetHeight (g), 0.01f, "Default");
547                                                 g.ScaleTransform (2, 4);
548                                                 Assert.AreEqual (expected, f.GetHeight (g), 0.01f, "ScaleTransform");
549                                                 g.PageScale = 3;
550                                                 Assert.AreEqual (expected, f.GetHeight (g), 0.01f, "PageScale");
551                                         }
552                                 }
553                         }
554                 }
555
556                 [Test]
557                 [ExpectedException (typeof (ArgumentNullException))]
558                 public void GetHeight_Graphics_Null ()
559                 {
560                         using (Font f = new Font (name, 12.5f)) {
561                                 Assert.AreEqual (0, f.GetHeight (null), "0");
562                         }
563                 }
564
565                 [Test]
566                 public void FontUniqueHashCode ()
567                 {
568                         Font f1 = new Font ("Arial", 14);
569                         Font f2 = new Font ("Arial", 12);
570                         Font f3 = new Font (f1, FontStyle.Bold);
571
572                         Assert.IsFalse (f1.GetHashCode () == f2.GetHashCode (), "1) Fonts with different sizes should have different HashCodes");
573                         Assert.IsFalse (f1.GetHashCode () == f3.GetHashCode (), "2) Fonts with different styles should have different HashCodes");
574                 }
575
576         [Test]
577         public void GetHashCode_UnitDiffers_HashesNotEqual()
578         {
579             Font f1 = new Font("Arial", 8.25F, GraphicsUnit.Point);
580             Font f2 = new Font("Arial", 8.25F, GraphicsUnit.Pixel);
581
582             Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(),
583                 "Hashcodes should differ if _unit member differs");
584         }
585
586         [Test]
587         public void GetHashCode_NameDiffers_HashesNotEqual()
588         {
589             Font f1 = new Font("Arial", 8.25F, GraphicsUnit.Point);
590             Font f2 = new Font("Courier New", 8.25F, GraphicsUnit.Point);
591
592             Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(),
593                 "Hashcodes should differ if _name member differs");
594         }
595
596         [Test]
597         public void GetHashCode_StyleEqualsGdiCharSet_HashesNotEqual()
598         {
599             Font f1 = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
600             Font f2 = new Font("Arial", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(1)));
601
602             Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(),
603                 "Hashcodes should differ if _style member differs");
604         }
605         }
606 }