ValidationSummaryTest.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // Tests for System.Web.UI.WebControls.ValidationSummary
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. [TestFixture]
  41. public class ValidationSummaryTest : ValidatorTest {
  42. public class NamingContainer : WebControl, INamingContainer {
  43. }
  44. public class ValidationSummaryTestClass : ValidationSummary {
  45. public ValidationSummaryTestClass ()
  46. : base () {
  47. }
  48. public StateBag StateBag {
  49. get { return base.ViewState; }
  50. }
  51. public string Render () {
  52. HtmlTextWriter writer;
  53. writer = ValidationSummaryTest.GetWriter();
  54. base.Render (writer);
  55. return writer.InnerWriter.ToString ();
  56. }
  57. public bool IsTrackingVS () {
  58. return IsTrackingViewState;
  59. }
  60. public void SetTrackingVS () {
  61. TrackViewState ();
  62. }
  63. public object Save() {
  64. return base.SaveViewState();
  65. }
  66. public void Load(object o) {
  67. base.LoadViewState(o);
  68. }
  69. public void CallInit() {
  70. base.OnInit(EventArgs.Empty);
  71. }
  72. public new void RenderContents(HtmlTextWriter writer) {
  73. base.RenderContents(writer);
  74. }
  75. public new void CreateControlCollection() {
  76. base.CreateControlCollection();
  77. }
  78. public new void AddAttributesToRender(HtmlTextWriter writer) {
  79. base.AddAttributesToRender(writer);
  80. }
  81. public string[] KeyValuePairs() {
  82. IEnumerator e;
  83. string[] result;
  84. int item;
  85. e = ViewState.GetEnumerator();
  86. result = new string[ViewState.Keys.Count];
  87. item = 0;
  88. while (e.MoveNext()) {
  89. DictionaryEntry d;
  90. StateItem si;
  91. d = (DictionaryEntry)e.Current;
  92. si = (StateItem)d.Value;
  93. if (si.Value is String[]) {
  94. string[] values;
  95. values = (string[]) si.Value;
  96. result[item] = d.Key.ToString() + "=";
  97. if (values.Length > 0) {
  98. result[item] += values[0];
  99. for (int i = 1; i < values.Length; i++) {
  100. result[item] += ", " + values[i];
  101. }
  102. }
  103. } else {
  104. result[item] = d.Key.ToString() + "=" + si.Value;
  105. }
  106. item++;
  107. }
  108. return result;
  109. }
  110. }
  111. private static HtmlTextWriter GetWriter () {
  112. StringWriter sw = new StringWriter ();
  113. sw.NewLine = "\n";
  114. return new HtmlTextWriter (sw);
  115. }
  116. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  117. int matches;
  118. bool[] notfound;
  119. if (a1.Length != a2.Length) {
  120. if (assertion != null) {
  121. Assert.Fail(assertion + "( different length )");
  122. }
  123. return false;
  124. }
  125. matches = 0;
  126. notfound = new bool[a1.Length];
  127. for (int i = 0; i < a1.Length; i++) {
  128. for (int j = 0; j < a2.Length; j++) {
  129. if (a1[i].Equals(a2[j])) {
  130. matches++;
  131. break;
  132. }
  133. }
  134. if ((assertion != null) && (matches != i+1)) {
  135. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  136. }
  137. }
  138. return matches == a1.Length;
  139. }
  140. [Test]
  141. public void ValidationSummary_Defaults () {
  142. ValidationSummaryTestClass v = new ValidationSummaryTestClass ();
  143. Assert.AreEqual (ValidationSummaryDisplayMode.BulletList, v.DisplayMode, "D1");
  144. Assert.AreEqual (true, v.EnableClientScript, "D2");
  145. Assert.AreEqual (Color.Red, v.ForeColor, "D3");
  146. Assert.AreEqual (string.Empty, v.HeaderText, "D4");
  147. Assert.AreEqual (true, v.ShowSummary, "D5");
  148. }
  149. #if NET_2_0
  150. [Test]
  151. public void ValidationSummary_ValidationGroup () {
  152. ValidationSummaryTestClass v = new ValidationSummaryTestClass ();
  153. v.SetTrackingVS();
  154. Assert.AreEqual ("", v.ValidationGroup, "VG1");
  155. v.ValidationGroup = "group";
  156. Assert.AreEqual ("group", v.ValidationGroup, "VG2");
  157. /* make sure ValidationGroup is stored in the view state */
  158. object state = v.Save ();
  159. ValidationSummaryTestClass v2 = new ValidationSummaryTestClass ();
  160. v2.SetTrackingVS();
  161. v2.Load (state);
  162. Assert.AreEqual ("group", v2.ValidationGroup, "VG3");
  163. }
  164. #endif
  165. [Test]
  166. public void ValidationSummaryRenderTest () {
  167. ValidationSummaryTestClass v;
  168. RangeValidatorTest.RangeValidatorTestClass p;
  169. RangeValidatorTest.RangeValidatorTestClass p2;
  170. TextBox t1;
  171. TextBox t2;
  172. v = new ValidationSummaryTestClass ();
  173. p = new RangeValidatorTest.RangeValidatorTestClass();
  174. v.HeaderText = "I am the header text";
  175. StartValidationTest(p);
  176. p.SetTrackingVS();
  177. p.Type = ValidationDataType.Integer;
  178. p.MinimumValue = "2";
  179. p.MaximumValue = "4";
  180. p.ErrorMessage = "aw shucks";
  181. p.Enabled = true;
  182. p.EnableViewState = true;
  183. p.CallInit();
  184. p.ID = "moep";
  185. t1 = SetValidationTextBox("textbox", "1");
  186. Assert.AreEqual(false, p.DoEvaluateIsValid(), "R1");
  187. p2 = new RangeValidatorTest.RangeValidatorTestClass();
  188. Page.Controls.Add(p2);
  189. p2.SetTrackingVS();
  190. p2.Type = ValidationDataType.Integer;
  191. p2.MinimumValue = "6";
  192. p2.MaximumValue = "7";
  193. p2.ErrorMessage = "WhamBamThankYouMam";
  194. p2.Enabled = true;
  195. p2.EnableViewState = true;
  196. p2.CallInit();
  197. p2.ID = "moep2";
  198. t2 = this.AddTextBox("textbox2", "2");
  199. p2.ControlToValidate = "textbox2";
  200. p.Validate();
  201. p2.Validate();
  202. Page.Controls.Add(v);
  203. // Default DisplayMode
  204. 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");
  205. v.DisplayMode = ValidationSummaryDisplayMode.BulletList;
  206. 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");
  207. v.DisplayMode = ValidationSummaryDisplayMode.List;
  208. #if NET_2_0
  209. Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text<br />aw shucks<br />WhamBamThankYouMam<br />\n</div>", v.Render(), "R4");
  210. #else
  211. Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text<br>aw shucks<br>WhamBamThankYouMam<br>\n</div>", v.Render(), "R4");
  212. #endif
  213. v.DisplayMode = ValidationSummaryDisplayMode.SingleParagraph;
  214. #if NET_2_0
  215. Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br />\n</div>", v.Render(), "R5");
  216. #else
  217. Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br>\n</div>", v.Render(), "R5");
  218. #endif
  219. v.ShowSummary = false;
  220. v.DisplayMode = ValidationSummaryDisplayMode.BulletList;
  221. Assert.AreEqual("", v.Render(), "R6");
  222. v.ShowSummary = true;
  223. v.EnableClientScript = true;
  224. v.ShowMessageBox = true;
  225. v.DisplayMode = ValidationSummaryDisplayMode.SingleParagraph;
  226. #if NET_2_0
  227. Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br />\n</div>", v.Render(), "R7");
  228. #else
  229. Assert.AreEqual("<div style=\"color:Red;\">\n\tI am the header text aw shucks WhamBamThankYouMam <br>\n</div>", v.Render(), "R7");
  230. #endif
  231. StopValidationTest();
  232. }
  233. }
  234. }