2 // Tests for System.Web.UI.WebControls.LinkButton
5 // Miguel de Icaza (miguel@novell.com) [copied alot of this from his Label test]
6 // Ben Maurer <bmaurer@novell.com>
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
35 using System.Globalization;
38 using System.Web.UI.WebControls;
39 using MonoTests.SystemWeb.Framework;
40 using System.Collections;
41 using MonoTests.stand_alone.WebHarness;
43 namespace MonoTests.System.Web.UI.WebControls
46 public class LinkButtonTest {
47 class Poker : LinkButton {
49 public new void AddParsedSubObject (object o)
51 base.AddParsedSubObject (o);
54 public void TrackState ()
59 public object SaveState ()
61 return SaveViewState ();
64 public void LoadState (object o)
69 public string Render ()
71 StringWriter sw = new StringWriter ();
73 HtmlTextWriter writer = new HtmlTextWriter (sw);
75 return writer.InnerWriter.ToString ();
78 public new void RaisePostBackEvent (string eventArgument)
80 base.RaisePostBackEvent (eventArgument);
83 public new PostBackOptions GetPostBackOptions ()
85 return base.GetPostBackOptions ();
94 WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
98 public void Defaults ()
100 Poker b = new Poker ();
102 Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick");
103 Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
104 Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup");
110 public void OnClientClick ()
112 Poker b = new Poker ();
113 b.OnClientClick = "MyClickMethod";
114 string html = b.Render ();
115 HtmlDiff.AssertAreEqual ("<a onclick=\"MyClickMethod;\"></a>", html, "OnClientClick Failed");
119 [Category ("NunitWeb")]
120 public void GetPostBackOptions ()
122 WebTest t = new WebTest ("NoEventValidation.aspx");
123 t.Invoker = PageInvoker.CreateOnLoad (GetPostBackOptions_Load);
127 public static void GetPostBackOptions_Load (Page p)
129 Poker b = new Poker ();
131 b.PostBackUrl = "~/MyPostBackUrl.aspx";
133 PostBackOptions opt = b.GetPostBackOptions ();
134 Assert.IsNotNull (opt, "PostBackOptions not created");
135 Assert.AreEqual ("MyPostBackUrl.aspx", opt.ActionUrl, "ActionUrl");
136 Assert.AreEqual (b, opt.TargetControl, "TargetControl");
141 [Category("NunitWeb")]
142 public void PostBackUrl ()
144 WebTest t = new WebTest ("NoEventValidation.aspx");
145 t.Invoker = PageInvoker.CreateOnLoad (PostBackUrl_Load);
149 public static void PostBackUrl_Load (Page p)
151 Poker b = new Poker ();
153 b.PostBackUrl = "~/MyPostBackUrl.aspx";
154 string html = b.Render ();
155 if (html.IndexOf ("MyPostBackUrl.aspx") == -1)
156 Assert.Fail ("PostBackUrl not created");
160 [Category ("NunitWeb")]
161 public void ValidationGroup ()
163 WebTest t = new WebTest ("NoEventValidation.aspx");
164 t.Invoker = PageInvoker.CreateOnLoad (ValidationGroup_Load);
165 string html = HtmlDiff.GetControlFromPageHtml (t.Run ());
166 if (html.IndexOf ("href") == -1)
167 Assert.Fail ("Link button not created");
168 if (html.IndexOf ("MyValidationGroup") == -1)
169 Assert.Fail ("Validation group not set: " + html);
172 public static void ValidationGroup_Load (Page p)
174 Poker b = new Poker ();
175 b.ValidationGroup = "MyValidationGroup";
176 TextBox tb = new TextBox ();
178 tb.ValidationGroup = "MyValidationGroup";
179 RequiredFieldValidator v = new RequiredFieldValidator ();
180 v.ControlToValidate = "tb";
181 v.ValidationGroup = "MyValidationGroup";
184 p.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
186 p.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
190 [Category ("NunitWeb")]
191 public void RaisePostBackEvent ()
193 WebTest t = new WebTest ("NoEventValidation.aspx");
194 t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
196 ArrayList eventlist = t.UserData as ArrayList;
197 if (eventlist == null)
198 Assert.Fail ("User data does not been created fail");
200 Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
201 Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
204 public static void RaisePostBackEvent_Load (Page p)
206 Poker b = new Poker ();
207 p.Form.Controls.Add (b);
208 b.Click += new EventHandler (b_Click);
209 b.Command += new CommandEventHandler (b_Command);
210 b.RaisePostBackEvent ("Click");
213 static void b_Command (object sender, CommandEventArgs e)
215 if (WebTest.CurrentTest.UserData == null) {
216 ArrayList list = new ArrayList ();
217 list.Add ("Command");
218 WebTest.CurrentTest.UserData = list;
221 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
223 throw new NullReferenceException ();
224 list.Add ("Command");
225 WebTest.CurrentTest.UserData = list;
229 static void b_Click (object sender, EventArgs e)
231 if (WebTest.CurrentTest.UserData == null) {
232 ArrayList list = new ArrayList ();
234 WebTest.CurrentTest.UserData = list;
237 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
239 throw new NullReferenceException ();
241 WebTest.CurrentTest.UserData = list;
246 public void ViewState ()
248 Poker p = new Poker ();
251 Assert.AreEqual (p.Text, "", "A1");
253 Assert.AreEqual (p.Text, "Hello", "A2");
255 object state = p.SaveState ();
257 Poker copy = new Poker ();
259 copy.LoadState (state);
260 Assert.AreEqual (copy.Text, "Hello", "A3");
264 public void Render ()
266 Poker l = new Poker ();
268 Assert.AreEqual ("<a>Hello</a>", l.Render (), "R1");
273 Poker p = new Poker ();
274 LinkButton ll = new LinkButton ();
276 p.AddParsedSubObject (new LiteralControl ("Hello"));
277 p.AddParsedSubObject (ll);
283 public void ChildControl ()
285 Poker l = MakeNested ();
286 Assert.AreEqual ("<a>Hello<a>, World</a></a>", l.Render ());
287 Assert.AreEqual ("", l.Text);
289 Assert.AreEqual ("<a>Hello</a>", l.Render ());
290 Assert.AreEqual ("Hello", l.Text);
291 Assert.IsFalse (l.HasControls ());
295 public void ChildControlViewstate ()
297 Poker l = MakeNested ();
301 object o = l.SaveState ();
306 Assert.AreEqual ("<a>Hello</a>", l.Render ());
307 Assert.AreEqual ("Hello", l.Text);
308 Assert.IsFalse (l.HasControls ());
311 class BubbleNet : Control {
312 public EventHandler Bubble;
313 protected override bool OnBubbleEvent (object s, EventArgs e)
322 // I (heart) anonymous methods
324 bool got_command = false;
325 bool got_click = false;
326 bool got_bubble = false;
328 public void Event_TestEvents_Click(object sender, EventArgs e) {
329 Assert.IsFalse (got_click, "#1");
330 Assert.IsFalse (got_command, "#2");
331 Assert.IsFalse (got_bubble, "#3");
334 public void Event_TestEvents_Command(object sender, CommandEventArgs e) {
335 Assert.IsTrue (got_click, "#4");
336 Assert.IsFalse (got_command, "#5");
337 Assert.IsFalse (got_bubble, "#6");
338 Assert.AreEqual ("N", e.CommandName, "#7");
339 Assert.AreEqual ("A", e.CommandArgument, "#8");
342 public void Event_TestEvents_Bubble(object sender, EventArgs e) {
343 Assert.IsTrue (got_click, "#9");
344 Assert.IsTrue (got_command, "#10");
345 Assert.IsFalse (got_bubble, "#11");
346 Assert.AreEqual ("N", ((CommandEventArgs) e).CommandName, "#12");
347 Assert.AreEqual ("A", ((CommandEventArgs) e).CommandArgument, "#13");
351 public void TestEvents ()
353 BubbleNet p = new BubbleNet ();
354 LinkButton l = new LinkButton ();
356 l.CommandArgument = "A";
357 l.CausesValidation = false; // avoid an NRE on msft
364 l.Click += new EventHandler(Event_TestEvents_Click);
366 l.Command += new CommandEventHandler(Event_TestEvents_Command);
368 p.Bubble += new EventHandler(Event_TestEvents_Bubble);
370 ((IPostBackEventHandler) l).RaisePostBackEvent ("");
371 Assert.IsTrue (got_click, "#14");
372 Assert.IsTrue (got_command, "#15");
373 Assert.IsTrue (got_bubble, "#16");
380 public void TestValidationGroup ()
382 Poker p = new Poker ();
385 Assert.AreEqual (p.ValidationGroup, "", "V1");
386 p.ValidationGroup = "VG1";
387 Assert.AreEqual (p.ValidationGroup, "VG1", "V2");
389 object state = p.SaveState ();
391 Poker copy = new Poker ();
393 copy.LoadState (state);
394 Assert.AreEqual (copy.ValidationGroup, "VG1", "A3");
397 [TestFixtureTearDown]
398 public void TearDown ()