StyleTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. //
  2. // Tests for System.Web.UI.WebControls.Style.cs
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  6. //
  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 NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Globalization;
  35. using refl = System.Reflection;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. namespace MonoTests.System.Web.UI.WebControls
  40. {
  41. public class StyleTestClass : Style {
  42. public StyleTestClass ()
  43. : base ()
  44. {
  45. }
  46. public StyleTestClass (StateBag bag)
  47. : base (bag)
  48. {
  49. }
  50. public StateBag StateBag {
  51. get { return base.ViewState; }
  52. }
  53. public bool Empty {
  54. get { return base.IsEmpty; }
  55. }
  56. public bool IsTracking {
  57. get { return base.IsTrackingViewState; }
  58. }
  59. #if NET_2_0
  60. public void SetCssClass(string name) {
  61. Type style = Type.GetType("System.Web.UI.WebControls.Style, System.Web");
  62. if (style != null) {
  63. refl.MethodInfo methodInfo = style.GetMethod("SetRegisteredCssClass",refl.BindingFlags.NonPublic | refl.BindingFlags.Instance);
  64. if (methodInfo != null) {
  65. object[] parameters = new object[] {name};
  66. methodInfo.Invoke(this, parameters);
  67. }
  68. }
  69. }
  70. #endif
  71. public string[] KeyValuePairs() {
  72. IEnumerator e;
  73. string[] result;
  74. int item;
  75. e = ViewState.GetEnumerator();
  76. result = new string[ViewState.Keys.Count];
  77. item = 0;
  78. while (e.MoveNext()) {
  79. DictionaryEntry d;
  80. StateItem si;
  81. d = (DictionaryEntry)e.Current;
  82. si = (StateItem)d.Value;
  83. if (si.Value is String[]) {
  84. string[] values;
  85. values = (string[]) si.Value;
  86. result[item] = d.Key.ToString() + "=";
  87. if (values.Length > 0) {
  88. result[item] += values[0];
  89. for (int i = 1; i < values.Length; i++) {
  90. result[item] += ", " + values[i];
  91. }
  92. }
  93. } else {
  94. result[item] = d.Key.ToString() + "=" + si.Value;
  95. }
  96. item++;
  97. }
  98. return result;
  99. }
  100. }
  101. [TestFixture]
  102. public class StyleTest {
  103. private static HtmlTextWriter GetWriter () {
  104. StringWriter sw = new StringWriter ();
  105. sw.NewLine = "\n";
  106. return new HtmlTextWriter (sw);
  107. }
  108. private void SetSomeValues(Style s) {
  109. s.BackColor = Color.Red;
  110. s.ForeColor = Color.Green;
  111. s.Width = new Unit(10);
  112. s.Font.Bold = true;
  113. }
  114. private void SetAllValues(Style s) {
  115. s.BackColor = Color.Red;
  116. s.BorderColor = Color.Green;
  117. s.BorderStyle = BorderStyle.None;
  118. s.BorderWidth = new Unit(1);
  119. s.CssClass = "Boing";
  120. s.Font.Bold = true;
  121. s.Font.Italic = true;
  122. s.Font.Names = new string[2] {"namelist1", "namelist2"};
  123. //s.Font.Name = string.Empty;
  124. //s.Font.Names = new string[0];
  125. //Console.WriteLine("Font name after setting name: {0}", s.Font.ToString());
  126. s.Font.Overline = true;
  127. s.Font.Size = new FontUnit(1);
  128. //Console.WriteLine("Font name after setting size: {0}", s.Font.ToString());
  129. s.Font.Strikeout = true;
  130. s.Font.Underline = true;
  131. s.ForeColor = Color.Blue;
  132. s.Height = new Unit(2);
  133. s.Width = new Unit(3);
  134. }
  135. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  136. int matches;
  137. bool[] notfound;
  138. if (a1.Length != a2.Length) {
  139. if (assertion != null) {
  140. Assert.Fail(assertion + "( different length )");
  141. }
  142. return false;
  143. }
  144. matches = 0;
  145. notfound = new bool[a1.Length];
  146. for (int i = 0; i < a1.Length; i++) {
  147. for (int j = 0; j < a2.Length; j++) {
  148. if (a1[i].Equals(a2[j])) {
  149. matches++;
  150. break;
  151. }
  152. }
  153. if ((assertion != null) && (matches != i+1)) {
  154. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  155. }
  156. }
  157. return matches == a1.Length;
  158. }
  159. [Test]
  160. public void Style_Defaults ()
  161. {
  162. Style s = new Style ();
  163. Assert.AreEqual (s.BackColor, Color.Empty, "Default1");
  164. Assert.AreEqual (s.BorderColor, Color.Empty, "Default22");
  165. Assert.AreEqual (s.BorderStyle, BorderStyle.NotSet, "Default3");
  166. Assert.AreEqual (s.BorderWidth, Unit.Empty, "Default4");
  167. Assert.AreEqual (s.CssClass, string.Empty, "Default5");
  168. Assert.AreEqual (s.ForeColor, Color.Empty, "Default6");
  169. Assert.AreEqual (s.Height, Unit.Empty, "Default7");
  170. Assert.AreEqual (s.Width, Unit.Empty, "Default8");
  171. }
  172. [Test]
  173. public void Style_State () {
  174. string[] keyvalues;
  175. string[] expect1 = {
  176. "BorderStyle=None",
  177. "Font_Bold=True",
  178. "Font_Italic=True",
  179. "Height=2px",
  180. "CssClass=Boing",
  181. "BorderWidth=1px",
  182. "ForeColor=Color [Blue]",
  183. "Font_Size=1pt",
  184. "Font_Overline=True",
  185. "Width=3px",
  186. "BorderColor=Color [Green]",
  187. "Font_Names=namelist1, namelist2",
  188. "Font_Underline=True",
  189. "BackColor=Color [Red]",
  190. "Font_Strikeout=True" };
  191. string[] expect2 = {
  192. "BorderStyle=None",
  193. "Font_Bold=True",
  194. "Font_Italic=True",
  195. "Height=2px",
  196. "CssClass=Boing",
  197. "BorderWidth=1px",
  198. "ForeColor=Color [Blue]",
  199. "Font_Size=1pt",
  200. "Font_Overline=True",
  201. "Width=3px",
  202. "BorderColor=Color [Green]",
  203. "Font_Underline=True",
  204. "BackColor=Color [Red]",
  205. "Font_Strikeout=True" };
  206. string[] expect3 = {
  207. "BorderStyle=None",
  208. "Font_Bold=True",
  209. "Font_Italic=True",
  210. "Height=2px",
  211. "CssClass=Boing",
  212. "BorderWidth=1px",
  213. "ForeColor=Color [Blue]",
  214. "Font_Size=1pt",
  215. "Font_Overline=True",
  216. "Width=3px",
  217. "BorderColor=Color [Green]",
  218. "Font_Names=",
  219. "Font_Underline=True",
  220. "BackColor=Color [Red]",
  221. "Font_Strikeout=True" };
  222. StyleTestClass s;
  223. StyleTestClass copy;
  224. s = new StyleTestClass();
  225. SetAllValues(s);
  226. keyvalues = s.KeyValuePairs();
  227. Assert.AreEqual (15, keyvalues.Length, "State1");
  228. IsEqual(keyvalues, expect1, "State2");
  229. s.Font.Name = string.Empty;
  230. keyvalues = s.KeyValuePairs();
  231. Assert.AreEqual (expect2.Length, keyvalues.Length, "State3");
  232. IsEqual(keyvalues, expect2, "State4");
  233. s.Font.Names = null;
  234. keyvalues = s.KeyValuePairs();
  235. Assert.AreEqual (expect2.Length, keyvalues.Length, "State5");
  236. IsEqual(keyvalues, expect2, "State6");
  237. copy = new StyleTestClass();
  238. copy.CopyFrom(s);
  239. keyvalues = copy.KeyValuePairs();
  240. Assert.AreEqual (expect3.Length, keyvalues.Length, "State7");
  241. IsEqual(keyvalues, expect3, "State8");
  242. Assert.AreEqual (false, copy.IsTracking, "State9");
  243. }
  244. [Test]
  245. public void Style_Merge ()
  246. {
  247. Style s = new Style ();
  248. Style copy = new Style ();
  249. SetSomeValues(s);
  250. copy.ForeColor = Color.Blue;
  251. copy.MergeWith(s);
  252. Assert.AreEqual (Color.Red, copy.BackColor, "Merge1");
  253. Assert.AreEqual (Color.Blue, copy.ForeColor, "Merge2");
  254. // Don't fail here
  255. copy.MergeWith(null);
  256. }
  257. [Test]
  258. public void Style_Copy ()
  259. {
  260. Style s = new Style ();
  261. Style copy = new Style ();
  262. SetSomeValues(s);
  263. copy.CopyFrom (s);
  264. Assert.AreEqual (Color.Red, s.BackColor, "Copy1");
  265. }
  266. #if NET_2_0
  267. [Test]
  268. public void Style_CssClass ()
  269. {
  270. StyleTestClass s = new StyleTestClass ();
  271. Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css1");
  272. s.SetCssClass ("blah");
  273. Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css2");
  274. s.BackColor = Color.AliceBlue;
  275. Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css3");
  276. }
  277. #endif
  278. [Test]
  279. public void StyleFonts () {
  280. Style s = new Style ();
  281. Assert.AreEqual(new string[0], s.Font.Names, "F1");
  282. s.Font.Name = string.Empty;
  283. Assert.AreEqual(new string[0], s.Font.Names, "F2");
  284. s.Font.Names = null;
  285. Assert.AreEqual(new string[0], s.Font.Names, "F3");
  286. }
  287. [Test]
  288. [ExpectedException(typeof(ArgumentNullException))]
  289. public void NullException1 ()
  290. {
  291. Style s = new Style ();
  292. s.Font.Name = null;
  293. }
  294. private Style GetStyle ()
  295. {
  296. Style s = new Style ();
  297. s.BackColor = Color.Aqua;
  298. s.BorderWidth = Unit.Pixel (1);
  299. return s;
  300. }
  301. private void CheckStyle (Style s)
  302. {
  303. Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
  304. Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
  305. }
  306. [Test]
  307. public void CopyFrom_Null ()
  308. {
  309. Style s = GetStyle ();
  310. s.CopyFrom (null);
  311. CheckStyle (s);
  312. }
  313. [Test]
  314. public void CopyFrom_Self ()
  315. {
  316. Style s = GetStyle ();
  317. s.CopyFrom (s);
  318. CheckStyle (s);
  319. }
  320. [Test]
  321. public void CopyFrom_Empty ()
  322. {
  323. StyleTestClass s = new StyleTestClass ();
  324. s.CopyFrom (new Style ());
  325. Assert.IsTrue (s.Empty, "Empty");
  326. }
  327. [Test]
  328. public void CopyFrom ()
  329. {
  330. Style c = GetStyle ();
  331. Style s = GetStyle ();
  332. c.BorderColor = Color.Azure;
  333. c.BorderWidth = Unit.Empty;
  334. c.CopyFrom (s);
  335. CheckStyle (c);
  336. Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
  337. // CopyFrom doesn't do a Reset
  338. }
  339. [Test]
  340. public void CopyFrom_IsEmpty ()
  341. {
  342. StyleTestClass c = new StyleTestClass ();
  343. Style s = GetStyle ();
  344. s.BorderColor = Color.Azure;
  345. s.BorderWidth = Unit.Empty;
  346. c.CopyFrom (s);
  347. Assert.IsFalse (c.Empty, "IsEmpty");
  348. }
  349. [Test]
  350. public void Constructor_StateBag_Null ()
  351. {
  352. StyleTestClass s = new StyleTestClass (null);
  353. Assert.IsNotNull (s.StateBag, "StateBag");
  354. s.CssClass = "mono";
  355. Assert.AreEqual ("mono", s.CssClass, "CssClass");
  356. }
  357. [Test]
  358. public void Empty ()
  359. {
  360. StyleTestClass s = new StyleTestClass ();
  361. Assert.IsTrue (s.Empty, "Empty");
  362. Assert.AreEqual (0, s.StateBag.Count, "Count");
  363. s.StateBag["Mono"] = "go!";
  364. Assert.IsTrue (s.Empty, "Still Empty");
  365. Assert.AreEqual (1, s.StateBag.Count, "Count");
  366. }
  367. [Test]
  368. public void FontInfo_Empty ()
  369. {
  370. FontInfo f;
  371. StyleTestClass s = new StyleTestClass ();
  372. f = s.Font;
  373. Assert.IsTrue (s.Empty, "Empty after getter");
  374. s.Font.Name = "Arial";
  375. Assert.IsFalse (s.Empty, "No longer empty");
  376. }
  377. public void Render ()
  378. {
  379. HtmlTextWriter writer;
  380. StyleTestClass s;
  381. writer = StyleTest.GetWriter();
  382. s = new StyleTestClass ();
  383. s.BorderColor = Color.BlueViolet;
  384. s.AddAttributesToRender(writer);
  385. // Figure out an order-independent way to verify rendered results
  386. }
  387. }
  388. }