New test.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / BaseValidatorTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.BaseValidator 
3 //
4 // Author:
5 //      Chris Toshok (toshok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Drawing;
35 using System.Web;
36 using System.Web.UI;
37 using System.ComponentModel;
38 using System.Web.UI.HtmlControls;
39 using System.Web.UI.WebControls;
40 using Alternate;
41
42 namespace Alternate {
43         [ValidationProperty ("SomeProperty")]
44         class MyTextBox : ICustomTypeDescriptor {
45                 public string SomeProperty {
46                         get { return null; }
47                         set {}
48                 }
49
50                 public System.ComponentModel.AttributeCollection GetAttributes ()
51                 {
52                         // This one is called and then GetProperties
53                         //Console.WriteLine ("GetAttributes ");
54                         return TypeDescriptor.GetAttributes  (this, true);
55                 }
56
57                 public string GetClassName()
58                 {
59                         //Console.WriteLine ("GetClassName");
60                         return TypeDescriptor.GetClassName (this, true);
61                 }
62
63                 public string GetComponentName()
64                 {
65                         //Console.WriteLine ("GetComponentName");
66                         return TypeDescriptor.GetComponentName (this, true);
67                 }
68
69                 public TypeConverter GetConverter()
70                 {
71                         //Console.WriteLine ("GetConverter");
72                         return TypeDescriptor.GetConverter (this, true);
73                 }
74
75                 public EventDescriptor GetDefaultEvent()
76                 {
77                         //Console.WriteLine ("GetDefaultEvent");
78                         return TypeDescriptor.GetDefaultEvent (this, true);
79                 }
80
81                 public PropertyDescriptor GetDefaultProperty()
82                 {
83                         //Console.WriteLine ("GetDefaultProperty");
84                         return TypeDescriptor.GetDefaultProperty (this, true);
85                 }
86
87                 public object GetEditor(Type editorBaseType)
88                 {
89                         //Console.WriteLine ("GetEditor (editorBaseType");
90                         return null;
91                 }
92
93                 public EventDescriptorCollection GetEvents()
94                 {
95                         //Console.WriteLine ("GetEvents");
96                         return TypeDescriptor.GetEvents (this, true);
97                 }
98
99                 public EventDescriptorCollection GetEvents(Attribute[] arr)
100                 {
101                         //Console.WriteLine ("GetEvents");
102                         return TypeDescriptor.GetEvents (arr, true);
103                 }
104
105                 public PropertyDescriptorCollection GetProperties()
106                 {
107                         //Console.WriteLine ("GetProperties");
108                         return TypeDescriptor.GetProperties (this, true);
109                 }
110
111                 public PropertyDescriptorCollection GetProperties(Attribute[] arr)
112                 {
113                         //Console.WriteLine ("GetProperties");
114                         return TypeDescriptor.GetProperties (this, arr, true);
115                 }
116
117                 public object GetPropertyOwner(PropertyDescriptor pd)
118                 {
119                         //Console.WriteLine ("GetPropertyOwner (pd)");
120                         return null;
121                 }
122         }
123 }
124
125 namespace MonoTests.System.Web.UI.WebControls
126 {
127         class BaseValidatorPoker : BaseValidator {
128                 public BaseValidatorPoker ()
129                 {
130                         TrackViewState (); 
131                 }
132                 
133                 public object SaveState ()
134                 {
135                         return SaveViewState ();
136                 }
137
138                 public void LoadState (object o)
139                 {
140                         LoadViewState (o);
141                 }
142
143                 public void CheckControlProperties ()
144                 {
145                         ControlPropertiesValid ();
146                 }
147
148                 protected override bool EvaluateIsValid ()
149                 {
150                         return IsValid;
151                 }
152
153                 public new PropertyDescriptor GetValidationProperty (object o)
154                 {
155                         return BaseValidator.GetValidationProperty (o);
156                 }
157
158                 public string DoGetControlValidationValue (string name)
159                 {
160                         return GetControlValidationValue (name);
161                 }
162
163                 public virtual bool DoControlPropertiesValid ()
164                 {
165                         return ControlPropertiesValid ();
166                 }
167           
168                 public void DoCheckControlValidationProperty (string name, string propertyName)
169                 {
170                         CheckControlValidationProperty (name, propertyName);
171                 }
172         }
173
174         [TestFixture]   
175         public class BaseValidatorTest : ValidatorTest {
176
177                 [Test]
178                 public void ViewState ()
179                 {
180                         BaseValidatorPoker p = new BaseValidatorPoker ();
181
182                         /* defaults */
183                         Assert.AreEqual (String.Empty, p.ControlToValidate, "D1");
184                         Assert.AreEqual (ValidatorDisplay.Static, p.Display, "D2");
185                         Assert.AreEqual (true, p.EnableClientScript, "D3");
186                         Assert.AreEqual (true, p.Enabled, "D4");
187                         Assert.AreEqual (String.Empty, p.ErrorMessage, "D5");
188                         Assert.AreEqual (Color.Red, p.ForeColor, "D6");
189                         Assert.AreEqual (true, p.IsValid, "D7");
190
191                         /* get/set */
192                         p.ControlToValidate = "foo";
193                         Assert.AreEqual ("foo", p.ControlToValidate, "D8");
194
195                         p.Display = ValidatorDisplay.Dynamic;
196                         Assert.AreEqual (ValidatorDisplay.Dynamic, p.Display, "D9");
197
198                         p.EnableClientScript = false;
199                         Assert.AreEqual (false, p.EnableClientScript, "D9");
200
201                         p.Enabled = false;
202                         Assert.AreEqual (false, p.Enabled, "D10");
203
204                         p.ErrorMessage = "stupid monkey";
205                         Assert.AreEqual ("stupid monkey", p.ErrorMessage, "D11");
206
207                         p.ForeColor = Color.Blue;
208                         Assert.AreEqual (Color.Blue, p.ForeColor, "D12");
209                         //XXX add check to see if setting the color alters the style at all.
210
211                         p.IsValid = false;
212                         Assert.AreEqual (false, p.IsValid, "D13");
213                 }
214
215                 [Test]
216                 public void ValidationProperty ()
217                 {
218                         BaseValidatorPoker p = new BaseValidatorPoker ();
219                         PropertyDescriptor d;
220
221                         StartValidationTest (p);
222
223                         TextBox box = AddTextBox ("textbox", "hello world");
224
225                         d = p.GetValidationProperty (box);
226                         Assert.AreEqual ("Text", d.Name, "A1");
227                         Assert.AreEqual ("hello world", p.DoGetControlValidationValue ("textbox"), "A2");
228
229                         StopValidationTest ();
230                 }
231
232                 [Test]
233                 public void ControlPropertiesValid1 ()
234                 {
235                         BaseValidatorPoker p = new BaseValidatorPoker ();
236
237                         StartValidationTest (p);
238                         TextBox box = SetValidationTextBox ("textbox", "hello world");
239
240                         Assert.IsTrue (p.DoControlPropertiesValid (), "B1");
241
242                         StopValidationTest ();
243                 }
244
245                 [Test]
246                 [ExpectedException (typeof (HttpException))]
247                 public void ControlPropertiesValid2 ()
248                 {
249                         BaseValidatorPoker p = new BaseValidatorPoker ();
250
251                         StartValidationTest (p);
252                         TextBox box = AddTextBox ("textbox", "hello world");
253
254                         /* successful */
255                         p.DoCheckControlValidationProperty ("textbox", "Text");
256
257                         /* failure (exception) due to unknown control */
258                         p.DoCheckControlValidationProperty ("textbox2", "Text");
259
260                         StopValidationTest ();
261                 }
262
263                 [Test]
264                 [ExpectedException (typeof (HttpException))]
265                 public void ControlPropertiesValid3 ()
266                 {
267                         BaseValidatorPoker p = new BaseValidatorPoker ();
268
269                         StartValidationTest (p);
270
271                         p.ControlToValidate = "textbox";
272
273                         /* failure (exception) due to unknown control */
274                         p.DoCheckControlValidationProperty ("textbox", "Text2");
275
276                         StopValidationTest ();
277                 }
278
279
280                 [Test]
281                 [ExpectedException(typeof(HttpException))]
282                 public void BasicExceptionTest () {
283                         BaseValidatorPoker p = new BaseValidatorPoker ();
284
285                         p.CheckControlProperties();
286                 }
287
288
289                 [Test]
290                 public void GetControlValidationValue ()
291                 {
292                         BaseValidatorPoker p = new BaseValidatorPoker ();
293
294                         StartValidationTest (p);
295                         TextBox box = AddTextBox ("textbox", "hello world");
296                         Label label = new Label ();
297
298                         label.ID = "label";
299
300                         Page.Controls.Add (label);
301
302                         /* successful */
303                         Assert.AreEqual ("hello world", p.DoGetControlValidationValue ("textbox"), "C1");
304
305                         /* failure (non-existant control)*/
306                         Assert.IsNull (p.DoGetControlValidationValue ("textbox2"), "C2");
307
308                         /* failure (control without a ValidationProperty */
309                         Assert.IsNull (p.DoGetControlValidationValue ("label"), "C3");
310
311                         StopValidationTest ();
312                 }
313
314                 [Test]
315                 public void CustomDescriptor ()
316                 {
317                         PropertyDescriptor pd = BaseValidator.GetValidationProperty (new MyTextBox ());
318                         Assert.AreEqual ("SomeProperty", pd.Name);
319                 }
320
321                 [Test]
322                 public void NoCustomDescriptor ()
323                 {
324                         PropertyDescriptor pd = BaseValidator.GetValidationProperty (new TextBox ());
325                         Assert.AreEqual ("Text", pd.Name);
326                 }
327         }
328 }