* HtmlAnchor.cs:
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlInputRadioButtonTest.cs
1 //
2 // HtmlInputRadioButtonTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlInputRadioButton
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Collections.Specialized;
32 using System.IO;
33 using System.Web.UI;
34 using System.Web.UI.HtmlControls;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Web.UI.HtmlControls {
39
40         public class TestHtmlInputRadioButton : HtmlInputRadioButton {
41
42                 public string RenderAttributes ()
43                 {
44                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
45                         base.RenderAttributes (writer);
46                         return writer.InnerWriter.ToString ();
47                 }
48 #if NET_2_0
49                 public bool LoadPost (string key, NameValueCollection nvc)
50                 {
51                         return base.LoadPostData (key, nvc);
52                 }
53
54                 public void Raise ()
55                 {
56                         base.RaisePostDataChangedEvent ();
57                 }
58 #endif
59         }
60
61         [TestFixture]
62         public class HtmlInputRadioButtonTest {
63
64                 private const int defaultAttributesCount = 1;
65
66                 [Test]
67                 public void DefaultProperties ()
68                 {
69                         HtmlInputRadioButton rb = new HtmlInputRadioButton ();
70                         Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count");
71
72                         Assert.IsFalse (rb.Checked, "Checked");
73                         Assert.AreEqual (String.Empty, rb.Name, "Name");
74                         Assert.IsNull (rb.Value, "Value");
75
76                         Assert.AreEqual ("input", rb.TagName, "TagName");
77                         Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count-2");
78                 }
79
80                 [Test]
81                 public void NullProperties ()
82                 {
83                         HtmlInputRadioButton rb = new HtmlInputRadioButton ();
84                         rb.Name = null;
85                         Assert.AreEqual (String.Empty, rb.Name, "Name");
86                         rb.Value = null;
87                         Assert.IsNull (rb.Value, "Value");
88
89                         Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count");
90                 }
91
92                 [Test]
93                 public void CleanProperties ()
94                 {
95                         HtmlInputRadioButton rb = new HtmlInputRadioButton ();
96                         rb.Checked = true;
97                         Assert.IsTrue (rb.Checked, "Checked");
98                         rb.Name = "name";
99                         Assert.AreEqual ("name", rb.Name, "Name");
100                         rb.Value = "value";
101                         Assert.AreEqual ("value", rb.Value, "Value");
102                         Assert.AreEqual (defaultAttributesCount + 3, rb.Attributes.Count, "1");
103
104                         rb.Checked = false;
105                         Assert.IsFalse (rb.Checked, "-Checked");
106                         rb.Name = null;
107                         Assert.AreEqual (String.Empty, rb.Name, "-Name");
108                         rb.Value = null;
109                         Assert.IsNull (rb.Value, "-Value");
110                         Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "0");
111                 }
112
113                 [Test]
114                 public void Value_Existing ()
115                 {
116                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
117                         rb.Value = "a";
118                         Assert.AreEqual ("a", rb.Value, "Value before");
119                         rb.ID = "id1";
120                         Assert.AreEqual ("id1", rb.ID, "ID");
121                         Assert.AreEqual ("a", rb.Value, "Value after");
122                 }
123
124                 [Test]
125                 public void Value_Resetting ()
126                 {
127                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
128                         rb.ID = "id1";
129                         Assert.AreEqual ("id1", rb.Value, "Value before");
130                         rb.Value = "a";
131                         Assert.AreEqual ("id1", rb.ID, "ID");
132                         Assert.AreEqual ("a", rb.Value, "Value after");
133                 }
134
135                 [Test]
136                 public void Value_ResetNull ()
137                 {
138                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
139                         rb.Value = "a";
140                         rb.ID = "id1";
141                         rb.Value = null;
142                         Assert.AreEqual ("id1", rb.ID, "ID");
143                         Assert.AreEqual ("id1", rb.Value, "Value");
144                 }
145
146                 [Test]
147                 // note: this behaviour isn't present in HtmlInputControl
148                 public void IDversusValue ()
149                 {
150                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
151                         Assert.IsNull (rb.Value, "Value before");
152                         rb.ID = "id1";
153                         Assert.AreEqual ("id1", rb.ID, "ID");
154                         Assert.AreEqual ("id1", rb.Value, "Value after");
155                 }
156
157                 [Test]
158                 [Ignore ("throws NullReferenceException")]
159                 public void RenderAttributes ()
160                 {
161                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
162                         rb.Checked = true;
163                         rb.Name = "mono";
164                         rb.Value = "value";
165                         rb.RenderAttributes ();
166                 }
167
168                 private bool serverChange;
169                 private void ServerChange (object sender, EventArgs e)
170                 {
171                         serverChange = true;
172                 }
173
174                 [Test]
175                 public void IPostBackDataHandler_RaisePostBackEvent ()
176                 {
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");
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (NullReferenceException))]
187                 public void IPostBackDataHandler_LoadPostData_NullCollection ()
188                 {
189                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
190                         IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
191                         pbdh.LoadPostData ("id1", null);
192                 }
193
194                 [Test]
195                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
196                 public void IPostBackDataHandler_LoadPostData_IdNull ()
197                 {
198                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
199                         rb.ID = "id1";
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");
205                 }
206
207                 [Test]
208                 public void IPostBackDataHandler_LoadPostData_WrongId ()
209                 {
210                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
211                         rb.ID = "id1";
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");
217                 }
218
219                 [Test]
220                 public void IPostBackDataHandler_LoadPostData ()
221                 {
222                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
223                         rb.ID = "id1";
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");
230                 }
231
232                 [Test]
233                 public void RenderValue1 ()
234                 {
235                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
236                         rb.ID = "id";
237                         string attrs = rb.RenderAttributes ();
238                         Assert.IsTrue (attrs.IndexOf ("value=\"id\"") >= 0);
239                         rb.Value = "hola";
240                         attrs = rb.RenderAttributes ();
241                         Assert.IsTrue (attrs.IndexOf ("value=\"hola\"") >= 0);
242                 }
243
244 #if NET_2_0
245                 [Test]
246                 public void RaisePostBackEvent ()
247                 {
248                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
249                         rb.ServerChange += new EventHandler (ServerChange);
250                         serverChange = false;
251                         rb.Raise ();
252                         Assert.IsTrue (serverChange, "ServerClick");
253                 }
254
255                 [Test]
256                 [ExpectedException (typeof (NullReferenceException))]
257                 public void LoadPostData_NullCollection ()
258                 {
259                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
260                         rb.LoadPost ("id1", null);
261                 }
262
263                 [Test]
264                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
265                 public void LoadPostData_IdNull ()
266                 {
267                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
268                         rb.ID = "id1";
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");
273                 }
274
275                 [Test]
276                 public void LoadPostData_WrongId ()
277                 {
278                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
279                         rb.ID = "id1";
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");
284                 }
285
286                 [Test]
287                 public void LoadPostData ()
288                 {
289                         TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
290                         rb.ID = "id1";
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");
295                 }
296 #endif
297         }
298 }