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 ();
65 class PokerRadioButtonList : RadioButtonList
67 public StateBag StateBag
69 get { return base.ViewState; }
72 public string Render ()
74 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
76 return writer.InnerWriter.ToString ();
79 protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
81 Style s = new Style ();
82 s.BackColor = Color.Red;
83 s.BorderStyle = BorderStyle.Solid;
84 WebTest.CurrentTest.UserData = "GetItemStyle";
88 public new bool HasFooter
92 return base.HasFooter;
96 public new bool HasHeader
100 return base.HasHeader;
104 public new bool HasSeparators
108 return base.HasSeparators;
112 public new int RepeatedItemCount
116 return base.RepeatedItemCount;
120 protected override void RaisePostDataChangedEvent ()
122 base.RaisePostDataChangedEvent ();
125 public void DoRaisePostDataChangedEvent ()
127 base.RaisePostDataChangedEvent ();
130 public new virtual void VerifyMultiSelect()
132 base.VerifyMultiSelect();
138 public void RadioButtonList_Constructor ()
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");
154 public void CellPaddingProperties ()
156 TestRadioButtonList r = new TestRadioButtonList ();
158 Assert.AreEqual (5, r.CellPadding, "setting");
160 string s = r.Render ();
161 // FIXME: missing some info to start rendering ?
162 Assert.AreEqual (String.Empty, s, "htmloutput");
166 public void CellSpacingProperties ()
168 TestRadioButtonList r = new TestRadioButtonList ();
170 Assert.AreEqual (5, r.CellSpacing, "setting");
172 string s = r.Render ();
173 // FIXME: missing some info to start rendering ?
174 Assert.AreEqual (String.Empty, s, "htmloutput");
178 [Category ("NunitWeb")]
179 public void Render ()
181 string RenderedPageHtml = new WebTest (PageInvoker.CreateOnLoad (Render_Load)).Run ();
182 string RenderedControlHtml = HtmlDiff.GetControlFromPageHtml (RenderedPageHtml);
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>";
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>";
188 HtmlDiff.AssertAreEqual (OriginControlHtml, RenderedControlHtml, "Render");
191 public static void Render_Load (Page p)
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"));
204 [ExpectedException (typeof (ArgumentOutOfRangeException))]
205 public void RepeatColumnsException ()
207 TestRadioButtonList r = new TestRadioButtonList ();
208 r.RepeatColumns = -1;
212 [ExpectedException (typeof (ArgumentOutOfRangeException))]
213 public void RepeatDirectionException ()
215 TestRadioButtonList r = new TestRadioButtonList ();
216 r.RepeatDirection = (RepeatDirection) 4;
221 [ExpectedException (typeof (ArgumentOutOfRangeException))]
222 public void RepeatLayoutException ()
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;
231 void OnSelected (object sender, EventArgs args)
237 public void LoadAndRaise1 ()
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 ();
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");
257 public void LoadAndRaise2 ()
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;
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");
277 public void LoadAndRaise3 ()
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";
287 IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
288 Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
293 [ExpectedException(typeof(HttpException))]
294 public void VerifyMultiSelectTest()
296 PokerRadioButtonList list = new PokerRadioButtonList();
297 list.VerifyMultiSelect();
301 public void Defaults ()
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");
311 [Category ("NunitWeb")]
312 public void GetItemStyle ()
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");
324 public static void GetItemStyle_Load (Page p)
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));
336 public void RaisePostDataChangedEvent ()
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");
345 bool eventSelectedIndexChanged;
346 void r_SelectedIndexChanged (object sender, EventArgs e)
348 eventSelectedIndexChanged = true;
352 [Category ("NunitWeb")]
353 public void RaisePostDataChangedEvent_PostBack ()
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");
362 fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
363 fr.Controls["__EVENTARGUMENT"].Value = "";
364 fr.Controls["RadioButtonList1"].Value = "test";
367 if (t.UserData == null)
368 Assert.Fail ("RaisePostDataChangedEvent Failed#1");
369 Assert.AreEqual ("SelectedIndexChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
372 public static void RaisePostDataChangedEvent_Init (Page p)
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);
381 public static void event_SelectedIndexChanged (object sender, EventArgs e)
383 WebTest.CurrentTest.UserData = "SelectedIndexChanged";
387 class Poker : RadioButtonList
389 protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
391 if (WebTest.CurrentTest.UserData == null) {
392 ArrayList list = new ArrayList ();
393 list.Add ("LoadPostData");
394 WebTest.CurrentTest.UserData = list;
397 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
399 throw new NullReferenceException ();
400 list.Add ("LoadPostData");
401 WebTest.CurrentTest.UserData = list;
403 return base.LoadPostData (postDataKey, postCollection);
406 protected internal override void OnLoad (EventArgs e)
408 if (WebTest.CurrentTest.UserData == null) {
409 ArrayList list = new ArrayList ();
410 list.Add ("ControlLoad");
411 WebTest.CurrentTest.UserData = list;
414 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
416 throw new NullReferenceException ();
417 list.Add ("ControlLoad");
418 WebTest.CurrentTest.UserData = list;
426 [Category ("NunitWeb")]
427 public void LoadPostData () //Just flow and not implementation detail
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");
436 fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
437 fr.Controls["__EVENTARGUMENT"].Value = "";
438 fr.Controls["RadioButtonList1"].Value = "test";
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");
452 public static void LoadPostData_Load (Page p)
454 Poker b = new Poker ();
455 b.ID = "RadioButtonList1";
456 b.Items.Add (new ListItem ("test", "test"));
457 p.Form.Controls.Add (b);
459 if (WebTest.CurrentTest.UserData == null) {
460 ArrayList list = new ArrayList ();
461 list.Add ("PageLoad");
462 WebTest.CurrentTest.UserData = list;
465 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
467 throw new NullReferenceException ();
468 list.Add ("PageLoad");
469 WebTest.CurrentTest.UserData = list;
475 public void RepeatedItemCount ()
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");
487 [TestFixtureTearDown]
488 public void TearDown ()