2006-12-19 Daniel Nauck <dna@mono-project.de>
[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
18
19 namespace MonoTests.System.Windows.Forms
20 {
21
22         [TestFixture]
23         public class ImageListTest
24         {
25                 [Test]
26                 public void ImageListPropertyTest ()
27                 {
28                         ImageList myimagelist = new ImageList ();
29
30                         // C
31                         Assert.AreEqual (ColorDepth.Depth8Bit, myimagelist.ColorDepth, "#C1");
32                         myimagelist.ColorDepth = ColorDepth.Depth32Bit;
33                         Assert.AreEqual (ColorDepth.Depth32Bit, myimagelist.ColorDepth, "#C2");
34                         Assert.AreEqual (0, myimagelist.Images.Count, "#C3");
35                         // H
36                         Assert.AreEqual (false, myimagelist.HandleCreated, "#H1");
37                         myimagelist.Handle.ToInt32 ();
38                         Assert.AreEqual (true, myimagelist.HandleCreated, "#H2");
39                         Assert.AreEqual ("System.IntPtr", myimagelist.Handle.GetType ().FullName, "#H3");
40                 
41                         // I
42                         Image myImage = Image.FromFile("M.gif");
43                         myimagelist.Images.Add (myImage);
44                         Assert.AreEqual (1, myimagelist.Images.Count, "#I1");
45                         Assert.AreEqual (16, myimagelist.ImageSize.Height, "#I2");
46                         Assert.AreEqual (16, myimagelist.ImageSize.Width, "#I3");
47                         // [MonoTODO ("Add test for ImageStream")]
48                         // [MonoTODO ("Test for Draw Method (visual test)")]
49                                                 
50                         // T
51                         Assert.AreEqual (Color.Transparent, myimagelist.TransparentColor, "#T1");
52                 }
53                 
54                 [Test]
55                 public void ImageListComponentModelTest ()
56                 {
57                         PropertyDescriptor colordepth_prop = TypeDescriptor.GetProperties (typeof (ImageList))["ColorDepth"];
58                         PropertyDescriptor imagesize_prop = TypeDescriptor.GetProperties (typeof (ImageList))["ImageSize"];
59                         PropertyDescriptor transparentcolor_prop = TypeDescriptor.GetProperties (typeof (ImageList))["TransparentColor"];
60
61                         // create a blank ImageList
62                         ImageList il = new ImageList ();
63
64                         // test its defaults
65 #if NET_2_0
66                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "1");
67                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "2");
68                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "3");
69                         Assert.IsTrue (imagesize_prop.CanResetValue (il), "4");
70                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "5");
71                         Assert.IsTrue (transparentcolor_prop.CanResetValue (il), "6");
72
73                         // test what happens when we set the transparent color to LightGray
74                         il.TransparentColor = Color.LightGray;
75                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "7");
76                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "8");
77
78                         // test what happens when we set the depth to something other than the default
79                         il.ColorDepth = ColorDepth.Depth16Bit;
80                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "9");
81                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "10");
82                         // same test for ImageSize
83                         il.ImageSize = new Size (32, 32);
84                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "11");
85                         Assert.IsTrue (imagesize_prop.CanResetValue (il), "12");
86
87                         // create an ImageList containing an image
88                         il = new ImageList ();
89                         il.Images.Add (Image.FromFile ("M.gif"));
90
91                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "13");
92                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "14");
93                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "15");
94                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "16");
95                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "17");
96                         Assert.IsTrue (transparentcolor_prop.CanResetValue (il), "18");
97
98                         // test what happens when we set the transparent color to LightGray
99                         il.TransparentColor = Color.LightGray;
100                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "19");
101                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "20");
102
103                         // test what happens when we set the depth to something other than the default
104                         il.ColorDepth = ColorDepth.Depth16Bit;
105                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "21");
106                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "22");
107
108                         // same test for ImageSize
109                         il.ImageSize = new Size (32, 32);
110                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "23");
111                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "24");
112 #else
113
114                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "1");
115                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "2");
116                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "3");
117                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "4");
118                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "5");
119                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "6");
120
121                         // test what happens when we set the transparent color to LightGray
122                         il.TransparentColor = Color.LightGray;
123                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "7");
124                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "8");
125
126                         // test what happens when we set the depth to something other than the default
127                         il.ColorDepth = ColorDepth.Depth16Bit;
128                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "9");
129                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "10");
130
131                         // same test for ImageSize
132                         il.ImageSize = new Size (32, 32);
133                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "11");
134                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "12");
135
136                         // create an ImageList containing an image
137                         il = new ImageList ();
138                         il.Images.Add (Image.FromFile ("M.gif"));
139
140                         Assert.IsFalse (colordepth_prop.ShouldSerializeValue (il), "13");
141                         Assert.IsFalse (colordepth_prop.CanResetValue (il), "14");
142                         Assert.IsFalse (imagesize_prop.ShouldSerializeValue (il), "15");
143                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "16");
144                         Assert.IsTrue (transparentcolor_prop.ShouldSerializeValue (il), "17");
145                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "18");
146
147                         // test what happens when we set the transparent color to LightGray
148                         il.TransparentColor = Color.LightGray;
149                         Assert.IsFalse (transparentcolor_prop.ShouldSerializeValue (il), "19");
150                         Assert.IsFalse (transparentcolor_prop.CanResetValue (il), "20");
151
152                         // test what happens when we set the depth to something other than the default
153                         il.ColorDepth = ColorDepth.Depth16Bit;
154                         Assert.IsTrue (colordepth_prop.ShouldSerializeValue (il), "21");
155                         Assert.IsTrue (colordepth_prop.CanResetValue (il), "22");
156
157                         // same test for ImageSize
158                         il.ImageSize = new Size (32, 32);
159                         Assert.IsTrue (imagesize_prop.ShouldSerializeValue (il), "23");
160                         Assert.IsFalse (imagesize_prop.CanResetValue (il), "24");
161 #endif
162                 }
163
164                 [Test]
165                 public void ToStringMethodTest () 
166                 {
167                         ImageList myimagelist = new ImageList ();
168                         Assert.AreEqual ("System.Windows.Forms.ImageList Images.Count: 0, ImageSize: {Width=16, Height=16}",
169                                          myimagelist.ToString (), "#T3");
170                 }
171                 
172                 [TestFixture]
173                 public class ImageListRecreateHandleEventClass
174                 {
175                         static bool eventhandled = false;
176                         public static void RecreateHandle_EventHandler (object sender, EventArgs e)
177                         {
178                                 eventhandled = true;
179                         }
180
181                         [Test]
182                         public void RecreateHandleEvenTest ()
183                         {
184                                 Form myform = new Form ();
185                                 myform.ShowInTaskbar = false;
186                                 Graphics mygraphics = null;
187                                 ImageList myimagelist = new ImageList ();
188                                 Image myImage = Image.FromFile("M.gif");
189                                 myimagelist.Images.Add (myImage);
190                                 myimagelist.ColorDepth = ColorDepth.Depth8Bit;
191                                 myimagelist.ImageSize = new Size (50,50);
192                                 myimagelist.RecreateHandle += new EventHandler (RecreateHandle_EventHandler);
193                                 mygraphics = Graphics.FromHwnd(myform.Handle);
194                                 myimagelist.Draw(mygraphics, new Point(5, 5), 0);
195                                 myimagelist.ImageSize = new Size (100,100);
196                                 Assert.AreEqual (true, eventhandled, "#1");
197                                 eventhandled = false;
198                                 myimagelist.Images.Add (myImage);
199                                 myimagelist.ColorDepth = ColorDepth.Depth32Bit;
200                                 Assert.AreEqual (true, eventhandled, "#2");
201                                 myform.Dispose ();
202                         }
203
204                 }
205         }
206 }