TableTest.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. //
  2. // TableTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.Table
  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 MonoTests.SystemWeb.Framework;
  35. using NUnit.Framework;
  36. using MonoTests.stand_alone.WebHarness;
  37. namespace MonoTests.System.Web.UI.WebControls {
  38. public class TestTable : Table {
  39. public string Tag {
  40. get { return base.TagName; }
  41. }
  42. public StateBag StateBag {
  43. get { return base.ViewState; }
  44. }
  45. public string Render ()
  46. {
  47. StringWriter sw = new StringWriter ();
  48. sw.NewLine = "\n";
  49. HtmlTextWriter writer = new HtmlTextWriter (sw);
  50. base.Render (writer);
  51. return writer.InnerWriter.ToString ();
  52. }
  53. public Style GetStyle ()
  54. {
  55. return base.CreateControlStyle ();
  56. }
  57. #if NET_2_0
  58. protected override void RaisePostBackEvent (string argument)
  59. {
  60. WebTest.CurrentTest.UserData = "RaisePostBackEvent";
  61. base.RaisePostBackEvent (argument);
  62. }
  63. #endif
  64. }
  65. [TestFixture]
  66. public class TableTest {
  67. private const string imageUrl = "http://www.mono-project.com/stylesheets/images.wiki.png";
  68. private const string localImageUrl = "foo.jpg";
  69. private HtmlTextWriter GetWriter ()
  70. {
  71. StringWriter sw = new StringWriter ();
  72. sw.NewLine = "\n";
  73. return new HtmlTextWriter (sw);
  74. }
  75. [Test]
  76. public void DefaultProperties ()
  77. {
  78. TestTable t = new TestTable ();
  79. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
  80. Assert.AreEqual (0, t.StateBag.Count, "ViewState.Count");
  81. Assert.AreEqual (String.Empty, t.BackImageUrl, "BackImageUrl");
  82. Assert.AreEqual (String.Empty, t.Caption, "Caption");
  83. Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign");
  84. Assert.AreEqual (-1, t.CellPadding, "CellPadding");
  85. Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
  86. Assert.AreEqual (GridLines.None, t.GridLines, "GridLines");
  87. Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "HorizontalAlign");
  88. Assert.AreEqual (0, t.Rows.Count, "Rows.Count");
  89. Assert.AreEqual ("table", t.Tag, "TagName");
  90. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count-2");
  91. Assert.AreEqual (0, t.StateBag.Count, "ViewState.Count-2");
  92. #if NET_2_0
  93. Assert.AreEqual (String.Empty, t.Caption, "Caption");
  94. Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign");
  95. #endif
  96. }
  97. #if NET_2_0
  98. [Test]
  99. public void Caption ()
  100. {
  101. TestTable t = new TestTable ();
  102. t.Caption = "CaptionText";
  103. string html = t.Render ();
  104. #if NET_4_0
  105. string orig = "<table>\n\t<caption>\n\t\tCaptionText\n\t</caption>\n</table>";
  106. #else
  107. string orig = "<table border=\"0\">\n\t<caption>\n\t\tCaptionText\n\t</caption>\n</table>";
  108. #endif
  109. HtmlDiff.AssertAreEqual (orig, html, "Caption");
  110. }
  111. [Test]
  112. public void CaptionAlign ()
  113. {
  114. TestTable t = new TestTable ();
  115. t.Caption = "CaptionText";
  116. t.CaptionAlign = TableCaptionAlign.Left;
  117. string html = t.Render ();
  118. #if NET_4_0
  119. string orig = "<table>\n\t<caption align=\"Left\">\n\t\tCaptionText\n\t</caption>\n</table>";
  120. #else
  121. string orig = "<table border=\"0\">\n\t<caption align=\"Left\">\n\t\tCaptionText\n\t</caption>\n</table>";
  122. #endif
  123. HtmlDiff.AssertAreEqual (orig, html, "CaptionAlign");
  124. }
  125. #endif
  126. [Test]
  127. public void NullProperties ()
  128. {
  129. TestTable t = new TestTable ();
  130. t.BackImageUrl = String.Empty; // doesn't accept null, see specific test
  131. Assert.AreEqual (String.Empty, t.BackImageUrl, "BackImageUrl");
  132. t.Caption = null; // doesn't get added to ViewState
  133. Assert.AreEqual (String.Empty, t.Caption, "Caption");
  134. t.CaptionAlign = TableCaptionAlign.NotSet;
  135. Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign");
  136. t.CellPadding = -1;
  137. Assert.AreEqual (-1, t.CellPadding, "CellPadding");
  138. t.CellSpacing = -1;
  139. Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
  140. t.GridLines = GridLines.None;
  141. Assert.AreEqual (GridLines.None, t.GridLines, "GridLines");
  142. t.HorizontalAlign = HorizontalAlign.NotSet;
  143. Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "HorizontalAlign");
  144. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
  145. Assert.AreEqual (6, t.StateBag.Count, "ViewState.Count-1");
  146. }
  147. [Test]
  148. public void CleanProperties ()
  149. {
  150. TestTable t = new TestTable ();
  151. t.BackImageUrl = imageUrl;
  152. Assert.AreEqual (imageUrl, t.BackImageUrl, "BackImageUrl");
  153. t.Caption = "Mono";
  154. Assert.AreEqual ("Mono", t.Caption, "Caption");
  155. t.CaptionAlign = TableCaptionAlign.Top;
  156. Assert.AreEqual (TableCaptionAlign.Top, t.CaptionAlign, "CaptionAlign");
  157. t.CellPadding = 1;
  158. Assert.AreEqual (1, t.CellPadding, "CellPadding");
  159. t.CellSpacing = 2;
  160. Assert.AreEqual (2, t.CellSpacing, "CellSpacing");
  161. t.GridLines = GridLines.Both;
  162. Assert.AreEqual (GridLines.Both, t.GridLines, "GridLines");
  163. t.HorizontalAlign = HorizontalAlign.Justify;
  164. Assert.AreEqual (HorizontalAlign.Justify, t.HorizontalAlign, "HorizontalAlign");
  165. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
  166. Assert.AreEqual (7, t.StateBag.Count, "ViewState.Count");
  167. t.BackImageUrl = String.Empty; // doesn't accept null, see specific test
  168. Assert.AreEqual (String.Empty, t.BackImageUrl, "-BackImageUrl");
  169. t.Caption = null; // removed
  170. Assert.AreEqual (String.Empty, t.Caption, "-Caption");
  171. t.CaptionAlign = TableCaptionAlign.NotSet;
  172. Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "-CaptionAlign");
  173. t.CellPadding = -1;
  174. Assert.AreEqual (-1, t.CellPadding, "-CellPadding");
  175. t.CellSpacing = -1;
  176. Assert.AreEqual (-1, t.CellSpacing, "-CellSpacing");
  177. t.GridLines = GridLines.None;
  178. Assert.AreEqual (GridLines.None, t.GridLines, "-GridLines");
  179. t.HorizontalAlign = HorizontalAlign.NotSet;
  180. Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "-HorizontalAlign");
  181. Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count-1");
  182. Assert.AreEqual (6, t.StateBag.Count, "ViewState.Count-1");
  183. }
  184. [Test]
  185. // LAMESPEC: undocumented (all others property I've seen takes null as the default value)
  186. [ExpectedException (typeof (ArgumentNullException))]
  187. public void BackImageUrl_Null ()
  188. {
  189. Table t = new Table ();
  190. t.BackImageUrl = null;
  191. }
  192. [Test]
  193. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  194. public void CaptionAlign_Invalid ()
  195. {
  196. Table t = new Table ();
  197. t.CaptionAlign = (TableCaptionAlign)Int32.MinValue;
  198. }
  199. [Test]
  200. // LAMESPEC: undocumented exception but similar to Image
  201. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  202. public void GridLines_Invalid ()
  203. {
  204. Table t = new Table ();
  205. t.GridLines = (GridLines)Int32.MinValue;
  206. }
  207. [Test]
  208. // LAMESPEC: undocumented exception but similar to Image
  209. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  210. public void HorizontalAlign_Invalid ()
  211. {
  212. Table t = new Table ();
  213. t.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
  214. }
  215. [Test]
  216. public void BorderWidth ()
  217. {
  218. Table t = new Table ();
  219. Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.None");
  220. t.GridLines = GridLines.Horizontal;
  221. Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Horizontal");
  222. t.GridLines = GridLines.Vertical;
  223. Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Vertical");
  224. t.GridLines = GridLines.Both;
  225. Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Both");
  226. // note: but border="1" when rendered
  227. }
  228. [Test]
  229. public void Render ()
  230. {
  231. TestTable t = new TestTable ();
  232. string s = t.Render ();
  233. #if NET_4_0
  234. Assert.AreEqual ("<table>\n\n</table>", s, "empty/default");
  235. #else
  236. Assert.AreEqual ("<table border=\"0\">\n\n</table>", s, "empty/default");
  237. #endif
  238. t.CellPadding = 1;
  239. s = t.Render ();
  240. #if NET_4_0
  241. Assert.AreEqual ("<table cellpadding=\"1\">\n\n</table>", s, "CellPadding");
  242. #else
  243. Assert.AreEqual ("<table cellpadding=\"1\" border=\"0\">\n\n</table>", s, "CellPadding");
  244. #endif
  245. t.CellPadding = -1;
  246. t.CellSpacing = 2;
  247. s = t.Render ();
  248. #if NET_4_0
  249. Assert.AreEqual ("<table cellspacing=\"2\">\n\n</table>", s, "CellSpacing");
  250. #else
  251. Assert.AreEqual ("<table cellspacing=\"2\" border=\"0\">\n\n</table>", s, "CellSpacing");
  252. #endif
  253. t.CellSpacing = -1;
  254. t.GridLines = GridLines.Horizontal;
  255. s = t.Render ();
  256. Assert.AreEqual ("<table rules=\"rows\" border=\"1\">\n\n</table>", s, "GridLines.Horizontal");
  257. t.GridLines = GridLines.Vertical;
  258. s = t.Render ();
  259. Assert.AreEqual ("<table rules=\"cols\" border=\"1\">\n\n</table>", s, "GridLines.Vertical");
  260. t.GridLines = GridLines.Both;
  261. s = t.Render ();
  262. Assert.AreEqual ("<table rules=\"all\" border=\"1\">\n\n</table>", s, "GridLines.Both");
  263. t.GridLines = GridLines.None;
  264. t.BorderWidth = new Unit (2);
  265. s = t.Render ();
  266. #if NET_4_0
  267. Assert.IsTrue ((s.IndexOf ("\"border-width:2px;border-style:solid;\"") > 0), "border=0/2");
  268. #else
  269. Assert.IsTrue ((s.IndexOf ("border=\"0\"") > 0), "border=0/2");
  270. #endif
  271. t.GridLines = GridLines.Horizontal;
  272. s = t.Render ();
  273. #if NET_4_0
  274. Console.WriteLine (s);
  275. Assert.IsTrue ((s.IndexOf ("rules=\"rows\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Horizontal");
  276. #else
  277. Assert.IsTrue ((s.IndexOf ("rules=\"rows\" border=\"2\"") > 0), "2/GridLines.Horizontal");
  278. #endif
  279. t.GridLines = GridLines.Vertical;
  280. s = t.Render ();
  281. #if NET_4_0
  282. Assert.IsTrue ((s.IndexOf ("rules=\"cols\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Vertical");
  283. #else
  284. Assert.IsTrue ((s.IndexOf ("rules=\"cols\" border=\"2\"") > 0), "2/GridLines.Vertical");
  285. #endif
  286. t.GridLines = GridLines.Both;
  287. s = t.Render ();
  288. #if NET_4_0
  289. Assert.IsTrue ((s.IndexOf ("rules=\"all\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Both");
  290. #else
  291. Assert.IsTrue ((s.IndexOf ("rules=\"all\" border=\"2\"") > 0), "2/GridLines.Both");
  292. #endif
  293. t.GridLines = GridLines.None;
  294. t.BorderWidth = new Unit ();
  295. t.HorizontalAlign = HorizontalAlign.Left;
  296. s = t.Render ();
  297. #if NET_4_0
  298. Assert.AreEqual ("<table align=\"left\">\n\n</table>", s.ToLower (), "HorizontalAlign.Left");
  299. #else
  300. Assert.AreEqual ("<table align=\"left\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Left");
  301. #endif
  302. t.HorizontalAlign = HorizontalAlign.Center;
  303. s = t.Render ();
  304. #if NET_4_0
  305. Assert.AreEqual ("<table align=\"center\">\n\n</table>", s.ToLower (), "HorizontalAlign.Center");
  306. #else
  307. Assert.AreEqual ("<table align=\"center\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Center");
  308. #endif
  309. t.HorizontalAlign = HorizontalAlign.Right;
  310. s = t.Render ();
  311. #if NET_4_0
  312. Assert.AreEqual ("<table align=\"right\">\n\n</table>", s.ToLower (), "HorizontalAlign.Right");
  313. #else
  314. Assert.AreEqual ("<table align=\"right\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Right");
  315. #endif
  316. t.HorizontalAlign = HorizontalAlign.Justify;
  317. s = t.Render ();
  318. #if NET_4_0
  319. Assert.AreEqual ("<table align=\"justify\">\n\n</table>", s.ToLower (), "HorizontalAlign.Justify");
  320. #else
  321. Assert.AreEqual ("<table align=\"justify\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Justify");
  322. #endif
  323. t.HorizontalAlign = HorizontalAlign.NotSet;
  324. t.Caption = "mono";
  325. s = t.Render ();
  326. #if NET_4_0
  327. Assert.AreEqual ("<table>\n\t<caption>\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption");
  328. #else
  329. Assert.AreEqual ("<table border=\"0\">\n\t<caption>\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption");
  330. #endif
  331. t.CaptionAlign = TableCaptionAlign.Top;
  332. s = t.Render ();
  333. #if NET_4_0
  334. Assert.AreEqual ("<table>\n\t<caption align=\"top\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Top");
  335. #else
  336. Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"top\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Top");
  337. #endif
  338. t.CaptionAlign = TableCaptionAlign.Bottom;
  339. s = t.Render ();
  340. #if NET_4_0
  341. Assert.AreEqual ("<table>\n\t<caption align=\"bottom\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Bottom");
  342. #else
  343. Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"bottom\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Bottom");
  344. #endif
  345. t.CaptionAlign = TableCaptionAlign.Right;
  346. s = t.Render ();
  347. #if NET_4_0
  348. Assert.AreEqual ("<table>\n\t<caption align=\"right\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Right");
  349. #else
  350. Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"right\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Right");
  351. #endif
  352. t.CaptionAlign = TableCaptionAlign.Left;
  353. s = t.Render ();
  354. #if NET_4_0
  355. Assert.AreEqual ("<table>\n\t<caption align=\"left\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Left");
  356. #else
  357. Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"left\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Left");
  358. #endif
  359. t.Caption = null;
  360. s = t.Render ();
  361. #if NET_4_0
  362. Assert.AreEqual ("<table>\n\n</table>", s, "CaptionAlign without Caption");
  363. #else
  364. Assert.AreEqual ("<table border=\"0\">\n\n</table>", s, "CaptionAlign without Caption");
  365. #endif
  366. t.CaptionAlign = TableCaptionAlign.NotSet;
  367. t.BackImageUrl = imageUrl;
  368. s = t.Render ();
  369. #if NET_4_0
  370. Assert.AreEqual ("<table style=\"background-image:url(http://www.mono-project.com/stylesheets/images.wiki.png);\">\n\n</table>", s, "BackImageUrl");
  371. #else
  372. Assert.AreEqual ("<table border=\"0\" style=\"background-image:url(http://www.mono-project.com/stylesheets/images.wiki.png);\">\n\n</table>", s, "BackImageUrl");
  373. #endif
  374. t.BackImageUrl = localImageUrl;
  375. s = t.Render ();
  376. #if NET_4_0
  377. Assert.AreEqual ("<table style=\"background-image:url(foo.jpg);\">\n\n</table>", s, "BackImageUrl");
  378. #else
  379. Assert.AreEqual ("<table border=\"0\" style=\"background-image:url(foo.jpg);\">\n\n</table>", s, "BackImageUrl");
  380. #endif
  381. t.BackImageUrl = String.Empty;
  382. }
  383. #if NET_2_0
  384. [Test]
  385. [Category ("NunitWeb")]
  386. public void RenderInAspxPage ()
  387. {
  388. WebTest t = new WebTest (PageInvoker.CreateOnLoad (RenderInAspxPage_OnLoad));
  389. string res = t.Run ();
  390. #if NET_4_0
  391. Assert.IsTrue (res.IndexOf ("<table id=\"MagicID_A1C3\" style=\"background-image:url(foo.jpg);\"") != -1, res);
  392. #else
  393. Assert.IsTrue (res.IndexOf ("<table id=\"MagicID_A1C3\" border=\"0\" style=\"background-image:url(foo.jpg);\"")!= -1, res);
  394. #endif
  395. }
  396. public static void RenderInAspxPage_OnLoad (Page p)
  397. {
  398. Table t = new Table ();
  399. t.BackImageUrl = "foo.jpg";
  400. t.ID = "MagicID_A1C3";
  401. p.Form.Controls.Add (t);
  402. p.Controls.Add (t);
  403. }
  404. #endif
  405. [Test]
  406. public void CreateControlStyle ()
  407. {
  408. TestTable t = new TestTable ();
  409. t.BackImageUrl = imageUrl;
  410. t.CellPadding = 1;
  411. t.CellSpacing = 2;
  412. t.GridLines = GridLines.Horizontal;
  413. t.HorizontalAlign = HorizontalAlign.Left;
  414. TableStyle ts = (TableStyle)t.GetStyle ();
  415. // is it live ?
  416. ts.BackImageUrl = "mono";
  417. Assert.AreEqual ("mono", t.BackImageUrl, "BackImageUrl-2");
  418. ts.CellPadding = Int32.MaxValue;
  419. Assert.AreEqual (Int32.MaxValue, t.CellPadding, "CellPadding-2");
  420. ts.CellSpacing = 0;
  421. Assert.AreEqual (0, t.CellSpacing, "CellSpacing-2");
  422. ts.GridLines = GridLines.Vertical;
  423. Assert.AreEqual (GridLines.Vertical, t.GridLines, "GridLines-2");
  424. ts.HorizontalAlign = HorizontalAlign.Right;
  425. Assert.AreEqual (HorizontalAlign.Right, t.HorizontalAlign, "HorizontalAlign-2");
  426. }
  427. private string Adjust (string s)
  428. {
  429. // right now Mono doesn't generate the exact same indentation/lines as MS implementation
  430. // and different fx versions have different casing for enums
  431. return s.Replace ("\n", "").Replace ("\t", "").ToLower ();
  432. }
  433. [Test]
  434. public void Rows ()
  435. {
  436. TestTable t = new TestTable ();
  437. Assert.AreEqual (0, t.Rows.Count, "0");
  438. TableRow tr = new TableRow ();
  439. t.Rows.Add (tr);
  440. Assert.AreEqual (1, t.Rows.Count, "r1");
  441. Assert.AreEqual (1, t.Controls.Count, "c1");
  442. string s = t.Render ();
  443. #if NET_4_0
  444. Assert.AreEqual (Adjust ("<table>\n\t<tr>\n\n\t</tr>\n</table>"), Adjust (s), "tr-1");
  445. #else
  446. Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr>\n\n\t</tr>\n</table>"), Adjust (s), "tr-1");
  447. #endif
  448. // change instance properties
  449. tr.HorizontalAlign = HorizontalAlign.Justify;
  450. s = t.Render ();
  451. #if NET_4_0
  452. Assert.AreEqual (Adjust ("<table>\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1j");
  453. #else
  454. Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1j");
  455. #endif
  456. // add it again (same instance)
  457. t.Rows.Add (tr);
  458. Assert.AreEqual (1, t.Rows.Count, "t1bis");
  459. Assert.AreEqual (1, t.Controls.Count, "c1bis");
  460. s = t.Render ();
  461. #if NET_4_0
  462. Assert.AreEqual (Adjust ("<table>\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1bis");
  463. #else
  464. Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1bis");
  465. #endif
  466. tr.HorizontalAlign = HorizontalAlign.NotSet;
  467. tr = new TableRow ();
  468. tr.HorizontalAlign = HorizontalAlign.Justify;
  469. t.Rows.Add (tr);
  470. Assert.AreEqual (2, t.Rows.Count, "r2");
  471. Assert.AreEqual (2, t.Controls.Count, "c2");
  472. s = t.Render ();
  473. #if NET_4_0
  474. Assert.AreEqual (Adjust ("<table>\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2");
  475. #else
  476. Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2");
  477. #endif
  478. tr = new TableRow ();
  479. tr.VerticalAlign = VerticalAlign.Bottom;
  480. t.Controls.Add (tr);
  481. Assert.AreEqual (3, t.Rows.Count, "r3");
  482. Assert.AreEqual (3, t.Controls.Count, "c3");
  483. s = t.Render ();
  484. #if NET_4_0
  485. Assert.AreEqual (Adjust ("<table>\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-3");
  486. #else
  487. Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-3");
  488. #endif
  489. t.Caption = "caption";
  490. s = t.Render ();
  491. #if NET_4_0
  492. Assert.AreEqual (Adjust ("<table>\n\t<caption>\n\t\tcaption\n\t</caption><tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2c");
  493. #else
  494. Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<caption>\n\t\tcaption\n\t</caption><tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2c");
  495. #endif
  496. }
  497. [Test]
  498. [ExpectedException (typeof (NullReferenceException))]
  499. public void ControlsAdd_Null ()
  500. {
  501. new Table ().Controls.Add (null);
  502. }
  503. [Test]
  504. [ExpectedException (typeof (ArgumentException))]
  505. public void ControlsAdd_LiteralControl ()
  506. {
  507. new Table ().Controls.Add (new LiteralControl ("mono"));
  508. }
  509. [Test]
  510. public void ControlsAdd_TableRow ()
  511. {
  512. Table t = new Table ();
  513. t.Controls.Add (new TableRow ());
  514. Assert.AreEqual (1, t.Controls.Count, "Controls");
  515. Assert.AreEqual (1, t.Rows.Count, "Rows");
  516. }
  517. [Test]
  518. public void ControlsAdd_TestTableRow ()
  519. {
  520. Table t = new Table ();
  521. t.Controls.Add (new TestTableRow ());
  522. Assert.AreEqual (1, t.Controls.Count, "Controls");
  523. Assert.AreEqual (1, t.Rows.Count, "Rows");
  524. }
  525. [Test]
  526. [ExpectedException (typeof (NullReferenceException))]
  527. public void ControlsAddAt_Null ()
  528. {
  529. new Table ().Controls.AddAt (0, null);
  530. }
  531. [Test]
  532. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  533. public void ControlsAddAt_Negative ()
  534. {
  535. new Table ().Controls.AddAt (Int32.MinValue, new TableRow ());
  536. }
  537. [Test]
  538. [ExpectedException (typeof (ArgumentException))]
  539. public void ControlsAddAt_LiteralControl ()
  540. {
  541. new Table ().Controls.AddAt (0, new LiteralControl ("mono"));
  542. }
  543. [Test]
  544. public void ControlsAddAt_TableRow ()
  545. {
  546. Table t = new Table ();
  547. t.Controls.AddAt (0, new TableRow ());
  548. Assert.AreEqual (1, t.Controls.Count, "Controls");
  549. Assert.AreEqual (1, t.Rows.Count, "Rows");
  550. }
  551. [Test]
  552. public void ControlsAddAt_TestTableRow ()
  553. {
  554. Table t = new Table ();
  555. t.Controls.AddAt (0, new TestTableRow ());
  556. Assert.AreEqual (1, t.Controls.Count, "Controls");
  557. Assert.AreEqual (1, t.Rows.Count, "Rows");
  558. }
  559. [Test]
  560. [ExpectedException (typeof (NullReferenceException))]
  561. public void RenderBeginTag_Null ()
  562. {
  563. Table t = new Table ();
  564. t.RenderBeginTag (null);
  565. }
  566. [Test]
  567. public void RenderBeginTag_Empty ()
  568. {
  569. HtmlTextWriter writer = GetWriter ();
  570. Table t = new Table ();
  571. t.RenderBeginTag (writer);
  572. string s = writer.InnerWriter.ToString ();
  573. #if NET_4_0
  574. Assert.AreEqual ("<table>\n", s, "empty");
  575. #else
  576. Assert.AreEqual ("<table border=\"0\">\n", s, "empty");
  577. #endif
  578. }
  579. [Test]
  580. public void RenderBeginTag_Attributes ()
  581. {
  582. HtmlTextWriter writer = GetWriter ();
  583. Table t = new Table ();
  584. t.CellPadding = 1;
  585. t.RenderBeginTag (writer);
  586. string s = writer.InnerWriter.ToString ();
  587. #if NET_4_0
  588. Assert.AreEqual ("<table cellpadding=\"1\">\n", s, "CellPadding");
  589. #else
  590. Assert.AreEqual ("<table cellpadding=\"1\" border=\"0\">\n", s, "CellPadding");
  591. #endif
  592. }
  593. [Test]
  594. public void RenderBeginTag_Caption ()
  595. {
  596. HtmlTextWriter writer = GetWriter ();
  597. Table t = new Table ();
  598. t.Caption = "caption";
  599. t.RenderBeginTag (writer);
  600. string s = writer.InnerWriter.ToString ();
  601. #if NET_4_0
  602. Assert.AreEqual ("<table>\n\t<caption>\n\t\tcaption\n\t</caption>", s, "caption");
  603. #else
  604. Assert.AreEqual ("<table border=\"0\">\n\t<caption>\n\t\tcaption\n\t</caption>", s, "caption");
  605. #endif
  606. }
  607. [Test]
  608. public void RenderBeginTag_Caption_Align ()
  609. {
  610. HtmlTextWriter writer = GetWriter ();
  611. Table t = new Table ();
  612. t.Caption = "caption";
  613. t.CaptionAlign = TableCaptionAlign.Top;
  614. t.RenderBeginTag (writer);
  615. string s = writer.InnerWriter.ToString ();
  616. #if NET_4_0
  617. Assert.AreEqual ("<table>\n\t<caption align=\"top\">\n\t\tcaption\n\t</caption>", s.ToLower (), "caption");
  618. #else
  619. Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"top\">\n\t\tcaption\n\t</caption>", s.ToLower (), "caption");
  620. #endif
  621. }
  622. [Test]
  623. public void RenderBeginTag_Row ()
  624. {
  625. HtmlTextWriter writer = GetWriter ();
  626. Table t = new Table ();
  627. t.Rows.Add (new TableRow ());
  628. t.RenderBeginTag (writer);
  629. string s = writer.InnerWriter.ToString ();
  630. #if NET_4_0
  631. Assert.AreEqual ("<table>\n", s, "tr");
  632. #else
  633. Assert.AreEqual ("<table border=\"0\">\n", s, "tr");
  634. #endif
  635. }
  636. #if NET_2_0
  637. [Test]
  638. [Category("NunitWeb")] // Note: No event fired , only flow been checked.
  639. public void RaisePostBackEvent ()
  640. {
  641. WebTest t = new WebTest (PageInvoker.CreateOnLoad (RaisePostBackEvent__Init));
  642. string str = t.Run ();
  643. FormRequest fr = new FormRequest (t.Response, "form1");
  644. fr.Controls.Add ("__EVENTTARGET");
  645. fr.Controls.Add ("__EVENTARGUMENT");
  646. fr.Controls["__EVENTTARGET"].Value = "Table";
  647. fr.Controls["__EVENTARGUMENT"].Value = "";
  648. t.Request = fr;
  649. t.Run ();
  650. Assert.AreEqual ("RaisePostBackEvent", (String) t.UserData, "RaisePostBackEvent");
  651. }
  652. public static void RaisePostBackEvent__Init (Page page)
  653. {
  654. TestTable t = new TestTable ();
  655. t.ID = "Table";
  656. page.Form.Controls.Add (t);
  657. }
  658. [TestFixtureTearDown]
  659. public void TearDown ()
  660. {
  661. WebTest.Unload ();
  662. }
  663. #endif
  664. }
  665. }