2
0

HtmlTableTest.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_Get ()
  152. {
  153. HtmlTable t = new HtmlTable ();
  154. Assert.IsNotNull (t.InnerHtml);
  155. }
  156. [Test]
  157. [ExpectedException (typeof (NotSupportedException))]
  158. public void InnerHtml_Set ()
  159. {
  160. HtmlTable t = new HtmlTable ();
  161. t.InnerHtml = String.Empty;
  162. }
  163. [Test]
  164. [ExpectedException (typeof (NotSupportedException))]
  165. public void InnerText_Get ()
  166. {
  167. HtmlTable t = new HtmlTable ();
  168. Assert.IsNotNull (t.InnerText);
  169. }
  170. [Test]
  171. [ExpectedException (typeof (NotSupportedException))]
  172. public void InnerText_Set ()
  173. {
  174. HtmlTable t = new HtmlTable ();
  175. t.InnerText = String.Empty;
  176. }
  177. private string RemoveWS (string s)
  178. {
  179. s = s.Replace ("\t", "");
  180. s = s.Replace ("\r", "");
  181. return s.Replace ("\n", "");
  182. }
  183. [Test]
  184. public void Render_Table_Simple ()
  185. {
  186. TestHtmlTable t = new TestHtmlTable ();
  187. Assert.AreEqual (RemoveWS ("<table>\r\n</table>\r\n"), RemoveWS (t.Render ()));
  188. }
  189. [Test]
  190. public void Render_Table ()
  191. {
  192. TestHtmlTable t = new TestHtmlTable ();
  193. t.Align = "*1*";
  194. t.BgColor = "*2*";
  195. t.Border = 3;
  196. t.BorderColor = "*4*";
  197. t.CellPadding = 5;
  198. t.CellSpacing = 6;
  199. t.Height = "*7*";
  200. t.Width = "*8*";
  201. Assert.AreEqual (RemoveWS ("<table align=\"*1*\" bgcolor=\"*2*\" border=\"3\" bordercolor=\"*4*\" cellpadding=\"5\" cellspacing=\"6\" height=\"*7*\" width=\"*8*\">\r\n</table>\r\n"), RemoveWS (t.Render ()));
  202. }
  203. [Test]
  204. public void Render_TableRow_Simple ()
  205. {
  206. TestHtmlTable t = new TestHtmlTable ();
  207. t.Rows.Add (new HtmlTableRow ());
  208. Assert.AreEqual (RemoveWS ("<table>\r\n\t<tr>\r\n\t</tr>\r\n</table>\r\n"), RemoveWS (t.Render ()));
  209. }
  210. [Test]
  211. public void Render_TableRow ()
  212. {
  213. TestHtmlTable t = new TestHtmlTable ();
  214. t.Border = 0;
  215. HtmlTableRow r1 = new HtmlTableRow ();
  216. r1.Align = "right";
  217. t.Rows.Add (r1);
  218. HtmlTableRow r2 = new HtmlTableRow ();
  219. r2.Align = "left";
  220. t.Rows.Add (r2);
  221. Assert.AreEqual (RemoveWS ("<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"), RemoveWS (t.Render ()));
  222. }
  223. [Test]
  224. public void Render_TableRowCell_Simple ()
  225. {
  226. TestHtmlTable t = new TestHtmlTable ();
  227. HtmlTableRow r = new HtmlTableRow ();
  228. r.Cells.Add (new HtmlTableCell ());
  229. t.Rows.Add (r);
  230. Assert.AreEqual (RemoveWS ("<table>\r\n\t<tr>\r\n\t\t<td></td>\r\n\t</tr>\r\n</table>\r\n"),
  231. RemoveWS (t.Render ()));
  232. }
  233. [Test]
  234. public void Render_TableRowCell ()
  235. {
  236. TestHtmlTable t = new TestHtmlTable ();
  237. t.Align = "center";
  238. HtmlTableRow r = new HtmlTableRow ();
  239. r.VAlign = "top";
  240. t.Rows.Add (r);
  241. HtmlTableCell c1 = new HtmlTableCell ();
  242. c1.Align = "right";
  243. c1.InnerText = "Go";
  244. r.Cells.Add (c1);
  245. HtmlTableCell c2 = new HtmlTableCell ();
  246. c2.Align = "left";
  247. c2.InnerHtml = "<a href=\"http://www.go-mono.com\">Mono</a>";
  248. r.Cells.Add (c2);
  249. Assert.AreEqual (RemoveWS ("<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"), RemoveWS (t.Render ()));
  250. }
  251. [Test]
  252. [ExpectedException (typeof (NullReferenceException))]
  253. public void HtmlTableRowControlCollectionAdd_Null ()
  254. {
  255. TestHtmlTable t = new TestHtmlTable ();
  256. ControlCollection c = t.GetCollection ();
  257. c.Add (null);
  258. }
  259. [Test]
  260. [ExpectedException (typeof (ArgumentException))]
  261. public void HtmlTableRowControlCollectionAdd_WrongType ()
  262. {
  263. TestHtmlTable t = new TestHtmlTable ();
  264. ControlCollection c = t.GetCollection ();
  265. c.Add (new HtmlTable ());
  266. }
  267. [Test]
  268. public void HtmlTableRowControlCollectionAdd ()
  269. {
  270. TestHtmlTable t = new TestHtmlTable ();
  271. ControlCollection c = t.GetCollection ();
  272. c.Add (new HtmlTableRow ());
  273. c.Add (new InheritedHtmlTableRow ());
  274. Assert.AreEqual (2, c.Count, "Rows");
  275. }
  276. [Test]
  277. [ExpectedException (typeof (NullReferenceException))]
  278. public void HtmlTableRowControlCollectionAddAt_Null ()
  279. {
  280. TestHtmlTable t = new TestHtmlTable ();
  281. ControlCollection c = t.GetCollection ();
  282. c.AddAt (0, null);
  283. }
  284. [Test]
  285. [ExpectedException (typeof (ArgumentException))]
  286. public void HtmlTableRowControlCollectionAddAt_WrongType ()
  287. {
  288. TestHtmlTable t = new TestHtmlTable ();
  289. ControlCollection c = t.GetCollection ();
  290. c.AddAt (0, new HtmlTable ());
  291. }
  292. [Test]
  293. public void HtmlTableRowControlCollectionAddAt ()
  294. {
  295. TestHtmlTable t = new TestHtmlTable ();
  296. ControlCollection c = t.GetCollection ();
  297. c.AddAt (0, new HtmlTableRow ());
  298. c.AddAt (0, new InheritedHtmlTableRow ());
  299. Assert.AreEqual (2, c.Count, "Rows");
  300. }
  301. }
  302. }