2005-09-23 Sebastien Pouliot <sebastien@ximian.com>
[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
41
42 namespace MonoTests.System.Web.UI.WebControls {
43
44         [TestFixture]
45         public class RadioButtonListTest {
46
47                 public class TestRadioButtonList : RadioButtonList {
48                         public StateBag StateBag {
49                                 get { return base.ViewState; }
50                         }
51
52                         public string Render ()
53                         {
54                                 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
55                                 base.Render (writer);
56                                 return writer.InnerWriter.ToString ();
57                         }
58                 }
59
60                 [Test]
61                 public void RadioButtonList_Constructor ()
62                 {
63                         TestRadioButtonList r = new TestRadioButtonList ();
64                         Assert.AreEqual (-1, r.CellPadding, "A1");
65                         Assert.AreEqual (-1, r.CellSpacing, "A2");
66                         Assert.AreEqual (0, r.RepeatColumns, "A3");
67                         Assert.AreEqual (RepeatDirection.Vertical, r.RepeatDirection, "A4");
68                         Assert.AreEqual (RepeatLayout.Table, r.RepeatLayout, "A5");
69                         Assert.AreEqual (TextAlign.Right, r.TextAlign, "A6");
70                         Assert.AreEqual (false, ((IRepeatInfoUser)r).HasFooter, "A7");
71                         Assert.AreEqual (false, ((IRepeatInfoUser)r).HasHeader, "A8");
72                         Assert.AreEqual (false, ((IRepeatInfoUser)r).HasSeparators, "A9");
73                         Assert.AreEqual (0, ((IRepeatInfoUser)r).RepeatedItemCount, "A10");
74                 }
75
76                 [Test]
77                 public void CellPaddingProperties ()
78                 {
79                         TestRadioButtonList r = new TestRadioButtonList ();
80                         r.CellPadding = 5;
81                         Assert.AreEqual (5, r.CellPadding, "setting");
82
83                         string s = r.Render (); 
84 #if NET_2_0
85                         // FIXME: missing some info to start rendering ?
86                         Assert.AreEqual (String.Empty, s, "htmloutput");
87 #else
88                         Assert.IsTrue (s.ToLower ().IndexOf ("cellpadding=\"5\"") !=  -1, "htmloutput");
89 #endif
90                 }       
91
92                 [Test]
93                 public void CellSpacingProperties ()
94                 {
95                         TestRadioButtonList r = new TestRadioButtonList ();
96                         r.CellSpacing = 5;
97                         Assert.AreEqual (5, r.CellSpacing, "setting");
98
99                         string s = r.Render (); 
100 #if NET_2_0
101                         // FIXME: missing some info to start rendering ?
102                         Assert.AreEqual (String.Empty, s, "htmloutput");
103 #else
104                         Assert.IsTrue (s.ToLower ().IndexOf ("cellspacing=\"5\"") !=  -1, "htmloutput");
105 #endif
106                 }       
107
108                 [Test]
109 #if NET_2_0
110                 // FIXME: missing some info to start rendering ?
111                 [ExpectedException (typeof (NullReferenceException))]
112 #endif
113                 public void Render ()
114                 {
115                         TestRadioButtonList c = new TestRadioButtonList ();
116
117                         c.Items.Add (new ListItem ("text2", "value1"));
118
119                         string s = c.Render ();
120
121                         Assert.IsTrue (s.ToLower ().IndexOf (" type=\"radio\"") !=  -1, "type");
122                         Assert.IsTrue (s.ToLower ().IndexOf ("value1") !=  -1, "value");
123                         Assert.IsTrue (s.ToLower ().IndexOf ("text2") !=  -1, "text");
124                 }
125
126                 // Exceptions
127                 [Test]
128                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
129                 public void RepeatColumnsException ()
130                 {
131                         TestRadioButtonList r = new TestRadioButtonList ();
132                         r.RepeatColumns = -1;
133                 }
134
135                 [Test]
136                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
137                 public void RepeatDirectionException ()
138                 {
139                         TestRadioButtonList r = new TestRadioButtonList ();
140                         r.RepeatDirection = (RepeatDirection) 4;
141                 }
142
143
144                 [Test]
145                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
146                 public void RepeatLayoutException ()
147                 {
148                         TestRadioButtonList r = new TestRadioButtonList ();
149                         r.RepeatLayout = (RepeatLayout) 3;
150                 }
151
152                 bool event_called;
153                 void OnSelected (object sender, EventArgs args)
154                 {
155                         event_called = true;
156                 }
157
158                 [Test]
159                 public void LoadAndRaise1 ()
160                 {
161                         RadioButtonList rbl = new RadioButtonList ();
162                         rbl.Items.Add (new ListItem ("Uno", "1"));
163                         rbl.Items.Add (new ListItem ("Dos", "2"));
164                         rbl.Items.Add (new ListItem ("Tres", "3"));
165                         rbl.SelectedIndex = 2;
166                         NameValueCollection nvc = new NameValueCollection ();
167                         nvc ["XXX"] = "3";
168
169                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
170 #if NET_2_0
171                         Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
172 #else
173                         Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
174 #endif
175                         rbl.SelectedIndexChanged += new EventHandler (OnSelected);
176                         event_called = false;
177                         handler.RaisePostDataChangedEvent ();
178 #if NET_2_0
179                         Assert.IsTrue (event_called, "#02");
180 #else
181                         // Not called. Value is the same as the selected previously
182                         Assert.IsFalse (event_called, "#02");
183 #endif
184                         Assert.AreEqual ("3", rbl.SelectedValue, "#03");
185                 }
186
187                 [Test]
188                 public void LoadAndRaise2 ()
189                 {
190                         RadioButtonList rbl = new RadioButtonList ();
191                         rbl.Items.Add (new ListItem ("Uno", "1"));
192                         rbl.Items.Add (new ListItem ("Dos", "2"));
193                         rbl.Items.Add (new ListItem ("Tres", "3"));
194                         rbl.SelectedIndex = 2;
195                         NameValueCollection nvc = new NameValueCollection ();
196                         nvc ["XXX"] = "2";
197
198                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
199                         Assert.AreEqual (true, handler.LoadPostData ("XXX", nvc), "#01");
200                         rbl.SelectedIndexChanged += new EventHandler (OnSelected);
201                         event_called = false;
202                         handler.RaisePostDataChangedEvent ();
203                         Assert.AreEqual (true, event_called, "#02");
204                         Assert.AreEqual ("2", rbl.SelectedValue, "#03");
205                 }
206
207                 [Test]
208                 public void LoadAndRaise3 ()
209                 {
210                         RadioButtonList rbl = new RadioButtonList ();
211                         rbl.Items.Add (new ListItem ("Uno", "1"));
212                         rbl.Items.Add (new ListItem ("Dos", "2"));
213                         rbl.Items.Add (new ListItem ("Tres", "3"));
214                         rbl.SelectedIndex = 2;
215                         NameValueCollection nvc = new NameValueCollection ();
216                         nvc ["XXX"] = "blah";
217
218                         IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
219                         Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
220                 }
221         }
222 }
223