StyleTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. public bool SetBitCalledFlag = false;
  110. public int SetBitCalledValue = 0;
  111. protected override void SetBit (int bit) {
  112. SetBitCalledFlag = true;
  113. SetBitCalledValue = bit;
  114. base.SetBit (bit);
  115. }
  116. }
  117. [TestFixture]
  118. public class StyleTest {
  119. [TestFixtureTearDown]
  120. public void TearDown ()
  121. {
  122. WebTest.Unload ();
  123. }
  124. private static HtmlTextWriter GetWriter () {
  125. StringWriter sw = new StringWriter ();
  126. sw.NewLine = "\n";
  127. return new HtmlTextWriter (sw);
  128. }
  129. private void SetSomeValues(Style s) {
  130. s.BackColor = Color.Red;
  131. s.ForeColor = Color.Green;
  132. s.Width = new Unit(10);
  133. s.Font.Bold = true;
  134. }
  135. private void SetAllValues(Style s) {
  136. s.BackColor = Color.Red;
  137. s.BorderColor = Color.Green;
  138. s.BorderStyle = BorderStyle.None;
  139. s.BorderWidth = new Unit(1);
  140. s.CssClass = "Boing";
  141. s.Font.Bold = true;
  142. s.Font.Italic = true;
  143. s.Font.Names = new string[2] {"namelist1", "namelist2"};
  144. //s.Font.Name = string.Empty;
  145. //s.Font.Names = new string[0];
  146. //Console.WriteLine("Font name after setting name: {0}", s.Font.ToString());
  147. s.Font.Overline = true;
  148. s.Font.Size = new FontUnit(1);
  149. //Console.WriteLine("Font name after setting size: {0}", s.Font.ToString());
  150. s.Font.Strikeout = true;
  151. s.Font.Underline = true;
  152. s.ForeColor = Color.Blue;
  153. s.Height = new Unit(2);
  154. s.Width = new Unit(3);
  155. }
  156. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  157. int matches;
  158. bool[] notfound;
  159. if (a1.Length != a2.Length) {
  160. if (assertion != null) {
  161. Assert.Fail(assertion + "( different length )");
  162. }
  163. return false;
  164. }
  165. matches = 0;
  166. notfound = new bool[a1.Length];
  167. for (int i = 0; i < a1.Length; i++) {
  168. for (int j = 0; j < a2.Length; j++) {
  169. if (a1[i].Equals(a2[j])) {
  170. matches++;
  171. break;
  172. }
  173. }
  174. if ((assertion != null) && (matches != i+1)) {
  175. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  176. }
  177. }
  178. return matches == a1.Length;
  179. }
  180. [Test]
  181. public void Style_Defaults ()
  182. {
  183. Style s = new Style ();
  184. Assert.AreEqual (s.BackColor, Color.Empty, "Default1");
  185. Assert.AreEqual (s.BorderColor, Color.Empty, "Default22");
  186. Assert.AreEqual (s.BorderStyle, BorderStyle.NotSet, "Default3");
  187. Assert.AreEqual (s.BorderWidth, Unit.Empty, "Default4");
  188. Assert.AreEqual (s.CssClass, string.Empty, "Default5");
  189. Assert.AreEqual (s.ForeColor, Color.Empty, "Default6");
  190. Assert.AreEqual (s.Height, Unit.Empty, "Default7");
  191. Assert.AreEqual (s.Width, Unit.Empty, "Default8");
  192. }
  193. [Test]
  194. public void Style_ViewState () {
  195. Style s = new Style ();
  196. ((IStateManager) s).TrackViewState ();
  197. SetSomeValues (s);
  198. object state = ((IStateManager) s).SaveViewState ();
  199. Style copy = new Style ();
  200. ((IStateManager) copy).LoadViewState (state);
  201. Assert.AreEqual (Color.Red, copy.BackColor, "ViewState1");
  202. Assert.AreEqual (Color.Green, copy.ForeColor, "ViewState2");
  203. Assert.AreEqual (new Unit (10), copy.Width, "ViewState3");
  204. Assert.AreEqual (true, copy.Font.Bold, "ViewState4");
  205. }
  206. [Test]
  207. public void Style_ViewState2 () {
  208. Style s = new Style (null);
  209. ((IStateManager) s).TrackViewState ();
  210. SetSomeValues (s);
  211. object state = ((IStateManager) s).SaveViewState ();
  212. Assert.AreEqual (null, state, "ViewState2");
  213. }
  214. [Test]
  215. public void Style_ViewState3 () {
  216. Style s = new Style (new StateBag());
  217. ((IStateManager) s).TrackViewState ();
  218. SetSomeValues (s);
  219. object state = ((IStateManager) s).SaveViewState ();
  220. Assert.AreEqual (null, state, "ViewState3");
  221. }
  222. [Test]
  223. public void Style_State () {
  224. string[] keyvalues;
  225. string[] expect1 = {
  226. "BorderStyle=None",
  227. "Font_Bold=True",
  228. "Font_Italic=True",
  229. "Height=2px",
  230. "CssClass=Boing",
  231. "BorderWidth=1px",
  232. "ForeColor=Color [Blue]",
  233. "Font_Size=1pt",
  234. "Font_Overline=True",
  235. "Width=3px",
  236. "BorderColor=Color [Green]",
  237. "Font_Names=namelist1, namelist2",
  238. "Font_Underline=True",
  239. "BackColor=Color [Red]",
  240. "Font_Strikeout=True" };
  241. string[] expect2 = {
  242. "BorderStyle=None",
  243. "Font_Bold=True",
  244. "Font_Italic=True",
  245. "Height=2px",
  246. "CssClass=Boing",
  247. "BorderWidth=1px",
  248. "ForeColor=Color [Blue]",
  249. "Font_Size=1pt",
  250. "Font_Overline=True",
  251. "Width=3px",
  252. "BorderColor=Color [Green]",
  253. "Font_Underline=True",
  254. "BackColor=Color [Red]",
  255. "Font_Strikeout=True" };
  256. string[] expect3 = {
  257. "BorderStyle=None",
  258. "Font_Bold=True",
  259. "Font_Italic=True",
  260. "Height=2px",
  261. "CssClass=Boing",
  262. "BorderWidth=1px",
  263. "ForeColor=Color [Blue]",
  264. "Font_Size=1pt",
  265. "Font_Overline=True",
  266. "Width=3px",
  267. "BorderColor=Color [Green]",
  268. "Font_Names=",
  269. "Font_Underline=True",
  270. "BackColor=Color [Red]",
  271. "Font_Strikeout=True" };
  272. StyleTestClass s;
  273. StyleTestClass copy;
  274. s = new StyleTestClass();
  275. SetAllValues(s);
  276. keyvalues = s.KeyValuePairs();
  277. Assert.AreEqual (15, keyvalues.Length, "State1");
  278. IsEqual(keyvalues, expect1, "State2");
  279. s.Font.Name = string.Empty;
  280. keyvalues = s.KeyValuePairs();
  281. Assert.AreEqual (expect2.Length, keyvalues.Length, "State3");
  282. IsEqual(keyvalues, expect2, "State4");
  283. s.Font.Names = null;
  284. keyvalues = s.KeyValuePairs();
  285. Assert.AreEqual (expect2.Length, keyvalues.Length, "State5");
  286. IsEqual(keyvalues, expect2, "State6");
  287. copy = new StyleTestClass();
  288. copy.CopyFrom(s);
  289. keyvalues = copy.KeyValuePairs();
  290. Assert.AreEqual (expect3.Length, keyvalues.Length, "State7");
  291. IsEqual(keyvalues, expect3, "State8");
  292. Assert.AreEqual (false, copy.IsTracking, "State9");
  293. }
  294. [Test]
  295. public void Style_Merge ()
  296. {
  297. Style s = new Style ();
  298. Style copy = new Style ();
  299. SetSomeValues(s);
  300. copy.ForeColor = Color.Blue;
  301. copy.MergeWith(s);
  302. Assert.AreEqual (Color.Red, copy.BackColor, "Merge1");
  303. Assert.AreEqual (Color.Blue, copy.ForeColor, "Merge2");
  304. // Don't fail here
  305. copy.MergeWith(null);
  306. }
  307. [Test]
  308. public void Style_Copy ()
  309. {
  310. Style s = new Style ();
  311. Style copy = new Style ();
  312. SetSomeValues(s);
  313. copy.CopyFrom (s);
  314. Assert.AreEqual (Color.Red, s.BackColor, "Copy1");
  315. }
  316. #if NET_2_0
  317. [Test]
  318. public void Style_CssClass ()
  319. {
  320. StyleTestClass s = new StyleTestClass ();
  321. Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css1");
  322. s.SetCssClass ("blah");
  323. Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
  324. s.BackColor = Color.AliceBlue;
  325. Assert.AreEqual ("blah", s.RegisteredCssClass, "Css3");
  326. }
  327. [Test]
  328. [Category ("NunitWeb")]
  329. public void Style_AddRegisteredCssClassAttribute () {
  330. new WebTest (PageInvoker.CreateOnLoad (Style_AddRegisteredCssClassAttribute_Load)).Run ();
  331. }
  332. public static void Style_AddRegisteredCssClassAttribute_Load (Page p) {
  333. StringWriter sw = new StringWriter ();
  334. HtmlTextWriter tw = new HtmlTextWriter (sw);
  335. Style s = new Style ();
  336. s.CssClass = "MyClass";
  337. s.BackColor = Color.AliceBlue;
  338. s.AddAttributesToRender (tw);
  339. tw.RenderBeginTag ("span");
  340. tw.RenderEndTag ();
  341. Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass\""), "AddRegisteredCssClassAttribute#1");
  342. Assert.AreEqual (true, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#2");
  343. sw = new StringWriter ();
  344. tw = new HtmlTextWriter (sw);
  345. s = new Style ();
  346. s.BackColor = Color.AliceBlue;
  347. p.Header.StyleSheet.RegisterStyle (s, p);
  348. s.AddAttributesToRender (tw);
  349. tw.RenderBeginTag ("span");
  350. tw.RenderEndTag ();
  351. Assert.AreEqual (true, sw.ToString ().Contains ("class"), "AddRegisteredCssClassAttribute#3");
  352. Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#4");
  353. sw = new StringWriter ();
  354. tw = new HtmlTextWriter (sw);
  355. s = new Style ();
  356. s.BackColor = Color.AliceBlue;
  357. s.CssClass = "MyClass";
  358. p.Header.StyleSheet.RegisterStyle (s, p);
  359. s.AddAttributesToRender (tw);
  360. tw.RenderBeginTag ("span");
  361. tw.RenderEndTag ();
  362. Assert.AreEqual (sw.ToString ().LastIndexOf ("class"), sw.ToString ().IndexOf ("class"), "AddRegisteredCssClassAttribute#5");
  363. Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#6");
  364. Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass "), "AddRegisteredCssClassAttribute#7");
  365. s = new Style ();
  366. p.Header.StyleSheet.RegisterStyle (s, p);
  367. Assert.AreEqual (false, s.IsEmpty, "AddRegisteredCssClassAttribute#8");
  368. }
  369. [Test]
  370. public void Style_AddAttributesToRender_use_FillStyleAttributes () {
  371. StringWriter sw = new StringWriter ();
  372. HtmlTextWriter tw = new HtmlTextWriter (sw);
  373. StyleTestClass s = new StyleTestClass ();
  374. s.AddAttributesToRender (tw);
  375. tw.RenderBeginTag ("span");
  376. tw.RenderEndTag ();
  377. HtmlDiff.AssertAreEqual ("<span style=\"FillStyleAttributes:FillStyleAttributes;\" />", sw.ToString (), "AddAttributesToRender_use_FillStyleAttributes#2");
  378. }
  379. [Test]
  380. public void Style_GetStyleAttributes () {
  381. Style s;
  382. CssStyleCollection css;
  383. s = new Style ();
  384. css = s.GetStyleAttributes (null);
  385. Assert.AreEqual (0, css.Count, "GetStyleAttributes#1");
  386. s.Font.Bold = true;
  387. s.Font.Italic = true;
  388. s.Font.Size = 10;
  389. s.Font.Names = new string [] { "Arial", "Veranda" };
  390. s.Font.Overline = true;
  391. s.Font.Strikeout = true;
  392. s.Font.Underline = true;
  393. css = s.GetStyleAttributes (null);
  394. Assert.AreEqual ("bold", css ["font-weight"], "GetStyleAttributes#2");
  395. Assert.AreEqual ("italic", css ["font-style"], "GetStyleAttributes#3");
  396. Assert.AreEqual ("10pt", css ["font-size"], "GetStyleAttributes#4");
  397. Assert.AreEqual ("Arial,Veranda", css ["font-family"], "GetStyleAttributes#5");
  398. Assert.AreEqual (true, css ["text-decoration"].Contains ("overline"), "GetStyleAttributes#6");
  399. Assert.AreEqual (true, css ["text-decoration"].Contains ("line-through"), "GetStyleAttributes#7");
  400. Assert.AreEqual (true, css ["text-decoration"].Contains ("underline"), "GetStyleAttributes#8");
  401. s.Font.Names = null;
  402. css = s.GetStyleAttributes (null);
  403. Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#9");
  404. s.Font.Name = "Arial, Veranda";
  405. css = s.GetStyleAttributes (null);
  406. Assert.AreEqual ("Arial, Veranda", css ["font-family"], "GetStyleAttributes#10");
  407. s.Font.Name = "";
  408. css = s.GetStyleAttributes (null);
  409. Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#11");
  410. s.Font.Bold = false;
  411. s.Font.Italic = false;
  412. s.Font.Size = FontUnit.Empty;
  413. s.Font.Overline = false;
  414. s.Font.Strikeout = false;
  415. s.Font.Underline = false;
  416. css = s.GetStyleAttributes (null);
  417. Assert.AreEqual ("normal", css ["font-weight"], "GetStyleAttributes#12");
  418. Assert.AreEqual ("normal", css ["font-style"], "GetStyleAttributes#13");
  419. Assert.AreEqual (null, css ["font-size"], "GetStyleAttributes#14");
  420. Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#15");
  421. s.Reset ();
  422. css = s.GetStyleAttributes (null);
  423. Assert.AreEqual (0, css.Count, "GetStyleAttributes#16");
  424. s.Reset ();
  425. s.Font.Underline = false;
  426. css = s.GetStyleAttributes (null);
  427. Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#17");
  428. s.Reset ();
  429. s.BorderWidth = 1;
  430. s.BorderStyle = BorderStyle.Dashed;
  431. css = s.GetStyleAttributes (null);
  432. Assert.AreEqual ("Dashed", css ["border-style"], "GetStyleAttributes#18");
  433. Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#19");
  434. s.BorderStyle = BorderStyle.NotSet;
  435. css = s.GetStyleAttributes (null);
  436. Assert.AreEqual ("solid", css ["border-style"], "GetStyleAttributes#20");
  437. Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#21");
  438. s.BorderWidth = 0;
  439. css = s.GetStyleAttributes (null);
  440. Assert.AreEqual (null, css ["border-style"], "GetStyleAttributes#22");
  441. Assert.AreEqual ("0px", css ["border-width"], "GetStyleAttributes#23");
  442. }
  443. #endif
  444. [Test]
  445. public void StyleFonts () {
  446. Style s = new Style ();
  447. Assert.AreEqual(new string[0], s.Font.Names, "F1");
  448. s.Font.Name = string.Empty;
  449. Assert.AreEqual(new string[0], s.Font.Names, "F2");
  450. s.Font.Names = null;
  451. Assert.AreEqual(new string[0], s.Font.Names, "F3");
  452. }
  453. [Test]
  454. [ExpectedException(typeof(ArgumentNullException))]
  455. public void NullException1 ()
  456. {
  457. Style s = new Style ();
  458. s.Font.Name = null;
  459. }
  460. private Style GetStyle ()
  461. {
  462. Style s = new Style ();
  463. s.BackColor = Color.Aqua;
  464. s.BorderWidth = Unit.Pixel (1);
  465. return s;
  466. }
  467. private void CheckStyle (Style s)
  468. {
  469. Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
  470. Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
  471. }
  472. [Test]
  473. public void CopyFrom_Null ()
  474. {
  475. Style s = GetStyle ();
  476. s.CopyFrom (null);
  477. CheckStyle (s);
  478. }
  479. [Test]
  480. public void CopyFrom_Self ()
  481. {
  482. Style s = GetStyle ();
  483. s.CopyFrom (s);
  484. CheckStyle (s);
  485. }
  486. [Test]
  487. public void CopyFrom_Empty ()
  488. {
  489. StyleTestClass s = new StyleTestClass ();
  490. s.CopyFrom (new Style ());
  491. Assert.IsTrue (s.Empty, "Empty");
  492. }
  493. [Test]
  494. public void CopyFrom ()
  495. {
  496. Style c = GetStyle ();
  497. Style s = GetStyle ();
  498. c.BorderColor = Color.Azure;
  499. c.BorderWidth = Unit.Empty;
  500. c.CopyFrom (s);
  501. CheckStyle (c);
  502. Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
  503. // CopyFrom doesn't do a Reset
  504. }
  505. [Test]
  506. public void CopyFrom_IsEmpty ()
  507. {
  508. StyleTestClass c = new StyleTestClass ();
  509. Style s = GetStyle ();
  510. s.BorderColor = Color.Azure;
  511. s.BorderWidth = Unit.Empty;
  512. c.CopyFrom (s);
  513. Assert.IsFalse (c.Empty, "IsEmpty");
  514. }
  515. [Test]
  516. public void Constructor_StateBag_Null ()
  517. {
  518. StyleTestClass s = new StyleTestClass (null);
  519. Assert.IsNotNull (s.StateBag, "StateBag");
  520. s.CssClass = "mono";
  521. Assert.AreEqual ("mono", s.CssClass, "CssClass");
  522. }
  523. [Test]
  524. public void Empty ()
  525. {
  526. StyleTestClass s = new StyleTestClass ();
  527. Assert.IsTrue (s.Empty, "Empty");
  528. Assert.AreEqual (0, s.StateBag.Count, "Count");
  529. s.StateBag["Mono"] = "go!";
  530. Assert.IsTrue (s.Empty, "Still Empty");
  531. Assert.AreEqual (1, s.StateBag.Count, "Count");
  532. }
  533. [Test]
  534. public void FontInfo_Empty ()
  535. {
  536. FontInfo f;
  537. StyleTestClass s = new StyleTestClass ();
  538. f = s.Font;
  539. Assert.IsTrue (s.Empty, "Empty after getter");
  540. s.Font.Name = "Arial";
  541. Assert.IsFalse (s.Empty, "No longer empty");
  542. }
  543. [Test]
  544. public void SetBitCalledWhenSetProperty () {
  545. StyleTestClass s = new StyleTestClass ();
  546. s.SetBitCalledFlag = false;
  547. s.BackColor = Color.Aqua;
  548. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BackColor");
  549. Assert.AreEqual (0x08, s.SetBitCalledValue, "SetBit() was called with wrong argument : BackColor");
  550. s.SetBitCalledFlag = false;
  551. s.BorderColor = Color.Blue;
  552. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderColor");
  553. Assert.AreEqual (0x10, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderColor");
  554. s.SetBitCalledFlag = false;
  555. s.BorderStyle = BorderStyle.Dashed;
  556. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderStyle");
  557. Assert.AreEqual (0x40, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderStyle");
  558. s.SetBitCalledFlag = false;
  559. s.BorderWidth = 1;
  560. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderWidth");
  561. Assert.AreEqual (0x20, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderWidth");
  562. s.SetBitCalledFlag = false;
  563. s.CssClass = "class";
  564. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CssClass");
  565. Assert.AreEqual (0x02, s.SetBitCalledValue, "SetBit() was called with wrong argument : CssClass");
  566. s.SetBitCalledFlag = false;
  567. s.ForeColor = Color.Red;
  568. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : ForeColor");
  569. Assert.AreEqual (0x04, s.SetBitCalledValue, "SetBit() was called with wrong argument : ForeColor");
  570. s.SetBitCalledFlag = false;
  571. s.Height = 1;
  572. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Height");
  573. Assert.AreEqual (0x80, s.SetBitCalledValue, "SetBit() was called with wrong argument : Height");
  574. s.SetBitCalledFlag = false;
  575. s.Width = 1;
  576. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Width");
  577. Assert.AreEqual (0x100, s.SetBitCalledValue, "SetBit() was called with wrong argument : Width");
  578. s.SetBitCalledFlag = false;
  579. s.Font.Bold = true;
  580. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Bold");
  581. Assert.AreEqual (0x800, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Bold");
  582. s.SetBitCalledFlag = false;
  583. s.Font.Italic = true;
  584. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Italic");
  585. Assert.AreEqual (0x1000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Italic");
  586. s.SetBitCalledFlag = false;
  587. s.Font.Overline = true;
  588. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Overline");
  589. Assert.AreEqual (0x4000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Overline");
  590. s.SetBitCalledFlag = false;
  591. s.Font.Underline = true;
  592. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Underline");
  593. Assert.AreEqual (0x2000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Underline");
  594. s.SetBitCalledFlag = false;
  595. s.Font.Strikeout = true;
  596. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Strikeout");
  597. Assert.AreEqual (0x8000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Strikeout");
  598. s.SetBitCalledFlag = false;
  599. s.Font.Names = new string [] { "Arial" };
  600. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Names");
  601. Assert.AreEqual (0x200, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Strikeout");
  602. s.SetBitCalledFlag = false;
  603. s.Font.Size = new FontUnit (10);
  604. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Size");
  605. Assert.AreEqual (0x400, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Size");
  606. }
  607. public void Render ()
  608. {
  609. HtmlTextWriter writer;
  610. StyleTestClass s;
  611. writer = StyleTest.GetWriter();
  612. s = new StyleTestClass ();
  613. s.BorderColor = Color.BlueViolet;
  614. s.AddAttributesToRender(writer);
  615. // Figure out an order-independent way to verify rendered results
  616. }
  617. #if NET_2_0
  618. class PokerStyle : Style
  619. {
  620. public IUrlResolutionService UrlResolver;
  621. protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  622. {
  623. UrlResolver = urlResolver;
  624. base.FillStyleAttributes (attributes, urlResolver);
  625. }
  626. }
  627. class PokerWebControl : WebControl
  628. {
  629. protected override Style CreateControlStyle ()
  630. {
  631. return new PokerStyle ();
  632. }
  633. }
  634. [Test]
  635. public void FillStyleAttributes_UrlResolver ()
  636. {
  637. PokerWebControl c = new PokerWebControl ();
  638. c.BackColor = Color.AliceBlue;
  639. c.RenderControl (new HtmlTextWriter (new StringWriter ()));
  640. Assert.AreEqual (c, ((PokerStyle) c.ControlStyle).UrlResolver);
  641. }
  642. #endif
  643. }
  644. }