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))]
97 public void Constructor_IconNull_Int_Int ()
99 new Icon ((Icon)null, 32, 32);
103 public void Constructor_Icon_IntNegative_Int ()
105 Icon neg = new Icon (icon, -32, 32);
106 Assert.AreEqual (32, neg.Height, "Height");
107 Assert.AreEqual (32, neg.Width, "Width");
111 [ExpectedException (typeof (ArgumentException))]
112 public void Constructor_IconNull_Size ()
114 new Icon ((Icon) null, new Size (32, 32));
118 public void Constructor_Icon_Size_Negative ()
120 Icon neg = new Icon (icon, new Size (-32, -32));
121 Assert.AreEqual (16, neg.Height, "Height");
122 Assert.AreEqual (16, neg.Width, "Width");
126 public void Constructor_Icon_Int_Int_NonSquare ()
128 Icon non_square = new Icon (icon, 32, 16);
129 Assert.AreEqual (32, non_square.Height, "Height");
130 Assert.AreEqual (32, non_square.Width, "Width");
134 public void Constructor_Icon_GetNormalSizeFromIconWith256 ()
136 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
138 Icon orig = new Icon (filepath);
139 Assert.AreEqual (32,orig.Height);
140 Assert.AreEqual (32,orig.Width);
142 Icon ret = new Icon (orig, 48, 48);
143 Assert.AreEqual (48, ret.Height);
144 Assert.AreEqual (48, ret.Width);
148 public void Constructor_Icon_DoesntReturn256Passing0 ()
150 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
152 Icon orig = new Icon (filepath);
153 Assert.AreEqual (32,orig.Height);
154 Assert.AreEqual (32,orig.Width);
156 Icon ret = new Icon (orig, 0, 0);
157 Assert.AreNotEqual (0, ret.Height);
158 Assert.AreNotEqual (0, ret.Width);
162 public void Constructor_Icon_DoesntReturn256Passing1 ()
164 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
166 Icon orig = new Icon (filepath);
167 Assert.AreEqual (32,orig.Height);
168 Assert.AreEqual (32,orig.Width);
170 Icon ret = new Icon (orig, 1, 1);
171 Assert.AreNotEqual (0, ret.Height);
172 Assert.AreNotEqual (0, ret.Width);
176 [ExpectedException (typeof (ArgumentException))]
177 public void Constructor_StreamNull ()
179 new Icon ((Stream) null);
183 [ExpectedException (typeof (ArgumentException))]
184 public void Constructor_StreamNull_Int_Int ()
186 new Icon ((Stream) null, 32, 32);
190 [ExpectedException (typeof (ArgumentNullException))]
191 public void Constructor_StringNull ()
193 new Icon ((string) null);
197 [ExpectedException (typeof (NullReferenceException))]
198 public void Constructor_TypeNull_String ()
200 new Icon ((Type) null, "mono.ico");
204 [ExpectedException (typeof (ArgumentException))]
205 public void Constructor_Type_StringNull ()
207 new Icon (typeof (Icon), null);
210 [ExpectedException (typeof (ArgumentException))]
211 public void Constructor_StreamNull_Size ()
213 new Icon ((Stream) null, new Size (32, 32));
217 [ExpectedException (typeof (ArgumentNullException))]
218 public void Constructor_StringNull_Size ()
220 new Icon ((string) null, new Size (32, 32));
224 [ExpectedException (typeof (ArgumentNullException))]
225 public void Constructor_StringNull_Int_Int ()
227 new Icon ((string) null, 32, 32);
231 public void TestProperties ()
233 Assert.AreEqual (32, icon.Height, "P#1");
234 Assert.AreEqual (32, icon.Width, "P#2");
235 Assert.AreEqual (32, icon.Size.Width, "P#3");
236 Assert.AreEqual (32, icon.Size.Height, "P#4");
242 Icon clone = (Icon) icon.Clone ();
243 Assert.AreEqual (32, clone.Height, "Height");
244 Assert.AreEqual (32, clone.Width, "Width");
245 Assert.AreEqual (32, clone.Size.Width, "Size.Width");
246 Assert.AreEqual (32, clone.Size.Height, "Size.Height");
250 public void CloneHandleIcon ()
252 Icon clone = (Icon) Icon.FromHandle (SystemIcons.Hand.Handle).Clone ();
253 Assert.AreEqual (SystemIcons.Hand.Height, clone.Height, "Height");
254 Assert.AreEqual (SystemIcons.Hand.Width, clone.Width, "Width");
255 Assert.AreEqual (SystemIcons.Hand.Size.Width, clone.Size.Width, "Size.Width");
256 Assert.AreEqual (SystemIcons.Hand.Size.Height, clone.Size.Height, "Size.Height");
259 private void XPIcon (int size)
261 // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
262 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
263 using (Icon xp = new Icon (fs, size, size)) {
264 Assert.AreEqual (size, xp.Height, "Height");
265 Assert.AreEqual (size, xp.Width, "Width");
266 Assert.AreEqual (size, xp.Size.Width, "Size.Width");
267 Assert.AreEqual (size, xp.Size.Height, "Size.Height");
269 Bitmap bmp = xp.ToBitmap ();
270 Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
271 Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
272 Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
273 Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
279 public void Icon32bits_XP16 ()
285 public void Icon32bits_XP32 ()
291 public void Icon32bits_XP48 ()
297 public void SelectFromUnusualSize_Small16 ()
299 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
300 using (Icon xp = new Icon (fs, 16, 16)) {
301 Assert.AreEqual (16, xp.Height, "Height");
302 Assert.AreEqual (10, xp.Width, "Width");
303 Assert.AreEqual (10, xp.Size.Width, "Size.Width");
304 Assert.AreEqual (16, xp.Size.Height, "Size.Height");
310 public void SelectFromUnusualSize_Normal32 ()
312 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
313 using (Icon xp = new Icon (fs, 32, 32)) {
314 Assert.AreEqual (22, xp.Height, "Height");
315 Assert.AreEqual (11, xp.Width, "Width");
316 Assert.AreEqual (11, xp.Size.Width, "Size.Width");
317 Assert.AreEqual (22, xp.Size.Height, "Size.Height");
322 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
324 using (MemoryStream ms = new MemoryStream ()) {
328 using (Icon loaded = new Icon (ms)) {
329 Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
330 Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
332 using (Bitmap expected = icon.ToBitmap ()) {
333 using (Bitmap actual = loaded.ToBitmap ()) {
334 Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
335 Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
337 for (int y = 0; y < expected.Height; y++) {
338 for (int x = 0; x < expected.Width; x++) {
339 Color e = expected.GetPixel (x, y);
340 Color a = actual.GetPixel (x, y);
342 Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
343 Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
344 Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
345 Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
357 SaveAndCompare ("16", icon16, true);
358 SaveAndCompare ("32", icon32, true);
359 SaveAndCompare ("48", icon48, true);
360 SaveAndCompare ("64", icon64, true);
361 SaveAndCompare ("96", icon96, true);
364 [Test] // bug #410608
365 public void Save_256 ()
367 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
369 using (Icon icon = new Icon (filepath)) {
371 SaveAndCompare ("256", icon, true);
375 var orig = new MemoryStream (File.ReadAllBytes (filepath));
376 var saved = new MemoryStream ();
377 using (Icon icon = new Icon (filepath))
379 FileAssert.AreEqual (orig, saved, "binary comparison");
383 [ExpectedException (typeof (NullReferenceException))]
384 public void Save_Null ()
390 public void Icon16ToBitmap ()
392 using (Bitmap b = icon16.ToBitmap ()) {
393 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
394 // unlike the GDI+ icon decoder the palette isn't kept
395 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
396 Assert.AreEqual (icon16.Height, b.Height, "Height");
397 Assert.AreEqual (icon16.Width, b.Width, "Width");
398 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
399 Assert.AreEqual (2, b.Flags, "Flags");
404 public void Icon32ToBitmap ()
406 using (Bitmap b = icon32.ToBitmap ()) {
407 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
408 // unlike the GDI+ icon decoder the palette isn't kept
409 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
410 Assert.AreEqual (icon32.Height, b.Height, "Height");
411 Assert.AreEqual (icon32.Width, b.Width, "Width");
412 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
413 Assert.AreEqual (2, b.Flags, "Flags");
418 public void Icon48ToBitmap ()
420 using (Bitmap b = icon48.ToBitmap ()) {
421 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
422 // unlike the GDI+ icon decoder the palette isn't kept
423 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
424 Assert.AreEqual (icon48.Height, b.Height, "Height");
425 Assert.AreEqual (icon48.Width, b.Width, "Width");
426 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
427 Assert.AreEqual (2, b.Flags, "Flags");
432 public void Icon64ToBitmap ()
434 using (Bitmap b = icon64.ToBitmap ()) {
435 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
436 // unlike the GDI+ icon decoder the palette isn't kept
437 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
438 Assert.AreEqual (icon64.Height, b.Height, "Height");
439 Assert.AreEqual (icon64.Width, b.Width, "Width");
440 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
441 Assert.AreEqual (2, b.Flags, "Flags");
446 public void Icon96ToBitmap ()
448 using (Bitmap b = icon96.ToBitmap ()) {
449 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
450 // unlike the GDI+ icon decoder the palette isn't kept
451 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
452 Assert.AreEqual (icon96.Height, b.Height, "Height");
453 Assert.AreEqual (icon96.Width, b.Width, "Width");
454 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
455 Assert.AreEqual (2, b.Flags, "Flags");
459 [Test] // bug #415581
460 public void Icon256ToBitmap ()
462 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
463 Icon icon = new Icon (fs, 48, 48);
464 using (Bitmap b = icon.ToBitmap ()) {
465 Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
466 Assert.AreEqual (48, b.Height, "#A2");
467 Assert.AreEqual (48, b.Width, "#A3");
468 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
469 Assert.AreEqual (2, b.Flags, "#A5");
474 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
475 Icon icon = new Icon (fs, 256, 256);
476 using (Bitmap b = icon.ToBitmap ()) {
477 Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
478 Assert.AreEqual (48, b.Height, "#B2");
479 Assert.AreEqual (48, b.Width, "#B3");
480 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
481 Assert.AreEqual (2, b.Flags, "#B5");
487 public void Icon256ToBitmap_Request0 ()
489 // 415581.ico has 2 images, the 256 and 48
490 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
491 Icon icon = new Icon (fs, 0, 0);
492 using (Bitmap b = icon.ToBitmap ()) {
493 Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
494 Assert.AreEqual (48, b.Height, "#B2");
495 Assert.AreEqual (48, b.Width, "#B3");
496 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
497 Assert.AreEqual (2, b.Flags, "#B5");
502 [Test, ExpectedException ()] //ToDo: System.ComponentModel.Win32Exception
503 public void Only256InFile ()
505 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/only256.ico"))) {
506 Icon icon = new Icon (fs, 0, 0);
512 [ExpectedException (typeof (ArgumentException))]
513 public void ExtractAssociatedIcon_Null ()
515 Icon.ExtractAssociatedIcon (null);
519 [ExpectedException (typeof (ArgumentException))]
520 public void ExtractAssociatedIcon_Empty ()
522 Icon.ExtractAssociatedIcon (String.Empty);
526 [ExpectedException (typeof (FileNotFoundException))]
527 public void ExtractAssociatedIcon_DoesNotExists ()
529 Icon.ExtractAssociatedIcon ("does-not-exists.png");
532 private static bool RunningOnUnix {
534 int p = (int) Environment.OSVersion.Platform;
536 return (p == 4) || (p == 6) || (p == 128);
542 public class IconFullTrustTest {
544 public void ExtractAssociatedIcon ()
546 string filename_dll = Assembly.GetExecutingAssembly ().Location;
547 Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
551 public void HandleRoundtrip ()
554 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
555 Assert.AreEqual (16, icon.Height, "Original.Height");
556 Assert.AreEqual (16, icon.Width, "Original.Width");
557 handle = icon.Handle;
558 using (Icon icon2 = Icon.FromHandle (handle)) {
559 Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
560 Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
561 Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
562 IconTest.SaveAndCompare ("Handle", icon2, false);
565 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
566 // commented / using freed memory is risky ;-)
567 /*using (Icon icon3 = Icon.FromHandle (handle)) {
568 Assert.AreEqual (0, icon3.Height, "Survivor.Height");
569 Assert.AreEqual (0, icon3.Width, "Survivor.Width");
570 Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
575 public void CreateMultipleIconFromSameHandle ()
578 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
579 Assert.AreEqual (16, icon.Height, "Original.Height");
580 Assert.AreEqual (16, icon.Width, "Original.Width");
581 handle = icon.Handle;
582 using (Icon icon2 = Icon.FromHandle (handle)) {
583 Assert.AreEqual (16, icon2.Height, "2.Height");
584 Assert.AreEqual (16, icon2.Width, "2.Width");
585 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
586 IconTest.SaveAndCompare ("Handle2", icon2, false);
588 using (Icon icon3 = Icon.FromHandle (handle)) {
589 Assert.AreEqual (16, icon3.Height, "3.Height");
590 Assert.AreEqual (16, icon3.Width, "3.Width");
591 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
592 IconTest.SaveAndCompare ("Handle3", icon3, false);
595 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
596 // commented / using freed memory is risky ;-)
600 public void HiconRoundtrip ()
603 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
604 Assert.AreEqual (16, icon.Height, "Original.Height");
605 Assert.AreEqual (16, icon.Width, "Original.Width");
606 handle = icon.ToBitmap ().GetHicon ();
609 using (Icon icon2 = Icon.FromHandle (handle)) {
610 Assert.AreEqual (16, icon2.Height, "Survivor.Height");
611 Assert.AreEqual (16, icon2.Width, "Survivor.Width");
612 Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
613 IconTest.SaveAndCompare ("HICON", icon2, false);
618 public void CreateMultipleIconFromSameHICON ()
621 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
622 Assert.AreEqual (16, icon.Height, "Original.Height");
623 Assert.AreEqual (16, icon.Width, "Original.Width");
624 handle = icon.ToBitmap ().GetHicon ();
627 using (Icon icon2 = Icon.FromHandle (handle)) {
628 Assert.AreEqual (16, icon2.Height, "2.Height");
629 Assert.AreEqual (16, icon2.Width, "2.Width");
630 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
631 IconTest.SaveAndCompare ("HICON2", icon2, false);
633 using (Icon icon3 = Icon.FromHandle (handle)) {
634 Assert.AreEqual (16, icon3.Height, "3.Height");
635 Assert.AreEqual (16, icon3.Width, "3.Width");
636 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
637 IconTest.SaveAndCompare ("HICON", icon3, false);