ImageListTest.cs 9.0 KB

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