Merge pull request #1870 from saper/langinfo_h
[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                         string origHtml = "alert(&#39;&lt;&amp;&#39;);";
161                         string origHtml2 = "alert('<&');";
162
163                         StringWriter sw = new StringWriter ();
164                         HtmlTextWriter tw = new HtmlTextWriter (sw);
165
166                         HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
167                         p.Page = new Page ();
168                         p.Attributes["onclick"] = "alert('<&');";
169                         p.DoRenderAttributes (tw);
170                         string str = sw.ToString ();
171                         int found = str.IndexOf (origHtml);
172                         Assert.IsTrue (found >= 0, "#01");
173                         p.ServerClick += new EventHandler (EmptyHandler);
174                         sw = new StringWriter ();
175                         tw = new HtmlTextWriter (sw);
176                         p.DoRenderAttributes (tw);
177                         str = sw.ToString ();
178                         found = str.IndexOf (origHtml2);
179                         Assert.IsTrue (found >= 0, "#02" + str);
180                 }
181
182                 private static void EmptyHandler (object sender, EventArgs e)
183                 {
184                 }
185
186                 [Test]
187                 public void RenderOnclick1 ()
188                 {
189                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
190                         it.ID = "id1";
191                         it.ServerClick += new EventHandler (EmptyHandler);
192                         string rendered = it.RenderToString ();
193                         Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
194                 }
195
196                 [Test]
197                 public void RenderOnclick2 ()
198                 {
199                         Page page = new Page ();
200                         page.EnableEventValidation = false;
201                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
202                         page.Controls.Add (it);
203                         it.ID = "id1";
204                         it.ServerClick += new EventHandler (EmptyHandler);
205                         string rendered = it.RenderToString ();
206                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
207                 }
208
209                 [Test]
210                 public void RenderOnclick3 ()
211                 {
212                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
213                         it.ID = "id1";
214                         it.ServerClick += new EventHandler (EmptyHandler);
215                         string rendered = it.RenderToString ();
216                         Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
217                 }
218
219                 [Test]
220                 [Category ("NotWorking")]
221                 public void RenderOnclick4 ()
222                 {
223                         Page page = new Page ();
224                         page.EnableEventValidation = false;
225                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
226                         page.Controls.Add (it);
227                         it.ID = "id1";
228                         it.ServerClick += new EventHandler (EmptyHandler);
229                         string rendered = it.RenderToString ();
230                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
231                         Assert.IsTrue (rendered.IndexOf ("__doPostBack") != -1, "#02");
232                         Assert.IsTrue (rendered.IndexOf ("type=\"submit\"") != -1, "#03");
233                 }
234
235                 [Test]
236                 public void RenderOnclick5 ()
237                 {
238                         Page page = new Page ();
239                         page.EnableEventValidation = false;
240                         RequiredFieldValidator val = new RequiredFieldValidator ();
241                         val.ControlToValidate = "id1";
242                         page.Validators.Add (val);
243                         HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
244                         page.Controls.Add (it);
245                         it.ID = "id1";
246                         it.ServerClick += new EventHandler (EmptyHandler);
247                         string rendered = it.RenderToString ();
248                         Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
249                 }
250         }       
251 }
252