2
0

ImageButtonTest.cs 11 KB

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