2 // Tests for System.Web.UI.WebControls.CustomValidator
5 // Peter Dennis Bartok (pbartok@novell.com)
6 // Klain Yoni (yonik@mainsoft.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
34 using System.Collections;
37 using System.Globalization;
40 using System.Web.UI.HtmlControls;
41 using System.Web.UI.WebControls;
42 using MonoTests.stand_alone.WebHarness;
44 namespace MonoTests.System.Web.UI.WebControls
47 public class CustomValidatorTest : ValidatorTest {
48 private bool bool_result;
50 public class CustomValidatorTestClass : CustomValidator {
51 public CustomValidatorTestClass () {
55 public bool AreControlPropertiesValid ()
57 return ControlPropertiesValid ();
60 public object SaveState () {
61 return SaveViewState ();
64 public void LoadState (object o) {
68 public void SetTrackingVS () {
72 public void CheckProperties () {
73 ControlPropertiesValid ();
76 public bool DoEvaluateIsValid () {
77 return EvaluateIsValid ();
80 protected new bool RenderUplevel {
86 public void CallInit() {
87 base.OnInit(EventArgs.Empty);
90 public bool Evaluate() {
91 return base.EvaluateIsValid();
94 public string Render () {
95 HtmlTextWriter writer;
97 writer = CustomValidatorTest.GetWriter();
99 return writer.InnerWriter.ToString ();
103 private static HtmlTextWriter GetWriter () {
104 StringWriter sw = new StringWriter ();
106 return new HtmlTextWriter (sw);
109 private void ServerValidateMethod(object sender, ServerValidateEventArgs e) {
110 bool_result = e.IsValid;
114 public void EventDefaults ()
116 CustomValidatorTestClass c;
119 c = new CustomValidatorTestClass();
121 Assert.AreEqual(true, c.Evaluate(), "E1");
122 Assert.AreEqual(false, bool_result, "E2");
124 c.ServerValidate += new ServerValidateEventHandler(ServerValidateMethod);
127 Assert.AreEqual(true, c.Evaluate(), "E3");
128 Assert.AreEqual(true, bool_result, "E4");
130 StartValidationTest(c);
131 t = SetValidationTextBox("textbox", "3");
134 Assert.AreEqual(true, c.Evaluate(), "E5");
135 Assert.AreEqual(true, bool_result, "E6");
139 public void Defaults () {
140 CustomValidatorTestClass c;
142 c= new CustomValidatorTestClass();
143 Assert.AreEqual(string.Empty, c.ClientValidationFunction , "D1");
145 c.ClientValidationFunction = "Hurra, hurra, die Schule brennt";
146 Assert.AreEqual("Hurra, hurra, die Schule brennt", c.ClientValidationFunction , "D1");
150 public void DefaultsNotWorking ()
152 CustomValidatorTestClass c = new CustomValidatorTestClass ();
154 Assert.AreEqual (false, c.ValidateEmptyText, "ValidateEmptyText");
160 // Variable for checking events
161 private bool checker;
164 public void ValidateEmptyText ()
166 CustomValidatorTestClass c = new CustomValidatorTestClass ();
167 Page p = new Page ();
168 TextBox tb = new TextBox ();
171 CustomValidator v = new CustomValidator ();
172 v.ServerValidate += new ServerValidateEventHandler (ServerValidate);
173 v.ControlToValidate = "tb";
178 Assert.AreEqual (false, checker, "Validate with ValidateEmptyText = false");
179 v.ValidateEmptyText = true;
181 Assert.AreEqual (true, checker, "Validate with ValidateEmptyText = true");
184 void ServerValidate (object source, ServerValidateEventArgs value)
192 public void Render () {
193 CustomValidatorTestClass c;
196 c = new CustomValidatorTestClass();
197 StartValidationTest(c);
198 t = SetValidationTextBox("textbox", "3");
200 c.ErrorMessage = "aw shucks";
201 c.Display = ValidatorDisplay.Static;
203 c.EnableViewState = true;
206 Assert.AreEqual("<span style=\"color:Red;\">aw shucks</span>", c.Render(), "R1");
209 c.ClientValidationFunction = "Father to a sister of thought";
211 Assert.AreEqual("<span style=\"color:Red;\">aw shucks</span>", c.Render(), "R2");
216 class Poker : CustomValidator
218 public string Render ()
220 HtmlTextWriter writer;
221 writer = CustomValidatorTest.GetWriter ();
222 base.Render (writer);
223 return writer.InnerWriter.ToString ();
228 public void Render_2_0 ()
230 Poker c = new Poker();
231 c.ErrorMessage = "aw shucks";
232 c.Display = ValidatorDisplay.Static;
234 c.EnableViewState = true;
235 string html = c.Render ();
236 HtmlDiff.AssertAreEqual ("<span style=\"color:Red;\">aw shucks</span>", html, "Render#1");
243 public void EmptyControlName ()
245 Page page = new Page ();
246 HtmlForm form = new HtmlForm ();
247 CustomValidatorTestClass tc = new CustomValidatorTestClass ();
248 page.Controls.Add (form);
249 form.Controls.Add (tc);
250 Assert.IsTrue (tc.AreControlPropertiesValid (), "#01");