StyleTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. using MonoTests.SystemWeb.Framework;
  40. using MonoTests.stand_alone.WebHarness;
  41. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. public class StyleTestClass : Style {
  44. public StyleTestClass ()
  45. : base ()
  46. {
  47. }
  48. public StyleTestClass (StateBag bag)
  49. : base (bag)
  50. {
  51. }
  52. public StateBag StateBag {
  53. get { return base.ViewState; }
  54. }
  55. public bool Empty {
  56. get { return base.IsEmpty; }
  57. }
  58. public bool IsTracking {
  59. get { return base.IsTrackingViewState; }
  60. }
  61. #if NET_2_0
  62. public void SetCssClass(string name) {
  63. Type style = typeof (Style);
  64. if (style != null) {
  65. refl.MethodInfo methodInfo = style.GetMethod("SetRegisteredCssClass",refl.BindingFlags.NonPublic | refl.BindingFlags.Instance);
  66. if (methodInfo != null) {
  67. object[] parameters = new object[] {name};
  68. methodInfo.Invoke(this, parameters);
  69. }
  70. }
  71. }
  72. public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner) {
  73. base.AddAttributesToRender (writer, owner);
  74. }
  75. protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver) {
  76. base.FillStyleAttributes (attributes, urlResolver);
  77. attributes.Add ("FillStyleAttributes", "FillStyleAttributes");
  78. }
  79. #endif
  80. public string[] KeyValuePairs() {
  81. IEnumerator e;
  82. string[] result;
  83. int item;
  84. e = ViewState.GetEnumerator();
  85. result = new string[ViewState.Keys.Count];
  86. item = 0;
  87. while (e.MoveNext()) {
  88. DictionaryEntry d;
  89. StateItem si;
  90. d = (DictionaryEntry)e.Current;
  91. si = (StateItem)d.Value;
  92. if (si.Value is String[]) {
  93. string[] values;
  94. values = (string[]) si.Value;
  95. result[item] = d.Key.ToString() + "=";
  96. if (values.Length > 0) {
  97. result[item] += values[0];
  98. for (int i = 1; i < values.Length; i++) {
  99. result[item] += ", " + values[i];
  100. }
  101. }
  102. } else {
  103. result[item] = d.Key.ToString() + "=" + si.Value;
  104. }
  105. item++;
  106. }
  107. return result;
  108. }
  109. }
  110. [TestFixture]
  111. public class StyleTest {
  112. private static HtmlTextWriter GetWriter () {
  113. StringWriter sw = new StringWriter ();
  114. sw.NewLine = "\n";
  115. return new HtmlTextWriter (sw);
  116. }
  117. private void SetSomeValues(Style s) {
  118. s.BackColor = Color.Red;
  119. s.ForeColor = Color.Green;
  120. s.Width = new Unit(10);
  121. s.Font.Bold = true;
  122. }
  123. private void SetAllValues(Style s) {
  124. s.BackColor = Color.Red;
  125. s.BorderColor = Color.Green;
  126. s.BorderStyle = BorderStyle.None;
  127. s.BorderWidth = new Unit(1);
  128. s.CssClass = "Boing";
  129. s.Font.Bold = true;
  130. s.Font.Italic = true;
  131. s.Font.Names = new string[2] {"namelist1", "namelist2"};
  132. //s.Font.Name = string.Empty;
  133. //s.Font.Names = new string[0];
  134. //Console.WriteLine("Font name after setting name: {0}", s.Font.ToString());
  135. s.Font.Overline = true;
  136. s.Font.Size = new FontUnit(1);
  137. //Console.WriteLine("Font name after setting size: {0}", s.Font.ToString());
  138. s.Font.Strikeout = true;
  139. s.Font.Underline = true;
  140. s.ForeColor = Color.Blue;
  141. s.Height = new Unit(2);
  142. s.Width = new Unit(3);
  143. }
  144. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  145. int matches;
  146. bool[] notfound;
  147. if (a1.Length != a2.Length) {
  148. if (assertion != null) {
  149. Assert.Fail(assertion + "( different length )");
  150. }
  151. return false;
  152. }
  153. matches = 0;
  154. notfound = new bool[a1.Length];
  155. for (int i = 0; i < a1.Length; i++) {
  156. for (int j = 0; j < a2.Length; j++) {
  157. if (a1[i].Equals(a2[j])) {
  158. matches++;
  159. break;
  160. }
  161. }
  162. if ((assertion != null) && (matches != i+1)) {
  163. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  164. }
  165. }
  166. return matches == a1.Length;
  167. }
  168. [Test]
  169. public void Style_Defaults ()
  170. {
  171. Style s = new Style ();
  172. Assert.AreEqual (s.BackColor, Color.Empty, "Default1");
  173. Assert.AreEqual (s.BorderColor, Color.Empty, "Default22");
  174. Assert.AreEqual (s.BorderStyle, BorderStyle.NotSet, "Default3");
  175. Assert.AreEqual (s.BorderWidth, Unit.Empty, "Default4");
  176. Assert.AreEqual (s.CssClass, string.Empty, "Default5");
  177. Assert.AreEqual (s.ForeColor, Color.Empty, "Default6");
  178. Assert.AreEqual (s.Height, Unit.Empty, "Default7");
  179. Assert.AreEqual (s.Width, Unit.Empty, "Default8");
  180. }
  181. [Test]
  182. public void Style_State () {
  183. string[] keyvalues;
  184. string[] expect1 = {
  185. "BorderStyle=None",
  186. "Font_Bold=True",
  187. "Font_Italic=True",
  188. "Height=2px",
  189. "CssClass=Boing",
  190. "BorderWidth=1px",
  191. "ForeColor=Color [Blue]",
  192. "Font_Size=1pt",
  193. "Font_Overline=True",
  194. "Width=3px",
  195. "BorderColor=Color [Green]",
  196. "Font_Names=namelist1, namelist2",
  197. "Font_Underline=True",
  198. "BackColor=Color [Red]",
  199. "Font_Strikeout=True" };
  200. string[] expect2 = {
  201. "BorderStyle=None",
  202. "Font_Bold=True",
  203. "Font_Italic=True",
  204. "Height=2px",
  205. "CssClass=Boing",
  206. "BorderWidth=1px",
  207. "ForeColor=Color [Blue]",
  208. "Font_Size=1pt",
  209. "Font_Overline=True",
  210. "Width=3px",
  211. "BorderColor=Color [Green]",
  212. "Font_Underline=True",
  213. "BackColor=Color [Red]",
  214. "Font_Strikeout=True" };
  215. string[] expect3 = {
  216. "BorderStyle=None",
  217. "Font_Bold=True",
  218. "Font_Italic=True",
  219. "Height=2px",
  220. "CssClass=Boing",
  221. "BorderWidth=1px",
  222. "ForeColor=Color [Blue]",
  223. "Font_Size=1pt",
  224. "Font_Overline=True",
  225. "Width=3px",
  226. "BorderColor=Color [Green]",
  227. "Font_Names=",
  228. "Font_Underline=True",
  229. "BackColor=Color [Red]",
  230. "Font_Strikeout=True" };
  231. StyleTestClass s;
  232. StyleTestClass copy;
  233. s = new StyleTestClass();
  234. SetAllValues(s);
  235. keyvalues = s.KeyValuePairs();
  236. Assert.AreEqual (15, keyvalues.Length, "State1");
  237. IsEqual(keyvalues, expect1, "State2");
  238. s.Font.Name = string.Empty;
  239. keyvalues = s.KeyValuePairs();
  240. Assert.AreEqual (expect2.Length, keyvalues.Length, "State3");
  241. IsEqual(keyvalues, expect2, "State4");
  242. s.Font.Names = null;
  243. keyvalues = s.KeyValuePairs();
  244. Assert.AreEqual (expect2.Length, keyvalues.Length, "State5");
  245. IsEqual(keyvalues, expect2, "State6");
  246. copy = new StyleTestClass();
  247. copy.CopyFrom(s);
  248. keyvalues = copy.KeyValuePairs();
  249. Assert.AreEqual (expect3.Length, keyvalues.Length, "State7");
  250. IsEqual(keyvalues, expect3, "State8");
  251. Assert.AreEqual (false, copy.IsTracking, "State9");
  252. }
  253. [Test]
  254. public void Style_Merge ()
  255. {
  256. Style s = new Style ();
  257. Style copy = new Style ();
  258. SetSomeValues(s);
  259. copy.ForeColor = Color.Blue;
  260. copy.MergeWith(s);
  261. Assert.AreEqual (Color.Red, copy.BackColor, "Merge1");
  262. Assert.AreEqual (Color.Blue, copy.ForeColor, "Merge2");
  263. // Don't fail here
  264. copy.MergeWith(null);
  265. }
  266. [Test]
  267. public void Style_Copy ()
  268. {
  269. Style s = new Style ();
  270. Style copy = new Style ();
  271. SetSomeValues(s);
  272. copy.CopyFrom (s);
  273. Assert.AreEqual (Color.Red, s.BackColor, "Copy1");
  274. }
  275. #if NET_2_0
  276. [Test]
  277. public void Style_CssClass ()
  278. {
  279. StyleTestClass s = new StyleTestClass ();
  280. Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css1");
  281. s.SetCssClass ("blah");
  282. Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
  283. s.BackColor = Color.AliceBlue;
  284. Assert.AreEqual ("blah", s.RegisteredCssClass, "Css3");
  285. }
  286. [Test]
  287. [Category ("NunitWeb")]
  288. public void Style_AddRegisteredCssClassAttribute () {
  289. new WebTest (PageInvoker.CreateOnLoad (Style_AddRegisteredCssClassAttribute_Load)).Run ();
  290. }
  291. public static void Style_AddRegisteredCssClassAttribute_Load (Page p) {
  292. StringWriter sw = new StringWriter ();
  293. HtmlTextWriter tw = new HtmlTextWriter (sw);
  294. Style s = new Style ();
  295. s.CssClass = "MyClass";
  296. s.BackColor = Color.AliceBlue;
  297. s.AddAttributesToRender (tw);
  298. tw.RenderBeginTag ("span");
  299. tw.RenderEndTag ();
  300. Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass\""), "AddRegisteredCssClassAttribute#1");
  301. Assert.AreEqual (true, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#2");
  302. sw = new StringWriter ();
  303. tw = new HtmlTextWriter (sw);
  304. s = new Style ();
  305. s.BackColor = Color.AliceBlue;
  306. p.Header.StyleSheet.RegisterStyle (s, p);
  307. s.AddAttributesToRender (tw);
  308. tw.RenderBeginTag ("span");
  309. tw.RenderEndTag ();
  310. Assert.AreEqual (true, sw.ToString ().Contains ("class"), "AddRegisteredCssClassAttribute#3");
  311. Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#4");
  312. sw = new StringWriter ();
  313. tw = new HtmlTextWriter (sw);
  314. s = new Style ();
  315. s.BackColor = Color.AliceBlue;
  316. s.CssClass = "MyClass";
  317. p.Header.StyleSheet.RegisterStyle (s, p);
  318. s.AddAttributesToRender (tw);
  319. tw.RenderBeginTag ("span");
  320. tw.RenderEndTag ();
  321. Assert.AreEqual (sw.ToString ().LastIndexOf ("class"), sw.ToString ().IndexOf ("class"), "AddRegisteredCssClassAttribute#5");
  322. Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#6");
  323. Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass "), "AddRegisteredCssClassAttribute#7");
  324. s = new Style ();
  325. p.Header.StyleSheet.RegisterStyle (s, p);
  326. Assert.AreEqual (false, s.IsEmpty, "AddRegisteredCssClassAttribute#8");
  327. }
  328. [Test]
  329. public void Style_AddAttributesToRender_use_FillStyleAttributes () {
  330. StringWriter sw = new StringWriter ();
  331. HtmlTextWriter tw = new HtmlTextWriter (sw);
  332. StyleTestClass s = new StyleTestClass ();
  333. s.AddAttributesToRender (tw);
  334. tw.RenderBeginTag ("span");
  335. tw.RenderEndTag ();
  336. HtmlDiff.AssertAreEqual ("<span style=\"FillStyleAttributes:FillStyleAttributes;\" />", sw.ToString (), "AddAttributesToRender_use_FillStyleAttributes#2");
  337. }
  338. [Test]
  339. public void Style_GetStyleAttributes () {
  340. Style s;
  341. CssStyleCollection css;
  342. s = new Style ();
  343. css = s.GetStyleAttributes (null);
  344. Assert.AreEqual (0, css.Count, "GetStyleAttributes#1");
  345. s.Font.Bold = true;
  346. s.Font.Italic = true;
  347. s.Font.Size = 10;
  348. s.Font.Names = new string [] { "Arial", "Veranda" };
  349. s.Font.Overline = true;
  350. s.Font.Strikeout = true;
  351. s.Font.Underline = true;
  352. css = s.GetStyleAttributes (null);
  353. Assert.AreEqual ("bold", css ["font-weight"], "GetStyleAttributes#2");
  354. Assert.AreEqual ("italic", css ["font-style"], "GetStyleAttributes#3");
  355. Assert.AreEqual ("10pt", css ["font-size"], "GetStyleAttributes#4");
  356. Assert.AreEqual ("Arial,Veranda", css ["font-family"], "GetStyleAttributes#5");
  357. Assert.AreEqual (true, css ["text-decoration"].Contains ("overline"), "GetStyleAttributes#6");
  358. Assert.AreEqual (true, css ["text-decoration"].Contains ("line-through"), "GetStyleAttributes#7");
  359. Assert.AreEqual (true, css ["text-decoration"].Contains ("underline"), "GetStyleAttributes#8");
  360. s.Font.Names = null;
  361. css = s.GetStyleAttributes (null);
  362. Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#9");
  363. s.Font.Name = "Arial, Veranda";
  364. css = s.GetStyleAttributes (null);
  365. Assert.AreEqual ("Arial, Veranda", css ["font-family"], "GetStyleAttributes#10");
  366. s.Font.Name = "";
  367. css = s.GetStyleAttributes (null);
  368. Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#11");
  369. s.Font.Bold = false;
  370. s.Font.Italic = false;
  371. s.Font.Size = FontUnit.Empty;
  372. s.Font.Overline = false;
  373. s.Font.Strikeout = false;
  374. s.Font.Underline = false;
  375. css = s.GetStyleAttributes (null);
  376. Assert.AreEqual ("normal", css ["font-weight"], "GetStyleAttributes#12");
  377. Assert.AreEqual ("normal", css ["font-style"], "GetStyleAttributes#13");
  378. Assert.AreEqual (null, css ["font-size"], "GetStyleAttributes#14");
  379. Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#15");
  380. s.Reset ();
  381. css = s.GetStyleAttributes (null);
  382. Assert.AreEqual (0, css.Count, "GetStyleAttributes#16");
  383. s.Reset ();
  384. s.Font.Underline = false;
  385. css = s.GetStyleAttributes (null);
  386. Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#17");
  387. s.Reset ();
  388. s.BorderWidth = 1;
  389. s.BorderStyle = BorderStyle.Dashed;
  390. css = s.GetStyleAttributes (null);
  391. Assert.AreEqual ("Dashed", css ["border-style"], "GetStyleAttributes#18");
  392. Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#19");
  393. s.BorderStyle = BorderStyle.NotSet;
  394. css = s.GetStyleAttributes (null);
  395. Assert.AreEqual ("solid", css ["border-style"], "GetStyleAttributes#20");
  396. Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#21");
  397. s.BorderWidth = 0;
  398. css = s.GetStyleAttributes (null);
  399. Assert.AreEqual (null, css ["border-style"], "GetStyleAttributes#22");
  400. Assert.AreEqual ("0px", css ["border-width"], "GetStyleAttributes#23");
  401. }
  402. #endif
  403. [Test]
  404. public void StyleFonts () {
  405. Style s = new Style ();
  406. Assert.AreEqual(new string[0], s.Font.Names, "F1");
  407. s.Font.Name = string.Empty;
  408. Assert.AreEqual(new string[0], s.Font.Names, "F2");
  409. s.Font.Names = null;
  410. Assert.AreEqual(new string[0], s.Font.Names, "F3");
  411. }
  412. [Test]
  413. [ExpectedException(typeof(ArgumentNullException))]
  414. public void NullException1 ()
  415. {
  416. Style s = new Style ();
  417. s.Font.Name = null;
  418. }
  419. private Style GetStyle ()
  420. {
  421. Style s = new Style ();
  422. s.BackColor = Color.Aqua;
  423. s.BorderWidth = Unit.Pixel (1);
  424. return s;
  425. }
  426. private void CheckStyle (Style s)
  427. {
  428. Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
  429. Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
  430. }
  431. [Test]
  432. public void CopyFrom_Null ()
  433. {
  434. Style s = GetStyle ();
  435. s.CopyFrom (null);
  436. CheckStyle (s);
  437. }
  438. [Test]
  439. public void CopyFrom_Self ()
  440. {
  441. Style s = GetStyle ();
  442. s.CopyFrom (s);
  443. CheckStyle (s);
  444. }
  445. [Test]
  446. public void CopyFrom_Empty ()
  447. {
  448. StyleTestClass s = new StyleTestClass ();
  449. s.CopyFrom (new Style ());
  450. Assert.IsTrue (s.Empty, "Empty");
  451. }
  452. [Test]
  453. public void CopyFrom ()
  454. {
  455. Style c = GetStyle ();
  456. Style s = GetStyle ();
  457. c.BorderColor = Color.Azure;
  458. c.BorderWidth = Unit.Empty;
  459. c.CopyFrom (s);
  460. CheckStyle (c);
  461. Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
  462. // CopyFrom doesn't do a Reset
  463. }
  464. [Test]
  465. public void CopyFrom_IsEmpty ()
  466. {
  467. StyleTestClass c = new StyleTestClass ();
  468. Style s = GetStyle ();
  469. s.BorderColor = Color.Azure;
  470. s.BorderWidth = Unit.Empty;
  471. c.CopyFrom (s);
  472. Assert.IsFalse (c.Empty, "IsEmpty");
  473. }
  474. [Test]
  475. public void Constructor_StateBag_Null ()
  476. {
  477. StyleTestClass s = new StyleTestClass (null);
  478. Assert.IsNotNull (s.StateBag, "StateBag");
  479. s.CssClass = "mono";
  480. Assert.AreEqual ("mono", s.CssClass, "CssClass");
  481. }
  482. [Test]
  483. public void Empty ()
  484. {
  485. StyleTestClass s = new StyleTestClass ();
  486. Assert.IsTrue (s.Empty, "Empty");
  487. Assert.AreEqual (0, s.StateBag.Count, "Count");
  488. s.StateBag["Mono"] = "go!";
  489. Assert.IsTrue (s.Empty, "Still Empty");
  490. Assert.AreEqual (1, s.StateBag.Count, "Count");
  491. }
  492. [Test]
  493. public void FontInfo_Empty ()
  494. {
  495. FontInfo f;
  496. StyleTestClass s = new StyleTestClass ();
  497. f = s.Font;
  498. Assert.IsTrue (s.Empty, "Empty after getter");
  499. s.Font.Name = "Arial";
  500. Assert.IsFalse (s.Empty, "No longer empty");
  501. }
  502. public void Render ()
  503. {
  504. HtmlTextWriter writer;
  505. StyleTestClass s;
  506. writer = StyleTest.GetWriter();
  507. s = new StyleTestClass ();
  508. s.BorderColor = Color.BlueViolet;
  509. s.AddAttributesToRender(writer);
  510. // Figure out an order-independent way to verify rendered results
  511. }
  512. }
  513. }