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