[asp.net] Do nothing if null SessionStateStoreData is passed to SessionStateServerHan...
[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 using MonoTests.stand_alone.WebHarness;
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                 [Category ("NotWorking")]
194                 public void RenderAttributes_Password ()
195                 {
196                         TestHtmlInputText it = new TestHtmlInputText ("password");
197                         it.MaxLength = 2;
198                         it.Size = 4;
199                         it.ID = "mono";
200                         it.Value = "s3kr3t";
201 #if NET_2_0
202                         // value is there, maybe because a new HtmlInputPassword class exists ?
203                         HtmlDiff.AssertAreEqual (" name=\"mono\" id=\"mono\" type=\"password\" maxlength=\"2\" size=\"4\" value=\"s3kr3t\" /", it.RenderAttributes (),"Render failed");
204                         Assert.IsFalse (it.ViewStateValueChanged, "ViewStateValueChanged");
205                         Assert.IsFalse (it.AttributeValueChanged, "AttributeValueChanged");
206 #else
207                         HtmlDiff.AssertAreEqual(" name=\"mono\" id=\"mono\" type=\"password\" maxlength=\"2\" size=\"4\" /", it.RenderAttributes(),"Render failed");
208                         Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
209                         Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
210 #endif
211                         Assert.IsNull (it.ViewStateNewValue, "ViewStateNewValue");
212                         Assert.IsNull (it.AttributeNewValue, "AttributeNewValue");
213                 }
214
215                 private bool serverChange;
216                 private void ServerChange (object sender, EventArgs e)
217                 {
218                         serverChange = true;
219                 }
220
221                 [Test]
222                 public void IPostBackDataHandler_RaisePostBackEvent ()
223                 {
224                         TestHtmlInputText it = new TestHtmlInputText ("password");
225                         it.ServerChange += new EventHandler (ServerChange);
226                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
227                         serverChange = false;
228                         pbdh.RaisePostDataChangedEvent ();
229                         Assert.IsTrue (serverChange, "ServerChange");
230                 }
231
232                 [Test]
233                 [ExpectedException (typeof (NullReferenceException))]
234                 public void IPostBackDataHandler_LoadPostData_NullCollection ()
235                 {
236                         TestHtmlInputText it = new TestHtmlInputText ("password");
237                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
238                         pbdh.LoadPostData ("id1", null);
239                 }
240
241                 [Test]
242                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
243                 public void IPostBackDataHandler_LoadPostData_IdNull ()
244                 {
245                         TestHtmlInputText it = new TestHtmlInputText ("password");
246                         it.ID = "id1";
247                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
248                         NameValueCollection nvc = new NameValueCollection ();
249                         nvc.Add ("id1", "mono");
250                         Assert.IsTrue (pbdh.LoadPostData (null, nvc), "LoadPostData");
251                         Assert.AreEqual (String.Empty, it.Value, "Value");
252                 }
253
254                 [Test]
255                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
256                 public void IPostBackDataHandler_LoadPostData_WrongId ()
257                 {
258                         TestHtmlInputText it = new TestHtmlInputText ("password");
259                         it.ID = "id1";
260                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
261                         NameValueCollection nvc = new NameValueCollection ();
262                         nvc.Add ("id1", "mono");
263                         Assert.IsTrue (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
264                         Assert.AreEqual (String.Empty, it.Value, "Value");
265                 }
266
267                 [Test]
268                 public void IPostBackDataHandler_LoadPostData ()
269                 {
270                         TestHtmlInputText it = new TestHtmlInputText ("password");
271                         it.ID = "id1";
272                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
273                         NameValueCollection nvc = new NameValueCollection ();
274                         nvc.Add ("id1", "mono");
275                         Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
276                         Assert.AreEqual ("mono", it.Value, "Value");
277                 }
278 #if NET_2_0
279                 [Test]
280                 public void RaisePostBackEvent ()
281                 {
282                         TestHtmlInputText it = new TestHtmlInputText ("password");
283                         it.ServerChange += new EventHandler (ServerChange);
284                         serverChange = false;
285                         it.Raise ();
286                         Assert.IsTrue (serverChange, "ServerClick");
287                 }
288
289                 [Test]
290                 [ExpectedException (typeof (NullReferenceException))]
291                 public void LoadPostData_NullCollection ()
292                 {
293                         TestHtmlInputText it = new TestHtmlInputText ("password");
294                         it.LoadPost ("id1", null);
295                 }
296
297                 [Test]
298                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
299                 public void LoadPostData_IdNull ()
300                 {
301                         TestHtmlInputText it = new TestHtmlInputText ("password");
302                         it.ID = "id1";
303                         NameValueCollection nvc = new NameValueCollection ();
304                         nvc.Add ("id1", "mono");
305                         Assert.IsTrue (it.LoadPost (null, nvc), "LoadPostData");
306                         Assert.AreEqual (String.Empty, it.Value, "Value");
307                 }
308
309                 [Test]
310                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
311                 public void LoadPostData_WrongId ()
312                 {
313                         TestHtmlInputText it = new TestHtmlInputText ("password");
314                         it.ID = "id1";
315                         NameValueCollection nvc = new NameValueCollection ();
316                         nvc.Add ("id1", "mono");
317                         Assert.IsTrue (it.LoadPost ("id2", nvc), "LoadPostData");
318                         Assert.AreEqual (String.Empty, it.Value, "Value");
319                 }
320
321                 [Test]
322                 public void LoadPostData ()
323                 {
324                         TestHtmlInputText it = new TestHtmlInputText ("password");
325                         it.ID = "id1";
326                         NameValueCollection nvc = new NameValueCollection ();
327                         nvc.Add ("id1", "mono");
328                         Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
329                         Assert.AreEqual ("mono", it.Value, "Value");
330                 }
331 #endif
332         }
333 }