2 // Icon class testing unit
5 // Sanjay Gupta <gsanjay@novell.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Drawing.Imaging;
34 using System.Reflection;
35 using System.Security.Permissions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Drawing {
41 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42 public class IconTest {
45 Icon icon16, icon32, icon48, icon64, icon96;
48 static string filename_dll;
50 // static ctor are executed outside the Deny
53 filename_dll = Assembly.GetExecutingAssembly ().Location;
59 String path = TestBitmap.getInFile ("bitmaps/smiley.ico");
60 icon = new Icon (path);
61 fs1 = new FileStream (path, FileMode.Open);
63 icon16 = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"));
64 icon32 = new Icon (TestBitmap.getInFile ("bitmaps/32x32x16.ico"));
65 icon48 = new Icon (TestBitmap.getInFile ("bitmaps/48x48x1.ico"));
66 icon64 = new Icon (TestBitmap.getInFile ("bitmaps/64x64x256.ico"));
67 icon96 = new Icon (TestBitmap.getInFile ("bitmaps/96x96x256.ico"));
71 public void TearDown ()
75 if (File.Exists ("newIcon.ico"))
76 File.Delete ("newIcon.ico");
80 public void TestConstructors ()
82 Assert.AreEqual (32, icon.Height, "C#0a");
83 Assert.AreEqual (32, icon.Width, "C#0b");
85 Icon newIcon = new Icon (fs1, 48, 48);
86 Assert.AreEqual (48, newIcon.Height, "C#1a");
87 Assert.AreEqual (48, newIcon.Width, "C#1b");
89 newIcon = new Icon (icon, 16, 16);
90 Assert.AreEqual (16, newIcon.Height, "C#2a");
91 Assert.AreEqual (16, newIcon.Width, "C#2b");
95 [ExpectedException (typeof (ArgumentException))]
97 [Ignore ("Check parameters")]
99 public void Constructor_IconNull_Int_Int ()
101 new Icon ((Icon)null, 32, 32);
106 [Ignore ("Constructor_Icon_IntNegative_Int Not Working")]
108 public void Constructor_Icon_IntNegative_Int ()
110 Icon neg = new Icon (icon, -32, 32);
111 Assert.AreEqual (32, neg.Height, "Height");
112 Assert.AreEqual (32, neg.Width, "Width");
116 [ExpectedException (typeof (ArgumentException))]
118 [Ignore ("Check parameters")]
120 public void Constructor_IconNull_Size ()
122 new Icon ((Icon) null, new Size (32, 32));
127 [Ignore ("Constructor_Icon_Size_Negative Not Working")]
129 public void Constructor_Icon_Size_Negative ()
131 Icon neg = new Icon (icon, new Size (-32, -32));
132 Assert.AreEqual (16, neg.Height, "Height");
133 Assert.AreEqual (16, neg.Width, "Width");
138 [Ignore ("Constructor_Icon_Int_Int_NonSquare Not Working")]
140 public void Constructor_Icon_Int_Int_NonSquare ()
142 Icon non_square = new Icon (icon, 32, 16);
143 Assert.AreEqual (32, non_square.Height, "Height");
144 Assert.AreEqual (32, non_square.Width, "Width");
148 [ExpectedException (typeof (ArgumentException))]
149 public void Constructor_StreamNull ()
151 new Icon ((Stream) null);
155 [ExpectedException (typeof (ArgumentException))]
156 public void Constructor_StreamNull_Int_Int ()
158 new Icon ((Stream) null, 32, 32);
162 [ExpectedException (typeof (ArgumentNullException))]
163 public void Constructor_StringNull ()
165 new Icon ((string) null);
169 [ExpectedException (typeof (NullReferenceException))]
170 public void Constructor_TypeNull_String ()
172 new Icon ((Type) null, "mono.ico");
176 [ExpectedException (typeof (ArgumentException))]
178 [Ignore ("Check parameters")]
180 public void Constructor_Type_StringNull ()
182 new Icon (typeof (Icon), null);
186 [ExpectedException (typeof (ArgumentException))]
188 [Ignore ("Constructor_StreamNull_Size Not Implemented")]
190 public void Constructor_StreamNull_Size ()
193 new Icon ((Stream) null, new Size (32, 32));
198 [ExpectedException (typeof (ArgumentNullException))]
200 [Ignore ("Constructor_StringNull_Size Not Implemented")]
202 public void Constructor_StringNull_Size ()
205 new Icon ((string) null, new Size (32, 32));
210 [ExpectedException (typeof (ArgumentNullException))]
212 [Ignore ("Constructor_StringNull_Int_Int Not Implemented")]
214 public void Constructor_StringNull_Int_Int ()
217 new Icon ((string) null, 32, 32);
223 public void TestProperties ()
225 Assert.AreEqual (32, icon.Height, "P#1");
226 Assert.AreEqual (32, icon.Width, "P#2");
227 Assert.AreEqual (32, icon.Size.Width, "P#3");
228 Assert.AreEqual (32, icon.Size.Height, "P#4");
234 Icon clone = (Icon) icon.Clone ();
235 Assert.AreEqual (32, clone.Height, "Height");
236 Assert.AreEqual (32, clone.Width, "Width");
237 Assert.AreEqual (32, clone.Size.Width, "Size.Width");
238 Assert.AreEqual (32, clone.Size.Height, "Size.Height");
241 private void XPIcon (int size)
243 // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
244 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
245 using (Icon xp = new Icon (fs, size, size)) {
246 Assert.AreEqual (size, xp.Height, "Height");
247 Assert.AreEqual (size, xp.Width, "Width");
248 Assert.AreEqual (size, xp.Size.Width, "Size.Width");
249 Assert.AreEqual (size, xp.Size.Height, "Size.Height");
251 Bitmap bmp = xp.ToBitmap ();
252 Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
253 Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
254 Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
255 Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
261 public void Icon32bits_XP16 ()
267 public void Icon32bits_XP32 ()
273 public void Icon32bits_XP48 ()
280 [Ignore ("SelectFromUnusualSize_Small16 Not Working")]
282 public void SelectFromUnusualSize_Small16 ()
284 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
285 using (Icon xp = new Icon (fs, 16, 16)) {
286 Assert.AreEqual (16, xp.Height, "Height");
287 Assert.AreEqual (10, xp.Width, "Width");
288 Assert.AreEqual (10, xp.Size.Width, "Size.Width");
289 Assert.AreEqual (16, xp.Size.Height, "Size.Height");
295 public void SelectFromUnusualSize_Normal32 ()
297 using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
298 using (Icon xp = new Icon (fs, 32, 32)) {
299 Assert.AreEqual (22, xp.Height, "Height");
300 Assert.AreEqual (11, xp.Width, "Width");
301 Assert.AreEqual (11, xp.Size.Width, "Size.Width");
302 Assert.AreEqual (22, xp.Size.Height, "Size.Height");
307 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
309 using (MemoryStream ms = new MemoryStream ()) {
313 using (Icon loaded = new Icon (ms)) {
314 Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
315 Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
317 using (Bitmap expected = icon.ToBitmap ()) {
318 using (Bitmap actual = loaded.ToBitmap ()) {
319 Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
320 Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
322 for (int y = 0; y < expected.Height; y++) {
323 for (int x = 0; x < expected.Width; x++) {
324 Color e = expected.GetPixel (x, y);
325 Color a = actual.GetPixel (x, y);
327 Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
328 Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
329 Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
330 Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
342 SaveAndCompare ("16", icon16, true);
343 SaveAndCompare ("32", icon32, true);
344 SaveAndCompare ("48", icon48, true);
345 SaveAndCompare ("64", icon64, true);
346 SaveAndCompare ("96", icon96, true);
350 [ExpectedException (typeof (NullReferenceException))]
352 [Ignore ("Throws NullReference, do we need to follow?")]
354 public void Save_Null ()
361 [Ignore ("Icon16ToBitmap Not Working")]
363 public void Icon16ToBitmap ()
365 using (Bitmap b = icon16.ToBitmap ()) {
366 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
367 // unlike the GDI+ icon decoder the palette isn't kept
368 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
369 Assert.AreEqual (icon16.Height, b.Height, "Height");
370 Assert.AreEqual (icon16.Width, b.Width, "Width");
371 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
372 Assert.AreEqual (2, b.Flags, "Flags");
378 [Ignore ("Icon32ToBitmap Not Working")]
380 public void Icon32ToBitmap ()
382 using (Bitmap b = icon32.ToBitmap ()) {
383 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
384 // unlike the GDI+ icon decoder the palette isn't kept
385 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
386 Assert.AreEqual (icon32.Height, b.Height, "Height");
387 Assert.AreEqual (icon32.Width, b.Width, "Width");
388 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
389 Assert.AreEqual (2, b.Flags, "Flags");
395 [Ignore ("Icon48ToBitmap Not Working")]
397 public void Icon48ToBitmap ()
399 using (Bitmap b = icon48.ToBitmap ()) {
400 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
401 // unlike the GDI+ icon decoder the palette isn't kept
402 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
403 Assert.AreEqual (icon48.Height, b.Height, "Height");
404 Assert.AreEqual (icon48.Width, b.Width, "Width");
405 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
406 Assert.AreEqual (2, b.Flags, "Flags");
412 [Ignore ("Icon64ToBitmap Not Working")]
414 public void Icon64ToBitmap ()
416 using (Bitmap b = icon64.ToBitmap ()) {
417 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
418 // unlike the GDI+ icon decoder the palette isn't kept
419 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
420 Assert.AreEqual (icon64.Height, b.Height, "Height");
421 Assert.AreEqual (icon64.Width, b.Width, "Width");
422 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
423 Assert.AreEqual (2, b.Flags, "Flags");
429 [Ignore ("Icon96ToBitmap Not Working")]
431 public void Icon96ToBitmap ()
433 using (Bitmap b = icon96.ToBitmap ()) {
434 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
435 // unlike the GDI+ icon decoder the palette isn't kept
436 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
437 Assert.AreEqual (icon96.Height, b.Height, "Height");
438 Assert.AreEqual (icon96.Width, b.Width, "Width");
439 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
440 Assert.AreEqual (2, b.Flags, "Flags");
446 [ExpectedException (typeof (ArgumentException))]
448 [Ignore ("ExtractAssociatedIcon is not implemented")]
450 public void ExtractAssociatedIcon_Null ()
453 Icon.ExtractAssociatedIcon (null);
458 [ExpectedException (typeof (ArgumentException))]
460 [Ignore ("ExtractAssociatedIcon is not implemented")]
462 public void ExtractAssociatedIcon_Empty ()
465 Icon.ExtractAssociatedIcon (String.Empty);
470 [ExpectedException (typeof (FileNotFoundException))]
472 [Ignore ("ExtractAssociatedIcon is not implemented")]
474 public void ExtractAssociatedIcon_DoesNotExists ()
477 Icon.ExtractAssociatedIcon ("does-not-exists.png");
485 [Ignore ("Unsafe code is not supported")]
487 public class IconFullTrustTest {
491 public void ExtractAssociatedIcon ()
493 string filename_dll = Assembly.GetExecutingAssembly ().Location;
494 Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
499 public void HandleRoundtrip ()
502 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
503 Assert.AreEqual (16, icon.Height, "Original.Height");
504 Assert.AreEqual (16, icon.Width, "Original.Width");
505 handle = icon.Handle;
506 using (Icon icon2 = Icon.FromHandle (handle)) {
507 Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
508 Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
509 Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
510 IconTest.SaveAndCompare ("Handle", icon2, false);
513 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
514 // commented / using freed memory is risky ;-)
515 /*using (Icon icon3 = Icon.FromHandle (handle)) {
516 Assert.AreEqual (0, icon3.Height, "Survivor.Height");
517 Assert.AreEqual (0, icon3.Width, "Survivor.Width");
518 Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
523 public void CreateMultipleIconFromSameHandle ()
526 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
527 Assert.AreEqual (16, icon.Height, "Original.Height");
528 Assert.AreEqual (16, icon.Width, "Original.Width");
529 handle = icon.Handle;
530 using (Icon icon2 = Icon.FromHandle (handle)) {
531 Assert.AreEqual (16, icon2.Height, "2.Height");
532 Assert.AreEqual (16, icon2.Width, "2.Width");
533 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
534 IconTest.SaveAndCompare ("Handle2", icon2, false);
536 using (Icon icon3 = Icon.FromHandle (handle)) {
537 Assert.AreEqual (16, icon3.Height, "3.Height");
538 Assert.AreEqual (16, icon3.Width, "3.Width");
539 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
540 IconTest.SaveAndCompare ("Handle3", icon3, false);
543 // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
544 // commented / using freed memory is risky ;-)
548 public void HiconRoundtrip ()
551 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
552 Assert.AreEqual (16, icon.Height, "Original.Height");
553 Assert.AreEqual (16, icon.Width, "Original.Width");
554 handle = icon.ToBitmap ().GetHicon ();
557 using (Icon icon2 = Icon.FromHandle (handle)) {
558 Assert.AreEqual (16, icon2.Height, "Survivor.Height");
559 Assert.AreEqual (16, icon2.Width, "Survivor.Width");
560 Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
561 IconTest.SaveAndCompare ("HICON", icon2, false);
566 public void CreateMultipleIconFromSameHICON ()
569 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
570 Assert.AreEqual (16, icon.Height, "Original.Height");
571 Assert.AreEqual (16, icon.Width, "Original.Width");
572 handle = icon.ToBitmap ().GetHicon ();
575 using (Icon icon2 = Icon.FromHandle (handle)) {
576 Assert.AreEqual (16, icon2.Height, "2.Height");
577 Assert.AreEqual (16, icon2.Width, "2.Width");
578 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
579 IconTest.SaveAndCompare ("HICON2", icon2, false);
581 using (Icon icon3 = Icon.FromHandle (handle)) {
582 Assert.AreEqual (16, icon3.Height, "3.Height");
583 Assert.AreEqual (16, icon3.Width, "3.Width");
584 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
585 IconTest.SaveAndCompare ("HICON", icon3, false);