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