PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web/Test/System.Web.UI.WebControls/LinkButtonTest.cs

https://bitbucket.org/danipen/mono
C# | 406 lines | 322 code | 52 blank | 32 comment | 20 complexity | 9f50721a2a96494ed407443658e14098 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // Tests for System.Web.UI.WebControls.LinkButton
  3. //
  4. // Author:
  5. // Miguel de Icaza (miguel@novell.com) [copied alot of this from his Label test]
  6. // Ben Maurer <bmaurer@novell.com>
  7. //
  8. //
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using NUnit.Framework;
  31. using System;
  32. using System.IO;
  33. using System.Globalization;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. using MonoTests.SystemWeb.Framework;
  38. using System.Collections;
  39. using MonoTests.stand_alone.WebHarness;
  40. namespace MonoTests.System.Web.UI.WebControls
  41. {
  42. [TestFixture]
  43. public class LinkButtonTest {
  44. class Poker : LinkButton {
  45. public new void AddParsedSubObject (object o)
  46. {
  47. base.AddParsedSubObject (o);
  48. }
  49. public void TrackState ()
  50. {
  51. TrackViewState ();
  52. }
  53. public object SaveState ()
  54. {
  55. return SaveViewState ();
  56. }
  57. public void LoadState (object o)
  58. {
  59. LoadViewState (o);
  60. }
  61. public string Render ()
  62. {
  63. StringWriter sw = new StringWriter ();
  64. sw.NewLine = "\n";
  65. HtmlTextWriter writer = new HtmlTextWriter (sw);
  66. base.Render (writer);
  67. return writer.InnerWriter.ToString ();
  68. }
  69. #if NET_2_0
  70. public new void RaisePostBackEvent (string eventArgument)
  71. {
  72. base.RaisePostBackEvent (eventArgument);
  73. }
  74. public new PostBackOptions GetPostBackOptions ()
  75. {
  76. return base.GetPostBackOptions ();
  77. }
  78. #endif
  79. }
  80. [TestFixtureSetUp]
  81. public void SetUp ()
  82. {
  83. WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
  84. }
  85. [Test]
  86. public void Defaults ()
  87. {
  88. Poker b = new Poker ();
  89. #if NET_2_0
  90. Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick");
  91. Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
  92. Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup");
  93. #endif
  94. }
  95. #if NET_2_0
  96. [Test]
  97. public void OnClientClick ()
  98. {
  99. Poker b = new Poker ();
  100. b.OnClientClick = "MyClickMethod";
  101. string html = b.Render ();
  102. HtmlDiff.AssertAreEqual ("<a onclick=\"MyClickMethod;\"></a>", html, "OnClientClick Failed");
  103. }
  104. [Test]
  105. [Category ("NunitWeb")]
  106. public void GetPostBackOptions ()
  107. {
  108. WebTest t = new WebTest ("NoEventValidation.aspx");
  109. t.Invoker = PageInvoker.CreateOnLoad (GetPostBackOptions_Load);
  110. t.Run ();
  111. }
  112. public static void GetPostBackOptions_Load (Page p)
  113. {
  114. Poker b = new Poker ();
  115. p.Controls.Add (b);
  116. b.PostBackUrl = "~/MyPostBackUrl.aspx";
  117. b.Text = "MyText";
  118. PostBackOptions opt = b.GetPostBackOptions ();
  119. Assert.IsNotNull (opt, "PostBackOptions not created");
  120. Assert.AreEqual ("MyPostBackUrl.aspx", opt.ActionUrl, "ActionUrl");
  121. Assert.AreEqual (b, opt.TargetControl, "TargetControl");
  122. }
  123. [Test]
  124. [Category("NunitWeb")]
  125. public void PostBackUrl ()
  126. {
  127. WebTest t = new WebTest ("NoEventValidation.aspx");
  128. t.Invoker = PageInvoker.CreateOnLoad (PostBackUrl_Load);
  129. t.Run ();
  130. }
  131. public static void PostBackUrl_Load (Page p)
  132. {
  133. Poker b = new Poker ();
  134. p.Controls.Add (b);
  135. b.PostBackUrl = "~/MyPostBackUrl.aspx";
  136. string html = b.Render ();
  137. if (html.IndexOf ("MyPostBackUrl.aspx") == -1)
  138. Assert.Fail ("PostBackUrl not created");
  139. }
  140. [Test]
  141. [Category ("NunitWeb")]
  142. public void ValidationGroup ()
  143. {
  144. WebTest t = new WebTest ("NoEventValidation.aspx");
  145. t.Invoker = PageInvoker.CreateOnLoad (ValidationGroup_Load);
  146. string html = HtmlDiff.GetControlFromPageHtml (t.Run ());
  147. if (html.IndexOf ("href") == -1)
  148. Assert.Fail ("Link button not created");
  149. if (html.IndexOf ("MyValidationGroup") == -1)
  150. Assert.Fail ("Validation group not set: " + html);
  151. }
  152. public static void ValidationGroup_Load (Page p)
  153. {
  154. Poker b = new Poker ();
  155. b.ValidationGroup = "MyValidationGroup";
  156. TextBox tb = new TextBox ();
  157. tb.ID = "tb";
  158. tb.ValidationGroup = "MyValidationGroup";
  159. RequiredFieldValidator v = new RequiredFieldValidator ();
  160. v.ControlToValidate = "tb";
  161. v.ValidationGroup = "MyValidationGroup";
  162. p.Controls.Add (tb);
  163. p.Controls.Add (v);
  164. p.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
  165. p.Controls.Add (b);
  166. p.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
  167. }
  168. [Test]
  169. [Category ("NunitWeb")]
  170. public void RaisePostBackEvent ()
  171. {
  172. WebTest t = new WebTest ("NoEventValidation.aspx");
  173. t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
  174. t.Run ();
  175. ArrayList eventlist = t.UserData as ArrayList;
  176. if (eventlist == null)
  177. Assert.Fail ("User data does not been created fail");
  178. Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
  179. Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
  180. }
  181. public static void RaisePostBackEvent_Load (Page p)
  182. {
  183. Poker b = new Poker ();
  184. p.Form.Controls.Add (b);
  185. b.Click += new EventHandler (b_Click);
  186. b.Command += new CommandEventHandler (b_Command);
  187. b.RaisePostBackEvent ("Click");
  188. }
  189. static void b_Command (object sender, CommandEventArgs e)
  190. {
  191. if (WebTest.CurrentTest.UserData == null) {
  192. ArrayList list = new ArrayList ();
  193. list.Add ("Command");
  194. WebTest.CurrentTest.UserData = list;
  195. }
  196. else {
  197. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  198. if (list == null)
  199. throw new NullReferenceException ();
  200. list.Add ("Command");
  201. WebTest.CurrentTest.UserData = list;
  202. }
  203. }
  204. static void b_Click (object sender, EventArgs e)
  205. {
  206. if (WebTest.CurrentTest.UserData == null) {
  207. ArrayList list = new ArrayList ();
  208. list.Add ("Click");
  209. WebTest.CurrentTest.UserData = list;
  210. }
  211. else {
  212. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  213. if (list == null)
  214. throw new NullReferenceException ();
  215. list.Add ("Click");
  216. WebTest.CurrentTest.UserData = list;
  217. }
  218. }
  219. #endif
  220. [Test]
  221. public void ViewState ()
  222. {
  223. Poker p = new Poker ();
  224. p.TrackState ();
  225. Assert.AreEqual (p.Text, "", "A1");
  226. p.Text = "Hello";
  227. Assert.AreEqual (p.Text, "Hello", "A2");
  228. object state = p.SaveState ();
  229. Poker copy = new Poker ();
  230. copy.TrackState ();
  231. copy.LoadState (state);
  232. Assert.AreEqual (copy.Text, "Hello", "A3");
  233. }
  234. [Test]
  235. public void Render ()
  236. {
  237. Poker l = new Poker ();
  238. l.Text = "Hello";
  239. Assert.AreEqual ("<a>Hello</a>", l.Render (), "R1");
  240. }
  241. Poker MakeNested ()
  242. {
  243. Poker p = new Poker ();
  244. LinkButton ll = new LinkButton ();
  245. ll.Text = ", World";
  246. p.AddParsedSubObject (new LiteralControl ("Hello"));
  247. p.AddParsedSubObject (ll);
  248. return p;
  249. }
  250. [Test]
  251. public void ChildControl ()
  252. {
  253. Poker l = MakeNested ();
  254. Assert.AreEqual ("<a>Hello<a>, World</a></a>", l.Render ());
  255. Assert.AreEqual ("", l.Text);
  256. l.Text = "Hello";
  257. Assert.AreEqual ("<a>Hello</a>", l.Render ());
  258. Assert.AreEqual ("Hello", l.Text);
  259. Assert.IsFalse (l.HasControls ());
  260. }
  261. [Test]
  262. public void ChildControlViewstate ()
  263. {
  264. Poker l = MakeNested ();
  265. l.TrackState ();
  266. l.Text = "Hello";
  267. object o = l.SaveState ();
  268. l = MakeNested ();
  269. l.TrackState ();
  270. l.LoadState (o);
  271. Assert.AreEqual ("<a>Hello</a>", l.Render ());
  272. Assert.AreEqual ("Hello", l.Text);
  273. Assert.IsFalse (l.HasControls ());
  274. }
  275. class BubbleNet : Control {
  276. public EventHandler Bubble;
  277. protected override bool OnBubbleEvent (object s, EventArgs e)
  278. {
  279. if (Bubble != null)
  280. Bubble (s, e);
  281. return false;
  282. }
  283. }
  284. //
  285. // I (heart) anonymous methods
  286. //
  287. bool got_command = false;
  288. bool got_click = false;
  289. bool got_bubble = false;
  290. public void Event_TestEvents_Click(object sender, EventArgs e) {
  291. Assert.IsFalse (got_click, "#1");
  292. Assert.IsFalse (got_command, "#2");
  293. Assert.IsFalse (got_bubble, "#3");
  294. got_click = true;
  295. }
  296. public void Event_TestEvents_Command(object sender, CommandEventArgs e) {
  297. Assert.IsTrue (got_click, "#4");
  298. Assert.IsFalse (got_command, "#5");
  299. Assert.IsFalse (got_bubble, "#6");
  300. Assert.AreEqual ("N", e.CommandName, "#7");
  301. Assert.AreEqual ("A", e.CommandArgument, "#8");
  302. got_command = true;
  303. }
  304. public void Event_TestEvents_Bubble(object sender, EventArgs e) {
  305. Assert.IsTrue (got_click, "#9");
  306. Assert.IsTrue (got_command, "#10");
  307. Assert.IsFalse (got_bubble, "#11");
  308. Assert.AreEqual ("N", ((CommandEventArgs) e).CommandName, "#12");
  309. Assert.AreEqual ("A", ((CommandEventArgs) e).CommandArgument, "#13");
  310. got_bubble = true;
  311. }
  312. [Test]
  313. public void TestEvents ()
  314. {
  315. BubbleNet p = new BubbleNet ();
  316. LinkButton l = new LinkButton ();
  317. l.CommandName = "N";
  318. l.CommandArgument = "A";
  319. l.CausesValidation = false; // avoid an NRE on msft
  320. p.Controls.Add (l);
  321. got_command = false;
  322. got_click = false;
  323. got_bubble = false;
  324. l.Click += new EventHandler(Event_TestEvents_Click);
  325. l.Command += new CommandEventHandler(Event_TestEvents_Command);
  326. p.Bubble += new EventHandler(Event_TestEvents_Bubble);
  327. ((IPostBackEventHandler) l).RaisePostBackEvent ("");
  328. Assert.IsTrue (got_click, "#14");
  329. Assert.IsTrue (got_command, "#15");
  330. Assert.IsTrue (got_bubble, "#16");
  331. }
  332. #if NET_2_0
  333. [Test]
  334. public void TestValidationGroup ()
  335. {
  336. Poker p = new Poker ();
  337. p.TrackState ();
  338. Assert.AreEqual (p.ValidationGroup, "", "V1");
  339. p.ValidationGroup = "VG1";
  340. Assert.AreEqual (p.ValidationGroup, "VG1", "V2");
  341. object state = p.SaveState ();
  342. Poker copy = new Poker ();
  343. copy.TrackState ();
  344. copy.LoadState (state);
  345. Assert.AreEqual (copy.ValidationGroup, "VG1", "A3");
  346. }
  347. [TestFixtureTearDown]
  348. public void TearDown ()
  349. {
  350. WebTest.Unload ();
  351. }
  352. #endif
  353. }
  354. }