3 // - Unit tests for System.Web.UI.HtmlControls.HtmlForm
6 // Dick Porter <dick@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
35 using System.Web.UI.HtmlControls;
36 using MonoTests.stand_alone.WebHarness;
37 using NUnit.Framework;
39 namespace MonoTests.System.Web.UI.HtmlControls {
41 class TestPage : SystemWebTestShim.Page {
43 private HttpContext ctx;
45 // don't call base class (so _context is never set to a non-null value)
46 protected internal override HttpContext Context {
49 ctx = new HttpContext (
50 new HttpRequest ("default.aspx", "http://mono-project.com/", "q=1&q2=2"),
51 new HttpResponse (new StringWriter ())
58 public void SetContext ()
64 public class FormPoker : HtmlForm {
69 public object SaveState ()
71 return SaveViewState ();
74 public void LoadState (object state)
76 LoadViewState (state);
79 /* Not called at all when running the current tests (2005/09/29)
80 protected override void OnInit (EventArgs e)
82 Console.WriteLine (Environment.StackTrace);
86 public string RenderChildren ()
88 StringWriter sw = new StringWriter();
89 HtmlTextWriter w = new HtmlTextWriter (sw);
96 public string RenderAttributes ()
98 StringWriter sw = new StringWriter();
99 HtmlTextWriter w = new HtmlTextWriter (sw);
101 RenderAttributes (w);
103 return sw.ToString ();
105 public ControlCollection GetControlCollection ()
107 return CreateControlCollection();
111 class FUControl : UserControl {
116 public class HtmlFormTest {
118 public void DefaultProperties ()
120 HtmlForm form = new HtmlForm ();
121 Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
123 Assert.AreEqual (String.Empty, form.Enctype, "Enctype");
124 Assert.AreEqual ("post", form.Method, "Method");
125 Assert.AreEqual (form.UniqueID, form.Name, "Name");
126 Assert.AreEqual (String.Empty, form.Target, "Target");
128 Assert.AreEqual ("form", form.TagName, "TagName");
130 Assert.IsFalse (form.SubmitDisabledControls, "TagName");
134 public void NullProperties ()
136 HtmlForm form = new HtmlForm ();
139 Assert.AreEqual (String.Empty, form.Enctype, "Enctype");
141 Assert.AreEqual ("post", form.Method, "Method");
143 Assert.AreEqual (form.UniqueID, form.Name, "Name");
145 Assert.AreEqual (String.Empty, form.Target, "Target");
147 form.DefaultButton = null;
148 Assert.AreEqual (String.Empty, form.DefaultButton, "DefaultButton");
150 form.DefaultFocus = null;
151 Assert.AreEqual (String.Empty, form.DefaultFocus, "DefaultFocus");
152 Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
157 public void Attributes ()
159 HtmlForm form = new HtmlForm ();
160 IAttributeAccessor a = (IAttributeAccessor)form;
162 /* not stored in Attributes */
163 form.DefaultButton = "defaultbutton";
164 Assert.IsNull (a.GetAttribute ("defaultbutton"), "A1");
166 /* not stored in Attributes */
167 form.DefaultFocus = "defaultfocus";
168 Assert.IsNull (a.GetAttribute ("defaultfocus"), "A2");
169 form.Enctype = "enctype";
170 Assert.AreEqual ("enctype", a.GetAttribute ("enctype"), "A3");
172 form.Method = "method";
173 Assert.AreEqual ("method", a.GetAttribute ("method"), "A4");
175 /* not stored in Attributes */
177 Assert.AreEqual (form.UniqueID, form.Name, "A5");
178 Assert.IsNull (form.Name, "A6");
179 Assert.IsNull (form.UniqueID, "A7");
180 Assert.IsNull (a.GetAttribute ("name"), "A8");
182 Assert.AreEqual ("hithere", form.Name, "A9");
184 form.SubmitDisabledControls = true;
185 Assert.IsNull (a.GetAttribute ("submitdisabledcontrols"), "A10");
187 form.Target = "target";
188 Assert.AreEqual ("target", a.GetAttribute ("target"), "A11");
193 public void ActionStringWithQuery ()
195 TestPage p = new TestPage ();
197 FormPoker form = new FormPoker ();
199 string attrs = form.RenderAttributes ();
201 // Indirect test for HttpRequest.QueryStringRaw, see
202 // https://bugzilla.novell.com/show_bug.cgi?id=376352
204 Assert.AreEqual (" method=\"post\" action=\"?q=1&q2=2\"", attrs, "A1");
205 form.RenderingCompatibility = new Version (3, 5);
206 attrs = form.RenderAttributes ();
208 Assert.AreEqual (" name=\"aspnetForm\" method=\"post\" action=\"?q=1&q2=2\"", attrs, "A2");
213 public void Undocumented_ActionProperty ()
215 TestPage p = new TestPage ();
217 FormPoker form = new FormPoker ();
219 form.Action = "someactionfile.aspx";
220 string attrs = form.RenderAttributes ();
222 Assert.AreEqual (" method=\"post\" action=\"someactionfile.aspx\"", attrs, "A1");
223 form.RenderingCompatibility = new Version (3, 5);
224 attrs = form.RenderAttributes ();
226 Assert.AreEqual (" name=\"aspnetForm\" method=\"post\" action=\"someactionfile.aspx\"", attrs, "A2");
230 public void ViewState ()
232 FormPoker form = new FormPoker();
233 FormPoker copy = new FormPoker();
235 form.DefaultButton = "defaultbutton";
236 form.DefaultFocus = "defaultfocus";
238 object state = form.SaveState();
239 copy.LoadState (state);
241 Assert.AreEqual ("", copy.DefaultButton, "A1");
242 Assert.AreEqual ("defaultfocus", form.DefaultFocus, "A2");
247 public void Name_InsideNaming ()
249 Control ctrl = new FUControl ();
251 FormPoker form = new FormPoker ();
252 ctrl.Controls.Add (form);
253 Assert.IsNull (form.ID, "ID");
255 Assert.AreEqual (form.Name, form.UniqueID, "name and unique id");
258 Assert.AreEqual ("id", form.ID, "ID-2");
259 Assert.AreEqual (form.UniqueID, form.Name, "Name-ID");
262 Assert.AreEqual (form.Name, form.UniqueID, "UniqueID-2");
265 Assert.IsNull (form.ID, "ID-3");
266 Assert.IsNotNull (form.UniqueID, "UniqueID-3");
267 Assert.IsNotNull (form.Name, "Name-2");
271 [Category ("NotWorking")]
272 public void RenderChildren ()
275 FormPoker form = new FormPoker ();
277 HtmlDiff.AssertAreEqual ("<div>\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"\r\n__VIEWSTATE\" value=\"\" />\r\n</div>", form.RenderChildren ().Trim (), "A1");
281 public void ControlCollection ()
283 FormPoker poker = new FormPoker();
284 ControlCollection col = poker.GetControlCollection();
285 Assert.AreEqual (col.GetType(), typeof (ControlCollection), "A1");
286 Assert.IsFalse (col.IsReadOnly, "A2");
287 Assert.AreEqual (0, col.Count, "A3");