2008-11-24 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ImageListTest.cs
1 //
2 // ImageImageListTest.cs: Test cases for ImageImageList.
3 //
4 // Author:
5 //   Ritvik Mayank (mritvik@novell.com)
6 //
7 // (C) 2005 Novell, Inc. (http://www.novell.com)
8 //
9
10 using System;
11 using System.Collections;
12 using System.ComponentModel;
13 using System.Drawing;
14 using System.Drawing.Imaging;
15 using System.Reflection;
16 using System.Threading;
17 using System.Windows.Forms;
18
19 using NUnit.Framework;
20
21 namespace MonoTests.System.Windows.Forms
22 {
23         [TestFixture]
24         public class ImageListTest : TestHelper
25         {
26                 [Test]
27                 public void ImageListPropertyTest ()
28                 {
29                         ImageList myimagelist = new ImageList ();
30
31                         // C
32                         Assert.AreEqual (ColorDepth.Depth8Bit, myimagelist.ColorDepth, "#C1");
33                         myimagelist.ColorDepth = ColorDepth.Depth32Bit;
34                         Assert.AreEqual (ColorDepth.Depth32Bit, myimagelist.ColorDepth, "#C2");
35                         Assert.AreEqual (0, myimagelist.Images.Count, "#C3");
36                         // H
37                         Assert.AreEqual (false, myimagelist.HandleCreated, "#H1");
38                         myimagelist.Handle.ToInt32 ();
39                         Assert.AreEqual (true, myimagelist.HandleCreated, "#H2");
40                         Assert.AreEqual ("System.IntPtr", myimagelist.Handle.GetType ().FullName, "#H3");
41                 
42                         // I
43                         Image myImage = Image.FromFile("M.gif");
44                         myimagelist.Images.Add (myImage);
45                         Assert.AreEqual (1, myimagelist.Images.Count, "#I1");
46                         Assert.AreEqual (16, myimagelist.ImageSize.Height, "#I2");
47                         Assert.AreEqual (16, myimagelist.ImageSize.Width, "#I3");
48                         // [MonoTODO ("Add test for ImageStream")]
49                         // [MonoTODO ("Test for Draw Method (visual test)")]
50                                                 
51                         // T
52                         Assert.AreEqual (Color.Transparent, myimagelist.TransparentColor, "#T1");
53                 }
54                 
55                 [Test]
56                 public void ImageListComponentModelTest ()
57                 {
58                         PropertyDescriptor colordepth_prop = TypeDescriptor.GetProperties (typeof (ImageList))["ColorDepth"];
59                         PropertyDescriptor imagesize_prop = TypeDescriptor.GetProperties (typeof (ImageList))["ImageSize"];
60                         PropertyDescriptor transparentcolor_prop = TypeDescriptor.GetProperties (typeof (ImageList))["TransparentColor"];
61
62                         // create a blank ImageList
63                         ImageList il = new ImageList ();
64
65                         // test its defaults
66 #if NET_2_0
67                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "1");
68                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "2");
69                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "3");
70                         Assert.IsTrue (imagesize_prop.CanResetValue (il), "4");
71                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "5");
72                         Assert.IsTrue (transparentcolor_prop.CanResetValue (il), "6");
73
74                         // test what happens when we set the transparent color to LightGray
75                         il.TransparentColor = Color.LightGray;
76                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "7");
77                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "8");
78
79                         // test what happens when we set the depth to something other than the default
80                         il.ColorDepth = ColorDepth.Depth16Bit;
81                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "9");
82                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "10");
83                         // same test for ImageSize
84                         il.ImageSize = new Size (32, 32);
85                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "11");
86                         Assert.IsTrue (imagesize_prop.CanResetValue (il), "12");
87
88                         // create an ImageList containing an image
89                         il = new ImageList ();
90                         il.Images.Add (Image.FromFile ("M.gif"));
91
92                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "13");
93                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "14");
94                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "15");
95                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "16");
96                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "17");
97                         Assert.IsTrue (transparentcolor_prop.CanResetValue (il), "18");
98
99                         // test what happens when we set the transparent color to LightGray
100                         il.TransparentColor = Color.LightGray;
101                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "19");
102                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "20");
103
104                         // test what happens when we set the depth to something other than the default
105                         il.ColorDepth = ColorDepth.Depth16Bit;
106                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "21");
107                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "22");
108
109                         // same test for ImageSize
110                         il.ImageSize = new Size (32, 32);
111                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "23");
112                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "24");
113 #else
114
115                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "1");
116                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "2");
117                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "3");
118                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "4");
119                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "5");
120                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "6");
121
122                         // test what happens when we set the transparent color to LightGray
123                         il.TransparentColor = Color.LightGray;
124                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "7");
125                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "8");
126
127                         // test what happens when we set the depth to something other than the default
128                         il.ColorDepth = ColorDepth.Depth16Bit;
129                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "9");
130                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "10");
131
132                         // same test for ImageSize
133                         il.ImageSize = new Size (32, 32);
134                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "11");
135                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "12");
136
137                         // create an ImageList containing an image
138                         il = new ImageList ();
139                         il.Images.Add (Image.FromFile ("M.gif"));
140
141                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "13");
142                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "14");
143                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "15");
144                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "16");
145                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "17");
146                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "18");
147
148                         // test what happens when we set the transparent color to LightGray
149                         il.TransparentColor = Color.LightGray;
150                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "19");
151                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "20");
152
153                         // test what happens when we set the depth to something other than the default
154                         il.ColorDepth = ColorDepth.Depth16Bit;
155                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "21");
156                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "22");
157
158                         // same test for ImageSize
159                         il.ImageSize = new Size (32, 32);
160                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "23");
161                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "24");
162 #endif
163                 }
164
165                 [Test]
166                 public void ToStringMethodTest () 
167                 {
168                         ImageList myimagelist = new ImageList ();
169                         Assert.AreEqual ("System.Windows.Forms.ImageList Images.Count: 0, ImageSize: {Width=16, Height=16}",
170                                          myimagelist.ToString (), "#T3");
171                 }
172
173                 [Test] // bug #409169
174                 public void ICollection_CopyTo ()
175                 {
176                         ImageList imgList = new ImageList ();
177                         ImageList.ImageCollection coll = imgList.Images;
178
179                         Image gif = Image.FromFile ("M.gif");
180                         coll.Add (gif);
181                         Bitmap bmp = new Bitmap (10, 10);
182                         coll.Add (bmp);
183
184                         const int dstOffset = 5;
185                         object [] dst = new object [dstOffset + coll.Count + 1];
186                         ((ICollection) coll).CopyTo (dst, dstOffset);
187
188                         Assert.IsNull (dst [0], "#1");
189                         Assert.IsNull (dst [1], "#2");
190                         Assert.IsNull (dst [2], "#3");
191                         Assert.IsNull (dst [3], "#4");
192                         Assert.IsNull (dst [4], "#5");
193                         Assert.IsNotNull (dst [5], "#6a");
194                         Assert.IsFalse (object.ReferenceEquals (gif, dst [5]), "#6b");
195                         Assert.AreEqual (typeof (Bitmap), dst [5].GetType (), "#6c");
196                         Assert.IsNotNull (dst [6], "#7a");
197                         Assert.IsFalse (object.ReferenceEquals (bmp, dst [6]), "#7b");
198                         Assert.AreEqual (typeof (Bitmap), dst [6].GetType (), "#7c");
199                         Assert.IsNull (dst [7], "#8");
200
201                         ((Image) dst [5]).Dispose ();
202                         ((Image) dst [6]).Dispose ();
203
204                         coll [0].RotateFlip (RotateFlipType.Rotate90FlipY);
205                         coll [1].RotateFlip (RotateFlipType.Rotate90FlipY);
206                 }
207
208                 [TestFixture]
209                 public class ImageListRecreateHandleEventClass : TestHelper
210                 {
211                         static bool eventhandled = false;
212                         public static void RecreateHandle_EventHandler (object sender, EventArgs e)
213                         {
214                                 eventhandled = true;
215                         }
216
217                         [Test]
218                         public void RecreateHandleEvenTest ()
219                         {
220                                 Form myform = new Form ();
221                                 myform.ShowInTaskbar = false;
222                                 Graphics mygraphics = null;
223                                 ImageList myimagelist = new ImageList ();
224                                 Image myImage = Image.FromFile("M.gif");
225                                 myimagelist.Images.Add (myImage);
226                                 myimagelist.ColorDepth = ColorDepth.Depth8Bit;
227                                 myimagelist.ImageSize = new Size (50,50);
228                                 myimagelist.RecreateHandle += new EventHandler (RecreateHandle_EventHandler);
229                                 mygraphics = Graphics.FromHwnd(myform.Handle);
230                                 myimagelist.Draw(mygraphics, new Point(5, 5), 0);
231                                 myimagelist.ImageSize = new Size (100,100);
232                                 Assert.AreEqual (true, eventhandled, "#1");
233                                 eventhandled = false;
234                                 myimagelist.Images.Add (myImage);
235                                 myimagelist.ColorDepth = ColorDepth.Depth32Bit;
236                                 Assert.AreEqual (true, eventhandled, "#2");
237                                 myform.Dispose ();
238                         }
239                 }
240         }
241 }