TableCellTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // TableCellTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.TableCell
  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;
  32. using System.Web.UI;
  33. using System.Web.UI.WebControls;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Web.UI.WebControls {
  36. public class TestTableCell : TableCell {
  37. public string Tag {
  38. get { return base.TagName; }
  39. }
  40. public StateBag StateBag {
  41. get { return base.ViewState; }
  42. }
  43. public string Render ()
  44. {
  45. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  46. base.Render (writer);
  47. return writer.InnerWriter.ToString ();
  48. }
  49. public Style GetStyle ()
  50. {
  51. return base.CreateControlStyle ();
  52. }
  53. public void Add (object o)
  54. {
  55. base.AddParsedSubObject (o);
  56. }
  57. }
  58. [TestFixture]
  59. public class TableCellTest {
  60. [Test]
  61. public void DefaultProperties ()
  62. {
  63. TestTableCell td = new TestTableCell ();
  64. Assert.AreEqual (0, td.Attributes.Count, "Attributes.Count");
  65. Assert.AreEqual (0, td.StateBag.Count, "ViewState.Count");
  66. Assert.AreEqual (0, td.ColumnSpan, "ColumnSpan");
  67. Assert.AreEqual (HorizontalAlign.NotSet, td.HorizontalAlign, "HorizontalAlign");
  68. Assert.AreEqual (0, td.RowSpan, "RowSpan");
  69. Assert.AreEqual (String.Empty, td.Text, "Text");
  70. Assert.AreEqual (VerticalAlign.NotSet, td.VerticalAlign, "VerticalAlign");
  71. Assert.IsTrue (td.Wrap, "Wrap");
  72. #if NET_2_0
  73. Assert.AreEqual (0, td.AssociatedHeaderCellID.Length, "AssociatedHeaderCellID");
  74. #endif
  75. Assert.AreEqual ("td", td.Tag, "TagName");
  76. Assert.AreEqual (0, td.Attributes.Count, "Attributes.Count-2");
  77. Assert.AreEqual (0, td.StateBag.Count, "ViewState.Count-2");
  78. }
  79. [Test]
  80. public void NullProperties ()
  81. {
  82. TestTableCell td = new TestTableCell ();
  83. td.ColumnSpan = 0;
  84. Assert.AreEqual (0, td.ColumnSpan, "ColumnSpan");
  85. td.HorizontalAlign = HorizontalAlign.NotSet;
  86. Assert.AreEqual (HorizontalAlign.NotSet, td.HorizontalAlign, "HorizontalAlign");
  87. td.RowSpan = 0;
  88. Assert.AreEqual (0, td.RowSpan, "RowSpan");
  89. td.Text = null;
  90. Assert.AreEqual (String.Empty, td.Text, "Text");
  91. td.VerticalAlign = VerticalAlign.NotSet;
  92. Assert.AreEqual (VerticalAlign.NotSet, td.VerticalAlign, "VerticalAlign");
  93. td.Wrap = true;
  94. Assert.IsTrue (td.Wrap, "Wrap");
  95. #if NET_2_0
  96. td.AssociatedHeaderCellID = new string[0];
  97. Assert.AreEqual (0, td.AssociatedHeaderCellID.Length, "AssociatedHeaderCellID");
  98. Assert.AreEqual (6, td.StateBag.Count, "ViewState.Count-1");
  99. #else
  100. Assert.AreEqual (5, td.StateBag.Count, "ViewState.Count-1");
  101. #endif
  102. Assert.AreEqual (0, td.Attributes.Count, "Attributes.Count");
  103. // note: nothing is removed (no need for CleanProperties test)
  104. }
  105. #if NET_2_0
  106. [Test]
  107. public void AssociatedHeaderCellID ()
  108. {
  109. TableCell td = new TableCell ();
  110. td.AssociatedHeaderCellID = null;
  111. Assert.AreEqual (0, td.AssociatedHeaderCellID.Length, "0");
  112. // no NRE
  113. td.AssociatedHeaderCellID = new string[1] { "mono" };
  114. Assert.AreEqual (1, td.AssociatedHeaderCellID.Length, "1");
  115. td.AssociatedHeaderCellID = null;
  116. Assert.AreEqual (0, td.AssociatedHeaderCellID.Length, "2");
  117. }
  118. #endif
  119. [Test]
  120. // LAMESPEC: undocumented exception but similar to integer properties of Table
  121. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  122. public void ColumnSpan_Negative ()
  123. {
  124. TableCell td = new TableCell ();
  125. td.ColumnSpan = -1;
  126. }
  127. [Test]
  128. // LAMESPEC: undocumented exception but similar to integer properties of Table
  129. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  130. public void RowSpan_Negative ()
  131. {
  132. TableCell td = new TableCell ();
  133. td.RowSpan = -1;
  134. }
  135. [Test]
  136. public void Render ()
  137. {
  138. TestTableCell td = new TestTableCell ();
  139. string s = td.Render ();
  140. Assert.AreEqual ("<td></td>", s, "empty/default");
  141. // case varies with fx versions
  142. td.HorizontalAlign = HorizontalAlign.Left;
  143. s = td.Render ();
  144. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"left\"") > 0), "HorizontalAlign.Left");
  145. td.HorizontalAlign = HorizontalAlign.Center;
  146. s = td.Render ();
  147. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"center\"") > 0), "HorizontalAlign.Center");
  148. td.HorizontalAlign = HorizontalAlign.Right;
  149. s = td.Render ();
  150. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"right\"") > 0), "HorizontalAlign.Justify");
  151. td.HorizontalAlign = HorizontalAlign.Justify;
  152. s = td.Render ();
  153. Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"justify\"") > 0), "HorizontalAlign.Justify");
  154. td.HorizontalAlign = HorizontalAlign.NotSet;
  155. td.VerticalAlign = VerticalAlign.Top;
  156. s = td.Render ();
  157. Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"top\"") > 0), "VerticalAlign.Top");
  158. td.VerticalAlign = VerticalAlign.Middle;
  159. s = td.Render ();
  160. Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"middle\"") > 0), "VerticalAlign.Middle");
  161. td.VerticalAlign = VerticalAlign.Bottom;
  162. s = td.Render ();
  163. Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"bottom\"") > 0), "VerticalAlign.Bottom");
  164. td.VerticalAlign = VerticalAlign.NotSet;
  165. td.ColumnSpan = 1;
  166. s = td.Render ();
  167. Assert.AreEqual ("<td colspan=\"1\"></td>", s, "ColumnSpan");
  168. td.ColumnSpan = 0;
  169. td.RowSpan = 1;
  170. s = td.Render ();
  171. Assert.AreEqual ("<td rowspan=\"1\"></td>", s, "RowSpan");
  172. td.RowSpan = 0;
  173. td.Text = "text";
  174. s = td.Render ();
  175. Assert.AreEqual ("<td>text</td>", s, "Text");
  176. td.Text = null;
  177. td.Wrap = false;
  178. s = td.Render ();
  179. #if NET_2_0
  180. Assert.AreEqual ("<td style=\"white-space:nowrap;\"></td>", s, "Wrap");
  181. // it seems that rendering with AssociatedHeaderCellID property set
  182. // isn't (at least easyly) possible even if we build a whole table
  183. // with a page... it keeps throwing NullReferenceException. Even in a
  184. // web page using that property makes it easy to throw exceptions :(
  185. #else
  186. Assert.AreEqual ("<td nowrap=\"nowrap\"></td>", s, "Wrap");
  187. #endif
  188. td.Wrap = true;
  189. }
  190. [Test]
  191. public void CreateControlStyle ()
  192. {
  193. TestTableCell td = new TestTableCell ();
  194. td.HorizontalAlign = HorizontalAlign.Left;
  195. td.VerticalAlign = VerticalAlign.Bottom;
  196. td.Wrap = false;
  197. TableItemStyle tis = (TableItemStyle)td.GetStyle ();
  198. // is it live ?
  199. tis.HorizontalAlign = HorizontalAlign.Right;
  200. Assert.AreEqual (HorizontalAlign.Right, td.HorizontalAlign, "HorizontalAlign-2");
  201. tis.VerticalAlign = VerticalAlign.Top;
  202. Assert.AreEqual (VerticalAlign.Top, td.VerticalAlign, "VerticalAlign-2");
  203. tis.Wrap = false;
  204. Assert.IsFalse (tis.Wrap, "Wrap-2");
  205. }
  206. [Test]
  207. public void Add_LiteralControl_NoText ()
  208. {
  209. TestTableCell td = new TestTableCell ();
  210. // this is moved into the (empty) Text property
  211. td.Add (new LiteralControl ("Mono"));
  212. Assert.IsFalse (td.HasControls (), "!HasControls");
  213. Assert.AreEqual ("Mono", td.Text, "Text");
  214. // this replace the current Text property
  215. td.Add (new LiteralControl ("Go Mono"));
  216. Assert.IsFalse (td.HasControls (), "!HasControls-2");
  217. Assert.AreEqual ("Go Mono", td.Text, "Text-2");
  218. }
  219. [Test]
  220. public void Text_Add_LiteralControl ()
  221. {
  222. TestTableCell td = new TestTableCell ();
  223. td.Text = "Mono";
  224. Assert.AreEqual ("Mono", td.Text, "Text-1");
  225. Assert.IsFalse (td.HasControls (), "!HasControls");
  226. // this replace the current Text property
  227. td.Add (new LiteralControl ("Go Mono"));
  228. Assert.IsFalse (td.HasControls (), "!HasControls-2");
  229. Assert.AreEqual ("Go Mono", td.Text, "Text-2");
  230. }
  231. [Test]
  232. public void Add_LiteralControl_Text ()
  233. {
  234. TestTableCell td = new TestTableCell ();
  235. // this is moved into the (empty) Text property
  236. td.Add (new LiteralControl ("Mono"));
  237. Assert.IsFalse (td.HasControls (), "!HasControls");
  238. Assert.AreEqual ("Mono", td.Text, "Text");
  239. // this replace the current Text property
  240. td.Text = "Go Mono";
  241. Assert.IsFalse (td.HasControls (), "!HasControls-2");
  242. Assert.AreEqual ("Go Mono", td.Text, "Text-2");
  243. }
  244. [Test]
  245. public void HasControls_Text ()
  246. {
  247. TestTableCell td = new TestTableCell ();
  248. for (int i = 0; i < 10; i++)
  249. td.Add (new Table ());
  250. Assert.AreEqual (10, td.Controls.Count, "10");
  251. // this removes all existing controls and set the Text property
  252. td.Text = "Mono";
  253. Assert.AreEqual ("Mono", td.Text, "Text");
  254. Assert.AreEqual (0, td.Controls.Count, "0");
  255. }
  256. [Test]
  257. public void Text_Add_Controls ()
  258. {
  259. TestTableCell td = new TestTableCell ();
  260. td.Text = "Mono";
  261. Assert.AreEqual ("Mono", td.Text, "Text");
  262. Assert.IsFalse (td.HasControls (), "!HasControls");
  263. // then add 10 more controls
  264. for (int i = 0; i < 10; i++)
  265. td.Add (new Table ());
  266. Assert.AreEqual (11, td.Controls.Count, "11");
  267. // Text was moved into a LiteralControl
  268. Assert.IsTrue ((td.Controls[0] is LiteralControl), "LiteralControl");
  269. // and removed from property
  270. Assert.AreEqual (String.Empty, td.Text, "Test-2");
  271. }
  272. }
  273. }