d73dcd4110a44e1effec3ce62b5569494860f419
[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                         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>";
195                         HtmlDiff.AssertAreEqual (OriginControlHtml, RenderedControlHtml, "Render");
196                 }
197
198                 public static void Render_Load (Page p)
199                 {
200                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
201                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
202                         TestRadioButtonList c = new TestRadioButtonList ();
203                         p.Form.Controls.Add (lcb);
204                         p.Form.Controls.Add (c);
205                         p.Form.Controls.Add (lce);
206                         c.Items.Add (new ListItem ("text2", "value1"));
207                 }
208 #else
209                 [Test]
210                 public void Render ()
211                 {
212                         TestRadioButtonList c = new TestRadioButtonList ();
213
214                         c.Items.Add (new ListItem ("text2", "value1"));
215
216                         string s = c.Render ();
217
218                         Assert.IsTrue (s.ToLower ().IndexOf (" type=\"radio\"") !=  -1, "type");
219                         Assert.IsTrue (s.ToLower ().IndexOf ("value1") !=  -1, "value");
220                         Assert.IsTrue (s.ToLower ().IndexOf ("text2") !=  -1, "text");
221                 }
222 #endif
223
224                 // Exceptions
225                 [Test]
226                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
227                 public void RepeatColumnsException ()
228                 {
229                         TestRadioButtonList r = new TestRadioButtonList ();
230                         r.RepeatColumns = -1;
231                 }
232
233                 [Test]
234                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
235                 public void RepeatDirectionException ()
236                 {
237                         TestRadioButtonList r = new TestRadioButtonList ();
238                         r.RepeatDirection = (RepeatDirection) 4;
239                 }
240
241
242                 [Test]
243                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
244                 public void RepeatLayoutException ()
245                 {
246                         TestRadioButtonList r = new TestRadioButtonList ();
247                         r.RepeatLayout = (RepeatLayout) 3;
248                 }
249
250                 bool event_called;
251                 void OnSelected (object sender, EventArgs args)
252                 {
253                         event_called = true;
254                 }
255
256                 [Test]
257                 public void LoadAndRaise1 ()
258                 {
259                         RadioButtonList rbl = new RadioButtonList ();
260                         rbl.Items.Add (new ListItem ("Uno", "1"));
261                         rbl.Items.Add (new ListItem ("Dos", "2"));
262                         rbl.Items.Add (new ListItem ("Tres", "3"));
263                         rbl.SelectedIndex = 2;
264                         NameValueCollection nvc = new NameValueCollection ();
265                         nvc ["XXX"] = "3";
266
267                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
268 #if NET_2_0
269                         Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
270 #else
271                         Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
272 #endif
273                         rbl.SelectedIndexChanged += new EventHandler (OnSelected);
274                         event_called = false;
275                         handler.RaisePostDataChangedEvent ();
276 #if NET_2_0
277                         Assert.IsTrue (event_called, "#02");
278 #else
279                         // Not called. Value is the same as the selected previously
280                         Assert.IsFalse (event_called, "#02");
281 #endif
282                         Assert.AreEqual ("3", rbl.SelectedValue, "#03");
283                 }
284
285                 [Test]
286                 public void LoadAndRaise2 ()
287                 {
288                         RadioButtonList rbl = new RadioButtonList ();
289                         rbl.Items.Add (new ListItem ("Uno", "1"));
290                         rbl.Items.Add (new ListItem ("Dos", "2"));
291                         rbl.Items.Add (new ListItem ("Tres", "3"));
292                         rbl.SelectedIndex = 2;
293                         NameValueCollection nvc = new NameValueCollection ();
294                         nvc ["XXX"] = "2";
295
296                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
297                         Assert.AreEqual (true, handler.LoadPostData ("XXX", nvc), "#01");
298                         rbl.SelectedIndexChanged += new EventHandler (OnSelected);
299                         event_called = false;
300                         handler.RaisePostDataChangedEvent ();
301                         Assert.AreEqual (true, event_called, "#02");
302                         Assert.AreEqual ("2", rbl.SelectedValue, "#03");
303                 }
304
305                 [Test]
306                 public void LoadAndRaise3 ()
307                 {
308                         RadioButtonList rbl = new RadioButtonList ();
309                         rbl.Items.Add (new ListItem ("Uno", "1"));
310                         rbl.Items.Add (new ListItem ("Dos", "2"));
311                         rbl.Items.Add (new ListItem ("Tres", "3"));
312                         rbl.SelectedIndex = 2;
313                         NameValueCollection nvc = new NameValueCollection ();
314                         nvc ["XXX"] = "blah";
315
316                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
317                         Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
318                 }
319
320                 
321 #if NET_2_0
322                 [Test]
323                 [ExpectedException(typeof(HttpException))]
324                 public void VerifyMultiSelectTest()
325                 {
326                     PokerRadioButtonList list = new PokerRadioButtonList();
327                     list.VerifyMultiSelect();
328                 }
329
330                 [Test]
331                 public void Defaults ()
332                 {
333                         PokerRadioButtonList r = new PokerRadioButtonList ();
334                         Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount");
335                         Assert.AreEqual (false, r.HasFooter, "HasFooter");
336                         Assert.AreEqual (false, r.HasHeader, "HasHeader");
337                         Assert.AreEqual (false, r.HasSeparators, "HasSeparators");
338                 }
339                 
340                 [Test]
341                 [Category ("NunitWeb")]
342                 public void GetItemStyle ()
343                 {
344                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
345                         string html = t.Run ();
346                         string ctrl = HtmlDiff.GetControlFromPageHtml (html);
347                         if (ctrl == string.Empty)
348                                 Assert.Fail ("RadioButtonList not created fail");
349                         Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
350                         if (ctrl.IndexOf ("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
351                                 Assert.Fail ("RadioButtonList style not rendered");
352                 }
353
354                 public static void GetItemStyle_Load (Page p)
355                 {
356                         PokerRadioButtonList rbl = new PokerRadioButtonList ();
357                         rbl.Items.Add (new ListItem ("Uno", "1"));
358                         rbl.Items.Add (new ListItem ("Dos", "2"));
359                         rbl.Items.Add (new ListItem ("Tres", "3"));
360                         p.Form.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
361                         p.Form.Controls.Add (rbl);
362                         p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
363                 }
364
365                 [Test]
366                 public void  RaisePostDataChangedEvent ()
367                 {
368                         PokerRadioButtonList r = new PokerRadioButtonList ();
369                         r.SelectedIndexChanged += new EventHandler (r_SelectedIndexChanged);
370                         Assert.AreEqual (false, eventSelectedIndexChanged, "Before");
371                         r.DoRaisePostDataChangedEvent ();
372                         Assert.AreEqual (true, eventSelectedIndexChanged, "After");
373                 }
374
375                 bool eventSelectedIndexChanged;
376                 void r_SelectedIndexChanged (object sender, EventArgs e)
377                 {
378                         eventSelectedIndexChanged = true;
379                 }
380
381                 [Test]
382                 [Category ("NunitWeb")]
383                 public void RaisePostDataChangedEvent_PostBack ()
384                 {
385                         WebTest t = new WebTest (PageInvoker.CreateOnInit (RaisePostDataChangedEvent_Init));
386                         string html = t.Run ();
387                         FormRequest fr = new FormRequest (t.Response, "form1");
388                         fr.Controls.Add ("__EVENTTARGET");
389                         fr.Controls.Add ("__EVENTARGUMENT");
390                         fr.Controls.Add ("RadioButtonList1");
391
392                         fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
393                         fr.Controls["__EVENTARGUMENT"].Value = "";
394                         fr.Controls["RadioButtonList1"].Value = "test";
395                         t.Request = fr;
396                         t.Run ();
397                         if (t.UserData == null)
398                                 Assert.Fail ("RaisePostDataChangedEvent Failed#1");
399                         Assert.AreEqual ("SelectedIndexChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
400                 }
401
402                 public static void RaisePostDataChangedEvent_Init (Page p)
403                 {
404                         TestRadioButtonList r = new TestRadioButtonList ();
405                         r.ID = "RadioButtonList1";
406                         r.Items.Add (new ListItem ("test", "test"));
407                         r.SelectedIndexChanged += new EventHandler (event_SelectedIndexChanged);
408                         p.Form.Controls.Add (r);
409                 }
410
411                 public static void event_SelectedIndexChanged (object sender, EventArgs e)
412                 {
413                         WebTest.CurrentTest.UserData = "SelectedIndexChanged";  
414                 }
415
416                 #region help_class
417                 class Poker : RadioButtonList
418                 {
419                         protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
420                         {
421                                 if (WebTest.CurrentTest.UserData == null) {
422                                         ArrayList list = new ArrayList ();
423                                         list.Add ("LoadPostData");
424                                         WebTest.CurrentTest.UserData = list;
425                                 }
426                                 else {
427                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
428                                         if (list == null)
429                                                 throw new NullReferenceException ();
430                                         list.Add ("LoadPostData");
431                                         WebTest.CurrentTest.UserData = list;
432                                 }
433                                 return base.LoadPostData (postDataKey, postCollection);
434                         }
435                         
436                         protected override void OnLoad (EventArgs e)
437                         {
438                                 if (WebTest.CurrentTest.UserData == null) {
439                                         ArrayList list = new ArrayList ();
440                                         list.Add ("ControlLoad");
441                                         WebTest.CurrentTest.UserData = list;
442                                 }
443                                 else {
444                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
445                                         if (list == null)
446                                                 throw new NullReferenceException ();
447                                         list.Add ("ControlLoad");
448                                         WebTest.CurrentTest.UserData = list;
449                                 }
450                                 base.OnLoad (e);
451                         }
452                 }
453                 #endregion
454
455                 [Test]
456                 [Category ("NunitWeb")]
457                 public void LoadPostData ()  //Just flow and not implementation detail
458                 {
459                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
460                         string html = t.Run ();
461                         FormRequest fr = new FormRequest (t.Response, "form1");
462                         fr.Controls.Add ("__EVENTTARGET");
463                         fr.Controls.Add ("__EVENTARGUMENT");
464                         fr.Controls.Add ("RadioButtonList1");
465
466                         fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
467                         fr.Controls["__EVENTARGUMENT"].Value = "";
468                         fr.Controls["RadioButtonList1"].Value = "test";
469                         t.Request = fr;
470                         t.Run ();
471
472                         ArrayList eventlist = t.UserData as ArrayList;
473                         if (eventlist == null)
474                                 Assert.Fail ("User data does not been created fail");
475                         Assert.AreEqual ("ControlLoad", eventlist[0], "Live Cycle Flow #1");
476                         Assert.AreEqual ("PageLoad", eventlist[1], "Live Cycle Flow #2");
477                         Assert.AreEqual ("ControlLoad", eventlist[2], "Live Cycle Flow #3");
478                         Assert.AreEqual ("LoadPostData", eventlist[3], "Live Cycle Flow #4");
479
480                 }
481
482                 public static void LoadPostData_Load (Page p)
483                 {
484                         Poker b = new Poker ();
485                         b.ID = "RadioButtonList1";
486                         b.Items.Add (new ListItem ("test", "test"));
487                         p.Form.Controls.Add (b);
488                         if (p.IsPostBack) {
489                                 if (WebTest.CurrentTest.UserData == null) {
490                                         ArrayList list = new ArrayList ();
491                                         list.Add ("PageLoad");
492                                         WebTest.CurrentTest.UserData = list;
493                                 }
494                                 else {
495                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
496                                         if (list == null)
497                                                 throw new NullReferenceException ();
498                                         list.Add ("PageLoad");
499                                         WebTest.CurrentTest.UserData = list;
500                                 }
501                         }
502                 }
503
504                 [Test]
505                 public void RepeatedItemCount ()
506                 {
507                         PokerRadioButtonList r = new PokerRadioButtonList ();
508                         Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount#1");
509                         r.Items.Add (new ListItem ("Uno", "1"));
510                         Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#2");
511                         r.Items.Add (new ListItem ("Dos", "2"));
512                         Assert.AreEqual (2, r.RepeatedItemCount, "RepeatedItemCount#3");
513                         r.Items.Remove (r.Items[1]);
514                         Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#4");
515                 }
516
517                 [TestFixtureTearDown]
518                 public void TearDown ()
519                 {
520                         WebTest.Unload ();
521                 }
522 #endif
523         }
524 }
525