2 // Icon class testing unit
5 // Sanjay Gupta <gsanjay@novell.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004,2006-2008 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Drawing.Imaging;
34 using System.Reflection;
35 using System.Security.Permissions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Drawing {
41 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42 public class IconTest {
45 Icon icon16, icon32, icon48, icon64, icon96;
48 static string filename_dll;
50 // static ctor are executed outside the Deny
53 filename_dll = Assembly.GetExecutingAssembly ().Location;
59 String path = TestBitmap.getInFile ("bitmaps/smiley.ico");
60 icon = new Icon (path);
61 fs1 = new FileStream (path, FileMode.Open);
63 icon16 = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"));
64 icon32 = new Icon (TestBitmap.getInFile ("bitmaps/32x32x16.ico"));
65 icon48 = new Icon (TestBitmap.getInFile ("bitmaps/48x48x1.ico"));
66 icon64 = new Icon (TestBitmap.getInFile ("bitmaps/64x64x256.ico"));
67 icon96 = new Icon (TestBitmap.getInFile ("bitmaps/96x96x256.ico"));
71 public void TearDown ()
75 if (File.Exists ("newIcon.ico"))
76 File.Delete ("newIcon.ico");
80 public void TestConstructors ()
82 Assert.AreEqual (32, icon.Height, "C#0a");
83 Assert.AreEqual (32, icon.Width, "C#0b");
85 Icon newIcon = new Icon (fs1, 48, 48);
86 Assert.AreEqual (48, newIcon.Height, "C#1a");
87 Assert.AreEqual (48, newIcon.Width, "C#1b");
89 newIcon = new Icon (icon, 16, 16);
90 Assert.AreEqual (16, newIcon.Height, "C#2a");
91 Assert.AreEqual (16, newIcon.Width, "C#2b");
95 [ExpectedException (typeof (ArgumentException))]
97 [Ignore ("Check parameters")]
99 public void Constructor_IconNull_Int_Int ()
101 new Icon ((Icon)null, 32, 32);
106 [Ignore ("Constructor_Icon_IntNegative_Int Not Working")]
108 public void Constructor_Icon_IntNegative_Int ()
110 Icon neg = new Icon (icon, -32, 32);
111 Assert.AreEqual (32, neg.Height, "Height");
112 Assert.AreEqual (32, neg.Width, "Width");
116 [ExpectedException (typeof (ArgumentException))]
118 [Ignore ("Check parameters")]
120 public void Constructor_IconNull_Size ()
122 new Icon ((Icon) null, new Size (32, 32));
127 [Ignore ("Constructor_Icon_Size_Negative Not Working")]
129 public void Constructor_Icon_Size_Negative ()
131 Icon neg = new Icon (icon, new Size (-32, -32));
132 Assert.AreEqual (16, neg.Height, "Height");
133 Assert.AreEqual (16, neg.Width, "Width");
138 [Ignore ("Constructor_Icon_Int_Int_NonSquare Not Working")]
140 public void Constructor_Icon_Int_Int_NonSquare ()
142 Icon non_square = new Icon (icon, 32, 16);
143 Assert.AreEqual (32, non_square.Height, "Height");
144 Assert.AreEqual (32, non_square.Width, "Width");
148 [ExpectedException (typeof (ArgumentException))]
149 public void Constructor_StreamNull ()
151 new Icon ((Stream) null);
155 [ExpectedException (typeof (ArgumentException))]
156 public void Constructor_StreamNull_Int_Int ()
158 new Icon ((Stream) null, 32, 32);
162 [ExpectedException (typeof (ArgumentNullException))]
163 public void Constructor_StringNull ()
165 new Icon ((string) null);
169 [ExpectedException (typeof (NullReferenceException))]
170 public void Constructor_TypeNull_String ()
172 new Icon ((Type) null, "mono.ico");
176 [ExpectedException (typeof (ArgumentException))]
178 [Ignore ("Check parameters")]
180 public void Constructor_Type_StringNull ()
182 new Icon (typeof (Icon), null);
186 [ExpectedException (typeof (ArgumentException))]
188 [Ignore ("Constructor_StreamNull_Size Not Implemented")]
190 public void Constructor_StreamNull_Size ()
193 new Icon ((Stream) null, new Size (32, 32));
198 [ExpectedException (typeof (ArgumentNullException))]
200 [Ignore ("Constructor_StringNull_Size Not Implemented")]
202 public void Constructor_StringNull_Size ()
205 new Icon ((string) null, new Size (32, 32));
210 [ExpectedException (typeof (ArgumentNullException))]
212 [Ignore ("Constructor_StringNull_Int_Int Not Implemented")]
214 public void Constructor_StringNull_Int_Int ()
217 new Icon ((string) null, 32, 32);
223 public void TestProperties ()
225 Assert.AreEqual (32, icon.Height, "P#1");
226 Assert.AreEqual (32, icon.Width, "P#2");
227 Assert.AreEqual (32, icon.Size.Width, "P#3");
228 Assert.AreEqual (32, icon.Size.Height, "P#4");
234 Icon clone = (Icon) icon.Clone ();
235 Assert.AreEqual (32, clone.Height, "Height");
236 Assert.AreEqual (32, clone.Width, "Width");
237 Assert.AreEqual (32, clone.Size.Width, "Size.Width");
238 Assert.AreEqual (32, clone.Size.Height, "Size.Height");
243 public void CloneHandleIcon ()
245 Icon clone = (Icon) Icon.FromHandle (SystemIcons.Hand.Handle).Clone ();
246 Assert.AreEqual (SystemIcons.Hand.Height, clone.Height, "Height");
247 Assert.AreEqual (SystemIcons.Hand.Width, clone.Width, "Width");
248 Assert.AreEqual (SystemIcons.Hand.Size.Width, clone.Size.Width, "Size.Width");
249 Assert.AreEqual (SystemIcons.Hand.Size.Height, clone.Size.Height, "Size.Height");
253 private void XPIcon (int size)
255 // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
256 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
257 using (Icon xp = new Icon (fs, size, size)) {
258 Assert.AreEqual (size, xp.Height, "Height");
259 Assert.AreEqual (size, xp.Width, "Width");
260 Assert.AreEqual (size, xp.Size.Width, "Size.Width");
261 Assert.AreEqual (size, xp.Size.Height, "Size.Height");
263 Bitmap bmp = xp.ToBitmap ();
264 Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
265 Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
266 Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
267 Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
273 public void Icon32bits_XP16 ()
279 public void Icon32bits_XP32 ()
285 public void Icon32bits_XP48 ()
292 [Ignore ("SelectFromUnusualSize_Small16 Not Working")]
294 public void SelectFromUnusualSize_Small16 ()
296 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
297 using (Icon xp = new Icon (fs, 16, 16)) {
298 Assert.AreEqual (16, xp.Height, "Height");
299 Assert.AreEqual (10, xp.Width, "Width");
300 Assert.AreEqual (10, xp.Size.Width, "Size.Width");
301 Assert.AreEqual (16, xp.Size.Height, "Size.Height");
307 public void SelectFromUnusualSize_Normal32 ()
309 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
310 using (Icon xp = new Icon (fs, 32, 32)) {
311 Assert.AreEqual (22, xp.Height, "Height");
312 Assert.AreEqual (11, xp.Width, "Width");
313 Assert.AreEqual (11, xp.Size.Width, "Size.Width");
314 Assert.AreEqual (22, xp.Size.Height, "Size.Height");
319 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
321 using (MemoryStream ms = new MemoryStream ()) {
325 using (Icon loaded = new Icon (ms)) {
326 Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
327 Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
329 using (Bitmap expected = icon.ToBitmap ()) {
330 using (Bitmap actual = loaded.ToBitmap ()) {
331 Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
332 Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
334 for (int y = 0; y < expected.Height; y++) {
335 for (int x = 0; x < expected.Width; x++) {
336 Color e = expected.GetPixel (x, y);
337 Color a = actual.GetPixel (x, y);
339 Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
340 Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
341 Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
342 Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
354 SaveAndCompare ("16", icon16, true);
355 SaveAndCompare ("32", icon32, true);
356 SaveAndCompare ("48", icon48, true);
357 SaveAndCompare ("64", icon64, true);
358 SaveAndCompare ("96", icon96, true);
361 [Test] // bug #410608
362 public void Save_256 ()
365 Assert.Ignore ("Depends on bug #323511");
367 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/323511.ico"))) {
368 // FIXME: use this instead after bug #415809 is fixed
369 //SaveAndCompare ("256", icon, true);
371 MemoryStream ms = new MemoryStream ();
375 using (Icon loaded = new Icon (ms)) {
376 Assert.AreEqual (icon.Height, loaded.Height, "Loaded.Height");
377 Assert.AreEqual (icon.Width, loaded.Width, "Loaded.Width");
383 [ExpectedException (typeof (NullReferenceException))]
385 [Ignore ("Throws NullReference, do we need to follow?")]
387 public void Save_Null ()
394 [Ignore ("Icon16ToBitmap Not Working")]
396 public void Icon16ToBitmap ()
398 using (Bitmap b = icon16.ToBitmap ()) {
399 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
400 // unlike the GDI+ icon decoder the palette isn't kept
401 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
402 Assert.AreEqual (icon16.Height, b.Height, "Height");
403 Assert.AreEqual (icon16.Width, b.Width, "Width");
404 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
405 Assert.AreEqual (2, b.Flags, "Flags");
411 [Ignore ("Icon32ToBitmap Not Working")]
413 public void Icon32ToBitmap ()
415 using (Bitmap b = icon32.ToBitmap ()) {
416 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
417 // unlike the GDI+ icon decoder the palette isn't kept
418 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
419 Assert.AreEqual (icon32.Height, b.Height, "Height");
420 Assert.AreEqual (icon32.Width, b.Width, "Width");
421 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
422 Assert.AreEqual (2, b.Flags, "Flags");
428 [Ignore ("Icon48ToBitmap Not Working")]
430 public void Icon48ToBitmap ()
432 using (Bitmap b = icon48.ToBitmap ()) {
433 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
434 // unlike the GDI+ icon decoder the palette isn't kept
435 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
436 Assert.AreEqual (icon48.Height, b.Height, "Height");
437 Assert.AreEqual (icon48.Width, b.Width, "Width");
438 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
439 Assert.AreEqual (2, b.Flags, "Flags");
445 [Ignore ("Icon64ToBitmap Not Working")]
447 public void Icon64ToBitmap ()
449 using (Bitmap b = icon64.ToBitmap ()) {
450 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
451 // unlike the GDI+ icon decoder the palette isn't kept
452 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
453 Assert.AreEqual (icon64.Height, b.Height, "Height");
454 Assert.AreEqual (icon64.Width, b.Width, "Width");
455 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
456 Assert.AreEqual (2, b.Flags, "Flags");
462 [Ignore ("Icon96ToBitmap Not Working")]
464 public void Icon96ToBitmap ()
466 using (Bitmap b = icon96.ToBitmap ()) {
467 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
468 // unlike the GDI+ icon decoder the palette isn't kept
469 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
470 Assert.AreEqual (icon96.Height, b.Height, "Height");
471 Assert.AreEqual (icon96.Width, b.Width, "Width");
472 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
473 Assert.AreEqual (2, b.Flags, "Flags");
477 [Test] // bug #415581
479 [Ignore ("Icon256ToBitmap Not Working")]
481 public void Icon256ToBitmap ()
484 Assert.Ignore ("Depends on bug #323511");
486 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
487 Icon icon = new Icon (fs, 48, 48);
488 using (Bitmap b = icon.ToBitmap ()) {
489 Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
490 Assert.AreEqual (48, b.Height, "#A2");
491 Assert.AreEqual (48, b.Width, "#A3");
492 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
493 Assert.AreEqual (2, b.Flags, "#A5");
498 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
499 Icon icon = new Icon (fs, 256, 256);
500 using (Bitmap b = icon.ToBitmap ()) {
501 Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
502 Assert.AreEqual (48, b.Height, "#B2");
503 Assert.AreEqual (48, b.Width, "#B3");
504 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
505 Assert.AreEqual (2, b.Flags, "#B5");
512 [ExpectedException (typeof (ArgumentException))]
514 [Ignore ("ExtractAssociatedIcon is not implemented")]
516 public void ExtractAssociatedIcon_Null ()
519 Icon.ExtractAssociatedIcon (null);
524 [ExpectedException (typeof (ArgumentException))]
526 [Ignore ("ExtractAssociatedIcon is not implemented")]
528 public void ExtractAssociatedIcon_Empty ()
531 Icon.ExtractAssociatedIcon (String.Empty);
536 [ExpectedException (typeof (FileNotFoundException))]
538 [Ignore ("ExtractAssociatedIcon is not implemented")]
540 public void ExtractAssociatedIcon_DoesNotExists ()
543 Icon.ExtractAssociatedIcon ("does-not-exists.png");
548 private static bool RunningOnUnix {
550 int p = (int) Environment.OSVersion.Platform;
552 return (p == 4) || (p == 6) || (p == 128);
559 [Ignore ("Unsafe code is not supported")]
561 public class IconFullTrustTest {
565 public void ExtractAssociatedIcon ()
567 string filename_dll = Assembly.GetExecutingAssembly ().Location;
568 Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
573 public void HandleRoundtrip ()
576 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
577 Assert.AreEqual (16, icon.Height, "Original.Height");
578 Assert.AreEqual (16, icon.Width, "Original.Width");
579 handle = icon.Handle;
580 using (Icon icon2 = Icon.FromHandle (handle)) {
581 Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
582 Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
583 Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
584 IconTest.SaveAndCompare ("Handle", icon2, false);
587 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
588 // commented / using freed memory is risky ;-)
589 /*using (Icon icon3 = Icon.FromHandle (handle)) {
590 Assert.AreEqual (0, icon3.Height, "Survivor.Height");
591 Assert.AreEqual (0, icon3.Width, "Survivor.Width");
592 Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
597 public void CreateMultipleIconFromSameHandle ()
600 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
601 Assert.AreEqual (16, icon.Height, "Original.Height");
602 Assert.AreEqual (16, icon.Width, "Original.Width");
603 handle = icon.Handle;
604 using (Icon icon2 = Icon.FromHandle (handle)) {
605 Assert.AreEqual (16, icon2.Height, "2.Height");
606 Assert.AreEqual (16, icon2.Width, "2.Width");
607 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
608 IconTest.SaveAndCompare ("Handle2", icon2, false);
610 using (Icon icon3 = Icon.FromHandle (handle)) {
611 Assert.AreEqual (16, icon3.Height, "3.Height");
612 Assert.AreEqual (16, icon3.Width, "3.Width");
613 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
614 IconTest.SaveAndCompare ("Handle3", icon3, false);
617 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
618 // commented / using freed memory is risky ;-)
622 public void HiconRoundtrip ()
625 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
626 Assert.AreEqual (16, icon.Height, "Original.Height");
627 Assert.AreEqual (16, icon.Width, "Original.Width");
628 handle = icon.ToBitmap ().GetHicon ();
631 using (Icon icon2 = Icon.FromHandle (handle)) {
632 Assert.AreEqual (16, icon2.Height, "Survivor.Height");
633 Assert.AreEqual (16, icon2.Width, "Survivor.Width");
634 Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
635 IconTest.SaveAndCompare ("HICON", icon2, false);
640 public void CreateMultipleIconFromSameHICON ()
643 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
644 Assert.AreEqual (16, icon.Height, "Original.Height");
645 Assert.AreEqual (16, icon.Width, "Original.Width");
646 handle = icon.ToBitmap ().GetHicon ();
649 using (Icon icon2 = Icon.FromHandle (handle)) {
650 Assert.AreEqual (16, icon2.Height, "2.Height");
651 Assert.AreEqual (16, icon2.Width, "2.Width");
652 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
653 IconTest.SaveAndCompare ("HICON2", icon2, false);
655 using (Icon icon3 = Icon.FromHandle (handle)) {
656 Assert.AreEqual (16, icon3.Height, "3.Height");
657 Assert.AreEqual (16, icon3.Width, "3.Width");
658 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
659 IconTest.SaveAndCompare ("HICON", icon3, false);