[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / FontUnitTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.FontUnit.cs 
3 //
4 // Author:
5 //      Miguel de Icaza (miguel@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Globalization;
33 using System.Threading;
34 using System.Web;
35 using System.Web.UI.WebControls;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Web.UI.WebControls
40 {
41         [TestFixture]   
42         public class FontUnitTest {
43
44                 [Test]
45                 public void FontUnitConstructors1 ()
46                 {
47                         FontUnit f1 = new FontUnit (FontSize.Large);
48                         
49                         Assert.AreEqual (f1.Type, FontSize.Large, "A1");
50                         Assert.AreEqual (f1.Unit, Unit.Empty, "A1.1");
51                 }
52
53                 [Test]
54                 public void FontUnitConstructors2 ()
55                 {
56                         // Test the AsUnit values
57                         FontUnit f1 = new FontUnit (FontSize.AsUnit);
58                         Assert.AreEqual (f1.Type, FontSize.AsUnit, "A2");
59                         Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A3");
60                         Assert.AreEqual (f1.Unit.Value, 10, "A4");
61                 }
62
63                 [Test]
64                 public void FontUnitConstructors3 ()
65                 {
66                         FontUnit f1 = new FontUnit (15);
67                         Assert.AreEqual (f1.Type, FontSize.AsUnit, "A5");
68                         Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A6");
69                         Assert.AreEqual (f1.Unit.Value, 15, "A7");
70                 }
71
72                 [Test]
73                 public void FontUnitConstructors4 ()
74                 {
75                         // Test the string constructor: null and empty
76                         FontUnit f1 = new FontUnit (null);
77                         Assert.AreEqual (f1.Type, FontSize.NotSet, "A8");
78                         Assert.AreEqual (f1.Unit.IsEmpty, true, "A9");
79                 }
80
81                 [Test]
82                 public void FontUnitConstructors5 ()
83                 {
84                         FontUnit f1 = new FontUnit ("");
85                         Assert.AreEqual (f1.Type, FontSize.NotSet, "A10");
86                         Assert.AreEqual (f1.Unit.IsEmpty, true, "A11");
87                 }
88
89                 [Test]
90                 public void FontUnitConstructors6 ()
91                 {
92                         FontUnit f1 = new FontUnit (2.5);
93                         Assert.AreEqual (f1.Type, FontSize.AsUnit, "A12");
94                         Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A13");
95                         Assert.AreEqual (f1.Unit.Value, 2.5, "A14");
96                 }
97
98                 [Test]
99                 public void FontUnitConstructors7 ()
100                 {
101                         FontUnit f1 = new FontUnit (5.0, UnitType.Percentage);
102                         Assert.AreEqual (f1.Type, FontSize.AsUnit, "A15");
103                         Assert.AreEqual (f1.Unit.Type, UnitType.Percentage, "A17");
104                         Assert.AreEqual (f1.Unit.Value, 5.0, "A18");
105                 }
106
107                 [Test]
108                 public void FontUnitConstructors_Em ()
109                 {
110                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
111                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
112                         try {
113                                 FontUnit fu = new FontUnit ("4,5em");
114                                 Assert.AreEqual (FontSize.AsUnit, fu.Type, "#1");
115                                 Assert.AreEqual (UnitType.Em, fu.Unit.Type, "#2");
116                                 Assert.AreEqual (4.5, fu.Unit.Value, "#3");
117                                 Assert.AreEqual ("4,5em", fu.ToString (), "#4");
118                         } finally {
119                                 // restore original culture
120                                 Thread.CurrentThread.CurrentCulture = originalCulture;
121                         }
122                 }
123
124                 [Test]
125                 public void FontUnitConstructors_Pixel ()
126                 {
127                         FontUnit f1 = new FontUnit ("10px");
128                         Assert.AreEqual (FontSize.AsUnit, f1.Type, "#1");
129                         Assert.AreEqual (UnitType.Pixel, f1.Unit.Type, "#2");
130                         Assert.AreEqual (10, f1.Unit.Value, "#3");
131                         Assert.AreEqual ("10px", f1.ToString (), "#4");
132                 }
133
134                 [Test]
135                 public void FontUnitConstructors_Point ()
136                 {
137                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
138                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
139                         try {
140                                 FontUnit f1 = new FontUnit ("12,5pt");
141                                 Assert.AreEqual (FontSize.AsUnit, f1.Type, "Type");
142                                 Assert.AreEqual (UnitType.Point, f1.Unit.Type, "Unit.Type");
143                                 Assert.AreEqual (12.5, f1.Unit.Value, "Unit.Value");
144                                 Assert.AreEqual ("12,5pt", f1.ToString (), "ToString");
145                         } finally {
146                                 // restore original culture
147                                 Thread.CurrentThread.CurrentCulture = originalCulture;
148                         }
149                 }
150
151                 [Test]
152                 public void FontUnitConstructors_Enum1 ()
153                 {
154                         FontUnit fu = new FontUnit ("Large");
155                         Assert.AreEqual (FontSize.Large, fu.Type, "Large");
156                         Assert.IsTrue (fu.Unit.IsEmpty, "Large.IsEmpty");
157                         Assert.AreEqual ("Large", fu.ToString (), "Large.ToString");
158                 }
159
160                 [Test]
161                 public void FontUnitConstructors_Enum2 ()
162                 {
163                         FontUnit fu = new FontUnit ("Larger");
164                         Assert.AreEqual (FontSize.Larger, fu.Type, "Larger");
165                         Assert.IsTrue (fu.Unit.IsEmpty, "Larger.IsEmpty");
166                         Assert.AreEqual ("Larger", fu.ToString (), "Larger.ToString");
167                 }
168
169                 [Test]
170                 public void FontUnitConstructors_Enum3 ()
171                 {
172                         FontUnit fu = new FontUnit ("Medium");
173                         Assert.AreEqual (FontSize.Medium, fu.Type, "Medium");
174                         Assert.IsTrue (fu.Unit.IsEmpty, "Medium.IsEmpty");
175                         Assert.AreEqual ("Medium", fu.ToString (), "Medium.ToString");
176                 }
177
178                 [Test]
179                 public void FontUnitConstructors_Enum4 ()
180                 {
181                         FontUnit fu = new FontUnit ("Small");
182                         Assert.AreEqual (FontSize.Small, fu.Type, "Small");
183                         Assert.IsTrue (fu.Unit.IsEmpty, "Small.IsEmpty");
184                         Assert.AreEqual ("Small", fu.ToString (), "Small.ToString");
185                 }
186
187                 [Test]
188                 public void FontUnitConstructors_Enum5 ()
189                 {
190                         FontUnit fu = new FontUnit ("Smaller");
191                         Assert.AreEqual (FontSize.Smaller, fu.Type, "Smaller");
192                         Assert.IsTrue (fu.Unit.IsEmpty, "Smaller.IsEmpty");
193                         Assert.AreEqual ("Smaller", fu.ToString (), "Smaller.ToString");
194                 }
195
196                 [Test]
197                 public void FontUnitConstructors_Enum6 ()
198                 {
199                         FontUnit fu = new FontUnit ("XLarge");
200                         Assert.AreEqual (FontSize.XLarge, fu.Type, "XLarge");
201                         Assert.IsTrue (fu.Unit.IsEmpty, "XLarge.IsEmpty");
202                         Assert.AreEqual ("X-Large", fu.ToString (), "XLarge.ToString");
203                 }
204
205                 [Test]
206                 public void FontUnitConstructors_Enum7 ()
207                 {
208                         FontUnit fu = new FontUnit ("X-Large");
209                         Assert.AreEqual (FontSize.XLarge, fu.Type, "X-Large");
210                         Assert.IsTrue (fu.Unit.IsEmpty, "X-Large.IsEmpty");
211                         Assert.AreEqual ("X-Large", fu.ToString (), "X-Large.ToString");
212                 }
213
214                 [Test]
215                 public void FontUnitConstructors_Enum9 ()
216                 {
217                         FontUnit fu = new FontUnit ("XSmall");
218                         Assert.AreEqual (FontSize.XSmall, fu.Type, "XSmall");
219                         Assert.IsTrue (fu.Unit.IsEmpty, "XSmall.IsEmpty");
220                         Assert.AreEqual ("X-Small", fu.ToString (), "XSmall.ToString");
221                 }
222
223                 [Test]
224                 public void FontUnitConstructors_Enum10 ()
225                 {
226                         FontUnit fu = new FontUnit ("X-Small");
227                         Assert.AreEqual (FontSize.XSmall, fu.Type, "X-Small");
228                         Assert.IsTrue (fu.Unit.IsEmpty, "X-Small.IsEmpty");
229                         Assert.AreEqual ("X-Small", fu.ToString (), "X-Small.ToString");
230                 }
231
232                 [Test]
233                 public void FontUnitConstructors_Enum11 ()
234                 {
235                         FontUnit fu = new FontUnit ("XXLarge");
236                         Assert.AreEqual (FontSize.XXLarge, fu.Type, "XXLarge");
237                         Assert.IsTrue (fu.Unit.IsEmpty, "XXLarge.IsEmpty");
238                         Assert.AreEqual ("XX-Large", fu.ToString (), "XXLarge.ToString");
239                 }
240
241                 [Test]
242                 public void FontUnitConstructors_Enum12 ()
243                 {
244                         FontUnit fu = new FontUnit ("XX-Large");
245                         Assert.AreEqual (FontSize.XXLarge, fu.Type, "XX-Large");
246                         Assert.IsTrue (fu.Unit.IsEmpty, "XX-Large.IsEmpty");
247                         Assert.AreEqual ("XX-Large", fu.ToString (), "XX-Large.ToString");
248                 }
249
250                 [Test]
251                 public void FontUnitConstructors_Enum13 ()
252                 {
253                         FontUnit fu = new FontUnit ("XXSmall");
254                         Assert.AreEqual (FontSize.XXSmall, fu.Type, "XXSmall");
255                         Assert.IsTrue (fu.Unit.IsEmpty, "XXSmall.IsEmpty");
256                         Assert.AreEqual ("XX-Small", fu.ToString (), "XXSmall.ToString");
257                 }
258
259                 [Test]
260                 public void FontUnitConstructors_Enum14 ()
261                 {
262                         FontUnit fu = new FontUnit ("XX-Small");
263                         Assert.AreEqual (FontSize.XXSmall, fu.Type, "XX-Small");
264                         Assert.IsTrue (fu.Unit.IsEmpty, "XX-Small.IsEmpty");
265                         Assert.AreEqual ("XX-Small", fu.ToString (), "XX-Small.ToString");
266                 }
267
268                 [Test]
269                 public void UnitEquality ()
270                 {
271                         FontUnit u1 = new FontUnit ("1px");
272                         FontUnit u2 = new FontUnit ("2px");
273                         FontUnit t1 = new FontUnit ("1px");
274                         FontUnit c2 = new FontUnit ("2cm");
275
276                         Assert.AreEqual (u1 == t1, true, "U1");
277                         Assert.AreEqual (u1 != u2, true, "U2");
278                         Assert.AreEqual (u1 == u2, false, "U3");
279                         Assert.AreEqual (u1 != t1, false, "U4");
280
281                         // Test that its comparing the units and value
282                         Assert.AreEqual (u2 != c2, true, "U5");
283                 }
284
285                 [Test]
286                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
287                 public void IncorrectConstructor ()
288                 {
289                         FontUnit a = new FontUnit ((FontSize) (-1));
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
294                 public void IncorrectConstructor2 ()
295                 {
296                         FontUnit a = new FontUnit ((FontSize) (FontSize.XXLarge + 1));
297                 }
298
299                 [Test]
300                 [ExpectedException (typeof (FormatException))]
301                 public void FontUnitConstructors_Enum_AsUnit ()
302                 {
303                         new FontUnit ("AsUnit");
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (FormatException))]
308                 public void FontUnitConstructors_Enum_NotSet ()
309                 {
310                         new FontUnit ("NotSet");
311                 }
312
313                 class MyFormatProvider : IFormatProvider
314                 {
315                         public object GetFormat (Type format_type)
316                         {
317                                 if (format_type.IsAssignableFrom (this.GetType ())) {
318                                         return this;
319                                 }
320                                 return null;
321                         }
322                 }
323
324                 [Test]
325                 public void FontUnit_IFormatProviderToString ()
326                 {
327                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
328                         CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
329
330                         try {
331                                 CultureInfo ci = CultureInfo.GetCultureInfo ("en-US");
332                                 Thread.CurrentThread.CurrentCulture = ci;
333                                 Thread.CurrentThread.CurrentUICulture = ci;
334                                 RunFontUnit_IFormatProviderToString_Tests ();
335                         } finally {
336                                 Thread.CurrentThread.CurrentCulture = currentCulture;
337                                 Thread.CurrentThread.CurrentUICulture = currentUICulture;
338                         }
339                 }
340
341                 void RunFontUnit_IFormatProviderToString_Tests ()
342                 {
343                         MyFormatProvider mfp = new MyFormatProvider ();
344
345                         FontUnit f1 = new FontUnit (FontSize.Large);
346                         Assert.AreEqual ("Large", f1.ToString (mfp), "T1");
347
348                         f1 = new FontUnit (FontSize.AsUnit);
349                         Assert.AreEqual ("10pt", f1.ToString (mfp), "T2");
350
351                         f1 = new FontUnit (15);
352                         Assert.AreEqual ("15pt", f1.ToString (mfp), "T3");
353
354                         f1 = new FontUnit (null);
355                         Assert.AreEqual ("", f1.ToString (mfp), "T4");
356
357                         f1 = new FontUnit ("");
358                         Assert.AreEqual ("", f1.ToString (mfp), "T5");
359
360                         f1 = new FontUnit (2.5);
361                         Assert.AreEqual ("2.5pt", f1.ToString (mfp), "T6");
362
363                         f1 = new FontUnit (5.0, UnitType.Percentage);
364                         Assert.AreEqual ("5%", f1.ToString (mfp), "T7");
365                 }
366         }
367 }