2
0

TableCellTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #if NET_2_0
  218. Assert.AreEqual ("MonoGo Mono", td.Text, "Text-2");
  219. #else
  220. Assert.AreEqual ("Go Mono", td.Text, "Text-2");
  221. #endif
  222. }
  223. [Test]
  224. public void Text_Add_LiteralControl ()
  225. {
  226. TestTableCell td = new TestTableCell ();
  227. td.Text = "Mono";
  228. Assert.AreEqual ("Mono", td.Text, "Text-1");
  229. Assert.IsFalse (td.HasControls (), "!HasControls");
  230. // this replace the current Text property
  231. td.Add (new LiteralControl ("Go Mono"));
  232. Assert.IsFalse (td.HasControls (), "!HasControls-2");
  233. Assert.AreEqual ("Go Mono", td.Text, "Text-2");
  234. }
  235. [Test]
  236. public void Add_LiteralControl_Text ()
  237. {
  238. TestTableCell td = new TestTableCell ();
  239. // this is moved into the (empty) Text property
  240. td.Add (new LiteralControl ("Mono"));
  241. Assert.IsFalse (td.HasControls (), "!HasControls");
  242. Assert.AreEqual ("Mono", td.Text, "Text");
  243. // this replace the current Text property
  244. td.Text = "Go Mono";
  245. Assert.IsFalse (td.HasControls (), "!HasControls-2");
  246. Assert.AreEqual ("Go Mono", td.Text, "Text-2");
  247. }
  248. [Test]
  249. public void Add_LiteralControl_Literal_And_Literal ()
  250. {
  251. TestTableCell td = new TestTableCell ();
  252. // this is moved into the (empty) Text property
  253. td.Add (new LiteralControl ("Mono"));
  254. Assert.IsFalse (td.HasControls (), "!HasControls");
  255. Assert.AreEqual ("Mono", td.Text, "Text");
  256. td.Add (new LiteralControl ("Mono2"));
  257. Assert.IsFalse (td.HasControls (), "HasControls-2");
  258. #if NET_2_0
  259. Assert.AreEqual ("MonoMono2", td.Text, "Text");
  260. #else
  261. Assert.AreEqual ("Mono2", td.Text, "Text");
  262. #endif
  263. Assert.AreEqual (0, td.Controls.Count, "NControls");
  264. }
  265. [Test]
  266. public void Add_LiteralControl_Control_And_Literal ()
  267. {
  268. TestTableCell td = new TestTableCell ();
  269. // this is moved into the (empty) Text property
  270. td.Add (new TableCell ());
  271. Assert.IsTrue (td.HasControls (), "HasControls");
  272. td.Add (new LiteralControl ("Mono2"));
  273. Assert.AreEqual (2, td.Controls.Count, "NControls");
  274. Assert.AreEqual (typeof (TableCell), td.Controls [0].GetType (), "type 1");
  275. Assert.AreEqual (typeof (LiteralControl), td.Controls [1].GetType (), "type 2");
  276. }
  277. [Test]
  278. public void Add_LiteralControl_Literal_And_Control ()
  279. {
  280. TestTableCell td = new TestTableCell ();
  281. // this is moved into the (empty) Text property
  282. td.Add (new LiteralControl ("Mono2"));
  283. Assert.IsFalse (td.HasControls (), "HasControls");
  284. td.Add (new TableCell ());
  285. Assert.AreEqual (2, td.Controls.Count, "NControls");
  286. Assert.AreEqual (typeof (LiteralControl), td.Controls [0].GetType (), "type 1");
  287. Assert.AreEqual (typeof (TableCell), td.Controls [1].GetType (), "type 2");
  288. }
  289. [Test]
  290. public void HasControls_Text ()
  291. {
  292. TestTableCell td = new TestTableCell ();
  293. for (int i = 0; i < 10; i++)
  294. td.Add (new Table ());
  295. Assert.AreEqual (10, td.Controls.Count, "10");
  296. // this removes all existing controls and set the Text property
  297. td.Text = "Mono";
  298. Assert.AreEqual ("Mono", td.Text, "Text");
  299. Assert.AreEqual (0, td.Controls.Count, "0");
  300. }
  301. [Test]
  302. public void Text_Add_Controls ()
  303. {
  304. TestTableCell td = new TestTableCell ();
  305. td.Text = "Mono";
  306. Assert.AreEqual ("Mono", td.Text, "Text");
  307. Assert.IsFalse (td.HasControls (), "!HasControls");
  308. // then add 10 more controls
  309. for (int i = 0; i < 10; i++)
  310. td.Add (new Table ());
  311. Assert.AreEqual (11, td.Controls.Count, "11");
  312. // Text was moved into a LiteralControl
  313. Assert.IsTrue ((td.Controls[0] is LiteralControl), "LiteralControl");
  314. // and removed from property
  315. Assert.AreEqual (String.Empty, td.Text, "Test-2");
  316. }
  317. [Test]
  318. public void NoDefaultID ()
  319. {
  320. Page page = new Page ();
  321. TableCell tc = new TableCell ();
  322. Assert.AreEqual (null, tc.ID, "#01");
  323. page.Controls.Add (tc);
  324. Assert.AreEqual (null, tc.ID, "#02");
  325. Assert.IsNotNull (tc.UniqueID, "#03");
  326. Assert.IsNull (tc.ID, "#04");
  327. }
  328. [Test]
  329. public void PropertyOrControls ()
  330. {
  331. TestTableCell tc = new TestTableCell ();
  332. tc.Controls.Add (new LiteralControl ("hola"));
  333. tc.StateBag ["Text"] = "adios";
  334. string str = tc.Render ();
  335. Assert.AreEqual (1, tc.Controls.Count, "#01");
  336. Assert.IsTrue (-1 != str.IndexOf ("hola"), "#02");
  337. Assert.IsTrue (-1 == str.IndexOf ("adios"), "#03");
  338. tc = new TestTableCell ();
  339. tc.StateBag ["Text"] = "adios";
  340. str = tc.Render ();
  341. Assert.AreEqual (0, tc.Controls.Count, "#04");
  342. Assert.IsTrue (-1 == str.IndexOf ("hola"), "#05");
  343. Assert.IsTrue (-1 != str.IndexOf ("adios"), "#06");
  344. }
  345. }
  346. }