2 // HtmlInputRadioButtonTest.cs
3 // - Unit tests for System.Web.UI.HtmlControls.HtmlInputRadioButton
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Specialized;
34 using System.Web.UI.HtmlControls;
36 using NUnit.Framework;
38 namespace MonoTests.System.Web.UI.HtmlControls {
40 public class TestHtmlInputRadioButton : HtmlInputRadioButton {
42 public string RenderAttributes ()
44 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
45 base.RenderAttributes (writer);
46 return writer.InnerWriter.ToString ();
49 public bool LoadPost (string key, NameValueCollection nvc)
51 return base.LoadPostData (key, nvc);
56 base.RaisePostDataChangedEvent ();
62 public class HtmlInputRadioButtonTest {
64 private const int defaultAttributesCount = 1;
67 public void DefaultProperties ()
69 HtmlInputRadioButton rb = new HtmlInputRadioButton ();
70 Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count");
72 Assert.IsFalse (rb.Checked, "Checked");
73 Assert.AreEqual (String.Empty, rb.Name, "Name");
74 Assert.IsNull (rb.Value, "Value");
76 Assert.AreEqual ("input", rb.TagName, "TagName");
77 Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count-2");
81 public void NullProperties ()
83 HtmlInputRadioButton rb = new HtmlInputRadioButton ();
85 Assert.AreEqual (String.Empty, rb.Name, "Name");
87 Assert.IsNull (rb.Value, "Value");
89 Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count");
93 public void CleanProperties ()
95 HtmlInputRadioButton rb = new HtmlInputRadioButton ();
97 Assert.IsTrue (rb.Checked, "Checked");
99 Assert.AreEqual ("name", rb.Name, "Name");
101 Assert.AreEqual ("value", rb.Value, "Value");
102 Assert.AreEqual (defaultAttributesCount + 3, rb.Attributes.Count, "1");
105 Assert.IsFalse (rb.Checked, "-Checked");
107 Assert.AreEqual (String.Empty, rb.Name, "-Name");
109 Assert.IsNull (rb.Value, "-Value");
110 Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "0");
114 public void Value_Existing ()
116 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
118 Assert.AreEqual ("a", rb.Value, "Value before");
120 Assert.AreEqual ("id1", rb.ID, "ID");
121 Assert.AreEqual ("a", rb.Value, "Value after");
125 public void Value_Resetting ()
127 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
129 Assert.AreEqual ("id1", rb.Value, "Value before");
131 Assert.AreEqual ("id1", rb.ID, "ID");
132 Assert.AreEqual ("a", rb.Value, "Value after");
136 public void Value_ResetNull ()
138 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
142 Assert.AreEqual ("id1", rb.ID, "ID");
143 Assert.AreEqual ("id1", rb.Value, "Value");
147 // note: this behaviour isn't present in HtmlInputControl
148 public void IDversusValue ()
150 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
151 Assert.IsNull (rb.Value, "Value before");
153 Assert.AreEqual ("id1", rb.ID, "ID");
154 Assert.AreEqual ("id1", rb.Value, "Value after");
158 [Ignore ("throws NullReferenceException")]
159 public void RenderAttributes ()
161 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
165 rb.RenderAttributes ();
168 private bool serverChange;
169 private void ServerChange (object sender, EventArgs e)
175 public void IPostBackDataHandler_RaisePostBackEvent ()
177 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
178 rb.ServerChange += new EventHandler (ServerChange);
179 IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
180 serverChange = false;
181 pbdh.RaisePostDataChangedEvent ();
182 Assert.IsTrue (serverChange, "ServerChange");
186 [ExpectedException (typeof (NullReferenceException))]
187 public void IPostBackDataHandler_LoadPostData_NullCollection ()
189 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
190 IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
191 pbdh.LoadPostData ("id1", null);
195 [Category ("NotDotNet")] // MS throws a NullReferenceException here
196 public void IPostBackDataHandler_LoadPostData_IdNull ()
198 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
200 IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
201 NameValueCollection nvc = new NameValueCollection ();
202 nvc.Add ("id1", "mono");
203 Assert.IsFalse (pbdh.LoadPostData (null, nvc), "LoadPostData");
204 Assert.AreEqual ("id1", rb.Value, "Value");
208 public void IPostBackDataHandler_LoadPostData_WrongId ()
210 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
212 IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
213 NameValueCollection nvc = new NameValueCollection ();
214 nvc.Add ("id1", "mono");
215 Assert.IsFalse (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
216 Assert.AreEqual ("id1", rb.Value, "Value");
220 public void IPostBackDataHandler_LoadPostData ()
222 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
224 IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
225 NameValueCollection nvc = new NameValueCollection ();
226 nvc.Add ("id1", "id1");
227 // we didn't change the state of the control
228 Assert.IsFalse (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
229 Assert.AreEqual ("id1", rb.Value, "Value");
233 public void RenderValue1 ()
235 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
237 string attrs = rb.RenderAttributes ();
238 Assert.IsTrue (attrs.IndexOf ("value=\"id\"") >= 0);
240 attrs = rb.RenderAttributes ();
241 Assert.IsTrue (attrs.IndexOf ("value=\"hola\"") >= 0);
246 public void RaisePostBackEvent ()
248 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
249 rb.ServerChange += new EventHandler (ServerChange);
250 serverChange = false;
252 Assert.IsTrue (serverChange, "ServerClick");
256 [ExpectedException (typeof (NullReferenceException))]
257 public void LoadPostData_NullCollection ()
259 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
260 rb.LoadPost ("id1", null);
264 [Category ("NotDotNet")] // MS throws a NullReferenceException here
265 public void LoadPostData_IdNull ()
267 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
269 NameValueCollection nvc = new NameValueCollection ();
270 nvc.Add ("id1", "mono");
271 Assert.IsFalse (rb.LoadPost (null, nvc), "LoadPostData");
272 Assert.AreEqual ("id1", rb.Value, "Value");
276 public void LoadPostData_WrongId ()
278 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
280 NameValueCollection nvc = new NameValueCollection ();
281 nvc.Add ("id1", "mono");
282 Assert.IsFalse (rb.LoadPost ("id2", nvc), "LoadPostData");
283 Assert.AreEqual ("id1", rb.Value, "Value");
287 public void LoadPostData ()
289 TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
291 NameValueCollection nvc = new NameValueCollection ();
292 nvc.Add ("id1", "mono");
293 Assert.IsFalse (rb.LoadPost ("id1", nvc), "LoadPostData");
294 Assert.AreEqual ("id1", rb.Value, "Value");