2 // Tests for System.Web.UI.WebControls.ImageButton.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
34 using System.Globalization;
37 using System.Web.UI.WebControls;
38 using MonoTests.SystemWeb.Framework;
39 using MonoTests.stand_alone.WebHarness;
40 using System.Collections;
42 namespace MonoTests.System.Web.UI.WebControls
44 class PokerImageButton : ImageButton {
45 public PokerImageButton ()
50 public object SaveState ()
52 return SaveViewState ();
55 public void LoadState (object o)
60 public string Render ()
62 StringWriter sw = new StringWriter ();
64 HtmlTextWriter writer = new HtmlTextWriter (sw);
66 return writer.InnerWriter.ToString ();
68 public new string Text
81 public new void RaisePostBackEvent (string eventArgument)
83 base.RaisePostBackEvent (eventArgument);
86 protected override void RaisePostDataChangedEvent ()
88 base.RaisePostDataChangedEvent ();
93 public class ImageButtonTest {
96 [Category("NotWorking")]
97 public void ImageButton_DefaultValues ()
99 ImageButton b = new ImageButton ();
100 Assert.AreEqual (true, b.CausesValidation, "CausesValidation");
101 Assert.AreEqual (string.Empty, b.CommandArgument, "CommandArgument");
102 Assert.AreEqual (string.Empty, b.CommandName, "CommandName");
103 Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup");
104 Assert.AreEqual (string.Empty, b.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory");
105 Assert.AreEqual (string.Empty, b.DescriptionUrl, "DescriptionUrl");
106 Assert.AreEqual (true, b.EnableTheming, "EnableTheming");
107 Assert.AreEqual (false, b.GenerateEmptyAlternateText, "GenerateEmptyAlternateText");
108 Assert.AreEqual (string.Empty, b.PostBackUrl, "PostBackUrl");
109 Assert.AreEqual (string.Empty, b.OnClientClick, "OnClientClick");
113 public void ImageButton_AssignedValues ()
115 ImageButton b = new ImageButton ();
116 Assert.AreEqual (string.Empty, b.ValidationGroup, "ValidationGroup#1");
117 b.ValidationGroup = "test";
118 Assert.AreEqual ("test", b.ValidationGroup, "ValidationGroup#2");
119 // NOTE: Default is wrong!
120 // Assert.AreEqual (string.Empty, b.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
121 b.AppRelativeTemplateSourceDirectory = "~/test";
122 Assert.AreEqual ("~/test", b.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
123 Assert.AreEqual (string.Empty, b.DescriptionUrl, "DescriptionUrl#1");
124 b.DescriptionUrl = "test";
125 Assert.AreEqual ("test", b.DescriptionUrl, "DescriptionUrl#2");
126 Assert.AreEqual (true, b.EnableTheming, "EnableTheming#1");
127 b.EnableTheming = false;
128 Assert.AreEqual (false, b.EnableTheming, "EnableTheming#2");
133 [Category ("NunitWeb")]
134 public void AppRelativeTemplateSourceDirectory ()
136 WebTest t = new WebTest (PageInvoker.CreateOnLoad (AppRelativeTemplateSourceDirectory_Load));
140 public static void AppRelativeTemplateSourceDirectory_Load (Page p)
142 PokerImageButton i = new PokerImageButton ();
144 Assert.AreEqual ("~/", i.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory");
148 public void DescriptionUrl ()
150 PokerImageButton i = new PokerImageButton ();
151 i.DescriptionUrl = "URLDescription";
152 string html = i.Render ();
153 if (html.IndexOf ("longdesc=\"URLDescription\"") == -1)
154 Assert.Fail ("DescriptionUrl Failed");
158 public void OnClientClick ()
160 PokerImageButton b = new PokerImageButton ();
161 b.OnClientClick = "MyMethod";
162 string html = b.Render ();
163 if (html.IndexOf ("onclick=\"MyMethod;\"") == -1)
164 Assert.Fail ("OnClientClick#1");
167 [Test] // Bug #463939
168 public void OnClientClickEmpty ()
170 PokerImageButton b = new PokerImageButton ();
171 string html = b.Render ();
172 Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A1");
174 b.OnClientClick = String.Empty;
176 Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A2");
178 b.OnClientClick = null;
180 Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A3");
184 [Category ("NunitWeb")]
185 public void PostBackUrl ()
187 WebTest test = new WebTest (PageInvoker.CreateOnLoad (PostBackUrl_load));
188 string html = HtmlDiff.GetControlFromPageHtml (test.Run ());
189 if (html.IndexOf ("onclick") == -1)
190 Assert.Fail ("PostBack script not created");
191 if (html.IndexOf ("MyURL.aspx") == -1)
192 Assert.Fail ("PostBack page URL not set");
193 if (html.IndexOf ("~/MyURL.aspx") != -1)
194 Assert.Fail ("PostBack page URL is not resolved");
197 public static void PostBackUrl_load (Page p)
199 PokerImageButton b = new PokerImageButton ();
200 p.Form.Controls.Add (new LiteralControl(HtmlDiff.BEGIN_TAG));
201 p.Form.Controls.Add (b);
202 p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
203 b.PostBackUrl = "~/MyURL.aspx";
207 [Category ("NunitWeb")]
208 public void ValidationGroup ()
210 WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
211 WebTest t = new WebTest ("NoEventValidation.aspx");
212 t.Invoker = PageInvoker.CreateOnLoad (ValidationGroup_Load);
213 string html = HtmlDiff.GetControlFromPageHtml (t.Run ());
214 if (html.IndexOf ("onclick") == -1)
215 Assert.Fail ("Validation script not created");
216 if (html.IndexOf ("MyValidationGroup") == -1)
217 Assert.Fail ("Validation group not set fail");
220 public static void ValidationGroup_Load(Page p)
222 PokerImageButton b = new PokerImageButton ();
223 b.ValidationGroup = "MyValidationGroup";
224 TextBox tb = new TextBox ();
226 tb.ValidationGroup = "MyValidationGroup";
227 RequiredFieldValidator v = new RequiredFieldValidator ();
228 v.ControlToValidate = "tb";
229 v.ValidationGroup = "MyValidationGroup";
230 p.Form.Controls.Add (tb);
231 p.Form.Controls.Add (v);
232 p.Form.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
233 p.Form.Controls.Add (b);
234 p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
241 string origHtml = "<input type=\"image\" src=\"\" alt=\"MyText\" />";
243 string origHtml = "<input type=\"image\" src=\"\" alt=\"MyText\" style=\"border-width:0px;\" />";
245 PokerImageButton b = new PokerImageButton ();
247 string html = b.Render ();
248 HtmlDiff.AssertAreEqual (origHtml, html, "Text#1");
252 [Category("NunitWeb")]
253 public void RaisePostBackEvent ()
255 WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
256 WebTest t = new WebTest ("NoEventValidation.aspx");
257 t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
259 ArrayList eventlist = t.UserData as ArrayList;
260 if (eventlist == null)
261 Assert.Fail ("User data does not been created fail");
263 Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
264 Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
267 public static void RaisePostBackEvent_Load (Page p)
269 PokerImageButton b = new PokerImageButton ();
270 p.Form.Controls.Add (b);
271 b.Click += new ImageClickEventHandler (b_Click);
272 b.Command += new CommandEventHandler (b_Command);
273 b.RaisePostBackEvent ("Click");
276 static void b_Command (object sender, CommandEventArgs e)
278 if (WebTest.CurrentTest.UserData == null) {
279 ArrayList list = new ArrayList ();
280 list.Add ("Command");
281 WebTest.CurrentTest.UserData = list;
284 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
286 throw new NullReferenceException ();
287 list.Add ("Command");
288 WebTest.CurrentTest.UserData = list;
292 static void b_Click (object sender, ImageClickEventArgs e)
294 if (WebTest.CurrentTest.UserData == null) {
295 ArrayList list = new ArrayList ();
297 WebTest.CurrentTest.UserData = list;
300 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
302 throw new NullReferenceException ();
304 WebTest.CurrentTest.UserData = list;
310 public void ImageButton_Render ()
312 StringWriter sw = new StringWriter ();
313 HtmlTextWriter tw = new HtmlTextWriter (sw);
315 ImageButton b = new ImageButton ();
316 b.RenderControl (tw);
318 Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
319 Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
323 public void ImageButton_ViewState ()
325 PokerImageButton p = new PokerImageButton ();
327 p.CommandArgument = "arg";
328 Assert.AreEqual (p.CommandArgument, "arg", "A1");
329 p.CommandName = "cmd";
330 Assert.AreEqual (p.CommandName, "cmd", "A2");
331 p.ValidationGroup = "VG1";
332 Assert.AreEqual (p.ValidationGroup, "VG1", "A3");
334 object state = p.SaveState ();
336 PokerImageButton copy = new PokerImageButton ();
337 copy.LoadState (state);
339 Assert.AreEqual (copy.CommandArgument, "arg", "A4");
340 Assert.AreEqual (copy.CommandName, "cmd", "A5");
341 Assert.AreEqual (copy.ValidationGroup, "VG1", "A6");
346 public void RenderName ()
348 StringWriter sw = new StringWriter ();
349 HtmlTextWriter tw = new HtmlTextWriter (sw);
351 Page page = new Page ();
352 page.EnableEventValidation = false;
353 ImageButton b = new ImageButton ();
354 page.Controls.Add (b);
355 page.RenderControl (tw);
356 Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
357 Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
358 Assert.AreEqual (true, sw.ToString().IndexOf ("name=\"") != -1, "A3");
361 [ExpectedException (typeof (NotSupportedException))]
362 public void GenerateEmptyAlternateText_Exception ()
364 ImageButton b = new ImageButton ();
365 b.GenerateEmptyAlternateText = true;
368 [TestFixtureTearDown]
369 public void TearDown ()