2009-11-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / LinkButtonTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.LinkButton 
3 //
4 // Author:
5 //      Miguel de Icaza (miguel@novell.com) [copied alot of this from his Label test]
6 //      Ben Maurer <bmaurer@novell.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 System.Collections;
41 using MonoTests.stand_alone.WebHarness;
42
43 namespace MonoTests.System.Web.UI.WebControls
44 {
45         [TestFixture]
46         public class LinkButtonTest {   
47                 class Poker : LinkButton {
48                         
49                         public new void AddParsedSubObject (object o)
50                         {
51                                 base.AddParsedSubObject (o);
52                         }
53
54                         public void TrackState () 
55                         {
56                                 TrackViewState ();
57                         }
58                         
59                         public object SaveState ()
60                         {
61                                 return SaveViewState ();
62                         }
63                         
64                         public void LoadState (object o)
65                         {
66                                 LoadViewState (o);
67                         }
68                         
69                         public string Render ()
70                         {
71                                 StringWriter sw = new StringWriter ();
72                                 sw.NewLine = "\n";
73                                 HtmlTextWriter writer = new HtmlTextWriter (sw);
74                                 base.Render (writer);
75                                 return writer.InnerWriter.ToString ();
76                         }               
77 #if NET_2_0
78                         public new void RaisePostBackEvent (string eventArgument)
79                         {
80                                 base.RaisePostBackEvent (eventArgument);
81                         }
82
83                         public new PostBackOptions GetPostBackOptions ()
84                         {
85                                 return base.GetPostBackOptions ();
86                         }
87 #endif
88
89                 }
90
91                 [TestFixtureSetUp]
92                 public void SetUp ()
93                 {
94                         WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
95                 }
96
97                 [Test]
98                 public void Defaults ()
99                 {
100                         Poker b = new Poker ();
101 #if NET_2_0
102                         Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick");
103                         Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
104                         Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup");
105 #endif
106                 }
107                 
108 #if NET_2_0
109                 [Test]
110                 public void OnClientClick ()
111                 {
112                         Poker b = new Poker ();
113                         b.OnClientClick = "MyClickMethod";
114                         string html = b.Render ();
115                         HtmlDiff.AssertAreEqual ("<a onclick=\"MyClickMethod;\"></a>", html, "OnClientClick Failed");
116                 }
117
118                 [Test]
119                 [Category ("NunitWeb")]
120                 public void GetPostBackOptions ()
121                 {
122                         WebTest t = new WebTest ("NoEventValidation.aspx");
123                         t.Invoker = PageInvoker.CreateOnLoad (GetPostBackOptions_Load);
124                         t.Run ();
125                 }
126
127                 public static void GetPostBackOptions_Load (Page p)
128                 {
129                         Poker b = new Poker ();
130                         p.Controls.Add (b);
131                         b.PostBackUrl = "~/MyPostBackUrl.aspx";
132                         b.Text = "MyText";
133                         PostBackOptions opt = b.GetPostBackOptions ();
134                         Assert.IsNotNull (opt, "PostBackOptions not created");
135                         Assert.AreEqual ("MyPostBackUrl.aspx", opt.ActionUrl, "ActionUrl");
136                         Assert.AreEqual (b, opt.TargetControl, "TargetControl");
137                 }
138                 
139
140                 [Test]
141                 [Category("NunitWeb")]
142                 public void PostBackUrl ()
143                 {
144                         WebTest t = new WebTest ("NoEventValidation.aspx");
145                         t.Invoker = PageInvoker.CreateOnLoad (PostBackUrl_Load);
146                         t.Run ();
147                 }
148
149                 public static void PostBackUrl_Load (Page p)
150                 {
151                         Poker b = new Poker ();
152                         p.Controls.Add (b);
153                         b.PostBackUrl = "~/MyPostBackUrl.aspx";
154                         string html = b.Render ();
155                         if (html.IndexOf ("MyPostBackUrl.aspx") == -1)
156                                 Assert.Fail ("PostBackUrl not created");
157                 }
158
159                 [Test]
160                 [Category ("NunitWeb")]
161                 public void ValidationGroup ()
162                 {
163                         WebTest t = new WebTest ("NoEventValidation.aspx");
164                         t.Invoker = PageInvoker.CreateOnLoad (ValidationGroup_Load);
165                         string html = HtmlDiff.GetControlFromPageHtml (t.Run ());
166                         if (html.IndexOf ("href") == -1)
167                                 Assert.Fail ("Link button not created");
168                         if (html.IndexOf ("MyValidationGroup") == -1)
169                                 Assert.Fail ("Validation group not set: " + html);
170                 }
171
172                 public static void ValidationGroup_Load (Page p)
173                 {
174                         Poker b = new Poker ();
175                         b.ValidationGroup = "MyValidationGroup";
176                         TextBox tb = new TextBox ();
177                         tb.ID = "tb";
178                         tb.ValidationGroup = "MyValidationGroup";
179                         RequiredFieldValidator v = new RequiredFieldValidator ();
180                         v.ControlToValidate = "tb";
181                         v.ValidationGroup = "MyValidationGroup";
182                         p.Controls.Add (tb);
183                         p.Controls.Add (v);
184                         p.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
185                         p.Controls.Add (b);
186                         p.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
187                 }
188
189                 [Test]
190                 [Category ("NunitWeb")]
191                 public void RaisePostBackEvent ()
192                 {
193                         WebTest t = new WebTest ("NoEventValidation.aspx");
194                         t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
195                         t.Run ();
196                         ArrayList eventlist = t.UserData as ArrayList;
197                         if (eventlist == null)
198                                 Assert.Fail ("User data does not been created fail");
199
200                         Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
201                         Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
202                 }
203
204                 public static void RaisePostBackEvent_Load (Page p)
205                 {
206                         Poker b = new Poker ();
207                         p.Form.Controls.Add (b);
208                         b.Click += new EventHandler (b_Click);
209                         b.Command += new CommandEventHandler (b_Command);
210                         b.RaisePostBackEvent ("Click");
211                 }
212
213                 static void b_Command (object sender, CommandEventArgs e)
214                 {
215                         if (WebTest.CurrentTest.UserData == null) {
216                                 ArrayList list = new ArrayList ();
217                                 list.Add ("Command");
218                                 WebTest.CurrentTest.UserData = list;
219                         }
220                         else {
221                                 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
222                                 if (list == null)
223                                         throw new NullReferenceException ();
224                                 list.Add ("Command");
225                                 WebTest.CurrentTest.UserData = list;
226                         }
227                 }
228
229                 static void b_Click (object sender, EventArgs e)
230                 {
231                         if (WebTest.CurrentTest.UserData == null) {
232                                 ArrayList list = new ArrayList ();
233                                 list.Add ("Click");
234                                 WebTest.CurrentTest.UserData = list;
235                         }
236                         else {
237                                 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
238                                 if (list == null)
239                                         throw new NullReferenceException ();
240                                 list.Add ("Click");
241                                 WebTest.CurrentTest.UserData = list;
242                         }
243                 }
244 #endif
245                 [Test]
246                 public void ViewState ()
247                 {
248                         Poker p = new Poker ();
249                         p.TrackState ();
250
251                         Assert.AreEqual (p.Text, "", "A1");
252                         p.Text = "Hello";
253                         Assert.AreEqual (p.Text, "Hello", "A2");
254
255                         object state = p.SaveState ();
256
257                         Poker copy = new Poker ();
258                         copy.TrackState ();
259                         copy.LoadState (state);
260                         Assert.AreEqual (copy.Text, "Hello", "A3");
261                 }
262
263                 [Test]
264                 public void Render ()
265                 {
266                         Poker l = new Poker ();
267                         l.Text = "Hello";
268                         Assert.AreEqual ("<a>Hello</a>", l.Render (), "R1");
269                 }
270
271                 Poker MakeNested ()
272                 {
273                         Poker p = new Poker ();
274                         LinkButton ll = new LinkButton ();
275                         ll.Text = ", World";
276                         p.AddParsedSubObject (new LiteralControl ("Hello"));
277                         p.AddParsedSubObject (ll);
278                         return p;
279                 }
280                 
281                 
282                 [Test]
283                 public void ChildControl ()
284                 {
285                         Poker l = MakeNested ();
286                         Assert.AreEqual ("<a>Hello<a>, World</a></a>", l.Render ());
287                         Assert.AreEqual ("", l.Text);
288                         l.Text = "Hello";
289                         Assert.AreEqual ("<a>Hello</a>", l.Render ());
290                         Assert.AreEqual ("Hello", l.Text);
291                         Assert.IsFalse (l.HasControls ());
292                 }
293
294                 [Test]
295                 public void ChildControlViewstate ()
296                 {
297                         Poker l = MakeNested ();
298                         l.TrackState ();
299                         l.Text = "Hello";
300
301                         object o = l.SaveState ();
302                         l = MakeNested ();
303                         l.TrackState ();
304                         l.LoadState (o);
305                         
306                         Assert.AreEqual ("<a>Hello</a>", l.Render ());
307                         Assert.AreEqual ("Hello", l.Text);
308                         Assert.IsFalse (l.HasControls ());
309                 }
310
311                 class BubbleNet : Control {
312                         public EventHandler Bubble;
313                         protected override bool OnBubbleEvent (object s, EventArgs e)
314                         {
315                                 if (Bubble != null)
316                                         Bubble (s, e);
317                                 return false;
318                         }       
319                 }
320
321                 //
322                 // I (heart) anonymous methods
323                 //
324                 bool got_command = false;
325                 bool got_click = false;
326                 bool got_bubble = false;
327
328                 public void Event_TestEvents_Click(object sender, EventArgs e) {
329                         Assert.IsFalse (got_click, "#1");
330                         Assert.IsFalse (got_command, "#2");
331                         Assert.IsFalse (got_bubble, "#3");
332                         got_click = true;
333                 }
334                 public void Event_TestEvents_Command(object sender, CommandEventArgs e) {
335                         Assert.IsTrue (got_click, "#4");
336                         Assert.IsFalse (got_command, "#5");
337                         Assert.IsFalse (got_bubble, "#6");
338                         Assert.AreEqual ("N", e.CommandName, "#7");
339                         Assert.AreEqual ("A", e.CommandArgument, "#8");
340                         got_command = true;
341                 }
342                 public void Event_TestEvents_Bubble(object sender, EventArgs e) {
343                         Assert.IsTrue (got_click, "#9");
344                         Assert.IsTrue (got_command, "#10");
345                         Assert.IsFalse (got_bubble, "#11");
346                         Assert.AreEqual ("N", ((CommandEventArgs) e).CommandName, "#12");
347                         Assert.AreEqual ("A", ((CommandEventArgs) e).CommandArgument, "#13");
348                         got_bubble = true;
349                 }
350                 [Test]
351                 public void TestEvents ()
352                 {
353                         BubbleNet p = new BubbleNet ();
354                         LinkButton l = new LinkButton ();
355                         l.CommandName = "N";
356                         l.CommandArgument = "A";
357                         l.CausesValidation = false; // avoid an NRE on msft
358                         p.Controls.Add (l);
359                         
360                         got_command = false;
361                         got_click = false;
362                         got_bubble = false;
363
364                         l.Click += new EventHandler(Event_TestEvents_Click);
365
366                         l.Command += new CommandEventHandler(Event_TestEvents_Command);
367
368                         p.Bubble += new EventHandler(Event_TestEvents_Bubble);
369                         
370                         ((IPostBackEventHandler) l).RaisePostBackEvent ("");
371                         Assert.IsTrue (got_click, "#14");
372                         Assert.IsTrue (got_command, "#15");
373                         Assert.IsTrue (got_bubble, "#16");
374                 }
375
376
377
378 #if NET_2_0
379                 [Test]
380                 public void TestValidationGroup ()
381                 {
382                         Poker p = new Poker ();
383                         p.TrackState ();
384
385                         Assert.AreEqual (p.ValidationGroup, "", "V1");
386                         p.ValidationGroup = "VG1";
387                         Assert.AreEqual (p.ValidationGroup, "VG1", "V2");
388
389                         object state = p.SaveState ();
390
391                         Poker copy = new Poker ();
392                         copy.TrackState ();
393                         copy.LoadState (state);
394                         Assert.AreEqual (copy.ValidationGroup, "VG1", "A3");
395                 }
396
397                 [TestFixtureTearDown]
398                 public void TearDown ()
399                 {
400                         WebTest.Unload ();
401                 }
402 #endif
403         }
404 }
405
406