FontUnitTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 FontUnitConstructors1 ()
  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. }
  45. [Test]
  46. public void FontUnitConstructors2 ()
  47. {
  48. // Test the AsUnit values
  49. FontUnit f1 = new FontUnit (FontSize.AsUnit);
  50. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A2");
  51. Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A3");
  52. Assert.AreEqual (f1.Unit.Value, 10, "A4");
  53. }
  54. [Test]
  55. public void FontUnitConstructors3 ()
  56. {
  57. FontUnit f1 = new FontUnit (15);
  58. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A5");
  59. Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A6");
  60. Assert.AreEqual (f1.Unit.Value, 15, "A7");
  61. }
  62. [Test]
  63. public void FontUnitConstructors4 ()
  64. {
  65. // Test the string constructor: null and empty
  66. FontUnit f1 = new FontUnit (null);
  67. Assert.AreEqual (f1.Type, FontSize.NotSet, "A8");
  68. Assert.AreEqual (f1.Unit.IsEmpty, true, "A9");
  69. }
  70. [Test]
  71. public void FontUnitConstructors5 ()
  72. {
  73. FontUnit f1 = new FontUnit ("");
  74. Assert.AreEqual (f1.Type, FontSize.NotSet, "A10");
  75. Assert.AreEqual (f1.Unit.IsEmpty, true, "A11");
  76. }
  77. #if NET_2_0
  78. [Test]
  79. public void FontUnitConstructors6 ()
  80. {
  81. FontUnit f1 = new FontUnit (2.5);
  82. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A12");
  83. Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A13");
  84. Assert.AreEqual (f1.Unit.Value, 2.5, "A14");
  85. }
  86. [Test]
  87. public void FontUnitConstructors7 ()
  88. {
  89. FontUnit f1 = new FontUnit (5.0, UnitType.Percentage);
  90. Assert.AreEqual (f1.Type, FontSize.AsUnit, "A15");
  91. Assert.AreEqual (f1.Unit.Type, UnitType.Percentage, "A17");
  92. Assert.AreEqual (f1.Unit.Value, 5.0, "A18");
  93. }
  94. #endif
  95. [Test]
  96. public void FontUnitConstructors_Pixel ()
  97. {
  98. FontUnit f1 = new FontUnit ("10px");
  99. Assert.AreEqual (FontSize.AsUnit, f1.Type, "A12");
  100. Assert.AreEqual (UnitType.Pixel, f1.Unit.Type, "A13");
  101. Assert.AreEqual (10, f1.Unit.Value, "A14");
  102. Assert.AreEqual ("10px", f1.ToString (), "A15");
  103. }
  104. [Test]
  105. public void FontUnitConstructors_Point ()
  106. {
  107. FontUnit f1 = new FontUnit ("12pt");
  108. Assert.AreEqual (FontSize.AsUnit, f1.Type, "Type");
  109. Assert.AreEqual (UnitType.Point, f1.Unit.Type, "Unit.Type");
  110. Assert.AreEqual (12, f1.Unit.Value, "Unit.Value");
  111. Assert.AreEqual ("12pt", f1.ToString (), "ToString");
  112. }
  113. [Test]
  114. public void FontUnitConstructors_Enum1 ()
  115. {
  116. FontUnit fu = new FontUnit ("Large");
  117. Assert.AreEqual (FontSize.Large, fu.Type, "Large");
  118. Assert.IsTrue (fu.Unit.IsEmpty, "Large.IsEmpty");
  119. Assert.AreEqual ("Large", fu.ToString (), "Large.ToString");
  120. }
  121. [Test]
  122. public void FontUnitConstructors_Enum2 ()
  123. {
  124. FontUnit fu = new FontUnit ("Larger");
  125. Assert.AreEqual (FontSize.Larger, fu.Type, "Larger");
  126. Assert.IsTrue (fu.Unit.IsEmpty, "Larger.IsEmpty");
  127. Assert.AreEqual ("Larger", fu.ToString (), "Larger.ToString");
  128. }
  129. [Test]
  130. public void FontUnitConstructors_Enum3 ()
  131. {
  132. FontUnit fu = new FontUnit ("Medium");
  133. Assert.AreEqual (FontSize.Medium, fu.Type, "Medium");
  134. Assert.IsTrue (fu.Unit.IsEmpty, "Medium.IsEmpty");
  135. Assert.AreEqual ("Medium", fu.ToString (), "Medium.ToString");
  136. }
  137. [Test]
  138. public void FontUnitConstructors_Enum4 ()
  139. {
  140. FontUnit fu = new FontUnit ("Small");
  141. Assert.AreEqual (FontSize.Small, fu.Type, "Small");
  142. Assert.IsTrue (fu.Unit.IsEmpty, "Small.IsEmpty");
  143. Assert.AreEqual ("Small", fu.ToString (), "Small.ToString");
  144. }
  145. [Test]
  146. public void FontUnitConstructors_Enum5 ()
  147. {
  148. FontUnit fu = new FontUnit ("Smaller");
  149. Assert.AreEqual (FontSize.Smaller, fu.Type, "Smaller");
  150. Assert.IsTrue (fu.Unit.IsEmpty, "Smaller.IsEmpty");
  151. Assert.AreEqual ("Smaller", fu.ToString (), "Smaller.ToString");
  152. }
  153. [Test]
  154. public void FontUnitConstructors_Enum6 ()
  155. {
  156. FontUnit fu = new FontUnit ("XLarge");
  157. Assert.AreEqual (FontSize.XLarge, fu.Type, "XLarge");
  158. Assert.IsTrue (fu.Unit.IsEmpty, "XLarge.IsEmpty");
  159. Assert.AreEqual ("X-Large", fu.ToString (), "XLarge.ToString");
  160. }
  161. [Test]
  162. public void FontUnitConstructors_Enum7 ()
  163. {
  164. FontUnit fu = new FontUnit ("X-Large");
  165. Assert.AreEqual (FontSize.XLarge, fu.Type, "X-Large");
  166. Assert.IsTrue (fu.Unit.IsEmpty, "X-Large.IsEmpty");
  167. Assert.AreEqual ("X-Large", fu.ToString (), "X-Large.ToString");
  168. }
  169. [Test]
  170. public void FontUnitConstructors_Enum9 ()
  171. {
  172. FontUnit fu = new FontUnit ("XSmall");
  173. Assert.AreEqual (FontSize.XSmall, fu.Type, "XSmall");
  174. Assert.IsTrue (fu.Unit.IsEmpty, "XSmall.IsEmpty");
  175. Assert.AreEqual ("X-Small", fu.ToString (), "XSmall.ToString");
  176. }
  177. [Test]
  178. public void FontUnitConstructors_Enum10 ()
  179. {
  180. FontUnit fu = new FontUnit ("X-Small");
  181. Assert.AreEqual (FontSize.XSmall, fu.Type, "X-Small");
  182. Assert.IsTrue (fu.Unit.IsEmpty, "X-Small.IsEmpty");
  183. Assert.AreEqual ("X-Small", fu.ToString (), "X-Small.ToString");
  184. }
  185. [Test]
  186. public void FontUnitConstructors_Enum11 ()
  187. {
  188. FontUnit fu = new FontUnit ("XXLarge");
  189. Assert.AreEqual (FontSize.XXLarge, fu.Type, "XXLarge");
  190. Assert.IsTrue (fu.Unit.IsEmpty, "XXLarge.IsEmpty");
  191. Assert.AreEqual ("XX-Large", fu.ToString (), "XXLarge.ToString");
  192. }
  193. [Test]
  194. public void FontUnitConstructors_Enum12 ()
  195. {
  196. FontUnit fu = new FontUnit ("XX-Large");
  197. Assert.AreEqual (FontSize.XXLarge, fu.Type, "XX-Large");
  198. Assert.IsTrue (fu.Unit.IsEmpty, "XX-Large.IsEmpty");
  199. Assert.AreEqual ("XX-Large", fu.ToString (), "XX-Large.ToString");
  200. }
  201. [Test]
  202. public void FontUnitConstructors_Enum13 ()
  203. {
  204. FontUnit fu = new FontUnit ("XXSmall");
  205. Assert.AreEqual (FontSize.XXSmall, fu.Type, "XXSmall");
  206. Assert.IsTrue (fu.Unit.IsEmpty, "XXSmall.IsEmpty");
  207. Assert.AreEqual ("XX-Small", fu.ToString (), "XXSmall.ToString");
  208. }
  209. [Test]
  210. public void FontUnitConstructors_Enum14 ()
  211. {
  212. FontUnit fu = new FontUnit ("XX-Small");
  213. Assert.AreEqual (FontSize.XXSmall, fu.Type, "XX-Small");
  214. Assert.IsTrue (fu.Unit.IsEmpty, "XX-Small.IsEmpty");
  215. Assert.AreEqual ("XX-Small", fu.ToString (), "XX-Small.ToString");
  216. }
  217. [Test]
  218. public void UnitEquality ()
  219. {
  220. FontUnit u1 = new FontUnit ("1px");
  221. FontUnit u2 = new FontUnit ("2px");
  222. FontUnit t1 = new FontUnit ("1px");
  223. FontUnit c2 = new FontUnit ("2cm");
  224. Assert.AreEqual (u1 == t1, true, "U1");
  225. Assert.AreEqual (u1 != u2, true, "U2");
  226. Assert.AreEqual (u1 == u2, false, "U3");
  227. Assert.AreEqual (u1 != t1, false, "U4");
  228. // Test that its comparing the units and value
  229. Assert.AreEqual (u2 != c2, true, "U5");
  230. }
  231. [Test]
  232. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  233. public void IncorrectConstructor ()
  234. {
  235. FontUnit a = new FontUnit ((FontSize) (-1));
  236. }
  237. [Test]
  238. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  239. public void IncorrectConstructor2 ()
  240. {
  241. FontUnit a = new FontUnit ((FontSize) (FontSize.XXLarge + 1));
  242. }
  243. [Test]
  244. [ExpectedException (typeof (FormatException))]
  245. public void FontUnitConstructors_Enum_AsUnit ()
  246. {
  247. new FontUnit ("AsUnit");
  248. }
  249. [Test]
  250. [ExpectedException (typeof (FormatException))]
  251. public void FontUnitConstructors_Enum_NotSet ()
  252. {
  253. new FontUnit ("NotSet");
  254. }
  255. #if NET_2_0
  256. class MyFormatProvider : IFormatProvider
  257. {
  258. public object GetFormat (Type format_type)
  259. {
  260. return Activator.CreateInstance (format_type);
  261. }
  262. }
  263. [Test]
  264. public void FontUnit_IFormatProviderToString ()
  265. {
  266. MyFormatProvider mfp = new MyFormatProvider ();
  267. FontUnit f1 = new FontUnit (FontSize.Large);
  268. Assert.AreEqual ("Large", f1.ToString (mfp), "T1");
  269. f1 = new FontUnit (FontSize.AsUnit);
  270. Assert.AreEqual ("10pt", f1.ToString (mfp), "T2");
  271. f1 = new FontUnit (15);
  272. Assert.AreEqual ("15pt", f1.ToString (mfp), "T3");
  273. f1 = new FontUnit (null);
  274. Assert.AreEqual ("", f1.ToString (mfp), "T4");
  275. f1 = new FontUnit ("");
  276. Assert.AreEqual ("", f1.ToString (mfp), "T5");
  277. f1 = new FontUnit (2.5);
  278. Assert.AreEqual ("2.5pt", f1.ToString (mfp), "T6");
  279. f1 = new FontUnit (5.0, UnitType.Percentage);
  280. Assert.AreEqual ("5%", f1.ToString (mfp), "T7");
  281. }
  282. #endif
  283. }
  284. }