HtmlFormTest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // HtmlFormTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlForm
  4. //
  5. // Author:
  6. // Dick Porter <[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.IO;
  31. using System.Text;
  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. class TestPage : Page {
  39. private HttpContext ctx;
  40. // don't call base class (so _context is never set to a non-null value)
  41. protected override HttpContext Context {
  42. get {
  43. if (ctx == null) {
  44. ctx = new HttpContext (
  45. new HttpRequest ("default.aspx", "http://mono-project.com/", "q=1"),
  46. new HttpResponse (new StringWriter ())
  47. );
  48. }
  49. return ctx;
  50. }
  51. }
  52. #if NET_2_0 && !TARGET_JVM
  53. public void SetContext ()
  54. {
  55. SetContext (Context);
  56. }
  57. #endif
  58. }
  59. public class FormPoker : HtmlForm {
  60. public FormPoker () {
  61. TrackViewState ();
  62. }
  63. public object SaveState ()
  64. {
  65. return SaveViewState ();
  66. }
  67. public void LoadState (object state)
  68. {
  69. LoadViewState (state);
  70. }
  71. /* Not called at all when running the current tests (2005/09/29)
  72. protected override void OnInit (EventArgs e)
  73. {
  74. Console.WriteLine (Environment.StackTrace);
  75. base.OnInit (e);
  76. }
  77. */
  78. public string RenderChildren ()
  79. {
  80. StringWriter sw = new StringWriter();
  81. HtmlTextWriter w = new HtmlTextWriter (sw);
  82. RenderChildren (w);
  83. return sw.ToString();
  84. }
  85. public string RenderAttributes ()
  86. {
  87. StringWriter sw = new StringWriter();
  88. HtmlTextWriter w = new HtmlTextWriter (sw);
  89. RenderAttributes (w);
  90. return sw.ToString ();
  91. }
  92. #if NET_2_0
  93. public ControlCollection GetControlCollection ()
  94. {
  95. return CreateControlCollection();
  96. }
  97. #endif
  98. }
  99. class FUControl : UserControl {
  100. }
  101. [TestFixture]
  102. public class HtmlFormTest {
  103. [Test]
  104. public void DefaultProperties ()
  105. {
  106. HtmlForm form = new HtmlForm ();
  107. Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
  108. Assert.AreEqual (String.Empty, form.Enctype, "Enctype");
  109. Assert.AreEqual ("post", form.Method, "Method");
  110. Assert.AreEqual (form.UniqueID, form.Name, "Name");
  111. Assert.AreEqual (String.Empty, form.Target, "Target");
  112. Assert.AreEqual ("form", form.TagName, "TagName");
  113. #if NET_2_0
  114. Assert.IsFalse (form.SubmitDisabledControls, "TagName");
  115. #endif
  116. }
  117. [Test]
  118. public void NullProperties ()
  119. {
  120. HtmlForm form = new HtmlForm ();
  121. form.Enctype = null;
  122. Assert.AreEqual (String.Empty, form.Enctype, "Enctype");
  123. form.Method = null;
  124. Assert.AreEqual ("post", form.Method, "Method");
  125. form.Name = null;
  126. Assert.AreEqual (form.UniqueID, form.Name, "Name");
  127. form.Target = null;
  128. Assert.AreEqual (String.Empty, form.Target, "Target");
  129. #if NET_2_0
  130. form.DefaultButton = null;
  131. Assert.AreEqual (String.Empty, form.DefaultButton, "DefaultButton");
  132. form.DefaultFocus = null;
  133. Assert.AreEqual (String.Empty, form.DefaultFocus, "DefaultFocus");
  134. #endif
  135. Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
  136. }
  137. [Test]
  138. public void Attributes ()
  139. {
  140. HtmlForm form = new HtmlForm ();
  141. IAttributeAccessor a = (IAttributeAccessor)form;
  142. #if NET_2_0
  143. /* not stored in Attributes */
  144. form.DefaultButton = "defaultbutton";
  145. Assert.IsNull (a.GetAttribute ("defaultbutton"), "A1");
  146. /* not stored in Attributes */
  147. form.DefaultFocus = "defaultfocus";
  148. Assert.IsNull (a.GetAttribute ("defaultfocus"), "A2");
  149. #endif
  150. form.Enctype = "enctype";
  151. Assert.AreEqual ("enctype", a.GetAttribute ("enctype"), "A3");
  152. form.Method = "method";
  153. Assert.AreEqual ("method", a.GetAttribute ("method"), "A4");
  154. /* not stored in Attributes */
  155. form.Name = "name";
  156. Assert.AreEqual (form.UniqueID, form.Name, "A5");
  157. Assert.IsNull (form.Name, "A6");
  158. Assert.IsNull (form.UniqueID, "A7");
  159. Assert.IsNull (a.GetAttribute ("name"), "A8");
  160. form.ID = "hithere";
  161. Assert.AreEqual ("hithere", form.Name, "A9");
  162. #if NET_2_0
  163. form.SubmitDisabledControls = true;
  164. Assert.IsNull (a.GetAttribute ("submitdisabledcontrols"), "A10");
  165. #endif
  166. form.Target = "target";
  167. Assert.AreEqual ("target", a.GetAttribute ("target"), "A11");
  168. }
  169. #if NET_2_0
  170. #if !TARGET_DOTNET && !TARGET_JVM
  171. [Test]
  172. public void ActionStringWithQuery ()
  173. {
  174. TestPage p = new TestPage ();
  175. p.SetContext ();
  176. FormPoker form = new FormPoker ();
  177. form.Page = p;
  178. string attrs = form.RenderAttributes ();
  179. // Indirect test for HttpRequest.QueryStringRaw, see
  180. // https://bugzilla.novell.com/show_bug.cgi?id=376352
  181. Assert.AreEqual (" method=\"post\" action=\"?q=1\"", attrs, "A1");
  182. }
  183. #endif
  184. #endif
  185. [Test]
  186. public void ViewState ()
  187. {
  188. FormPoker form = new FormPoker();
  189. FormPoker copy = new FormPoker();
  190. #if NET_2_0
  191. form.DefaultButton = "defaultbutton";
  192. form.DefaultFocus = "defaultfocus";
  193. #endif
  194. object state = form.SaveState();
  195. copy.LoadState (state);
  196. #if NET_2_0
  197. Assert.AreEqual ("", copy.DefaultButton, "A1");
  198. Assert.AreEqual ("defaultfocus", form.DefaultFocus, "A2");
  199. #endif
  200. }
  201. [Test]
  202. public void Name_InsideNaming ()
  203. {
  204. Control ctrl = new FUControl ();
  205. ctrl.ID = "parent";
  206. FormPoker form = new FormPoker ();
  207. ctrl.Controls.Add (form);
  208. Assert.IsNull (form.ID, "ID");
  209. form.Name = "name";
  210. Assert.AreEqual (form.Name, form.UniqueID, "name and unique id");
  211. form.ID = "id";
  212. Assert.AreEqual ("id", form.ID, "ID-2");
  213. Assert.AreEqual (form.UniqueID, form.Name, "Name-ID");
  214. form.Name = "name";
  215. Assert.AreEqual (form.Name, form.UniqueID, "UniqueID-2");
  216. form.ID = null;
  217. Assert.IsNull (form.ID, "ID-3");
  218. Assert.IsNotNull (form.UniqueID, "UniqueID-3");
  219. Assert.IsNotNull (form.Name, "Name-2");
  220. }
  221. [Test]
  222. [Category ("NotWorking")]
  223. public void RenderChildren ()
  224. {
  225. Page p = new Page();
  226. FormPoker form = new FormPoker ();
  227. form.Page = p;
  228. #if NET_2_0
  229. HtmlDiff.AssertAreEqual ("<div>\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"\r\n__VIEWSTATE\" value=\"\" />\r\n</div>", form.RenderChildren ().Trim (), "A1");
  230. #else
  231. HtmlDiff.AssertAreEqual ("<input type=\"hidden\" name=\"__VIEWSTATE\" value=\"\" />", form.RenderChildren ().Trim (), "A1");
  232. #endif
  233. }
  234. #if NET_2_0
  235. [Test]
  236. public void ControlCollection ()
  237. {
  238. FormPoker poker = new FormPoker();
  239. ControlCollection col = poker.GetControlCollection();
  240. Assert.AreEqual (col.GetType(), typeof (ControlCollection), "A1");
  241. Assert.IsFalse (col.IsReadOnly, "A2");
  242. Assert.AreEqual (0, col.Count, "A3");
  243. }
  244. #endif
  245. }
  246. }