HtmlFormTest.cs 7.9 KB

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