More 4.0 test fixes
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / RadioButtonListTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.RadioButtonList.cs
3 //
4 // Authors:
5 //      Jordi Mas i Hernandez (jordi@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10
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 System.Web.UI.WebControls;
33 using NUnit.Framework;
34 using System;
35 using System.Collections.Specialized;
36 using System.IO;
37 using System.Web;
38 using System.Web.UI;
39 using System.Globalization;
40 using MonoTests.SystemWeb.Framework;
41 using MonoTests.stand_alone.WebHarness;
42 using System.Drawing;
43 using System.Collections;
44
45
46 namespace MonoTests.System.Web.UI.WebControls {
47
48         [TestFixture]
49         public class RadioButtonListTest
50         {
51                 #region help_classes
52                 public class TestRadioButtonList : RadioButtonList {
53                         public StateBag StateBag {
54                                 get { return base.ViewState; }
55                         }
56
57                         public string Render ()
58                         {
59                                 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
60                                 base.Render (writer);
61                                 return writer.InnerWriter.ToString ();
62                         }
63                 }
64
65 #if NET_2_0
66                 class PokerRadioButtonList : RadioButtonList
67                 {
68                         public StateBag StateBag
69                         {
70                                 get { return base.ViewState; }
71                         }
72
73                         public string Render ()
74                         {
75                                 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
76                                 base.Render (writer);
77                                 return writer.InnerWriter.ToString ();
78                         }
79
80                         protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
81                         {
82                                 Style s = new Style ();
83                                 s.BackColor = Color.Red;
84                                 s.BorderStyle = BorderStyle.Solid;
85                                 WebTest.CurrentTest.UserData = "GetItemStyle";
86                                 return s;
87                         }
88
89                         public new bool HasFooter
90                         {
91                                 get
92                                 {
93                                         return base.HasFooter;
94                                 }
95                         }
96
97                         public new bool HasHeader
98                         {
99                                 get
100                                 {
101                                         return base.HasHeader;
102                                 }
103                         }
104
105                         public new bool HasSeparators
106                         {
107                                 get
108                                 {
109                                         return base.HasSeparators;
110                                 }
111                         }
112
113                         public new int RepeatedItemCount
114                         {
115                                 get
116                                 {
117                                         return base.RepeatedItemCount;
118                                 }
119                         }
120
121                         protected override void RaisePostDataChangedEvent ()
122                         {
123                                 base.RaisePostDataChangedEvent ();
124                         }
125
126                         public void DoRaisePostDataChangedEvent ()
127                         {
128                                 base.RaisePostDataChangedEvent ();
129                         }
130
131                         public new virtual void VerifyMultiSelect()
132                         {
133                                 base.VerifyMultiSelect();
134                         }
135                 }
136 #endif
137                 #endregion
138
139                 [Test]
140                 public void RadioButtonList_Constructor ()
141                 {
142                         TestRadioButtonList r = new TestRadioButtonList ();
143                         Assert.AreEqual (-1, r.CellPadding, "A1");
144                         Assert.AreEqual (-1, r.CellSpacing, "A2");
145                         Assert.AreEqual (0, r.RepeatColumns, "A3");
146                         Assert.AreEqual (RepeatDirection.Vertical, r.RepeatDirection, "A4");
147                         Assert.AreEqual (RepeatLayout.Table, r.RepeatLayout, "A5");
148                         Assert.AreEqual (TextAlign.Right, r.TextAlign, "A6");
149                         Assert.AreEqual (false, ((IRepeatInfoUser)r).HasFooter, "A7");
150                         Assert.AreEqual (false, ((IRepeatInfoUser)r).HasHeader, "A8");
151                         Assert.AreEqual (false, ((IRepeatInfoUser)r).HasSeparators, "A9");
152                         Assert.AreEqual (0, ((IRepeatInfoUser)r).RepeatedItemCount, "A10");
153                 }
154
155                 [Test]
156                 public void CellPaddingProperties ()
157                 {
158                         TestRadioButtonList r = new TestRadioButtonList ();
159                         r.CellPadding = 5;
160                         Assert.AreEqual (5, r.CellPadding, "setting");
161
162                         string s = r.Render (); 
163 #if NET_2_0
164                         // FIXME: missing some info to start rendering ?
165                         Assert.AreEqual (String.Empty, s, "htmloutput");
166 #else
167                         Assert.IsTrue (s.ToLower ().IndexOf ("cellpadding=\"5\"") !=  -1, "htmloutput");
168 #endif
169                 }       
170
171                 [Test]
172                 public void CellSpacingProperties ()
173                 {
174                         TestRadioButtonList r = new TestRadioButtonList ();
175                         r.CellSpacing = 5;
176                         Assert.AreEqual (5, r.CellSpacing, "setting");
177
178                         string s = r.Render (); 
179 #if NET_2_0
180                         // FIXME: missing some info to start rendering ?
181                         Assert.AreEqual (String.Empty, s, "htmloutput");
182 #else
183                         Assert.IsTrue (s.ToLower ().IndexOf ("cellspacing=\"5\"") !=  -1, "htmloutput");
184 #endif
185                 }       
186
187 #if NET_2_0
188                 [Test]
189                 [Category ("NunitWeb")]
190                 public void Render ()
191                 {
192                         string RenderedPageHtml = new WebTest (PageInvoker.CreateOnLoad (Render_Load)).Run ();
193                         string RenderedControlHtml = HtmlDiff.GetControlFromPageHtml (RenderedPageHtml);
194 #if NET_4_0
195                         string OriginControlHtml = "<table id=\"ctl01\">\r\n\t<tr>\r\n\t\t<td><input id=\"ctl01_0\" type=\"radio\" name=\"ctl01\" value=\"value1\" /><label for=\"ctl01_0\">text2</label></td>\r\n\t</tr>\r\n</table>";
196 #else
197                         string OriginControlHtml = "<table id=\"ctl01\" border=\"0\">\r\n\t<tr>\r\n\t\t<td><input id=\"ctl01_0\" type=\"radio\" name=\"ctl01\" value=\"value1\" /><label for=\"ctl01_0\">text2</label></td>\r\n\t</tr>\r\n</table>";
198 #endif
199                         HtmlDiff.AssertAreEqual (OriginControlHtml, RenderedControlHtml, "Render");
200                 }
201
202                 public static void Render_Load (Page p)
203                 {
204                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
205                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
206                         TestRadioButtonList c = new TestRadioButtonList ();
207                         p.Form.Controls.Add (lcb);
208                         p.Form.Controls.Add (c);
209                         p.Form.Controls.Add (lce);
210                         c.Items.Add (new ListItem ("text2", "value1"));
211                 }
212 #else
213                 [Test]
214                 public void Render ()
215                 {
216                         TestRadioButtonList c = new TestRadioButtonList ();
217
218                         c.Items.Add (new ListItem ("text2", "value1"));
219
220                         string s = c.Render ();
221
222                         Assert.IsTrue (s.ToLower ().IndexOf (" type=\"radio\"") !=  -1, "type");
223                         Assert.IsTrue (s.ToLower ().IndexOf ("value1") !=  -1, "value");
224                         Assert.IsTrue (s.ToLower ().IndexOf ("text2") !=  -1, "text");
225                 }
226 #endif
227
228                 // Exceptions
229                 [Test]
230                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
231                 public void RepeatColumnsException ()
232                 {
233                         TestRadioButtonList r = new TestRadioButtonList ();
234                         r.RepeatColumns = -1;
235                 }
236
237                 [Test]
238                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
239                 public void RepeatDirectionException ()
240                 {
241                         TestRadioButtonList r = new TestRadioButtonList ();
242                         r.RepeatDirection = (RepeatDirection) 4;
243                 }
244
245
246                 [Test]
247                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
248                 public void RepeatLayoutException ()
249                 {
250                         TestRadioButtonList r = new TestRadioButtonList ();
251                         Array a = Enum.GetValues (typeof (RepeatLayout));
252                         int max = (int) a.GetValue (a.Length - 1) + 1;
253                         r.RepeatLayout = (RepeatLayout) max;
254                 }
255
256                 bool event_called;
257                 void OnSelected (object sender, EventArgs args)
258                 {
259                         event_called = true;
260                 }
261
262                 [Test]
263                 public void LoadAndRaise1 ()
264                 {
265                         RadioButtonList rbl = new RadioButtonList ();
266                         rbl.Items.Add (new ListItem ("Uno", "1"));
267                         rbl.Items.Add (new ListItem ("Dos", "2"));
268                         rbl.Items.Add (new ListItem ("Tres", "3"));
269                         rbl.SelectedIndex = 2;
270                         NameValueCollection nvc = new NameValueCollection ();
271                         nvc ["XXX"] = "3";
272
273                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
274 #if NET_2_0
275                         Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
276 #else
277                         Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
278 #endif
279                         rbl.SelectedIndexChanged += new EventHandler (OnSelected);
280                         event_called = false;
281                         handler.RaisePostDataChangedEvent ();
282 #if NET_2_0
283                         Assert.IsTrue (event_called, "#02");
284 #else
285                         // Not called. Value is the same as the selected previously
286                         Assert.IsFalse (event_called, "#02");
287 #endif
288                         Assert.AreEqual ("3", rbl.SelectedValue, "#03");
289                 }
290
291                 [Test]
292                 public void LoadAndRaise2 ()
293                 {
294                         RadioButtonList rbl = new RadioButtonList ();
295                         rbl.Items.Add (new ListItem ("Uno", "1"));
296                         rbl.Items.Add (new ListItem ("Dos", "2"));
297                         rbl.Items.Add (new ListItem ("Tres", "3"));
298                         rbl.SelectedIndex = 2;
299                         NameValueCollection nvc = new NameValueCollection ();
300                         nvc ["XXX"] = "2";
301
302                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
303                         Assert.AreEqual (true, handler.LoadPostData ("XXX", nvc), "#01");
304                         rbl.SelectedIndexChanged += new EventHandler (OnSelected);
305                         event_called = false;
306                         handler.RaisePostDataChangedEvent ();
307                         Assert.AreEqual (true, event_called, "#02");
308                         Assert.AreEqual ("2", rbl.SelectedValue, "#03");
309                 }
310
311                 [Test]
312                 public void LoadAndRaise3 ()
313                 {
314                         RadioButtonList rbl = new RadioButtonList ();
315                         rbl.Items.Add (new ListItem ("Uno", "1"));
316                         rbl.Items.Add (new ListItem ("Dos", "2"));
317                         rbl.Items.Add (new ListItem ("Tres", "3"));
318                         rbl.SelectedIndex = 2;
319                         NameValueCollection nvc = new NameValueCollection ();
320                         nvc ["XXX"] = "blah";
321
322                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
323                         Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
324                 }
325
326                 
327 #if NET_2_0
328                 [Test]
329                 [ExpectedException(typeof(HttpException))]
330                 public void VerifyMultiSelectTest()
331                 {
332                     PokerRadioButtonList list = new PokerRadioButtonList();
333                     list.VerifyMultiSelect();
334                 }
335
336                 [Test]
337                 public void Defaults ()
338                 {
339                         PokerRadioButtonList r = new PokerRadioButtonList ();
340                         Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount");
341                         Assert.AreEqual (false, r.HasFooter, "HasFooter");
342                         Assert.AreEqual (false, r.HasHeader, "HasHeader");
343                         Assert.AreEqual (false, r.HasSeparators, "HasSeparators");
344                 }
345                 
346                 [Test]
347                 [Category ("NunitWeb")]
348                 public void GetItemStyle ()
349                 {
350                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
351                         string html = t.Run ();
352                         string ctrl = HtmlDiff.GetControlFromPageHtml (html);
353                         if (ctrl == string.Empty)
354                                 Assert.Fail ("RadioButtonList not created fail");
355                         Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
356                         if (ctrl.IndexOf ("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
357                                 Assert.Fail ("RadioButtonList style not rendered");
358                 }
359
360                 public static void GetItemStyle_Load (Page p)
361                 {
362                         PokerRadioButtonList rbl = new PokerRadioButtonList ();
363                         rbl.Items.Add (new ListItem ("Uno", "1"));
364                         rbl.Items.Add (new ListItem ("Dos", "2"));
365                         rbl.Items.Add (new ListItem ("Tres", "3"));
366                         p.Form.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
367                         p.Form.Controls.Add (rbl);
368                         p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
369                 }
370
371                 [Test]
372                 public void  RaisePostDataChangedEvent ()
373                 {
374                         PokerRadioButtonList r = new PokerRadioButtonList ();
375                         r.SelectedIndexChanged += new EventHandler (r_SelectedIndexChanged);
376                         Assert.AreEqual (false, eventSelectedIndexChanged, "Before");
377                         r.DoRaisePostDataChangedEvent ();
378                         Assert.AreEqual (true, eventSelectedIndexChanged, "After");
379                 }
380
381                 bool eventSelectedIndexChanged;
382                 void r_SelectedIndexChanged (object sender, EventArgs e)
383                 {
384                         eventSelectedIndexChanged = true;
385                 }
386
387                 [Test]
388                 [Category ("NunitWeb")]
389                 public void RaisePostDataChangedEvent_PostBack ()
390                 {
391                         WebTest t = new WebTest (PageInvoker.CreateOnInit (RaisePostDataChangedEvent_Init));
392                         string html = t.Run ();
393                         FormRequest fr = new FormRequest (t.Response, "form1");
394                         fr.Controls.Add ("__EVENTTARGET");
395                         fr.Controls.Add ("__EVENTARGUMENT");
396                         fr.Controls.Add ("RadioButtonList1");
397
398                         fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
399                         fr.Controls["__EVENTARGUMENT"].Value = "";
400                         fr.Controls["RadioButtonList1"].Value = "test";
401                         t.Request = fr;
402                         t.Run ();
403                         if (t.UserData == null)
404                                 Assert.Fail ("RaisePostDataChangedEvent Failed#1");
405                         Assert.AreEqual ("SelectedIndexChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
406                 }
407
408                 public static void RaisePostDataChangedEvent_Init (Page p)
409                 {
410                         TestRadioButtonList r = new TestRadioButtonList ();
411                         r.ID = "RadioButtonList1";
412                         r.Items.Add (new ListItem ("test", "test"));
413                         r.SelectedIndexChanged += new EventHandler (event_SelectedIndexChanged);
414                         p.Form.Controls.Add (r);
415                 }
416
417                 public static void event_SelectedIndexChanged (object sender, EventArgs e)
418                 {
419                         WebTest.CurrentTest.UserData = "SelectedIndexChanged";  
420                 }
421
422                 #region help_class
423                 class Poker : RadioButtonList
424                 {
425                         protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
426                         {
427                                 if (WebTest.CurrentTest.UserData == null) {
428                                         ArrayList list = new ArrayList ();
429                                         list.Add ("LoadPostData");
430                                         WebTest.CurrentTest.UserData = list;
431                                 }
432                                 else {
433                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
434                                         if (list == null)
435                                                 throw new NullReferenceException ();
436                                         list.Add ("LoadPostData");
437                                         WebTest.CurrentTest.UserData = list;
438                                 }
439                                 return base.LoadPostData (postDataKey, postCollection);
440                         }
441                         
442                         protected override void OnLoad (EventArgs e)
443                         {
444                                 if (WebTest.CurrentTest.UserData == null) {
445                                         ArrayList list = new ArrayList ();
446                                         list.Add ("ControlLoad");
447                                         WebTest.CurrentTest.UserData = list;
448                                 }
449                                 else {
450                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
451                                         if (list == null)
452                                                 throw new NullReferenceException ();
453                                         list.Add ("ControlLoad");
454                                         WebTest.CurrentTest.UserData = list;
455                                 }
456                                 base.OnLoad (e);
457                         }
458                 }
459                 #endregion
460
461                 [Test]
462                 [Category ("NunitWeb")]
463                 public void LoadPostData ()  //Just flow and not implementation detail
464                 {
465                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
466                         string html = t.Run ();
467                         FormRequest fr = new FormRequest (t.Response, "form1");
468                         fr.Controls.Add ("__EVENTTARGET");
469                         fr.Controls.Add ("__EVENTARGUMENT");
470                         fr.Controls.Add ("RadioButtonList1");
471
472                         fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
473                         fr.Controls["__EVENTARGUMENT"].Value = "";
474                         fr.Controls["RadioButtonList1"].Value = "test";
475                         t.Request = fr;
476                         t.Run ();
477
478                         ArrayList eventlist = t.UserData as ArrayList;
479                         if (eventlist == null)
480                                 Assert.Fail ("User data does not been created fail");
481                         Assert.AreEqual ("ControlLoad", eventlist[0], "Live Cycle Flow #1");
482                         Assert.AreEqual ("PageLoad", eventlist[1], "Live Cycle Flow #2");
483                         Assert.AreEqual ("ControlLoad", eventlist[2], "Live Cycle Flow #3");
484                         Assert.AreEqual ("LoadPostData", eventlist[3], "Live Cycle Flow #4");
485
486                 }
487
488                 public static void LoadPostData_Load (Page p)
489                 {
490                         Poker b = new Poker ();
491                         b.ID = "RadioButtonList1";
492                         b.Items.Add (new ListItem ("test", "test"));
493                         p.Form.Controls.Add (b);
494                         if (p.IsPostBack) {
495                                 if (WebTest.CurrentTest.UserData == null) {
496                                         ArrayList list = new ArrayList ();
497                                         list.Add ("PageLoad");
498                                         WebTest.CurrentTest.UserData = list;
499                                 }
500                                 else {
501                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
502                                         if (list == null)
503                                                 throw new NullReferenceException ();
504                                         list.Add ("PageLoad");
505                                         WebTest.CurrentTest.UserData = list;
506                                 }
507                         }
508                 }
509
510                 [Test]
511                 public void RepeatedItemCount ()
512                 {
513                         PokerRadioButtonList r = new PokerRadioButtonList ();
514                         Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount#1");
515                         r.Items.Add (new ListItem ("Uno", "1"));
516                         Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#2");
517                         r.Items.Add (new ListItem ("Dos", "2"));
518                         Assert.AreEqual (2, r.RepeatedItemCount, "RepeatedItemCount#3");
519                         r.Items.Remove (r.Items[1]);
520                         Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#4");
521                 }
522
523                 [TestFixtureTearDown]
524                 public void TearDown ()
525                 {
526                         WebTest.Unload ();
527                 }
528 #endif
529         }
530 }
531