CustomValidatorTest.cs 4.3 KB

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