StyleTest.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. [Test]
  317. public void Style_CssClass ()
  318. {
  319. StyleTestClass s = new StyleTestClass ();
  320. Assert.AreEqual (String.Empty, s.CssClass, "#A1");
  321. s.CssClass = "css1";
  322. Assert.AreEqual ("css1", s.CssClass, "#A2");
  323. s.CssClass = String.Empty;
  324. Assert.AreEqual (String.Empty, s.CssClass, "#A3");
  325. s.CssClass = null;
  326. Assert.AreEqual (String.Empty, s.CssClass, "#A4");
  327. }
  328. [Test]
  329. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  330. public void Style_BorderStyle_InvalidValue1 ()
  331. {
  332. StyleTestClass s = new StyleTestClass ();
  333. s.BorderStyle = (BorderStyle)(BorderStyle.NotSet - 1);
  334. }
  335. [Test]
  336. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  337. public void Style_BorderStyle_InvalidValue2 ()
  338. {
  339. StyleTestClass s = new StyleTestClass ();
  340. s.BorderStyle = (BorderStyle)(BorderStyle.Outset + 1);
  341. }
  342. #if NET_2_0
  343. [Test]
  344. public void Style_RegisteredCssClass ()
  345. {
  346. StyleTestClass s = new StyleTestClass ();
  347. Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css1");
  348. s.SetCssClass ("blah");
  349. Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
  350. s.BackColor = Color.AliceBlue;
  351. Assert.AreEqual ("blah", s.RegisteredCssClass, "Css3");
  352. }
  353. [Test]
  354. [Category ("NunitWeb")]
  355. public void Style_AddRegisteredCssClassAttribute () {
  356. new WebTest (PageInvoker.CreateOnLoad (Style_AddRegisteredCssClassAttribute_Load)).Run ();
  357. }
  358. public static void Style_AddRegisteredCssClassAttribute_Load (Page p) {
  359. StringWriter sw = new StringWriter ();
  360. HtmlTextWriter tw = new HtmlTextWriter (sw);
  361. Style s = new Style ();
  362. s.CssClass = "MyClass";
  363. s.BackColor = Color.AliceBlue;
  364. s.AddAttributesToRender (tw);
  365. tw.RenderBeginTag ("span");
  366. tw.RenderEndTag ();
  367. Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass\""), "AddRegisteredCssClassAttribute#1");
  368. Assert.AreEqual (true, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#2");
  369. sw = new StringWriter ();
  370. tw = new HtmlTextWriter (sw);
  371. s = new Style ();
  372. s.BackColor = Color.AliceBlue;
  373. p.Header.StyleSheet.RegisterStyle (s, p);
  374. s.AddAttributesToRender (tw);
  375. tw.RenderBeginTag ("span");
  376. tw.RenderEndTag ();
  377. Assert.AreEqual (true, sw.ToString ().Contains ("class"), "AddRegisteredCssClassAttribute#3");
  378. Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#4");
  379. sw = new StringWriter ();
  380. tw = new HtmlTextWriter (sw);
  381. s = new Style ();
  382. s.BackColor = Color.AliceBlue;
  383. s.CssClass = "MyClass";
  384. p.Header.StyleSheet.RegisterStyle (s, p);
  385. s.AddAttributesToRender (tw);
  386. tw.RenderBeginTag ("span");
  387. tw.RenderEndTag ();
  388. Assert.AreEqual (sw.ToString ().LastIndexOf ("class"), sw.ToString ().IndexOf ("class"), "AddRegisteredCssClassAttribute#5");
  389. Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#6");
  390. Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass "), "AddRegisteredCssClassAttribute#7");
  391. s = new Style ();
  392. p.Header.StyleSheet.RegisterStyle (s, p);
  393. Assert.AreEqual (false, s.IsEmpty, "AddRegisteredCssClassAttribute#8");
  394. }
  395. [Test]
  396. public void Style_AddAttributesToRender_use_FillStyleAttributes () {
  397. StringWriter sw = new StringWriter ();
  398. HtmlTextWriter tw = new HtmlTextWriter (sw);
  399. StyleTestClass s = new StyleTestClass ();
  400. s.AddAttributesToRender (tw);
  401. tw.RenderBeginTag ("span");
  402. tw.RenderEndTag ();
  403. HtmlDiff.AssertAreEqual ("<span style=\"FillStyleAttributes:FillStyleAttributes;\" />", sw.ToString (), "AddAttributesToRender_use_FillStyleAttributes#2");
  404. }
  405. [Test]
  406. public void Style_GetStyleAttributes () {
  407. Style s;
  408. CssStyleCollection css;
  409. s = new Style ();
  410. css = s.GetStyleAttributes (null);
  411. Assert.AreEqual (0, css.Count, "GetStyleAttributes#1");
  412. s.Font.Bold = true;
  413. s.Font.Italic = true;
  414. s.Font.Size = 10;
  415. s.Font.Names = new string [] { "Arial", "Veranda" };
  416. s.Font.Overline = true;
  417. s.Font.Strikeout = true;
  418. s.Font.Underline = true;
  419. css = s.GetStyleAttributes (null);
  420. Assert.AreEqual ("bold", css ["font-weight"], "GetStyleAttributes#2");
  421. Assert.AreEqual ("italic", css ["font-style"], "GetStyleAttributes#3");
  422. Assert.AreEqual ("10pt", css ["font-size"], "GetStyleAttributes#4");
  423. Assert.AreEqual ("Arial,Veranda", css ["font-family"], "GetStyleAttributes#5");
  424. Assert.AreEqual (true, css ["text-decoration"].Contains ("overline"), "GetStyleAttributes#6");
  425. Assert.AreEqual (true, css ["text-decoration"].Contains ("line-through"), "GetStyleAttributes#7");
  426. Assert.AreEqual (true, css ["text-decoration"].Contains ("underline"), "GetStyleAttributes#8");
  427. s.Font.Names = null;
  428. css = s.GetStyleAttributes (null);
  429. Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#9");
  430. s.Font.Name = "Arial, Veranda";
  431. css = s.GetStyleAttributes (null);
  432. Assert.AreEqual ("Arial, Veranda", css ["font-family"], "GetStyleAttributes#10");
  433. s.Font.Name = "";
  434. css = s.GetStyleAttributes (null);
  435. Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#11");
  436. s.Font.Bold = false;
  437. s.Font.Italic = false;
  438. s.Font.Size = FontUnit.Empty;
  439. s.Font.Overline = false;
  440. s.Font.Strikeout = false;
  441. s.Font.Underline = false;
  442. css = s.GetStyleAttributes (null);
  443. Assert.AreEqual ("normal", css ["font-weight"], "GetStyleAttributes#12");
  444. Assert.AreEqual ("normal", css ["font-style"], "GetStyleAttributes#13");
  445. Assert.AreEqual (null, css ["font-size"], "GetStyleAttributes#14");
  446. Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#15");
  447. s.Reset ();
  448. css = s.GetStyleAttributes (null);
  449. Assert.AreEqual (0, css.Count, "GetStyleAttributes#16");
  450. s.Reset ();
  451. s.Font.Underline = false;
  452. css = s.GetStyleAttributes (null);
  453. Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#17");
  454. s.Reset ();
  455. s.BorderWidth = 1;
  456. s.BorderStyle = BorderStyle.Dashed;
  457. css = s.GetStyleAttributes (null);
  458. Assert.AreEqual ("Dashed", css ["border-style"], "GetStyleAttributes#18");
  459. Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#19");
  460. s.BorderStyle = BorderStyle.NotSet;
  461. css = s.GetStyleAttributes (null);
  462. Assert.AreEqual ("solid", css ["border-style"], "GetStyleAttributes#20");
  463. Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#21");
  464. s.BorderWidth = 0;
  465. css = s.GetStyleAttributes (null);
  466. Assert.AreEqual (null, css ["border-style"], "GetStyleAttributes#22");
  467. Assert.AreEqual ("0px", css ["border-width"], "GetStyleAttributes#23");
  468. }
  469. #endif
  470. [Test]
  471. public void StyleFonts () {
  472. Style s = new Style ();
  473. Assert.AreEqual(new string[0], s.Font.Names, "F1");
  474. s.Font.Name = string.Empty;
  475. Assert.AreEqual(new string[0], s.Font.Names, "F2");
  476. s.Font.Names = null;
  477. Assert.AreEqual(new string[0], s.Font.Names, "F3");
  478. }
  479. [Test]
  480. [ExpectedException(typeof(ArgumentNullException))]
  481. public void NullException1 ()
  482. {
  483. Style s = new Style ();
  484. s.Font.Name = null;
  485. }
  486. private Style GetStyle ()
  487. {
  488. Style s = new Style ();
  489. s.BackColor = Color.Aqua;
  490. s.BorderWidth = Unit.Pixel (1);
  491. return s;
  492. }
  493. private void CheckStyle (Style s)
  494. {
  495. Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
  496. Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
  497. }
  498. [Test]
  499. public void CopyFrom_Null ()
  500. {
  501. Style s = GetStyle ();
  502. s.CopyFrom (null);
  503. CheckStyle (s);
  504. }
  505. [Test]
  506. public void CopyFrom_Self ()
  507. {
  508. Style s = GetStyle ();
  509. s.CopyFrom (s);
  510. CheckStyle (s);
  511. }
  512. [Test]
  513. public void CopyFrom_Empty ()
  514. {
  515. StyleTestClass s = new StyleTestClass ();
  516. s.CopyFrom (new Style ());
  517. Assert.IsTrue (s.Empty, "Empty");
  518. }
  519. [Test]
  520. public void CopyFrom ()
  521. {
  522. Style c = GetStyle ();
  523. Style s = GetStyle ();
  524. c.BorderColor = Color.Azure;
  525. c.BorderWidth = Unit.Empty;
  526. c.CopyFrom (s);
  527. CheckStyle (c);
  528. Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
  529. // CopyFrom doesn't do a Reset
  530. }
  531. [Test]
  532. public void CopyFrom_IsEmpty ()
  533. {
  534. StyleTestClass c = new StyleTestClass ();
  535. Style s = GetStyle ();
  536. s.BorderColor = Color.Azure;
  537. s.BorderWidth = Unit.Empty;
  538. c.CopyFrom (s);
  539. Assert.IsFalse (c.Empty, "IsEmpty");
  540. }
  541. [Test]
  542. public void Constructor_StateBag_Null ()
  543. {
  544. StyleTestClass s = new StyleTestClass (null);
  545. Assert.IsNotNull (s.StateBag, "StateBag");
  546. s.CssClass = "mono";
  547. Assert.AreEqual ("mono", s.CssClass, "CssClass");
  548. }
  549. [Test]
  550. public void Empty ()
  551. {
  552. StyleTestClass s = new StyleTestClass ();
  553. Assert.IsTrue (s.Empty, "Empty");
  554. Assert.AreEqual (0, s.StateBag.Count, "Count");
  555. s.StateBag["Mono"] = "go!";
  556. Assert.IsTrue (s.Empty, "Still Empty");
  557. Assert.AreEqual (1, s.StateBag.Count, "Count");
  558. }
  559. [Test]
  560. public void FontInfo_Empty ()
  561. {
  562. FontInfo f;
  563. StyleTestClass s = new StyleTestClass ();
  564. f = s.Font;
  565. Assert.IsTrue (s.Empty, "Empty after getter");
  566. s.Font.Name = "Arial";
  567. Assert.IsFalse (s.Empty, "No longer empty");
  568. }
  569. [Test]
  570. public void SetBitCalledWhenSetProperty () {
  571. StyleTestClass s = new StyleTestClass ();
  572. s.SetBitCalledFlag = false;
  573. s.BackColor = Color.Aqua;
  574. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BackColor");
  575. Assert.AreEqual (0x08, s.SetBitCalledValue, "SetBit() was called with wrong argument : BackColor");
  576. s.SetBitCalledFlag = false;
  577. s.BorderColor = Color.Blue;
  578. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderColor");
  579. Assert.AreEqual (0x10, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderColor");
  580. s.SetBitCalledFlag = false;
  581. s.BorderStyle = BorderStyle.Dashed;
  582. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderStyle");
  583. Assert.AreEqual (0x40, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderStyle");
  584. s.SetBitCalledFlag = false;
  585. s.BorderWidth = 1;
  586. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderWidth");
  587. Assert.AreEqual (0x20, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderWidth");
  588. s.SetBitCalledFlag = false;
  589. s.CssClass = "class";
  590. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CssClass");
  591. Assert.AreEqual (0x02, s.SetBitCalledValue, "SetBit() was called with wrong argument : CssClass");
  592. s.SetBitCalledFlag = false;
  593. s.ForeColor = Color.Red;
  594. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : ForeColor");
  595. Assert.AreEqual (0x04, s.SetBitCalledValue, "SetBit() was called with wrong argument : ForeColor");
  596. s.SetBitCalledFlag = false;
  597. s.Height = 1;
  598. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Height");
  599. Assert.AreEqual (0x80, s.SetBitCalledValue, "SetBit() was called with wrong argument : Height");
  600. s.SetBitCalledFlag = false;
  601. s.Width = 1;
  602. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Width");
  603. Assert.AreEqual (0x100, s.SetBitCalledValue, "SetBit() was called with wrong argument : Width");
  604. s.SetBitCalledFlag = false;
  605. s.Font.Bold = true;
  606. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Bold");
  607. Assert.AreEqual (0x800, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Bold");
  608. s.SetBitCalledFlag = false;
  609. s.Font.Italic = true;
  610. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Italic");
  611. Assert.AreEqual (0x1000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Italic");
  612. s.SetBitCalledFlag = false;
  613. s.Font.Overline = true;
  614. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Overline");
  615. Assert.AreEqual (0x4000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Overline");
  616. s.SetBitCalledFlag = false;
  617. s.Font.Underline = true;
  618. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Underline");
  619. Assert.AreEqual (0x2000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Underline");
  620. s.SetBitCalledFlag = false;
  621. s.Font.Strikeout = true;
  622. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Strikeout");
  623. Assert.AreEqual (0x8000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Strikeout");
  624. s.SetBitCalledFlag = false;
  625. s.Font.Names = new string [] { "Arial" };
  626. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Names");
  627. Assert.AreEqual (0x200, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Strikeout");
  628. s.SetBitCalledFlag = false;
  629. s.Font.Size = new FontUnit (10);
  630. Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Size");
  631. Assert.AreEqual (0x400, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Size");
  632. }
  633. public void Render ()
  634. {
  635. HtmlTextWriter writer;
  636. StyleTestClass s;
  637. writer = StyleTest.GetWriter();
  638. s = new StyleTestClass ();
  639. s.BorderColor = Color.BlueViolet;
  640. s.AddAttributesToRender(writer);
  641. // Figure out an order-independent way to verify rendered results
  642. }
  643. #if NET_2_0
  644. class PokerStyle : Style
  645. {
  646. public IUrlResolutionService UrlResolver;
  647. protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  648. {
  649. UrlResolver = urlResolver;
  650. base.FillStyleAttributes (attributes, urlResolver);
  651. }
  652. }
  653. class PokerWebControl : WebControl
  654. {
  655. protected override Style CreateControlStyle ()
  656. {
  657. return new PokerStyle ();
  658. }
  659. }
  660. [Test]
  661. public void FillStyleAttributes_UrlResolver ()
  662. {
  663. PokerWebControl c = new PokerWebControl ();
  664. c.BackColor = Color.AliceBlue;
  665. c.RenderControl (new HtmlTextWriter (new StringWriter ()));
  666. Assert.AreEqual (c, ((PokerStyle) c.ControlStyle).UrlResolver);
  667. }
  668. #endif
  669. }
  670. }