2
0

BaseValidatorTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // Tests for System.Web.UI.WebControls.BaseValidator
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. //
  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. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Drawing;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.ComponentModel;
  36. using System.Web.UI.HtmlControls;
  37. using System.Web.UI.WebControls;
  38. using Alternate;
  39. namespace Alternate {
  40. [ValidationProperty ("SomeProperty")]
  41. class MyTextBox : ICustomTypeDescriptor {
  42. public string SomeProperty {
  43. get { return null; }
  44. set {}
  45. }
  46. public System.ComponentModel.AttributeCollection GetAttributes ()
  47. {
  48. // This one is called and then GetProperties
  49. //Console.WriteLine ("GetAttributes ");
  50. return TypeDescriptor.GetAttributes (this, true);
  51. }
  52. public string GetClassName()
  53. {
  54. //Console.WriteLine ("GetClassName");
  55. return TypeDescriptor.GetClassName (this, true);
  56. }
  57. public string GetComponentName()
  58. {
  59. //Console.WriteLine ("GetComponentName");
  60. return TypeDescriptor.GetComponentName (this, true);
  61. }
  62. public TypeConverter GetConverter()
  63. {
  64. //Console.WriteLine ("GetConverter");
  65. return TypeDescriptor.GetConverter (this, true);
  66. }
  67. public EventDescriptor GetDefaultEvent()
  68. {
  69. //Console.WriteLine ("GetDefaultEvent");
  70. return TypeDescriptor.GetDefaultEvent (this, true);
  71. }
  72. public PropertyDescriptor GetDefaultProperty()
  73. {
  74. //Console.WriteLine ("GetDefaultProperty");
  75. return TypeDescriptor.GetDefaultProperty (this, true);
  76. }
  77. public object GetEditor(Type editorBaseType)
  78. {
  79. //Console.WriteLine ("GetEditor (editorBaseType");
  80. return null;
  81. }
  82. public EventDescriptorCollection GetEvents()
  83. {
  84. //Console.WriteLine ("GetEvents");
  85. return TypeDescriptor.GetEvents (this, true);
  86. }
  87. public EventDescriptorCollection GetEvents(Attribute[] arr)
  88. {
  89. //Console.WriteLine ("GetEvents");
  90. return TypeDescriptor.GetEvents (arr, true);
  91. }
  92. public PropertyDescriptorCollection GetProperties()
  93. {
  94. //Console.WriteLine ("GetProperties");
  95. return TypeDescriptor.GetProperties (this, true);
  96. }
  97. public PropertyDescriptorCollection GetProperties(Attribute[] arr)
  98. {
  99. //Console.WriteLine ("GetProperties");
  100. return TypeDescriptor.GetProperties (this, arr, true);
  101. }
  102. public object GetPropertyOwner(PropertyDescriptor pd)
  103. {
  104. //Console.WriteLine ("GetPropertyOwner (pd)");
  105. return null;
  106. }
  107. }
  108. }
  109. namespace MonoTests.System.Web.UI.WebControls
  110. {
  111. class BaseValidatorPoker : BaseValidator {
  112. public BaseValidatorPoker ()
  113. {
  114. TrackViewState ();
  115. }
  116. public object SaveState ()
  117. {
  118. return SaveViewState ();
  119. }
  120. public void LoadState (object o)
  121. {
  122. LoadViewState (o);
  123. }
  124. public void CheckControlProperties ()
  125. {
  126. ControlPropertiesValid ();
  127. }
  128. protected override bool EvaluateIsValid ()
  129. {
  130. return IsValid;
  131. }
  132. public new PropertyDescriptor GetValidationProperty (object o)
  133. {
  134. return BaseValidator.GetValidationProperty (o);
  135. }
  136. public string DoGetControlValidationValue (string name)
  137. {
  138. return GetControlValidationValue (name);
  139. }
  140. public virtual bool DoControlPropertiesValid ()
  141. {
  142. return ControlPropertiesValid ();
  143. }
  144. public void DoCheckControlValidationProperty (string name, string propertyName)
  145. {
  146. CheckControlValidationProperty (name, propertyName);
  147. }
  148. }
  149. [TestFixture]
  150. public class BaseValidatorTest : ValidatorTest {
  151. [Test]
  152. public void ViewState ()
  153. {
  154. BaseValidatorPoker p = new BaseValidatorPoker ();
  155. /* defaults */
  156. Assert.AreEqual (String.Empty, p.ControlToValidate, "D1");
  157. Assert.AreEqual (ValidatorDisplay.Static, p.Display, "D2");
  158. Assert.AreEqual (true, p.EnableClientScript, "D3");
  159. Assert.AreEqual (true, p.Enabled, "D4");
  160. Assert.AreEqual (String.Empty, p.ErrorMessage, "D5");
  161. Assert.AreEqual (Color.Red, p.ForeColor, "D6");
  162. Assert.AreEqual (true, p.IsValid, "D7");
  163. /* get/set */
  164. p.ControlToValidate = "foo";
  165. Assert.AreEqual ("foo", p.ControlToValidate, "D8");
  166. p.Display = ValidatorDisplay.Dynamic;
  167. Assert.AreEqual (ValidatorDisplay.Dynamic, p.Display, "D9");
  168. p.EnableClientScript = false;
  169. Assert.AreEqual (false, p.EnableClientScript, "D9");
  170. p.Enabled = false;
  171. Assert.AreEqual (false, p.Enabled, "D10");
  172. p.ErrorMessage = "stupid monkey";
  173. Assert.AreEqual ("stupid monkey", p.ErrorMessage, "D11");
  174. p.ForeColor = Color.Blue;
  175. Assert.AreEqual (Color.Blue, p.ForeColor, "D12");
  176. //XXX add check to see if setting the color alters the style at all.
  177. p.IsValid = false;
  178. Assert.AreEqual (false, p.IsValid, "D13");
  179. }
  180. [Test]
  181. public void ValidationProperty ()
  182. {
  183. BaseValidatorPoker p = new BaseValidatorPoker ();
  184. PropertyDescriptor d;
  185. StartValidationTest (p);
  186. TextBox box = AddTextBox ("textbox", "hello world");
  187. d = p.GetValidationProperty (box);
  188. Assert.AreEqual ("Text", d.Name, "A1");
  189. Assert.AreEqual ("hello world", p.DoGetControlValidationValue ("textbox"), "A2");
  190. StopValidationTest ();
  191. }
  192. [Test]
  193. public void ControlPropertiesValid1 ()
  194. {
  195. BaseValidatorPoker p = new BaseValidatorPoker ();
  196. StartValidationTest (p);
  197. TextBox box = SetValidationTextBox ("textbox", "hello world");
  198. Assert.IsTrue (p.DoControlPropertiesValid (), "B1");
  199. StopValidationTest ();
  200. }
  201. [Test]
  202. [ExpectedException (typeof (HttpException))]
  203. public void ControlPropertiesValid2 ()
  204. {
  205. BaseValidatorPoker p = new BaseValidatorPoker ();
  206. StartValidationTest (p);
  207. TextBox box = AddTextBox ("textbox", "hello world");
  208. /* successful */
  209. p.DoCheckControlValidationProperty ("textbox", "Text");
  210. /* failure (exception) due to unknown control */
  211. p.DoCheckControlValidationProperty ("textbox2", "Text");
  212. StopValidationTest ();
  213. }
  214. [Test]
  215. [ExpectedException (typeof (HttpException))]
  216. public void ControlPropertiesValid3 ()
  217. {
  218. BaseValidatorPoker p = new BaseValidatorPoker ();
  219. StartValidationTest (p);
  220. p.ControlToValidate = "textbox";
  221. /* failure (exception) due to unknown control */
  222. p.DoCheckControlValidationProperty ("textbox", "Text2");
  223. StopValidationTest ();
  224. }
  225. [Test]
  226. [ExpectedException(typeof(HttpException))]
  227. public void BasicExceptionTest () {
  228. BaseValidatorPoker p = new BaseValidatorPoker ();
  229. p.CheckControlProperties();
  230. }
  231. [Test]
  232. public void GetControlValidationValue ()
  233. {
  234. BaseValidatorPoker p = new BaseValidatorPoker ();
  235. StartValidationTest (p);
  236. TextBox box = AddTextBox ("textbox", "hello world");
  237. Label label = new Label ();
  238. label.ID = "label";
  239. Page.Controls.Add (label);
  240. /* successful */
  241. Assert.AreEqual ("hello world", p.DoGetControlValidationValue ("textbox"), "C1");
  242. /* failure (non-existant control)*/
  243. Assert.IsNull (p.DoGetControlValidationValue ("textbox2"), "C2");
  244. /* failure (control without a ValidationProperty */
  245. Assert.IsNull (p.DoGetControlValidationValue ("label"), "C3");
  246. StopValidationTest ();
  247. }
  248. [Test]
  249. public void CustomDescriptor ()
  250. {
  251. PropertyDescriptor pd = BaseValidator.GetValidationProperty (new MyTextBox ());
  252. Assert.AreEqual ("SomeProperty", pd.Name);
  253. }
  254. [Test]
  255. public void NoCustomDescriptor ()
  256. {
  257. PropertyDescriptor pd = BaseValidator.GetValidationProperty (new TextBox ());
  258. Assert.AreEqual ("Text", pd.Name);
  259. }
  260. }
  261. }