Merge pull request #1496 from echampet/serializers
[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
31 using System;
32 using System.Collections.Specialized;
33 using System.IO;
34 using System.Web.UI;
35 using System.Web.UI.HtmlControls;
36
37 using NUnit.Framework;
38
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 string RenderAttributes ()
55                 {
56                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
57                         string val = (string) ViewState ["value"];
58                         string att = Attributes ["value"];
59                         base.RenderAttributes (writer);
60                         if (val != (string) ViewState ["value"]) {
61                                 value_changed = true;
62                                 new_value = (string) ViewState ["value"];
63                         }
64                         if (att != Attributes ["value"]) {
65                                 attr_value_changed = true;
66                                 attr_new_value = Attributes ["value"];
67                         }
68                         return writer.InnerWriter.ToString ();
69                 }
70
71                 public bool ViewStateValueChanged {
72                         get { return value_changed; }
73                 }
74
75                 public string ViewStateNewValue {
76                         get { return new_value; }
77                 }
78
79                 public bool AttributeValueChanged {
80                         get { return attr_value_changed; }
81                 }
82
83                 public string AttributeNewValue {
84                         get { return attr_new_value; }
85                 }
86
87                 public bool LoadPost (string key, NameValueCollection nvc)
88                 {
89                         return base.LoadPostData (key, nvc);
90                 }
91
92                 public void Raise ()
93                 {
94                         base.RaisePostDataChangedEvent ();
95                 }
96         }
97
98
99         [TestFixture]
100         public class HtmlInputPasswordTest {
101
102                 private const int defaultAttributesCount = 1;
103
104                 [Test]
105                 public void DefaultProperties ()
106                 {
107                         HtmlInputPassword it = new HtmlInputPassword ();
108                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
109
110                         Assert.AreEqual ("password", it.Type, "Type");
111                         Assert.AreEqual (String.Empty, it.Value, "Value");
112
113                         Assert.AreEqual ("input", it.TagName, "TagName");
114                         Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
115                 }
116
117                 [Test]
118                 public void RenderAttributes ()
119                 {
120                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
121                         it.MaxLength = 4;
122                         it.Size = 2;
123                         it.Name = "mono";
124                         it.Value = "value";
125                         Assert.AreEqual (" name type=\"password\" maxlength=\"4\" size=\"2\" /", it.RenderAttributes ());
126                         Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
127                         Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
128                 }
129
130                 private bool serverChange;
131                 private void ServerChange (object sender, EventArgs e)
132                 {
133                         serverChange = true;
134                 }
135
136                 [Test]
137                 public void IPostBackDataHandler_RaisePostBackEvent ()
138                 {
139                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
140                         it.ServerChange += new EventHandler (ServerChange);
141                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
142                         serverChange = false;
143                         pbdh.RaisePostDataChangedEvent ();
144                         Assert.IsTrue (serverChange, "ServerChange");
145                 }
146
147                 [Test]
148                 [ExpectedException (typeof (NullReferenceException))]
149                 public void IPostBackDataHandler_LoadPostData_NullCollection ()
150                 {
151                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
152                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
153                         pbdh.LoadPostData ("id1", null);
154                 }
155
156                 [Test]
157                 public void IPostBackDataHandler_LoadPostData ()
158                 {
159                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
160                         it.ID = "id1";
161                         IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
162                         NameValueCollection nvc = new NameValueCollection ();
163                         nvc.Add ("id1", "mono");
164                         Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
165                         Assert.AreEqual ("mono", it.Value, "Value");
166                 }
167
168                 [Test]
169                 public void RaisePostBackEvent ()
170                 {
171                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
172                         it.ServerChange += new EventHandler (ServerChange);
173                         serverChange = false;
174                         it.Raise ();
175                         Assert.IsTrue (serverChange, "ServerClick");
176                 }
177
178                 [Test]
179                 [ExpectedException (typeof (NullReferenceException))]
180                 public void LoadPostData_NullCollection ()
181                 {
182                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
183                         it.LoadPost ("id1", null);
184                 }
185
186                 [Test]
187                 public void LoadPostData ()
188                 {
189                         TestHtmlInputPassword it = new TestHtmlInputPassword ();
190                         it.ID = "id1";
191                         NameValueCollection nvc = new NameValueCollection ();
192                         nvc.Add ("id1", "mono");
193                         Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
194                         Assert.AreEqual ("mono", it.Value, "Value");
195                 }
196         }
197 }
198