Author: spouliot
Date: 2007-01-12 10:06:20 -0500 (Fri, 12 Jan 2007)
New Revision: 70922

Modified:
   trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog
   trunk/mcs/class/System.Drawing/Test/System.Drawing/TestBitmap.cs
   trunk/mcs/class/System.Drawing/Test/System.Drawing/TestIcon.cs
Log:
2007-01-12  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * TestBitmap.cs: Add test cases for FromHicon and roundtripping using
        GetHbitmap and FromHbitmap.
        * TestIcon.cs: Add test cases for all constructors. Add test cases for
        saving icons (all sizes). Add test cases for roundtripping icons using
        Handle and HICON.



Modified: trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog
===================================================================
--- trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog        
2007-01-12 15:02:40 UTC (rev 70921)
+++ trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog        
2007-01-12 15:06:20 UTC (rev 70922)
@@ -1,3 +1,11 @@
+2007-01-12  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * TestBitmap.cs: Add test cases for FromHicon and roundtripping using
+       GetHbitmap and FromHbitmap.
+       * TestIcon.cs: Add test cases for all constructors. Add test cases for
+       saving icons (all sizes). Add test cases for roundtripping icons using
+       Handle and HICON.
+
 2007-01-11  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * GDIPlusTest.cs: Add test case for loading an icon and playing with

Modified: trunk/mcs/class/System.Drawing/Test/System.Drawing/TestBitmap.cs
===================================================================
--- trunk/mcs/class/System.Drawing/Test/System.Drawing/TestBitmap.cs    
2007-01-12 15:02:40 UTC (rev 70921)
+++ trunk/mcs/class/System.Drawing/Test/System.Drawing/TestBitmap.cs    
2007-01-12 15:06:20 UTC (rev 70922)
@@ -783,5 +783,131 @@
                        }
                }
        }
+
+       [TestFixture]
+       public class BitmapFullTrustTest {
+
+               // BitmapFromHicon## is *almost* the same as 
IconTest.Icon##ToBitmap except
+               // for the Flags property
+
+               private void HiconTest (string msg, Bitmap b, int size)
+               {
+                       Assert.AreEqual (PixelFormat.Format32bppArgb, 
b.PixelFormat, msg + ".PixelFormat");
+                       // unlike the GDI+ icon decoder the palette isn't kept
+                       Assert.AreEqual (0, b.Palette.Entries.Length, msg + 
".Palette");
+                       Assert.AreEqual (size, b.Height, msg + ".Height");
+                       Assert.AreEqual (size, b.Width, msg + ".Width");
+                       Assert.IsTrue (b.RawFormat.Equals 
(ImageFormat.MemoryBmp), msg + ".RawFormat");
+                       Assert.AreEqual (335888, b.Flags, msg + ".Flags");
+               }
+
+               [Test]
+               public void Hicon16 ()
+               {
+                       IntPtr hicon;
+                       int size;
+                       using (Icon icon = new Icon (TestBitmap.getInFile 
("bitmaps/16x16x16.ico"))) {
+                               size = icon.Width;
+                               using (Bitmap bitmap = Bitmap.FromHicon 
(icon.Handle)) {
+                                       HiconTest ("Icon.Handle/FromHicon", 
bitmap, size);
+                                       hicon = bitmap.GetHicon ();
+                               }
+                       }
+                       using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
+                               // hicon survives bitmap and icon disposal
+                               HiconTest ("GetHicon/FromHicon", bitmap2, size);
+                       }
+               }
+
+               [Test]
+               public void Hicon32 ()
+               {
+                       IntPtr hicon;
+                       int size;
+                       using (Icon icon = new Icon (TestBitmap.getInFile 
("bitmaps/32x32x16.ico"))) {
+                               size = icon.Width;
+                               using (Bitmap bitmap = Bitmap.FromHicon 
(icon.Handle)) {
+                                       HiconTest ("Icon.Handle/FromHicon", 
bitmap, size);
+                                       hicon = bitmap.GetHicon ();
+                               }
+                       }
+                       using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
+                               // hicon survives bitmap and icon disposal
+                               HiconTest ("GetHicon/FromHicon", bitmap2, size);
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               [Category ("NotWorking")] // libgdiplus has lost track of the 
original 1bpp state
+               public void Hicon48 ()
+               {
+                       using (Icon icon = new Icon (TestBitmap.getInFile 
("bitmaps/48x48x1.ico"))) {
+                               // looks like 1bbp icons aren't welcome as 
bitmaps ;-)
+                               Bitmap.FromHicon (icon.Handle);
+                       }
+               }
+
+               [Test]
+               public void Hicon64 ()
+               {
+                       IntPtr hicon;
+                       int size;
+                       using (Icon icon = new Icon (TestBitmap.getInFile 
("bitmaps/64x64x256.ico"))) {
+                               size = icon.Width;
+                               using (Bitmap bitmap = Bitmap.FromHicon 
(icon.Handle)) {
+                                       HiconTest ("Icon.Handle/FromHicon", 
bitmap, size);
+                                       hicon = bitmap.GetHicon ();
+                               }
+                       }
+                       using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
+                               // hicon survives bitmap and icon disposal
+                               HiconTest ("GetHicon/FromHicon", bitmap2, size);
+                       }
+               }
+
+               [Test]
+               public void Hicon96 ()
+               {
+                       IntPtr hicon;
+                       int size;
+                       using (Icon icon = new Icon (TestBitmap.getInFile 
("bitmaps/96x96x256.ico"))) {
+                               size = icon.Width;
+                               using (Bitmap bitmap = Bitmap.FromHicon 
(icon.Handle)) {
+                                       HiconTest ("Icon.Handle/FromHicon", 
bitmap, size);
+                                       hicon = bitmap.GetHicon ();
+                               }
+                       }
+                       using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
+                               // hicon survives bitmap and icon disposal
+                               HiconTest ("GetHicon/FromHicon", bitmap2, size);
+                       }
+               }
+
+               [Test]
+               public void HBitmap ()
+               {
+                       IntPtr hbitmap;
+                       string sInFile = TestBitmap.getInFile 
("bitmaps/almogaver24bits.bmp");
+                       using (Bitmap bitmap = new Bitmap (sInFile)) {
+                               Assert.AreEqual (PixelFormat.Format24bppRgb, 
bitmap.PixelFormat, "Original.PixelFormat");
+                               Assert.AreEqual (0, 
bitmap.Palette.Entries.Length, "Original.Palette");
+                               Assert.AreEqual (183, bitmap.Height, 
"Original.Height");
+                               Assert.AreEqual (173, bitmap.Width, 
"Original.Width");
+                               Assert.AreEqual (73744, bitmap.Flags, 
"Original.Flags");
+                               Assert.IsTrue (bitmap.RawFormat.Equals 
(ImageFormat.Bmp), "Original.RawFormat");
+                               hbitmap = bitmap.GetHbitmap ();
+                       }
+                       // hbitmap survives original bitmap disposal
+                       using (Image image = Image.FromHbitmap (hbitmap)) {
+                               //Assert.AreEqual (PixelFormat.Format32bppRgb, 
image.PixelFormat, "FromHbitmap.PixelFormat");
+                               Assert.AreEqual (0, 
image.Palette.Entries.Length, "FromHbitmap.Palette");
+                               Assert.AreEqual (183, image.Height, 
"FromHbitmap.Height");
+                               Assert.AreEqual (173, image.Width, 
"FromHbitmap.Width");
+                               Assert.AreEqual (335888, image.Flags, 
"FromHbitmap.Flags");
+                               Assert.IsTrue (image.RawFormat.Equals 
(ImageFormat.MemoryBmp), "FromHbitmap.RawFormat");
+                       }
+               }
+       }
 }
 

