Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestIcon.cs
index 40b893f362760f0b579263d51f83072791fa63ba..203dfe3479469582760b4703bf4bb1e137ab59cd 100644 (file)
@@ -1,13 +1,12 @@
 //
 // Icon class testing unit
 //
-// Author:
+// Authors:
+//     Gary Barnett <gary.barnett.mono@gmail.com>
+//     Sanjay Gupta <gsanjay@novell.com>
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
-//      Sanjay Gupta <gsanjay@novell.com>
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004,2006-2008 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 
 using System;
 using System.Drawing;
-using NUnit.Framework;
+using System.Drawing.Imaging;
 using System.IO;
+using System.Reflection;
 using System.Security.Permissions;
+using NUnit.Framework;
 
-namespace MonoTests.System.Drawing{
+namespace MonoTests.System.Drawing {
 
        [TestFixture]   
        [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
-       public class TestIcon {
+       public class IconTest {
                
                Icon icon;
-               Icon newIcon;
-               FileStream fs;
+               Icon icon16, icon32, icon48, icon64, icon96;
                FileStream fs1;
+
+               static string filename_dll;
+
+               // static ctor are executed outside the Deny
+               static IconTest ()
+               {
+                       filename_dll = Assembly.GetExecutingAssembly ().Location;
+               }
                
                [SetUp]
                public void SetUp ()            
@@ -52,70 +60,588 @@ namespace MonoTests.System.Drawing{
                        String path = TestBitmap.getInFile ("bitmaps/smiley.ico");
                        icon = new Icon (path);                 
                        fs1 = new FileStream (path, FileMode.Open);
+
+                       icon16 = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"));
+                       icon32 = new Icon (TestBitmap.getInFile ("bitmaps/32x32x16.ico"));
+                       icon48 = new Icon (TestBitmap.getInFile ("bitmaps/48x48x1.ico"));
+                       icon64 = new Icon (TestBitmap.getInFile ("bitmaps/64x64x256.ico"));
+                       icon96 = new Icon (TestBitmap.getInFile ("bitmaps/96x96x256.ico"));
+               }
+
+               [TearDown]
+               public void TearDown ()
+               {
+                       if (fs1 != null)
+                               fs1.Close ();
+                       if (File.Exists ("newIcon.ico"))
+                               File.Delete ("newIcon.ico");
                }
 
                [Test]
-#if TARGET_JVM
-               [Category ("NotWorking")]
-#endif
                public void TestConstructors ()
                {
-                       newIcon = new Icon (fs1, 48, 48);
+                       Assert.AreEqual (32, icon.Height, "C#0a");
+                       Assert.AreEqual (32, icon.Width, "C#0b");
+
+                       Icon newIcon = new Icon (fs1, 48, 48);
                        Assert.AreEqual (48, newIcon.Height, "C#1a");                   
                        Assert.AreEqual (48, newIcon.Width, "C#1b");
 
                        newIcon = new Icon (icon, 16, 16);
                        Assert.AreEqual (16, newIcon.Height, "C#2a");                   
                        Assert.AreEqual (16, newIcon.Width, "C#2b");
-               }                               
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_IconNull_Int_Int ()
+               {
+                       new Icon ((Icon)null, 32, 32);
+               }
+
+               [Test]
+               public void Constructor_Icon_IntNegative_Int ()
+               {
+                       Icon neg = new Icon (icon, -32, 32);
+                       Assert.AreEqual (32, neg.Height, "Height");
+                       Assert.AreEqual (32, neg.Width, "Width");
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_IconNull_Size ()
+               {
+                       new Icon ((Icon) null, new Size (32, 32));
+               }
+
+               [Test]
+               public void Constructor_Icon_Size_Negative ()
+               {
+                       Icon neg = new Icon (icon, new Size (-32, -32));
+                       Assert.AreEqual (16, neg.Height, "Height");
+                       Assert.AreEqual (16, neg.Width, "Width");
+               }
+
+               [Test]
+               public void Constructor_Icon_Int_Int_NonSquare ()
+               {
+                       Icon non_square = new Icon (icon, 32, 16);
+                       Assert.AreEqual (32, non_square.Height, "Height");
+                       Assert.AreEqual (32, non_square.Width, "Width");
+               }
+
+               [Test]
+               public void Constructor_Icon_GetNormalSizeFromIconWith256 ()
+               {
+                       string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
+
+                       Icon orig = new Icon (filepath);
+                       Assert.AreEqual (32,orig.Height);
+                       Assert.AreEqual (32,orig.Width);
+
+                       Icon ret = new Icon (orig, 48, 48);
+                       Assert.AreEqual (48, ret.Height);
+                       Assert.AreEqual (48, ret.Width);
+               }
+
+               [Test]
+               public void Constructor_Icon_DoesntReturn256Passing0 ()
+               {
+                       string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
+                       
+                       Icon orig = new Icon (filepath);
+                       Assert.AreEqual (32,orig.Height);
+                       Assert.AreEqual (32,orig.Width);
+                       
+                       Icon ret = new Icon (orig, 0, 0);
+                       Assert.AreNotEqual (0, ret.Height);
+                       Assert.AreNotEqual (0, ret.Width);
+               }
+
+               [Test]
+               public void Constructor_Icon_DoesntReturn256Passing1 ()
+               {
+                       string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
+                       
+                       Icon orig = new Icon (filepath);
+                       Assert.AreEqual (32,orig.Height);
+                       Assert.AreEqual (32,orig.Width);
+                       
+                       Icon ret = new Icon (orig, 1, 1);
+                       Assert.AreNotEqual (0, ret.Height);
+                       Assert.AreNotEqual (0, ret.Width);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_StreamNull ()
+               {
+                       new Icon ((Stream) null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_StreamNull_Int_Int ()
+               {
+                       new Icon ((Stream) null, 32, 32);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void Constructor_StringNull ()
+               {
+                       new Icon ((string) null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (NullReferenceException))]
+               public void Constructor_TypeNull_String ()
+               {
+                       new Icon ((Type) null, "mono.ico");
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_Type_StringNull ()
+               {
+                       new Icon (typeof (Icon), null);
+               }
+#if NET_2_0
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_StreamNull_Size ()
+               {
+                       new Icon ((Stream) null, new Size (32, 32));
+               }
 
                [Test]
-#if TARGET_JVM
-               [Category ("NotWorking")]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void Constructor_StringNull_Size ()
+               {
+                       new Icon ((string) null, new Size (32, 32));
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void Constructor_StringNull_Int_Int ()
+               {
+                       new Icon ((string) null, 32, 32);
+               }
 #endif
+
+               [Test]
                public void TestProperties ()
                {
                        Assert.AreEqual (32, icon.Height, "P#1");
                        Assert.AreEqual (32, icon.Width, "P#2");
                        Assert.AreEqual (32, icon.Size.Width, "P#3");
                        Assert.AreEqual (32, icon.Size.Height, "P#4");
+               }
+
+               [Test]
+               public void Clone ()
+               {
+                       Icon clone = (Icon) icon.Clone ();
+                       Assert.AreEqual (32, clone.Height, "Height");
+                       Assert.AreEqual (32, clone.Width, "Width");
+                       Assert.AreEqual (32, clone.Size.Width, "Size.Width");
+                       Assert.AreEqual (32, clone.Size.Height, "Size.Height");
+               }
+
+               [Test]
+               public void CloneHandleIcon ()
+               {
+                       Icon clone = (Icon) Icon.FromHandle (SystemIcons.Hand.Handle).Clone ();
+                       Assert.AreEqual (SystemIcons.Hand.Height, clone.Height, "Height");
+                       Assert.AreEqual (SystemIcons.Hand.Width, clone.Width, "Width");
+                       Assert.AreEqual (SystemIcons.Hand.Size.Width, clone.Size.Width, "Size.Width");
+                       Assert.AreEqual (SystemIcons.Hand.Size.Height, clone.Size.Height, "Size.Height");
+               }
+
+               private void XPIcon (int size)
+               {
+                       // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
+                       using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
+                               using (Icon xp = new Icon (fs, size, size)) {
+                                       Assert.AreEqual (size, xp.Height, "Height");
+                                       Assert.AreEqual (size, xp.Width, "Width");
+                                       Assert.AreEqual (size, xp.Size.Width, "Size.Width");
+                                       Assert.AreEqual (size, xp.Size.Height, "Size.Height");
+
+                                       Bitmap bmp = xp.ToBitmap ();
+                                       Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
+                                       Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
+                                       Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
+                                       Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
+                               }
+                       }
+               }
+
+               [Test]
+               public void Icon32bits_XP16 ()
+               {
+                       XPIcon (16);
+               }
+
+               [Test]
+               public void Icon32bits_XP32 ()
+               {
+                       XPIcon (32);
+               }
+
+               [Test]
+               public void Icon32bits_XP48 ()
+               {
+                       XPIcon (48);
+               }
+
+               [Test]
+               public void SelectFromUnusualSize_Small16 ()
+               {
+                       using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
+                               using (Icon xp = new Icon (fs, 16, 16)) {
+                                       Assert.AreEqual (16, xp.Height, "Height");
+                                       Assert.AreEqual (10, xp.Width, "Width");
+                                       Assert.AreEqual (10, xp.Size.Width, "Size.Width");
+                                       Assert.AreEqual (16, xp.Size.Height, "Size.Height");
+                               }
+                       }
+               }
+
+               [Test]
+               public void SelectFromUnusualSize_Normal32 ()
+               {
+                       using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
+                               using (Icon xp = new Icon (fs, 32, 32)) {
+                                       Assert.AreEqual (22, xp.Height, "Height");
+                                       Assert.AreEqual (11, xp.Width, "Width");
+                                       Assert.AreEqual (11, xp.Size.Width, "Size.Width");
+                                       Assert.AreEqual (22, xp.Size.Height, "Size.Height");
+                               }
+                       }
+               }
+
+               internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
+               {
+                       using (MemoryStream ms = new MemoryStream ()) {
+                               icon.Save (ms);
+                               ms.Position = 0;
+
+                               using (Icon loaded = new Icon (ms)) {
+                                       Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
+                                       Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
+
+                                       using (Bitmap expected = icon.ToBitmap ()) {
+                                               using (Bitmap actual = loaded.ToBitmap ()) {
+                                                       Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
+                                                       Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
+
+                                                       for (int y = 0; y < expected.Height; y++) {
+                                                               for (int x = 0; x < expected.Width; x++) {
+                                                                       Color e = expected.GetPixel (x, y);
+                                                                       Color a = actual.GetPixel (x, y);
+                                                                       if (alpha)
+                                                                               Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
+                                                                       Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
+                                                                       Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
+                                                                       Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               [Test]
+               public void Save ()
+               {
+                       SaveAndCompare ("16", icon16, true);
+                       SaveAndCompare ("32", icon32, true);
+                       SaveAndCompare ("48", icon48, true);
+                       SaveAndCompare ("64", icon64, true);
+                       SaveAndCompare ("96", icon96, true);
+               }
+
+               [Test] // bug #410608
+               public void Save_256 ()
+               {
+                       string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
+
+                       using (Icon icon = new Icon (filepath)) {
+                               // bug #415809 fixed
+                               SaveAndCompare ("256", icon, true);
+                       }
+
+                       // binary comparison
+                       var orig = new MemoryStream (File.ReadAllBytes (filepath));
+                       var saved = new MemoryStream ();
+                       using (Icon icon = new Icon (filepath))
+                               icon.Save (saved);
+                       FileAssert.AreEqual (orig, saved, "binary comparison");
+               }
+
+               [Test]
+               [ExpectedException (typeof (NullReferenceException))]
+               public void Save_Null ()
+               {
+                       icon.Save (null);
+               }
+
+               [Test]
+               public void Icon16ToBitmap ()
+               {
+                       using (Bitmap b = icon16.ToBitmap ()) {
+                               Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+                               // unlike the GDI+ icon decoder the palette isn't kept
+                               Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+                               Assert.AreEqual (icon16.Height, b.Height, "Height");
+                               Assert.AreEqual (icon16.Width, b.Width, "Width");
+                               Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+                               Assert.AreEqual (2, b.Flags, "Flags");
+                       }
+               }
+
+               [Test]
+               public void Icon32ToBitmap ()
+               {
+                       using (Bitmap b = icon32.ToBitmap ()) {
+                               Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+                               // unlike the GDI+ icon decoder the palette isn't kept
+                               Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+                               Assert.AreEqual (icon32.Height, b.Height, "Height");
+                               Assert.AreEqual (icon32.Width, b.Width, "Width");
+                               Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+                               Assert.AreEqual (2, b.Flags, "Flags");
+                       }
+               }
+
+               [Test]
+               public void Icon48ToBitmap ()
+               {
+                       using (Bitmap b = icon48.ToBitmap ()) {
+                               Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+                               // unlike the GDI+ icon decoder the palette isn't kept
+                               Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+                               Assert.AreEqual (icon48.Height, b.Height, "Height");
+                               Assert.AreEqual (icon48.Width, b.Width, "Width");
+                               Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+                               Assert.AreEqual (2, b.Flags, "Flags");
+                       }
+               }
+
+               [Test]
+               public void Icon64ToBitmap ()
+               {
+                       using (Bitmap b = icon64.ToBitmap ()) {
+                               Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+                               // unlike the GDI+ icon decoder the palette isn't kept
+                               Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+                               Assert.AreEqual (icon64.Height, b.Height, "Height");
+                               Assert.AreEqual (icon64.Width, b.Width, "Width");
+                               Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+                               Assert.AreEqual (2, b.Flags, "Flags");
+                       }
+               }
 
+               [Test]
+               public void Icon96ToBitmap ()
+               {
+                       using (Bitmap b = icon96.ToBitmap ()) {
+                               Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+                               // unlike the GDI+ icon decoder the palette isn't kept
+                               Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+                               Assert.AreEqual (icon96.Height, b.Height, "Height");
+                               Assert.AreEqual (icon96.Width, b.Width, "Width");
+                               Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+                               Assert.AreEqual (2, b.Flags, "Flags");
+                       }
+               }
+
+               [Test] // bug #415581
+               public void Icon256ToBitmap ()
+               {
+                       using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
+                               Icon icon = new Icon (fs, 48, 48);
+                               using (Bitmap b = icon.ToBitmap ()) {
+                                       Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
+                                       Assert.AreEqual (48, b.Height, "#A2");
+                                       Assert.AreEqual (48, b.Width, "#A3");
+                                       Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
+                                       Assert.AreEqual (2, b.Flags, "#A5");
+                               }
+                               icon.Dispose ();
+                       }
+
+                       using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
+                               Icon icon = new Icon (fs, 256, 256);
+                               using (Bitmap b = icon.ToBitmap ()) {
+                                       Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
+                                       Assert.AreEqual (48, b.Height, "#B2");
+                                       Assert.AreEqual (48, b.Width, "#B3");
+                                       Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
+                                       Assert.AreEqual (2, b.Flags, "#B5");
+                               }
+                       }
+               }
+
+               [Test]
+               public void Icon256ToBitmap_Request0 ()
+               {
+                       // 415581.ico has 2 images, the 256 and 48
+                       using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
+                               Icon icon = new Icon (fs, 0, 0);
+                               using (Bitmap b = icon.ToBitmap ()) {
+                                       Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
+                                       Assert.AreEqual (48, b.Height, "#B2");
+                                       Assert.AreEqual (48, b.Width, "#B3");
+                                       Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
+                                       Assert.AreEqual (2, b.Flags, "#B5");
+                               }
+                       }
+               }
+
+               [Test, ExpectedException ()] //ToDo: System.ComponentModel.Win32Exception
+               public void Only256InFile ()
+               {
+                       using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/only256.ico"))) {
+                               Icon icon = new Icon (fs, 0, 0);
+                       }
+               }
+
+
+#if NET_2_0
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void ExtractAssociatedIcon_Null ()
+               {
+                       Icon.ExtractAssociatedIcon (null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void ExtractAssociatedIcon_Empty ()
+               {
+                       Icon.ExtractAssociatedIcon (String.Empty);
+               }
+
+               [Test]
+               [ExpectedException (typeof (FileNotFoundException))]
+               public void ExtractAssociatedIcon_DoesNotExists ()
+               {
+                       Icon.ExtractAssociatedIcon ("does-not-exists.png");
                }
+#endif
+
+               private static bool RunningOnUnix {
+                       get {
+                               int p = (int) Environment.OSVersion.Platform;
 
+                               return (p == 4) || (p == 6) || (p == 128);
+                       }
+               }
+       }
+
+       [TestFixture]
+       public class IconFullTrustTest {
+#if NET_2_0
                [Test]
-#if TARGET_JVM
-               [Category ("NotWorking")]
+               public void ExtractAssociatedIcon ()
+               {
+                       string filename_dll = Assembly.GetExecutingAssembly ().Location;
+                       Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
+               }
 #endif
-               public void TestMethods ()
+
+               [Test]
+               public void HandleRoundtrip ()
                {
-                       /*
-                       
-                       TODO: This does not work on Win32
-                       
-                       newIcon = (Icon) icon.Clone ();
-                       Assert.AreEqual (32, newIcon.Height, "M#1a");
-                       Assert.AreEqual (32, newIcon.Width, "M#1b");
-                       
-                       Bitmap bmp = icon.ToBitmap();
-                       Assert.AreEqual (32, bmp.Height, "M#2a");
-                       Assert.AreEqual (32, bmp.Width, "M#2b");
-                       */
-                       
-                       fs = new FileStream ("newIcon.ico", FileMode.Create);
-                       icon.Save (fs);
-                       
-                       Assert.AreEqual (fs1.Length, fs.Length, "M#3");                 
+                       IntPtr handle;
+                       using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+                               Assert.AreEqual (16, icon.Height, "Original.Height");
+                               Assert.AreEqual (16, icon.Width, "Original.Width");
+                               handle = icon.Handle;
+                               using (Icon icon2 = Icon.FromHandle (handle)) {
+                                       Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
+                                       Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
+                                       Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
+                                       IconTest.SaveAndCompare ("Handle", icon2, false);
+                               }
+                       }
+                       // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
+                       // commented / using freed memory is risky ;-)
+                       /*using (Icon icon3 = Icon.FromHandle (handle)) {
+                               Assert.AreEqual (0, icon3.Height, "Survivor.Height");
+                               Assert.AreEqual (0, icon3.Width, "Survivor.Width");
+                               Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
+                       }*/
                }
 
-               [TearDown]
-               public void TearDown () 
+               [Test]
+               public void CreateMultipleIconFromSameHandle ()
                {
-                       if (fs != null)
-                               fs.Close();
-                       if (fs1 != null)
-                               fs1.Close();
-                       if (File.Exists ("newIcon.ico"))
-                               File.Delete("newIcon.ico");
+                       IntPtr handle;
+                       using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+                               Assert.AreEqual (16, icon.Height, "Original.Height");
+                               Assert.AreEqual (16, icon.Width, "Original.Width");
+                               handle = icon.Handle;
+                               using (Icon icon2 = Icon.FromHandle (handle)) {
+                                       Assert.AreEqual (16, icon2.Height, "2.Height");
+                                       Assert.AreEqual (16, icon2.Width, "2.Width");
+                                       Assert.AreEqual (handle, icon2.Handle, "2.Handle");
+                                       IconTest.SaveAndCompare ("Handle2", icon2, false);
+                               }
+                               using (Icon icon3 = Icon.FromHandle (handle)) {
+                                       Assert.AreEqual (16, icon3.Height, "3.Height");
+                                       Assert.AreEqual (16, icon3.Width, "3.Width");
+                                       Assert.AreEqual (handle, icon3.Handle, "3.Handle");
+                                       IconTest.SaveAndCompare ("Handle3", icon3, false);
+                               }
+                       }
+                       // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
+                       // commented / using freed memory is risky ;-)
+               }
+
+               [Test]
+               public void HiconRoundtrip ()
+               {
+                       IntPtr handle;
+                       using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+                               Assert.AreEqual (16, icon.Height, "Original.Height");
+                               Assert.AreEqual (16, icon.Width, "Original.Width");
+                               handle = icon.ToBitmap ().GetHicon ();
+                       }
+                       // HICON survives
+                       using (Icon icon2 = Icon.FromHandle (handle)) {
+                               Assert.AreEqual (16, icon2.Height, "Survivor.Height");
+                               Assert.AreEqual (16, icon2.Width, "Survivor.Width");
+                               Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
+                               IconTest.SaveAndCompare ("HICON", icon2, false);
+                       }
+               }
+
+               [Test]
+               public void CreateMultipleIconFromSameHICON ()
+               {
+                       IntPtr handle;
+                       using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+                               Assert.AreEqual (16, icon.Height, "Original.Height");
+                               Assert.AreEqual (16, icon.Width, "Original.Width");
+                               handle = icon.ToBitmap ().GetHicon ();
+                       }
+                       // HICON survives
+                       using (Icon icon2 = Icon.FromHandle (handle)) {
+                               Assert.AreEqual (16, icon2.Height, "2.Height");
+                               Assert.AreEqual (16, icon2.Width, "2.Width");
+                               Assert.AreEqual (handle, icon2.Handle, "2.Handle");
+                               IconTest.SaveAndCompare ("HICON2", icon2, false);
+                       }
+                       using (Icon icon3 = Icon.FromHandle (handle)) {
+                               Assert.AreEqual (16, icon3.Height, "3.Height");
+                               Assert.AreEqual (16, icon3.Width, "3.Width");
+                               Assert.AreEqual (handle, icon3.Handle, "3.Handle");
+                               IconTest.SaveAndCompare ("HICON", icon3, false);
+                       }
                }
        }
 }