2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ButtonTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.Button.cs
3 //
4 // Author:
5 //      Jordi Mas i Hernandez (jordi@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8
9 //
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.IO;
35 using System.Globalization;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.UI.WebControls;
39 using MonoTests.SystemWeb.Framework;
40 using MonoTests.stand_alone.WebHarness;
41
42 namespace MonoTests.System.Web.UI.WebControls
43 {
44         class PokerButton : Button {
45                 public PokerButton ()
46                 {
47                         TrackViewState ();
48                 }
49
50                 public object SaveState ()
51                 {
52                         return SaveViewState ();
53                 }
54
55                 public void LoadState (object o)
56                 {
57                         LoadViewState (o);
58                 }
59
60                 public string Render ()
61                 {
62                         StringWriter sw = new StringWriter ();
63                         sw.NewLine = "\n";
64                         HtmlTextWriter writer = new HtmlTextWriter (sw);
65                         base.Render (writer);
66                         return writer.InnerWriter.ToString ();
67                 }               
68 #if NET_2_0
69                 public new PostBackOptions GetPostBackOptions ()
70                 {
71                         return base.GetPostBackOptions ();
72                 }
73
74                 public new void OnPreRender (EventArgs e)
75                 {
76                         base.OnPreRender (e);
77                 }
78
79                 public new void RaisePostBackEvent (string eventArgument)
80                 {
81                         base.RaisePostBackEvent (eventArgument);
82                 }
83 #endif
84         }
85
86
87         [TestFixture]
88         public class ButtonTest {
89                 
90                 [Test]
91                 public void Button_DefaultValues ()
92                 {
93                         Button b = new Button ();
94                         Assert.AreEqual (true, b.CausesValidation, "CausesValidation");
95                         Assert.AreEqual (string.Empty, b.CommandArgument, "CommandArgument");
96                         Assert.AreEqual (string.Empty, b.CommandName, "CommandName");                   
97 #if NET_2_0
98                         Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup");
99                         Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick");
100                         Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
101                         Assert.AreEqual (true, b.UseSubmitBehavior, "UseSubmitBehavior");
102 #endif
103                 }
104
105                 [Test]
106                 public void AssignProperties ()
107                 {
108                         Button b = new Button ();
109 #if NET_2_0
110                         Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick#1");
111                         b.OnClientClick = "Test()";
112                         Assert.AreEqual ("Test()", b.OnClientClick, "OnClientClick#2");
113                         Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
114                         b.PostBackUrl = "Test";
115                         Assert.AreEqual ("Test", b.PostBackUrl, "PostBackUrl");
116                         Assert.AreEqual (true, b.UseSubmitBehavior, "UseSubmitBehavior#1");
117                         b.UseSubmitBehavior = false;
118                         Assert.AreEqual (false, b.UseSubmitBehavior, "UseSubmitBehavior#2");
119                         Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup#1");
120                         b.ValidationGroup = "test";
121                         Assert.AreEqual ("test", b.ValidationGroup, "ValidationGroup#2");
122 #endif
123                 }
124
125                 [Test]
126                 public void Button_ViewState ()
127                 {
128                         PokerButton p = new PokerButton ();
129
130                         Assert.AreEqual (p.Text, "", "A1");
131                         p.Text = "Hello";
132                         Assert.AreEqual (p.Text, "Hello", "A2");
133
134 #if NET_2_0
135                         p.ValidationGroup = "VG1";
136                         p.UseSubmitBehavior = false;
137                         p.OnClientClick = "ClientClick()";
138                         p.PostBackUrl = "PostBackUrl";
139                         Assert.AreEqual (p.ValidationGroup, "VG1", "A3");
140                         Assert.AreEqual (false, p.UseSubmitBehavior, "ViewState_UseSubmitBehavior#original");
141                         Assert.AreEqual ("ClientClick()", p.OnClientClick, "ViewState_OnClientClick#original");
142                         Assert.AreEqual ("PostBackUrl", p.PostBackUrl, "ViewState_PostBackUrl#original");
143 #endif
144
145                         object state = p.SaveState ();
146
147                         PokerButton copy = new PokerButton ();
148                         copy.LoadState (state);
149                         Assert.AreEqual (copy.Text, "Hello", "A4");
150
151 #if NET_2_0
152                         Assert.AreEqual (copy.ValidationGroup, "VG1", "A5");
153                         Assert.AreEqual (false, copy.UseSubmitBehavior, "ViewState_UseSubmitBehavior#copy");
154                         Assert.AreEqual ("ClientClick()", p.OnClientClick, "ViewState_OnClientClick#copy");
155                         Assert.AreEqual ("PostBackUrl", p.PostBackUrl, "ViewState_PostBackUrl#copy");
156 #endif
157                 }
158
159                 [Test]
160                 public void Button_Render ()
161                 {
162                         StringWriter sw = new StringWriter ();
163                         HtmlTextWriter tw = new HtmlTextWriter (sw);
164
165                         Button b = new Button ();
166                         b.Text = "Hello";
167                         b.RenderControl (tw);
168                         
169                         Assert.AreEqual (true, sw.ToString().IndexOf ("value=\"Hello\"") != -1, "A4");
170                         Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A5");
171                         Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"submit\"") != -1, "A6");
172                 }
173
174                 [Test]
175                 public void IgnoresChildren ()
176                 {
177                         Button b = new  Button ();
178                         b.Controls.Add (new LiteralControl ("hola"));
179                         Assert.AreEqual (1, b.Controls.Count, "controls");
180                         StringWriter sw = new StringWriter ();
181                         HtmlTextWriter tw = new HtmlTextWriter (sw);
182                         b.RenderControl (tw);
183                         string str = tw.ToString ();
184                         Assert.AreEqual (-1, str.IndexOf ("hola"), "hola");
185                 }
186
187 #if NET_2_0
188                 [Test]
189                 public void Button_Render2 () {
190                         StringWriter sw = new StringWriter ();
191                         HtmlTextWriter tw = new HtmlTextWriter (sw);
192                         
193                         Button b = new Button ();
194                         b.ID = "MyButton";
195                         b.Text = "Hello";
196                         b.UseSubmitBehavior = false;
197                         b.Enabled = false;
198                         b.ToolTip = "Hello_ToolTip";
199                         b.RenderControl (tw);
200                         
201                         string strTarget = "<input type=\"button\" name=\"MyButton\" value=\"Hello\" disabled=\"disabled\" title=\"Hello_ToolTip\" />";
202                         string str = sw.ToString();
203                         HtmlDiff.AssertAreEqual (strTarget, str, "Button_Render2");
204                 }
205
206                 [Test]
207                 public void GetPostBackOptions ()
208                 {
209                         PokerButton b = new PokerButton ();
210                         PostBackOptions opt = b.GetPostBackOptions ();
211                         Assert.AreEqual (typeof (PokerButton), opt.TargetControl.GetType (), "GetPostBackOptions#1");
212                 }
213
214                 [Test]
215                 public void OnPreRender ()
216                 {
217                         PokerButton b = new PokerButton ();
218                         b.PreRender += new EventHandler (b_PreRender);
219                         Assert.AreEqual (false, eventPreRender, "Before PreRender");
220                         b.OnPreRender (new EventArgs ());
221                         Assert.AreEqual (true, eventPreRender, "After PreRender");
222                 }
223
224                 bool eventPreRender;
225                 void b_PreRender (object sender, EventArgs e)
226                 {
227                         eventPreRender = true;
228                 }
229
230                 [Test]
231                 [Category("NunitWeb")]
232                 public void PostBackUrl ()
233                 {
234                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (PostBackUrl_Load));
235                         string html = t.Run ();
236                         if (html.IndexOf ("onclick") == -1)
237                                 Assert.Fail ("Button Postback script not created fail");
238                         if (html.IndexOf ("MyPageWithMaster.aspx") == -1)
239                                 Assert.Fail ("Link to postback page not created fail");
240                         if (html.IndexOf ("__PREVIOUSPAGE") == -1)
241                                 Assert.Fail ("Previos page hidden control not created fail");
242                 }
243
244                 public static void PostBackUrl_Load (Page p)
245                 {
246                         PokerButton b = new PokerButton ();
247                         b.PostBackUrl = "~/MyPageWithMaster.aspx";
248                         p.Form.Controls.Add (b);
249                 }
250
251                 [Test]
252                 public void RaisePostBackEvent ()
253                 {
254                         Page p = new Page ();
255                         PokerButton b = new PokerButton ();
256                         b.Click += new EventHandler (b_Click);
257                         p.Controls.Add (b);
258                         Assert.AreEqual (false, eventRaisePostBackEvent, "RaisePostBackEvent#1");
259                         b.RaisePostBackEvent ("Click");
260                         Assert.AreEqual (true, eventRaisePostBackEvent, "RaisePostBackEvent#2");
261                 }
262
263                 bool eventRaisePostBackEvent;
264                 void b_Click (object sender, EventArgs e)
265                 {
266                         eventRaisePostBackEvent = true;
267                 }
268
269                 [Test]
270                 [Category ("NunitWeb")]
271                 public void UseSubmitBehavior ()
272                 {
273                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (UseSubmitBehavior_Load));
274                         string html = t.Run ();
275                         if (html.IndexOf ("onclick") == -1)
276                                 Assert.Fail ("Button Postback script not created fail");
277                 }
278
279                 public static void UseSubmitBehavior_Load (Page p)
280                 {
281                         PokerButton b = new PokerButton ();
282                         b.UseSubmitBehavior = false;
283                         p.Controls.Add (b);
284                 }
285
286                 [Test]
287                 public void ValidationGroup ()
288                 {
289                         // Client side. 
290                 }
291
292                 [TestFixtureTearDown]
293                 public void TearDown ()
294                 {
295                         WebTest.Unload ();
296                 }
297 #endif
298
299         }
300 }
301
302