Remove CAS from System.Drawing unit tests
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestIcon.cs
1 //
2 // Icon class testing unit
3 //
4 // Authors:
5 //      Gary Barnett <gary.barnett.mono@gmail.com>
6 //      Sanjay Gupta <gsanjay@novell.com>
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2004,2006-2008 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.ComponentModel;
33 using System.Drawing;
34 using System.Drawing.Imaging;
35 using System.IO;
36 using System.Reflection;
37 using System.Security.Permissions;
38 using NUnit.Framework;
39
40 namespace MonoTests.System.Drawing {
41
42         [TestFixture]   
43         public class IconTest {
44                 
45                 Icon icon;
46                 Icon icon16, icon32, icon48, icon64, icon96;
47                 FileStream fs1;
48
49                 static string filename_dll;
50
51                 // static ctor are executed outside the Deny
52                 static IconTest ()
53                 {
54                         filename_dll = Assembly.GetExecutingAssembly ().Location;
55                 }
56                 
57                 [SetUp]
58                 public void SetUp ()            
59                 {
60                         String path = TestBitmap.getInFile ("bitmaps/smiley.ico");
61                         icon = new Icon (path);                 
62                         fs1 = new FileStream (path, FileMode.Open);
63
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"));
69                 }
70
71                 [TearDown]
72                 public void TearDown ()
73                 {
74                         if (fs1 != null)
75                                 fs1.Close ();
76                         if (File.Exists ("newIcon.ico"))
77                                 File.Delete ("newIcon.ico");
78                 }
79
80                 [Test]
81                 public void TestConstructors ()
82                 {
83                         Assert.AreEqual (32, icon.Height, "C#0a");
84                         Assert.AreEqual (32, icon.Width, "C#0b");
85
86                         Icon newIcon = new Icon (fs1, 48, 48);
87                         Assert.AreEqual (48, newIcon.Height, "C#1a");                   
88                         Assert.AreEqual (48, newIcon.Width, "C#1b");
89
90                         newIcon = new Icon (icon, 16, 16);
91                         Assert.AreEqual (16, newIcon.Height, "C#2a");                   
92                         Assert.AreEqual (16, newIcon.Width, "C#2b");
93                 }
94
95                 [Test]
96                 public void Constructor_IconNull_Int_Int ()
97                 {
98                         Assert.Throws<ArgumentException> (() => new Icon ((Icon)null, 32, 32));
99                 }
100
101                 [Test]
102                 public void Constructor_Icon_IntNegative_Int ()
103                 {
104                         Icon neg = new Icon (icon, -32, 32);
105                         Assert.AreEqual (32, neg.Height, "Height");
106                         Assert.AreEqual (32, neg.Width, "Width");
107                 }
108
109                 [Test]
110                 public void Constructor_IconNull_Size ()
111                 {
112                         Assert.Throws<ArgumentException> (() => new Icon ((Icon) null, new Size (32, 32)));
113                 }
114
115                 [Test]
116                 public void Constructor_Icon_Size_Negative ()
117                 {
118                         Icon neg = new Icon (icon, new Size (-32, -32));
119                         Assert.AreEqual (16, neg.Height, "Height");
120                         Assert.AreEqual (16, neg.Width, "Width");
121                 }
122
123                 [Test]
124                 public void Constructor_Icon_Int_Int_NonSquare ()
125                 {
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");
129                 }
130
131                 [Test]
132                 public void Constructor_Icon_GetNormalSizeFromIconWith256 ()
133                 {
134                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
135
136                         Icon orig = new Icon (filepath);
137                         Assert.AreEqual (32,orig.Height);
138                         Assert.AreEqual (32,orig.Width);
139
140                         Icon ret = new Icon (orig, 48, 48);
141                         Assert.AreEqual (48, ret.Height);
142                         Assert.AreEqual (48, ret.Width);
143                 }
144
145                 [Test]
146                 public void Constructor_Icon_DoesntReturn256Passing0 ()
147                 {
148                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
149                         
150                         Icon orig = new Icon (filepath);
151                         Assert.AreEqual (32,orig.Height);
152                         Assert.AreEqual (32,orig.Width);
153                         
154                         Icon ret = new Icon (orig, 0, 0);
155                         Assert.AreNotEqual (0, ret.Height);
156                         Assert.AreNotEqual (0, ret.Width);
157                 }
158
159                 [Test]
160                 public void Constructor_Icon_DoesntReturn256Passing1 ()
161                 {
162                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
163                         
164                         Icon orig = new Icon (filepath);
165                         Assert.AreEqual (32,orig.Height);
166                         Assert.AreEqual (32,orig.Width);
167                         
168                         Icon ret = new Icon (orig, 1, 1);
169                         Assert.AreNotEqual (0, ret.Height);
170                         Assert.AreNotEqual (0, ret.Width);
171                 }
172
173                 [Test]
174                 public void Constructor_StreamNull ()
175                 {
176                         Assert.Throws<ArgumentException> (() => new Icon ((Stream) null));
177                 }
178
179                 [Test]
180                 public void Constructor_StreamNull_Int_Int ()
181                 {
182                         Assert.Throws<ArgumentException> (() => new Icon ((Stream) null, 32, 32));
183                 }
184
185                 [Test]
186                 public void Constructor_StringNull ()
187                 {
188                         Assert.Throws<ArgumentNullException> (() => new Icon ((string) null));
189                 }
190
191                 [Test]
192                 public void Constructor_TypeNull_String ()
193                 {
194                         Assert.Throws<NullReferenceException> (() => new Icon ((Type) null, "mono.ico"));
195                 }
196
197                 [Test]
198                 public void Constructor_Type_StringNull ()
199                 {
200                         Assert.Throws<ArgumentException> (() => new Icon (typeof (Icon), null));
201                 }
202                 [Test]
203                 public void Constructor_StreamNull_Size ()
204                 {
205                         Assert.Throws<ArgumentException> (() => new Icon ((Stream) null, new Size (32, 32)));
206                 }
207
208                 [Test]
209                 public void Constructor_StringNull_Size ()
210                 {
211                         Assert.Throws<ArgumentNullException> (() => new Icon ((string) null, new Size (32, 32)));
212                 }
213
214                 [Test]
215                 public void Constructor_StringNull_Int_Int ()
216                 {
217                         Assert.Throws<ArgumentNullException> (() => new Icon ((string) null, 32, 32));
218                 }
219
220                 [Test]
221                 public void TestProperties ()
222                 {
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");
227                 }
228
229                 [Test]
230                 public void Clone ()
231                 {
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");
237                 }
238
239                 [Test]
240                 public void CloneHandleIcon ()
241                 {
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");
247                 }
248
249                 private void XPIcon (int size)
250                 {
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");
258
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");
264                                 }
265                         }
266                 }
267
268                 [Test]
269                 public void Icon32bits_XP16 ()
270                 {
271                         XPIcon (16);
272                 }
273
274                 [Test]
275                 public void Icon32bits_XP32 ()
276                 {
277                         XPIcon (32);
278                 }
279
280                 [Test]
281                 public void Icon32bits_XP48 ()
282                 {
283                         XPIcon (48);
284                 }
285
286                 [Test]
287                 public void SelectFromUnusualSize_Small16 ()
288                 {
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");
295                                 }
296                         }
297                 }
298
299                 [Test]
300                 public void SelectFromUnusualSize_Normal32 ()
301                 {
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");
308                                 }
309                         }
310                 }
311
312                 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
313                 {
314                         using (MemoryStream ms = new MemoryStream ()) {
315                                 icon.Save (ms);
316                                 ms.Position = 0;
317
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");
321
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");
326
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);
331                                                                         if (alpha)
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));
336                                                                 }
337                                                         }
338                                                 }
339                                         }
340                                 }
341                         }
342                 }
343
344                 [Test]
345                 public void Save ()
346                 {
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);
352                 }
353
354                 [Test] // bug #410608
355                 public void Save_256 ()
356                 {
357                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
358
359                         using (Icon icon = new Icon (filepath)) {
360                                 // bug #415809 fixed
361                                 SaveAndCompare ("256", icon, true);
362                         }
363
364                         // binary comparison
365                         var orig = new MemoryStream (File.ReadAllBytes (filepath));
366                         var saved = new MemoryStream ();
367                         using (Icon icon = new Icon (filepath))
368                                 icon.Save (saved);
369                         FileAssert.AreEqual (orig, saved, "binary comparison");
370                 }
371
372                 [Test]
373                 public void Save_Null ()
374                 {
375                         Assert.Throws<NullReferenceException> (() => icon.Save (null));
376                 }
377
378                 [Test]
379                 public void Icon16ToBitmap ()
380                 {
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");
389                         }
390                 }
391
392                 [Test]
393                 public void Icon32ToBitmap ()
394                 {
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");
403                         }
404                 }
405
406                 [Test]
407                 public void Icon48ToBitmap ()
408                 {
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");
417                         }
418                 }
419
420                 [Test]
421                 public void Icon64ToBitmap ()
422                 {
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");
431                         }
432                 }
433
434                 [Test]
435                 public void Icon96ToBitmap ()
436                 {
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");
445                         }
446                 }
447
448                 [Test] // bug #415581
449                 public void Icon256ToBitmap ()
450                 {
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");
459                                 }
460                                 icon.Dispose ();
461                         }
462
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");
471                                 }
472                         }
473                 }
474
475                 [Test]
476                 public void Icon256ToBitmap_Request0 ()
477                 {
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");
487                                 }
488                         }
489                 }
490
491                 [Test]
492                 public void Only256InFile ()
493                 {
494                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/only256.ico"))) {
495                                 Assert.Throws<Win32Exception> (() => new Icon (fs, 0, 0));
496                         }
497                 }
498
499
500                 [Test]
501                 public void ExtractAssociatedIcon_Null ()
502                 {
503                         Assert.Throws<ArgumentException> (() => Icon.ExtractAssociatedIcon (null));
504                 }
505
506                 [Test]
507                 public void ExtractAssociatedIcon_Empty ()
508                 {
509                         Assert.Throws<ArgumentException> (() => Icon.ExtractAssociatedIcon (String.Empty));
510                 }
511
512                 [Test]
513                 public void ExtractAssociatedIcon_DoesNotExists ()
514                 {
515                         Assert.Throws<FileNotFoundException> (() => Icon.ExtractAssociatedIcon ("does-not-exists.png"));
516                 }
517
518                 private static bool RunningOnUnix {
519                         get {
520                                 int p = (int) Environment.OSVersion.Platform;
521
522                                 return (p == 4) || (p == 6) || (p == 128);
523                         }
524                 }
525         }
526
527         [TestFixture]
528         public class IconFullTrustTest {
529                 [Test]
530                 public void ExtractAssociatedIcon ()
531                 {
532                         string filename_dll = Assembly.GetExecutingAssembly ().Location;
533                         Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
534                 }
535
536                 [Test]
537                 public void HandleRoundtrip ()
538                 {
539                         IntPtr handle;
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);
549                                 }
550                         }
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");
557                         }*/
558                 }
559
560                 [Test]
561                 public void CreateMultipleIconFromSameHandle ()
562                 {
563                         IntPtr handle;
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);
573                                 }
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);
579                                 }
580                         }
581                         // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
582                         // commented / using freed memory is risky ;-)
583                 }
584
585                 [Test]
586                 public void HiconRoundtrip ()
587                 {
588                         IntPtr handle;
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 ();
593                         }
594                         // HICON survives
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);
600                         }
601                 }
602
603                 [Test]
604                 public void CreateMultipleIconFromSameHICON ()
605                 {
606                         IntPtr handle;
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 ();
611                         }
612                         // HICON survives
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);
618                         }
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);
624                         }
625                 }
626         }
627 }