ImageListTest.cs 7.8 KB

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