HtmlTableTest.cs 9.6 KB

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