2 // Icon class testing unit
5 // Gary Barnett <gary.barnett.mono@gmail.com>
6 // Sanjay Gupta <gsanjay@novell.com>
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2004,2006-2008 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Drawing.Imaging;
35 using System.Reflection;
36 using System.Security.Permissions;
37 using NUnit.Framework;
39 namespace MonoTests.System.Drawing {
42 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43 public class IconTest {
46 Icon icon16, icon32, icon48, icon64, icon96;
49 static string filename_dll;
51 // static ctor are executed outside the Deny
54 filename_dll = Assembly.GetExecutingAssembly ().Location;
60 String path = TestBitmap.getInFile ("bitmaps/smiley.ico");
61 icon = new Icon (path);
62 fs1 = new FileStream (path, FileMode.Open);
64 icon16 = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"));
65 icon32 = new Icon (TestBitmap.getInFile ("bitmaps/32x32x16.ico"));
66 icon48 = new Icon (TestBitmap.getInFile ("bitmaps/48x48x1.ico"));
67 icon64 = new Icon (TestBitmap.getInFile ("bitmaps/64x64x256.ico"));
68 icon96 = new Icon (TestBitmap.getInFile ("bitmaps/96x96x256.ico"));
72 public void TearDown ()
76 if (File.Exists ("newIcon.ico"))
77 File.Delete ("newIcon.ico");
81 public void TestConstructors ()
83 Assert.AreEqual (32, icon.Height, "C#0a");
84 Assert.AreEqual (32, icon.Width, "C#0b");
86 Icon newIcon = new Icon (fs1, 48, 48);
87 Assert.AreEqual (48, newIcon.Height, "C#1a");
88 Assert.AreEqual (48, newIcon.Width, "C#1b");
90 newIcon = new Icon (icon, 16, 16);
91 Assert.AreEqual (16, newIcon.Height, "C#2a");
92 Assert.AreEqual (16, newIcon.Width, "C#2b");
96 [ExpectedException (typeof (ArgumentException))]
98 [Ignore ("Check parameters")]
100 public void Constructor_IconNull_Int_Int ()
102 new Icon ((Icon)null, 32, 32);
107 [Ignore ("Constructor_Icon_IntNegative_Int Not Working")]
109 public void Constructor_Icon_IntNegative_Int ()
111 Icon neg = new Icon (icon, -32, 32);
112 Assert.AreEqual (32, neg.Height, "Height");
113 Assert.AreEqual (32, neg.Width, "Width");
117 [ExpectedException (typeof (ArgumentException))]
119 [Ignore ("Check parameters")]
121 public void Constructor_IconNull_Size ()
123 new Icon ((Icon) null, new Size (32, 32));
128 [Ignore ("Constructor_Icon_Size_Negative Not Working")]
130 public void Constructor_Icon_Size_Negative ()
132 Icon neg = new Icon (icon, new Size (-32, -32));
133 Assert.AreEqual (16, neg.Height, "Height");
134 Assert.AreEqual (16, neg.Width, "Width");
139 [Ignore ("Constructor_Icon_Int_Int_NonSquare Not Working")]
141 public void Constructor_Icon_Int_Int_NonSquare ()
143 Icon non_square = new Icon (icon, 32, 16);
144 Assert.AreEqual (32, non_square.Height, "Height");
145 Assert.AreEqual (32, non_square.Width, "Width");
149 public void Constructor_Icon_GetNormalSizeFromIconWith256 ()
151 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
153 Icon orig = new Icon (filepath);
154 Assert.AreEqual (32,orig.Height);
155 Assert.AreEqual (32,orig.Width);
157 Icon ret = new Icon (orig, 48, 48);
158 Assert.AreEqual (48, ret.Height);
159 Assert.AreEqual (48, ret.Width);
163 public void Constructor_Icon_DoesntReturn256Passing0 ()
165 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
167 Icon orig = new Icon (filepath);
168 Assert.AreEqual (32,orig.Height);
169 Assert.AreEqual (32,orig.Width);
171 Icon ret = new Icon (orig, 0, 0);
172 Assert.AreNotEqual (0, ret.Height);
173 Assert.AreNotEqual (0, ret.Width);
177 public void Constructor_Icon_DoesntReturn256Passing1 ()
179 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
181 Icon orig = new Icon (filepath);
182 Assert.AreEqual (32,orig.Height);
183 Assert.AreEqual (32,orig.Width);
185 Icon ret = new Icon (orig, 1, 1);
186 Assert.AreNotEqual (0, ret.Height);
187 Assert.AreNotEqual (0, ret.Width);
191 [ExpectedException (typeof (ArgumentException))]
192 public void Constructor_StreamNull ()
194 new Icon ((Stream) null);
198 [ExpectedException (typeof (ArgumentException))]
199 public void Constructor_StreamNull_Int_Int ()
201 new Icon ((Stream) null, 32, 32);
205 [ExpectedException (typeof (ArgumentNullException))]
206 public void Constructor_StringNull ()
208 new Icon ((string) null);
212 [ExpectedException (typeof (NullReferenceException))]
213 public void Constructor_TypeNull_String ()
215 new Icon ((Type) null, "mono.ico");
219 [ExpectedException (typeof (ArgumentException))]
221 [Ignore ("Check parameters")]
223 public void Constructor_Type_StringNull ()
225 new Icon (typeof (Icon), null);
229 [ExpectedException (typeof (ArgumentException))]
231 [Ignore ("Constructor_StreamNull_Size Not Implemented")]
233 public void Constructor_StreamNull_Size ()
236 new Icon ((Stream) null, new Size (32, 32));
241 [ExpectedException (typeof (ArgumentNullException))]
243 [Ignore ("Constructor_StringNull_Size Not Implemented")]
245 public void Constructor_StringNull_Size ()
248 new Icon ((string) null, new Size (32, 32));
253 [ExpectedException (typeof (ArgumentNullException))]
255 [Ignore ("Constructor_StringNull_Int_Int Not Implemented")]
257 public void Constructor_StringNull_Int_Int ()
260 new Icon ((string) null, 32, 32);
266 public void TestProperties ()
268 Assert.AreEqual (32, icon.Height, "P#1");
269 Assert.AreEqual (32, icon.Width, "P#2");
270 Assert.AreEqual (32, icon.Size.Width, "P#3");
271 Assert.AreEqual (32, icon.Size.Height, "P#4");
277 Icon clone = (Icon) icon.Clone ();
278 Assert.AreEqual (32, clone.Height, "Height");
279 Assert.AreEqual (32, clone.Width, "Width");
280 Assert.AreEqual (32, clone.Size.Width, "Size.Width");
281 Assert.AreEqual (32, clone.Size.Height, "Size.Height");
286 public void CloneHandleIcon ()
288 Icon clone = (Icon) Icon.FromHandle (SystemIcons.Hand.Handle).Clone ();
289 Assert.AreEqual (SystemIcons.Hand.Height, clone.Height, "Height");
290 Assert.AreEqual (SystemIcons.Hand.Width, clone.Width, "Width");
291 Assert.AreEqual (SystemIcons.Hand.Size.Width, clone.Size.Width, "Size.Width");
292 Assert.AreEqual (SystemIcons.Hand.Size.Height, clone.Size.Height, "Size.Height");
296 private void XPIcon (int size)
298 // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
299 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
300 using (Icon xp = new Icon (fs, size, size)) {
301 Assert.AreEqual (size, xp.Height, "Height");
302 Assert.AreEqual (size, xp.Width, "Width");
303 Assert.AreEqual (size, xp.Size.Width, "Size.Width");
304 Assert.AreEqual (size, xp.Size.Height, "Size.Height");
306 Bitmap bmp = xp.ToBitmap ();
307 Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
308 Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
309 Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
310 Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
316 public void Icon32bits_XP16 ()
322 public void Icon32bits_XP32 ()
328 public void Icon32bits_XP48 ()
335 [Ignore ("SelectFromUnusualSize_Small16 Not Working")]
337 public void SelectFromUnusualSize_Small16 ()
339 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
340 using (Icon xp = new Icon (fs, 16, 16)) {
341 Assert.AreEqual (16, xp.Height, "Height");
342 Assert.AreEqual (10, xp.Width, "Width");
343 Assert.AreEqual (10, xp.Size.Width, "Size.Width");
344 Assert.AreEqual (16, xp.Size.Height, "Size.Height");
350 public void SelectFromUnusualSize_Normal32 ()
352 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
353 using (Icon xp = new Icon (fs, 32, 32)) {
354 Assert.AreEqual (22, xp.Height, "Height");
355 Assert.AreEqual (11, xp.Width, "Width");
356 Assert.AreEqual (11, xp.Size.Width, "Size.Width");
357 Assert.AreEqual (22, xp.Size.Height, "Size.Height");
362 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
364 using (MemoryStream ms = new MemoryStream ()) {
368 using (Icon loaded = new Icon (ms)) {
369 Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
370 Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
372 using (Bitmap expected = icon.ToBitmap ()) {
373 using (Bitmap actual = loaded.ToBitmap ()) {
374 Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
375 Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
377 for (int y = 0; y < expected.Height; y++) {
378 for (int x = 0; x < expected.Width; x++) {
379 Color e = expected.GetPixel (x, y);
380 Color a = actual.GetPixel (x, y);
382 Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
383 Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
384 Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
385 Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
397 SaveAndCompare ("16", icon16, true);
398 SaveAndCompare ("32", icon32, true);
399 SaveAndCompare ("48", icon48, true);
400 SaveAndCompare ("64", icon64, true);
401 SaveAndCompare ("96", icon96, true);
404 [Test] // bug #410608
405 public void Save_256 ()
407 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
409 using (Icon icon = new Icon (filepath)) {
411 SaveAndCompare ("256", icon, true);
415 var orig = new MemoryStream (File.ReadAllBytes (filepath));
416 var saved = new MemoryStream ();
417 using (Icon icon = new Icon (filepath))
419 FileAssert.AreEqual (orig, saved, "binary comparison");
423 [ExpectedException (typeof (NullReferenceException))]
425 [Ignore ("Throws NullReference, do we need to follow?")]
427 public void Save_Null ()
434 [Ignore ("Icon16ToBitmap Not Working")]
436 public void Icon16ToBitmap ()
438 using (Bitmap b = icon16.ToBitmap ()) {
439 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
440 // unlike the GDI+ icon decoder the palette isn't kept
441 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
442 Assert.AreEqual (icon16.Height, b.Height, "Height");
443 Assert.AreEqual (icon16.Width, b.Width, "Width");
444 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
445 Assert.AreEqual (2, b.Flags, "Flags");
451 [Ignore ("Icon32ToBitmap Not Working")]
453 public void Icon32ToBitmap ()
455 using (Bitmap b = icon32.ToBitmap ()) {
456 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
457 // unlike the GDI+ icon decoder the palette isn't kept
458 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
459 Assert.AreEqual (icon32.Height, b.Height, "Height");
460 Assert.AreEqual (icon32.Width, b.Width, "Width");
461 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
462 Assert.AreEqual (2, b.Flags, "Flags");
468 [Ignore ("Icon48ToBitmap Not Working")]
470 public void Icon48ToBitmap ()
472 using (Bitmap b = icon48.ToBitmap ()) {
473 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
474 // unlike the GDI+ icon decoder the palette isn't kept
475 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
476 Assert.AreEqual (icon48.Height, b.Height, "Height");
477 Assert.AreEqual (icon48.Width, b.Width, "Width");
478 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
479 Assert.AreEqual (2, b.Flags, "Flags");
485 [Ignore ("Icon64ToBitmap Not Working")]
487 public void Icon64ToBitmap ()
489 using (Bitmap b = icon64.ToBitmap ()) {
490 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
491 // unlike the GDI+ icon decoder the palette isn't kept
492 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
493 Assert.AreEqual (icon64.Height, b.Height, "Height");
494 Assert.AreEqual (icon64.Width, b.Width, "Width");
495 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
496 Assert.AreEqual (2, b.Flags, "Flags");
502 [Ignore ("Icon96ToBitmap Not Working")]
504 public void Icon96ToBitmap ()
506 using (Bitmap b = icon96.ToBitmap ()) {
507 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
508 // unlike the GDI+ icon decoder the palette isn't kept
509 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
510 Assert.AreEqual (icon96.Height, b.Height, "Height");
511 Assert.AreEqual (icon96.Width, b.Width, "Width");
512 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
513 Assert.AreEqual (2, b.Flags, "Flags");
517 [Test] // bug #415581
519 [Ignore ("Icon256ToBitmap Not Working")]
521 public void Icon256ToBitmap ()
523 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
524 Icon icon = new Icon (fs, 48, 48);
525 using (Bitmap b = icon.ToBitmap ()) {
526 Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
527 Assert.AreEqual (48, b.Height, "#A2");
528 Assert.AreEqual (48, b.Width, "#A3");
529 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
530 Assert.AreEqual (2, b.Flags, "#A5");
535 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
536 Icon icon = new Icon (fs, 256, 256);
537 using (Bitmap b = icon.ToBitmap ()) {
538 Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
539 Assert.AreEqual (48, b.Height, "#B2");
540 Assert.AreEqual (48, b.Width, "#B3");
541 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
542 Assert.AreEqual (2, b.Flags, "#B5");
548 public void Icon256ToBitmap_Request0 ()
550 // 415581.ico has 2 images, the 256 and 48
551 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
552 Icon icon = new Icon (fs, 0, 0);
553 using (Bitmap b = icon.ToBitmap ()) {
554 Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
555 Assert.AreEqual (48, b.Height, "#B2");
556 Assert.AreEqual (48, b.Width, "#B3");
557 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
558 Assert.AreEqual (2, b.Flags, "#B5");
563 [Test, ExpectedException ()] //ToDo: System.ComponentModel.Win32Exception
564 public void Only256InFile ()
566 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/only256.ico"))) {
567 Icon icon = new Icon (fs, 0, 0);
574 [ExpectedException (typeof (ArgumentException))]
576 [Ignore ("ExtractAssociatedIcon is not implemented")]
578 public void ExtractAssociatedIcon_Null ()
581 Icon.ExtractAssociatedIcon (null);
586 [ExpectedException (typeof (ArgumentException))]
588 [Ignore ("ExtractAssociatedIcon is not implemented")]
590 public void ExtractAssociatedIcon_Empty ()
593 Icon.ExtractAssociatedIcon (String.Empty);
598 [ExpectedException (typeof (FileNotFoundException))]
600 [Ignore ("ExtractAssociatedIcon is not implemented")]
602 public void ExtractAssociatedIcon_DoesNotExists ()
605 Icon.ExtractAssociatedIcon ("does-not-exists.png");
610 private static bool RunningOnUnix {
612 int p = (int) Environment.OSVersion.Platform;
614 return (p == 4) || (p == 6) || (p == 128);
621 [Ignore ("Unsafe code is not supported")]
623 public class IconFullTrustTest {
627 public void ExtractAssociatedIcon ()
629 string filename_dll = Assembly.GetExecutingAssembly ().Location;
630 Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
635 public void HandleRoundtrip ()
638 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
639 Assert.AreEqual (16, icon.Height, "Original.Height");
640 Assert.AreEqual (16, icon.Width, "Original.Width");
641 handle = icon.Handle;
642 using (Icon icon2 = Icon.FromHandle (handle)) {
643 Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
644 Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
645 Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
646 IconTest.SaveAndCompare ("Handle", icon2, false);
649 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
650 // commented / using freed memory is risky ;-)
651 /*using (Icon icon3 = Icon.FromHandle (handle)) {
652 Assert.AreEqual (0, icon3.Height, "Survivor.Height");
653 Assert.AreEqual (0, icon3.Width, "Survivor.Width");
654 Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
659 public void CreateMultipleIconFromSameHandle ()
662 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
663 Assert.AreEqual (16, icon.Height, "Original.Height");
664 Assert.AreEqual (16, icon.Width, "Original.Width");
665 handle = icon.Handle;
666 using (Icon icon2 = Icon.FromHandle (handle)) {
667 Assert.AreEqual (16, icon2.Height, "2.Height");
668 Assert.AreEqual (16, icon2.Width, "2.Width");
669 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
670 IconTest.SaveAndCompare ("Handle2", icon2, false);
672 using (Icon icon3 = Icon.FromHandle (handle)) {
673 Assert.AreEqual (16, icon3.Height, "3.Height");
674 Assert.AreEqual (16, icon3.Width, "3.Width");
675 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
676 IconTest.SaveAndCompare ("Handle3", icon3, false);
679 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
680 // commented / using freed memory is risky ;-)
684 public void HiconRoundtrip ()
687 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
688 Assert.AreEqual (16, icon.Height, "Original.Height");
689 Assert.AreEqual (16, icon.Width, "Original.Width");
690 handle = icon.ToBitmap ().GetHicon ();
693 using (Icon icon2 = Icon.FromHandle (handle)) {
694 Assert.AreEqual (16, icon2.Height, "Survivor.Height");
695 Assert.AreEqual (16, icon2.Width, "Survivor.Width");
696 Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
697 IconTest.SaveAndCompare ("HICON", icon2, false);
702 public void CreateMultipleIconFromSameHICON ()
705 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
706 Assert.AreEqual (16, icon.Height, "Original.Height");
707 Assert.AreEqual (16, icon.Width, "Original.Width");
708 handle = icon.ToBitmap ().GetHicon ();
711 using (Icon icon2 = Icon.FromHandle (handle)) {
712 Assert.AreEqual (16, icon2.Height, "2.Height");
713 Assert.AreEqual (16, icon2.Width, "2.Width");
714 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
715 IconTest.SaveAndCompare ("HICON2", icon2, false);
717 using (Icon icon3 = Icon.FromHandle (handle)) {
718 Assert.AreEqual (16, icon3.Height, "3.Height");
719 Assert.AreEqual (16, icon3.Width, "3.Width");
720 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
721 IconTest.SaveAndCompare ("HICON", icon3, false);