New test.
[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                         try {
52                                 using (FontFamily ff = new FontFamily (GenericFontFamilies.Monospace)) {
53                                         name = ff.Name;
54                                 }
55                         }
56                         catch (ArgumentException) {
57                                 Assert.Ignore ("No font family could be found.");
58                         }
59                 }
60
61                 [Test]
62                 [ExpectedException (typeof (ArgumentException))]
63                 public void FontFamily_String_Null ()
64                 {
65                         new FontFamily (null);
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (ArgumentException))]
70                 [Category ("NotWorking")] // libgdiplus/fontconfig always return something
71                 public void FontFamily_String_Empty ()
72                 {
73                         new FontFamily (String.Empty);
74                 }
75
76                 private void CheckMono (FontFamily ff)
77                 {
78                         Assert.IsTrue (ff.Equals (FontFamily.GenericMonospace), "GenericMonospace");
79 #if NET_2_0
80                         // note: Mono has this behaviour on both 1.1 and 2.0 profiles
81                         Assert.AreEqual (ff.Name.GetHashCode (), ff.GetHashCode (), "GetHashCode");
82 #endif
83                 }
84
85                 [Test]
86                 public void FontFamily_String ()
87                 {
88                         FontFamily ff = new FontFamily (name);
89                         CheckMono (ff);
90                         FontStyle style = FontStyle.Bold;
91                         Assert.AreEqual (ff.Name, ff.GetName (0), "GetName");
92                         Assert.IsTrue ((ff.GetCellAscent (style) > 0), "GetCellAscent");
93                         Assert.IsTrue ((ff.GetCellDescent (style) > 0), "GetCellDescent");
94                         Assert.IsTrue ((ff.GetEmHeight (style) > 0), "GetEmHeight");
95                         Assert.IsTrue ((ff.GetLineSpacing (style) > 0), "GetLineSpacing");
96                         Assert.IsTrue (ff.IsStyleAvailable (style), "IsStyleAvailable");
97                 }
98
99                 [Test]
100                 public void FontFamily_String_FontCollection_Null ()
101                 {
102                         FontFamily ff = new FontFamily (name, null);
103                         CheckMono (ff);
104                 }
105
106                 [Test]
107                 public void FontFamily_String_InstalledFontCollection ()
108                 {
109                         FontFamily ff = new FontFamily (name, new InstalledFontCollection ());
110                         CheckMono (ff);
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentException))]
115                 public void FontFamily_String_PrivateFontCollection ()
116                 {
117                         new FontFamily (name, new PrivateFontCollection ());
118                 }
119
120                 [Test]
121                 public void FontFamily_Monospace ()
122                 {
123                         FontFamily ff = new FontFamily (GenericFontFamilies.Monospace);
124                         CheckMono (ff);
125                 }
126
127                 [Test]
128                 public void FontFamily_SansSerif ()
129                 {
130                         FontFamily ff = new FontFamily (GenericFontFamilies.SansSerif);
131                         Assert.IsTrue (ff.Equals (FontFamily.GenericSansSerif), "GenericSansSerif");
132 #if NET_2_0
133                         // note: Mono has this behaviour on both 1.1 and 2.0 profiles
134                         Assert.AreEqual (ff.Name.GetHashCode (), ff.GetHashCode (), "GetHashCode");
135 #endif
136                 }
137
138                 [Test]
139                 public void FontFamily_Serif ()
140                 {
141                         FontFamily ff = new FontFamily (GenericFontFamilies.Serif);
142                         Assert.IsTrue (ff.Equals (FontFamily.GenericSerif), "GenericSerif");
143 #if NET_2_0
144                         // note: Mono has this behaviour on both 1.1 and 2.0 profiles
145                         Assert.AreEqual (ff.Name.GetHashCode (), ff.GetHashCode (), "GetHashCode");
146 #endif
147                 }
148
149                 [Test]
150                 public void FontFamily_Invalid ()
151                 {
152                         FontFamily ff = new FontFamily ((GenericFontFamilies)Int32.MinValue);
153                         // default to Monospace
154                         Assert.IsTrue (ff.Equals (FontFamily.GenericMonospace), "GenericMonospace");
155                         CheckMono (ff);
156                 }
157
158                 [Test]
159                 public void GenericMonospace ()
160                 {
161                         FontFamily ff = FontFamily.GenericMonospace;
162                         string ts = ff.ToString ();
163                         Assert.AreEqual ('[', ts[0], "[");
164                         Assert.IsTrue ((ts.IndexOf (name) >= 0), "ToString");
165                         Assert.AreEqual (']', ts[ts.Length - 1], "]");
166                 }
167
168                 [Test]
169                 public void GenericSansSerif ()
170                 {
171                         FontFamily ff = FontFamily.GenericSansSerif;
172                         string name = ff.Name;
173                         ff.Dispose ();
174                         Assert.AreEqual (name, FontFamily.GenericSansSerif.Name, "Name");
175                 }
176
177                 [Test]
178                 public void GenericSerif ()
179                 {
180                         FontFamily ff = FontFamily.GenericSerif;
181                         string name = ff.Name;
182                         ff.Dispose ();
183                         Assert.AreEqual (name, FontFamily.GenericSerif.Name, "Name");
184                 }
185
186                 [Test]
187                 [ExpectedException (typeof (ArgumentNullException))]
188                 public void GetFamilies_Null ()
189                 {
190                         FontFamily.GetFamilies (null);
191                 }
192
193                 [Test]
194                 public void GetFamilies ()
195                 {
196                         FontFamily[] ffc = FontFamily.GetFamilies (graphic);
197                         Assert.AreEqual (ffc.Length, FontFamily.Families.Length, "Length");
198                 }
199
200                 [Test]
201                 public void Dispose_Double ()
202                 {
203                         FontFamily ff = FontFamily.GenericSerif;
204                         ff.Dispose ();
205                         ff.Dispose ();
206                 }
207
208                 [Test]
209                 [ExpectedException (typeof (ArgumentException))]
210                 public void Dispose_UseAfter ()
211                 {
212                         FontFamily ff = FontFamily.GenericMonospace;
213                         ff.Dispose ();
214                         Assert.AreEqual (name, ff.Name, "Name");
215                 }
216         }
217 }