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);
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");
198 public static void Render_Load (Page p)
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"));
210 public void Render ()
212 TestRadioButtonList c = new TestRadioButtonList ();
214 c.Items.Add (new ListItem ("text2", "value1"));
216 string s = c.Render ();
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");
226 [ExpectedException (typeof (ArgumentOutOfRangeException))]
227 public void RepeatColumnsException ()
229 TestRadioButtonList r = new TestRadioButtonList ();
230 r.RepeatColumns = -1;
234 [ExpectedException (typeof (ArgumentOutOfRangeException))]
235 public void RepeatDirectionException ()
237 TestRadioButtonList r = new TestRadioButtonList ();
238 r.RepeatDirection = (RepeatDirection) 4;
243 [ExpectedException (typeof (ArgumentOutOfRangeException))]
244 public void RepeatLayoutException ()
246 TestRadioButtonList r = new TestRadioButtonList ();
247 r.RepeatLayout = (RepeatLayout) 3;
251 void OnSelected (object sender, EventArgs args)
257 public void LoadAndRaise1 ()
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 ();
267 IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
269 Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
271 Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
273 rbl.SelectedIndexChanged += new EventHandler (OnSelected);
274 event_called = false;
275 handler.RaisePostDataChangedEvent ();
277 Assert.IsTrue (event_called, "#02");
279 // Not called. Value is the same as the selected previously
280 Assert.IsFalse (event_called, "#02");
282 Assert.AreEqual ("3", rbl.SelectedValue, "#03");
286 public void LoadAndRaise2 ()
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 ();
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");
306 public void LoadAndRaise3 ()
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";
316 IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
317 Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
323 [ExpectedException(typeof(HttpException))]
324 public void VerifyMultiSelectTest()
326 PokerRadioButtonList list = new PokerRadioButtonList();
327 list.VerifyMultiSelect();
331 public void Defaults ()
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");
341 [Category ("NunitWeb")]
342 public void GetItemStyle ()
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");
354 public static void GetItemStyle_Load (Page p)
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));
366 public void RaisePostDataChangedEvent ()
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");
375 bool eventSelectedIndexChanged;
376 void r_SelectedIndexChanged (object sender, EventArgs e)
378 eventSelectedIndexChanged = true;
382 [Category ("NunitWeb")]
383 public void RaisePostDataChangedEvent_PostBack ()
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");
392 fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
393 fr.Controls["__EVENTARGUMENT"].Value = "";
394 fr.Controls["RadioButtonList1"].Value = "test";
397 if (t.UserData == null)
398 Assert.Fail ("RaisePostDataChangedEvent Failed#1");
399 Assert.AreEqual ("SelectedIndexChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
402 public static void RaisePostDataChangedEvent_Init (Page p)
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);
411 public static void event_SelectedIndexChanged (object sender, EventArgs e)
413 WebTest.CurrentTest.UserData = "SelectedIndexChanged";
417 class Poker : RadioButtonList
419 protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
421 if (WebTest.CurrentTest.UserData == null) {
422 ArrayList list = new ArrayList ();
423 list.Add ("LoadPostData");
424 WebTest.CurrentTest.UserData = list;
427 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
429 throw new NullReferenceException ();
430 list.Add ("LoadPostData");
431 WebTest.CurrentTest.UserData = list;
433 return base.LoadPostData (postDataKey, postCollection);
436 protected override void OnLoad (EventArgs e)
438 if (WebTest.CurrentTest.UserData == null) {
439 ArrayList list = new ArrayList ();
440 list.Add ("ControlLoad");
441 WebTest.CurrentTest.UserData = list;
444 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
446 throw new NullReferenceException ();
447 list.Add ("ControlLoad");
448 WebTest.CurrentTest.UserData = list;
456 [Category ("NunitWeb")]
457 public void LoadPostData () //Just flow and not implementation detail
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");
466 fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
467 fr.Controls["__EVENTARGUMENT"].Value = "";
468 fr.Controls["RadioButtonList1"].Value = "test";
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");
482 public static void LoadPostData_Load (Page p)
484 Poker b = new Poker ();
485 b.ID = "RadioButtonList1";
486 b.Items.Add (new ListItem ("test", "test"));
487 p.Form.Controls.Add (b);
489 if (WebTest.CurrentTest.UserData == null) {
490 ArrayList list = new ArrayList ();
491 list.Add ("PageLoad");
492 WebTest.CurrentTest.UserData = list;
495 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
497 throw new NullReferenceException ();
498 list.Add ("PageLoad");
499 WebTest.CurrentTest.UserData = list;
505 public void RepeatedItemCount ()
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");
517 [TestFixtureTearDown]
518 public void TearDown ()