2007-01-30 Adar Wesley <adarw@mainsofot.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / CustomValidatorTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.CustomValidator
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //      Klain Yoni          (yonik@mainsoft.com)
7 //
8
9 //
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Collections;
35 using System.Drawing;
36 using System.IO;
37 using System.Globalization;
38 using System.Web;
39 using System.Web.UI;
40 using System.Web.UI.HtmlControls;
41 using System.Web.UI.WebControls;
42 using MonoTests.stand_alone.WebHarness;
43
44 namespace MonoTests.System.Web.UI.WebControls
45 {
46         [TestFixture]   
47         public class CustomValidatorTest : ValidatorTest {
48                 private bool    bool_result;
49
50                 public class CustomValidatorTestClass : CustomValidator {
51                         public CustomValidatorTestClass () {
52                                 TrackViewState();
53                         }
54
55                         public bool AreControlPropertiesValid ()
56                         {
57                                 return ControlPropertiesValid ();
58                         }
59
60                         public object SaveState () {
61                                 return SaveViewState ();
62                         }
63
64                         public void LoadState (object o) {
65                                 LoadViewState (o);
66                         }
67
68                         public void SetTrackingVS () {
69                                 TrackViewState ();
70                         }
71
72                         public void CheckProperties () {
73                                 ControlPropertiesValid ();
74                         }
75
76                         public bool DoEvaluateIsValid () {
77                                 return EvaluateIsValid ();
78                         }
79
80                         protected new bool RenderUplevel {
81                                 get {
82                                         return true;
83                                 }
84                         }
85
86                         public void CallInit() {
87                                 base.OnInit(EventArgs.Empty);
88                         }
89
90                         public bool Evaluate() {
91                                 return base.EvaluateIsValid();
92                         }
93
94                         public string Render () {
95                                 HtmlTextWriter  writer;
96
97                                 writer = CustomValidatorTest.GetWriter();
98                                 base.Render (writer);
99                                 return writer.InnerWriter.ToString ();
100                         }
101                 }
102
103                 private static HtmlTextWriter GetWriter () {
104                         StringWriter sw = new StringWriter ();
105                         sw.NewLine = "\n";
106                         return new HtmlTextWriter (sw);
107                 }
108
109                 private void ServerValidateMethod(object sender, ServerValidateEventArgs e) {
110                         bool_result = e.IsValid;
111                 }
112
113                 [Test]
114                 public void EventDefaults ()
115                 {
116                         CustomValidatorTestClass        c;
117                         TextBox                         t;
118
119                         c = new CustomValidatorTestClass();
120
121                         Assert.AreEqual(true, c.Evaluate(), "E1");
122                         Assert.AreEqual(false, bool_result, "E2");
123
124                         c.ServerValidate += new ServerValidateEventHandler(ServerValidateMethod);
125                         bool_result = false;
126
127                         Assert.AreEqual(true, c.Evaluate(), "E3");
128                         Assert.AreEqual(true, bool_result, "E4");
129
130                         StartValidationTest(c);
131                         t = SetValidationTextBox("textbox", "3");
132                         bool_result = false;
133
134                         Assert.AreEqual(true, c.Evaluate(), "E5");
135                         Assert.AreEqual(true, bool_result, "E6");
136                 }
137
138                 [Test]
139                 public void Defaults () {
140                         CustomValidatorTestClass        c;
141
142                         c= new CustomValidatorTestClass();
143                         Assert.AreEqual(string.Empty, c.ClientValidationFunction , "D1");
144
145                         c.ClientValidationFunction = "Hurra, hurra, die Schule brennt";
146                         Assert.AreEqual("Hurra, hurra, die Schule brennt", c.ClientValidationFunction , "D1");
147                 }
148
149                 [Test]
150                 public void DefaultsNotWorking ()
151                 {
152                         CustomValidatorTestClass c = new CustomValidatorTestClass ();
153 #if NET_2_0
154                         Assert.AreEqual (false, c.ValidateEmptyText, "ValidateEmptyText");
155 #endif
156                 }
157
158
159 #if NET_2_0
160                 // Variable for checking events 
161                 private bool checker;
162
163                 [Test]
164                 public void ValidateEmptyText ()
165                 {
166                         CustomValidatorTestClass c = new CustomValidatorTestClass ();
167                         Page p = new Page ();
168                         TextBox tb = new TextBox ();
169                         tb.ID = "tb";
170                         
171                         CustomValidator v = new CustomValidator ();
172                         v.ServerValidate += new ServerValidateEventHandler (ServerValidate);
173                         v.ControlToValidate = "tb";
174                         p.Controls.Add (tb);
175                         p.Controls.Add (v);
176
177                         v.Validate ();
178                         Assert.AreEqual (false, checker, "Validate with ValidateEmptyText = false");
179                         v.ValidateEmptyText = true;
180                         v.Validate ();
181                         Assert.AreEqual (true, checker, "Validate with ValidateEmptyText = true");
182                 }
183
184                 void ServerValidate (object source, ServerValidateEventArgs value)
185                 {
186                         checker = true;
187                 }
188 #endif
189
190
191                 [Test]
192                 public void Render () {
193                         CustomValidatorTestClass        c;
194                         TextBox                         t;
195
196                         c = new CustomValidatorTestClass();
197                         StartValidationTest(c);
198                         t = SetValidationTextBox("textbox", "3");
199                         c.Validate();
200                         c.ErrorMessage = "aw shucks";
201                         c.Display = ValidatorDisplay.Static;
202                         c.Enabled = true;
203                         c.EnableViewState = true;
204
205 #if! NET_2_0
206                         Assert.AreEqual("<span style=\"color:Red;\">aw shucks</span>", c.Render(), "R1");
207 #endif
208
209                         c.ClientValidationFunction = "Father to a sister of thought";
210 #if! NET_2_0
211                         Assert.AreEqual("<span style=\"color:Red;\">aw shucks</span>", c.Render(), "R2");
212 #endif
213                 }
214
215 #if NET_2_0
216                 class Poker : CustomValidator
217                 {
218                         public string Render ()
219                         {
220                                 HtmlTextWriter writer;
221                                 writer = CustomValidatorTest.GetWriter ();
222                                 base.Render (writer);
223                                 return writer.InnerWriter.ToString ();
224                         }
225                 }
226
227                 [Test]
228                 public void Render_2_0 ()
229                 {
230                         Poker c = new Poker();
231                         c.ErrorMessage = "aw shucks";
232                         c.Display = ValidatorDisplay.Static;
233                         c.Enabled = true;
234                         c.EnableViewState = true;
235                         string html = c.Render ();
236                         HtmlDiff.AssertAreEqual ("<span style=\"color:Red;\">aw shucks</span>", html, "Render#1");
237                 }
238
239                 
240 #endif
241
242                 [Test]
243                 public void EmptyControlName ()
244                 {
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");
251                 }
252         }
253 }