[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlInputButtonTest.cs
1 //
2 // HtmlInputButtonTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlInputButton
4 //
5 // Author:
6 //      Jackson Harper  (jackson@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.Web.UI;
33 using System.Web.UI.HtmlControls;
34 using System.Web.UI.WebControls;
35 using MonoTests.stand_alone.WebHarness;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Web.UI.HtmlControls {
39
40         public class HtmlInputButtonPoker : HtmlInputButton {
41
42                 public HtmlInputButtonPoker ()
43                 {
44                         TrackViewState ();
45                 }
46
47                 public HtmlInputButtonPoker (string type) : base (type)
48                 {
49                         TrackViewState ();
50                 }
51
52                 public object SaveState ()
53                 {
54                         return SaveViewState ();
55                 }
56
57                 public void LoadState (object state)
58                 {
59                         LoadViewState (state);
60                 }
61
62                 public void DoRenderAttributes (HtmlTextWriter writer)
63                 {
64                         RenderAttributes (writer);
65                 }
66
67                 public string RenderToString ()
68                 {
69                         StringWriter sr = new StringWriter ();
70                         RenderAttributes (new HtmlTextWriter (sr));
71                         return sr.ToString ();
72                 }
73         }
74
75         [TestFixture]
76         public class HtmlInputButtonTest {
77
78                 [Test]
79                 public void Defaults ()
80                 {
81                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
82
83                         Assert.IsTrue (p.CausesValidation, "A1");
84                 }
85
86                 [Test]
87                 public void CleanProperties ()
88                 {
89                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
90
91                         p.CausesValidation = false;
92                         Assert.IsFalse (p.CausesValidation, "A1");
93
94                         p.CausesValidation = true;
95                         Assert.IsTrue (p.CausesValidation, "A2");
96
97                         p.CausesValidation = false;
98                         Assert.IsFalse (p.CausesValidation, "A3");
99                 }
100
101                 [Test]
102                 public void ViewState ()
103                 {
104                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
105                         p.CausesValidation = false;
106                         p.ValidationGroup = "VG";
107                         object s = p.SaveState();
108                         HtmlInputButtonPoker copy = new HtmlInputButtonPoker ();
109                         copy.LoadState (s);
110
111                         Assert.IsFalse (copy.CausesValidation, "A1");
112                         Assert.AreEqual ("VG", p.ValidationGroup, "A2");
113                 }
114
115                 [Test]
116                 public void RenderAttributes ()
117                 {
118                         StringWriter sw = new StringWriter ();
119                         HtmlTextWriter tw = new HtmlTextWriter (sw);
120
121                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
122                         
123                         p.Page = new Page ();
124
125                         p.CausesValidation = false;
126                         p.ValidationGroup = "VG";
127
128                         Assert.AreEqual (3, p.Attributes.Count, "A1");
129
130                         tw.WriteBeginTag ("dummy");
131                         p.DoRenderAttributes (tw);
132                         tw.Write ('>');
133                         HtmlDiff.AssertAreEqual ("<dummy name type=\"button\" ValidationGroup=\"VG\" />", sw.ToString (), "A2");
134                 }
135
136                 [Test]
137                 public void OnClickAttribute ()
138                 {
139                         StringWriter sw = new StringWriter ();
140                         HtmlTextWriter tw = new HtmlTextWriter (sw);
141
142                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
143                         p.Page = new Page ();
144                         p.DoRenderAttributes (tw);
145                         string str = sw.ToString ();
146                         int found = str.IndexOf ("onclick");
147                         Assert.AreEqual (-1, found, "#01");
148                         p.ServerClick += new EventHandler (EmptyHandler);
149                         sw = new StringWriter ();
150                         tw = new HtmlTextWriter (sw);
151                         p.DoRenderAttributes (tw);
152                         str = sw.ToString ();
153                         found = str.IndexOf ("onclick");
154                         Assert.IsTrue (found >= 0, "#02");
155                 }
156
157                 [Test]
158                 public void OnClickAttributeWithSpecials ()
159                 {
160 #if NET_4_0
161                         string origHtml = "alert(&#39;&lt;&amp;&#39;);";
162                         string origHtml2 = "alert('<&');";
163 #else
164                         string origHtml = "alert('&lt;&amp;');";
165                         string origHtml2 = "alert('<&');";
166 #endif
167
168                         StringWriter sw = new StringWriter ();
169                         HtmlTextWriter tw = new HtmlTextWriter (sw);
170
171                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
172                         p.Page = new Page ();
173                         p.Attributes["onclick"] = "alert('<&');";
174                         p.DoRenderAttributes (tw);
175                         string str = sw.ToString ();
176                         int found = str.IndexOf (origHtml);
177                         Assert.IsTrue (found >= 0, "#01");
178                         p.ServerClick += new EventHandler (EmptyHandler);
179                         sw = new StringWriter ();
180                         tw = new HtmlTextWriter (sw);
181                         p.DoRenderAttributes (tw);
182                         str = sw.ToString ();
183                         found = str.IndexOf (origHtml2);
184                         Assert.IsTrue (found >= 0, "#02" + str);
185                 }
186
187                 private static void EmptyHandler (object sender, EventArgs e)
188                 {
189                 }
190
191                 [Test]
192                 public void RenderOnclick1 ()
193                 {
194                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
195                         it.ID = "id1";
196                         it.ServerClick += new EventHandler (EmptyHandler);
197                         string rendered = it.RenderToString ();
198                         Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
199                 }
200
201                 [Test]
202                 public void RenderOnclick2 ()
203                 {
204                         Page page = new Page ();
205                         page.EnableEventValidation = false;
206                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
207                         page.Controls.Add (it);
208                         it.ID = "id1";
209                         it.ServerClick += new EventHandler (EmptyHandler);
210                         string rendered = it.RenderToString ();
211                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
212                 }
213
214                 [Test]
215                 public void RenderOnclick3 ()
216                 {
217                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
218                         it.ID = "id1";
219                         it.ServerClick += new EventHandler (EmptyHandler);
220                         string rendered = it.RenderToString ();
221                         Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
222                 }
223
224                 [Test]
225                 [Category ("NotWorking")]
226                 public void RenderOnclick4 ()
227                 {
228                         Page page = new Page ();
229                         page.EnableEventValidation = false;
230                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
231                         page.Controls.Add (it);
232                         it.ID = "id1";
233                         it.ServerClick += new EventHandler (EmptyHandler);
234                         string rendered = it.RenderToString ();
235                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
236                         Assert.IsTrue (rendered.IndexOf ("__doPostBack") != -1, "#02");
237                         Assert.IsTrue (rendered.IndexOf ("type=\"submit\"") != -1, "#03");
238                 }
239
240                 [Test]
241                 public void RenderOnclick5 ()
242                 {
243                         Page page = new Page ();
244                         page.EnableEventValidation = false;
245                         RequiredFieldValidator val = new RequiredFieldValidator ();
246                         val.ControlToValidate = "id1";
247                         page.Validators.Add (val);
248                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
249                         page.Controls.Add (it);
250                         it.ID = "id1";
251                         it.ServerClick += new EventHandler (EmptyHandler);
252                         string rendered = it.RenderToString ();
253                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
254                 }
255         }       
256 }
257