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));
240 string origHtml = "<input type=\"image\" src=\"\" alt=\"MyText\" />";
241 PokerImageButton b = new PokerImageButton ();
243 string html = b.Render ();
244 HtmlDiff.AssertAreEqual (origHtml, html, "Text#1");
248 [Category("NunitWeb")]
249 public void RaisePostBackEvent ()
251 WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
252 WebTest t = new WebTest ("NoEventValidation.aspx");
253 t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
255 ArrayList eventlist = t.UserData as ArrayList;
256 if (eventlist == null)
257 Assert.Fail ("User data does not been created fail");
259 Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
260 Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
263 public static void RaisePostBackEvent_Load (Page p)
265 PokerImageButton b = new PokerImageButton ();
266 p.Form.Controls.Add (b);
267 b.Click += new ImageClickEventHandler (b_Click);
268 b.Command += new CommandEventHandler (b_Command);
269 b.RaisePostBackEvent ("Click");
272 static void b_Command (object sender, CommandEventArgs e)
274 if (WebTest.CurrentTest.UserData == null) {
275 ArrayList list = new ArrayList ();
276 list.Add ("Command");
277 WebTest.CurrentTest.UserData = list;
280 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
282 throw new NullReferenceException ();
283 list.Add ("Command");
284 WebTest.CurrentTest.UserData = list;
288 static void b_Click (object sender, ImageClickEventArgs e)
290 if (WebTest.CurrentTest.UserData == null) {
291 ArrayList list = new ArrayList ();
293 WebTest.CurrentTest.UserData = list;
296 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
298 throw new NullReferenceException ();
300 WebTest.CurrentTest.UserData = list;
306 public void ImageButton_Render ()
308 StringWriter sw = new StringWriter ();
309 HtmlTextWriter tw = new HtmlTextWriter (sw);
311 ImageButton b = new ImageButton ();
312 b.RenderControl (tw);
314 Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
315 Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
319 public void ImageButton_ViewState ()
321 PokerImageButton p = new PokerImageButton ();
323 p.CommandArgument = "arg";
324 Assert.AreEqual (p.CommandArgument, "arg", "A1");
325 p.CommandName = "cmd";
326 Assert.AreEqual (p.CommandName, "cmd", "A2");
327 p.ValidationGroup = "VG1";
328 Assert.AreEqual (p.ValidationGroup, "VG1", "A3");
330 object state = p.SaveState ();
332 PokerImageButton copy = new PokerImageButton ();
333 copy.LoadState (state);
335 Assert.AreEqual (copy.CommandArgument, "arg", "A4");
336 Assert.AreEqual (copy.CommandName, "cmd", "A5");
337 Assert.AreEqual (copy.ValidationGroup, "VG1", "A6");
342 public void RenderName ()
344 StringWriter sw = new StringWriter ();
345 HtmlTextWriter tw = new HtmlTextWriter (sw);
347 Page page = new Page ();
348 page.EnableEventValidation = false;
349 ImageButton b = new ImageButton ();
350 page.Controls.Add (b);
351 page.RenderControl (tw);
352 Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
353 Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
354 Assert.AreEqual (true, sw.ToString().IndexOf ("name=\"") != -1, "A3");
357 [ExpectedException (typeof (NotSupportedException))]
358 public void GenerateEmptyAlternateText_Exception ()
360 ImageButton b = new ImageButton ();
361 b.GenerateEmptyAlternateText = true;
364 [TestFixtureTearDown]
365 public void TearDown ()