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