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. #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]
  157. [Category ("NotWorking")]
  158. public void PostBackUrl ()
  159. {
  160. Page p = new Page ();
  161. PokerImageButton b = new PokerImageButton ();
  162. p.Controls.Add (b);
  163. p.EnableEventValidation = false;
  164. b.PostBackUrl = "~/MyURL.aspx";
  165. string html = b.Render ();
  166. if (html.IndexOf ("onclick") == -1)
  167. Assert.Fail ("PostBack script not created");
  168. if (html.IndexOf ("~/MyURL.aspx") == -1)
  169. Assert.Fail ("PostBack page URL not set");
  170. }
  171. [Test]
  172. [Category ("NunitWeb")]
  173. public void ValidationGroup ()
  174. {
  175. WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
  176. WebTest t = new WebTest ("NoEventValidation.aspx");
  177. t.Invoker = PageInvoker.CreateOnLoad (ValidationGroup_Load);
  178. t.Run ();
  179. }
  180. public static void ValidationGroup_Load(Page p)
  181. {
  182. PokerImageButton b = new PokerImageButton ();
  183. b.ValidationGroup = "MyValidationGroup";
  184. TextBox tb = new TextBox ();
  185. tb.ID = "tb";
  186. tb.ValidationGroup = "MyValidationGroup";
  187. RequiredFieldValidator v = new RequiredFieldValidator ();
  188. v.ControlToValidate = "tb";
  189. v.ValidationGroup = "MyValidationGroup";
  190. p.Controls.Add (tb);
  191. p.Controls.Add (v);
  192. p.Controls.Add (b);
  193. string html = b.Render ();
  194. if (html.IndexOf ("onclick") == -1)
  195. Assert.Fail ("Validation script not created");
  196. if (html.IndexOf ("MyValidationGroup") == -1)
  197. Assert.Fail ("Validation group not set fail");
  198. }
  199. [Test]
  200. public void Text ()
  201. {
  202. PokerImageButton b = new PokerImageButton ();
  203. b.Text = "MyText";
  204. string html = b.Render ();
  205. HtmlDiff.AssertAreEqual ("<input type=\"image\" src=\"\" alt=\"MyText\" style=\"border-width:0px;\" />", html, "Text#1");
  206. }
  207. [Test]
  208. [Category ("NotWorking")]
  209. [Category("NunitWeb")]
  210. public void RaisePostBackEvent ()
  211. {
  212. WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
  213. WebTest t = new WebTest ("NoEventValidation.aspx");
  214. t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
  215. t.Run ();
  216. ArrayList eventlist = t.UserData as ArrayList;
  217. if (eventlist == null)
  218. Assert.Fail ("User data does not been created fail");
  219. Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
  220. Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
  221. }
  222. public static void RaisePostBackEvent_Load (Page p)
  223. {
  224. PokerImageButton b = new PokerImageButton ();
  225. p.Form.Controls.Add (b);
  226. b.Click += new ImageClickEventHandler (b_Click);
  227. b.Command += new CommandEventHandler (b_Command);
  228. b.RaisePostBackEvent ("Click");
  229. }
  230. static void b_Command (object sender, CommandEventArgs e)
  231. {
  232. if (WebTest.CurrentTest.UserData == null) {
  233. ArrayList list = new ArrayList ();
  234. list.Add ("Command");
  235. WebTest.CurrentTest.UserData = list;
  236. }
  237. else {
  238. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  239. if (list == null)
  240. throw new NullReferenceException ();
  241. list.Add ("Command");
  242. WebTest.CurrentTest.UserData = list;
  243. }
  244. }
  245. static void b_Click (object sender, ImageClickEventArgs e)
  246. {
  247. if (WebTest.CurrentTest.UserData == null) {
  248. ArrayList list = new ArrayList ();
  249. list.Add ("Click");
  250. WebTest.CurrentTest.UserData = list;
  251. }
  252. else {
  253. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  254. if (list == null)
  255. throw new NullReferenceException ();
  256. list.Add ("Click");
  257. WebTest.CurrentTest.UserData = list;
  258. }
  259. }
  260. [Test]
  261. public void RaisePostDataChangedEvent ()
  262. {
  263. // This protected method empty for to be ovveriden by developer
  264. // and not called by Page and Control lifecycle
  265. }
  266. #endif
  267. [Test]
  268. public void ImageButton_Render ()
  269. {
  270. StringWriter sw = new StringWriter ();
  271. HtmlTextWriter tw = new HtmlTextWriter (sw);
  272. ImageButton b = new ImageButton ();
  273. b.RenderControl (tw);
  274. Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
  275. Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
  276. }
  277. [Test]
  278. public void ImageButton_ViewState ()
  279. {
  280. PokerImageButton p = new PokerImageButton ();
  281. p.CommandArgument = "arg";
  282. Assert.AreEqual (p.CommandArgument, "arg", "A1");
  283. p.CommandName = "cmd";
  284. Assert.AreEqual (p.CommandName, "cmd", "A2");
  285. #if NET_2_0
  286. p.ValidationGroup = "VG1";
  287. Assert.AreEqual (p.ValidationGroup, "VG1", "A3");
  288. #endif
  289. object state = p.SaveState ();
  290. PokerImageButton copy = new PokerImageButton ();
  291. copy.LoadState (state);
  292. Assert.AreEqual (copy.CommandArgument, "arg", "A4");
  293. Assert.AreEqual (copy.CommandName, "cmd", "A5");
  294. #if NET_2_0
  295. Assert.AreEqual (copy.ValidationGroup, "VG1", "A6");
  296. #endif
  297. }
  298. [Test]
  299. public void RenderName ()
  300. {
  301. StringWriter sw = new StringWriter ();
  302. HtmlTextWriter tw = new HtmlTextWriter (sw);
  303. Page page = new Page ();
  304. #if NET_2_0
  305. page.EnableEventValidation = false;
  306. #endif
  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. #if NET_2_0
  315. [Test]
  316. [Category ("NotWorking")] // Not implemented
  317. [ExpectedException (typeof (NotSupportedException))]
  318. public void GenerateEmptyAlternateText_Exception ()
  319. {
  320. ImageButton b = new ImageButton ();
  321. b.GenerateEmptyAlternateText = true;
  322. }
  323. [TestFixtureTearDown]
  324. public void TearDown ()
  325. {
  326. WebTest.Unload ();
  327. }
  328. #endif
  329. }
  330. }