2 // Tests for System.Web.UI.WebControls.FontUnit.cs
5 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Globalization;
33 using System.Threading;
35 using System.Web.UI.WebControls;
37 using NUnit.Framework;
39 namespace MonoTests.System.Web.UI.WebControls
42 public class FontUnitTest {
45 public void FontUnitConstructors1 ()
47 FontUnit f1 = new FontUnit (FontSize.Large);
49 Assert.AreEqual (f1.Type, FontSize.Large, "A1");
50 Assert.AreEqual (f1.Unit, Unit.Empty, "A1.1");
54 public void FontUnitConstructors2 ()
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");
64 public void FontUnitConstructors3 ()
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");
73 public void FontUnitConstructors4 ()
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");
82 public void FontUnitConstructors5 ()
84 FontUnit f1 = new FontUnit ("");
85 Assert.AreEqual (f1.Type, FontSize.NotSet, "A10");
86 Assert.AreEqual (f1.Unit.IsEmpty, true, "A11");
90 public void FontUnitConstructors6 ()
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");
99 public void FontUnitConstructors7 ()
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");
108 public void FontUnitConstructors_Em ()
110 CultureInfo originalCulture = CultureInfo.CurrentCulture;
111 Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
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");
119 // restore original culture
120 Thread.CurrentThread.CurrentCulture = originalCulture;
125 public void FontUnitConstructors_Pixel ()
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");
135 public void FontUnitConstructors_Point ()
137 CultureInfo originalCulture = CultureInfo.CurrentCulture;
138 Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
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");
146 // restore original culture
147 Thread.CurrentThread.CurrentCulture = originalCulture;
152 public void FontUnitConstructors_Enum1 ()
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");
161 public void FontUnitConstructors_Enum2 ()
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");
170 public void FontUnitConstructors_Enum3 ()
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");
179 public void FontUnitConstructors_Enum4 ()
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");
188 public void FontUnitConstructors_Enum5 ()
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");
197 public void FontUnitConstructors_Enum6 ()
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");
206 public void FontUnitConstructors_Enum7 ()
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");
215 public void FontUnitConstructors_Enum9 ()
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");
224 public void FontUnitConstructors_Enum10 ()
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");
233 public void FontUnitConstructors_Enum11 ()
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");
242 public void FontUnitConstructors_Enum12 ()
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");
251 public void FontUnitConstructors_Enum13 ()
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");
260 public void FontUnitConstructors_Enum14 ()
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");
269 public void UnitEquality ()
271 FontUnit u1 = new FontUnit ("1px");
272 FontUnit u2 = new FontUnit ("2px");
273 FontUnit t1 = new FontUnit ("1px");
274 FontUnit c2 = new FontUnit ("2cm");
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");
281 // Test that its comparing the units and value
282 Assert.AreEqual (u2 != c2, true, "U5");
286 [ExpectedException (typeof (ArgumentOutOfRangeException))]
287 public void IncorrectConstructor ()
289 FontUnit a = new FontUnit ((FontSize) (-1));
293 [ExpectedException (typeof (ArgumentOutOfRangeException))]
294 public void IncorrectConstructor2 ()
296 FontUnit a = new FontUnit ((FontSize) (FontSize.XXLarge + 1));
300 [ExpectedException (typeof (FormatException))]
301 public void FontUnitConstructors_Enum_AsUnit ()
303 new FontUnit ("AsUnit");
307 [ExpectedException (typeof (FormatException))]
308 public void FontUnitConstructors_Enum_NotSet ()
310 new FontUnit ("NotSet");
313 class MyFormatProvider : IFormatProvider
315 public object GetFormat (Type format_type)
317 if (format_type.IsAssignableFrom (this.GetType ())) {
325 public void FontUnit_IFormatProviderToString ()
327 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
328 CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
331 CultureInfo ci = CultureInfo.GetCultureInfo ("en-US");
332 Thread.CurrentThread.CurrentCulture = ci;
333 Thread.CurrentThread.CurrentUICulture = ci;
334 RunFontUnit_IFormatProviderToString_Tests ();
336 Thread.CurrentThread.CurrentCulture = currentCulture;
337 Thread.CurrentThread.CurrentUICulture = currentUICulture;
341 void RunFontUnit_IFormatProviderToString_Tests ()
343 MyFormatProvider mfp = new MyFormatProvider ();
345 FontUnit f1 = new FontUnit (FontSize.Large);
346 Assert.AreEqual ("Large", f1.ToString (mfp), "T1");
348 f1 = new FontUnit (FontSize.AsUnit);
349 Assert.AreEqual ("10pt", f1.ToString (mfp), "T2");
351 f1 = new FontUnit (15);
352 Assert.AreEqual ("15pt", f1.ToString (mfp), "T3");
354 f1 = new FontUnit (null);
355 Assert.AreEqual ("", f1.ToString (mfp), "T4");
357 f1 = new FontUnit ("");
358 Assert.AreEqual ("", f1.ToString (mfp), "T5");
360 f1 = new FontUnit (2.5);
361 Assert.AreEqual ("2.5pt", f1.ToString (mfp), "T6");
363 f1 = new FontUnit (5.0, UnitType.Percentage);
364 Assert.AreEqual ("5%", f1.ToString (mfp), "T7");