[bcl] Remove more NET_2_0 checks from class libs
[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.Drawing;
33 using System.Drawing.Imaging;
34 using System.IO;
35 using System.Reflection;
36 using System.Security.Permissions;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Drawing {
40
41         [TestFixture]   
42         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
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                 [ExpectedException (typeof (ArgumentException))]
97                 public void Constructor_IconNull_Int_Int ()
98                 {
99                         new Icon ((Icon)null, 32, 32);
100                 }
101
102                 [Test]
103                 public void Constructor_Icon_IntNegative_Int ()
104                 {
105                         Icon neg = new Icon (icon, -32, 32);
106                         Assert.AreEqual (32, neg.Height, "Height");
107                         Assert.AreEqual (32, neg.Width, "Width");
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (ArgumentException))]
112                 public void Constructor_IconNull_Size ()
113                 {
114                         new Icon ((Icon) null, new Size (32, 32));
115                 }
116
117                 [Test]
118                 public void Constructor_Icon_Size_Negative ()
119                 {
120                         Icon neg = new Icon (icon, new Size (-32, -32));
121                         Assert.AreEqual (16, neg.Height, "Height");
122                         Assert.AreEqual (16, neg.Width, "Width");
123                 }
124
125                 [Test]
126                 public void Constructor_Icon_Int_Int_NonSquare ()
127                 {
128                         Icon non_square = new Icon (icon, 32, 16);
129                         Assert.AreEqual (32, non_square.Height, "Height");
130                         Assert.AreEqual (32, non_square.Width, "Width");
131                 }
132
133                 [Test]
134                 public void Constructor_Icon_GetNormalSizeFromIconWith256 ()
135                 {
136                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
137
138                         Icon orig = new Icon (filepath);
139                         Assert.AreEqual (32,orig.Height);
140                         Assert.AreEqual (32,orig.Width);
141
142                         Icon ret = new Icon (orig, 48, 48);
143                         Assert.AreEqual (48, ret.Height);
144                         Assert.AreEqual (48, ret.Width);
145                 }
146
147                 [Test]
148                 public void Constructor_Icon_DoesntReturn256Passing0 ()
149                 {
150                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
151                         
152                         Icon orig = new Icon (filepath);
153                         Assert.AreEqual (32,orig.Height);
154                         Assert.AreEqual (32,orig.Width);
155                         
156                         Icon ret = new Icon (orig, 0, 0);
157                         Assert.AreNotEqual (0, ret.Height);
158                         Assert.AreNotEqual (0, ret.Width);
159                 }
160
161                 [Test]
162                 public void Constructor_Icon_DoesntReturn256Passing1 ()
163                 {
164                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
165                         
166                         Icon orig = new Icon (filepath);
167                         Assert.AreEqual (32,orig.Height);
168                         Assert.AreEqual (32,orig.Width);
169                         
170                         Icon ret = new Icon (orig, 1, 1);
171                         Assert.AreNotEqual (0, ret.Height);
172                         Assert.AreNotEqual (0, ret.Width);
173                 }
174
175                 [Test]
176                 [ExpectedException (typeof (ArgumentException))]
177                 public void Constructor_StreamNull ()
178                 {
179                         new Icon ((Stream) null);
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentException))]
184                 public void Constructor_StreamNull_Int_Int ()
185                 {
186                         new Icon ((Stream) null, 32, 32);
187                 }
188
189                 [Test]
190                 [ExpectedException (typeof (ArgumentNullException))]
191                 public void Constructor_StringNull ()
192                 {
193                         new Icon ((string) null);
194                 }
195
196                 [Test]
197                 [ExpectedException (typeof (NullReferenceException))]
198                 public void Constructor_TypeNull_String ()
199                 {
200                         new Icon ((Type) null, "mono.ico");
201                 }
202
203                 [Test]
204                 [ExpectedException (typeof (ArgumentException))]
205                 public void Constructor_Type_StringNull ()
206                 {
207                         new Icon (typeof (Icon), null);
208                 }
209                 [Test]
210                 [ExpectedException (typeof (ArgumentException))]
211                 public void Constructor_StreamNull_Size ()
212                 {
213                         new Icon ((Stream) null, new Size (32, 32));
214                 }
215
216                 [Test]
217                 [ExpectedException (typeof (ArgumentNullException))]
218                 public void Constructor_StringNull_Size ()
219                 {
220                         new Icon ((string) null, new Size (32, 32));
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof (ArgumentNullException))]
225                 public void Constructor_StringNull_Int_Int ()
226                 {
227                         new Icon ((string) null, 32, 32);
228                 }
229
230                 [Test]
231                 public void TestProperties ()
232                 {
233                         Assert.AreEqual (32, icon.Height, "P#1");
234                         Assert.AreEqual (32, icon.Width, "P#2");
235                         Assert.AreEqual (32, icon.Size.Width, "P#3");
236                         Assert.AreEqual (32, icon.Size.Height, "P#4");
237                 }
238
239                 [Test]
240                 public void Clone ()
241                 {
242                         Icon clone = (Icon) icon.Clone ();
243                         Assert.AreEqual (32, clone.Height, "Height");
244                         Assert.AreEqual (32, clone.Width, "Width");
245                         Assert.AreEqual (32, clone.Size.Width, "Size.Width");
246                         Assert.AreEqual (32, clone.Size.Height, "Size.Height");
247                 }
248
249                 [Test]
250                 public void CloneHandleIcon ()
251                 {
252                         Icon clone = (Icon) Icon.FromHandle (SystemIcons.Hand.Handle).Clone ();
253                         Assert.AreEqual (SystemIcons.Hand.Height, clone.Height, "Height");
254                         Assert.AreEqual (SystemIcons.Hand.Width, clone.Width, "Width");
255                         Assert.AreEqual (SystemIcons.Hand.Size.Width, clone.Size.Width, "Size.Width");
256                         Assert.AreEqual (SystemIcons.Hand.Size.Height, clone.Size.Height, "Size.Height");
257                 }
258
259                 private void XPIcon (int size)
260                 {
261                         // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
262                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
263                                 using (Icon xp = new Icon (fs, size, size)) {
264                                         Assert.AreEqual (size, xp.Height, "Height");
265                                         Assert.AreEqual (size, xp.Width, "Width");
266                                         Assert.AreEqual (size, xp.Size.Width, "Size.Width");
267                                         Assert.AreEqual (size, xp.Size.Height, "Size.Height");
268
269                                         Bitmap bmp = xp.ToBitmap ();
270                                         Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
271                                         Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
272                                         Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
273                                         Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
274                                 }
275                         }
276                 }
277
278                 [Test]
279                 public void Icon32bits_XP16 ()
280                 {
281                         XPIcon (16);
282                 }
283
284                 [Test]
285                 public void Icon32bits_XP32 ()
286                 {
287                         XPIcon (32);
288                 }
289
290                 [Test]
291                 public void Icon32bits_XP48 ()
292                 {
293                         XPIcon (48);
294                 }
295
296                 [Test]
297                 public void SelectFromUnusualSize_Small16 ()
298                 {
299                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
300                                 using (Icon xp = new Icon (fs, 16, 16)) {
301                                         Assert.AreEqual (16, xp.Height, "Height");
302                                         Assert.AreEqual (10, xp.Width, "Width");
303                                         Assert.AreEqual (10, xp.Size.Width, "Size.Width");
304                                         Assert.AreEqual (16, xp.Size.Height, "Size.Height");
305                                 }
306                         }
307                 }
308
309                 [Test]
310                 public void SelectFromUnusualSize_Normal32 ()
311                 {
312                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
313                                 using (Icon xp = new Icon (fs, 32, 32)) {
314                                         Assert.AreEqual (22, xp.Height, "Height");
315                                         Assert.AreEqual (11, xp.Width, "Width");
316                                         Assert.AreEqual (11, xp.Size.Width, "Size.Width");
317                                         Assert.AreEqual (22, xp.Size.Height, "Size.Height");
318                                 }
319                         }
320                 }
321
322                 internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
323                 {
324                         using (MemoryStream ms = new MemoryStream ()) {
325                                 icon.Save (ms);
326                                 ms.Position = 0;
327
328                                 using (Icon loaded = new Icon (ms)) {
329                                         Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
330                                         Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
331
332                                         using (Bitmap expected = icon.ToBitmap ()) {
333                                                 using (Bitmap actual = loaded.ToBitmap ()) {
334                                                         Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
335                                                         Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
336
337                                                         for (int y = 0; y < expected.Height; y++) {
338                                                                 for (int x = 0; x < expected.Width; x++) {
339                                                                         Color e = expected.GetPixel (x, y);
340                                                                         Color a = actual.GetPixel (x, y);
341                                                                         if (alpha)
342                                                                                 Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
343                                                                         Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
344                                                                         Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
345                                                                         Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
346                                                                 }
347                                                         }
348                                                 }
349                                         }
350                                 }
351                         }
352                 }
353
354                 [Test]
355                 public void Save ()
356                 {
357                         SaveAndCompare ("16", icon16, true);
358                         SaveAndCompare ("32", icon32, true);
359                         SaveAndCompare ("48", icon48, true);
360                         SaveAndCompare ("64", icon64, true);
361                         SaveAndCompare ("96", icon96, true);
362                 }
363
364                 [Test] // bug #410608
365                 public void Save_256 ()
366                 {
367                         string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
368
369                         using (Icon icon = new Icon (filepath)) {
370                                 // bug #415809 fixed
371                                 SaveAndCompare ("256", icon, true);
372                         }
373
374                         // binary comparison
375                         var orig = new MemoryStream (File.ReadAllBytes (filepath));
376                         var saved = new MemoryStream ();
377                         using (Icon icon = new Icon (filepath))
378                                 icon.Save (saved);
379                         FileAssert.AreEqual (orig, saved, "binary comparison");
380                 }
381
382                 [Test]
383                 [ExpectedException (typeof (NullReferenceException))]
384                 public void Save_Null ()
385                 {
386                         icon.Save (null);
387                 }
388
389                 [Test]
390                 public void Icon16ToBitmap ()
391                 {
392                         using (Bitmap b = icon16.ToBitmap ()) {
393                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
394                                 // unlike the GDI+ icon decoder the palette isn't kept
395                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
396                                 Assert.AreEqual (icon16.Height, b.Height, "Height");
397                                 Assert.AreEqual (icon16.Width, b.Width, "Width");
398                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
399                                 Assert.AreEqual (2, b.Flags, "Flags");
400                         }
401                 }
402
403                 [Test]
404                 public void Icon32ToBitmap ()
405                 {
406                         using (Bitmap b = icon32.ToBitmap ()) {
407                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
408                                 // unlike the GDI+ icon decoder the palette isn't kept
409                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
410                                 Assert.AreEqual (icon32.Height, b.Height, "Height");
411                                 Assert.AreEqual (icon32.Width, b.Width, "Width");
412                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
413                                 Assert.AreEqual (2, b.Flags, "Flags");
414                         }
415                 }
416
417                 [Test]
418                 public void Icon48ToBitmap ()
419                 {
420                         using (Bitmap b = icon48.ToBitmap ()) {
421                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
422                                 // unlike the GDI+ icon decoder the palette isn't kept
423                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
424                                 Assert.AreEqual (icon48.Height, b.Height, "Height");
425                                 Assert.AreEqual (icon48.Width, b.Width, "Width");
426                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
427                                 Assert.AreEqual (2, b.Flags, "Flags");
428                         }
429                 }
430
431                 [Test]
432                 public void Icon64ToBitmap ()
433                 {
434                         using (Bitmap b = icon64.ToBitmap ()) {
435                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
436                                 // unlike the GDI+ icon decoder the palette isn't kept
437                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
438                                 Assert.AreEqual (icon64.Height, b.Height, "Height");
439                                 Assert.AreEqual (icon64.Width, b.Width, "Width");
440                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
441                                 Assert.AreEqual (2, b.Flags, "Flags");
442                         }
443                 }
444
445                 [Test]
446                 public void Icon96ToBitmap ()
447                 {
448                         using (Bitmap b = icon96.ToBitmap ()) {
449                                 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
450                                 // unlike the GDI+ icon decoder the palette isn't kept
451                                 Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
452                                 Assert.AreEqual (icon96.Height, b.Height, "Height");
453                                 Assert.AreEqual (icon96.Width, b.Width, "Width");
454                                 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
455                                 Assert.AreEqual (2, b.Flags, "Flags");
456                         }
457                 }
458
459                 [Test] // bug #415581
460                 public void Icon256ToBitmap ()
461                 {
462                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
463                                 Icon icon = new Icon (fs, 48, 48);
464                                 using (Bitmap b = icon.ToBitmap ()) {
465                                         Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
466                                         Assert.AreEqual (48, b.Height, "#A2");
467                                         Assert.AreEqual (48, b.Width, "#A3");
468                                         Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
469                                         Assert.AreEqual (2, b.Flags, "#A5");
470                                 }
471                                 icon.Dispose ();
472                         }
473
474                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
475                                 Icon icon = new Icon (fs, 256, 256);
476                                 using (Bitmap b = icon.ToBitmap ()) {
477                                         Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
478                                         Assert.AreEqual (48, b.Height, "#B2");
479                                         Assert.AreEqual (48, b.Width, "#B3");
480                                         Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
481                                         Assert.AreEqual (2, b.Flags, "#B5");
482                                 }
483                         }
484                 }
485
486                 [Test]
487                 public void Icon256ToBitmap_Request0 ()
488                 {
489                         // 415581.ico has 2 images, the 256 and 48
490                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
491                                 Icon icon = new Icon (fs, 0, 0);
492                                 using (Bitmap b = icon.ToBitmap ()) {
493                                         Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
494                                         Assert.AreEqual (48, b.Height, "#B2");
495                                         Assert.AreEqual (48, b.Width, "#B3");
496                                         Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
497                                         Assert.AreEqual (2, b.Flags, "#B5");
498                                 }
499                         }
500                 }
501
502                 [Test, ExpectedException ()] //ToDo: System.ComponentModel.Win32Exception
503                 public void Only256InFile ()
504                 {
505                         using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/only256.ico"))) {
506                                 Icon icon = new Icon (fs, 0, 0);
507                         }
508                 }
509
510
511                 [Test]
512                 [ExpectedException (typeof (ArgumentException))]
513                 public void ExtractAssociatedIcon_Null ()
514                 {
515                         Icon.ExtractAssociatedIcon (null);
516                 }
517
518                 [Test]
519                 [ExpectedException (typeof (ArgumentException))]
520                 public void ExtractAssociatedIcon_Empty ()
521                 {
522                         Icon.ExtractAssociatedIcon (String.Empty);
523                 }
524
525                 [Test]
526                 [ExpectedException (typeof (FileNotFoundException))]
527                 public void ExtractAssociatedIcon_DoesNotExists ()
528                 {
529                         Icon.ExtractAssociatedIcon ("does-not-exists.png");
530                 }
531
532                 private static bool RunningOnUnix {
533                         get {
534                                 int p = (int) Environment.OSVersion.Platform;
535
536                                 return (p == 4) || (p == 6) || (p == 128);
537                         }
538                 }
539         }
540
541         [TestFixture]
542         public class IconFullTrustTest {
543                 [Test]
544                 public void ExtractAssociatedIcon ()
545                 {
546                         string filename_dll = Assembly.GetExecutingAssembly ().Location;
547                         Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
548                 }
549
550                 [Test]
551                 public void HandleRoundtrip ()
552                 {
553                         IntPtr handle;
554                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
555                                 Assert.AreEqual (16, icon.Height, "Original.Height");
556                                 Assert.AreEqual (16, icon.Width, "Original.Width");
557                                 handle = icon.Handle;
558                                 using (Icon icon2 = Icon.FromHandle (handle)) {
559                                         Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
560                                         Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
561                                         Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
562                                         IconTest.SaveAndCompare ("Handle", icon2, false);
563                                 }
564                         }
565                         // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
566                         // commented / using freed memory is risky ;-)
567                         /*using (Icon icon3 = Icon.FromHandle (handle)) {
568                                 Assert.AreEqual (0, icon3.Height, "Survivor.Height");
569                                 Assert.AreEqual (0, icon3.Width, "Survivor.Width");
570                                 Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
571                         }*/
572                 }
573
574                 [Test]
575                 public void CreateMultipleIconFromSameHandle ()
576                 {
577                         IntPtr handle;
578                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
579                                 Assert.AreEqual (16, icon.Height, "Original.Height");
580                                 Assert.AreEqual (16, icon.Width, "Original.Width");
581                                 handle = icon.Handle;
582                                 using (Icon icon2 = Icon.FromHandle (handle)) {
583                                         Assert.AreEqual (16, icon2.Height, "2.Height");
584                                         Assert.AreEqual (16, icon2.Width, "2.Width");
585                                         Assert.AreEqual (handle, icon2.Handle, "2.Handle");
586                                         IconTest.SaveAndCompare ("Handle2", icon2, false);
587                                 }
588                                 using (Icon icon3 = Icon.FromHandle (handle)) {
589                                         Assert.AreEqual (16, icon3.Height, "3.Height");
590                                         Assert.AreEqual (16, icon3.Width, "3.Width");
591                                         Assert.AreEqual (handle, icon3.Handle, "3.Handle");
592                                         IconTest.SaveAndCompare ("Handle3", icon3, false);
593                                 }
594                         }
595                         // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
596                         // commented / using freed memory is risky ;-)
597                 }
598
599                 [Test]
600                 public void HiconRoundtrip ()
601                 {
602                         IntPtr handle;
603                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
604                                 Assert.AreEqual (16, icon.Height, "Original.Height");
605                                 Assert.AreEqual (16, icon.Width, "Original.Width");
606                                 handle = icon.ToBitmap ().GetHicon ();
607                         }
608                         // HICON survives
609                         using (Icon icon2 = Icon.FromHandle (handle)) {
610                                 Assert.AreEqual (16, icon2.Height, "Survivor.Height");
611                                 Assert.AreEqual (16, icon2.Width, "Survivor.Width");
612                                 Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
613                                 IconTest.SaveAndCompare ("HICON", icon2, false);
614                         }
615                 }
616
617                 [Test]
618                 public void CreateMultipleIconFromSameHICON ()
619                 {
620                         IntPtr handle;
621                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
622                                 Assert.AreEqual (16, icon.Height, "Original.Height");
623                                 Assert.AreEqual (16, icon.Width, "Original.Width");
624                                 handle = icon.ToBitmap ().GetHicon ();
625                         }
626                         // HICON survives
627                         using (Icon icon2 = Icon.FromHandle (handle)) {
628                                 Assert.AreEqual (16, icon2.Height, "2.Height");
629                                 Assert.AreEqual (16, icon2.Width, "2.Width");
630                                 Assert.AreEqual (handle, icon2.Handle, "2.Handle");
631                                 IconTest.SaveAndCompare ("HICON2", icon2, false);
632                         }
633                         using (Icon icon3 = Icon.FromHandle (handle)) {
634                                 Assert.AreEqual (16, icon3.Height, "3.Height");
635                                 Assert.AreEqual (16, icon3.Width, "3.Width");
636                                 Assert.AreEqual (handle, icon3.Handle, "3.Handle");
637                                 IconTest.SaveAndCompare ("HICON", icon3, false);
638                         }
639                 }
640         }
641 }