2
0

TableStyleTest.cs 11 KB

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