Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.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                 [TestFixtureSetUp]
91                 public void SetUp ()
92                 {
93                         WebTest.CopyResource (GetType (), "ButtonColor_Bug325489.aspx", "ButtonColor_Bug325489.aspx");
94                 }
95
96                 [Test]
97                 public void ButtonColor_Bug325489 ()
98                 {
99                         WebTest t = new WebTest ("ButtonColor_Bug325489.aspx");
100                         string origHtml = @"<input type=""submit"" name=""button1"" value="""" id=""button1"" style=""background-color:#316AC5;"" />";
101                         string html = t.Run ();
102                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (html);
103
104                         HtmlDiff.AssertAreEqual (origHtml, renderedHtml, "#A1");
105                 }
106                 
107                 [Test]
108                 public void Button_DefaultValues ()
109                 {
110                         Button b = new Button ();
111                         Assert.AreEqual (true, b.CausesValidation, "CausesValidation");
112                         Assert.AreEqual (string.Empty, b.CommandArgument, "CommandArgument");
113                         Assert.AreEqual (string.Empty, b.CommandName, "CommandName");                   
114 #if NET_2_0
115                         Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup");
116                         Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick");
117                         Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
118                         Assert.AreEqual (true, b.UseSubmitBehavior, "UseSubmitBehavior");
119 #endif
120                 }
121
122                 [Test]
123                 public void AssignProperties ()
124                 {
125                         Button b = new Button ();
126 #if NET_2_0
127                         Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick#1");
128                         b.OnClientClick = "Test()";
129                         Assert.AreEqual ("Test()", b.OnClientClick, "OnClientClick#2");
130                         Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
131                         b.PostBackUrl = "Test";
132                         Assert.AreEqual ("Test", b.PostBackUrl, "PostBackUrl");
133                         Assert.AreEqual (true, b.UseSubmitBehavior, "UseSubmitBehavior#1");
134                         b.UseSubmitBehavior = false;
135                         Assert.AreEqual (false, b.UseSubmitBehavior, "UseSubmitBehavior#2");
136                         Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup#1");
137                         b.ValidationGroup = "test";
138                         Assert.AreEqual ("test", b.ValidationGroup, "ValidationGroup#2");
139 #endif
140                 }
141
142                 [Test]
143                 public void Button_ViewState ()
144                 {
145                         PokerButton p = new PokerButton ();
146
147                         Assert.AreEqual (p.Text, "", "A1");
148                         p.Text = "Hello";
149                         Assert.AreEqual (p.Text, "Hello", "A2");
150
151 #if NET_2_0
152                         p.ValidationGroup = "VG1";
153                         p.UseSubmitBehavior = false;
154                         p.OnClientClick = "ClientClick()";
155                         p.PostBackUrl = "PostBackUrl";
156                         Assert.AreEqual (p.ValidationGroup, "VG1", "A3");
157                         Assert.AreEqual (false, p.UseSubmitBehavior, "ViewState_UseSubmitBehavior#original");
158                         Assert.AreEqual ("ClientClick()", p.OnClientClick, "ViewState_OnClientClick#original");
159                         Assert.AreEqual ("PostBackUrl", p.PostBackUrl, "ViewState_PostBackUrl#original");
160 #endif
161
162                         object state = p.SaveState ();
163
164                         PokerButton copy = new PokerButton ();
165                         copy.LoadState (state);
166                         Assert.AreEqual (copy.Text, "Hello", "A4");
167
168 #if NET_2_0
169                         Assert.AreEqual (copy.ValidationGroup, "VG1", "A5");
170                         Assert.AreEqual (false, copy.UseSubmitBehavior, "ViewState_UseSubmitBehavior#copy");
171                         Assert.AreEqual ("ClientClick()", p.OnClientClick, "ViewState_OnClientClick#copy");
172                         Assert.AreEqual ("PostBackUrl", p.PostBackUrl, "ViewState_PostBackUrl#copy");
173 #endif
174                 }
175
176                 [Test]
177                 public void Button_Render ()
178                 {
179                         StringWriter sw = new StringWriter ();
180                         HtmlTextWriter tw = new HtmlTextWriter (sw);
181
182                         Button b = new Button ();
183                         b.Text = "Hello";
184                         b.RenderControl (tw);
185                         
186                         Assert.AreEqual (true, sw.ToString().IndexOf ("value=\"Hello\"") != -1, "A4");
187                         Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A5");
188                         Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"submit\"") != -1, "A6");
189                 }
190
191                 [Test]
192                 public void IgnoresChildren ()
193                 {
194                         Button b = new  Button ();
195                         b.Controls.Add (new LiteralControl ("hola"));
196                         Assert.AreEqual (1, b.Controls.Count, "controls");
197                         StringWriter sw = new StringWriter ();
198                         HtmlTextWriter tw = new HtmlTextWriter (sw);
199                         b.RenderControl (tw);
200                         string str = tw.ToString ();
201                         Assert.AreEqual (-1, str.IndexOf ("hola"), "hola");
202                 }
203
204 #if NET_2_0
205                 [Test]
206                 public void Button_Render2 () {
207                         StringWriter sw = new StringWriter ();
208                         HtmlTextWriter tw = new HtmlTextWriter (sw);
209                         
210                         Button b = new Button ();
211                         b.ID = "MyButton";
212                         b.Text = "Hello";
213                         b.UseSubmitBehavior = false;
214                         b.Enabled = false;
215                         b.ToolTip = "Hello_ToolTip";
216                         b.RenderControl (tw);
217                         
218                         string strTarget = "<input type=\"button\" name=\"MyButton\" value=\"Hello\" disabled=\"disabled\" title=\"Hello_ToolTip\" />";
219                         string str = sw.ToString();
220                         HtmlDiff.AssertAreEqual (strTarget, str, "Button_Render2");
221                 }
222
223                 [Test]
224                 public void GetPostBackOptions ()
225                 {
226                         PokerButton b = new PokerButton ();
227                         PostBackOptions opt = b.GetPostBackOptions ();
228                         Assert.AreEqual (typeof (PokerButton), opt.TargetControl.GetType (), "GetPostBackOptions#1");
229                 }
230
231                 [Test]
232                 public void OnPreRender ()
233                 {
234                         PokerButton b = new PokerButton ();
235                         b.PreRender += new EventHandler (b_PreRender);
236                         Assert.AreEqual (false, eventPreRender, "Before PreRender");
237                         b.OnPreRender (new EventArgs ());
238                         Assert.AreEqual (true, eventPreRender, "After PreRender");
239                 }
240
241                 bool eventPreRender;
242                 void b_PreRender (object sender, EventArgs e)
243                 {
244                         eventPreRender = true;
245                 }
246
247                 [Test]
248                 [Category("NunitWeb")]
249                 public void PostBackUrl ()
250                 {
251                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (PostBackUrl_Load));
252                         string html = t.Run ();
253                         if (html.IndexOf ("onclick") == -1)
254                                 Assert.Fail ("Button Postback script not created fail");
255                         if (html.IndexOf ("MyPageWithMaster.aspx") == -1)
256                                 Assert.Fail ("Link to postback page not created fail");
257                         if (html.IndexOf ("__PREVIOUSPAGE") == -1)
258                                 Assert.Fail ("Previos page hidden control not created fail");
259                 }
260
261                 public static void PostBackUrl_Load (Page p)
262                 {
263                         PokerButton b = new PokerButton ();
264                         b.PostBackUrl = "~/MyPageWithMaster.aspx";
265                         p.Form.Controls.Add (b);
266                 }
267
268                 [Test]
269                 public void RaisePostBackEvent ()
270                 {
271                         Page p = new Page ();
272                         PokerButton b = new PokerButton ();
273                         b.Click += new EventHandler (b_Click);
274                         p.Controls.Add (b);
275                         Assert.AreEqual (false, eventRaisePostBackEvent, "RaisePostBackEvent#1");
276                         b.RaisePostBackEvent ("Click");
277                         Assert.AreEqual (true, eventRaisePostBackEvent, "RaisePostBackEvent#2");
278                 }
279
280                 bool eventRaisePostBackEvent;
281                 void b_Click (object sender, EventArgs e)
282                 {
283                         eventRaisePostBackEvent = true;
284                 }
285
286                 [Test]
287                 [Category ("NunitWeb")]
288                 public void UseSubmitBehavior ()
289                 {
290                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (UseSubmitBehavior_Load));
291                         string html = t.Run ();
292                         if (html.IndexOf ("onclick") == -1)
293                                 Assert.Fail ("Button Postback script not created fail");
294                 }
295
296                 public static void UseSubmitBehavior_Load (Page p)
297                 {
298                         PokerButton b = new PokerButton ();
299                         b.UseSubmitBehavior = false;
300                         p.Controls.Add (b);
301                 }
302
303                 [Test]
304                 public void ValidationGroup ()
305                 {
306                         // Client side. 
307                 }
308
309                 [TestFixtureTearDown]
310                 public void TearDown ()
311                 {
312                         WebTest.Unload ();
313                 }
314 #endif
315
316         }
317 }
318
319