HtmlFormTest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. public class FormPoker : HtmlForm {
  39. public FormPoker () {
  40. TrackViewState ();
  41. }
  42. public object SaveState ()
  43. {
  44. return SaveViewState ();
  45. }
  46. public void LoadState (object state)
  47. {
  48. LoadViewState (state);
  49. }
  50. /* Not called at all when running the current tests (2005/09/29)
  51. protected override void OnInit (EventArgs e)
  52. {
  53. Console.WriteLine (Environment.StackTrace);
  54. base.OnInit (e);
  55. }
  56. */
  57. public string RenderChildren ()
  58. {
  59. StringWriter sw = new StringWriter();
  60. HtmlTextWriter w = new HtmlTextWriter (sw);
  61. RenderChildren (w);
  62. return sw.ToString();
  63. }
  64. #if NET_2_0
  65. public ControlCollection GetControlCollection ()
  66. {
  67. return CreateControlCollection();
  68. }
  69. #endif
  70. }
  71. class FUControl : UserControl {
  72. }
  73. [TestFixture]
  74. public class HtmlFormTest {
  75. [Test]
  76. public void DefaultProperties ()
  77. {
  78. HtmlForm form = new HtmlForm ();
  79. Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
  80. Assert.AreEqual (String.Empty, form.Enctype, "Enctype");
  81. Assert.AreEqual ("post", form.Method, "Method");
  82. Assert.AreEqual (form.UniqueID, form.Name, "Name");
  83. Assert.AreEqual (String.Empty, form.Target, "Target");
  84. Assert.AreEqual ("form", form.TagName, "TagName");
  85. #if NET_2_0
  86. Assert.IsFalse (form.SubmitDisabledControls, "TagName");
  87. #endif
  88. }
  89. [Test]
  90. public void NullProperties ()
  91. {
  92. HtmlForm form = new HtmlForm ();
  93. form.Enctype = null;
  94. Assert.AreEqual (String.Empty, form.Enctype, "Enctype");
  95. form.Method = null;
  96. Assert.AreEqual ("post", form.Method, "Method");
  97. form.Name = null;
  98. Assert.AreEqual (form.UniqueID, form.Name, "Name");
  99. form.Target = null;
  100. Assert.AreEqual (String.Empty, form.Target, "Target");
  101. #if NET_2_0
  102. form.DefaultButton = null;
  103. Assert.AreEqual (String.Empty, form.DefaultButton, "DefaultButton");
  104. form.DefaultFocus = null;
  105. Assert.AreEqual (String.Empty, form.DefaultFocus, "DefaultFocus");
  106. #endif
  107. Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
  108. }
  109. [Test]
  110. public void Attributes ()
  111. {
  112. HtmlForm form = new HtmlForm ();
  113. IAttributeAccessor a = (IAttributeAccessor)form;
  114. #if NET_2_0
  115. /* not stored in Attributes */
  116. form.DefaultButton = "defaultbutton";
  117. Assert.IsNull (a.GetAttribute ("defaultbutton"), "A1");
  118. /* not stored in Attributes */
  119. form.DefaultFocus = "defaultfocus";
  120. Assert.IsNull (a.GetAttribute ("defaultfocus"), "A2");
  121. #endif
  122. form.Enctype = "enctype";
  123. Assert.AreEqual ("enctype", a.GetAttribute ("enctype"), "A3");
  124. form.Method = "method";
  125. Assert.AreEqual ("method", a.GetAttribute ("method"), "A4");
  126. /* not stored in Attributes */
  127. form.Name = "name";
  128. Assert.AreEqual (form.UniqueID, form.Name, "A5");
  129. Assert.IsNull (form.Name, "A6");
  130. Assert.IsNull (form.UniqueID, "A7");
  131. Assert.IsNull (a.GetAttribute ("name"), "A8");
  132. form.ID = "hithere";
  133. Assert.AreEqual ("hithere", form.Name, "A9");
  134. #if NET_2_0
  135. form.SubmitDisabledControls = true;
  136. Assert.IsNull (a.GetAttribute ("submitdisabledcontrols"), "A10");
  137. #endif
  138. form.Target = "target";
  139. Assert.AreEqual ("target", a.GetAttribute ("target"), "A11");
  140. }
  141. [Test]
  142. public void ViewState ()
  143. {
  144. FormPoker form = new FormPoker();
  145. FormPoker copy = new FormPoker();
  146. #if NET_2_0
  147. form.DefaultButton = "defaultbutton";
  148. form.DefaultFocus = "defaultfocus";
  149. #endif
  150. object state = form.SaveState();
  151. copy.LoadState (state);
  152. #if NET_2_0
  153. Assert.AreEqual ("", copy.DefaultButton, "A1");
  154. Assert.AreEqual ("defaultfocus", form.DefaultFocus, "A2");
  155. #endif
  156. }
  157. [Test]
  158. public void Name_InsideNaming ()
  159. {
  160. Control ctrl = new FUControl ();
  161. ctrl.ID = "parent";
  162. FormPoker form = new FormPoker ();
  163. ctrl.Controls.Add (form);
  164. Assert.IsNull (form.ID, "ID");
  165. form.Name = "name";
  166. Assert.AreEqual (form.Name, form.UniqueID, "name and unique id");
  167. form.ID = "id";
  168. Assert.AreEqual ("id", form.ID, "ID-2");
  169. Assert.AreEqual (form.UniqueID, form.Name, "Name-ID");
  170. form.Name = "name";
  171. Assert.AreEqual (form.Name, form.UniqueID, "UniqueID-2");
  172. form.ID = null;
  173. Assert.IsNull (form.ID, "ID-3");
  174. Assert.IsNotNull (form.UniqueID, "UniqueID-3");
  175. Assert.IsNotNull (form.Name, "Name-2");
  176. }
  177. [Test]
  178. [Category ("NotWorking")]
  179. public void RenderChildren ()
  180. {
  181. Page p = new Page();
  182. FormPoker form = new FormPoker ();
  183. form.Page = p;
  184. #if NET_2_0
  185. HtmlDiff.AssertAreEqual ("<div>\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"\r\n__VIEWSTATE\" value=\"\" />\r\n</div>", form.RenderChildren ().Trim (), "A1");
  186. #else
  187. HtmlDiff.AssertAreEqual ("<input type=\"hidden\" name=\"__VIEWSTATE\" value=\"\" />", form.RenderChildren ().Trim (), "A1");
  188. #endif
  189. }
  190. #if NET_2_0
  191. [Test]
  192. public void ControlCollection ()
  193. {
  194. FormPoker poker = new FormPoker();
  195. ControlCollection col = poker.GetControlCollection();
  196. Assert.AreEqual (col.GetType(), typeof (ControlCollection), "A1");
  197. Assert.IsFalse (col.IsReadOnly, "A2");
  198. Assert.AreEqual (0, col.Count, "A3");
  199. }
  200. #endif
  201. }
  202. }