2 // Tests for System.Web.UI.WebControls.RadioButtonList.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Web.UI.WebControls;
33 using NUnit.Framework;
35 using System.Collections.Specialized;
39 using System.Globalization;
40 using MonoTests.SystemWeb.Framework;
41 using MonoTests.stand_alone.WebHarness;
43 using System.Collections;
46 namespace MonoTests.System.Web.UI.WebControls {
49 public class RadioButtonListTest
52 public class TestRadioButtonList : RadioButtonList {
53 public StateBag StateBag {
54 get { return base.ViewState; }
57 public string Render ()
59 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
61 return writer.InnerWriter.ToString ();
66 class PokerRadioButtonList : RadioButtonList
68 public StateBag StateBag
70 get { return base.ViewState; }
73 public string Render ()
75 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
77 return writer.InnerWriter.ToString ();
80 protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
82 Style s = new Style ();
83 s.BackColor = Color.Red;
84 s.BorderStyle = BorderStyle.Solid;
85 WebTest.CurrentTest.UserData = "GetItemStyle";
89 public new bool HasFooter
93 return base.HasFooter;
97 public new bool HasHeader
101 return base.HasHeader;
105 public new bool HasSeparators
109 return base.HasSeparators;
113 public new int RepeatedItemCount
117 return base.RepeatedItemCount;
121 protected override void RaisePostDataChangedEvent ()
123 base.RaisePostDataChangedEvent ();
126 public void DoRaisePostDataChangedEvent ()
128 base.RaisePostDataChangedEvent ();
131 public new virtual void VerifyMultiSelect()
133 base.VerifyMultiSelect();
140 public void RadioButtonList_Constructor ()
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");
156 public void CellPaddingProperties ()
158 TestRadioButtonList r = new TestRadioButtonList ();
160 Assert.AreEqual (5, r.CellPadding, "setting");
162 string s = r.Render ();
164 // FIXME: missing some info to start rendering ?
165 Assert.AreEqual (String.Empty, s, "htmloutput");
167 Assert.IsTrue (s.ToLower ().IndexOf ("cellpadding=\"5\"") != -1, "htmloutput");
172 public void CellSpacingProperties ()
174 TestRadioButtonList r = new TestRadioButtonList ();
176 Assert.AreEqual (5, r.CellSpacing, "setting");
178 string s = r.Render ();
180 // FIXME: missing some info to start rendering ?
181 Assert.AreEqual (String.Empty, s, "htmloutput");
183 Assert.IsTrue (s.ToLower ().IndexOf ("cellspacing=\"5\"") != -1, "htmloutput");
189 [Category ("NunitWeb")]
190 public void Render ()
192 string RenderedPageHtml = new WebTest (PageInvoker.CreateOnLoad (Render_Load)).Run ();
193 string RenderedControlHtml = HtmlDiff.GetControlFromPageHtml (RenderedPageHtml);
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>";
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>";
199 HtmlDiff.AssertAreEqual (OriginControlHtml, RenderedControlHtml, "Render");
202 public static void Render_Load (Page p)
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"));
214 public void Render ()
216 TestRadioButtonList c = new TestRadioButtonList ();
218 c.Items.Add (new ListItem ("text2", "value1"));
220 string s = c.Render ();
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");
230 [ExpectedException (typeof (ArgumentOutOfRangeException))]
231 public void RepeatColumnsException ()
233 TestRadioButtonList r = new TestRadioButtonList ();
234 r.RepeatColumns = -1;
238 [ExpectedException (typeof (ArgumentOutOfRangeException))]
239 public void RepeatDirectionException ()
241 TestRadioButtonList r = new TestRadioButtonList ();
242 r.RepeatDirection = (RepeatDirection) 4;
247 [ExpectedException (typeof (ArgumentOutOfRangeException))]
248 public void RepeatLayoutException ()
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;
257 void OnSelected (object sender, EventArgs args)
263 public void LoadAndRaise1 ()
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 ();
273 IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
275 Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
277 Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
279 rbl.SelectedIndexChanged += new EventHandler (OnSelected);
280 event_called = false;
281 handler.RaisePostDataChangedEvent ();
283 Assert.IsTrue (event_called, "#02");
285 // Not called. Value is the same as the selected previously
286 Assert.IsFalse (event_called, "#02");
288 Assert.AreEqual ("3", rbl.SelectedValue, "#03");
292 public void LoadAndRaise2 ()
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 ();
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");
312 public void LoadAndRaise3 ()
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";
322 IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
323 Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
329 [ExpectedException(typeof(HttpException))]
330 public void VerifyMultiSelectTest()
332 PokerRadioButtonList list = new PokerRadioButtonList();
333 list.VerifyMultiSelect();
337 public void Defaults ()
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");
347 [Category ("NunitWeb")]
348 public void GetItemStyle ()
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");
360 public static void GetItemStyle_Load (Page p)
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));
372 public void RaisePostDataChangedEvent ()
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");
381 bool eventSelectedIndexChanged;
382 void r_SelectedIndexChanged (object sender, EventArgs e)
384 eventSelectedIndexChanged = true;
388 [Category ("NunitWeb")]
389 public void RaisePostDataChangedEvent_PostBack ()
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");
398 fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
399 fr.Controls["__EVENTARGUMENT"].Value = "";
400 fr.Controls["RadioButtonList1"].Value = "test";
403 if (t.UserData == null)
404 Assert.Fail ("RaisePostDataChangedEvent Failed#1");
405 Assert.AreEqual ("SelectedIndexChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
408 public static void RaisePostDataChangedEvent_Init (Page p)
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);
417 public static void event_SelectedIndexChanged (object sender, EventArgs e)
419 WebTest.CurrentTest.UserData = "SelectedIndexChanged";
423 class Poker : RadioButtonList
425 protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
427 if (WebTest.CurrentTest.UserData == null) {
428 ArrayList list = new ArrayList ();
429 list.Add ("LoadPostData");
430 WebTest.CurrentTest.UserData = list;
433 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
435 throw new NullReferenceException ();
436 list.Add ("LoadPostData");
437 WebTest.CurrentTest.UserData = list;
439 return base.LoadPostData (postDataKey, postCollection);
442 protected override void OnLoad (EventArgs e)
444 if (WebTest.CurrentTest.UserData == null) {
445 ArrayList list = new ArrayList ();
446 list.Add ("ControlLoad");
447 WebTest.CurrentTest.UserData = list;
450 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
452 throw new NullReferenceException ();
453 list.Add ("ControlLoad");
454 WebTest.CurrentTest.UserData = list;
462 [Category ("NunitWeb")]
463 public void LoadPostData () //Just flow and not implementation detail
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");
472 fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
473 fr.Controls["__EVENTARGUMENT"].Value = "";
474 fr.Controls["RadioButtonList1"].Value = "test";
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");
488 public static void LoadPostData_Load (Page p)
490 Poker b = new Poker ();
491 b.ID = "RadioButtonList1";
492 b.Items.Add (new ListItem ("test", "test"));
493 p.Form.Controls.Add (b);
495 if (WebTest.CurrentTest.UserData == null) {
496 ArrayList list = new ArrayList ();
497 list.Add ("PageLoad");
498 WebTest.CurrentTest.UserData = list;
501 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
503 throw new NullReferenceException ();
504 list.Add ("PageLoad");
505 WebTest.CurrentTest.UserData = list;
511 public void RepeatedItemCount ()
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");
523 [TestFixtureTearDown]
524 public void TearDown ()