FontUnitTest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // Tests for System.Web.UI.WebControls.FontUnit.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Globalization;
  32. using System.Web;
  33. using System.Web.UI.WebControls;
  34. namespace MonoTests.System.Web.UI.WebControls
  35. {
  36. [TestFixture]
  37. public class FontUnitTest {
  38. [Test]
  39. public void FontUnitConstructors ()
  40. {
  41. FontUnit f1 = new FontUnit (FontSize.Large);
  42. Assert.AreEqual (f1.Type, FontSize.Large, "A1");
  43. Assert.AreEqual (f1.Unit, Unit.Empty, "A1.1");
  44. // Test the AsUnit values
  45. f1 = new FontUnit (FontSize.AsUnit);
  46. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A2");
  47. Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A3");
  48. Assert.AreEqual (f1.Unit.Value, 10, "A4");
  49. f1 = new FontUnit (15);
  50. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A5");
  51. Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A6");
  52. Assert.AreEqual (f1.Unit.Value, 15, "A7");
  53. // Test the string constructor: null and empty
  54. f1 = new FontUnit (null);
  55. Assert.AreEqual (f1.Type, FontSize.NotSet, "A8");
  56. Assert.AreEqual (f1.Unit.IsEmpty, true, "A9");
  57. f1 = new FontUnit ("");
  58. Assert.AreEqual (f1.Type, FontSize.NotSet, "A10");
  59. Assert.AreEqual (f1.Unit.IsEmpty, true, "A11");
  60. #if NET_2_0
  61. f1 = new FontUnit (2.5);
  62. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A12");
  63. Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A13");
  64. Assert.AreEqual (f1.Unit.Value, 2.5, "A14");
  65. f1 = new FontUnit (5.0, UnitType.Percentage);
  66. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A15");
  67. Assert.AreEqual (f1.Unit.Type, UnitType.Percentage, "A17");
  68. Assert.AreEqual (f1.Unit.Value, 5.0, "A18");
  69. #endif
  70. }
  71. [Test]
  72. public void FontUnitConstructors_Pixel ()
  73. {
  74. FontUnit f1 = new FontUnit ("10px");
  75. Assert.AreEqual (FontSize.AsUnit, f1.Type, "A12");
  76. Assert.AreEqual (UnitType.Pixel, f1.Unit.Type, "A13");
  77. Assert.AreEqual (10, f1.Unit.Value, "A14");
  78. Assert.AreEqual ("10px", f1.ToString (), "A15");
  79. }
  80. [Test]
  81. public void FontUnitConstructors_Point ()
  82. {
  83. FontUnit f1 = new FontUnit ("12pt");
  84. Assert.AreEqual (FontSize.AsUnit, f1.Type, "Type");
  85. Assert.AreEqual (UnitType.Point, f1.Unit.Type, "Unit.Type");
  86. Assert.AreEqual (12, f1.Unit.Value, "Unit.Value");
  87. Assert.AreEqual ("12pt", f1.ToString (), "ToString");
  88. }
  89. [Test]
  90. [Category ("NotWorking")] // X* ToString
  91. public void FontUnitConstructors_Enum ()
  92. {
  93. // All the enumeration values
  94. FontUnit fu = new FontUnit ("Large");
  95. Assert.AreEqual (FontSize.Large, fu.Type, "Large");
  96. Assert.IsTrue (fu.Unit.IsEmpty, "Large.IsEmpty");
  97. Assert.AreEqual ("Large", fu.ToString (), "Large.ToString");
  98. fu = new FontUnit ("Larger");
  99. Assert.AreEqual (FontSize.Larger, fu.Type, "Larger");
  100. Assert.IsTrue (fu.Unit.IsEmpty, "Larger.IsEmpty");
  101. Assert.AreEqual ("Larger", fu.ToString (), "Larger.ToString");
  102. fu = new FontUnit ("Medium");
  103. Assert.AreEqual (FontSize.Medium, fu.Type, "Medium");
  104. Assert.IsTrue (fu.Unit.IsEmpty, "Medium.IsEmpty");
  105. Assert.AreEqual ("Medium", fu.ToString (), "Medium.ToString");
  106. fu = new FontUnit ("Small");
  107. Assert.AreEqual (FontSize.Small, fu.Type, "Small");
  108. Assert.IsTrue (fu.Unit.IsEmpty, "Small.IsEmpty");
  109. Assert.AreEqual ("Small", fu.ToString (), "Small.ToString");
  110. fu = new FontUnit ("Smaller");
  111. Assert.AreEqual (FontSize.Smaller, fu.Type, "Smaller");
  112. Assert.IsTrue (fu.Unit.IsEmpty, "Smaller.IsEmpty");
  113. Assert.AreEqual ("Smaller", fu.ToString (), "Smaller.ToString");
  114. fu = new FontUnit ("XLarge");
  115. Assert.AreEqual (FontSize.XLarge, fu.Type, "XLarge");
  116. Assert.IsTrue (fu.Unit.IsEmpty, "XLarge.IsEmpty");
  117. Assert.AreEqual ("X-Large", fu.ToString (), "XLarge.ToString");
  118. fu = new FontUnit ("XSmall");
  119. Assert.AreEqual (FontSize.XSmall, fu.Type, "XSmall");
  120. Assert.IsTrue (fu.Unit.IsEmpty, "XSmall.IsEmpty");
  121. Assert.AreEqual ("X-Small", fu.ToString (), "XSmall.ToString");
  122. fu = new FontUnit ("XXLarge");
  123. Assert.AreEqual (FontSize.XXLarge, fu.Type, "XXLarge");
  124. Assert.IsTrue (fu.Unit.IsEmpty, "XXLarge.IsEmpty");
  125. Assert.AreEqual ("XX-Large", fu.ToString (), "XXLarge.ToString");
  126. fu = new FontUnit ("XXSmall");
  127. Assert.AreEqual (FontSize.XXSmall, fu.Type, "XXSmall");
  128. Assert.IsTrue (fu.Unit.IsEmpty, "XXSmall.IsEmpty");
  129. Assert.AreEqual ("XX-Small", fu.ToString (), "XXSmall.ToString");
  130. }
  131. [Test]
  132. public void UnitEquality ()
  133. {
  134. FontUnit u1 = new FontUnit ("1px");
  135. FontUnit u2 = new FontUnit ("2px");
  136. FontUnit t1 = new FontUnit ("1px");
  137. FontUnit c2 = new FontUnit ("2cm");
  138. Assert.AreEqual (u1 == t1, true, "U1");
  139. Assert.AreEqual (u1 != u2, true, "U2");
  140. Assert.AreEqual (u1 == u2, false, "U3");
  141. Assert.AreEqual (u1 != t1, false, "U4");
  142. // Test that its comparing the units and value
  143. Assert.AreEqual (u2 != c2, true, "U5");
  144. }
  145. [Test]
  146. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  147. public void IncorrectConstructor ()
  148. {
  149. FontUnit a = new FontUnit ((FontSize) (-1));
  150. }
  151. [Test]
  152. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  153. public void IncorrectConstructor2 ()
  154. {
  155. FontUnit a = new FontUnit ((FontSize) (FontSize.XXLarge + 1));
  156. }
  157. [Test]
  158. [ExpectedException (typeof (FormatException))]
  159. [Category ("NotWorking")] // wrong exception
  160. public void FontUnitConstructors_Enum_AsUnit ()
  161. {
  162. new FontUnit ("AsUnit");
  163. }
  164. [Test]
  165. [ExpectedException (typeof (FormatException))]
  166. [Category ("NotWorking")] // wrong exception
  167. public void FontUnitConstructors_Enum_NotSet ()
  168. {
  169. new FontUnit ("NotSet");
  170. }
  171. #if NET_2_0
  172. class MyFormatProvider : IFormatProvider
  173. {
  174. public object GetFormat (Type format_type)
  175. {
  176. return Activator.CreateInstance (format_type);
  177. }
  178. }
  179. [Test]
  180. public void FontUnit_IFormatProviderToString ()
  181. {
  182. MyFormatProvider mfp = new MyFormatProvider ();
  183. FontUnit f1 = new FontUnit (FontSize.Large);
  184. Assert.AreEqual ("Large", f1.ToString (mfp), "T1");
  185. f1 = new FontUnit (FontSize.AsUnit);
  186. Assert.AreEqual ("10pt", f1.ToString (mfp), "T2");
  187. f1 = new FontUnit (15);
  188. Assert.AreEqual ("15pt", f1.ToString (mfp), "T3");
  189. f1 = new FontUnit (null);
  190. Assert.AreEqual ("", f1.ToString (mfp), "T4");
  191. f1 = new FontUnit ("");
  192. Assert.AreEqual ("", f1.ToString (mfp), "T5");
  193. f1 = new FontUnit (2.5);
  194. Assert.AreEqual ("2.5pt", f1.ToString (mfp), "T6");
  195. f1 = new FontUnit (5.0, UnitType.Percentage);
  196. Assert.AreEqual ("5%", f1.ToString (mfp), "T7");
  197. }
  198. #endif
  199. }
  200. }