LinkButtonTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // Tests for System.Web.UI.WebControls.LinkButton
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected]) [copied alot of this from his Label test]
  6. // Ben Maurer <[email protected]>
  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. namespace MonoTests.System.Web.UI.WebControls
  38. {
  39. [TestFixture]
  40. public class LinkButtonText {
  41. class Poker : LinkButton {
  42. public new void AddParsedSubObject (object o)
  43. {
  44. base.AddParsedSubObject (o);
  45. }
  46. public void TrackState ()
  47. {
  48. TrackViewState ();
  49. }
  50. public object SaveState ()
  51. {
  52. return SaveViewState ();
  53. }
  54. public void LoadState (object o)
  55. {
  56. LoadViewState (o);
  57. }
  58. public string Render ()
  59. {
  60. StringWriter sw = new StringWriter ();
  61. sw.NewLine = "\n";
  62. HtmlTextWriter writer = new HtmlTextWriter (sw);
  63. base.Render (writer);
  64. return writer.InnerWriter.ToString ();
  65. }
  66. }
  67. [Test]
  68. public void ViewState ()
  69. {
  70. Poker p = new Poker ();
  71. p.TrackState ();
  72. Assert.AreEqual (p.Text, "", "A1");
  73. p.Text = "Hello";
  74. Assert.AreEqual (p.Text, "Hello", "A2");
  75. object state = p.SaveState ();
  76. Poker copy = new Poker ();
  77. copy.TrackState ();
  78. copy.LoadState (state);
  79. Assert.AreEqual (copy.Text, "Hello", "A3");
  80. }
  81. [Test]
  82. public void Render ()
  83. {
  84. Poker l = new Poker ();
  85. l.Text = "Hello";
  86. Assert.AreEqual ("<a>Hello</a>", l.Render (), "R1");
  87. }
  88. Poker MakeNested ()
  89. {
  90. Poker p = new Poker ();
  91. LinkButton ll = new LinkButton ();
  92. ll.Text = ", World";
  93. p.AddParsedSubObject (new LiteralControl ("Hello"));
  94. p.AddParsedSubObject (ll);
  95. return p;
  96. }
  97. [Test]
  98. public void ChildControl ()
  99. {
  100. Poker l = MakeNested ();
  101. Assert.AreEqual ("<a>Hello<a>, World</a></a>", l.Render ());
  102. Assert.AreEqual ("", l.Text);
  103. l.Text = "Hello";
  104. Assert.AreEqual ("<a>Hello</a>", l.Render ());
  105. Assert.AreEqual ("Hello", l.Text);
  106. Assert.IsFalse (l.HasControls ());
  107. }
  108. [Test]
  109. public void ChildControlViewstate ()
  110. {
  111. Poker l = MakeNested ();
  112. l.TrackState ();
  113. l.Text = "Hello";
  114. object o = l.SaveState ();
  115. l = MakeNested ();
  116. l.TrackState ();
  117. l.LoadState (o);
  118. Assert.AreEqual ("<a>Hello</a>", l.Render ());
  119. Assert.AreEqual ("Hello", l.Text);
  120. Assert.IsFalse (l.HasControls ());
  121. }
  122. class BubbleNet : Control {
  123. public EventHandler Bubble;
  124. protected override bool OnBubbleEvent (object s, EventArgs e)
  125. {
  126. if (Bubble != null)
  127. Bubble (s, e);
  128. return false;
  129. }
  130. }
  131. //
  132. // I (heart) anonymous methods
  133. //
  134. [Test]
  135. public void TestEvents ()
  136. {
  137. BubbleNet p = new BubbleNet ();
  138. LinkButton l = new LinkButton ();
  139. l.CommandName = "N";
  140. l.CommandArgument = "A";
  141. l.CausesValidation = false; // avoid an NRE on msft
  142. p.Controls.Add (l);
  143. bool got_command = false;
  144. bool got_click = false;
  145. bool got_bubble = false;
  146. l.Click += delegate {
  147. Assert.IsFalse (got_click, "#1");
  148. Assert.IsFalse (got_command, "#2");
  149. Assert.IsFalse (got_bubble, "#3");
  150. got_click = true;
  151. };
  152. l.Command += delegate (object sender, CommandEventArgs e) {
  153. Assert.IsTrue (got_click, "#4");
  154. Assert.IsFalse (got_command, "#5");
  155. Assert.IsFalse (got_bubble, "#6");
  156. Assert.AreEqual ("N", e.CommandName, "#7");
  157. Assert.AreEqual ("A", e.CommandArgument, "#8");
  158. got_command = true;
  159. };
  160. p.Bubble += delegate (object sender, EventArgs e) {
  161. Assert.IsTrue (got_click, "#9");
  162. Assert.IsTrue (got_command, "#10");
  163. Assert.IsFalse (got_bubble, "#11");
  164. Assert.AreEqual ("N", ((CommandEventArgs) e).CommandName, "#12");
  165. Assert.AreEqual ("A", ((CommandEventArgs) e).CommandArgument, "#13");
  166. got_bubble = true;
  167. };
  168. ((IPostBackEventHandler) l).RaisePostBackEvent ("");
  169. Assert.IsTrue (got_click, "#14");
  170. Assert.IsTrue (got_command, "#15");
  171. Assert.IsTrue (got_bubble, "#16");
  172. }
  173. #if NET_2_0
  174. [Test]
  175. public void TestValidationGroup ()
  176. {
  177. Poker p = new Poker ();
  178. p.TrackState ();
  179. Assert.AreEqual (p.ValidationGroup, "", "V1");
  180. p.ValidationGroup = "VG1";
  181. Assert.AreEqual (p.ValidationGroup, "VG1", "V2");
  182. object state = p.SaveState ();
  183. Poker copy = new Poker ();
  184. copy.TrackState ();
  185. copy.LoadState (state);
  186. Assert.AreEqual (copy.ValidationGroup, "VG1", "A3");
  187. }
  188. #endif
  189. }
  190. }