2 // Tests for System.Web.UI.WebControls.BaseValidator
5 // Chris Toshok (toshok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
37 using System.ComponentModel;
38 using System.Web.UI.HtmlControls;
39 using System.Web.UI.WebControls;
43 [ValidationProperty ("SomeProperty")]
44 class MyTextBox : ICustomTypeDescriptor {
45 public string SomeProperty {
50 public System.ComponentModel.AttributeCollection GetAttributes ()
52 // This one is called and then GetProperties
53 //Console.WriteLine ("GetAttributes ");
54 return TypeDescriptor.GetAttributes (this, true);
57 public string GetClassName()
59 //Console.WriteLine ("GetClassName");
60 return TypeDescriptor.GetClassName (this, true);
63 public string GetComponentName()
65 //Console.WriteLine ("GetComponentName");
66 return TypeDescriptor.GetComponentName (this, true);
69 public TypeConverter GetConverter()
71 //Console.WriteLine ("GetConverter");
72 return TypeDescriptor.GetConverter (this, true);
75 public EventDescriptor GetDefaultEvent()
77 //Console.WriteLine ("GetDefaultEvent");
78 return TypeDescriptor.GetDefaultEvent (this, true);
81 public PropertyDescriptor GetDefaultProperty()
83 //Console.WriteLine ("GetDefaultProperty");
84 return TypeDescriptor.GetDefaultProperty (this, true);
87 public object GetEditor(Type editorBaseType)
89 //Console.WriteLine ("GetEditor (editorBaseType");
93 public EventDescriptorCollection GetEvents()
95 //Console.WriteLine ("GetEvents");
96 return TypeDescriptor.GetEvents (this, true);
99 public EventDescriptorCollection GetEvents(Attribute[] arr)
101 //Console.WriteLine ("GetEvents");
102 return TypeDescriptor.GetEvents (arr, true);
105 public PropertyDescriptorCollection GetProperties()
107 //Console.WriteLine ("GetProperties");
108 return TypeDescriptor.GetProperties (this, true);
111 public PropertyDescriptorCollection GetProperties(Attribute[] arr)
113 //Console.WriteLine ("GetProperties");
114 return TypeDescriptor.GetProperties (this, arr, true);
117 public object GetPropertyOwner(PropertyDescriptor pd)
119 //Console.WriteLine ("GetPropertyOwner (pd)");
125 namespace MonoTests.System.Web.UI.WebControls
127 class BaseValidatorPoker : BaseValidator {
128 public BaseValidatorPoker ()
133 public object SaveState ()
135 return SaveViewState ();
138 public void LoadState (object o)
143 public void CheckControlProperties ()
145 ControlPropertiesValid ();
148 protected override bool EvaluateIsValid ()
153 public new PropertyDescriptor GetValidationProperty (object o)
155 return BaseValidator.GetValidationProperty (o);
158 public string DoGetControlValidationValue (string name)
160 return GetControlValidationValue (name);
163 public virtual bool DoControlPropertiesValid ()
165 return ControlPropertiesValid ();
168 public void DoCheckControlValidationProperty (string name, string propertyName)
170 CheckControlValidationProperty (name, propertyName);
175 public class BaseValidatorTest : ValidatorTest {
178 public void ViewState ()
180 BaseValidatorPoker p = new BaseValidatorPoker ();
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");
192 p.ControlToValidate = "foo";
193 Assert.AreEqual ("foo", p.ControlToValidate, "D8");
195 p.Display = ValidatorDisplay.Dynamic;
196 Assert.AreEqual (ValidatorDisplay.Dynamic, p.Display, "D9");
198 p.EnableClientScript = false;
199 Assert.AreEqual (false, p.EnableClientScript, "D9");
202 Assert.AreEqual (false, p.Enabled, "D10");
204 p.ErrorMessage = "stupid monkey";
205 Assert.AreEqual ("stupid monkey", p.ErrorMessage, "D11");
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.
212 Assert.AreEqual (false, p.IsValid, "D13");
216 public void ValidationProperty ()
218 BaseValidatorPoker p = new BaseValidatorPoker ();
219 PropertyDescriptor d;
221 StartValidationTest (p);
223 TextBox box = AddTextBox ("textbox", "hello world");
225 d = p.GetValidationProperty (box);
226 Assert.AreEqual ("Text", d.Name, "A1");
227 Assert.AreEqual ("hello world", p.DoGetControlValidationValue ("textbox"), "A2");
229 StopValidationTest ();
233 public void ControlPropertiesValid1 ()
235 BaseValidatorPoker p = new BaseValidatorPoker ();
237 StartValidationTest (p);
238 TextBox box = SetValidationTextBox ("textbox", "hello world");
240 Assert.IsTrue (p.DoControlPropertiesValid (), "B1");
242 StopValidationTest ();
246 [ExpectedException (typeof (HttpException))]
247 public void ControlPropertiesValid2 ()
249 BaseValidatorPoker p = new BaseValidatorPoker ();
251 StartValidationTest (p);
252 TextBox box = AddTextBox ("textbox", "hello world");
255 p.DoCheckControlValidationProperty ("textbox", "Text");
257 /* failure (exception) due to unknown control */
258 p.DoCheckControlValidationProperty ("textbox2", "Text");
260 StopValidationTest ();
264 [ExpectedException (typeof (HttpException))]
265 public void ControlPropertiesValid3 ()
267 BaseValidatorPoker p = new BaseValidatorPoker ();
269 StartValidationTest (p);
271 p.ControlToValidate = "textbox";
273 /* failure (exception) due to unknown control */
274 p.DoCheckControlValidationProperty ("textbox", "Text2");
276 StopValidationTest ();
281 [ExpectedException(typeof(HttpException))]
282 public void BasicExceptionTest () {
283 BaseValidatorPoker p = new BaseValidatorPoker ();
285 p.CheckControlProperties();
290 public void GetControlValidationValue ()
292 BaseValidatorPoker p = new BaseValidatorPoker ();
294 StartValidationTest (p);
295 TextBox box = AddTextBox ("textbox", "hello world");
296 Label label = new Label ();
300 Page.Controls.Add (label);
303 Assert.AreEqual ("hello world", p.DoGetControlValidationValue ("textbox"), "C1");
305 /* failure (non-existant control)*/
306 Assert.IsNull (p.DoGetControlValidationValue ("textbox2"), "C2");
308 /* failure (control without a ValidationProperty */
309 Assert.IsNull (p.DoGetControlValidationValue ("label"), "C3");
311 StopValidationTest ();
315 public void CustomDescriptor ()
317 PropertyDescriptor pd = BaseValidator.GetValidationProperty (new MyTextBox ());
318 Assert.AreEqual ("SomeProperty", pd.Name);
322 public void NoCustomDescriptor ()
324 PropertyDescriptor pd = BaseValidator.GetValidationProperty (new TextBox ());
325 Assert.AreEqual ("Text", pd.Name);