CssStyleCollectionTest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Tests for System.Web.UI.CssStyleCollection.cs
  3. //
  4. // Author:
  5. // Igor Zelmanovich ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2006 Mainsoft, Inc (http://www.mainsoft.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 System.Collections.Specialized;
  40. using System.Text;
  41. namespace MonoTests.System.Web.UI
  42. {
  43. [TestFixture]
  44. public class CssStyleCollectionTest
  45. {
  46. #if NET_2_0
  47. [Test]
  48. public void CssStyleCollection_BackgroundImage () {
  49. WebControl c = new WebControl (HtmlTextWriterTag.A);
  50. CssStyleCollection css = c.Style;
  51. string url = "http://www.go-mono.com/space here?key1=val1&key2=val2";
  52. string url_encoded = "http://www.go-mono.com/space%20here?key1=val1&key2=val2";
  53. css.Add (HtmlTextWriterStyle.BackgroundImage, url);
  54. Assert.AreEqual (url, css ["background-image"], "CssStyleCollection_BackgroundImage#1");
  55. Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
  56. Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes["style"], "CssStyleCollection_BackgroundImage#4");
  57. }
  58. #endif
  59. [Test]
  60. public void CssStyleCollection_BackgroundImage2 () {
  61. WebControl c = new WebControl (HtmlTextWriterTag.A);
  62. CssStyleCollection css = c.Style;
  63. string url = "http://www.go-mono.com/space here?key1=val1&key2=val2";
  64. string url_encoded = "http://www.go-mono.com/space%20here?key1=val1&key2=val2";
  65. css.Add ("background-image", url);
  66. Assert.AreEqual (url, css ["background-image"], "CssStyleCollection_BackgroundImage#1");
  67. #if NET_2_0
  68. Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
  69. #endif
  70. Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes ["style"], "CssStyleCollection_BackgroundImage#4");
  71. }
  72. [Test]
  73. public void CssStyleCollection_BackgroundImage3 () {
  74. WebControl c = new WebControl (HtmlTextWriterTag.A);
  75. CssStyleCollection css = c.Style;
  76. string url = "http://www.go-mono.com/space here?key1=val1&key2=val2";
  77. string url_encoded = "http://www.go-mono.com/space%20here?key1=val1&key2=val2";
  78. css.Add ("background-image", "url(" + url_encoded + ")");
  79. Assert.AreEqual ("url(" + url_encoded + ")", css ["background-image"], "CssStyleCollection_BackgroundImage#1");
  80. #if NET_2_0
  81. Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
  82. #endif
  83. Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes ["style"], "CssStyleCollection_BackgroundImage#4");
  84. }
  85. [Test]
  86. public void CssStyleCollection_BackgroundImage4 () {
  87. WebControl c = new WebControl (HtmlTextWriterTag.A);
  88. CssStyleCollection css = c.Style;
  89. string url = "http://www.go-mono.com/space here?key1=val1&key2=val2";
  90. string url_encoded = "http://www.go-mono.com/space%20here?key1=val1&key2=val2";
  91. c.Attributes ["style"] = "background-image:url(" + url_encoded + ");";
  92. Assert.AreEqual ("url(" + url_encoded + ")", css ["background-image"], "CssStyleCollection_BackgroundImage#1");
  93. #if NET_2_0
  94. Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
  95. #endif
  96. Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes ["style"], "CssStyleCollection_BackgroundImage#4");
  97. }
  98. [Test]
  99. public void CssStyleCollection_Enumerator () {
  100. WebControl c = new WebControl (HtmlTextWriterTag.A);
  101. c.BackColor = Color.Beige;
  102. c.ForeColor = Color.Brown;
  103. c.Font.Bold = true;
  104. c.Attributes ["style"] = "padding: 0px; margin: 0px";
  105. Assert.AreEqual (2, c.Style.Count, "Style Count");
  106. #if NET_2_0
  107. Assert.AreEqual (3, c.ControlStyle.GetStyleAttributes (c).Count, "ControlStyle Count");
  108. #endif
  109. CssStyleCollection col = c.Style;
  110. NameValueCollection styles = new NameValueCollection ();
  111. foreach (string key in col.Keys) {
  112. styles [key] = col [key];
  113. }
  114. Assert.AreEqual ("0px", styles ["padding"], "Style padding");
  115. Assert.AreEqual ("0px",styles ["margin"], "Style margin");
  116. }
  117. [Test]
  118. public void CssStyleCollection_Style_Attribute () {
  119. WebControl c = new WebControl (HtmlTextWriterTag.A);
  120. Assert.IsTrue (Object.ReferenceEquals (c.Style, c.Attributes.CssStyle));
  121. // style attribute is parsed to CssStyleCollection
  122. c.Attributes.Add ("style", "padding: 1px; margin: 2px");
  123. Assert.AreEqual (2, c.Style.Count, "Style Count");
  124. Assert.AreEqual ("1px", c.Style ["padding"], "");
  125. Assert.AreEqual ("2px", c.Style ["margin"], "");
  126. // CssStyleCollection is merged to style attribute
  127. c.Style.Add ("color", "red");
  128. Assert.AreEqual (3, c.Style.Count, "Style Count");
  129. Assert.AreEqual ("red", c.Style ["color"], "");
  130. Assert.IsTrue (c.Attributes ["style"].IndexOf("color:")>=0);
  131. // replacing style attribute replaces CssStyleCollection's items
  132. c.Attributes ["style"] = "align: center";
  133. Assert.AreEqual (1, c.Style.Count, "Style Count");
  134. Assert.AreEqual ("center", c.Style ["align"], "");
  135. // removing style attribute clears CssStyleCollection
  136. c.Attributes.Remove("style");
  137. Assert.AreEqual (0, c.Style.Count, "Style Count");
  138. // adding to CssStyleCollection create style attribute
  139. c.Style.Add ("color", "red");
  140. Assert.AreEqual (1, c.Attributes.Count, "Attributes Count");
  141. c.Attributes ["style"] = "align: center; color: red;";
  142. Assert.AreEqual (2, c.Style.Count, "Style Count");
  143. Assert.AreEqual ("center", c.Style ["align"], "");
  144. Assert.AreEqual ("red", c.Style ["color"], "");
  145. // clearing CssStyleCollection removes style attribute
  146. c.Style.Clear ();
  147. Assert.AreEqual (0, c.Attributes.Count, "Attributes Count");
  148. }
  149. [Test]
  150. public void CssStyleCollection_case_sensitive () {
  151. WebControl c = new WebControl (HtmlTextWriterTag.A);
  152. c.Style.Add ("color", "red");
  153. #if NET_2_0
  154. Assert.AreEqual ("red", c.Style ["Color"], "");
  155. #else
  156. Assert.AreEqual (null, c.Style ["Color"], "");
  157. Assert.AreEqual ("red", c.Style ["color"], "");
  158. #endif
  159. c.Style.Add ("Color", "Blue");
  160. #if NET_2_0
  161. Assert.AreEqual ("Blue", c.Style ["color"], "");
  162. Assert.AreEqual (1, c.Style.Count, "Style Count");
  163. #else
  164. Assert.AreEqual ("red", c.Style ["color"], "");
  165. Assert.AreEqual ("Blue", c.Style ["Color"], "");
  166. Assert.AreEqual (2, c.Style.Count, "Style Count");
  167. #endif
  168. }
  169. }
  170. }