TableStyleTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // TableStyleTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.TableStyle
  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 TestTableStyle : TableStyle {
  37. public TestTableStyle ()
  38. : base ()
  39. {
  40. }
  41. public TestTableStyle (StateBag bag)
  42. : base (bag)
  43. {
  44. }
  45. public bool Empty {
  46. get { return base.IsEmpty; }
  47. }
  48. public StateBag StateBag {
  49. get { return base.ViewState; }
  50. }
  51. public void LoadVS (object o)
  52. {
  53. LoadViewState (o);
  54. }
  55. public void TrackVS ()
  56. {
  57. TrackViewState ();
  58. }
  59. public object SaveVS ()
  60. {
  61. return SaveViewState ();
  62. }
  63. #if NET_2_0
  64. public void Fill (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  65. {
  66. base.FillStyleAttributes (attributes, urlResolver);
  67. }
  68. #endif
  69. }
  70. #if NET_2_0
  71. public class TestResolutionService : IUrlResolutionService {
  72. public string ResolveClientUrl (string relativeUrl)
  73. {
  74. return "http://www.mono-project.com";
  75. }
  76. }
  77. #endif
  78. [TestFixture]
  79. public class TableStyleTest {
  80. private const string imageUrl = "http://www.mono-project.com/stylesheets/images.wiki.png";
  81. private void DefaultProperties (TestTableStyle ts)
  82. {
  83. Assert.AreEqual (0, ts.StateBag.Count, "ViewState.Count");
  84. Assert.AreEqual (String.Empty, ts.BackImageUrl, "BackImageUrl");
  85. Assert.AreEqual (-1, ts.CellPadding, "CellPadding");
  86. Assert.AreEqual (-1, ts.CellSpacing, "CellSpacing");
  87. // LAMESPEC: default is document to be GridLines.Both
  88. Assert.AreEqual (GridLines.None, ts.GridLines, "GridLines");
  89. Assert.AreEqual (HorizontalAlign.NotSet, ts.HorizontalAlign, "HorizontalAlign");
  90. Assert.AreEqual (0, ts.StateBag.Count, "ViewState.Count-2");
  91. ts.Reset ();
  92. Assert.AreEqual (0, ts.StateBag.Count, "Reset");
  93. }
  94. private void NullProperties (TestTableStyle ts)
  95. {
  96. Assert.IsTrue (ts.Empty, "Empty");
  97. ts.BackImageUrl = String.Empty; // doesn't accept null, see specific test
  98. Assert.AreEqual (String.Empty, ts.BackImageUrl, "BackImageUrl");
  99. Assert.IsFalse (ts.Empty, "!Empty");
  100. ts.CellPadding = -1;
  101. Assert.AreEqual (-1, ts.CellPadding, "CellPadding");
  102. ts.CellSpacing = -1;
  103. Assert.AreEqual (-1, ts.CellSpacing, "CellSpacing");
  104. ts.GridLines = GridLines.None;
  105. Assert.AreEqual (GridLines.None, ts.GridLines, "GridLines");
  106. ts.HorizontalAlign = HorizontalAlign.NotSet;
  107. Assert.AreEqual (HorizontalAlign.NotSet, ts.HorizontalAlign, "HorizontalAlign");
  108. Assert.AreEqual (5, ts.StateBag.Count, "ViewState.Count-1");
  109. ts.Reset ();
  110. Assert.AreEqual (0, ts.StateBag.Count, "Reset");
  111. Assert.IsTrue (ts.Empty, "Empty/Reset");
  112. }
  113. [Test]
  114. public void Constructor_Default ()
  115. {
  116. TestTableStyle ts = new TestTableStyle ();
  117. DefaultProperties (ts);
  118. NullProperties (ts);
  119. }
  120. [Test]
  121. public void Constructor_StateBag_Null ()
  122. {
  123. TestTableStyle ts = new TestTableStyle (null);
  124. Assert.IsNotNull (ts.StateBag, "StateBag");
  125. DefaultProperties (ts);
  126. NullProperties (ts);
  127. }
  128. [Test]
  129. public void Constructor_StateBag ()
  130. {
  131. TestTableStyle ts = new TestTableStyle (new StateBag ());
  132. Assert.IsNotNull (ts.StateBag, "StateBag");
  133. DefaultProperties (ts);
  134. NullProperties (ts);
  135. }
  136. [Test]
  137. [ExpectedException (typeof (ArgumentNullException))]
  138. public void BackImageUrl_Null ()
  139. {
  140. TableStyle ts = new TableStyle ();
  141. ts.BackImageUrl = null;
  142. }
  143. [Test]
  144. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  145. public void CellPadding_Invalid ()
  146. {
  147. TableStyle ts = new TableStyle ();
  148. ts.CellPadding = Int32.MinValue;
  149. }
  150. [Test]
  151. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  152. public void CellSpacing_Invalid ()
  153. {
  154. TableStyle ts = new TableStyle ();
  155. ts.CellSpacing = Int32.MinValue;
  156. }
  157. [Test]
  158. // LAMESPEC: documented as ArgumentException
  159. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  160. public void GridLines_Invalid ()
  161. {
  162. TableStyle ts = new TableStyle ();
  163. ts.GridLines = (GridLines)Int32.MinValue;
  164. }
  165. [Test]
  166. // LAMESPEC: documented as ArgumentException
  167. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  168. public void HorizontalAlign_Invalid ()
  169. {
  170. TableStyle ts = new TableStyle ();
  171. ts.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
  172. }
  173. [Test]
  174. public void AddAttributesToRender_Null_WebControl ()
  175. {
  176. TableStyle ts = new TableStyle ();
  177. ts.AddAttributesToRender (null, new Table ());
  178. // no exception
  179. }
  180. [Test]
  181. public void AddAttributesToRender_HtmlTextWriter_Null ()
  182. {
  183. TableStyle ts = GetTableStyle ();
  184. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  185. ts.AddAttributesToRender (writer, null);
  186. Assert.AreEqual (String.Empty, writer.InnerWriter.ToString (), "empty");
  187. }
  188. [Test]
  189. public void AddAttributesToRender ()
  190. {
  191. TableStyle ts = GetTableStyle ();
  192. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  193. ts.AddAttributesToRender (writer, new Table ());
  194. Assert.AreEqual (String.Empty, writer.InnerWriter.ToString (), "empty");
  195. }
  196. private TableStyle GetTableStyle ()
  197. {
  198. TableStyle ts = new TableStyle ();
  199. ts.BackImageUrl = imageUrl;
  200. ts.CellPadding = 1;
  201. ts.CellSpacing = 2;
  202. ts.GridLines = GridLines.Both;
  203. ts.HorizontalAlign = HorizontalAlign.Justify;
  204. return ts;
  205. }
  206. private void CheckTableStyle (TableStyle ts)
  207. {
  208. Assert.AreEqual (imageUrl, ts.BackImageUrl, "BackImageUrl");
  209. Assert.AreEqual (1, ts.CellPadding, "CellPadding");
  210. Assert.AreEqual (2, ts.CellSpacing, "CellSpacing");
  211. Assert.AreEqual (GridLines.Both, ts.GridLines, "GridLines");
  212. Assert.AreEqual (HorizontalAlign.Justify, ts.HorizontalAlign, "HorizontalAlign");
  213. }
  214. [Test]
  215. public void CopyFrom_Null ()
  216. {
  217. TableStyle ts = GetTableStyle ();
  218. ts.CopyFrom (null);
  219. CheckTableStyle (ts);
  220. }
  221. [Test]
  222. public void CopyFrom_Self ()
  223. {
  224. TableStyle ts = GetTableStyle ();
  225. ts.CopyFrom (ts);
  226. CheckTableStyle (ts);
  227. }
  228. [Test]
  229. public void CopyFrom_Empty ()
  230. {
  231. TestTableStyle ts = new TestTableStyle ();
  232. ts.CopyFrom (new TableStyle ());
  233. DefaultProperties (ts);
  234. }
  235. [Test]
  236. public void CopyFrom_IsEmpty ()
  237. {
  238. TestTableStyle c = new TestTableStyle ();
  239. TableStyle s = new TableStyle ();
  240. s.BorderWidth = Unit.Empty;
  241. c.CopyFrom (s);
  242. Assert.IsTrue (c.Empty, "A1");
  243. s.GridLines = GridLines.Both;
  244. c.CopyFrom (s);
  245. Assert.IsFalse (c.Empty, "A2");
  246. }
  247. [Test]
  248. public void CopyFrom ()
  249. {
  250. TableStyle ts = new TableStyle ();
  251. ts.BackImageUrl = imageUrl + "1";
  252. ts.CellPadding = 2;
  253. ts.CellSpacing = 3;
  254. ts.GridLines = GridLines.Horizontal;
  255. ts.HorizontalAlign = HorizontalAlign.Left;
  256. ts.CopyFrom (GetTableStyle ());
  257. CheckTableStyle (ts);
  258. }
  259. [Test]
  260. public void MergeWith_Null ()
  261. {
  262. TableStyle ts = GetTableStyle ();
  263. ts.MergeWith (null);
  264. CheckTableStyle (ts);
  265. }
  266. [Test]
  267. public void MergeWith_Self ()
  268. {
  269. TableStyle ts = GetTableStyle ();
  270. ts.MergeWith (ts);
  271. CheckTableStyle (ts);
  272. }
  273. [Test]
  274. public void MergeWith_Empty ()
  275. {
  276. TestTableStyle ts = new TestTableStyle ();
  277. ts.MergeWith (new TableStyle ());
  278. DefaultProperties (ts);
  279. }
  280. [Test]
  281. public void MergeWith ()
  282. {
  283. TableStyle ts = new TableStyle ();
  284. ts.BackImageUrl = imageUrl + "1";
  285. ts.CellPadding = 2;
  286. ts.CellSpacing = 3;
  287. ts.GridLines = GridLines.Horizontal;
  288. ts.HorizontalAlign = HorizontalAlign.Left;
  289. ts.MergeWith (GetTableStyle ());
  290. Assert.AreEqual (imageUrl + "1", ts.BackImageUrl, "BackImageUrl");
  291. Assert.AreEqual (2, ts.CellPadding, "CellPadding");
  292. Assert.AreEqual (3, ts.CellSpacing, "CellSpacing");
  293. Assert.AreEqual (GridLines.Horizontal, ts.GridLines, "GridLines");
  294. Assert.AreEqual (HorizontalAlign.Left, ts.HorizontalAlign, "HorizontalAlign");
  295. }
  296. [Test]
  297. public void GridLines_VS ()
  298. {
  299. TestTableStyle ts = new TestTableStyle ();
  300. ts.TrackVS ();
  301. ts.GridLines = GridLines.Both;
  302. object o = ts.SaveVS ();
  303. ts = new TestTableStyle ();
  304. ts.LoadVS (o);
  305. Assert.AreEqual (GridLines.Both, ts.GridLines, "GL");
  306. }
  307. #if NET_2_0
  308. private CssStyleCollection GetCssCollection ()
  309. {
  310. return new AttributeCollection (new StateBag ()).CssStyle;
  311. }
  312. [Test]
  313. public void FillStyleAttributes_Null_Resolver ()
  314. {
  315. TestTableStyle ts = new TestTableStyle ();
  316. ts.Fill (null, new TestResolutionService ());
  317. // no exception
  318. }
  319. [Test]
  320. public void FillStyleAttributes_Css_Null ()
  321. {
  322. TestTableStyle ts = new TestTableStyle ();
  323. ts.Fill (GetCssCollection (), null);
  324. // no exception
  325. }
  326. [Test]
  327. public void FillStyleAttributes_Empty ()
  328. {
  329. CssStyleCollection css = GetCssCollection ();
  330. TestTableStyle ts = new TestTableStyle ();
  331. ts.Fill (css, new TestResolutionService ());
  332. Assert.AreEqual (0, css.Count, "Count");
  333. }
  334. [Test]
  335. public void FillStyleAttributes_NotCss ()
  336. {
  337. CssStyleCollection css = GetCssCollection ();
  338. TestTableStyle ts = new TestTableStyle ();
  339. ts.CellPadding = 1;
  340. ts.CellSpacing = 1;
  341. ts.GridLines = GridLines.Both;
  342. ts.HorizontalAlign = HorizontalAlign.Justify;
  343. ts.Fill (css, new TestResolutionService ());
  344. Assert.AreEqual (0, css.Count, "Count");
  345. }
  346. [Test]
  347. public void FillStyleAttributes_Css_WithoutResolution ()
  348. {
  349. CssStyleCollection css = GetCssCollection ();
  350. TestTableStyle ts = new TestTableStyle ();
  351. ts.BackImageUrl = "http://www.go-mono.com";
  352. ts.Fill (css, null);
  353. Assert.AreEqual (1, css.Count, "Count");
  354. Assert.AreEqual ("http://www.go-mono.com", css["background-image"], "css[string]");
  355. Assert.AreEqual ("http://www.go-mono.com", css[HtmlTextWriterStyle.BackgroundImage], "css[HtmlTextWriterStyle]");
  356. Assert.AreEqual ("background-image:url(http://www.go-mono.com);", css.Value, "css.Value");
  357. }
  358. [Test]
  359. public void FillStyleAttributes_Css_WithResolution ()
  360. {
  361. CssStyleCollection css = GetCssCollection ();
  362. TestTableStyle ts = new TestTableStyle ();
  363. ts.BackImageUrl = "http://www.go-mono.com";
  364. ts.Fill (css, new TestResolutionService ());
  365. Assert.AreEqual (1, css.Count, "Count");
  366. Assert.AreEqual ("http://www.mono-project.com", css["background-image"], "css[string]");
  367. Assert.AreEqual ("http://www.mono-project.com", css[HtmlTextWriterStyle.BackgroundImage], "css[HtmlTextWriterStyle]");
  368. Assert.AreEqual ("background-image:url(http://www.mono-project.com);", css.Value, "css.Value");
  369. Assert.AreEqual ("http://www.go-mono.com", ts.BackImageUrl, "BackImageUrl");
  370. }
  371. #endif
  372. }
  373. }