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;
35 using MonoTests.stand_alone.WebHarness;
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; }
90 public bool LoadPost (string key, NameValueCollection nvc)
92 return base.LoadPostData(key, nvc);
97 base.RaisePostDataChangedEvent ();
102 public class HtmlInputTextTest {
104 private const int defaultAttributesCount = 1;
107 public void ConstructorType ()
109 HtmlInputText it = new HtmlInputText ("mono");
110 Assert.AreEqual ("mono", it.Type, "Type");
114 public void DefaultProperties ()
116 HtmlInputText it = new HtmlInputText ();
117 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
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");
125 Assert.AreEqual ("input", it.TagName, "TagName");
126 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
130 public void NullProperties ()
132 HtmlInputText it = new HtmlInputText ();
134 Assert.AreEqual (-1, it.MaxLength, "MaxLength");
136 Assert.IsNull (it.Name, "Name");
138 Assert.AreEqual (-1, it.Size, "Size");
140 Assert.AreEqual (String.Empty, it.Value, "Value");
142 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
146 public void CleanProperties ()
148 HtmlInputText it = new HtmlInputText ();
150 Assert.AreEqual (1, it.MaxLength, "MaxLength");
152 Assert.IsNull (it.Name, "Name");
154 Assert.AreEqual (2, it.Size, "Size");
156 Assert.AreEqual ("value", it.Value, "Value");
157 Assert.AreEqual (defaultAttributesCount + 3, it.Attributes.Count, "1");
160 Assert.AreEqual (-1, it.MaxLength, "-MaxLength");
162 Assert.IsNull (it.Name, "-Name");
164 Assert.AreEqual (-1, it.Size, "Size");
166 Assert.AreEqual (String.Empty, it.Value, "-Value");
167 Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "0");
171 public void Password ()
173 TestHtmlInputText it = new TestHtmlInputText ("password");
176 Assert.AreEqual ("s3kr3t", it.Value, "Value");
180 public void RenderAttributes ()
182 TestHtmlInputText it = new TestHtmlInputText ();
187 Assert.AreEqual (" name type=\"text\" maxlength=\"4\" size=\"2\" value=\"value\" /", it.RenderAttributes ());
191 [Category ("NotWorking")]
192 public void RenderAttributes_Password ()
194 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
207 private bool serverChange;
208 private void ServerChange (object sender, EventArgs e)
214 public void IPostBackDataHandler_RaisePostBackEvent ()
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");
225 [ExpectedException (typeof (NullReferenceException))]
226 public void IPostBackDataHandler_LoadPostData_NullCollection ()
228 TestHtmlInputText it = new TestHtmlInputText ("password");
229 IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
230 pbdh.LoadPostData ("id1", null);
234 [Category ("NotDotNet")] // MS throws a NullReferenceException here
235 public void IPostBackDataHandler_LoadPostData_IdNull ()
237 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
247 [Category ("NotDotNet")] // MS throws a NullReferenceException here
248 public void IPostBackDataHandler_LoadPostData_WrongId ()
250 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
260 public void IPostBackDataHandler_LoadPostData ()
262 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
271 public void RaisePostBackEvent ()
273 TestHtmlInputText it = new TestHtmlInputText ("password");
274 it.ServerChange += new EventHandler (ServerChange);
275 serverChange = false;
277 Assert.IsTrue (serverChange, "ServerClick");
281 [ExpectedException (typeof (NullReferenceException))]
282 public void LoadPostData_NullCollection ()
284 TestHtmlInputText it = new TestHtmlInputText ("password");
285 it.LoadPost ("id1", null);
289 [Category ("NotDotNet")] // MS throws a NullReferenceException here
290 public void LoadPostData_IdNull ()
292 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
301 [Category ("NotDotNet")] // MS throws a NullReferenceException here
302 public void LoadPostData_WrongId ()
304 TestHtmlInputText it = new TestHtmlInputText ("password");
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");
313 public void LoadPostData ()
315 TestHtmlInputText it = new TestHtmlInputText ("password");
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");