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