Avoid an assertion in mono_thread_state_init_from_handle () when the domain is not...
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestIcon.cs
1 //
2 // Icon class testing unit
3 //
4 // Authors:
5 //      Sanjay Gupta <gsanjay@novell.com>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004,2006-2008 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Drawing;
32 using System.Drawing.Imaging;
33 using System.IO;
34 using System.Reflection;
35 using System.Security.Permissions;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Drawing {
39
40         [TestFixture]   
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class IconTest {
43                 
44                 Icon icon;
45                 Icon icon16, icon32, icon48, icon64, icon96;
46                 FileStream fs1;
47
48                 static string filename_dll;
49
50                 // static ctor are executed outside the Deny
51                 static IconTest ()
52                 {
53                         filename_dll = Assembly.GetExecutingAssembly ().Location;
54                 }
55                 
56                 [SetUp]
57                 public void SetUp ()            
58                 {
59                         String path = TestBitmap.getInFile ("bitmaps/smiley.ico");
60                         icon = new Icon (path);                 
61                         fs1 = new FileStream (path, FileMode.Open);
62
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"));
68                 }
69
70                 [TearDown]
71                 public void TearDown ()
72                 {
73                         if (fs1 != null)
74                                 fs1.Close ();
75                         if (File.Exists ("newIcon.ico"))
76                                 File.Delete ("newIcon.ico");
77                 }
78
79                 [Test]
80                 public void TestConstructors ()
81                 {
82                         Assert.AreEqual (32, icon.Height, "C#0a");
83                         Assert.AreEqual (32, icon.Width, "C#0b");
84
85                         Icon newIcon = new Icon (fs1, 48, 48);
86                         Assert.AreEqual (48, newIcon.Height, "C#1a");                   
87                         Assert.AreEqual (48, newIcon.Width, "C#1b");
88
89                         newIcon = new Icon (icon, 16, 16);
90                         Assert.AreEqual (16, newIcon.Height, "C#2a");                   
91                         Assert.AreEqual (16, newIcon.Width, "C#2b");
92                 }
93
94                 [Test]
95                 [ExpectedException (typeof (ArgumentException))]
96 #if TARGET_JVM
97                 [Ignore ("Check parameters")]
98 #endif
99                 public void Constructor_IconNull_Int_Int ()
100                 {
101                         new Icon ((Icon)null, 32, 32);
102                 }
103
104                 [Test]
105 #if TARGET_JVM
106                 [Ignore ("Constructor_Icon_IntNegative_Int Not Working")]
107 #endif
108                 public void Constructor_Icon_IntNegative_Int ()
109                 {
110                         Icon neg = new Icon (icon, -32, 32);
111                         Assert.AreEqual (32, neg.Height, "Height");
112                         Assert.AreEqual (32, neg.Width, "Width");
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentException))]
117 #if TARGET_JVM
118                 [Ignore ("Check parameters")]
119 #endif
120                 public void Constructor_IconNull_Size ()
121                 {
122                         new Icon ((Icon) null, new Size (32, 32));
123                 }
124
125                 [Test]
126 #if TARGET_JVM
127                 [Ignore ("Constructor_Icon_Size_Negative Not Working")]
128 #endif
129                 public void Constructor_Icon_Size_Negative ()
130                 {
131                         Icon neg = new Icon (icon, new Size (-32, -32));
132                         Assert.AreEqual (16, neg.Height, "Height");
133                         Assert.AreEqual (16, neg.Width, "Width");
134                 }
135
136                 [Test]
137 #if TARGET_JVM
138                 [Ignore ("Constructor_Icon_Int_Int_NonSquare Not Working")]
139 #endif
140                 public void Constructor_Icon_Int_Int_NonSquare ()
141                 {
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");
145                 }
146
147                 [Test]
148                 [ExpectedException (typeof (ArgumentException))]
149                 public void Constructor_StreamNull ()
150                 {
151                         new Icon ((Stream) null);
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (ArgumentException))]
156                 public void Constructor_StreamNull_Int_Int ()
157                 {
158                         new Icon ((Stream) null, 32, 32);
159                 }
160
161                 [Test]
162                 [ExpectedException (typeof (ArgumentNullException))]
163                 public void Constructor_StringNull ()
164                 {
165                         new Icon ((string) null);
166                 }
167
168                 [Test]
169                 [ExpectedException (typeof (NullReferenceException))]
170                 public void Constructor_TypeNull_String ()
171                 {
172                         new Icon ((Type) null, "mono.ico");
173                 }
174
175                 [Test]
176                 [ExpectedException (typeof (ArgumentException))]
177 #if TARGET_JVM
178                 [Ignore ("Check parameters")]
179 #endif
180                 public void Constructor_Type_StringNull ()
181                 {
182                         new Icon (typeof (Icon), null);
183                 }
184 #if NET_2_0
185                 [Test]
186                 [ExpectedException (typeof (ArgumentException))]
187 #if TARGET_JVM
188                 [Ignore ("Constructor_StreamNull_Size Not Implemented")]
189 #endif
190                 public void Constructor_StreamNull_Size ()
191                 {
192 #if !TARGET_JVM
193                         new Icon ((Stream) null, new Size (32, 32));
194 #endif
195                 }
196
197                 [Test]
198                 [ExpectedException (typeof (ArgumentNullException))]
199 #if TARGET_JVM
200                 [Ignore ("Constructor_StringNull_Size Not Implemented")]
201 #endif
202                 public void Constructor_StringNull_Size ()
203                 {
204 #if !TARGET_JVM
205                         new Icon ((string) null, new Size (32, 32));
206 #endif
207                 }
208
209                 [Test]
210                 [ExpectedException (typeof (ArgumentNullException))]
211 #if TARGET_JVM
212                 [Ignore ("Constructor_StringNull_Int_Int Not Implemented")]
213 #endif
214                 public void Constructor_StringNull_Int_Int ()
215                 {
216 #if !TARGET_JVM
217                         new Icon ((string) null, 32, 32);
218 #endif
219                 }
220 #endif
221
222                 [Test]
223                 public void TestProperties ()
224                 {
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");
229                 }
230
231                 [Test]
232                 public void Clone ()
233                 {
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");
239                 }
240
241 #if !TARGET_JVM
242                 [Test]
243                 public void CloneHandleIcon ()
244                 {
245                         Icon clone = (Icon) Icon.FromHandle (SystemIcons.Hand.Handle).Clone ();
246                         Assert.AreEqual (SystemIcons.Hand.Height, clone.Height, "Height");
247                         Assert.AreEqual (SystemIcons.Hand.Width, clone.Width, "Width");
248                         Assert.AreEqual (SystemIcons.Hand.Size.Width, clone.Size.Width, "Size.Width");
249                         Assert.AreEqual (SystemIcons.Hand.Size.Height, clone.Size.Height, "Size.Height");
250                 }
251 #endif
252
253                 private void XPIcon (int size)
254                 {
255                         // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
256                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
257                                 using (Icon xp = new Icon (fs, size, size)) {
258                                         Assert.AreEqual (size, xp.Height, "Height");
259                                         Assert.AreEqual (size, xp.Width, "Width");
260                                         Assert.AreEqual (size, xp.Size.Width, "Size.Width");
261                                         Assert.AreEqual (size, xp.Size.Height, "Size.Height");
262
263                                         Bitmap bmp = xp.ToBitmap ();
264                                         Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
265                                         Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
266                                         Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
267                                         Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
268                                 }
269                         }
270                 }
271
272                 [Test]
273                 public void Icon32bits_XP16 ()
274                 {
275                         XPIcon (16);
276                 }
277
278                 [Test]
279                 public void Icon32bits_XP32 ()
280                 {
281                         XPIcon (32);
282                 }
283
284                 [Test]
285                 public void Icon32bits_XP48 ()
286                 {
287                         XPIcon (48);
288                 }
289
290                 [Test]
291 #if TARGET_JVM
292                 [Ignore ("SelectFromUnusualSize_Small16 Not Working")]
293 #endif
294                 public void SelectFromUnusualSize_Small16 ()
295                 {
296                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
297                                 using (Icon xp = new Icon (fs, 16, 16)) {
298                                         Assert.AreEqual (16, xp.Height, "Height");
299                                         Assert.AreEqual (10, xp.Width, "Width");
300                                         Assert.AreEqual (10, xp.Size.Width, "Size.Width");
301                                         Assert.AreEqual (16, xp.Size.Height, "Size.Height");
302                                 }
303                         }
304                 }
305
306                 [Test]
307                 public void SelectFromUnusualSize_Normal32 ()
308                 {
309                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
310                                 using (Icon xp = new Icon (fs, 32, 32)) {
311                                         Assert.AreEqual (22, xp.Height, "Height");
312                                         Assert.AreEqual (11, xp.Width, "Width");
313                                         Assert.AreEqual (11, xp.Size.Width, "Size.Width");
314                                         Assert.AreEqual (22, xp.Size.Height, "Size.Height");
315                                 }
316                         }
317                 }
318
319                 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
320                 {
321                         using (MemoryStream ms = new MemoryStream ()) {
322                                 icon.Save (ms);
323                                 ms.Position = 0;
324
325                                 using (Icon loaded = new Icon (ms)) {
326                                         Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
327                                         Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
328
329                                         using (Bitmap expected = icon.ToBitmap ()) {
330                                                 using (Bitmap actual = loaded.ToBitmap ()) {
331                                                         Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
332                                                         Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
333
334                                                         for (int y = 0; y < expected.Height; y++) {
335                                                                 for (int x = 0; x < expected.Width; x++) {
336                                                                         Color e = expected.GetPixel (x, y);
337                                                                         Color a = actual.GetPixel (x, y);
338                                                                         if (alpha)
339                                                                                 Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
340                                                                         Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
341                                                                         Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
342                                                                         Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
343                                                                 }
344                                                         }
345                                                 }
346                                         }
347                                 }
348                         }
349                 }
350
351                 [Test]
352                 public void Save ()
353                 {
354                         SaveAndCompare ("16", icon16, true);
355                         SaveAndCompare ("32", icon32, true);
356                         SaveAndCompare ("48", icon48, true);
357                         SaveAndCompare ("64", icon64, true);
358                         SaveAndCompare ("96", icon96, true);
359                 }
360
361                 [Test] // bug #410608
362                 public void Save_256 ()
363                 {
364                         if (RunningOnUnix)
365                                 Assert.Ignore ("Depends on bug #323511");
366
367                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/323511.ico"))) {
368                                 // FIXME: use this instead after bug #415809 is fixed
369                                 //SaveAndCompare ("256", icon, true);
370
371                                 MemoryStream ms = new MemoryStream ();
372                                 icon.Save (ms);
373                                 ms.Position = 0;
374
375                                 using (Icon loaded = new Icon (ms)) {
376                                         Assert.AreEqual (icon.Height, loaded.Height, "Loaded.Height");
377                                         Assert.AreEqual (icon.Width, loaded.Width, "Loaded.Width");
378                                 }
379                         }
380                 }
381
382                 [Test]
383                 [ExpectedException (typeof (NullReferenceException))]
384 #if TARGET_JVM
385                 [Ignore ("Throws NullReference, do we need to follow?")]
386 #endif
387                 public void Save_Null ()
388                 {
389                         icon.Save (null);
390                 }
391
392                 [Test]
393 #if TARGET_JVM
394                 [Ignore ("Icon16ToBitmap Not Working")]
395 #endif
396                 public void Icon16ToBitmap ()
397                 {
398                         using (Bitmap b = icon16.ToBitmap ()) {
399                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
400                                 // unlike the GDI+ icon decoder the palette isn't kept
401                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
402                                 Assert.AreEqual (icon16.Height, b.Height, "Height");
403                                 Assert.AreEqual (icon16.Width, b.Width, "Width");
404                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
405                                 Assert.AreEqual (2, b.Flags, "Flags");
406                         }
407                 }
408
409                 [Test]
410 #if TARGET_JVM
411                 [Ignore ("Icon32ToBitmap Not Working")]
412 #endif
413                 public void Icon32ToBitmap ()
414                 {
415                         using (Bitmap b = icon32.ToBitmap ()) {
416                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
417                                 // unlike the GDI+ icon decoder the palette isn't kept
418                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
419                                 Assert.AreEqual (icon32.Height, b.Height, "Height");
420                                 Assert.AreEqual (icon32.Width, b.Width, "Width");
421                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
422                                 Assert.AreEqual (2, b.Flags, "Flags");
423                         }
424                 }
425
426                 [Test]
427 #if TARGET_JVM
428                 [Ignore ("Icon48ToBitmap Not Working")]
429 #endif
430                 public void Icon48ToBitmap ()
431                 {
432                         using (Bitmap b = icon48.ToBitmap ()) {
433                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
434                                 // unlike the GDI+ icon decoder the palette isn't kept
435                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
436                                 Assert.AreEqual (icon48.Height, b.Height, "Height");
437                                 Assert.AreEqual (icon48.Width, b.Width, "Width");
438                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
439                                 Assert.AreEqual (2, b.Flags, "Flags");
440                         }
441                 }
442
443                 [Test]
444 #if TARGET_JVM
445                 [Ignore ("Icon64ToBitmap Not Working")]
446 #endif
447                 public void Icon64ToBitmap ()
448                 {
449                         using (Bitmap b = icon64.ToBitmap ()) {
450                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
451                                 // unlike the GDI+ icon decoder the palette isn't kept
452                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
453                                 Assert.AreEqual (icon64.Height, b.Height, "Height");
454                                 Assert.AreEqual (icon64.Width, b.Width, "Width");
455                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
456                                 Assert.AreEqual (2, b.Flags, "Flags");
457                         }
458                 }
459
460                 [Test]
461 #if TARGET_JVM
462                 [Ignore ("Icon96ToBitmap Not Working")]
463 #endif
464                 public void Icon96ToBitmap ()
465                 {
466                         using (Bitmap b = icon96.ToBitmap ()) {
467                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
468                                 // unlike the GDI+ icon decoder the palette isn't kept
469                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
470                                 Assert.AreEqual (icon96.Height, b.Height, "Height");
471                                 Assert.AreEqual (icon96.Width, b.Width, "Width");
472                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
473                                 Assert.AreEqual (2, b.Flags, "Flags");
474                         }
475                 }
476
477                 [Test] // bug #415581
478 #if TARGET_JVM
479                 [Ignore ("Icon256ToBitmap Not Working")]
480 #endif
481                 public void Icon256ToBitmap ()
482                 {
483                         if (RunningOnUnix)
484                                 Assert.Ignore ("Depends on bug #323511");
485
486                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
487                                 Icon icon = new Icon (fs, 48, 48);
488                                 using (Bitmap b = icon.ToBitmap ()) {
489                                         Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
490                                         Assert.AreEqual (48, b.Height, "#A2");
491                                         Assert.AreEqual (48, b.Width, "#A3");
492                                         Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
493                                         Assert.AreEqual (2, b.Flags, "#A5");
494                                 }
495                                 icon.Dispose ();
496                         }
497
498                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
499                                 Icon icon = new Icon (fs, 256, 256);
500                                 using (Bitmap b = icon.ToBitmap ()) {
501                                         Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
502                                         Assert.AreEqual (48, b.Height, "#B2");
503                                         Assert.AreEqual (48, b.Width, "#B3");
504                                         Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
505                                         Assert.AreEqual (2, b.Flags, "#B5");
506                                 }
507                         }
508                 }
509
510 #if NET_2_0
511                 [Test]
512                 [ExpectedException (typeof (ArgumentException))]
513 #if TARGET_JVM
514                 [Ignore ("ExtractAssociatedIcon is not implemented")]
515 #endif
516                 public void ExtractAssociatedIcon_Null ()
517                 {
518 #if !TARGET_JVM
519                         Icon.ExtractAssociatedIcon (null);
520 #endif
521                 }
522
523                 [Test]
524                 [ExpectedException (typeof (ArgumentException))]
525 #if TARGET_JVM
526                 [Ignore ("ExtractAssociatedIcon is not implemented")]
527 #endif
528                 public void ExtractAssociatedIcon_Empty ()
529                 {
530 #if !TARGET_JVM
531                         Icon.ExtractAssociatedIcon (String.Empty);
532 #endif
533                 }
534
535                 [Test]
536                 [ExpectedException (typeof (FileNotFoundException))]
537 #if TARGET_JVM
538                 [Ignore ("ExtractAssociatedIcon is not implemented")]
539 #endif
540                 public void ExtractAssociatedIcon_DoesNotExists ()
541                 {
542 #if !TARGET_JVM
543                         Icon.ExtractAssociatedIcon ("does-not-exists.png");
544 #endif
545                 }
546 #endif
547
548                 private static bool RunningOnUnix {
549                         get {
550                                 int p = (int) Environment.OSVersion.Platform;
551
552                                 return (p == 4) || (p == 6) || (p == 128);
553                         }
554                 }
555         }
556
557         [TestFixture]
558 #if TARGET_JVM
559         [Ignore ("Unsafe code is not supported")]
560 #endif  
561         public class IconFullTrustTest {
562 #if !TARGET_JVM
563 #if NET_2_0
564                 [Test]
565                 public void ExtractAssociatedIcon ()
566                 {
567                         string filename_dll = Assembly.GetExecutingAssembly ().Location;
568                         Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
569                 }
570 #endif
571
572                 [Test]
573                 public void HandleRoundtrip ()
574                 {
575                         IntPtr handle;
576                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
577                                 Assert.AreEqual (16, icon.Height, "Original.Height");
578                                 Assert.AreEqual (16, icon.Width, "Original.Width");
579                                 handle = icon.Handle;
580                                 using (Icon icon2 = Icon.FromHandle (handle)) {
581                                         Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
582                                         Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
583                                         Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
584                                         IconTest.SaveAndCompare ("Handle", icon2, false);
585                                 }
586                         }
587                         // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
588                         // commented / using freed memory is risky ;-)
589                         /*using (Icon icon3 = Icon.FromHandle (handle)) {
590                                 Assert.AreEqual (0, icon3.Height, "Survivor.Height");
591                                 Assert.AreEqual (0, icon3.Width, "Survivor.Width");
592                                 Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
593                         }*/
594                 }
595
596                 [Test]
597                 public void CreateMultipleIconFromSameHandle ()
598                 {
599                         IntPtr handle;
600                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
601                                 Assert.AreEqual (16, icon.Height, "Original.Height");
602                                 Assert.AreEqual (16, icon.Width, "Original.Width");
603                                 handle = icon.Handle;
604                                 using (Icon icon2 = Icon.FromHandle (handle)) {
605                                         Assert.AreEqual (16, icon2.Height, "2.Height");
606                                         Assert.AreEqual (16, icon2.Width, "2.Width");
607                                         Assert.AreEqual (handle, icon2.Handle, "2.Handle");
608                                         IconTest.SaveAndCompare ("Handle2", icon2, false);
609                                 }
610                                 using (Icon icon3 = Icon.FromHandle (handle)) {
611                                         Assert.AreEqual (16, icon3.Height, "3.Height");
612                                         Assert.AreEqual (16, icon3.Width, "3.Width");
613                                         Assert.AreEqual (handle, icon3.Handle, "3.Handle");
614                                         IconTest.SaveAndCompare ("Handle3", icon3, false);
615                                 }
616                         }
617                         // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
618                         // commented / using freed memory is risky ;-)
619                 }
620
621                 [Test]
622                 public void HiconRoundtrip ()
623                 {
624                         IntPtr handle;
625                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
626                                 Assert.AreEqual (16, icon.Height, "Original.Height");
627                                 Assert.AreEqual (16, icon.Width, "Original.Width");
628                                 handle = icon.ToBitmap ().GetHicon ();
629                         }
630                         // HICON survives
631                         using (Icon icon2 = Icon.FromHandle (handle)) {
632                                 Assert.AreEqual (16, icon2.Height, "Survivor.Height");
633                                 Assert.AreEqual (16, icon2.Width, "Survivor.Width");
634                                 Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
635                                 IconTest.SaveAndCompare ("HICON", icon2, false);
636                         }
637                 }
638
639                 [Test]
640                 public void CreateMultipleIconFromSameHICON ()
641                 {
642                         IntPtr handle;
643                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
644                                 Assert.AreEqual (16, icon.Height, "Original.Height");
645                                 Assert.AreEqual (16, icon.Width, "Original.Width");
646                                 handle = icon.ToBitmap ().GetHicon ();
647                         }
648                         // HICON survives
649                         using (Icon icon2 = Icon.FromHandle (handle)) {
650                                 Assert.AreEqual (16, icon2.Height, "2.Height");
651                                 Assert.AreEqual (16, icon2.Width, "2.Width");
652                                 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
653                                 IconTest.SaveAndCompare ("HICON2", icon2, false);
654                         }
655                         using (Icon icon3 = Icon.FromHandle (handle)) {
656                                 Assert.AreEqual (16, icon3.Height, "3.Height");
657                                 Assert.AreEqual (16, icon3.Width, "3.Width");
658                                 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
659                                 IconTest.SaveAndCompare ("HICON", icon3, false);
660                         }
661                 }
662 #endif
663         }
664 }