HtmlInputButtonTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. tw.WriteBeginTag ("dummy");
  115. p.DoRenderAttributes (tw);
  116. tw.Write ('>');
  117. #if NET_2_0
  118. HtmlDiff.AssertAreEqual ("<dummy name type=\"button\" ValidationGroup=\"VG\" />", sw.ToString (), "A2");
  119. #else
  120. HtmlDiff.AssertAreEqual ("<dummy name type=\"button\" />", sw.ToString (), "A2");
  121. #endif
  122. }
  123. [Test]
  124. public void OnClickAttribute ()
  125. {
  126. StringWriter sw = new StringWriter ();
  127. HtmlTextWriter tw = new HtmlTextWriter (sw);
  128. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  129. p.Page = new Page ();
  130. p.DoRenderAttributes (tw);
  131. string str = sw.ToString ();
  132. int found = str.IndexOf ("onclick");
  133. Assert.AreEqual (-1, found, "#01");
  134. p.ServerClick += new EventHandler (EmptyHandler);
  135. sw = new StringWriter ();
  136. tw = new HtmlTextWriter (sw);
  137. p.DoRenderAttributes (tw);
  138. str = sw.ToString ();
  139. found = str.IndexOf ("onclick");
  140. Assert.IsTrue (found >= 0, "#02");
  141. }
  142. [Test]
  143. public void OnClickAttributeWithSpecials ()
  144. {
  145. #if NET_4_0
  146. string origHtml = "alert(&#39;&lt;&amp;&#39;);";
  147. string origHtml2 = "alert('<&');";
  148. #else
  149. string origHtml = "alert('&lt;&amp;');";
  150. string origHtml2 = "alert('<&');";
  151. #endif
  152. StringWriter sw = new StringWriter ();
  153. HtmlTextWriter tw = new HtmlTextWriter (sw);
  154. HtmlInputButtonPoker p = new HtmlInputButtonPoker ();
  155. p.Page = new Page ();
  156. p.Attributes["onclick"] = "alert('<&');";
  157. p.DoRenderAttributes (tw);
  158. string str = sw.ToString ();
  159. int found = str.IndexOf (origHtml);
  160. Assert.IsTrue (found >= 0, "#01");
  161. p.ServerClick += new EventHandler (EmptyHandler);
  162. sw = new StringWriter ();
  163. tw = new HtmlTextWriter (sw);
  164. p.DoRenderAttributes (tw);
  165. str = sw.ToString ();
  166. found = str.IndexOf (origHtml2);
  167. Assert.IsTrue (found >= 0, "#02" + str);
  168. }
  169. private static void EmptyHandler (object sender, EventArgs e)
  170. {
  171. }
  172. [Test]
  173. public void RenderOnclick1 ()
  174. {
  175. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
  176. it.ID = "id1";
  177. it.ServerClick += new EventHandler (EmptyHandler);
  178. string rendered = it.RenderToString ();
  179. Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
  180. }
  181. [Test]
  182. public void RenderOnclick2 ()
  183. {
  184. Page page = new Page ();
  185. #if NET_2_0
  186. page.EnableEventValidation = false;
  187. #endif
  188. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("button");
  189. page.Controls.Add (it);
  190. it.ID = "id1";
  191. it.ServerClick += new EventHandler (EmptyHandler);
  192. string rendered = it.RenderToString ();
  193. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  194. }
  195. [Test]
  196. public void RenderOnclick3 ()
  197. {
  198. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  199. it.ID = "id1";
  200. it.ServerClick += new EventHandler (EmptyHandler);
  201. string rendered = it.RenderToString ();
  202. Assert.IsTrue (rendered.IndexOf ("onclick") == -1, "#01");
  203. }
  204. [Test]
  205. [Category ("NotWorking")]
  206. public void RenderOnclick4 ()
  207. {
  208. Page page = new Page ();
  209. #if NET_2_0
  210. page.EnableEventValidation = false;
  211. #endif
  212. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  213. page.Controls.Add (it);
  214. it.ID = "id1";
  215. it.ServerClick += new EventHandler (EmptyHandler);
  216. string rendered = it.RenderToString ();
  217. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  218. Assert.IsTrue (rendered.IndexOf ("__doPostBack") != -1, "#02");
  219. Assert.IsTrue (rendered.IndexOf ("type=\"submit\"") != -1, "#03");
  220. }
  221. [Test]
  222. public void RenderOnclick5 ()
  223. {
  224. Page page = new Page ();
  225. #if NET_2_0
  226. page.EnableEventValidation = false;
  227. #endif
  228. RequiredFieldValidator val = new RequiredFieldValidator ();
  229. val.ControlToValidate = "id1";
  230. page.Validators.Add (val);
  231. HtmlInputButtonPoker it = new HtmlInputButtonPoker ("submit");
  232. page.Controls.Add (it);
  233. it.ID = "id1";
  234. it.ServerClick += new EventHandler (EmptyHandler);
  235. string rendered = it.RenderToString ();
  236. Assert.IsTrue (rendered.IndexOf ("onclick") != -1, "#01");
  237. }
  238. }
  239. }