HtmlFormTest.cs 6.3 KB

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