2
0

CustomValidatorTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Tests for System.Web.UI.WebControls.CustomValidator
  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.HtmlControls;
  38. using System.Web.UI.WebControls;
  39. namespace MonoTests.System.Web.UI.WebControls
  40. {
  41. [TestFixture]
  42. public class CustomValidatorTest : ValidatorTest {
  43. private bool bool_result;
  44. public class CustomValidatorTestClass : CustomValidator {
  45. public CustomValidatorTestClass () {
  46. TrackViewState();
  47. }
  48. public bool AreControlPropertiesValid ()
  49. {
  50. return ControlPropertiesValid ();
  51. }
  52. public object SaveState () {
  53. return SaveViewState ();
  54. }
  55. public void LoadState (object o) {
  56. LoadViewState (o);
  57. }
  58. public void SetTrackingVS () {
  59. TrackViewState ();
  60. }
  61. public void CheckProperties () {
  62. ControlPropertiesValid ();
  63. }
  64. public bool DoEvaluateIsValid () {
  65. return EvaluateIsValid ();
  66. }
  67. protected new bool RenderUplevel {
  68. get {
  69. return true;
  70. }
  71. }
  72. public void CallInit() {
  73. base.OnInit(EventArgs.Empty);
  74. }
  75. public bool Evaluate() {
  76. return base.EvaluateIsValid();
  77. }
  78. public string Render () {
  79. HtmlTextWriter writer;
  80. writer = CustomValidatorTest.GetWriter();
  81. base.Render (writer);
  82. return writer.InnerWriter.ToString ();
  83. }
  84. }
  85. private static HtmlTextWriter GetWriter () {
  86. StringWriter sw = new StringWriter ();
  87. sw.NewLine = "\n";
  88. return new HtmlTextWriter (sw);
  89. }
  90. private void ServerValidateMethod(object sender, ServerValidateEventArgs e) {
  91. bool_result = e.IsValid;
  92. }
  93. [Test]
  94. public void EventDefaults ()
  95. {
  96. CustomValidatorTestClass c;
  97. TextBox t;
  98. c = new CustomValidatorTestClass();
  99. Assert.AreEqual(true, c.Evaluate(), "E1");
  100. Assert.AreEqual(false, bool_result, "E2");
  101. c.ServerValidate += new ServerValidateEventHandler(ServerValidateMethod);
  102. bool_result = false;
  103. Assert.AreEqual(true, c.Evaluate(), "E3");
  104. Assert.AreEqual(true, bool_result, "E4");
  105. StartValidationTest(c);
  106. t = SetValidationTextBox("textbox", "3");
  107. bool_result = false;
  108. Assert.AreEqual(true, c.Evaluate(), "E5");
  109. Assert.AreEqual(true, bool_result, "E6");
  110. }
  111. [Test]
  112. public void Defaults () {
  113. CustomValidatorTestClass c;
  114. c= new CustomValidatorTestClass();
  115. Assert.AreEqual(string.Empty, c.ClientValidationFunction , "D1");
  116. c.ClientValidationFunction = "Hurra, hurra, die Schule brennt";
  117. Assert.AreEqual("Hurra, hurra, die Schule brennt", c.ClientValidationFunction , "D1");
  118. }
  119. [Test]
  120. public void Render () {
  121. CustomValidatorTestClass c;
  122. TextBox t;
  123. c = new CustomValidatorTestClass();
  124. StartValidationTest(c);
  125. t = SetValidationTextBox("textbox", "3");
  126. c.Validate();
  127. c.ErrorMessage = "aw shucks";
  128. c.Display = ValidatorDisplay.Static;
  129. c.Enabled = true;
  130. c.EnableViewState = true;
  131. #if NET_2_0
  132. Assert.AreEqual(" ", c.Render(), "R1");
  133. #else
  134. Assert.AreEqual("<span style=\"color:Red;\">aw shucks</span>", c.Render(), "R1");
  135. #endif
  136. c.ClientValidationFunction = "Father to a sister of thought";
  137. #if NET_2_0
  138. Assert.AreEqual("&nbsp;", c.Render(), "R2");
  139. #else
  140. Assert.AreEqual("<span style=\"color:Red;\">aw shucks</span>", c.Render(), "R2");
  141. #endif
  142. }
  143. [Test]
  144. public void EmptyControlName ()
  145. {
  146. Page page = new Page ();
  147. HtmlForm form = new HtmlForm ();
  148. CustomValidatorTestClass tc = new CustomValidatorTestClass ();
  149. page.Controls.Add (form);
  150. form.Controls.Add (tc);
  151. Assert.IsTrue (tc.AreControlPropertiesValid (), "#01");
  152. }
  153. }
  154. }