HtmlFormTest.cs 5.9 KB

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