2006-08-07 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / FontFamilyTest.cs
1 //
2 // System.Drawing.FontFamily unit tests
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Drawing;
31 using System.Drawing.Drawing2D;
32 using System.Drawing.Text;
33 using System.Security.Permissions;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Drawing {
37
38         [TestFixture]
39         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
40         public class FontFamilyTest {
41
42                 private Bitmap bitmap;
43                 private Graphics graphic;
44                 private string name;
45
46                 [TestFixtureSetUp]
47                 public void FixtureSetUp ()
48                 {
49                         bitmap = new Bitmap (10, 10);
50                         graphic = Graphics.FromImage (bitmap);
51                         using (FontFamily ff = new FontFamily (GenericFontFamilies.Monospace)) {
52                                 name = ff.Name;
53                         }
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentException))]
58                 public void FontFamily_String_Null ()
59                 {
60                         new FontFamily (null);
61                 }
62
63                 [Test]
64                 [ExpectedException (typeof (ArgumentException))]
65                 [Category ("NotWorking")] // libgdiplus/fontconfig always return something
66                 public void FontFamily_String_Empty ()
67                 {
68                         new FontFamily (String.Empty);
69                 }
70
71                 private void CheckMono (FontFamily ff)
72                 {
73                         Assert.IsTrue (ff.Equals (FontFamily.GenericMonospace), "GenericMonospace");
74 #if NET_2_0
75                         // note: Mono has this behaviour on both 1.1 and 2.0 profiles
76                         Assert.AreEqual (ff.Name.GetHashCode (), ff.GetHashCode (), "GetHashCode");
77 #endif
78                 }
79
80                 [Test]
81                 public void FontFamily_String ()
82                 {
83                         FontFamily ff = new FontFamily (name);
84                         CheckMono (ff);
85                         FontStyle style = FontStyle.Bold;
86                         Assert.AreEqual (ff.Name, ff.GetName (0), "GetName");
87                         Assert.IsTrue ((ff.GetCellAscent (style) > 0), "GetCellAscent");
88                         Assert.IsTrue ((ff.GetCellDescent (style) > 0), "GetCellDescent");
89                         Assert.IsTrue ((ff.GetEmHeight (style) > 0), "GetEmHeight");
90                         Assert.IsTrue ((ff.GetLineSpacing (style) > 0), "GetLineSpacing");
91                         Assert.IsTrue (ff.IsStyleAvailable (style), "IsStyleAvailable");
92                 }
93
94                 [Test]
95                 public void FontFamily_String_FontCollection_Null ()
96                 {
97                         FontFamily ff = new FontFamily (name, null);
98                         CheckMono (ff);
99                 }
100
101                 [Test]
102                 public void FontFamily_String_InstalledFontCollection ()
103                 {
104                         FontFamily ff = new FontFamily (name, new InstalledFontCollection ());
105                         CheckMono (ff);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentException))]
110                 public void FontFamily_String_PrivateFontCollection ()
111                 {
112                         new FontFamily (name, new PrivateFontCollection ());
113                 }
114
115                 [Test]
116                 public void FontFamily_Monospace ()
117                 {
118                         FontFamily ff = new FontFamily (GenericFontFamilies.Monospace);
119                         CheckMono (ff);
120                 }
121
122                 [Test]
123                 public void FontFamily_SansSerif ()
124                 {
125                         FontFamily ff = new FontFamily (GenericFontFamilies.SansSerif);
126                         Assert.IsTrue (ff.Equals (FontFamily.GenericSansSerif), "GenericSansSerif");
127 #if NET_2_0
128                         // note: Mono has this behaviour on both 1.1 and 2.0 profiles
129                         Assert.AreEqual (ff.Name.GetHashCode (), ff.GetHashCode (), "GetHashCode");
130 #endif
131                 }
132
133                 [Test]
134                 public void FontFamily_Serif ()
135                 {
136                         FontFamily ff = new FontFamily (GenericFontFamilies.Serif);
137                         Assert.IsTrue (ff.Equals (FontFamily.GenericSerif), "GenericSerif");
138 #if NET_2_0
139                         // note: Mono has this behaviour on both 1.1 and 2.0 profiles
140                         Assert.AreEqual (ff.Name.GetHashCode (), ff.GetHashCode (), "GetHashCode");
141 #endif
142                 }
143
144                 [Test]
145                 public void FontFamily_Invalid ()
146                 {
147                         FontFamily ff = new FontFamily ((GenericFontFamilies)Int32.MinValue);
148                         // default to Monospace
149                         Assert.IsTrue (ff.Equals (FontFamily.GenericMonospace), "GenericMonospace");
150                         CheckMono (ff);
151                 }
152
153                 [Test]
154                 public void GenericMonospace ()
155                 {
156                         FontFamily ff = FontFamily.GenericMonospace;
157                         string ts = ff.ToString ();
158                         Assert.AreEqual ('[', ts[0], "[");
159                         Assert.IsTrue ((ts.IndexOf (name) >= 0), "ToString");
160                         Assert.AreEqual (']', ts[ts.Length - 1], "]");
161                 }
162
163                 [Test]
164                 public void GenericSansSerif ()
165                 {
166                         FontFamily ff = FontFamily.GenericSansSerif;
167                         string name = ff.Name;
168                         ff.Dispose ();
169                         Assert.AreEqual (name, FontFamily.GenericSansSerif.Name, "Name");
170                 }
171
172                 [Test]
173                 public void GenericSerif ()
174                 {
175                         FontFamily ff = FontFamily.GenericSerif;
176                         string name = ff.Name;
177                         ff.Dispose ();
178                         Assert.AreEqual (name, FontFamily.GenericSerif.Name, "Name");
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (ArgumentNullException))]
183                 public void GetFamilies_Null ()
184                 {
185                         FontFamily.GetFamilies (null);
186                 }
187
188                 [Test]
189                 public void GetFamilies ()
190                 {
191                         FontFamily[] ffc = FontFamily.GetFamilies (graphic);
192                         Assert.AreEqual (ffc.Length, FontFamily.Families.Length, "Length");
193                 }
194
195                 [Test]
196                 public void Dispose_Double ()
197                 {
198                         FontFamily ff = FontFamily.GenericSerif;
199                         ff.Dispose ();
200                         ff.Dispose ();
201                 }
202
203                 [Test]
204                 [ExpectedException (typeof (ArgumentException))]
205                 public void Dispose_UseAfter ()
206                 {
207                         FontFamily ff = FontFamily.GenericMonospace;
208                         ff.Dispose ();
209                         Assert.AreEqual (name, ff.Name, "Name");
210                 }
211         }
212 }