HtmlTextAreaTest.cs 11 KB

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