AttributeCollectionTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Tests for System.Web.UI.AttributeCollection.cs and CssStyleCollection
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier ([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.IO;
  32. using System.Globalization;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Collections;
  36. using AC = System.Web.UI.AttributeCollection;
  37. namespace MonoTests.System.Web.UI {
  38. [TestFixture]
  39. public class AttributeCollectionTest {
  40. [Test]
  41. public void InitialNoBag1 ()
  42. {
  43. AC ac = new AC (null);
  44. Assert.IsNotNull (ac.CssStyle, "style");
  45. }
  46. [Test]
  47. [ExpectedException (typeof (NullReferenceException))]
  48. public void InitialNoBag2 ()
  49. {
  50. AC ac = new AC (null);
  51. int i = ac.Count;
  52. }
  53. [Test]
  54. [ExpectedException (typeof (NullReferenceException))]
  55. public void InitialNoBag3 ()
  56. {
  57. AC ac = new AC (null);
  58. ICollection coll = ac.Keys;
  59. }
  60. [Test]
  61. [ExpectedException (typeof (NullReferenceException))]
  62. public void InitialNoBag4 ()
  63. {
  64. AC ac = new AC (null);
  65. string k = ac ["hola"];
  66. }
  67. [Test]
  68. [ExpectedException (typeof (NullReferenceException))]
  69. public void InitialNoBag5 ()
  70. {
  71. AC ac = new AC (null);
  72. ac.Add ("att", "value");
  73. }
  74. [Test]
  75. [ExpectedException (typeof (NullReferenceException))]
  76. public void InitialNoBag6 ()
  77. {
  78. AC ac = new AC (null);
  79. ac.Clear ();
  80. }
  81. [Test]
  82. [ExpectedException (typeof (NullReferenceException))]
  83. public void InitialNoBag7 ()
  84. {
  85. AC ac = new AC (null);
  86. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  87. ac.AddAttributes (writer);
  88. }
  89. [Test]
  90. [ExpectedException (typeof (NullReferenceException))]
  91. public void InitialNoBag8 ()
  92. {
  93. AC ac = new AC (null);
  94. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  95. ac.Render (writer);
  96. }
  97. [Test]
  98. [ExpectedException (typeof (NullReferenceException))]
  99. public void InitialNoBag9 ()
  100. {
  101. AC ac = new AC (null);
  102. ac.Remove ("hola");
  103. }
  104. [Test]
  105. [ExpectedException (typeof (NullReferenceException))]
  106. public void InitialNoBag10 ()
  107. {
  108. AC ac = new AC (null);
  109. CssStyleCollection css = ac.CssStyle;
  110. int i = css.Count;
  111. }
  112. [Test]
  113. [ExpectedException (typeof (NullReferenceException))]
  114. public void InitialNoBag11 ()
  115. {
  116. AC ac = new AC (null);
  117. CssStyleCollection css = ac.CssStyle;
  118. ICollection coll = css.Keys;
  119. }
  120. [Test]
  121. [ExpectedException (typeof (NullReferenceException))]
  122. public void InitialNoBag12 ()
  123. {
  124. AC ac = new AC (null);
  125. CssStyleCollection css = ac.CssStyle;
  126. string v = css ["hola"];
  127. }
  128. [Test]
  129. public void InitialBag1 ()
  130. {
  131. StateBag bag = new StateBag (true);
  132. AC ac = new AC (bag);
  133. Assert.AreEqual (0, ac.Count, "count");
  134. Assert.AreEqual (null, ac ["hola"], "item");
  135. Assert.AreEqual (0, ac.Keys.Count, "keys");
  136. ac.Add ("notexists", "invalid");
  137. ac.Remove ("notexists");
  138. ac.Remove ("notexists");
  139. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  140. ac.AddAttributes (writer);
  141. ac.Render (writer);
  142. Assert.AreEqual (0, writer.InnerWriter.ToString().Length, "length");
  143. CssStyleCollection css = ac.CssStyle;
  144. Assert.AreEqual (0, css.Count, "csscount");
  145. Assert.AreEqual (null, css ["hola"], "cssitem");
  146. Assert.AreEqual (0, css.Keys.Count, "csskeys");
  147. css.Add ("notexists", "invalid");
  148. css.Remove ("notexists");
  149. css.Remove ("notexists");
  150. css.Add ("notexists", "invalid");
  151. css.Clear ();
  152. Assert.AreEqual (0, css.Keys.Count, "csskeys2");
  153. }
  154. [Test]
  155. public void NonStyleAttributes1 ()
  156. {
  157. StateBag bag = new StateBag (true);
  158. AC ac = new AC (bag);
  159. StringWriter sr = new StringWriter ();
  160. HtmlTextWriter writer = new HtmlTextWriter (sr);
  161. ac.Add ("notexists", "somevalue");
  162. ac.AddAttributes (writer);
  163. string str = sr.ToString ();
  164. Assert.AreEqual ("", str, "value1");
  165. Assert.AreEqual (1, bag.Count, "count1");
  166. writer = new HtmlTextWriter (sr);
  167. writer.RenderBeginTag (HtmlTextWriterTag.A);
  168. ac.AddAttributes (writer);
  169. writer.RenderEndTag ();
  170. Assert.AreEqual ("", str, "value2");
  171. Assert.AreEqual (1, bag.Count, "count2");
  172. }
  173. [Test]
  174. public void NonStyleAttributes2 ()
  175. {
  176. StateBag bag = new StateBag (true);
  177. AC ac = new AC (bag);
  178. StringWriter sr = new StringWriter ();
  179. HtmlTextWriter writer = new HtmlTextWriter (sr);
  180. ac.Add ("class", "classname");
  181. ac.AddAttributes (writer);
  182. string str = sr.ToString ();
  183. Assert.AreEqual ("", str, "value1");
  184. Assert.AreEqual (1, bag.Count, "count1");
  185. writer = new HtmlTextWriter (sr);
  186. writer.RenderBeginTag (HtmlTextWriterTag.A);
  187. ac.AddAttributes (writer);
  188. writer.RenderEndTag ();
  189. Assert.AreEqual ("", str, "value2");
  190. Assert.AreEqual (1, bag.Count, "count2");
  191. }
  192. }
  193. }