2007-02-04 yonik <yonik@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ValidationSummaryTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.ValidationSummary
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@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.Collections;
34 using System.Drawing;
35 using System.IO;
36 using System.Globalization;
37 using System.Web;
38 using System.Web.UI;
39 using System.Web.UI.WebControls;
40
41 namespace MonoTests.System.Web.UI.WebControls
42 {
43         [TestFixture]   
44         public class ValidationSummaryTest : ValidatorTest {
45                 public class NamingContainer : WebControl, INamingContainer {
46
47                 }
48
49                 public class ValidationSummaryTestClass : ValidationSummary {
50
51                         public ValidationSummaryTestClass ()
52                                 : base () {
53                         }
54
55                         public StateBag StateBag {
56                                 get { return base.ViewState; }
57                         }
58
59                         public string Render () {
60                                 HtmlTextWriter  writer;
61
62                                 writer = ValidationSummaryTest.GetWriter();
63                                 base.Render (writer);
64                                 return writer.InnerWriter.ToString ();
65                         }
66
67                         public bool IsTrackingVS () {
68                                 return IsTrackingViewState;
69                         }
70
71                         public void SetTrackingVS () {
72                                 TrackViewState ();
73                         }
74
75                         public object Save() {
76                                 return base.SaveViewState();
77                         }
78
79                         public void Load(object o) {
80                                 base.LoadViewState(o);
81                         }
82
83                         public void CallInit() {
84                                 base.OnInit(EventArgs.Empty);
85                         }
86         
87                         public new void RenderContents(HtmlTextWriter writer) {
88                                 base.RenderContents(writer);
89                         }
90
91                         public new void CreateControlCollection() {
92                                 base.CreateControlCollection();
93                         }
94
95                         public new void AddAttributesToRender(HtmlTextWriter writer) {
96                                 base.AddAttributesToRender(writer);
97                         }
98
99                         public string[] KeyValuePairs() {
100                                 IEnumerator     e;
101                                 string[]        result;
102                                 int             item;
103
104                                 e = ViewState.GetEnumerator();
105                                 result = new string[ViewState.Keys.Count];
106                                 item = 0;
107
108                                 while (e.MoveNext()) {
109                                         DictionaryEntry d;
110                                         StateItem       si;
111
112                                         d = (DictionaryEntry)e.Current;
113                                         si = (StateItem)d.Value;
114
115                                         if (si.Value is String[]) {
116                                                 string[] values;
117
118                                                 values = (string[]) si.Value;
119                                                 result[item] = d.Key.ToString() + "=";
120                                                 if (values.Length > 0) {
121                                                         result[item] += values[0];
122
123                                                         for (int i = 1; i < values.Length; i++) {
124                                                                 result[item] += ", " + values[i];
125                                                         }
126                                                 }
127                                         } else {
128                                                 result[item] =  d.Key.ToString() + "=" + si.Value;
129                                         }
130                                         item++;
131                                 }
132
133                                 return result;
134                         }
135                 }
136
137                 private static HtmlTextWriter GetWriter () {
138                         StringWriter sw = new StringWriter ();
139                         sw.NewLine = "\n";
140                         return new HtmlTextWriter (sw);
141                 }
142
143                 private bool IsEqual(object[] a1, object[] a2, string assertion) {
144                         int     matches;
145                         bool[]  notfound;       
146
147                         if (a1.Length != a2.Length) {
148                                 if (assertion != null) {
149                                         Assert.Fail(assertion + "( different length )");
150                                 }
151                                 return false;
152                         }
153
154                         matches = 0;
155                         notfound = new bool[a1.Length];
156
157                         for (int i = 0; i < a1.Length; i++) {
158                                 for (int j = 0; j < a2.Length; j++) {
159                                         if (a1[i].Equals(a2[j])) {
160                                                 matches++;
161                                                 break;
162                                         }
163                                 }
164                                 if ((assertion != null) && (matches != i+1)) {
165                                         Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
166                                 }
167                         }
168
169                         return matches == a1.Length;
170                 }
171
172                 [Test]
173                 public void ValidationSummary_Defaults () {
174                         ValidationSummaryTestClass v = new ValidationSummaryTestClass ();
175
176                         Assert.AreEqual (ValidationSummaryDisplayMode.BulletList, v.DisplayMode, "D1");
177                         Assert.AreEqual (true, v.EnableClientScript, "D2");
178                         Assert.AreEqual (Color.Red, v.ForeColor, "D3");
179                         Assert.AreEqual (string.Empty, v.HeaderText, "D4");
180                         Assert.AreEqual (true, v.ShowSummary, "D5");
181                 }
182
183 #if NET_2_0
184                 [Test]
185                 public void ValidationSummary_ValidationGroup () {
186                         ValidationSummaryTestClass v = new ValidationSummaryTestClass ();
187                         v.SetTrackingVS();
188                         Assert.AreEqual ("", v.ValidationGroup, "VG1");
189
190                         v.ValidationGroup = "group";
191                         Assert.AreEqual ("group", v.ValidationGroup, "VG2");
192
193                         /* make sure ValidationGroup is stored in the view state */
194                         object state = v.Save ();
195
196                         ValidationSummaryTestClass v2 = new ValidationSummaryTestClass ();
197                         v2.SetTrackingVS();
198                         v2.Load (state);
199
200                         Assert.AreEqual ("group", v2.ValidationGroup, "VG3");
201                 }
202 #endif
203
204                 [Test]
205                 public void ValidationSummaryRenderTest () {
206                         ValidationSummaryTestClass      v;
207                         RangeValidatorTest.RangeValidatorTestClass              p;
208                         RangeValidatorTest.RangeValidatorTestClass              p2;
209                         TextBox                         t1;
210                         TextBox                         t2;
211
212                         v = new ValidationSummaryTestClass ();
213                         p = new RangeValidatorTest.RangeValidatorTestClass();
214
215                         v.HeaderText = "I am the header text";
216
217                         StartValidationTest(p);
218                         p.SetTrackingVS();
219                         p.Type = ValidationDataType.Integer;
220                         p.MinimumValue = "2";
221                         p.MaximumValue = "4";
222                         p.ErrorMessage = "aw shucks";
223                         p.Enabled = true;
224                         p.EnableViewState = true;
225                         p.CallInit();
226                         p.ID = "moep";
227
228                         t1 = SetValidationTextBox("textbox", "1");
229                         Assert.AreEqual(false, p.DoEvaluateIsValid(), "R1");
230
231                         p2 = new RangeValidatorTest.RangeValidatorTestClass();
232                         Page.Controls.Add(p2);
233                         p2.SetTrackingVS();
234                         p2.Type = ValidationDataType.Integer;
235                         p2.MinimumValue = "6";
236                         p2.MaximumValue = "7";
237                         p2.ErrorMessage = "WhamBamThankYouMam";
238                         p2.Enabled = true;
239                         p2.EnableViewState = true;
240                         p2.CallInit();
241                         p2.ID = "moep2";
242
243                         t2 = this.AddTextBox("textbox2", "2");
244                         p2.ControlToValidate = "textbox2";
245
246                         p.Validate();
247                         p2.Validate();
248
249                         Page.Controls.Add(v);
250
251                         // Default DisplayMode
252                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text<ul><li>aw shucks</li><li>WhamBamThankYouMam</li></ul>\n</div>", v.Render(), "R2");
253
254                         v.DisplayMode = ValidationSummaryDisplayMode.BulletList;
255                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text<ul><li>aw shucks</li><li>WhamBamThankYouMam</li></ul>\n</div>", v.Render(), "R3");
256
257                         v.DisplayMode = ValidationSummaryDisplayMode.List;
258 #if NET_2_0
259                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text<br />aw shucks<br />WhamBamThankYouMam<br />\n</div>", v.Render(), "R4");
260 #else
261                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text<br>aw shucks<br>WhamBamThankYouMam<br>\n</div>", v.Render(), "R4");
262 #endif
263
264                         v.DisplayMode = ValidationSummaryDisplayMode.SingleParagraph;
265 #if NET_2_0
266                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br />\n</div>", v.Render(), "R5");
267 #else
268                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br>\n</div>", v.Render(), "R5");
269 #endif
270
271                         v.ShowSummary = false;
272                         v.DisplayMode = ValidationSummaryDisplayMode.BulletList;
273                         Assert.AreEqual("", v.Render(), "R6");
274
275                         v.ShowSummary = true;
276                         v.EnableClientScript = true;
277                         v.ShowMessageBox = true;
278                         v.DisplayMode = ValidationSummaryDisplayMode.SingleParagraph;
279 #if NET_2_0
280                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br />\n</div>", v.Render(), "R7");
281 #else
282                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br>\n</div>", v.Render(), "R7");
283 #endif
284
285                         StopValidationTest();
286                 }
287         }
288 }