Merge branch 'sgen-disable-gc'
[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 #if NET_2_0
106                         p.CausesValidation = false;
107                         p.ValidationGroup = "VG";
108 #endif
109                         object s = p.SaveState();
110                         HtmlInputButtonPoker copy = new HtmlInputButtonPoker ();
111                         copy.LoadState (s);
112
113 #if NET_2_0
114                         Assert.IsFalse (copy.CausesValidation, "A1");
115                         Assert.AreEqual ("VG", p.ValidationGroup, "A2");
116 #endif
117                 }
118
119                 [Test]
120                 public void RenderAttributes ()
121                 {
122                         StringWriter sw = new StringWriter ();
123                         HtmlTextWriter tw = new HtmlTextWriter (sw);
124
125                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
126                         
127                         p.Page = new Page ();
128
129                         p.CausesValidation = false;
130 #if NET_2_0
131                         p.ValidationGroup = "VG";
132
133                         Assert.AreEqual (3, p.Attributes.Count, "A1");
134 #else
135                         Assert.AreEqual (2, p.Attributes.Count, "A1");
136 #endif
137
138                         tw.WriteBeginTag ("dummy");
139                         p.DoRenderAttributes (tw);
140                         tw.Write ('>');
141 #if NET_2_0
142                         HtmlDiff.AssertAreEqual ("<dummy name type=\"button\" ValidationGroup=\"VG\" />", sw.ToString (), "A2");
143 #else
144                         HtmlDiff.AssertAreEqual ("<dummy name type=\"button\" />", sw.ToString (), "A2");
145 #endif
146                 }
147
148                 [Test]
149                 public void OnClickAttribute ()
150                 {
151                         StringWriter sw = new StringWriter ();
152                         HtmlTextWriter tw = new HtmlTextWriter (sw);
153
154                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
155                         p.Page = new Page ();
156                         p.DoRenderAttributes (tw);
157                         string str = sw.ToString ();
158                         int found = str.IndexOf ("onclick");
159                         Assert.AreEqual (-1, found, "#01");
160                         p.ServerClick += new EventHandler (EmptyHandler);
161                         sw = new StringWriter ();
162                         tw = new HtmlTextWriter (sw);
163                         p.DoRenderAttributes (tw);
164                         str = sw.ToString ();
165                         found = str.IndexOf ("onclick");
166                         Assert.IsTrue (found >= 0, "#02");
167                 }
168
169                 [Test]
170                 public void OnClickAttributeWithSpecials ()
171                 {
172 #if NET_4_0
173                         string origHtml = "alert(&#39;&lt;&amp;&#39;);";
174                         string origHtml2 = "alert('<&');";
175 #else
176                         string origHtml = "alert('&lt;&amp;');";
177                         string origHtml2 = "alert('<&');";
178 #endif
179
180                         StringWriter sw = new StringWriter ();
181                         HtmlTextWriter tw = new HtmlTextWriter (sw);
182
183                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
184                         p.Page = new Page ();
185                         p.Attributes["onclick"] = "alert('<&');";
186                         p.DoRenderAttributes (tw);
187                         string str = sw.ToString ();
188                         int found = str.IndexOf (origHtml);
189                         Assert.IsTrue (found >= 0, "#01");
190                         p.ServerClick += new EventHandler (EmptyHandler);
191                         sw = new StringWriter ();
192                         tw = new HtmlTextWriter (sw);
193                         p.DoRenderAttributes (tw);
194                         str = sw.ToString ();
195                         found = str.IndexOf (origHtml2);
196                         Assert.IsTrue (found >= 0, "#02" + str);
197                 }
198
199                 private static void EmptyHandler (object sender, EventArgs e)
200                 {
201                 }
202
203                 [Test]
204                 public void RenderOnclick1 ()
205                 {
206                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
207                         it.ID = "id1";
208                         it.ServerClick += new EventHandler (EmptyHandler);
209                         string rendered = it.RenderToString ();
210                         Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
211                 }
212
213                 [Test]
214                 public void RenderOnclick2 ()
215                 {
216                         Page page = new Page ();
217 #if NET_2_0
218                         page.EnableEventValidation = false;
219 #endif
220                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
221                         page.Controls.Add (it);
222                         it.ID = "id1";
223                         it.ServerClick += new EventHandler (EmptyHandler);
224                         string rendered = it.RenderToString ();
225                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
226                 }
227
228                 [Test]
229                 public void RenderOnclick3 ()
230                 {
231                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
232                         it.ID = "id1";
233                         it.ServerClick += new EventHandler (EmptyHandler);
234                         string rendered = it.RenderToString ();
235                         Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
236                 }
237
238                 [Test]
239                 [Category ("NotWorking")]
240                 public void RenderOnclick4 ()
241                 {
242                         Page page = new Page ();
243 #if NET_2_0
244                         page.EnableEventValidation = false;
245 #endif
246                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
247                         page.Controls.Add (it);
248                         it.ID = "id1";
249                         it.ServerClick += new EventHandler (EmptyHandler);
250                         string rendered = it.RenderToString ();
251                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
252                         Assert.IsTrue (rendered.IndexOf ("__doPostBack") != -1, "#02");
253                         Assert.IsTrue (rendered.IndexOf ("type=\"submit\"") != -1, "#03");
254                 }
255
256                 [Test]
257                 public void RenderOnclick5 ()
258                 {
259                         Page page = new Page ();
260 #if NET_2_0
261                         page.EnableEventValidation = false;
262 #endif
263                         RequiredFieldValidator val = new RequiredFieldValidator ();
264                         val.ControlToValidate = "id1";
265                         page.Validators.Add (val);
266                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
267                         page.Controls.Add (it);
268                         it.ID = "id1";
269                         it.ServerClick += new EventHandler (EmptyHandler);
270                         string rendered = it.RenderToString ();
271                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
272                 }
273         }       
274 }
275