Modified: trunk/mcs/class/System.Drawing/Test/System.Drawing/TestIcon.cs
===================================================================
--- trunk/mcs/class/System.Drawing/Test/System.Drawing/TestIcon.cs      
2007-01-12 15:02:40 UTC (rev 70921)
+++ trunk/mcs/class/System.Drawing/Test/System.Drawing/TestIcon.cs      
2007-01-12 15:06:20 UTC (rev 70922)
@@ -42,9 +42,7 @@
        public class IconTest {
                
                Icon icon;
-               Icon newIcon;
                Icon icon16, icon32, icon48, icon64, icon96;
-               FileStream fs;
                FileStream fs1;
 
                static string filename_dll;
@@ -72,8 +70,6 @@
                [TearDown]
                public void TearDown ()
                {
-                       if (fs != null)
-                               fs.Close ();
                        if (fs1 != null)
                                fs1.Close ();
                        if (File.Exists ("newIcon.ico"))
@@ -89,16 +85,111 @@
                        Assert.AreEqual (32, icon.Height, "C#0a");
                        Assert.AreEqual (32, icon.Width, "C#0b");
 
-                       newIcon = new Icon (fs1, 48, 48);
+                       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]
+               [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]
+               [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]
 #if TARGET_JVM
                [Category ("NotWorking")]
 #endif
@@ -114,28 +205,42 @@
 #if TARGET_JVM
                [Category ("NotWorking")]
 #endif
-               public void TestMethods ()
+               public void Clone ()
                {
-                       /*
-                       
-                       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");         
        
+                       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");
                }
 
+               internal static void SaveAndCompare (string msg, Icon icon)
+               {
+                       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");
+                               }
+                       }
+               }
+
                [Test]
+#if TARGET_JVM
+               [Category ("NotWorking")]
+#endif
+               public void Save ()
+               {
+                       SaveAndCompare ("16", icon16);
+                       SaveAndCompare ("32", icon32);
+                       SaveAndCompare ("48", icon48);
+                       SaveAndCompare ("64", icon64);
+                       SaveAndCompare ("96", icon96);
+               }
+
+               [Test]
                public void Icon16ToBitmap ()
                {
                        using (Bitmap b = icon16.ToBitmap ()) {
@@ -239,5 +344,49 @@
                        Assert.IsNotNull (Icon.ExtractAssociatedIcon 
(filename_dll), "dll");
                }
 #endif
+
+               [Test]
+               public void HandleRoundtrip ()
+               {
+                       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");
+// enable when Save is enabled for re-constructed icons
+//                                     IconTest.SaveAndCompare ("Handle", 
icon2);
+                               }
+                       }
+                       // 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");
+                       }*/
+               }
+
+               [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");
+// enable when Save is enabled for re-constructed icons
+//                             IconTest.SaveAndCompare ("HICON", icon2);
+                       }
+               }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to