2 // Tests for System.Web.UI.WebControls.ValidationSummary
5 // Peter Dennis Bartok (pbartok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
33 using System.Collections;
36 using System.Globalization;
39 using System.Web.UI.WebControls;
41 namespace MonoTests.System.Web.UI.WebControls
44 public class ValidationSummaryTest : ValidatorTest {
45 public class NamingContainer : WebControl, INamingContainer {
49 public class ValidationSummaryTestClass : ValidationSummary {
51 public ValidationSummaryTestClass ()
55 public StateBag StateBag {
56 get { return base.ViewState; }
59 public string Render () {
60 HtmlTextWriter writer;
62 writer = ValidationSummaryTest.GetWriter();
64 return writer.InnerWriter.ToString ();
67 public bool IsTrackingVS () {
68 return IsTrackingViewState;
71 public void SetTrackingVS () {
75 public object Save() {
76 return base.SaveViewState();
79 public void Load(object o) {
80 base.LoadViewState(o);
83 public void CallInit() {
84 base.OnInit(EventArgs.Empty);
87 public new void RenderContents(HtmlTextWriter writer) {
88 base.RenderContents(writer);
91 public new void CreateControlCollection() {
92 base.CreateControlCollection();
95 public new void AddAttributesToRender(HtmlTextWriter writer) {
96 base.AddAttributesToRender(writer);
99 public string[] KeyValuePairs() {
104 e = ViewState.GetEnumerator();
105 result = new string[ViewState.Keys.Count];
108 while (e.MoveNext()) {
112 d = (DictionaryEntry)e.Current;
113 si = (StateItem)d.Value;
115 if (si.Value is String[]) {
118 values = (string[]) si.Value;
119 result[item] = d.Key.ToString() + "=";
120 if (values.Length > 0) {
121 result[item] += values[0];
123 for (int i = 1; i < values.Length; i++) {
124 result[item] += ", " + values[i];
128 result[item] = d.Key.ToString() + "=" + si.Value;
137 private static HtmlTextWriter GetWriter () {
138 StringWriter sw = new StringWriter ();
140 return new HtmlTextWriter (sw);
143 private bool IsEqual(object[] a1, object[] a2, string assertion) {
147 if (a1.Length != a2.Length) {
148 if (assertion != null) {
149 Assert.Fail(assertion + "( different length )");
155 notfound = new bool[a1.Length];
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])) {
164 if ((assertion != null) && (matches != i+1)) {
165 Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
169 return matches == a1.Length;
173 public void ValidationSummary_Defaults () {
174 ValidationSummaryTestClass v = new ValidationSummaryTestClass ();
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");
184 public void ValidationSummary_ValidationGroup () {
185 ValidationSummaryTestClass v = new ValidationSummaryTestClass ();
187 Assert.AreEqual ("", v.ValidationGroup, "VG1");
189 v.ValidationGroup = "group";
190 Assert.AreEqual ("group", v.ValidationGroup, "VG2");
192 /* make sure ValidationGroup is stored in the view state */
193 object state = v.Save ();
195 ValidationSummaryTestClass v2 = new ValidationSummaryTestClass ();
199 Assert.AreEqual ("group", v2.ValidationGroup, "VG3");
203 public void ValidationSummaryRenderTest () {
204 ValidationSummaryTestClass v;
205 RangeValidatorTest.RangeValidatorTestClass p;
206 RangeValidatorTest.RangeValidatorTestClass p2;
210 v = new ValidationSummaryTestClass ();
211 p = new RangeValidatorTest.RangeValidatorTestClass();
213 v.HeaderText = "I am the header text";
215 StartValidationTest(p);
217 p.Type = ValidationDataType.Integer;
218 p.MinimumValue = "2";
219 p.MaximumValue = "4";
220 p.ErrorMessage = "aw shucks";
222 p.EnableViewState = true;
226 t1 = SetValidationTextBox("textbox", "1");
227 Assert.AreEqual(false, p.DoEvaluateIsValid(), "R1");
229 p2 = new RangeValidatorTest.RangeValidatorTestClass();
230 Page.Controls.Add(p2);
232 p2.Type = ValidationDataType.Integer;
233 p2.MinimumValue = "6";
234 p2.MaximumValue = "7";
235 p2.ErrorMessage = "WhamBamThankYouMam";
237 p2.EnableViewState = true;
241 t2 = this.AddTextBox("textbox2", "2");
242 p2.ControlToValidate = "textbox2";
247 Page.Controls.Add(v);
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");
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");
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");
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");
261 v.ShowSummary = false;
262 v.DisplayMode = ValidationSummaryDisplayMode.BulletList;
263 Assert.AreEqual("", v.Render(), "R6");
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");
271 StopValidationTest();
274 public void SupportsDisabledAttribute ()
276 var ver40 = new Version (4, 0);
277 var ver35 = new Version (3, 5);
278 var p = new ValidationSummaryTestClass ();
279 Assert.AreEqual (ver40, p.RenderingCompatibility, "#A1-1");
280 Assert.IsFalse (p.SupportsDisabledAttribute, "#A1-2");
282 p.RenderingCompatibility = new Version (3, 5);
283 Assert.AreEqual (ver35, p.RenderingCompatibility, "#A2-1");
284 Assert.IsTrue (p.SupportsDisabledAttribute, "#A2-2");