[System] UriKind.RelativeOrAbsolute workaround.
[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                 [Test]
184                 public void ValidationSummary_ValidationGroup () {
185                         ValidationSummaryTestClass v = new ValidationSummaryTestClass ();
186                         v.SetTrackingVS();
187                         Assert.AreEqual ("", v.ValidationGroup, "VG1");
188
189                         v.ValidationGroup = "group";
190                         Assert.AreEqual ("group", v.ValidationGroup, "VG2");
191
192                         /* make sure ValidationGroup is stored in the view state */
193                         object state = v.Save ();
194
195                         ValidationSummaryTestClass v2 = new ValidationSummaryTestClass ();
196                         v2.SetTrackingVS();
197                         v2.Load (state);
198
199                         Assert.AreEqual ("group", v2.ValidationGroup, "VG3");
200                 }
201
202                 [Test]
203                 public void ValidationSummaryRenderTest () {
204                         ValidationSummaryTestClass      v;
205                         RangeValidatorTest.RangeValidatorTestClass              p;
206                         RangeValidatorTest.RangeValidatorTestClass              p2;
207                         TextBox                         t1;
208                         TextBox                         t2;
209
210                         v = new ValidationSummaryTestClass ();
211                         p = new RangeValidatorTest.RangeValidatorTestClass();
212
213                         v.HeaderText = "I am the header text";
214
215                         StartValidationTest(p);
216                         p.SetTrackingVS();
217                         p.Type = ValidationDataType.Integer;
218                         p.MinimumValue = "2";
219                         p.MaximumValue = "4";
220                         p.ErrorMessage = "aw shucks";
221                         p.Enabled = true;
222                         p.EnableViewState = true;
223                         p.CallInit();
224                         p.ID = "moep";
225
226                         t1 = SetValidationTextBox("textbox", "1");
227                         Assert.AreEqual(false, p.DoEvaluateIsValid(), "R1");
228
229                         p2 = new RangeValidatorTest.RangeValidatorTestClass();
230                         Page.Controls.Add(p2);
231                         p2.SetTrackingVS();
232                         p2.Type = ValidationDataType.Integer;
233                         p2.MinimumValue = "6";
234                         p2.MaximumValue = "7";
235                         p2.ErrorMessage = "WhamBamThankYouMam";
236                         p2.Enabled = true;
237                         p2.EnableViewState = true;
238                         p2.CallInit();
239                         p2.ID = "moep2";
240
241                         t2 = this.AddTextBox("textbox2", "2");
242                         p2.ControlToValidate = "textbox2";
243
244                         p.Validate();
245                         p2.Validate();
246
247                         Page.Controls.Add(v);
248
249                         // Default DisplayMode
250                         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");
251
252                         v.DisplayMode = ValidationSummaryDisplayMode.BulletList;
253                         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");
254
255                         v.DisplayMode = ValidationSummaryDisplayMode.List;
256                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text<br />aw shucks<br />WhamBamThankYouMam<br />\n</div>", v.Render(), "R4");
257
258                         v.DisplayMode = ValidationSummaryDisplayMode.SingleParagraph;
259                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br />\n</div>", v.Render(), "R5");
260
261                         v.ShowSummary = false;
262                         v.DisplayMode = ValidationSummaryDisplayMode.BulletList;
263                         Assert.AreEqual("", v.Render(), "R6");
264
265                         v.ShowSummary = true;
266                         v.EnableClientScript = true;
267                         v.ShowMessageBox = true;
268                         v.DisplayMode = ValidationSummaryDisplayMode.SingleParagraph;
269                         Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br />\n</div>", v.Render(), "R7");
270
271                         StopValidationTest();
272                 }
273 #if NET_4_0
274                 [Test]
275                 public void SupportsDisabledAttribute ()
276                 {
277                         var ver40 = new Version (4, 0);
278                         var ver35 = new Version (3, 5);
279                         var p = new ValidationSummaryTestClass ();
280                         Assert.AreEqual (ver40, p.RenderingCompatibility, "#A1-1");
281                         Assert.IsFalse (p.SupportsDisabledAttribute, "#A1-2");
282
283                         p.RenderingCompatibility = new Version (3, 5);
284                         Assert.AreEqual (ver35, p.RenderingCompatibility, "#A2-1");
285                         Assert.IsTrue (p.SupportsDisabledAttribute, "#A2-2");
286                 }
287 #endif
288         }
289 }