2 // Tests for System.Web.UI.WebControls.ImageMap.cs
5 // Hagit Yidov (hagity@mainsoft.com
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
32 using System.Globalization;
35 using System.Web.UI.WebControls;
36 using MonoTests.stand_alone.WebHarness;
37 using MonoTests.SystemWeb.Framework;
38 using System.Threading;
40 namespace MonoTests.System.Web.UI.WebControls
42 class PokerImageMap : ImageMap
45 public PokerImageMap ()
50 public object SaveState ()
52 return SaveViewState ();
55 public void LoadState (object o)
60 public StateBag StateBag
62 get { return base.ViewState; }
65 public void DoOnClick (ImageMapEventArgs e)
70 public void DoOnBubbleEven (Object source, ImageMapEventArgs e)
72 base.OnBubbleEvent (source, e);
76 public string Render ()
78 StringWriter sw = new StringWriter ();
79 HtmlTextWriter tw = new HtmlTextWriter (sw);
82 return sw.ToString ();
87 public class ImageMapTest
92 WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
98 public void ImageMap_DefaultProperties ()
100 PokerImageMap imageMap = new PokerImageMap ();
101 Assert.AreEqual (0, imageMap.StateBag.Count, "ViewState.Count");
102 Assert.AreEqual (true, imageMap.Enabled, "Enabled");
103 Assert.AreEqual (HotSpotMode.NotSet, imageMap.HotSpotMode, "HotSpotMode");
104 Assert.AreEqual (0, imageMap.HotSpots.Count, "HotSpots.Count");
105 Assert.AreEqual (string.Empty, imageMap.Target, "Target");
109 public void ImageMap_AssignToDefaultProperties ()
111 PokerImageMap imageMap = new PokerImageMap ();
113 Assert.AreEqual (0, imageMap.StateBag.Count, "ViewState.Count");
115 imageMap.Enabled = true;
116 Assert.AreEqual (true, imageMap.Enabled, "Enabled");
117 Assert.AreEqual (0, imageMap.StateBag.Count, "ViewState.Count-1");
119 imageMap.HotSpotMode = HotSpotMode.Navigate;
120 Assert.AreEqual (HotSpotMode.Navigate, imageMap.HotSpotMode, "HotSpotMode");
121 Assert.AreEqual (1, imageMap.StateBag.Count, "ViewState.Count-2");
123 imageMap.HotSpots.Add (new CircleHotSpot ());
124 Assert.AreEqual (1, imageMap.HotSpots.Count, "HotSpots.Count");
125 Assert.AreEqual (1, imageMap.StateBag.Count, "ViewState.Count-3");
127 imageMap.Target = "Target";
128 Assert.AreEqual ("Target", imageMap.Target, "Target");
129 Assert.AreEqual (2, imageMap.StateBag.Count, "ViewState.Count-4");
133 public void ImageMap_Defaults_Render ()
135 PokerImageMap imageMap = new PokerImageMap ();
136 string originalHtml = "<img src=\"\" />";
137 string renderedHtml = imageMap.Render ();
138 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "RenderDefault");
142 public void ImageMap_AssignedValues_RenderNavigate ()
144 // HotSpotMode = Navigate using NavigateURL
145 //-----------------------------------------
146 PokerImageMap imageMap = new PokerImageMap ();
147 imageMap.Enabled = true;
148 imageMap.HotSpotMode = HotSpotMode.Navigate;
149 imageMap.Target = "Target";
150 CircleHotSpot circle = new CircleHotSpot ();
151 circle.NavigateUrl = "NavigateURL";
152 imageMap.HotSpots.Add (circle);
153 string originalHtml = "<img src=\"\" usemap=\"#ImageMap\" /><map name=\"ImageMap\" id=\"ImageMap\">\r\n\t<area shape=\"circle\" coords=\"0,0,0\" href=\"NavigateURL\" target=\"Target\" title=\"\" alt=\"\" />\r\n</map>";
154 string renderedHtml = imageMap.Render ();
155 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "RenderNavigateTextAssigned");
159 public void ImageMap_AssignedValues_RenderNavigateCircle ()
161 // Circle.HotSpotMode = Navigate
162 //------------------------------
163 PokerImageMap imageMap = new PokerImageMap ();
164 imageMap.Enabled = true;
165 CircleHotSpot circle = new CircleHotSpot ();
166 circle.AccessKey = "A";
167 circle.AlternateText = "Circle";
168 circle.HotSpotMode = HotSpotMode.Navigate;
169 circle.NavigateUrl = "NavigateURL";
174 imageMap.HotSpots.Add (circle);
175 string originalHtml = "<img src=\"\" usemap=\"#ImageMap\" /><map name=\"ImageMap\" id=\"ImageMap\">\r\n\t<area shape=\"circle\" coords=\"30,40,10\" href=\"NavigateURL\" title=\"Circle\" alt=\"Circle\" accesskey=\"A\" tabindex=\"1\" />\r\n</map>";
176 string renderedHtml = imageMap.Render ();
177 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "RenderNavigateCircleTextAssigned");
181 public void ImageMap_AssignedValues_RenderNavigateShapes ()
183 // Rectangle/Polygon.HotSpotMode = Navigate
184 //-----------------------------------------
185 PokerImageMap imageMap = new PokerImageMap ();
186 imageMap.Enabled = true;
187 imageMap.HotSpotMode = HotSpotMode.NotSet;
188 RectangleHotSpot rect = new RectangleHotSpot ();
189 rect.AccessKey = "R";
190 rect.AlternateText = "Rectangle";
191 rect.HotSpotMode = HotSpotMode.Navigate;
192 rect.NavigateUrl = "NavigateUrlRect";
198 imageMap.HotSpots.Add (rect);
199 imageMap.HotSpotMode = HotSpotMode.Navigate;
200 PolygonHotSpot poly = new PolygonHotSpot ();
201 poly.AccessKey = "P";
202 poly.AlternateText = "Polygon";
203 poly.NavigateUrl = "NavigateUrlPoly";
205 poly.Coordinates = "10,20,30,40,50,60,100,200";
206 imageMap.HotSpots.Add (poly);
207 string originalHtml = "<img src=\"\" usemap=\"#ImageMap\" /><map name=\"ImageMap\" id=\"ImageMap\">\r\n\t<area shape=\"rect\" coords=\"30,20,40,10\" href=\"NavigateUrlRect\" title=\"Rectangle\" alt=\"Rectangle\" accesskey=\"R\" tabindex=\"1\" /><area shape=\"poly\" coords=\"10,20,30,40,50,60,100,200\" href=\"NavigateUrlPoly\" title=\"Polygon\" alt=\"Polygon\" accesskey=\"P\" tabindex=\"2\" />\r\n</map>";
208 string renderedHtml = imageMap.Render ();
209 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "RenderNavigateShapesTextAssigned");
213 public void ImageMap_AssignedValues_RenderInactive ()
215 // HotSpotMode = Inactive
216 //-----------------------
217 PokerImageMap imageMap = new PokerImageMap ();
218 imageMap.Enabled = true;
219 imageMap.HotSpotMode = HotSpotMode.Inactive;
220 imageMap.Target = "Target";
221 imageMap.HotSpots.Add (new CircleHotSpot ());
222 string originalHtml = "<img src=\"\" usemap=\"#ImageMap\" /><map name=\"ImageMap\" id=\"ImageMap\">\r\n\t<area shape=\"circle\" coords=\"0,0,0\" nohref=\"true\" title=\"\" alt=\"\" />\r\n</map>";
223 string renderedHtml = imageMap.Render ();
224 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "RenderInaciveTextAssigned");
228 public void ImageMap_AssignedValues_RenderDisabled ()
232 PokerImageMap imageMap = new PokerImageMap ();
233 imageMap.Enabled = false;
234 imageMap.HotSpotMode = HotSpotMode.Navigate;
235 imageMap.Target = "Target";
236 CircleHotSpot circle = new CircleHotSpot ();
237 circle.NavigateUrl = "NavigateURL";
238 imageMap.HotSpots.Add (circle);
239 string originalHtml = "<img class=\"aspNetDisabled\" src=\"\" usemap=\"#ImageMap\" /><map name=\"ImageMap\" id=\"ImageMap\">\r\n\t<area shape=\"circle\" coords=\"0,0,0\" target=\"Target\" title=\"\" alt=\"\" />\r\n</map>";
240 string renderedHtml = imageMap.Render ();
241 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "RenderDisabledTextAssigne");
245 public void ImageMap_ViewState ()
247 PokerImageMap imageMap = new PokerImageMap ();
248 imageMap.Enabled = true;
249 Assert.AreEqual (true, imageMap.Enabled, "Enabled-beforecopy");
250 imageMap.HotSpotMode = HotSpotMode.Navigate;
251 Assert.AreEqual (HotSpotMode.Navigate, imageMap.HotSpotMode, "HotSpotMode-beforecopy");
252 imageMap.HotSpots.Add (new CircleHotSpot ());
253 Assert.AreEqual (1, imageMap.HotSpots.Count, "HotSpots.Count-beforecopy");
254 imageMap.Target = "Target";
255 Assert.AreEqual ("Target", imageMap.Target, "Target-beforecopy");
256 object state = imageMap.SaveState ();
257 PokerImageMap copy = new PokerImageMap ();
258 copy.LoadState (state);
259 Assert.AreEqual (true, copy.Enabled, "Enabled-aftercopy");
260 Assert.AreEqual (HotSpotMode.Navigate, copy.HotSpotMode, "HotSpotMode-aftercopy");
261 //Assert.AreEqual(1, copy.HotSpots.Count, "HotSpots.Count-aftercopy");
262 Assert.AreEqual ("Target", copy.Target, "Target-aftercopy");
266 private bool clicked = false;
267 private string pbValue;
269 private void ImageMapClickHandler (object sender, ImageMapEventArgs e)
272 pbValue = e.PostBackValue;
275 private void ResetEvents ()
282 public void ImageMap_Event ()
284 PokerImageMap imageMap = new PokerImageMap ();
286 imageMap.HotSpotMode = HotSpotMode.PostBack;
287 imageMap.Click += new ImageMapEventHandler (ImageMapClickHandler);
288 Assert.AreEqual (false, clicked, "BeforeClick");
289 imageMap.DoOnClick (new ImageMapEventArgs ("HotSpotName"));
290 Assert.AreEqual (true, clicked, "AfterClick");
294 public void ImageMap_EventCircle ()
296 PokerImageMap imageMap = new PokerImageMap ();
298 imageMap.HotSpotMode = HotSpotMode.NotSet;
299 CircleHotSpot circle = new CircleHotSpot ();
300 circle.HotSpotMode = HotSpotMode.PostBack;
301 circle.PostBackValue = "myCircle";
302 imageMap.HotSpots.Add (circle);
303 imageMap.Click += new ImageMapEventHandler (ImageMapClickHandler);
304 Assert.AreEqual ("Init", pbValue, "BeforeClick");
305 imageMap.DoOnClick (new ImageMapEventArgs (circle.PostBackValue));
306 Assert.AreEqual ("myCircle", pbValue, "AfterClick");
310 public void ImageMap_EventRectangle ()
312 PokerImageMap imageMap = new PokerImageMap ();
314 imageMap.HotSpotMode = HotSpotMode.PostBack;
315 RectangleHotSpot rect = new RectangleHotSpot ();
316 rect.PostBackValue = "myRect";
317 imageMap.HotSpots.Add (rect);
318 imageMap.Click += new ImageMapEventHandler (ImageMapClickHandler);
319 Assert.AreEqual ("Init", pbValue, "BeforeClick");
320 imageMap.DoOnClick (new ImageMapEventArgs (rect.PostBackValue));
321 Assert.AreEqual ("myRect", pbValue, "AfterClick");
325 public void ImageMap_EventPolygon ()
327 PokerImageMap imageMap = new PokerImageMap ();
329 imageMap.HotSpotMode = HotSpotMode.NotSet;
330 PolygonHotSpot poly = new PolygonHotSpot ();
331 poly.HotSpotMode = HotSpotMode.PostBack;
332 poly.PostBackValue = "myPoly";
333 imageMap.HotSpots.Add (poly);
334 imageMap.Click += new ImageMapEventHandler (ImageMapClickHandler);
335 Assert.AreEqual ("Init", pbValue, "BeforeClick");
336 imageMap.DoOnClick (new ImageMapEventArgs (poly.PostBackValue));
337 Assert.AreEqual ("myPoly", pbValue, "AfterClick");
340 public void ImageMap_BubbleEvent ()
342 PokerImageMap imageMap = new PokerImageMap ();
344 ImageMapEventArgs args = new ImageMapEventArgs ("HotSpotName");
345 imageMap.Click += new ImageMapEventHandler (ImageMapClickHandler);
346 Assert.AreEqual (false, clicked, "BeforeClick");
347 imageMap.DoOnBubbleEven (imageMap, args);
348 Assert.AreEqual (true, clicked, "AfterClick");
351 private static void ImageMapClickHandler2 (object sender, ImageMapEventArgs e)
353 WebTest.CurrentTest.UserData = e.PostBackValue;
357 [Category ("NunitWeb")]
358 public void ImageMap_PostBackRectangle ()
360 WebTest t = new WebTest (PageInvoker.CreateOnLoad (myPageLoad));
362 FormRequest fr = new FormRequest (t.Response, "form1");
363 fr.Controls.Add ("__EVENTTARGET");
364 fr.Controls.Add ("__EVENTARGUMENT");
365 fr.Controls["__EVENTTARGET"].Value = "imgmap";
366 fr.Controls["__EVENTARGUMENT"].Value = "0";
369 Assert.AreEqual ("Rectangle", t.UserData, "AfterPostBack");
373 [Category ("NunitWeb")]
374 public void ImageMap_PostBackFireEvent ()
376 WebTest t = new WebTest ("NoEventValidation.aspx");
377 t.Invoker = PageInvoker.CreateOnLoad (PostBackFireEvent_Init);
379 FormRequest fr = new FormRequest (t.Response, "form1");
380 fr.Controls.Add ("__EVENTTARGET");
381 fr.Controls.Add ("__EVENTARGUMENT");
382 fr.Controls["__EVENTTARGET"].Value = "imgmap";
383 fr.Controls["__EVENTARGUMENT"].Value = "0";
386 if (t.UserData == null)
387 Assert.Fail ("Event not fired fail");
388 Assert.AreEqual ("ImageMapClickHandler", t.UserData.ToString (), "PostBackFireEvent");
391 #region PostBackFireEvent
392 public static void PostBackFireEvent_Init (Page p)
394 ImageMap imgmap = new ImageMap ();
395 imgmap.ID = "imgmap";
396 imgmap.HotSpotMode = HotSpotMode.NotSet;
397 imgmap.Click += new ImageMapEventHandler (ImageMapClickHandler3);
398 RectangleHotSpot rect = new RectangleHotSpot ();
399 rect.HotSpotMode = HotSpotMode.PostBack;
400 rect.PostBackValue = "Rectangle";
401 imgmap.HotSpots.Add (rect);
402 p.Form.Controls.Add (imgmap);
405 public static void ImageMapClickHandler3 (object sender, ImageMapEventArgs e)
407 WebTest.CurrentTest.UserData = "ImageMapClickHandler";
412 [Category ("NunitWeb")]
413 public void ImageMap_PostBackCircle ()
415 WebTest t = new WebTest (PageInvoker.CreateOnLoad (myPageLoad));
417 FormRequest fr = new FormRequest (t.Response, "form1");
418 fr.Controls.Add ("__EVENTTARGET");
419 fr.Controls.Add ("__EVENTARGUMENT");
420 fr.Controls["__EVENTTARGET"].Value = "imgmap";
421 fr.Controls["__EVENTARGUMENT"].Value = "2";
424 Assert.AreEqual ("Circle", t.UserData, "AfterPostBack");
428 [Category ("NunitWeb")]
429 public void ImageMap_PostBackPolygon ()
431 WebTest t = new WebTest (PageInvoker.CreateOnLoad (myPageLoad));
433 FormRequest fr = new FormRequest (t.Response, "form1");
434 fr.Controls.Add ("__EVENTTARGET");
435 fr.Controls.Add ("__EVENTARGUMENT");
436 fr.Controls["__EVENTTARGET"].Value = "imgmap";
437 fr.Controls["__EVENTARGUMENT"].Value = "1";
440 Assert.AreEqual ("Polygon", t.UserData, "AfterPostBack");
444 [Category ("NunitWeb")]
445 public void ImageMap_PostBack_RenderBefore ()
447 WebTest t = new WebTest (PageInvoker.CreateOnLoad (myPageLoad));
449 string strTarget = "<img id=\"imgmap\" src=\"\" usemap=\"#ImageMapimgmap\" /><map name=\"ImageMapimgmap\" id=\"ImageMapimgmap\">\r\n\t<area shape=\"rect\" coords=\"0,0,0,0\" href=\"javascript:__doPostBack('imgmap','0')\" title=\"\" alt=\"\" /><area shape=\"poly\" coords=\"\" href=\"javascript:__doPostBack('imgmap','1')\" title=\"\" alt=\"\" /><area shape=\"circle\" coords=\"0,0,0\" href=\"javascript:__doPostBack('imgmap','2')\" title=\"\" alt=\"\" />\r\n</map>";
451 string RenderedPageHtml = t.Run ();
452 string RenderedControlHtml = HtmlDiff.GetControlFromPageHtml (RenderedPageHtml);
453 HtmlDiff.AssertAreEqual (strTarget, RenderedControlHtml, "BeforePostBack");
457 [Category ("NunitWeb")]
458 public void ImageMap_PostBack_RenderAfter ()
460 WebTest t = new WebTest (PageInvoker.CreateOnLoad (myPageLoad));
462 FormRequest fr = new FormRequest (t.Response, "form1");
463 fr.Controls.Add ("__EVENTTARGET");
464 fr.Controls.Add ("__EVENTARGUMENT");
465 fr.Controls["__EVENTTARGET"].Value = "imgmap";
466 fr.Controls["__EVENTARGUMENT"].Value = "0";
469 string strTarget = "<img id=\"imgmap\" src=\"\" usemap=\"#ImageMapimgmap\" /><map name=\"ImageMapimgmap\" id=\"ImageMapimgmap\">\r\n\t<area shape=\"rect\" coords=\"0,0,0,0\" href=\"javascript:__doPostBack('imgmap','0')\" title=\"\" alt=\"\" /><area shape=\"poly\" coords=\"\" href=\"javascript:__doPostBack('imgmap','1')\" title=\"\" alt=\"\" /><area shape=\"circle\" coords=\"0,0,0\" href=\"javascript:__doPostBack('imgmap','2')\" title=\"\" alt=\"\" />\r\n</map>";
471 string RenderedPageHtml = t.Run ();
472 string RenderedControlHtml = HtmlDiff.GetControlFromPageHtml (RenderedPageHtml);
473 HtmlDiff.AssertAreEqual (strTarget, RenderedControlHtml, "AfterPostBack");
476 public static void myPageLoad (Page page)
478 WebTest.CurrentTest.UserData = "Init";
479 ImageMap imgmap = new ImageMap ();
480 imgmap.ID = "imgmap";
481 imgmap.HotSpotMode = HotSpotMode.NotSet;
482 imgmap.Click += new ImageMapEventHandler (ImageMapClickHandler2);
483 RectangleHotSpot rect = new RectangleHotSpot ();
484 rect.HotSpotMode = HotSpotMode.PostBack;
485 rect.PostBackValue = "Rectangle";
486 imgmap.HotSpots.Add (rect);
487 PolygonHotSpot poly = new PolygonHotSpot ();
488 poly.HotSpotMode = HotSpotMode.PostBack;
489 poly.PostBackValue = "Polygon";
490 imgmap.HotSpots.Add (poly);
491 imgmap.HotSpotMode = HotSpotMode.PostBack;
492 CircleHotSpot circle = new CircleHotSpot ();
493 circle.PostBackValue = "Circle";
494 imgmap.HotSpots.Add (circle);
495 // Two marks for getting controls from form
496 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
497 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
498 page.Form.Controls.Add (lcb);
499 page.Form.Controls.Add (imgmap);
500 page.Form.Controls.Add (lce);
504 public void SetUpTest ()
509 [TestFixtureTearDown]
510 public void TearDown ()