[asp.net] Do nothing if null SessionStateStoreData is passed to SessionStateServerHan...
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlInputControlTest.cs
1 //
2 // HtmlInputControlTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlInputControl
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@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.IO;
32 using System.Web.UI;
33 using System.Web.UI.HtmlControls;
34 using MonoTests.stand_alone.WebHarness;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Web.UI.HtmlControls {
38
39         public class TestHtmlInputControl : HtmlInputControl {
40                 bool name_called;
41
42                 public TestHtmlInputControl ()
43                         : base ("mono")
44                 {
45                 }
46
47                 public TestHtmlInputControl (string type)
48                         : base (type)
49                 {
50                 }
51
52                 public string RenderAttributes ()
53                 {
54                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
55                         writer.Write ("<dummy");
56                         base.RenderAttributes (writer);
57                         writer.Write (">");
58                         return writer.InnerWriter.ToString ();
59                 }
60
61                 public override string Name {
62                         get {
63                                 name_called = true;
64                                 return base.Name;
65                         }
66                 }
67
68                 public bool NameCalled {
69                         get { return name_called; }
70                         set { name_called = value; }
71                 }
72         }
73
74         public class UControl : UserControl {
75         }
76
77         [TestFixture]
78         public class HtmlInputControlTest {
79
80                 private const int defaultAttributesCount = 1;
81
82                 [Test]
83                 public void DefaultProperties ()
84                 {
85                         TestHtmlInputControl ic = new TestHtmlInputControl ();
86                         Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
87                         Assert.IsNull (ic.Name, "Name");
88                         Assert.AreEqual ("mono", ic.Type, "Type");
89                         Assert.AreEqual (String.Empty, ic.Value, "Value");
90
91                         Assert.AreEqual ("input", ic.TagName, "TagName");
92                         Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count-2");
93                 }
94
95                 [Test]
96                 public void NullProperties ()
97                 {
98                         TestHtmlInputControl ic = new TestHtmlInputControl ();
99                         ic.Name = null;
100                         Assert.IsNull (ic.Name, "Name");
101                         ic.Value = null;
102                         Assert.AreEqual (String.Empty, ic.Value, "Value");
103
104                         Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
105                 }
106
107                 [Test]
108                 public void CleanProperties ()
109                 {
110                         TestHtmlInputControl ic = new TestHtmlInputControl ();
111                         ic.Name = "name";
112                         Assert.IsNull (ic.Name, "Name");
113                         Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "always null");
114
115                         ic.Value = "value";
116                         Assert.AreEqual ("value", ic.Value, "Value");
117                         Assert.AreEqual (defaultAttributesCount + 1, ic.Attributes.Count, "1");
118
119                         ic.Name = null;
120                         Assert.IsNull (ic.Name, "-Name");
121                         ic.Value = null;
122                         Assert.AreEqual (String.Empty, ic.Value, "-Value");
123                         Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "0");
124                 }
125
126                 [Test]
127                 public void Name ()
128                 {
129                         TestHtmlInputControl ic = new TestHtmlInputControl ();
130                         Assert.IsNull (ic.UniqueID, "UniqueID");
131                         Assert.IsNull (ic.ID, "ID");
132                         ic.Name = "name";
133                         Assert.IsNull (ic.Name, "Name");
134
135                         ic.ID = "id";
136                         Assert.AreEqual ("id", ic.ID, "ID-2");
137                         Assert.AreEqual ("id", ic.UniqueID, "UniqueID");
138                         Assert.AreEqual ("id", ic.Name, "Name-ID");
139
140                         ic.Name = "name";
141                         Assert.AreEqual ("id", ic.Name, "Name-ID-2");
142                         Assert.AreEqual ("id", ic.UniqueID, "UniqueID-2");
143
144                         ic.ID = null;
145                         Assert.IsNull (ic.ID, "ID-3");
146                         Assert.IsNull (ic.UniqueID, "UniqueID-3");
147                         Assert.IsNull (ic.Name, "Name-2");
148                 }
149
150                 [Test]
151                 public void Name_InsideNaming ()
152                 {
153                         Control ctrl = new UControl ();
154                         ctrl.ID = "parent";
155                         TestHtmlInputControl ic = new TestHtmlInputControl ();
156                         ctrl.Controls.Add (ic);
157                         Assert.IsNull (ic.ID, "ID");
158                         Assert.AreEqual (false, ic.NameCalled);
159                         ic.Name = "name";
160                         Assert.AreEqual (ic.Name, ic.UniqueID, "name and unique id");
161                         Assert.AreEqual (true, ic.NameCalled, "name called");
162
163                         ic.ID = "id";
164                         Assert.AreEqual ("id", ic.ID, "ID-2");
165                         Assert.AreEqual (ic.UniqueID, ic.Name, "Name-ID");
166
167                         ic.Name = "name";
168                         Assert.AreEqual (ic.Name, ic.UniqueID, "UniqueID-2");
169
170                         ic.ID = null;
171                         Assert.IsNull (ic.ID, "ID-3");
172                         Assert.IsNotNull (ic.UniqueID, "UniqueID-3");
173                         Assert.IsNotNull (ic.Name, "Name-2");
174                 }
175
176                 [Test]
177                 public void IDversusValue ()
178                 {
179                         TestHtmlInputControl ic = new TestHtmlInputControl ();
180                         Assert.AreEqual (String.Empty, ic.Value, "Value before");
181                         ic.ID = "id1";
182                         Assert.AreEqual ("id1", ic.ID, "ID");
183                         Assert.AreEqual (String.Empty, ic.Value, "Value after");
184                         // HtmlInputRadioButton has a different behaviour
185                 }
186
187                 [Test]
188                 public void RenderAttributes ()
189                 {
190                         TestHtmlInputControl ic = new TestHtmlInputControl ("test");
191                         ic.Name = "mono";
192                         ic.Value = "value";
193
194                         HtmlDiff.AssertAreEqual ("<dummy name type=\"test\" value=\"value\" />", ic.RenderAttributes (), "RenderAttributes failed #1");
195
196                         ic.ID = "toto";
197                         HtmlDiff.AssertAreEqual ("<dummy name=\"toto\" id=\"toto\" type=\"test\" value=\"value\" />", ic.RenderAttributes (), "RenderAttributes failed #2");
198                 }
199
200                 [Test]
201                 public void Constructor_Null ()
202                 {
203                         TestHtmlInputControl ic = new TestHtmlInputControl (null);
204                         Assert.AreEqual (String.Empty, ic.Type, "Type");
205                 }
206
207                 [Test]
208                 public void Password ()
209                 {
210                         TestHtmlInputControl ic = new TestHtmlInputControl ("password");
211                         ic.Name = "mono";
212                         ic.Value = "s3kr3t";
213
214                         // logic to hide password isn't in HtmlInputControl
215                         HtmlDiff.AssertAreEqual ("<dummy name type=\"password\" value=\"s3kr3t\" />", ic.RenderAttributes (), "Password failed");
216                 }
217         }
218 }