HtmlTextAreaTest.cs 11 KB

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