[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlFormTest.cs
1 //
2 // HtmlFormTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlForm
4 //
5 // Author:
6 //      Dick Porter  <dick@ximian.com>
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
30 using System;
31 using System.IO;
32 using System.Text;
33 using System.Web;
34 using System.Web.UI;
35 using System.Web.UI.HtmlControls;
36 using MonoTests.stand_alone.WebHarness;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Web.UI.HtmlControls {
40
41         class TestPage : SystemWebTestShim.Page {
42
43                 private HttpContext ctx;
44
45                 // don't call base class (so _context is never set to a non-null value)
46                 protected internal override HttpContext Context {
47                         get {
48                                 if (ctx == null) {
49                                         ctx = new HttpContext (
50                                                 new HttpRequest ("default.aspx", "http://mono-project.com/", "q=1&q2=2"),
51                                                 new HttpResponse (new StringWriter ())
52                                                 );
53                                 }
54                                 return ctx;
55                         }
56                 }
57
58                 public void SetContext ()
59                 {            
60                         SetContext (Context);
61                 }
62         }
63         
64         public class FormPoker : HtmlForm {
65                 public FormPoker () {
66                         TrackViewState ();
67                 }
68
69                 public object SaveState ()
70                 {
71                         return SaveViewState ();
72                 }
73
74                 public void LoadState (object state)
75                 {
76                         LoadViewState (state);
77                 }
78
79                 /* Not called at all when running the current tests (2005/09/29)
80                 protected override void OnInit (EventArgs e)
81                 {
82                         Console.WriteLine (Environment.StackTrace);
83                         base.OnInit (e);
84                 }
85                 */
86                 public string RenderChildren ()
87                 {
88                         StringWriter sw = new StringWriter();
89                         HtmlTextWriter w = new HtmlTextWriter (sw);
90                         
91                         RenderChildren (w);
92
93                         return sw.ToString();
94                 }
95
96                 public string RenderAttributes ()
97                 {
98                         StringWriter sw = new StringWriter();
99                         HtmlTextWriter w = new HtmlTextWriter (sw);
100                         
101                         RenderAttributes (w);
102
103                         return sw.ToString ();
104                 }
105                 public ControlCollection GetControlCollection ()
106                 {
107                         return CreateControlCollection();
108                 }
109         }
110
111         class FUControl : UserControl {
112         }
113
114
115         [TestFixture]
116         public class HtmlFormTest {
117                 [Test]
118                 public void DefaultProperties ()
119                 {
120                         HtmlForm form = new HtmlForm ();
121                         Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
122
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");
127
128                         Assert.AreEqual ("form", form.TagName, "TagName");
129
130                         Assert.IsFalse (form.SubmitDisabledControls, "TagName");
131                 }
132
133                 [Test]
134                 public void NullProperties ()
135                 {
136                         HtmlForm form = new HtmlForm ();
137
138                         form.Enctype = null;
139                         Assert.AreEqual (String.Empty, form.Enctype, "Enctype");
140                         form.Method = null;
141                         Assert.AreEqual ("post", form.Method, "Method");
142                         form.Name = null;
143                         Assert.AreEqual (form.UniqueID, form.Name, "Name");
144                         form.Target = null;
145                         Assert.AreEqual (String.Empty, form.Target, "Target");
146
147                         form.DefaultButton = null;
148                         Assert.AreEqual (String.Empty, form.DefaultButton, "DefaultButton");
149
150                         form.DefaultFocus = null;
151                         Assert.AreEqual (String.Empty, form.DefaultFocus, "DefaultFocus");
152                         Assert.AreEqual (0, form.Attributes.Count, "Attributes.Count");
153
154                 }
155
156                 [Test]
157                 public void Attributes ()
158                 {
159                         HtmlForm form = new HtmlForm ();
160                         IAttributeAccessor a = (IAttributeAccessor)form;
161
162                         /* not stored in Attributes */
163                         form.DefaultButton = "defaultbutton";
164                         Assert.IsNull (a.GetAttribute ("defaultbutton"), "A1");
165
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");
171
172                         form.Method = "method";
173                         Assert.AreEqual ("method", a.GetAttribute ("method"), "A4");
174
175                         /* not stored in Attributes */
176                         form.Name = "name";
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");
181                         form.ID = "hithere";
182                         Assert.AreEqual ("hithere", form.Name, "A9");
183
184                         form.SubmitDisabledControls = true;
185                         Assert.IsNull (a.GetAttribute ("submitdisabledcontrols"), "A10");
186
187                         form.Target = "target";
188                         Assert.AreEqual ("target", a.GetAttribute ("target"), "A11");
189                 }
190
191 #if !TARGET_DOTNET
192                 [Test]
193                 public void ActionStringWithQuery ()
194                 {
195                         TestPage p = new TestPage ();
196                         p.SetContext ();
197                         FormPoker form = new FormPoker ();
198                         form.Page = p;
199                         string attrs = form.RenderAttributes ();
200
201                         // Indirect test for HttpRequest.QueryStringRaw, see
202                         // https://bugzilla.novell.com/show_bug.cgi?id=376352
203 #if NET_4_0
204                         Assert.AreEqual (" method=\"post\" action=\"?q=1&amp;q2=2\"", attrs, "A1");
205                         form.RenderingCompatibility = new Version (3, 5);
206                         attrs = form.RenderAttributes ();
207 #endif
208                         Assert.AreEqual (" name=\"aspnetForm\" method=\"post\" action=\"?q=1&amp;q2=2\"", attrs, "A2");
209                 }
210 #endif
211
212                 [Test]
213                 public void Undocumented_ActionProperty ()
214                 {
215                         TestPage p = new TestPage ();
216                         p.SetContext ();
217                         FormPoker form = new FormPoker ();
218                         form.Page = p;
219                         form.Action = "someactionfile.aspx";
220                         string attrs = form.RenderAttributes ();
221 #if NET_4_0
222                         Assert.AreEqual (" method=\"post\" action=\"someactionfile.aspx\"", attrs, "A1");
223                         form.RenderingCompatibility = new Version (3, 5);
224                         attrs = form.RenderAttributes ();
225 #endif
226                         Assert.AreEqual (" name=\"aspnetForm\" method=\"post\" action=\"someactionfile.aspx\"", attrs, "A2");
227                 }
228                 
229                 [Test]
230                 public void ViewState ()
231                 {
232                         FormPoker form = new FormPoker();
233                         FormPoker copy = new FormPoker();
234
235                         form.DefaultButton = "defaultbutton";
236                         form.DefaultFocus = "defaultfocus";
237
238                         object state = form.SaveState();
239                         copy.LoadState (state);
240
241                         Assert.AreEqual ("", copy.DefaultButton, "A1");
242                         Assert.AreEqual ("defaultfocus", form.DefaultFocus, "A2");
243
244                 }
245
246                 [Test]
247                 public void Name_InsideNaming ()
248                 {
249                         Control ctrl = new FUControl ();
250                         ctrl.ID = "parent";
251                         FormPoker form = new FormPoker ();
252                         ctrl.Controls.Add (form);
253                         Assert.IsNull (form.ID, "ID");
254                         form.Name = "name";
255                         Assert.AreEqual (form.Name, form.UniqueID, "name and unique id");
256
257                         form.ID = "id";
258                         Assert.AreEqual ("id", form.ID, "ID-2");
259                         Assert.AreEqual (form.UniqueID, form.Name, "Name-ID");
260
261                         form.Name = "name";
262                         Assert.AreEqual (form.Name, form.UniqueID, "UniqueID-2");
263
264                         form.ID = null;
265                         Assert.IsNull (form.ID, "ID-3");
266                         Assert.IsNotNull (form.UniqueID, "UniqueID-3");
267                         Assert.IsNotNull (form.Name, "Name-2");
268                 }
269
270                 [Test]
271                 [Category ("NotWorking")]
272                 public void RenderChildren ()
273                 {
274                         Page p = new Page();
275                         FormPoker form = new FormPoker ();
276                         form.Page = p;
277                         HtmlDiff.AssertAreEqual ("<div>\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"\r\n__VIEWSTATE\" value=\"\" />\r\n</div>", form.RenderChildren ().Trim (), "A1");
278                 }
279
280                 [Test]
281                 public void ControlCollection ()
282                 {
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");
288                 }
289         }
290 }