2
0

HtmlTableTest.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // HtmlTableTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlTable
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  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 System;
  30. using System.IO;
  31. using System.Web.UI;
  32. using System.Web.UI.HtmlControls;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Web.UI.HtmlControls {
  35. public class TestHtmlTable : HtmlTable {
  36. public string Render ()
  37. {
  38. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  39. base.Render (writer);
  40. return writer.InnerWriter.ToString ();
  41. }
  42. public ControlCollection GetCollection ()
  43. {
  44. return base.CreateControlCollection ();
  45. }
  46. }
  47. public class InheritedHtmlTableRow : HtmlTableRow {
  48. }
  49. [TestFixture]
  50. public class HtmlTableTest {
  51. [Test]
  52. public void DefaultProperties ()
  53. {
  54. HtmlTable t = new HtmlTable ();
  55. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
  56. Assert.AreEqual (String.Empty, t.Align, "Align");
  57. Assert.AreEqual (String.Empty, t.BgColor, "BgColor");
  58. Assert.AreEqual (-1, t.Border, "Border");
  59. Assert.AreEqual (String.Empty, t.BorderColor, "BorderColor");
  60. Assert.AreEqual (-1, t.CellPadding, "CellPadding");
  61. Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
  62. Assert.AreEqual (String.Empty, t.Height, "Height");
  63. Assert.AreEqual (0, t.Rows.Count, "Rows");
  64. Assert.AreEqual (String.Empty, t.Width, "Width");
  65. Assert.AreEqual ("table", t.TagName, "TagName");
  66. }
  67. [Test]
  68. public void NullProperties ()
  69. {
  70. HtmlTable t = new HtmlTable ();
  71. t.Align = null;
  72. Assert.AreEqual (String.Empty, t.Align, "Align");
  73. t.BgColor = null;
  74. Assert.AreEqual (String.Empty, t.BgColor, "BgColor");
  75. t.BorderColor = null;
  76. Assert.AreEqual (String.Empty, t.BorderColor, "BorderColor");
  77. t.Height = null;
  78. Assert.AreEqual (String.Empty, t.Height, "Height");
  79. t.Width = null;
  80. Assert.AreEqual (String.Empty, t.Width, "Width");
  81. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
  82. }
  83. [Test]
  84. public void EmptyProperties ()
  85. {
  86. HtmlTable t = new HtmlTable ();
  87. t.Border = -1;
  88. Assert.AreEqual (-1, t.Border, "Border");
  89. t.CellPadding = -1;
  90. Assert.AreEqual (-1, t.CellPadding, "CellPadding");
  91. t.CellSpacing = -1;
  92. Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
  93. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
  94. }
  95. [Test]
  96. public void CleanProperties ()
  97. {
  98. HtmlTable t = new HtmlTable ();
  99. t.Align = "center";
  100. Assert.AreEqual ("center", t.Align, "Align");
  101. t.Border = 1;
  102. Assert.AreEqual (1, t.Border, "Border");
  103. Assert.AreEqual (2, t.Attributes.Count, "2");
  104. t.Border = -1;
  105. Assert.AreEqual (-1, t.Border, "-Border");
  106. t.Align = null;
  107. Assert.AreEqual (String.Empty, t.Align, "-Align");
  108. Assert.AreEqual (0, t.Attributes.Count, "0");
  109. }
  110. [Test]
  111. public void MaxInt32 ()
  112. {
  113. HtmlTable t = new HtmlTable ();
  114. t.Border = Int32.MaxValue;
  115. Assert.AreEqual (Int32.MaxValue, t.Border, "Border");
  116. t.CellPadding = Int32.MaxValue;
  117. Assert.AreEqual (Int32.MaxValue, t.CellPadding, "CellPadding");
  118. t.CellSpacing = Int32.MaxValue;
  119. Assert.AreEqual (Int32.MaxValue, t.CellSpacing, "CellSpacing");
  120. Assert.AreEqual (3, t.Attributes.Count, "Attributes.Count");
  121. }
  122. [Test]
  123. public void MinInt32 ()
  124. {
  125. HtmlTable t = new HtmlTable ();
  126. t.Border = Int32.MinValue;
  127. Assert.AreEqual (Int32.MinValue, t.Border, "Border");
  128. t.CellPadding = Int32.MinValue;
  129. Assert.AreEqual (Int32.MinValue, t.CellPadding, "CellPadding");
  130. t.CellSpacing = Int32.MinValue;
  131. Assert.AreEqual (Int32.MinValue, t.CellSpacing, "CellSpacing");
  132. Assert.AreEqual (3, t.Attributes.Count, "Attributes.Count");
  133. }
  134. [Test]
  135. [ExpectedException (typeof (FormatException))]
  136. public void WrongTypeString ()
  137. {
  138. HtmlTable t = new HtmlTable ();
  139. t.Attributes.Add ("Border", "yes");
  140. Assert.AreEqual (-1, t.Border, "Border");
  141. }
  142. [Test]
  143. public void WrongTypeInt ()
  144. {
  145. HtmlTable t = new HtmlTable ();
  146. t.Border = 42;
  147. Assert.AreEqual ("42", t.Attributes ["border"], "Border");
  148. }
  149. [Test]
  150. [ExpectedException (typeof (NotSupportedException))]
  151. public void InnerHtml ()
  152. {
  153. HtmlTable t = new HtmlTable ();
  154. Assert.IsNotNull (t.InnerHtml);
  155. }
  156. [Test]
  157. [ExpectedException (typeof (NotSupportedException))]
  158. public void InnerText ()
  159. {
  160. HtmlTable t = new HtmlTable ();
  161. Assert.IsNotNull (t.InnerText);
  162. }
  163. private string AdjustLineEndings (string s)
  164. {
  165. return s.Replace ("\r\n", Environment.NewLine);
  166. }
  167. [Test]
  168. public void Render_Table_Simple ()
  169. {
  170. TestHtmlTable t = new TestHtmlTable ();
  171. Assert.AreEqual (AdjustLineEndings ("<table>\r\n</table>\r\n"), t.Render ());
  172. }
  173. [Test]
  174. public void Render_Table ()
  175. {
  176. TestHtmlTable t = new TestHtmlTable ();
  177. t.Align = "*1*";
  178. t.BgColor = "*2*";
  179. t.Border = 3;
  180. t.BorderColor = "*4*";
  181. t.CellPadding = 5;
  182. t.CellSpacing = 6;
  183. t.Height = "*7*";
  184. t.Width = "*8*";
  185. Assert.AreEqual (AdjustLineEndings ("<table align=\"*1*\" bgcolor=\"*2*\" border=\"3\" bordercolor=\"*4*\" cellpadding=\"5\" cellspacing=\"6\" height=\"*7*\" width=\"*8*\">\r\n</table>\r\n"), t.Render ());
  186. }
  187. [Test]
  188. public void Render_TableRow_Simple ()
  189. {
  190. TestHtmlTable t = new TestHtmlTable ();
  191. t.Rows.Add (new HtmlTableRow ());
  192. Assert.AreEqual (AdjustLineEndings ("<table>\r\n\t<tr>\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
  193. }
  194. [Test]
  195. public void Render_TableRow ()
  196. {
  197. TestHtmlTable t = new TestHtmlTable ();
  198. t.Border = 0;
  199. HtmlTableRow r1 = new HtmlTableRow ();
  200. r1.Align = "right";
  201. t.Rows.Add (r1);
  202. HtmlTableRow r2 = new HtmlTableRow ();
  203. r2.Align = "left";
  204. t.Rows.Add (r2);
  205. Assert.AreEqual (AdjustLineEndings ("<table border=\"0\">\r\n\t<tr align=\"right\">\r\n\t</tr>\r\n\t<tr align=\"left\">\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
  206. }
  207. [Test]
  208. public void Render_TableRowCell_Simple ()
  209. {
  210. TestHtmlTable t = new TestHtmlTable ();
  211. HtmlTableRow r = new HtmlTableRow ();
  212. r.Cells.Add (new HtmlTableCell ());
  213. t.Rows.Add (r);
  214. Assert.AreEqual (AdjustLineEndings ("<table>\r\n\t<tr>\r\n\t\t<td></td>\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
  215. }
  216. [Test]
  217. public void Render_TableRowCell ()
  218. {
  219. TestHtmlTable t = new TestHtmlTable ();
  220. t.Align = "center";
  221. HtmlTableRow r = new HtmlTableRow ();
  222. r.VAlign = "top";
  223. t.Rows.Add (r);
  224. HtmlTableCell c1 = new HtmlTableCell ();
  225. c1.Align = "right";
  226. c1.InnerText = "Go";
  227. r.Cells.Add (c1);
  228. HtmlTableCell c2 = new HtmlTableCell ();
  229. c2.Align = "left";
  230. c2.InnerHtml = "<a href=\"http://www.go-mono.com\">Mono</a>";
  231. r.Cells.Add (c2);
  232. Assert.AreEqual (AdjustLineEndings ("<table align=\"center\">\r\n\t<tr valign=\"top\">\r\n\t\t<td align=\"right\">Go</td>\r\n\t\t<td align=\"left\"><a href=\"http://www.go-mono.com\">Mono</a></td>\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
  233. }
  234. [Test]
  235. [ExpectedException (typeof (NullReferenceException))]
  236. public void HtmlTableRowControlCollectionAdd_Null ()
  237. {
  238. TestHtmlTable t = new TestHtmlTable ();
  239. ControlCollection c = t.GetCollection ();
  240. c.Add (null);
  241. }
  242. [Test]
  243. [ExpectedException (typeof (ArgumentException))]
  244. public void HtmlTableRowControlCollectionAdd_WrongType ()
  245. {
  246. TestHtmlTable t = new TestHtmlTable ();
  247. ControlCollection c = t.GetCollection ();
  248. c.Add (new HtmlTable ());
  249. }
  250. [Test]
  251. public void HtmlTableRowControlCollectionAdd ()
  252. {
  253. TestHtmlTable t = new TestHtmlTable ();
  254. ControlCollection c = t.GetCollection ();
  255. c.Add (new HtmlTableRow ());
  256. c.Add (new InheritedHtmlTableRow ());
  257. Assert.AreEqual (2, c.Count, "Rows");
  258. }
  259. [Test]
  260. [ExpectedException (typeof (NullReferenceException))]
  261. public void HtmlTableRowControlCollectionAddAt_Null ()
  262. {
  263. TestHtmlTable t = new TestHtmlTable ();
  264. ControlCollection c = t.GetCollection ();
  265. c.AddAt (0, null);
  266. }
  267. [Test]
  268. [ExpectedException (typeof (ArgumentException))]
  269. public void HtmlTableRowControlCollectionAddAt_WrongType ()
  270. {
  271. TestHtmlTable t = new TestHtmlTable ();
  272. ControlCollection c = t.GetCollection ();
  273. c.AddAt (0, new HtmlTable ());
  274. }
  275. [Test]
  276. public void HtmlTableRowControlCollectionAddAt ()
  277. {
  278. TestHtmlTable t = new TestHtmlTable ();
  279. ControlCollection c = t.GetCollection ();
  280. c.AddAt (0, new HtmlTableRow ());
  281. c.AddAt (0, new InheritedHtmlTableRow ());
  282. Assert.AreEqual (2, c.Count, "Rows");
  283. }
  284. }
  285. }