ImageButtonTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // Tests for System.Web.UI.WebControls.ImageButton.cs
  3. //
  4. // Author:
  5. // Jordi Mas i Hernandez ([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.IO;
  32. using System.Globalization;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. using MonoTests.SystemWeb.Framework;
  37. using MonoTests.stand_alone.WebHarness;
  38. using System.Collections;
  39. namespace MonoTests.System.Web.UI.WebControls
  40. {
  41. class PokerImageButton : ImageButton {
  42. public PokerImageButton ()
  43. {
  44. TrackViewState ();
  45. }
  46. public object SaveState ()
  47. {
  48. return SaveViewState ();
  49. }
  50. public void LoadState (object o)
  51. {
  52. LoadViewState (o);
  53. }
  54. public string Render ()
  55. {
  56. StringWriter sw = new StringWriter ();
  57. sw.NewLine = "\n";
  58. HtmlTextWriter writer = new HtmlTextWriter (sw);
  59. base.Render (writer);
  60. return writer.InnerWriter.ToString ();
  61. }
  62. #if NET_2_0
  63. public new string Text
  64. {
  65. get
  66. {
  67. return base.Text;
  68. }
  69. set
  70. {
  71. base.Text = value;
  72. }
  73. }
  74. public new void RaisePostBackEvent (string eventArgument)
  75. {
  76. base.RaisePostBackEvent (eventArgument);
  77. }
  78. protected override void RaisePostDataChangedEvent ()
  79. {
  80. base.RaisePostDataChangedEvent ();
  81. }
  82. #endif
  83. }
  84. [TestFixture]
  85. public class ImageButtonTest {
  86. [Test]
  87. [Category("NotWorking")]
  88. public void ImageButton_DefaultValues ()
  89. {
  90. ImageButton b = new ImageButton ();
  91. Assert.AreEqual (true, b.CausesValidation, "CausesValidation");
  92. Assert.AreEqual (string.Empty, b.CommandArgument, "CommandArgument");
  93. Assert.AreEqual (string.Empty, b.CommandName, "CommandName");
  94. #if NET_2_0
  95. Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup");
  96. Assert.AreEqual (string.Empty, b.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory");
  97. Assert.AreEqual (string.Empty, b.DescriptionUrl, "DescriptionUrl");
  98. Assert.AreEqual (true, b.EnableTheming, "EnableTheming");
  99. Assert.AreEqual (false, b.GenerateEmptyAlternateText, "GenerateEmptyAlternateText");
  100. Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
  101. Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick");
  102. #endif
  103. }
  104. [Test]
  105. public void ImageButton_AssignedValues ()
  106. {
  107. ImageButton b = new ImageButton ();
  108. #if NET_2_0
  109. Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup#1");
  110. b.ValidationGroup = "test";
  111. Assert.AreEqual ("test", b.ValidationGroup, "ValidationGroup#2");
  112. // NOTE: Default is wrong!
  113. // Assert.AreEqual (string.Empty, b.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
  114. b.AppRelativeTemplateSourceDirectory = "~/test";
  115. Assert.AreEqual ("~/test", b.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
  116. Assert.AreEqual (string.Empty, b.DescriptionUrl, "DescriptionUrl#1");
  117. b.DescriptionUrl = "test";
  118. Assert.AreEqual ("test", b.DescriptionUrl, "DescriptionUrl#2");
  119. Assert.AreEqual (true, b.EnableTheming, "EnableTheming#1");
  120. b.EnableTheming = false;
  121. Assert.AreEqual (false, b.EnableTheming, "EnableTheming#2");
  122. #endif
  123. }
  124. #if NET_2_0
  125. [Test]
  126. [Category ("NunitWeb")]
  127. public void AppRelativeTemplateSourceDirectory ()
  128. {
  129. WebTest t = new WebTest (PageInvoker.CreateOnLoad (AppRelativeTemplateSourceDirectory_Load));
  130. t.Run ();
  131. }
  132. public static void AppRelativeTemplateSourceDirectory_Load (Page p)
  133. {
  134. PokerImageButton i = new PokerImageButton ();
  135. i.ID = "I";
  136. Assert.AreEqual ("~/", i.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory");
  137. }
  138. [Test]
  139. public void DescriptionUrl ()
  140. {
  141. PokerImageButton i = new PokerImageButton ();
  142. i.DescriptionUrl = "URLDescription";
  143. string html = i.Render ();
  144. if (html.IndexOf ("longdesc=\"URLDescription\"") == -1)
  145. Assert.Fail ("DescriptionUrl Failed");
  146. }
  147. [Test]
  148. public void OnClientClick ()
  149. {
  150. PokerImageButton b = new PokerImageButton ();
  151. b.OnClientClick = "MyMethod";
  152. string html = b.Render ();
  153. if (html.IndexOf ("onclick=\"MyMethod;\"") == -1)
  154. Assert.Fail ("OnClientClick#1");
  155. }
  156. [Test] // Bug #463939
  157. public void OnClientClickEmpty ()
  158. {
  159. PokerImageButton b = new PokerImageButton ();
  160. string html = b.Render ();
  161. Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A1");
  162. b.OnClientClick = String.Empty;
  163. html = b.Render ();
  164. Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A2");
  165. b.OnClientClick = null;
  166. html = b.Render ();
  167. Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A3");
  168. }
  169. [Test]
  170. [Category ("NunitWeb")]
  171. public void PostBackUrl ()
  172. {
  173. WebTest test = new WebTest (PageInvoker.CreateOnLoad (PostBackUrl_load));
  174. string html = HtmlDiff.GetControlFromPageHtml (test.Run ());
  175. if (html.IndexOf ("onclick") == -1)
  176. Assert.Fail ("PostBack script not created");
  177. if (html.IndexOf ("MyURL.aspx") == -1)
  178. Assert.Fail ("PostBack page URL not set");
  179. if (html.IndexOf ("~/MyURL.aspx") != -1)
  180. Assert.Fail ("PostBack page URL is not resolved");
  181. }
  182. public static void PostBackUrl_load (Page p)
  183. {
  184. PokerImageButton b = new PokerImageButton ();
  185. p.Form.Controls.Add (new LiteralControl(HtmlDiff.BEGIN_TAG));
  186. p.Form.Controls.Add (b);
  187. p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
  188. b.PostBackUrl = "~/MyURL.aspx";
  189. }
  190. [Test]
  191. [Category ("NunitWeb")]
  192. public void ValidationGroup ()
  193. {
  194. #if VISUAL_STUDIO
  195. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.NoEventValidation.aspx", "NoEventValidation.aspx");
  196. #else
  197. WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
  198. #endif
  199. WebTest t = new WebTest ("NoEventValidation.aspx");
  200. t.Invoker = PageInvoker.CreateOnLoad (ValidationGroup_Load);
  201. string html = HtmlDiff.GetControlFromPageHtml (t.Run ());
  202. if (html.IndexOf ("onclick") == -1)
  203. Assert.Fail ("Validation script not created");
  204. if (html.IndexOf ("MyValidationGroup") == -1)
  205. Assert.Fail ("Validation group not set fail");
  206. }
  207. public static void ValidationGroup_Load(Page p)
  208. {
  209. PokerImageButton b = new PokerImageButton ();
  210. b.ValidationGroup = "MyValidationGroup";
  211. TextBox tb = new TextBox ();
  212. tb.ID = "tb";
  213. tb.ValidationGroup = "MyValidationGroup";
  214. RequiredFieldValidator v = new RequiredFieldValidator ();
  215. v.ControlToValidate = "tb";
  216. v.ValidationGroup = "MyValidationGroup";
  217. p.Form.Controls.Add (tb);
  218. p.Form.Controls.Add (v);
  219. p.Form.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
  220. p.Form.Controls.Add (b);
  221. p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
  222. }
  223. [Test]
  224. public void Text ()
  225. {
  226. PokerImageButton b = new PokerImageButton ();
  227. b.Text = "MyText";
  228. string html = b.Render ();
  229. HtmlDiff.AssertAreEqual ("<input type=\"image\" src=\"\" alt=\"MyText\" style=\"border-width:0px;\" />", html, "Text#1");
  230. }
  231. [Test]
  232. [Category("NunitWeb")]
  233. public void RaisePostBackEvent ()
  234. {
  235. #if VISUAL_STUDIO
  236. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.NoEventValidation.aspx", "NoEventValidation.aspx");
  237. #else
  238. WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
  239. #endif
  240. WebTest t = new WebTest ("NoEventValidation.aspx");
  241. t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
  242. t.Run ();
  243. ArrayList eventlist = t.UserData as ArrayList;
  244. if (eventlist == null)
  245. Assert.Fail ("User data does not been created fail");
  246. Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
  247. Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
  248. }
  249. public static void RaisePostBackEvent_Load (Page p)
  250. {
  251. PokerImageButton b = new PokerImageButton ();
  252. p.Form.Controls.Add (b);
  253. b.Click += new ImageClickEventHandler (b_Click);
  254. b.Command += new CommandEventHandler (b_Command);
  255. b.RaisePostBackEvent ("Click");
  256. }
  257. static void b_Command (object sender, CommandEventArgs e)
  258. {
  259. if (WebTest.CurrentTest.UserData == null) {
  260. ArrayList list = new ArrayList ();
  261. list.Add ("Command");
  262. WebTest.CurrentTest.UserData = list;
  263. }
  264. else {
  265. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  266. if (list == null)
  267. throw new NullReferenceException ();
  268. list.Add ("Command");
  269. WebTest.CurrentTest.UserData = list;
  270. }
  271. }
  272. static void b_Click (object sender, ImageClickEventArgs e)
  273. {
  274. if (WebTest.CurrentTest.UserData == null) {
  275. ArrayList list = new ArrayList ();
  276. list.Add ("Click");
  277. WebTest.CurrentTest.UserData = list;
  278. }
  279. else {
  280. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  281. if (list == null)
  282. throw new NullReferenceException ();
  283. list.Add ("Click");
  284. WebTest.CurrentTest.UserData = list;
  285. }
  286. }
  287. #endif
  288. [Test]
  289. public void ImageButton_Render ()
  290. {
  291. StringWriter sw = new StringWriter ();
  292. HtmlTextWriter tw = new HtmlTextWriter (sw);
  293. ImageButton b = new ImageButton ();
  294. b.RenderControl (tw);
  295. Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
  296. Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
  297. }
  298. [Test]
  299. public void ImageButton_ViewState ()
  300. {
  301. PokerImageButton p = new PokerImageButton ();
  302. p.CommandArgument = "arg";
  303. Assert.AreEqual (p.CommandArgument, "arg", "A1");
  304. p.CommandName = "cmd";
  305. Assert.AreEqual (p.CommandName, "cmd", "A2");
  306. #if NET_2_0
  307. p.ValidationGroup = "VG1";
  308. Assert.AreEqual (p.ValidationGroup, "VG1", "A3");
  309. #endif
  310. object state = p.SaveState ();
  311. PokerImageButton copy = new PokerImageButton ();
  312. copy.LoadState (state);
  313. Assert.AreEqual (copy.CommandArgument, "arg", "A4");
  314. Assert.AreEqual (copy.CommandName, "cmd", "A5");
  315. #if NET_2_0
  316. Assert.AreEqual (copy.ValidationGroup, "VG1", "A6");
  317. #endif
  318. }
  319. [Test]
  320. public void RenderName ()
  321. {
  322. StringWriter sw = new StringWriter ();
  323. HtmlTextWriter tw = new HtmlTextWriter (sw);
  324. Page page = new Page ();
  325. #if NET_2_0
  326. page.EnableEventValidation = false;
  327. #endif
  328. ImageButton b = new ImageButton ();
  329. page.Controls.Add (b);
  330. page.RenderControl (tw);
  331. Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
  332. Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
  333. Assert.AreEqual (true, sw.ToString().IndexOf ("name=\"") != -1, "A3");
  334. }
  335. #if NET_2_0
  336. [Test]
  337. [ExpectedException (typeof (NotSupportedException))]
  338. public void GenerateEmptyAlternateText_Exception ()
  339. {
  340. ImageButton b = new ImageButton ();
  341. b.GenerateEmptyAlternateText = true;
  342. }
  343. [TestFixtureTearDown]
  344. public void TearDown ()
  345. {
  346. WebTest.Unload ();
  347. }
  348. #endif
  349. }
  350. }