New test.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlInputTextTest.cs
1 //
2 // HtmlInputTextTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlInputText
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 TestHtmlInputText : HtmlInputText {
41
42                 bool value_changed; // true if the "value" is changed in RenderAttributes
43                 string new_value; // "value" in ViewState if value_changed is true.
44                 bool attr_value_changed; // same but for attributes (instead of viewstate)
45                 string attr_new_value;
46
47
48                 public TestHtmlInputText ()
49                         : base ()
50                 {
51                 }
52
53                 public TestHtmlInputText (string type)
54                         : base (type)
55                 {
56                 }
57
58                 public string RenderAttributes ()
59                 {
60                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
61                         string val = (string) ViewState ["value"];
62                         string att = Attributes ["value"];
63                         base.RenderAttributes (writer);
64                         if (val != (string) ViewState ["value"]) {
65                                 value_changed = true;
66                                 new_value = (string) ViewState ["value"];
67                         }
68                         if (att != Attributes ["value"]) {
69                                 attr_value_changed = true;
70                                 attr_new_value = Attributes ["value"];
71                         }
72                         return writer.InnerWriter.ToString ();
73                 }
74
75                 public bool ViewStateValueChanged {
76                         get { return value_changed; }
77                 }
78
79                 public string ViewStateNewValue {
80                         get { return new_value; }
81                 }
82
83                 public bool AttributeValueChanged {
84                         get { return attr_value_changed; }
85                 }
86
87                 public string AttributeNewValue {
88                         get { return attr_new_value; }
89                 }
90 #if NET_2_0
91                 public bool LoadPost (string key, NameValueCollection nvc)
92                 {
93                         return base.LoadPostData (key, nvc);
94                 }
95
96                 public void Raise ()
97                 {
98                         base.RaisePostDataChangedEvent ();
99                 }
100 #endif
101         }
102
103         [TestFixture]
104         public class HtmlInputTextTest {
105
106                 private const int defaultAttributesCount = 1;
107
108                 [Test]
109                 public void ConstructorType ()
110                 {
111                         HtmlInputText it = new HtmlInputText ("mono");
112                         Assert.AreEqual ("mono", it.Type, "Type");
113                 }
114
115                 [Test]
116                 public void DefaultProperties ()
117                 {
118                         HtmlInputText it = new HtmlInputText ();
119                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
120
121                         Assert.AreEqual (-1, it.MaxLength, "MaxLength");
122                         Assert.IsNull (it.Name, "Name");
123                         Assert.AreEqual (-1, it.Size, "Size");
124                         Assert.AreEqual ("text", it.Type, "Type");
125                         Assert.AreEqual (String.Empty, it.Value, "Value");
126
127                         Assert.AreEqual ("input", it.TagName, "TagName");
128                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
129                 }
130
131                 [Test]
132                 public void NullProperties ()
133                 {
134                         HtmlInputText it = new HtmlInputText ();
135                         it.MaxLength = -1;
136                         Assert.AreEqual (-1, it.MaxLength, "MaxLength");
137                         it.Name = null;
138                         Assert.IsNull (it.Name, "Name");
139                         it.Size = -1;
140                         Assert.AreEqual (-1, it.Size, "Size");
141                         it.Value = null;
142                         Assert.AreEqual (String.Empty, it.Value, "Value");
143
144                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
145                 }
146
147                 [Test]
148                 public void CleanProperties ()
149                 {
150                         HtmlInputText it = new HtmlInputText ();
151                         it.MaxLength = 1;
152                         Assert.AreEqual (1, it.MaxLength, "MaxLength");
153                         it.Name = "name";
154                         Assert.IsNull (it.Name, "Name");
155                         it.Size = 2;
156                         Assert.AreEqual (2, it.Size, "Size");
157                         it.Value = "value";
158                         Assert.AreEqual ("value", it.Value, "Value");
159                         Assert.AreEqual (defaultAttributesCount + 3, it.Attributes.Count, "1");
160
161                         it.MaxLength = -1;
162                         Assert.AreEqual (-1, it.MaxLength, "-MaxLength");
163                         it.Name = null;
164                         Assert.IsNull (it.Name, "-Name");
165                         it.Size = -1;
166                         Assert.AreEqual (-1, it.Size, "Size");
167                         it.Value = null;
168                         Assert.AreEqual (String.Empty, it.Value, "-Value");
169                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "0");
170                 }
171
172                 [Test]
173                 public void Password ()
174                 {
175                         TestHtmlInputText it = new TestHtmlInputText ("password");
176                         it.Value = "s3kr3t";
177                         it.ID = "passwd";
178                         Assert.AreEqual ("s3kr3t", it.Value, "Value");
179                 }
180
181                 [Test]
182                 public void RenderAttributes ()
183                 {
184                         TestHtmlInputText it = new TestHtmlInputText ();
185                         it.MaxLength = 4;
186                         it.Size = 2;
187                         it.Name = "mono";
188                         it.Value = "value";
189                         Assert.AreEqual (" name type=\"text\" maxlength=\"4\" size=\"2\" value=\"value\" /", it.RenderAttributes ());
190                 }
191
192                 [Test]
193                 public void RenderAttributes_Password ()
194                 {
195                         TestHtmlInputText it = new TestHtmlInputText ("password");
196                         it.MaxLength = 2;
197                         it.Size = 4;
198                         it.ID = "mono";
199                         it.Value = "s3kr3t";
200 #if NET_2_0
201                         // value is there, maybe because a new HtmlInputPassword class exists ?
202                         Assert.AreEqual (" name=\"mono\" type=\"password\" id=\"mono\" maxlength=\"2\" size=\"4\" value=\"s3kr3t\" /", it.RenderAttributes ());
203                         Assert.IsFalse (it.ViewStateValueChanged, "ViewStateValueChanged");
204                         Assert.IsFalse (it.AttributeValueChanged, "AttributeValueChanged");
205 #else
206                         Assert.AreEqual (" name=\"mono\" id=\"mono\" type=\"password\" maxlength=\"2\" size=\"4\" /", it.RenderAttributes ());
207                         Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
208                         Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
209 #endif
210                         Assert.IsNull (it.ViewStateNewValue, "ViewStateNewValue");
211                         Assert.IsNull (it.AttributeNewValue, "AttributeNewValue");
212                 }
213
214                 private bool serverChange;
215                 private void ServerChange (object sender, EventArgs e)
216                 {
217                         serverChange = true;
218                 }
219
220                 [Test]
221                 public void IPostBackDataHandler_RaisePostBackEvent ()
222                 {
223                         TestHtmlInputText it = new TestHtmlInputText ("password");
224                         it.ServerChange += new EventHandler (ServerChange);
225                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
226                         serverChange = false;
227                         pbdh.RaisePostDataChangedEvent ();
228                         Assert.IsTrue (serverChange, "ServerChange");
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (NullReferenceException))]
233                 public void IPostBackDataHandler_LoadPostData_NullCollection ()
234                 {
235                         TestHtmlInputText it = new TestHtmlInputText ("password");
236                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
237                         pbdh.LoadPostData ("id1", null);
238                 }
239
240                 [Test]
241                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
242                 public void IPostBackDataHandler_LoadPostData_IdNull ()
243                 {
244                         TestHtmlInputText it = new TestHtmlInputText ("password");
245                         it.ID = "id1";
246                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
247                         NameValueCollection nvc = new NameValueCollection ();
248                         nvc.Add ("id1", "mono");
249                         Assert.IsTrue (pbdh.LoadPostData (null, nvc), "LoadPostData");
250                         Assert.AreEqual (String.Empty, it.Value, "Value");
251                 }
252
253                 [Test]
254                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
255                 public void IPostBackDataHandler_LoadPostData_WrongId ()
256                 {
257                         TestHtmlInputText it = new TestHtmlInputText ("password");
258                         it.ID = "id1";
259                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
260                         NameValueCollection nvc = new NameValueCollection ();
261                         nvc.Add ("id1", "mono");
262                         Assert.IsTrue (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
263                         Assert.AreEqual (String.Empty, it.Value, "Value");
264                 }
265
266                 [Test]
267                 public void IPostBackDataHandler_LoadPostData ()
268                 {
269                         TestHtmlInputText it = new TestHtmlInputText ("password");
270                         it.ID = "id1";
271                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
272                         NameValueCollection nvc = new NameValueCollection ();
273                         nvc.Add ("id1", "mono");
274                         Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
275                         Assert.AreEqual ("mono", it.Value, "Value");
276                 }
277 #if NET_2_0
278                 [Test]
279                 public void RaisePostBackEvent ()
280                 {
281                         TestHtmlInputText it = new TestHtmlInputText ("password");
282                         it.ServerChange += new EventHandler (ServerChange);
283                         serverChange = false;
284                         it.Raise ();
285                         Assert.IsTrue (serverChange, "ServerClick");
286                 }
287
288                 [Test]
289                 [ExpectedException (typeof (NullReferenceException))]
290                 public void LoadPostData_NullCollection ()
291                 {
292                         TestHtmlInputText it = new TestHtmlInputText ("password");
293                         it.LoadPost ("id1", null);
294                 }
295
296                 [Test]
297                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
298                 public void LoadPostData_IdNull ()
299                 {
300                         TestHtmlInputText it = new TestHtmlInputText ("password");
301                         it.ID = "id1";
302                         NameValueCollection nvc = new NameValueCollection ();
303                         nvc.Add ("id1", "mono");
304                         Assert.IsTrue (it.LoadPost (null, nvc), "LoadPostData");
305                         Assert.AreEqual (String.Empty, it.Value, "Value");
306                 }
307
308                 [Test]
309                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
310                 public void LoadPostData_WrongId ()
311                 {
312                         TestHtmlInputText it = new TestHtmlInputText ("password");
313                         it.ID = "id1";
314                         NameValueCollection nvc = new NameValueCollection ();
315                         nvc.Add ("id1", "mono");
316                         Assert.IsTrue (it.LoadPost ("id2", nvc), "LoadPostData");
317                         Assert.AreEqual (String.Empty, it.Value, "Value");
318                 }
319
320                 [Test]
321                 public void LoadPostData ()
322                 {
323                         TestHtmlInputText it = new TestHtmlInputText ("password");
324                         it.ID = "id1";
325                         NameValueCollection nvc = new NameValueCollection ();
326                         nvc.Add ("id1", "mono");
327                         Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
328                         Assert.AreEqual ("mono", it.Value, "Value");
329                 }
330 #endif
331         }
332 }