2 // HtmlInputTextTest.cs
3 // - Unit tests for System.Web.UI.HtmlControls.HtmlInputText
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 TestHtmlInputText : HtmlInputText {
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;
48 public TestHtmlInputText ()
53 public TestHtmlInputText (string type)
58 public string RenderAttributes ()
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"]) {
66 new_value = (string) ViewState ["value"];
68 if (att != Attributes ["value"]) {
69 attr_value_changed = true;
70 attr_new_value = Attributes ["value"];
72 return writer.InnerWriter.ToString ();
75 public bool ViewStateValueChanged {
76 get { return value_changed; }
79 public string ViewStateNewValue {
80 get { return new_value; }
83 public bool AttributeValueChanged {
84 get { return attr_value_changed; }
87 public string AttributeNewValue {
88 get { return attr_new_value; }
91 public bool LoadPost (string key, NameValueCollection nvc)
93 return base.LoadPostData (key, nvc);
98 base.RaisePostDataChangedEvent ();
104 public class HtmlInputTextTest {
106 private const int defaultAttributesCount = 1;
109 public void ConstructorType ()
111 HtmlInputText it = new HtmlInputText ("mono");
112 Assert.AreEqual ("mono", it.Type, "Type");
116 public void DefaultProperties ()
118 HtmlInputText it = new HtmlInputText ();
119 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
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");
127 Assert.AreEqual ("input", it.TagName, "TagName");
128 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
132 public void NullProperties ()
134 HtmlInputText it = new HtmlInputText ();
136 Assert.AreEqual (-1, it.MaxLength, "MaxLength");
138 Assert.IsNull (it.Name, "Name");
140 Assert.AreEqual (-1, it.Size, "Size");
142 Assert.AreEqual (String.Empty, it.Value, "Value");
144 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
148 public void CleanProperties ()
150 HtmlInputText it = new HtmlInputText ();
152 Assert.AreEqual (1, it.MaxLength, "MaxLength");
154 Assert.IsNull (it.Name, "Name");
156 Assert.AreEqual (2, it.Size, "Size");
158 Assert.AreEqual ("value", it.Value, "Value");
159 Assert.AreEqual (defaultAttributesCount + 3, it.Attributes.Count, "1");
162 Assert.AreEqual (-1, it.MaxLength, "-MaxLength");
164 Assert.IsNull (it.Name, "-Name");
166 Assert.AreEqual (-1, it.Size, "Size");
168 Assert.AreEqual (String.Empty, it.Value, "-Value");
169 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "0");
173 public void Password ()
175 TestHtmlInputText it = new TestHtmlInputText ("password");
178 Assert.AreEqual ("s3kr3t", it.Value, "Value");
182 public void RenderAttributes ()
184 TestHtmlInputText it = new TestHtmlInputText ();
189 Assert.AreEqual (" name type=\"text\" maxlength=\"4\" size=\"2\" value=\"value\" /", it.RenderAttributes ());
193 public void RenderAttributes_Password ()
195 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
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");
210 Assert.IsNull (it.ViewStateNewValue, "ViewStateNewValue");
211 Assert.IsNull (it.AttributeNewValue, "AttributeNewValue");
214 private bool serverChange;
215 private void ServerChange (object sender, EventArgs e)
221 public void IPostBackDataHandler_RaisePostBackEvent ()
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");
232 [ExpectedException (typeof (NullReferenceException))]
233 public void IPostBackDataHandler_LoadPostData_NullCollection ()
235 TestHtmlInputText it = new TestHtmlInputText ("password");
236 IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
237 pbdh.LoadPostData ("id1", null);
241 [Category ("NotDotNet")] // MS throws a NullReferenceException here
242 public void IPostBackDataHandler_LoadPostData_IdNull ()
244 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
254 [Category ("NotDotNet")] // MS throws a NullReferenceException here
255 public void IPostBackDataHandler_LoadPostData_WrongId ()
257 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
267 public void IPostBackDataHandler_LoadPostData ()
269 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
279 public void RaisePostBackEvent ()
281 TestHtmlInputText it = new TestHtmlInputText ("password");
282 it.ServerChange += new EventHandler (ServerChange);
283 serverChange = false;
285 Assert.IsTrue (serverChange, "ServerClick");
289 [ExpectedException (typeof (NullReferenceException))]
290 public void LoadPostData_NullCollection ()
292 TestHtmlInputText it = new TestHtmlInputText ("password");
293 it.LoadPost ("id1", null);
297 [Category ("NotDotNet")] // MS throws a NullReferenceException here
298 public void LoadPostData_IdNull ()
300 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
309 [Category ("NotDotNet")] // MS throws a NullReferenceException here
310 public void LoadPostData_WrongId ()
312 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
321 public void LoadPostData ()
323 TestHtmlInputText it = new TestHtmlInputText ("password");
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");