HtmlInputButtonTest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // HtmlInputButtonTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlInputButton
  4. //
  5. // Author:
  6. // Jackson Harper ([email protected])
  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 System;
  30. using System.IO;
  31. using System.Web.UI;
  32. using System.Web.UI.HtmlControls;
  33. using System.Web.UI.WebControls;
  34. using MonoTests.stand_alone.WebHarness;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Web.UI.HtmlControls {
  37. public class HtmlInputButtonPoker : HtmlInputButton {
  38. public HtmlInputButtonPoker ()
  39. {
  40. TrackViewState ();
  41. }
  42. public HtmlInputButtonPoker (string type) : base (type)
  43. {
  44. TrackViewState ();
  45. }
  46. public object SaveState ()
  47. {
  48. return SaveViewState ();
  49. }
  50. public void LoadState (object state)
  51. {
  52. LoadViewState (state);
  53. }
  54. public void DoRenderAttributes (HtmlTextWriter writer)
  55. {
  56. RenderAttributes (writer);
  57. }
  58. public string RenderToString ()
  59. {
  60. StringWriter sr = new StringWriter ();
  61. RenderAttributes (new HtmlTextWriter (sr));
  62. return sr.ToString ();
  63. }
  64. }
  65. [TestFixture]
  66. public class HtmlInputButtonTest {
  67. [Test]
  68. public void Defaults ()
  69. {
  70. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  71. Assert.IsTrue (p.CausesValidation, "A1");
  72. }
  73. [Test]
  74. public void CleanProperties ()
  75. {
  76. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  77. p.CausesValidation = false;
  78. Assert.IsFalse (p.CausesValidation, "A1");
  79. p.CausesValidation = true;
  80. Assert.IsTrue (p.CausesValidation, "A2");
  81. p.CausesValidation = false;
  82. Assert.IsFalse (p.CausesValidation, "A3");
  83. }
  84. [Test]
  85. public void ViewState ()
  86. {
  87. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  88. #if NET_2_0
  89. p.CausesValidation = false;
  90. p.ValidationGroup = "VG";
  91. #endif
  92. object s = p.SaveState();
  93. HtmlInputButtonPoker copy = new HtmlInputButtonPoker ();
  94. copy.LoadState (s);
  95. #if NET_2_0
  96. Assert.IsFalse (copy.CausesValidation, "A1");
  97. Assert.AreEqual ("VG", p.ValidationGroup, "A2");
  98. #endif
  99. }
  100. [Test]
  101. public void RenderAttributes ()
  102. {
  103. StringWriter sw = new StringWriter ();
  104. HtmlTextWriter tw = new HtmlTextWriter (sw);
  105. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  106. p.Page = new Page ();
  107. p.CausesValidation = false;
  108. #if NET_2_0
  109. p.ValidationGroup = "VG";
  110. Assert.AreEqual (3, p.Attributes.Count, "A1");
  111. #else
  112. Assert.AreEqual (2, p.Attributes.Count, "A1");
  113. #endif
  114. p.DoRenderAttributes (tw);
  115. #if NET_2_0
  116. HtmlDiff.AssertAreEqual (" name type=\"button\" ValidationGroup=\"VG\" /", sw.ToString (), "A2");
  117. #else
  118. HtmlDiff.AssertAreEqual (" name type=\"button\" /", sw.ToString (), "A2");
  119. #endif
  120. }
  121. [Test]
  122. public void OnClickAttribute ()
  123. {
  124. StringWriter sw = new StringWriter ();
  125. HtmlTextWriter tw = new HtmlTextWriter (sw);
  126. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  127. p.Page = new Page ();
  128. p.DoRenderAttributes (tw);
  129. string str = sw.ToString ();
  130. int found = str.IndexOf ("onclick");
  131. Assert.AreEqual (-1, found, "#01");
  132. p.ServerClick += new EventHandler (EmptyHandler);
  133. sw = new StringWriter ();
  134. tw = new HtmlTextWriter (sw);
  135. p.DoRenderAttributes (tw);
  136. str = sw.ToString ();
  137. found = str.IndexOf ("onclick");
  138. Assert.IsTrue (found >= 0, "#02");
  139. }
  140. private static void EmptyHandler (object sender, EventArgs e)
  141. {
  142. }
  143. [Test]
  144. public void RenderOnclick1 ()
  145. {
  146. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
  147. it.ID = "id1";
  148. it.ServerClick += new EventHandler (EmptyHandler);
  149. string rendered = it.RenderToString ();
  150. Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
  151. }
  152. [Test]
  153. public void RenderOnclick2 ()
  154. {
  155. Page page = new Page ();
  156. #if NET_2_0
  157. page.EnableEventValidation = false;
  158. #endif
  159. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
  160. page.Controls.Add (it);
  161. it.ID = "id1";
  162. it.ServerClick += new EventHandler (EmptyHandler);
  163. string rendered = it.RenderToString ();
  164. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  165. }
  166. [Test]
  167. public void RenderOnclick3 ()
  168. {
  169. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  170. it.ID = "id1";
  171. it.ServerClick += new EventHandler (EmptyHandler);
  172. string rendered = it.RenderToString ();
  173. Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
  174. }
  175. [Test]
  176. [Category ("NotWorking")]
  177. public void RenderOnclick4 ()
  178. {
  179. Page page = new Page ();
  180. #if NET_2_0
  181. page.EnableEventValidation = false;
  182. #endif
  183. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  184. page.Controls.Add (it);
  185. it.ID = "id1";
  186. it.ServerClick += new EventHandler (EmptyHandler);
  187. string rendered = it.RenderToString ();
  188. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  189. Assert.IsTrue (rendered.IndexOf ("__doPostBack") != -1, "#02");
  190. Assert.IsTrue (rendered.IndexOf ("type=\"submit\"") != -1, "#03");
  191. }
  192. [Test]
  193. public void RenderOnclick5 ()
  194. {
  195. Page page = new Page ();
  196. #if NET_2_0
  197. page.EnableEventValidation = false;
  198. #endif
  199. RequiredFieldValidator val = new RequiredFieldValidator ();
  200. val.ControlToValidate = "id1";
  201. page.Validators.Add (val);
  202. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  203. page.Controls.Add (it);
  204. it.ID = "id1";
  205. it.ServerClick += new EventHandler (EmptyHandler);
  206. string rendered = it.RenderToString ();
  207. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  208. }
  209. }
  210. }