HtmlInputButtonTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. p.CausesValidation = false;
  89. p.ValidationGroup = "VG";
  90. object s = p.SaveState();
  91. HtmlInputButtonPoker copy = new HtmlInputButtonPoker ();
  92. copy.LoadState (s);
  93. Assert.IsFalse (copy.CausesValidation, "A1");
  94. Assert.AreEqual ("VG", p.ValidationGroup, "A2");
  95. }
  96. [Test]
  97. public void RenderAttributes ()
  98. {
  99. StringWriter sw = new StringWriter ();
  100. HtmlTextWriter tw = new HtmlTextWriter (sw);
  101. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  102. p.Page = new Page ();
  103. p.CausesValidation = false;
  104. p.ValidationGroup = "VG";
  105. Assert.AreEqual (3, p.Attributes.Count, "A1");
  106. tw.WriteBeginTag ("dummy");
  107. p.DoRenderAttributes (tw);
  108. tw.Write ('>');
  109. HtmlDiff.AssertAreEqual ("<dummy name type=\"button\" ValidationGroup=\"VG\" />", sw.ToString (), "A2");
  110. }
  111. [Test]
  112. public void OnClickAttribute ()
  113. {
  114. StringWriter sw = new StringWriter ();
  115. HtmlTextWriter tw = new HtmlTextWriter (sw);
  116. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  117. p.Page = new Page ();
  118. p.DoRenderAttributes (tw);
  119. string str = sw.ToString ();
  120. int found = str.IndexOf ("onclick");
  121. Assert.AreEqual (-1, found, "#01");
  122. p.ServerClick += new EventHandler (EmptyHandler);
  123. sw = new StringWriter ();
  124. tw = new HtmlTextWriter (sw);
  125. p.DoRenderAttributes (tw);
  126. str = sw.ToString ();
  127. found = str.IndexOf ("onclick");
  128. Assert.IsTrue (found >= 0, "#02");
  129. }
  130. [Test]
  131. public void OnClickAttributeWithSpecials ()
  132. {
  133. string origHtml = "alert(&#39;&lt;&amp;&#39;);";
  134. string origHtml2 = "alert('<&');";
  135. StringWriter sw = new StringWriter ();
  136. HtmlTextWriter tw = new HtmlTextWriter (sw);
  137. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  138. p.Page = new Page ();
  139. p.Attributes["onclick"] = "alert('<&');";
  140. p.DoRenderAttributes (tw);
  141. string str = sw.ToString ();
  142. int found = str.IndexOf (origHtml);
  143. Assert.IsTrue (found >= 0, "#01");
  144. p.ServerClick += new EventHandler (EmptyHandler);
  145. sw = new StringWriter ();
  146. tw = new HtmlTextWriter (sw);
  147. p.DoRenderAttributes (tw);
  148. str = sw.ToString ();
  149. found = str.IndexOf (origHtml2);
  150. Assert.IsTrue (found >= 0, "#02" + str);
  151. }
  152. private static void EmptyHandler (object sender, EventArgs e)
  153. {
  154. }
  155. [Test]
  156. public void RenderOnclick1 ()
  157. {
  158. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
  159. it.ID = "id1";
  160. it.ServerClick += new EventHandler (EmptyHandler);
  161. string rendered = it.RenderToString ();
  162. Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
  163. }
  164. [Test]
  165. public void RenderOnclick2 ()
  166. {
  167. Page page = new Page ();
  168. page.EnableEventValidation = false;
  169. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
  170. page.Controls.Add (it);
  171. it.ID = "id1";
  172. it.ServerClick += new EventHandler (EmptyHandler);
  173. string rendered = it.RenderToString ();
  174. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  175. }
  176. [Test]
  177. public void RenderOnclick3 ()
  178. {
  179. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  180. it.ID = "id1";
  181. it.ServerClick += new EventHandler (EmptyHandler);
  182. string rendered = it.RenderToString ();
  183. Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
  184. }
  185. [Test]
  186. [Category ("NotWorking")]
  187. public void RenderOnclick4 ()
  188. {
  189. Page page = new Page ();
  190. page.EnableEventValidation = false;
  191. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  192. page.Controls.Add (it);
  193. it.ID = "id1";
  194. it.ServerClick += new EventHandler (EmptyHandler);
  195. string rendered = it.RenderToString ();
  196. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  197. Assert.IsTrue (rendered.IndexOf ("__doPostBack") != -1, "#02");
  198. Assert.IsTrue (rendered.IndexOf ("type=\"submit\"") != -1, "#03");
  199. }
  200. [Test]
  201. public void RenderOnclick5 ()
  202. {
  203. Page page = new Page ();
  204. page.EnableEventValidation = false;
  205. RequiredFieldValidator val = new RequiredFieldValidator ();
  206. val.ControlToValidate = "id1";
  207. page.Validators.Add (val);
  208. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  209. page.Controls.Add (it);
  210. it.ID = "id1";
  211. it.ServerClick += new EventHandler (EmptyHandler);
  212. string rendered = it.RenderToString ();
  213. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  214. }
  215. }
  216. }