svn path=/branches/mono-1-1-9/mcs/; revision=50439
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlInputPasswordTest.cs
1 //
2 // HtmlInputPasswordTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlInputPassword
4 //
5 // Author:
6 //      Chris Toshok  <toshok@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 #if NET_2_0
39 namespace MonoTests.System.Web.UI.HtmlControls {
40
41         public class TestHtmlInputPassword : HtmlInputPassword {
42
43                 bool value_changed; // true if the "value" is changed in RenderAttributes
44                 string new_value; // "value" in ViewState if value_changed is true.
45                 bool attr_value_changed; // same but for attributes (instead of viewstate)
46                 string attr_new_value;
47
48
49                 public TestHtmlInputPassword ()
50                         : base ()
51                 {
52                 }
53
54                 public TestHtmlInputPassword (string type)
55                         : base (type)
56                 {
57                 }
58
59                 public string RenderAttributes ()
60                 {
61                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
62                         string val = (string) ViewState ["value"];
63                         string att = Attributes ["value"];
64                         base.RenderAttributes (writer);
65                         if (val != (string) ViewState ["value"]) {
66                                 value_changed = true;
67                                 new_value = (string) ViewState ["value"];
68                         }
69                         if (att != Attributes ["value"]) {
70                                 attr_value_changed = true;
71                                 attr_new_value = Attributes ["value"];
72                         }
73                         return writer.InnerWriter.ToString ();
74                 }
75
76                 public bool ViewStateValueChanged {
77                         get { return value_changed; }
78                 }
79
80                 public string ViewStateNewValue {
81                         get { return new_value; }
82                 }
83
84                 public bool AttributeValueChanged {
85                         get { return attr_value_changed; }
86                 }
87
88                 public string AttributeNewValue {
89                         get { return attr_new_value; }
90                 }
91 #if NET_2_0
92                 public bool LoadPost (string key, NameValueCollection nvc)
93                 {
94                         return base.LoadPostData (key, nvc);
95                 }
96
97                 public void Raise ()
98                 {
99                         base.RaisePostDataChangedEvent ();
100                 }
101 #endif
102         }
103
104
105         [TestFixture]
106         public class HtmlInputPasswordTest {
107                 private const int defaultAttributesCount = 0;
108
109                 [Test]
110                 public void ConstructorType ()
111                 {
112                         HtmlInputPassword it = new HtmlInputPassword ("mono");
113                         Assert.AreEqual ("mono", it.Type, "Type");
114                 }
115
116                 [Test]
117                 public void DefaultProperties ()
118                 {
119                         HtmlInputPassword it = new HtmlInputPassword ();
120                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
121
122                         Assert.AreEqual ("password", 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 RenderAttributes ()
131                 {
132                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
133                         it.MaxLength = 4;
134                         it.Size = 2;
135                         it.Name = "mono";
136                         it.Value = "value";
137                         Assert.AreEqual (" name type=\"password\" maxlength=\"4\" size=\"2\" /", it.RenderAttributes ());
138                         Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
139                         Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
140                 }
141
142                 private bool serverChange;
143                 private void ServerChange (object sender, EventArgs e)
144                 {
145                         serverChange = true;
146                 }
147
148                 [Test]
149                 public void IPostBackDataHandler_RaisePostBackEvent ()
150                 {
151                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
152                         it.ServerChange += new EventHandler (ServerChange);
153                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
154                         serverChange = false;
155                         pbdh.RaisePostDataChangedEvent ();
156                         Assert.IsTrue (serverChange, "ServerChange");
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (NullReferenceException))]
161                 public void IPostBackDataHandler_LoadPostData_NullCollection ()
162                 {
163                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
164                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
165                         pbdh.LoadPostData ("id1", null);
166                 }
167
168                 [Test]
169                 public void IPostBackDataHandler_LoadPostData ()
170                 {
171                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
172                         it.ID = "id1";
173                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
174                         NameValueCollection nvc = new NameValueCollection ();
175                         nvc.Add ("id1", "mono");
176                         Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
177                         Assert.AreEqual ("mono", it.Value, "Value");
178                 }
179
180                 [Test]
181                 public void RaisePostBackEvent ()
182                 {
183                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
184                         it.ServerChange += new EventHandler (ServerChange);
185                         serverChange = false;
186                         it.Raise ();
187                         Assert.IsTrue (serverChange, "ServerClick");
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (NullReferenceException))]
192                 public void LoadPostData_NullCollection ()
193                 {
194                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
195                         it.LoadPost ("id1", null);
196                 }
197
198                 [Test]
199                 public void LoadPostData ()
200                 {
201                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
202                         it.ID = "id1";
203                         NameValueCollection nvc = new NameValueCollection ();
204                         nvc.Add ("id1", "mono");
205                         Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
206                         Assert.AreEqual ("mono", it.Value, "Value");
207                 }
208         }
209 }
210
211 #endif