HtmlTextAreaTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //
  2. // HtmlTextAreaTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlTextArea
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  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 System;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.Web.UI.HtmlControls;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Web.UI.HtmlControls {
  37. public class TestHtmlTextArea : HtmlTextArea {
  38. public StateBag StateBag {
  39. get { return base.ViewState; }
  40. }
  41. public string RenderAttributes ()
  42. {
  43. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  44. base.RenderAttributes (writer);
  45. return writer.InnerWriter.ToString ();
  46. }
  47. public string Render ()
  48. {
  49. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  50. base.Render (writer);
  51. return writer.InnerWriter.ToString ();
  52. }
  53. public void PublicAddParsedSubObject (object o)
  54. {
  55. base.AddParsedSubObject (o);
  56. }
  57. #if NET_2_0
  58. public bool LoadPost (string key, NameValueCollection nvc)
  59. {
  60. return base.LoadPostData (key, nvc);
  61. }
  62. public void Raise ()
  63. {
  64. base.RaisePostDataChangedEvent ();
  65. }
  66. #endif
  67. }
  68. [TestFixture]
  69. public class HtmlTextAreaTest {
  70. [Test]
  71. public void DefaultProperties ()
  72. {
  73. TestHtmlTextArea ta = new TestHtmlTextArea ();
  74. Assert.AreEqual (0, ta.Attributes.Count, "Attributes.Count");
  75. Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count");
  76. Assert.AreEqual (-1, ta.Cols, "Cols");
  77. Assert.IsNull (ta.Name, "Name");
  78. Assert.AreEqual (-1, ta.Rows, "Rows");
  79. Assert.AreEqual (String.Empty, ta.Value, "Value");
  80. Assert.AreEqual ("textarea", ta.TagName, "TagName");
  81. Assert.AreEqual (0, ta.Attributes.Count, "Attributes.Count-2");
  82. Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count-2");
  83. }
  84. [Test]
  85. public void NullProperties ()
  86. {
  87. TestHtmlTextArea ta = new TestHtmlTextArea ();
  88. ta.Cols = -1;
  89. Assert.AreEqual (-1, ta.Cols, "Cols");
  90. ta.Name = null;
  91. Assert.IsNull (ta.Name, "Name");
  92. ta.Rows = -1;
  93. Assert.AreEqual (-1, ta.Rows, "Rows");
  94. ta.Value = null;
  95. Assert.AreEqual (String.Empty, ta.Value, "Value");
  96. Assert.AreEqual (0, ta.Attributes.Count, "Attributes.Count");
  97. Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count");
  98. }
  99. [Test]
  100. public void CleanProperties ()
  101. {
  102. TestHtmlTextArea ta = new TestHtmlTextArea ();
  103. ta.Cols = 1;
  104. Assert.AreEqual (1, ta.Cols, "Cols");
  105. ta.Name = "name";
  106. Assert.IsNull (ta.Name, "Name");
  107. ta.Rows = 2;
  108. Assert.AreEqual (2, ta.Rows, "Rows");
  109. ta.Value = "value";
  110. Assert.AreEqual ("value", ta.Value, "Value");
  111. Assert.AreEqual (3, ta.Attributes.Count, "3");
  112. Assert.AreEqual (3, ta.StateBag.Count, "StateBag.Count=3");
  113. ta.Cols = -1;
  114. Assert.AreEqual (-1, ta.Cols, "-Cols");
  115. ta.Name = null;
  116. Assert.IsNull (ta.Name, "-Name");
  117. ta.Rows = -1;
  118. Assert.AreEqual (-1, ta.Rows, "Rows");
  119. ta.Value = null;
  120. Assert.AreEqual (String.Empty, ta.Value, "-Value");
  121. Assert.AreEqual (0, ta.Attributes.Count, "0");
  122. Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count=0");
  123. }
  124. [Test]
  125. public void Name ()
  126. {
  127. HtmlTextArea ta = new HtmlTextArea ();
  128. Assert.IsNull (ta.ID, "ID");
  129. ta.Name = "name";
  130. Assert.IsNull (ta.Name, "Name");
  131. ta.ID = "id";
  132. Assert.AreEqual ("id", ta.ID, "ID-2");
  133. Assert.AreEqual ("id", ta.Name, "Name-ID");
  134. ta.Name = "name";
  135. Assert.AreEqual ("id", ta.Name, "Name-ID-2");
  136. ta.ID = null;
  137. Assert.IsNull (ta.ID, "ID-3");
  138. Assert.IsNull (ta.Name, "Name-2");
  139. }
  140. [Test]
  141. public void Value ()
  142. {
  143. TestHtmlTextArea ta = new TestHtmlTextArea ();
  144. Assert.AreEqual (0, ta.Attributes.Count, "0");
  145. Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count=0");
  146. ta.Value = "value";
  147. Assert.AreEqual ("value", ta.Value, "Value");
  148. Assert.AreEqual (1, ta.Attributes.Count, "1");
  149. Assert.AreEqual (1, ta.StateBag.Count, "StateBag.Count=1");
  150. // however it's not in attributes
  151. Assert.IsNull (ta.Attributes["value"], "Attributes");
  152. // but in InnerText and InnerHtml
  153. Assert.AreEqual ("value", ta.InnerText, "InnerText");
  154. Assert.AreEqual ("value", ta.InnerHtml, "InnerHtml");
  155. // the later is kept in the attributes
  156. Assert.IsNull (ta.Attributes["innertext"], "Attributes-InnerText");
  157. Assert.AreEqual ("value", ta.Attributes["innerhtml"], "Attributes-InnerHtml");
  158. }
  159. [Test]
  160. public void RenderAttributes ()
  161. {
  162. TestHtmlTextArea ta = new TestHtmlTextArea ();
  163. ta.Cols = 4;
  164. ta.Rows = 2;
  165. ta.Name = "mono";
  166. ta.Value = "value";
  167. // value is out
  168. Assert.AreEqual (" name cols=\"4\" rows=\"2\"", ta.RenderAttributes ());
  169. // docs talks about UniqueID but it's not assignable
  170. // and anyway it's not what's being done in HtmlInputControl
  171. ta.ID = "go";
  172. Assert.AreEqual (" name=\"go\" id=\"go\" cols=\"4\" rows=\"2\"", ta.RenderAttributes ());
  173. }
  174. [Test]
  175. public void Render ()
  176. {
  177. TestHtmlTextArea ta = new TestHtmlTextArea ();
  178. ta.Cols = 4;
  179. ta.Rows = 2;
  180. ta.Name = "mono";
  181. ta.Value = "value";
  182. // value is out
  183. Assert.AreEqual ("<textarea name cols=\"4\" rows=\"2\">value</textarea>", ta.Render ());
  184. // docs talks about UniqueID but it's not assignable
  185. // and anyway it's not what's being done in HtmlInputControl
  186. ta.ID = "go";
  187. Assert.AreEqual ("<textarea name=\"go\" id=\"go\" cols=\"4\" rows=\"2\">value</textarea>", ta.Render ());
  188. }
  189. [Test]
  190. [ExpectedException (typeof (NullReferenceException))]
  191. [NUnit.Framework.Category ("NotWorking")] // Mono throw HttpException
  192. public void AddParsedSubObject_Null ()
  193. {
  194. TestHtmlTextArea ta = new TestHtmlTextArea ();
  195. ta.PublicAddParsedSubObject (null);
  196. }
  197. [Test]
  198. [ExpectedException (typeof (HttpException))]
  199. public void AddParsedSubObject_WrongType ()
  200. {
  201. TestHtmlTextArea ta = new TestHtmlTextArea ();
  202. ta.PublicAddParsedSubObject (this);
  203. }
  204. [Test]
  205. public void AddParsedSubObject_LiteralControl ()
  206. {
  207. TestHtmlTextArea ta = new TestHtmlTextArea ();
  208. ta.PublicAddParsedSubObject (new LiteralControl ());
  209. }
  210. [Test]
  211. public void AddParsedSubObject_DataBoundLiteralControl ()
  212. {
  213. TestHtmlTextArea ta = new TestHtmlTextArea ();
  214. ta.PublicAddParsedSubObject (new DataBoundLiteralControl (1,1));
  215. }
  216. private bool serverChange;
  217. private void ServerChange (object sender, EventArgs e)
  218. {
  219. serverChange = true;
  220. }
  221. [Test]
  222. public void IPostBackDataHandler_RaisePostBackEvent ()
  223. {
  224. TestHtmlTextArea ta = new TestHtmlTextArea ();
  225. ta.ServerChange += new EventHandler (ServerChange);
  226. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  227. serverChange = false;
  228. pbdh.RaisePostDataChangedEvent ();
  229. Assert.IsTrue (serverChange, "ServerChange");
  230. }
  231. [Test]
  232. [ExpectedException (typeof (NullReferenceException))]
  233. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  234. {
  235. TestHtmlTextArea ta = new TestHtmlTextArea ();
  236. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  237. pbdh.LoadPostData ("id1", null);
  238. }
  239. [Test]
  240. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  241. public void IPostBackDataHandler_LoadPostData_IdNull ()
  242. {
  243. TestHtmlTextArea ta = new TestHtmlTextArea ();
  244. ta.ID = "id1";
  245. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  246. NameValueCollection nvc = new NameValueCollection ();
  247. nvc.Add ("id1", "mono");
  248. Assert.IsFalse (pbdh.LoadPostData (null, new NameValueCollection ()));
  249. Assert.AreEqual (String.Empty, ta.Value, "Value");
  250. }
  251. [Test]
  252. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  253. public void IPostBackDataHandler_LoadPostData_WrongId ()
  254. {
  255. TestHtmlTextArea ta = new TestHtmlTextArea ();
  256. ta.ID = "id1";
  257. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  258. NameValueCollection nvc = new NameValueCollection ();
  259. nvc.Add ("id1", "mono");
  260. Assert.IsFalse (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
  261. Assert.AreEqual (String.Empty, ta.Value, "Value");
  262. }
  263. [Test]
  264. public void IPostBackDataHandler_LoadPostData ()
  265. {
  266. TestHtmlTextArea ta = new TestHtmlTextArea ();
  267. ta.ID = "id1";
  268. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  269. NameValueCollection nvc = new NameValueCollection ();
  270. nvc.Add ("id1", "mono");
  271. Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  272. Assert.AreEqual ("mono", ta.Value, "Value");
  273. }
  274. #if NET_2_0
  275. [Test]
  276. public void RaisePostBackEvent ()
  277. {
  278. TestHtmlTextArea ta = new TestHtmlTextArea ();
  279. ta.ServerChange += new EventHandler (ServerChange);
  280. serverChange = false;
  281. ta.Raise ();
  282. Assert.IsTrue (serverChange, "ServerClick");
  283. }
  284. [Test]
  285. [ExpectedException (typeof (NullReferenceException))]
  286. public void LoadPostData_NullCollection ()
  287. {
  288. TestHtmlTextArea ta = new TestHtmlTextArea ();
  289. ta.LoadPost ("id1", null);
  290. }
  291. [Test]
  292. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  293. public void LoadPostData_IdNull ()
  294. {
  295. TestHtmlTextArea ta = new TestHtmlTextArea ();
  296. ta.ID = "id1";
  297. NameValueCollection nvc = new NameValueCollection ();
  298. nvc.Add ("id1", "mono");
  299. Assert.IsFalse (ta.LoadPost (null, nvc), "LoadPostData");
  300. Assert.AreEqual (String.Empty, ta.Value, "Value");
  301. }
  302. [Test]
  303. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  304. public void LoadPostData_WrongId ()
  305. {
  306. TestHtmlTextArea ta = new TestHtmlTextArea ();
  307. ta.ID = "id1";
  308. NameValueCollection nvc = new NameValueCollection ();
  309. nvc.Add ("id1", "mono");
  310. Assert.IsFalse (ta.LoadPost ("id2", nvc), "LoadPostData");
  311. Assert.AreEqual (String.Empty, ta.Value, "Value");
  312. }
  313. [Test]
  314. public void LoadPostData ()
  315. {
  316. TestHtmlTextArea ta = new TestHtmlTextArea ();
  317. ta.ID = "id1";
  318. NameValueCollection nvc = new NameValueCollection ();
  319. nvc.Add ("id1", "mono");
  320. Assert.IsTrue (ta.LoadPost ("id1", nvc), "LoadPostData");
  321. Assert.AreEqual ("mono", ta.Value, "Value");
  322. }
  323. #endif
  324. }
  325. }