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.
32 using System.ComponentModel;
34 using System.Drawing.Imaging;
36 using System.Reflection;
37 using System.Security.Permissions;
38 using NUnit.Framework;
40 namespace MonoTests.System.Drawing {
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 public void Constructor_IconNull_Int_Int ()
98 Assert.Throws<ArgumentException> (() => new Icon ((Icon)null, 32, 32));
102 public void Constructor_Icon_IntNegative_Int ()
104 Icon neg = new Icon (icon, -32, 32);
105 Assert.AreEqual (32, neg.Height, "Height");
106 Assert.AreEqual (32, neg.Width, "Width");
110 public void Constructor_IconNull_Size ()
112 Assert.Throws<ArgumentException> (() => new Icon ((Icon) null, new Size (32, 32)));
116 public void Constructor_Icon_Size_Negative ()
118 Icon neg = new Icon (icon, new Size (-32, -32));
119 Assert.AreEqual (16, neg.Height, "Height");
120 Assert.AreEqual (16, neg.Width, "Width");
124 public void Constructor_Icon_Int_Int_NonSquare ()
126 Icon non_square = new Icon (icon, 32, 16);
127 Assert.AreEqual (32, non_square.Height, "Height");
128 Assert.AreEqual (32, non_square.Width, "Width");
132 public void Constructor_Icon_GetNormalSizeFromIconWith256 ()
134 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
136 Icon orig = new Icon (filepath);
137 Assert.AreEqual (32,orig.Height);
138 Assert.AreEqual (32,orig.Width);
140 Icon ret = new Icon (orig, 48, 48);
141 Assert.AreEqual (48, ret.Height);
142 Assert.AreEqual (48, ret.Width);
146 public void Constructor_Icon_DoesntReturn256Passing0 ()
148 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
150 Icon orig = new Icon (filepath);
151 Assert.AreEqual (32,orig.Height);
152 Assert.AreEqual (32,orig.Width);
154 Icon ret = new Icon (orig, 0, 0);
155 Assert.AreNotEqual (0, ret.Height);
156 Assert.AreNotEqual (0, ret.Width);
160 public void Constructor_Icon_DoesntReturn256Passing1 ()
162 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
164 Icon orig = new Icon (filepath);
165 Assert.AreEqual (32,orig.Height);
166 Assert.AreEqual (32,orig.Width);
168 Icon ret = new Icon (orig, 1, 1);
169 Assert.AreNotEqual (0, ret.Height);
170 Assert.AreNotEqual (0, ret.Width);
174 public void Constructor_StreamNull ()
176 Assert.Throws<ArgumentException> (() => new Icon ((Stream) null));
180 public void Constructor_StreamNull_Int_Int ()
182 Assert.Throws<ArgumentException> (() => new Icon ((Stream) null, 32, 32));
186 public void Constructor_StringNull ()
188 Assert.Throws<ArgumentNullException> (() => new Icon ((string) null));
192 public void Constructor_TypeNull_String ()
194 Assert.Throws<NullReferenceException> (() => new Icon ((Type) null, "mono.ico"));
198 public void Constructor_Type_StringNull ()
200 Assert.Throws<ArgumentException> (() => new Icon (typeof (Icon), null));
203 public void Constructor_StreamNull_Size ()
205 Assert.Throws<ArgumentException> (() => new Icon ((Stream) null, new Size (32, 32)));
209 public void Constructor_StringNull_Size ()
211 Assert.Throws<ArgumentNullException> (() => new Icon ((string) null, new Size (32, 32)));
215 public void Constructor_StringNull_Int_Int ()
217 Assert.Throws<ArgumentNullException> (() => new Icon ((string) null, 32, 32));
221 public void TestProperties ()
223 Assert.AreEqual (32, icon.Height, "P#1");
224 Assert.AreEqual (32, icon.Width, "P#2");
225 Assert.AreEqual (32, icon.Size.Width, "P#3");
226 Assert.AreEqual (32, icon.Size.Height, "P#4");
232 Icon clone = (Icon) icon.Clone ();
233 Assert.AreEqual (32, clone.Height, "Height");
234 Assert.AreEqual (32, clone.Width, "Width");
235 Assert.AreEqual (32, clone.Size.Width, "Size.Width");
236 Assert.AreEqual (32, clone.Size.Height, "Size.Height");
240 public void CloneHandleIcon ()
242 Icon clone = (Icon) Icon.FromHandle (SystemIcons.Hand.Handle).Clone ();
243 Assert.AreEqual (SystemIcons.Hand.Height, clone.Height, "Height");
244 Assert.AreEqual (SystemIcons.Hand.Width, clone.Width, "Width");
245 Assert.AreEqual (SystemIcons.Hand.Size.Width, clone.Size.Width, "Size.Width");
246 Assert.AreEqual (SystemIcons.Hand.Size.Height, clone.Size.Height, "Size.Height");
249 private void XPIcon (int size)
251 // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
252 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
253 using (Icon xp = new Icon (fs, size, size)) {
254 Assert.AreEqual (size, xp.Height, "Height");
255 Assert.AreEqual (size, xp.Width, "Width");
256 Assert.AreEqual (size, xp.Size.Width, "Size.Width");
257 Assert.AreEqual (size, xp.Size.Height, "Size.Height");
259 Bitmap bmp = xp.ToBitmap ();
260 Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
261 Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
262 Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
263 Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
269 public void Icon32bits_XP16 ()
275 public void Icon32bits_XP32 ()
281 public void Icon32bits_XP48 ()
287 public void SelectFromUnusualSize_Small16 ()
289 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
290 using (Icon xp = new Icon (fs, 16, 16)) {
291 Assert.AreEqual (16, xp.Height, "Height");
292 Assert.AreEqual (10, xp.Width, "Width");
293 Assert.AreEqual (10, xp.Size.Width, "Size.Width");
294 Assert.AreEqual (16, xp.Size.Height, "Size.Height");
300 public void SelectFromUnusualSize_Normal32 ()
302 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
303 using (Icon xp = new Icon (fs, 32, 32)) {
304 Assert.AreEqual (22, xp.Height, "Height");
305 Assert.AreEqual (11, xp.Width, "Width");
306 Assert.AreEqual (11, xp.Size.Width, "Size.Width");
307 Assert.AreEqual (22, xp.Size.Height, "Size.Height");
312 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
314 using (MemoryStream ms = new MemoryStream ()) {
318 using (Icon loaded = new Icon (ms)) {
319 Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
320 Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
322 using (Bitmap expected = icon.ToBitmap ()) {
323 using (Bitmap actual = loaded.ToBitmap ()) {
324 Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
325 Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
327 for (int y = 0; y < expected.Height; y++) {
328 for (int x = 0; x < expected.Width; x++) {
329 Color e = expected.GetPixel (x, y);
330 Color a = actual.GetPixel (x, y);
332 Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
333 Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
334 Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
335 Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
347 SaveAndCompare ("16", icon16, true);
348 SaveAndCompare ("32", icon32, true);
349 SaveAndCompare ("48", icon48, true);
350 SaveAndCompare ("64", icon64, true);
351 SaveAndCompare ("96", icon96, true);
354 [Test] // bug #410608
355 public void Save_256 ()
357 string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
359 using (Icon icon = new Icon (filepath)) {
361 SaveAndCompare ("256", icon, true);
365 var orig = new MemoryStream (File.ReadAllBytes (filepath));
366 var saved = new MemoryStream ();
367 using (Icon icon = new Icon (filepath))
369 FileAssert.AreEqual (orig, saved, "binary comparison");
373 public void Save_Null ()
375 Assert.Throws<NullReferenceException> (() => icon.Save (null));
379 public void Icon16ToBitmap ()
381 using (Bitmap b = icon16.ToBitmap ()) {
382 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
383 // unlike the GDI+ icon decoder the palette isn't kept
384 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
385 Assert.AreEqual (icon16.Height, b.Height, "Height");
386 Assert.AreEqual (icon16.Width, b.Width, "Width");
387 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
388 Assert.AreEqual (2, b.Flags, "Flags");
393 public void Icon32ToBitmap ()
395 using (Bitmap b = icon32.ToBitmap ()) {
396 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
397 // unlike the GDI+ icon decoder the palette isn't kept
398 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
399 Assert.AreEqual (icon32.Height, b.Height, "Height");
400 Assert.AreEqual (icon32.Width, b.Width, "Width");
401 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
402 Assert.AreEqual (2, b.Flags, "Flags");
407 public void Icon48ToBitmap ()
409 using (Bitmap b = icon48.ToBitmap ()) {
410 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
411 // unlike the GDI+ icon decoder the palette isn't kept
412 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
413 Assert.AreEqual (icon48.Height, b.Height, "Height");
414 Assert.AreEqual (icon48.Width, b.Width, "Width");
415 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
416 Assert.AreEqual (2, b.Flags, "Flags");
421 public void Icon64ToBitmap ()
423 using (Bitmap b = icon64.ToBitmap ()) {
424 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
425 // unlike the GDI+ icon decoder the palette isn't kept
426 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
427 Assert.AreEqual (icon64.Height, b.Height, "Height");
428 Assert.AreEqual (icon64.Width, b.Width, "Width");
429 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
430 Assert.AreEqual (2, b.Flags, "Flags");
435 public void Icon96ToBitmap ()
437 using (Bitmap b = icon96.ToBitmap ()) {
438 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
439 // unlike the GDI+ icon decoder the palette isn't kept
440 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
441 Assert.AreEqual (icon96.Height, b.Height, "Height");
442 Assert.AreEqual (icon96.Width, b.Width, "Width");
443 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
444 Assert.AreEqual (2, b.Flags, "Flags");
448 [Test] // bug #415581
449 public void Icon256ToBitmap ()
451 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
452 Icon icon = new Icon (fs, 48, 48);
453 using (Bitmap b = icon.ToBitmap ()) {
454 Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
455 Assert.AreEqual (48, b.Height, "#A2");
456 Assert.AreEqual (48, b.Width, "#A3");
457 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
458 Assert.AreEqual (2, b.Flags, "#A5");
463 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
464 Icon icon = new Icon (fs, 256, 256);
465 using (Bitmap b = icon.ToBitmap ()) {
466 Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
467 Assert.AreEqual (48, b.Height, "#B2");
468 Assert.AreEqual (48, b.Width, "#B3");
469 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
470 Assert.AreEqual (2, b.Flags, "#B5");
476 public void Icon256ToBitmap_Request0 ()
478 // 415581.ico has 2 images, the 256 and 48
479 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
480 Icon icon = new Icon (fs, 0, 0);
481 using (Bitmap b = icon.ToBitmap ()) {
482 Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
483 Assert.AreEqual (48, b.Height, "#B2");
484 Assert.AreEqual (48, b.Width, "#B3");
485 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
486 Assert.AreEqual (2, b.Flags, "#B5");
492 public void Only256InFile ()
494 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/only256.ico"))) {
495 Assert.Throws<Win32Exception> (() => new Icon (fs, 0, 0));
501 public void ExtractAssociatedIcon_Null ()
503 Assert.Throws<ArgumentException> (() => Icon.ExtractAssociatedIcon (null));
507 public void ExtractAssociatedIcon_Empty ()
509 Assert.Throws<ArgumentException> (() => Icon.ExtractAssociatedIcon (String.Empty));
513 public void ExtractAssociatedIcon_DoesNotExists ()
515 Assert.Throws<FileNotFoundException> (() => Icon.ExtractAssociatedIcon ("does-not-exists.png"));
518 private static bool RunningOnUnix {
520 int p = (int) Environment.OSVersion.Platform;
522 return (p == 4) || (p == 6) || (p == 128);
528 public class IconFullTrustTest {
530 public void ExtractAssociatedIcon ()
532 string filename_dll = Assembly.GetExecutingAssembly ().Location;
533 Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
537 public void HandleRoundtrip ()
540 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
541 Assert.AreEqual (16, icon.Height, "Original.Height");
542 Assert.AreEqual (16, icon.Width, "Original.Width");
543 handle = icon.Handle;
544 using (Icon icon2 = Icon.FromHandle (handle)) {
545 Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
546 Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
547 Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
548 IconTest.SaveAndCompare ("Handle", icon2, false);
551 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
552 // commented / using freed memory is risky ;-)
553 /*using (Icon icon3 = Icon.FromHandle (handle)) {
554 Assert.AreEqual (0, icon3.Height, "Survivor.Height");
555 Assert.AreEqual (0, icon3.Width, "Survivor.Width");
556 Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
561 public void CreateMultipleIconFromSameHandle ()
564 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
565 Assert.AreEqual (16, icon.Height, "Original.Height");
566 Assert.AreEqual (16, icon.Width, "Original.Width");
567 handle = icon.Handle;
568 using (Icon icon2 = Icon.FromHandle (handle)) {
569 Assert.AreEqual (16, icon2.Height, "2.Height");
570 Assert.AreEqual (16, icon2.Width, "2.Width");
571 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
572 IconTest.SaveAndCompare ("Handle2", icon2, false);
574 using (Icon icon3 = Icon.FromHandle (handle)) {
575 Assert.AreEqual (16, icon3.Height, "3.Height");
576 Assert.AreEqual (16, icon3.Width, "3.Width");
577 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
578 IconTest.SaveAndCompare ("Handle3", icon3, false);
581 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
582 // commented / using freed memory is risky ;-)
586 public void HiconRoundtrip ()
589 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
590 Assert.AreEqual (16, icon.Height, "Original.Height");
591 Assert.AreEqual (16, icon.Width, "Original.Width");
592 handle = icon.ToBitmap ().GetHicon ();
595 using (Icon icon2 = Icon.FromHandle (handle)) {
596 Assert.AreEqual (16, icon2.Height, "Survivor.Height");
597 Assert.AreEqual (16, icon2.Width, "Survivor.Width");
598 Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
599 IconTest.SaveAndCompare ("HICON", icon2, false);
604 public void CreateMultipleIconFromSameHICON ()
607 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
608 Assert.AreEqual (16, icon.Height, "Original.Height");
609 Assert.AreEqual (16, icon.Width, "Original.Width");
610 handle = icon.ToBitmap ().GetHicon ();
613 using (Icon icon2 = Icon.FromHandle (handle)) {
614 Assert.AreEqual (16, icon2.Height, "2.Height");
615 Assert.AreEqual (16, icon2.Width, "2.Width");
616 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
617 IconTest.SaveAndCompare ("HICON2", icon2, false);
619 using (Icon icon3 = Icon.FromHandle (handle)) {
620 Assert.AreEqual (16, icon3.Height, "3.Height");
621 Assert.AreEqual (16, icon3.Width, "3.Width");
622 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
623 IconTest.SaveAndCompare ("HICON", icon3, false);