TableRowTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // TableRowTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.TableRow
  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.Drawing;
  31. using System.IO;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.Web.UI.WebControls;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Web.UI.WebControls {
  37. public interface ITableRowTest {
  38. // testing
  39. string Tag { get; }
  40. StateBag StateBag { get; }
  41. string Render ();
  42. Style GetStyle ();
  43. // TableRow
  44. AttributeCollection Attributes { get; }
  45. Color BackColor { get; set; }
  46. ControlCollection Controls { get; }
  47. TableCellCollection Cells { get; }
  48. Style ControlStyle { get; }
  49. HorizontalAlign HorizontalAlign { get; set; }
  50. VerticalAlign VerticalAlign { get; set; }
  51. #if NET_2_0
  52. TableRowSection TableSection { get; set; }
  53. #endif
  54. }
  55. public class TestTableRow : TableRow, ITableRowTest {
  56. public string Tag {
  57. get { return base.TagName; }
  58. }
  59. public StateBag StateBag {
  60. get { return base.ViewState; }
  61. }
  62. public string Render ()
  63. {
  64. StringWriter sw = new StringWriter ();
  65. sw.NewLine = "\n";
  66. HtmlTextWriter writer = new HtmlTextWriter (sw);
  67. base.Render (writer);
  68. return writer.InnerWriter.ToString ();
  69. }
  70. public Style GetStyle ()
  71. {
  72. return base.CreateControlStyle ();
  73. }
  74. }
  75. [TestFixture]
  76. public class TableRowTest {
  77. private HtmlTextWriter GetWriter ()
  78. {
  79. StringWriter sw = new StringWriter ();
  80. sw.NewLine = "\n";
  81. return new HtmlTextWriter (sw);
  82. }
  83. public virtual TableRow GetNewTableRow ()
  84. {
  85. return new TableRow ();
  86. }
  87. public virtual ITableRowTest GetNewTestTableRow ()
  88. {
  89. return new TestTableRow ();
  90. }
  91. [Test]
  92. public void DefaultProperties ()
  93. {
  94. ITableRowTest tr = GetNewTestTableRow ();
  95. Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
  96. Assert.AreEqual (0, tr.StateBag.Count, "ViewState.Count");
  97. Assert.AreEqual (0, tr.Cells.Count, "Cells.Count");
  98. Assert.AreEqual (HorizontalAlign.NotSet, tr.HorizontalAlign, "HorizontalAlign");
  99. Assert.AreEqual (VerticalAlign.NotSet, tr.VerticalAlign, "VerticalAlign");
  100. #if NET_2_0
  101. Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableSection");
  102. #endif
  103. Assert.AreEqual ("tr", tr.Tag, "TagName");
  104. Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count-2");
  105. Assert.AreEqual (0, tr.StateBag.Count, "ViewState.Count-2");
  106. }
  107. [Test]
  108. public void NullProperties ()
  109. {
  110. ITableRowTest tr = GetNewTestTableRow ();
  111. tr.HorizontalAlign = HorizontalAlign.NotSet;
  112. Assert.AreEqual (HorizontalAlign.NotSet, tr.HorizontalAlign, "HorizontalAlign");
  113. tr.VerticalAlign = VerticalAlign.NotSet;
  114. Assert.AreEqual (VerticalAlign.NotSet, tr.VerticalAlign, "VerticalAlign");
  115. #if NET_2_0
  116. tr.TableSection = TableRowSection.TableBody;
  117. Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableSection");
  118. Assert.AreEqual (3, tr.StateBag.Count, "ViewState.Count-1");
  119. #else
  120. Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count-1");
  121. #endif
  122. Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
  123. }
  124. [Test]
  125. public void CleanProperties ()
  126. {
  127. ITableRowTest tr = GetNewTestTableRow ();
  128. tr.HorizontalAlign = HorizontalAlign.Justify;
  129. Assert.AreEqual (HorizontalAlign.Justify, tr.HorizontalAlign, "HorizontalAlign");
  130. tr.VerticalAlign = VerticalAlign.Bottom;
  131. Assert.AreEqual (VerticalAlign.Bottom, tr.VerticalAlign, "VerticalAlign");
  132. Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
  133. Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count");
  134. tr.HorizontalAlign = HorizontalAlign.NotSet;
  135. Assert.AreEqual (HorizontalAlign.NotSet, tr.HorizontalAlign, "HorizontalAlign");
  136. tr.VerticalAlign = VerticalAlign.NotSet;
  137. Assert.AreEqual (VerticalAlign.NotSet, tr.VerticalAlign, "VerticalAlign");
  138. #if NET_2_0
  139. tr.TableSection = TableRowSection.TableFooter;
  140. Assert.AreEqual (TableRowSection.TableFooter, tr.TableSection, "TableFooter");
  141. tr.TableSection = TableRowSection.TableHeader;
  142. Assert.AreEqual (TableRowSection.TableHeader, tr.TableSection, "TableHeader");
  143. tr.TableSection = TableRowSection.TableBody;
  144. Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableBody");
  145. Assert.AreEqual (3, tr.StateBag.Count, "ViewState.Count-1");
  146. #else
  147. Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count-1");
  148. #endif
  149. Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
  150. }
  151. [Test]
  152. // LAMESPEC: undocumented exception but similar to Image
  153. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  154. public void HorizontalAlign_Invalid ()
  155. {
  156. TableRow tr = GetNewTableRow ();
  157. tr.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
  158. }
  159. [Test]
  160. // LAMESPEC: undocumented exception but similar to Image
  161. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  162. public void VerticalAlign_Invalid ()
  163. {
  164. TableRow tr = GetNewTableRow ();
  165. tr.VerticalAlign = (VerticalAlign)Int32.MinValue;
  166. }
  167. #if NET_2_0
  168. [Test]
  169. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  170. public void TableSection_Invalid ()
  171. {
  172. TableRow tr = GetNewTableRow ();
  173. tr.TableSection = (TableRowSection)Int32.MinValue;
  174. }
  175. #endif
  176. [Test]
  177. public void Render ()
  178. {
  179. ITableRowTest tr = GetNewTestTableRow ();
  180. string s = tr.Render ();
  181. Assert.AreEqual ("<tr>\n\n</tr>", s, "empty/default");
  182. // case varies with fx versions
  183. tr.HorizontalAlign = HorizontalAlign.Left;
  184. s = tr.Render ();
  185. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"left\"") > 0), "HorizontalAlign.Left");
  186. tr.HorizontalAlign = HorizontalAlign.Center;
  187. s = tr.Render ();
  188. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"center\"") > 0), "HorizontalAlign.Center");
  189. tr.HorizontalAlign = HorizontalAlign.Right;
  190. s = tr.Render ();
  191. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"right\"") > 0), "HorizontalAlign.Justify");
  192. tr.HorizontalAlign = HorizontalAlign.Justify;
  193. s = tr.Render ();
  194. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"justify\"") > 0), "HorizontalAlign.Justify");
  195. tr.HorizontalAlign = HorizontalAlign.NotSet;
  196. tr.VerticalAlign = VerticalAlign.Top;
  197. s = tr.Render ();
  198. Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"top\"") > 0), "VerticalAlign.Top");
  199. tr.VerticalAlign = VerticalAlign.Middle;
  200. s = tr.Render ();
  201. Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"middle\"") > 0), "VerticalAlign.Middle");
  202. tr.VerticalAlign = VerticalAlign.Bottom;
  203. s = tr.Render ();
  204. Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"bottom\"") > 0), "VerticalAlign.Bottom");
  205. tr.VerticalAlign = VerticalAlign.NotSet;
  206. #if NET_2_0
  207. // TableSection has no influence over the "row" rendering
  208. tr.TableSection = TableRowSection.TableFooter;
  209. s = tr.Render ();
  210. Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableFooter");
  211. tr.TableSection = TableRowSection.TableHeader;
  212. s = tr.Render ();
  213. Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableHeader");
  214. tr.TableSection = TableRowSection.TableBody;
  215. s = tr.Render ();
  216. Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableBody");
  217. #endif
  218. }
  219. [Test]
  220. public void Render_Style ()
  221. {
  222. ITableRowTest tr = GetNewTestTableRow ();
  223. tr.BackColor = Color.Aqua;
  224. string s = tr.Render ();
  225. Assert.AreEqual ("<tr style=\"background-color:Aqua;\">\n\n</tr>", s, "direct");
  226. TableItemStyle tis = new TableItemStyle ();
  227. tis.BackColor = Color.Red;
  228. tr.ControlStyle.CopyFrom (tis);
  229. s = tr.Render ();
  230. Assert.AreEqual ("<tr style=\"background-color:Red;\">\n\n</tr>", s, "CopyFrom");
  231. }
  232. [Test]
  233. public void CreateControlStyle ()
  234. {
  235. ITableRowTest tr = GetNewTestTableRow ();
  236. tr.HorizontalAlign = HorizontalAlign.Left;
  237. tr.VerticalAlign = VerticalAlign.Bottom;
  238. TableItemStyle tis = (TableItemStyle)tr.GetStyle ();
  239. // is it live ?
  240. tis.HorizontalAlign = HorizontalAlign.Right;
  241. Assert.AreEqual (HorizontalAlign.Right, tr.HorizontalAlign, "HorizontalAlign-2");
  242. tis.VerticalAlign = VerticalAlign.Top;
  243. Assert.AreEqual (VerticalAlign.Top, tr.VerticalAlign, "VerticalAlign-2");
  244. }
  245. private string Adjust (string s)
  246. {
  247. // right now Mono doesn't generate the exact same indentation/lines as MS implementation
  248. // and different fx versions have different casing for enums
  249. return s.Replace ("\n", "").Replace ("\t", "").ToLower ();
  250. }
  251. [Test]
  252. public void Cells ()
  253. {
  254. ITableRowTest tr = GetNewTestTableRow ();
  255. Assert.AreEqual (0, tr.Cells.Count, "0");
  256. TableCell td = new TableCell ();
  257. tr.Cells.Add (td);
  258. Assert.AreEqual (1, tr.Cells.Count, "c1");
  259. Assert.AreEqual (1, tr.Controls.Count, "k1");
  260. string s = tr.Render ();
  261. Assert.AreEqual (Adjust ("<tr>\n\t<td></td>\n</tr>"), Adjust (s), "td-1");
  262. // change instance properties
  263. td.RowSpan = 1;
  264. s = tr.Render ();
  265. Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td>\n</tr>"), Adjust (s), "td-1r");
  266. // add it again (same instance)
  267. tr.Cells.Add (td);
  268. Assert.AreEqual (1, tr.Cells.Count, "c1bis");
  269. Assert.AreEqual (1, tr.Controls.Count, "k1bis");
  270. s = tr.Render ();
  271. Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td>\n</tr>"), Adjust (s), "tr-1bis");
  272. td = new TableCell ();
  273. td.VerticalAlign = VerticalAlign.Top;
  274. tr.Cells.Add (td);
  275. Assert.AreEqual (2, tr.Cells.Count, "c2");
  276. Assert.AreEqual (2, tr.Controls.Count, "k2");
  277. s = tr.Render ();
  278. Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td><td valign=\"top\"></td>\n</tr>"), Adjust (s), "tr-2");
  279. td = new TableCell ();
  280. td.HorizontalAlign = HorizontalAlign.Center;
  281. tr.Cells.Add (td);
  282. Assert.AreEqual (3, tr.Cells.Count, "c3");
  283. Assert.AreEqual (3, tr.Controls.Count, "k3");
  284. s = tr.Render ();
  285. Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td><td valign=\"top\"></td><td align=\"center\"></td>\n</tr>"), Adjust (s), "tr-3");
  286. tr.HorizontalAlign = HorizontalAlign.Right;
  287. s = tr.Render ();
  288. Assert.AreEqual (Adjust ("<tr align=\"right\">\n\t<td rowspan=\"1\"></td><td valign=\"top\"></td><td align=\"center\"></td>\n</tr>"), Adjust (s), "tr-3a");
  289. }
  290. [Test]
  291. [ExpectedException (typeof (NullReferenceException))]
  292. public void ControlsAdd_Null ()
  293. {
  294. GetNewTableRow ().Controls.Add (null);
  295. }
  296. [Test]
  297. [ExpectedException (typeof (ArgumentException))]
  298. public void ControlsAdd_LiteralControl ()
  299. {
  300. GetNewTableRow ().Controls.Add (new LiteralControl ("mono"));
  301. }
  302. [Test]
  303. public void ControlsAdd_TableCell ()
  304. {
  305. TableRow tr = GetNewTableRow ();
  306. tr.Controls.Add (new TableCell ());
  307. Assert.AreEqual (1, tr.Controls.Count, "Controls");
  308. Assert.AreEqual (1, tr.Cells.Count, "Cells");
  309. }
  310. [Test]
  311. public void ControlsAdd_TestTableRow ()
  312. {
  313. TableRow tr = GetNewTableRow ();
  314. tr.Controls.Add (new TestTableCell ());
  315. Assert.AreEqual (1, tr.Controls.Count, "Controls");
  316. Assert.AreEqual (1, tr.Cells.Count, "Cells");
  317. }
  318. [Test]
  319. [ExpectedException (typeof (NullReferenceException))]
  320. public void ControlsAddAt_Null ()
  321. {
  322. GetNewTableRow ().Controls.AddAt (0, null);
  323. }
  324. [Test]
  325. [ExpectedException (typeof (ArgumentException))]
  326. // note: for Table it's ArgumentOutOfRangeException
  327. public void ControlsAddAt_Negative ()
  328. {
  329. GetNewTableRow ().Controls.AddAt (Int32.MinValue, new TableRow ());
  330. }
  331. [Test]
  332. [ExpectedException (typeof (ArgumentException))]
  333. public void ControlsAddAt_LiteralControl ()
  334. {
  335. GetNewTableRow ().Controls.AddAt (0, new LiteralControl ("mono"));
  336. }
  337. [Test]
  338. public void ControlsAddAt_TableRow ()
  339. {
  340. TableRow tr = GetNewTableRow ();
  341. tr.Controls.AddAt (0, new TableCell ());
  342. Assert.AreEqual (1, tr.Controls.Count, "Controls");
  343. Assert.AreEqual (1, tr.Cells.Count, "Cells");
  344. }
  345. [Test]
  346. public void ControlsAddAt_TestTableRow ()
  347. {
  348. TableRow tr = GetNewTableRow ();
  349. tr.Controls.AddAt (0, new TestTableCell ());
  350. Assert.AreEqual (1, tr.Controls.Count, "Controls");
  351. Assert.AreEqual (1, tr.Cells.Count, "Cells");
  352. }
  353. [Test]
  354. [ExpectedException (typeof (NullReferenceException))]
  355. public void RenderBeginTag_Null ()
  356. {
  357. TableRow tr = GetNewTableRow ();
  358. tr.RenderBeginTag (null);
  359. }
  360. [Test]
  361. public void RenderBeginTag_Empty ()
  362. {
  363. HtmlTextWriter writer = GetWriter ();
  364. TableRow tr = GetNewTableRow ();
  365. tr.RenderBeginTag (writer);
  366. string s = writer.InnerWriter.ToString ();
  367. Assert.AreEqual ("<tr>\n", s, "empty");
  368. }
  369. [Test]
  370. public void RenderBeginTag_HorizontalAlign ()
  371. {
  372. HtmlTextWriter writer = GetWriter ();
  373. TableRow tr = GetNewTableRow ();
  374. tr.HorizontalAlign = HorizontalAlign.Center;
  375. tr.RenderBeginTag (writer);
  376. string s = writer.InnerWriter.ToString ();
  377. Assert.IsTrue (s.ToLower ().StartsWith ("<tr align=\"center\">"), "HorizontalAlign");
  378. }
  379. [Test]
  380. public void RenderBeginTag_Cells ()
  381. {
  382. HtmlTextWriter writer = GetWriter ();
  383. TableRow tr = GetNewTableRow ();
  384. tr.Cells.Add (new TableCell ());
  385. tr.RenderBeginTag (writer);
  386. string s = writer.InnerWriter.ToString ();
  387. Assert.AreEqual ("<tr>\n", s, "td");
  388. }
  389. }
  390. }