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