[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ImageButtonTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.ImageButton.cs
3 //
4 // Author:
5 //      Jordi Mas i Hernandez (jordi@ximian.com)
6 //
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
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Globalization;
35 using System.Web;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38 using MonoTests.SystemWeb.Framework;
39 using MonoTests.stand_alone.WebHarness;
40 using System.Collections;
41
42 namespace MonoTests.System.Web.UI.WebControls
43 {
44         class PokerImageButton : ImageButton {
45                 public PokerImageButton ()
46                 {
47                         TrackViewState ();
48                 }
49
50                 public object SaveState ()
51                 {
52                         return SaveViewState ();
53                 }
54
55                 public void LoadState (object o)
56                 {
57                         LoadViewState (o);
58                 }
59
60                 public string Render ()
61                 {
62                         StringWriter sw = new StringWriter ();
63                         sw.NewLine = "\n";
64                         HtmlTextWriter writer = new HtmlTextWriter (sw);
65                         base.Render (writer);
66                         return writer.InnerWriter.ToString ();
67                 }
68                 public new string Text
69                 {
70                         get
71                         {
72                                 return base.Text;
73                         }
74                         set
75                         {
76                                 base.Text = value;
77                         }
78                 }
79
80
81                 public new void RaisePostBackEvent (string eventArgument)
82                 {
83                         base.RaisePostBackEvent (eventArgument);
84                 }
85
86                 protected override void RaisePostDataChangedEvent ()
87                 {
88                         base.RaisePostDataChangedEvent ();
89                 }
90         }
91
92         [TestFixture]
93         public class ImageButtonTest {
94                 
95                 [Test]
96                 [Category("NotWorking")]
97                 public void ImageButton_DefaultValues ()
98                 {
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");
110                 }
111
112                 [Test]
113                 public void ImageButton_AssignedValues ()
114                 {
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");
129                 }
130
131
132                 [Test]
133                 [Category ("NunitWeb")]
134                 public void AppRelativeTemplateSourceDirectory ()
135                 {
136                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (AppRelativeTemplateSourceDirectory_Load));
137                         t.Run ();
138                 }
139         
140                 public static void AppRelativeTemplateSourceDirectory_Load (Page p)
141                 {
142                         PokerImageButton i = new PokerImageButton ();
143                         i.ID = "I";
144                         Assert.AreEqual ("~/", i.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory");
145                 }
146
147                 [Test]
148                 public void DescriptionUrl ()
149                 {
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");
155                 }
156
157                 [Test]
158                 public void OnClientClick ()
159                 {
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");
165                 }
166
167                 [Test] // Bug #463939
168                 public void OnClientClickEmpty ()
169                 {
170                         PokerImageButton b = new PokerImageButton ();
171                         string html = b.Render ();
172                         Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A1");
173
174                         b.OnClientClick = String.Empty;
175                         html = b.Render ();
176                         Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A2");
177
178                         b.OnClientClick = null;
179                         html = b.Render ();
180                         Assert.AreEqual (-1, html.IndexOf ("onclick=\""), "#A3");
181                 }
182                 
183                 [Test]
184                 [Category ("NunitWeb")]
185                 public void PostBackUrl ()
186                 {
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");
195                 }
196                 
197                 public static void PostBackUrl_load (Page p)
198                 {
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";
204                 }
205
206                 [Test]
207                 [Category ("NunitWeb")]
208                 public void ValidationGroup ()
209                 {
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");
218                 }
219
220                 public static void ValidationGroup_Load(Page p)
221                 {
222                         PokerImageButton b = new PokerImageButton ();
223                         b.ValidationGroup = "MyValidationGroup";
224                         TextBox tb = new TextBox ();
225                         tb.ID = "tb";
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));
235                 }
236
237                 [Test]
238                 public void Text ()
239                 {
240 #if NET_4_0
241                         string origHtml = "<input type=\"image\" src=\"\" alt=\"MyText\" />";
242 #else
243                         string origHtml = "<input type=\"image\" src=\"\" alt=\"MyText\" style=\"border-width:0px;\" />";
244 #endif
245                         PokerImageButton b = new PokerImageButton ();
246                         b.Text = "MyText";
247                         string html = b.Render ();
248                         HtmlDiff.AssertAreEqual (origHtml, html, "Text#1");
249                 }
250
251                 [Test]
252                 [Category("NunitWeb")]
253                 public void RaisePostBackEvent ()
254                 {
255                         WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
256                         WebTest t = new WebTest ("NoEventValidation.aspx");
257                         t.Invoker = PageInvoker.CreateOnLoad (RaisePostBackEvent_Load);
258                         t.Run ();
259                         ArrayList eventlist = t.UserData as ArrayList;
260                         if (eventlist == null)
261                                 Assert.Fail ("User data does not been created fail");
262
263                         Assert.AreEqual ("Click", eventlist[0], "Event Flow #0");
264                         Assert.AreEqual ("Command", eventlist[1], "Event Flow #1");
265                 }
266
267                 public static void RaisePostBackEvent_Load (Page p)
268                 {
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");
274                 }
275
276                 static void b_Command (object sender, CommandEventArgs e)
277                 {
278                         if (WebTest.CurrentTest.UserData == null) {
279                                 ArrayList list = new ArrayList ();
280                                 list.Add ("Command");
281                                 WebTest.CurrentTest.UserData = list;
282                         }
283                         else {
284                                 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
285                                 if (list == null)
286                                         throw new NullReferenceException ();
287                                 list.Add ("Command");
288                                 WebTest.CurrentTest.UserData = list;
289                         }
290                 }
291
292                 static void b_Click (object sender, ImageClickEventArgs e)
293                 {
294                         if (WebTest.CurrentTest.UserData == null) {
295                                 ArrayList list = new ArrayList ();
296                                 list.Add ("Click");
297                                 WebTest.CurrentTest.UserData = list;
298                         }
299                         else {
300                                 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
301                                 if (list == null)
302                                         throw new NullReferenceException ();
303                                 list.Add ("Click");
304                                 WebTest.CurrentTest.UserData = list;
305                         }
306                 }
307
308
309                 [Test]
310                 public void ImageButton_Render ()
311                 {
312                         StringWriter sw = new StringWriter ();
313                         HtmlTextWriter tw = new HtmlTextWriter (sw);
314
315                         ImageButton b = new ImageButton ();                     
316                         b.RenderControl (tw);                           
317                         
318                         Assert.AreEqual (true, sw.ToString().IndexOf ("<input") != -1, "A1");
319                         Assert.AreEqual (true, sw.ToString().IndexOf ("type=\"image\"") != -1, "A2");
320                 }
321
322                 [Test]
323                 public void ImageButton_ViewState ()
324                 {
325                         PokerImageButton p = new PokerImageButton ();
326
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");
333
334                         object state = p.SaveState ();
335
336                         PokerImageButton copy = new PokerImageButton ();
337                         copy.LoadState (state);
338
339                         Assert.AreEqual (copy.CommandArgument, "arg", "A4");
340                         Assert.AreEqual (copy.CommandName, "cmd", "A5");
341                         Assert.AreEqual (copy.ValidationGroup, "VG1", "A6");
342
343                 }
344
345                 [Test]
346                 public void RenderName ()
347                 {
348                         StringWriter sw = new StringWriter ();
349                         HtmlTextWriter tw = new HtmlTextWriter (sw);
350
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");
359                 }
360                 [Test]
361                 [ExpectedException (typeof (NotSupportedException))]
362                 public void GenerateEmptyAlternateText_Exception ()
363                 {
364                         ImageButton b = new ImageButton ();
365                         b.GenerateEmptyAlternateText = true;
366                 }
367
368                 [TestFixtureTearDown]
369                 public void TearDown ()
370                 {
371                         WebTest.Unload ();
372                 }
373
374         }
375 }
376
377