ImageButtonTest.cs 10 KB

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