2008-07-15 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.Windows.Forms;
12 using System.Drawing;
13 using System.Reflection;
14 using System.ComponentModel;
15 using NUnit.Framework;
16 using System.Threading;
17 using System.Collections;
18
19
20 namespace MonoTests.System.Windows.Forms
21 {
22
23         [TestFixture]
24         public class ImageListTest
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]
174                 public void Bug409169 ()
175                 {
176                         ImageList imgList = new ImageList ();
177                         ImageList.ImageCollection coll = imgList.Images;
178                         Bitmap img1 = new Bitmap (10, 10);
179                         coll.Add (img1);
180
181                         const int dstOffset = 5;
182                         object[] dst = new object[dstOffset + coll.Count];
183                         ((IList)coll).CopyTo (dst, dstOffset);
184                         
185                         Assert.IsNotNull (dst[dstOffset], "A1");
186                 }
187                 
188                 [TestFixture]
189                 public class ImageListRecreateHandleEventClass
190                 {
191                         static bool eventhandled = false;
192                         public static void RecreateHandle_EventHandler (object sender, EventArgs e)
193                         {
194                                 eventhandled = true;
195                         }
196
197                         [Test]
198                         public void RecreateHandleEvenTest ()
199                         {
200                                 Form myform = new Form ();
201                                 myform.ShowInTaskbar = false;
202                                 Graphics mygraphics = null;
203                                 ImageList myimagelist = new ImageList ();
204                                 Image myImage = Image.FromFile("M.gif");
205                                 myimagelist.Images.Add (myImage);
206                                 myimagelist.ColorDepth = ColorDepth.Depth8Bit;
207                                 myimagelist.ImageSize = new Size (50,50);
208                                 myimagelist.RecreateHandle += new EventHandler (RecreateHandle_EventHandler);
209                                 mygraphics = Graphics.FromHwnd(myform.Handle);
210                                 myimagelist.Draw(mygraphics, new Point(5, 5), 0);
211                                 myimagelist.ImageSize = new Size (100,100);
212                                 Assert.AreEqual (true, eventhandled, "#1");
213                                 eventhandled = false;
214                                 myimagelist.Images.Add (myImage);
215                                 myimagelist.ColorDepth = ColorDepth.Depth32Bit;
216                                 Assert.AreEqual (true, eventhandled, "#2");
217                                 myform.Dispose ();
218                         }
219
220                 }
221         }
222 }