Merge pull request #1345 from mattleibow/websocket-continuation-frame-fix
[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                 public bool LoadPost (string key, NameValueCollection nvc)
91                 {
92                         return base.LoadPostData(key, nvc);
93                 }
94
95                 public void Raise ()
96                 {
97                         base.RaisePostDataChangedEvent ();
98                 }
99         }
100
101         [TestFixture]
102         public class HtmlInputTextTest {
103
104                 private const int defaultAttributesCount = 1;
105
106                 [Test]
107                 public void ConstructorType ()
108                 {
109                         HtmlInputText it = new HtmlInputText ("mono");
110                         Assert.AreEqual ("mono", it.Type, "Type");
111                 }
112
113                 [Test]
114                 public void DefaultProperties ()
115                 {
116                         HtmlInputText it = new HtmlInputText ();
117                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
118
119                         Assert.AreEqual (-1, it.MaxLength, "MaxLength");
120                         Assert.IsNull (it.Name, "Name");
121                         Assert.AreEqual (-1, it.Size, "Size");
122                         Assert.AreEqual ("text", it.Type, "Type");
123                         Assert.AreEqual (String.Empty, it.Value, "Value");
124
125                         Assert.AreEqual ("input", it.TagName, "TagName");
126                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
127                 }
128
129                 [Test]
130                 public void NullProperties ()
131                 {
132                         HtmlInputText it = new HtmlInputText ();
133                         it.MaxLength = -1;
134                         Assert.AreEqual (-1, it.MaxLength, "MaxLength");
135                         it.Name = null;
136                         Assert.IsNull (it.Name, "Name");
137                         it.Size = -1;
138                         Assert.AreEqual (-1, it.Size, "Size");
139                         it.Value = null;
140                         Assert.AreEqual (String.Empty, it.Value, "Value");
141
142                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
143                 }
144
145                 [Test]
146                 public void CleanProperties ()
147                 {
148                         HtmlInputText it = new HtmlInputText ();
149                         it.MaxLength = 1;
150                         Assert.AreEqual (1, it.MaxLength, "MaxLength");
151                         it.Name = "name";
152                         Assert.IsNull (it.Name, "Name");
153                         it.Size = 2;
154                         Assert.AreEqual (2, it.Size, "Size");
155                         it.Value = "value";
156                         Assert.AreEqual ("value", it.Value, "Value");
157                         Assert.AreEqual (defaultAttributesCount + 3, it.Attributes.Count, "1");
158
159                         it.MaxLength = -1;
160                         Assert.AreEqual (-1, it.MaxLength, "-MaxLength");
161                         it.Name = null;
162                         Assert.IsNull (it.Name, "-Name");
163                         it.Size = -1;
164                         Assert.AreEqual (-1, it.Size, "Size");
165                         it.Value = null;
166                         Assert.AreEqual (String.Empty, it.Value, "-Value");
167                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "0");
168                 }
169
170                 [Test]
171                 public void Password ()
172                 {
173                         TestHtmlInputText it = new TestHtmlInputText ("password");
174                         it.Value = "s3kr3t";
175                         it.ID = "passwd";
176                         Assert.AreEqual ("s3kr3t", it.Value, "Value");
177                 }
178
179                 [Test]
180                 public void RenderAttributes ()
181                 {
182                         TestHtmlInputText it = new TestHtmlInputText ();
183                         it.MaxLength = 4;
184                         it.Size = 2;
185                         it.Name = "mono";
186                         it.Value = "value";
187                         Assert.AreEqual (" name type=\"text\" maxlength=\"4\" size=\"2\" value=\"value\" /", it.RenderAttributes ());
188                 }
189
190                 [Test]
191                 [Category ("NotWorking")]
192                 public void RenderAttributes_Password ()
193                 {
194                         TestHtmlInputText it = new TestHtmlInputText ("password");
195                         it.MaxLength = 2;
196                         it.Size = 4;
197                         it.ID = "mono";
198                         it.Value = "s3kr3t";
199                         // value is there, maybe because a new HtmlInputPassword class exists ?
200                         HtmlDiff.AssertAreEqual (" name=\"mono\" id=\"mono\" type=\"password\" maxlength=\"2\" size=\"4\" value=\"s3kr3t\" /", it.RenderAttributes (),"Render failed");
201                         Assert.IsFalse (it.ViewStateValueChanged, "ViewStateValueChanged");
202                         Assert.IsFalse (it.AttributeValueChanged, "AttributeValueChanged");
203                         Assert.IsNull (it.ViewStateNewValue, "ViewStateNewValue");
204                         Assert.IsNull (it.AttributeNewValue, "AttributeNewValue");
205                 }
206
207                 private bool serverChange;
208                 private void ServerChange (object sender, EventArgs e)
209                 {
210                         serverChange = true;
211                 }
212
213                 [Test]
214                 public void IPostBackDataHandler_RaisePostBackEvent ()
215                 {
216                         TestHtmlInputText it = new TestHtmlInputText ("password");
217                         it.ServerChange += new EventHandler (ServerChange);
218                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
219                         serverChange = false;
220                         pbdh.RaisePostDataChangedEvent ();
221                         Assert.IsTrue (serverChange, "ServerChange");
222                 }
223
224                 [Test]
225                 [ExpectedException (typeof (NullReferenceException))]
226                 public void IPostBackDataHandler_LoadPostData_NullCollection ()
227                 {
228                         TestHtmlInputText it = new TestHtmlInputText ("password");
229                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
230                         pbdh.LoadPostData ("id1", null);
231                 }
232
233                 [Test]
234                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
235                 public void IPostBackDataHandler_LoadPostData_IdNull ()
236                 {
237                         TestHtmlInputText it = new TestHtmlInputText ("password");
238                         it.ID = "id1";
239                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
240                         NameValueCollection nvc = new NameValueCollection ();
241                         nvc.Add ("id1", "mono");
242                         Assert.IsTrue (pbdh.LoadPostData (null, nvc), "LoadPostData");
243                         Assert.AreEqual (String.Empty, it.Value, "Value");
244                 }
245
246                 [Test]
247                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
248                 public void IPostBackDataHandler_LoadPostData_WrongId ()
249                 {
250                         TestHtmlInputText it = new TestHtmlInputText ("password");
251                         it.ID = "id1";
252                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
253                         NameValueCollection nvc = new NameValueCollection ();
254                         nvc.Add ("id1", "mono");
255                         Assert.IsTrue (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
256                         Assert.AreEqual (String.Empty, it.Value, "Value");
257                 }
258
259                 [Test]
260                 public void IPostBackDataHandler_LoadPostData ()
261                 {
262                         TestHtmlInputText it = new TestHtmlInputText ("password");
263                         it.ID = "id1";
264                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
265                         NameValueCollection nvc = new NameValueCollection ();
266                         nvc.Add ("id1", "mono");
267                         Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
268                         Assert.AreEqual ("mono", it.Value, "Value");
269                 }
270                 [Test]
271                 public void RaisePostBackEvent ()
272                 {
273                         TestHtmlInputText it = new TestHtmlInputText ("password");
274                         it.ServerChange += new EventHandler (ServerChange);
275                         serverChange = false;
276                         it.Raise ();
277                         Assert.IsTrue (serverChange, "ServerClick");
278                 }
279
280                 [Test]
281                 [ExpectedException (typeof (NullReferenceException))]
282                 public void LoadPostData_NullCollection ()
283                 {
284                         TestHtmlInputText it = new TestHtmlInputText ("password");
285                         it.LoadPost ("id1", null);
286                 }
287
288                 [Test]
289                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
290                 public void LoadPostData_IdNull ()
291                 {
292                         TestHtmlInputText it = new TestHtmlInputText ("password");
293                         it.ID = "id1";
294                         NameValueCollection nvc = new NameValueCollection ();
295                         nvc.Add ("id1", "mono");
296                         Assert.IsTrue (it.LoadPost (null, nvc), "LoadPostData");
297                         Assert.AreEqual (String.Empty, it.Value, "Value");
298                 }
299
300                 [Test]
301                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
302                 public void LoadPostData_WrongId ()
303                 {
304                         TestHtmlInputText it = new TestHtmlInputText ("password");
305                         it.ID = "id1";
306                         NameValueCollection nvc = new NameValueCollection ();
307                         nvc.Add ("id1", "mono");
308                         Assert.IsTrue (it.LoadPost ("id2", nvc), "LoadPostData");
309                         Assert.AreEqual (String.Empty, it.Value, "Value");
310                 }
311
312                 [Test]
313                 public void LoadPostData ()
314                 {
315                         TestHtmlInputText it = new TestHtmlInputText ("password");
316                         it.ID = "id1";
317                         NameValueCollection nvc = new NameValueCollection ();
318                         nvc.Add ("id1", "mono");
319                         Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
320                         Assert.AreEqual ("mono", it.Value, "Value");
321                 }
322         }
323 }