HtmlTextAreaTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. ta.ID = "go";
  170. Assert.AreEqual (" name=\"go\" id=\"go\" cols=\"4\" rows=\"2\"", ta.RenderAttributes ());
  171. }
  172. [Test]
  173. public void RenderName1 ()
  174. {
  175. UserControl ctrl = new UserControl ();
  176. ctrl.ID = "UC";
  177. Page page = new Page ();
  178. TestHtmlTextArea ta = new TestHtmlTextArea ();
  179. page.Controls.Add (ctrl);
  180. ctrl.Controls.Add (ta);
  181. ta.Name = "mono";
  182. ta.ID = "go";
  183. Assert.AreEqual (" name=\"UC:go\" id=\"UC_go\"", ta.RenderAttributes ());
  184. }
  185. [Test]
  186. public void Render ()
  187. {
  188. TestHtmlTextArea ta = new TestHtmlTextArea ();
  189. ta.Cols = 4;
  190. ta.Rows = 2;
  191. ta.Name = "mono";
  192. ta.Value = "value";
  193. // value is out
  194. Assert.AreEqual ("<textarea name cols=\"4\" rows=\"2\">value</textarea>", ta.Render ());
  195. ta.ID = "go";
  196. Assert.AreEqual ("<textarea name=\"go\" id=\"go\" cols=\"4\" rows=\"2\">value</textarea>", ta.Render ());
  197. }
  198. [Test]
  199. [ExpectedException (typeof (NullReferenceException))]
  200. [NUnit.Framework.Category ("NotWorking")] // Mono throw HttpException
  201. public void AddParsedSubObject_Null ()
  202. {
  203. TestHtmlTextArea ta = new TestHtmlTextArea ();
  204. ta.PublicAddParsedSubObject (null);
  205. }
  206. [Test]
  207. [ExpectedException (typeof (HttpException))]
  208. public void AddParsedSubObject_WrongType ()
  209. {
  210. TestHtmlTextArea ta = new TestHtmlTextArea ();
  211. ta.PublicAddParsedSubObject (this);
  212. }
  213. [Test]
  214. public void AddParsedSubObject_LiteralControl ()
  215. {
  216. TestHtmlTextArea ta = new TestHtmlTextArea ();
  217. ta.PublicAddParsedSubObject (new LiteralControl ());
  218. }
  219. [Test]
  220. public void AddParsedSubObject_DataBoundLiteralControl ()
  221. {
  222. TestHtmlTextArea ta = new TestHtmlTextArea ();
  223. ta.PublicAddParsedSubObject (new DataBoundLiteralControl (1,1));
  224. }
  225. private bool serverChange;
  226. private void ServerChange (object sender, EventArgs e)
  227. {
  228. serverChange = true;
  229. }
  230. [Test]
  231. public void IPostBackDataHandler_RaisePostBackEvent ()
  232. {
  233. TestHtmlTextArea ta = new TestHtmlTextArea ();
  234. ta.ServerChange += new EventHandler (ServerChange);
  235. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  236. serverChange = false;
  237. pbdh.RaisePostDataChangedEvent ();
  238. Assert.IsTrue (serverChange, "ServerChange");
  239. }
  240. [Test]
  241. [ExpectedException (typeof (NullReferenceException))]
  242. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  243. {
  244. TestHtmlTextArea ta = new TestHtmlTextArea ();
  245. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  246. pbdh.LoadPostData ("id1", null);
  247. }
  248. [Test]
  249. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  250. public void IPostBackDataHandler_LoadPostData_IdNull ()
  251. {
  252. TestHtmlTextArea ta = new TestHtmlTextArea ();
  253. ta.ID = "id1";
  254. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  255. NameValueCollection nvc = new NameValueCollection ();
  256. nvc.Add ("id1", "mono");
  257. Assert.IsFalse (pbdh.LoadPostData (null, new NameValueCollection ()));
  258. Assert.AreEqual (String.Empty, ta.Value, "Value");
  259. }
  260. [Test]
  261. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  262. public void IPostBackDataHandler_LoadPostData_WrongId ()
  263. {
  264. TestHtmlTextArea ta = new TestHtmlTextArea ();
  265. ta.ID = "id1";
  266. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  267. NameValueCollection nvc = new NameValueCollection ();
  268. nvc.Add ("id1", "mono");
  269. Assert.IsFalse (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
  270. Assert.AreEqual (String.Empty, ta.Value, "Value");
  271. }
  272. [Test]
  273. public void IPostBackDataHandler_LoadPostData ()
  274. {
  275. TestHtmlTextArea ta = new TestHtmlTextArea ();
  276. ta.ID = "id1";
  277. IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
  278. NameValueCollection nvc = new NameValueCollection ();
  279. nvc.Add ("id1", "mono");
  280. Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  281. Assert.AreEqual ("mono", ta.Value, "Value");
  282. }
  283. #if NET_2_0
  284. [Test]
  285. public void RaisePostBackEvent ()
  286. {
  287. TestHtmlTextArea ta = new TestHtmlTextArea ();
  288. ta.ServerChange += new EventHandler (ServerChange);
  289. serverChange = false;
  290. ta.Raise ();
  291. Assert.IsTrue (serverChange, "ServerClick");
  292. }
  293. [Test]
  294. [ExpectedException (typeof (NullReferenceException))]
  295. public void LoadPostData_NullCollection ()
  296. {
  297. TestHtmlTextArea ta = new TestHtmlTextArea ();
  298. ta.LoadPost ("id1", null);
  299. }
  300. [Test]
  301. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  302. public void LoadPostData_IdNull ()
  303. {
  304. TestHtmlTextArea ta = new TestHtmlTextArea ();
  305. ta.ID = "id1";
  306. NameValueCollection nvc = new NameValueCollection ();
  307. nvc.Add ("id1", "mono");
  308. Assert.IsFalse (ta.LoadPost (null, nvc), "LoadPostData");
  309. Assert.AreEqual (String.Empty, ta.Value, "Value");
  310. }
  311. [Test]
  312. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  313. public void LoadPostData_WrongId ()
  314. {
  315. TestHtmlTextArea ta = new TestHtmlTextArea ();
  316. ta.ID = "id1";
  317. NameValueCollection nvc = new NameValueCollection ();
  318. nvc.Add ("id1", "mono");
  319. Assert.IsFalse (ta.LoadPost ("id2", nvc), "LoadPostData");
  320. Assert.AreEqual (String.Empty, ta.Value, "Value");
  321. }
  322. [Test]
  323. public void LoadPostData ()
  324. {
  325. TestHtmlTextArea ta = new TestHtmlTextArea ();
  326. ta.ID = "id1";
  327. NameValueCollection nvc = new NameValueCollection ();
  328. nvc.Add ("id1", "mono");
  329. Assert.IsTrue (ta.LoadPost ("id1", nvc), "LoadPostData");
  330. Assert.AreEqual ("mono", ta.Value, "Value");
  331. }
  332. #endif
  333. }
  334. }