PropertyDescriptorTests.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // System.ComponentModel.PropertyDescriptor test cases
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (c) 2006 Novell, Inc. (http://www.novell.com/)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Globalization;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.ComponentModel
  15. {
  16. [TestFixture]
  17. public class PropertyDescriptorTests
  18. {
  19. class MissingConverterType_test
  20. {
  21. class NestedClass { }
  22. [TypeConverter ("missing-type-name")]
  23. public NestedClass Prop {
  24. get { return null; }
  25. }
  26. [TypeConverter ("missing-type-name")]
  27. public int IntProp {
  28. get { return 5; }
  29. }
  30. [TypeConverter ("missing-type-name")]
  31. public string StringProp {
  32. get { return ""; }
  33. }
  34. }
  35. class ReadOnlyProperty_test
  36. {
  37. public int Prop {
  38. get { return 5; }
  39. }
  40. }
  41. class ReadOnlyAttribute_test
  42. {
  43. [ReadOnly (true)]
  44. public int Prop {
  45. get { return 5; }
  46. set { }
  47. }
  48. }
  49. class ConflictingReadOnly_test
  50. {
  51. [ReadOnly (false)]
  52. public int Prop {
  53. get { return 5; }
  54. }
  55. }
  56. class ShouldSerialize_public_test
  57. {
  58. public int Prop {
  59. get { return 5; }
  60. }
  61. public bool ShouldSerializeProp()
  62. {
  63. return false;
  64. }
  65. }
  66. class ShouldSerialize_protected_test
  67. {
  68. public int Prop {
  69. get { return 5; }
  70. }
  71. protected bool ShouldSerializeProp()
  72. {
  73. return false;
  74. }
  75. }
  76. class ShouldSerialize_private_test
  77. {
  78. public int Prop {
  79. get { return 5; }
  80. }
  81. private bool ShouldSerializeProp()
  82. {
  83. return false;
  84. }
  85. }
  86. class ShouldSerializeFalseEffectOnCanReset_test
  87. {
  88. public int Prop {
  89. get { return 5; }
  90. set { }
  91. }
  92. public bool ShouldSerializeProp()
  93. {
  94. return false;
  95. }
  96. public void ResetProp()
  97. {
  98. }
  99. }
  100. class NoSerializeOrResetProp_test
  101. {
  102. public int Prop {
  103. get { return 5; }
  104. }
  105. }
  106. class CanReset_public_test
  107. {
  108. int prop = 5;
  109. public int Prop {
  110. get { return prop; }
  111. set { prop = value; }
  112. }
  113. public void ResetProp()
  114. {
  115. prop = 10;
  116. }
  117. }
  118. class CanReset_protected_test
  119. {
  120. int prop = 5;
  121. public int Prop {
  122. get { return prop; }
  123. set { prop = value; }
  124. }
  125. protected void ResetProp()
  126. {
  127. prop = 10;
  128. }
  129. }
  130. class CanReset_private_test
  131. {
  132. int prop = 5;
  133. public int Prop {
  134. get { return prop; }
  135. set { prop = value; }
  136. }
  137. private void ResetProp()
  138. {
  139. prop = 10;
  140. }
  141. }
  142. class CanResetNoSetter_test
  143. {
  144. int prop = 5;
  145. public int Prop {
  146. get { return prop; }
  147. }
  148. private void ResetProp()
  149. {
  150. prop = 10;
  151. }
  152. }
  153. [Test]
  154. public void MissingTypeConverter ()
  155. {
  156. PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
  157. PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
  158. PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
  159. Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
  160. Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
  161. Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
  162. }
  163. [Test]
  164. public void ShouldSerializeTest_public ()
  165. {
  166. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
  167. ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
  168. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  169. }
  170. [Test]
  171. public void ShouldSerializeTest_protected ()
  172. {
  173. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  174. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  175. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  176. }
  177. [Test]
  178. public void ShouldSerializeTest_private ()
  179. {
  180. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  181. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  182. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  183. }
  184. [Test]
  185. public void CanResetTest_public ()
  186. {
  187. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
  188. CanReset_public_test test = new CanReset_public_test ();
  189. Assert.IsTrue (p.CanResetValue (test), "1");
  190. Assert.AreEqual (5, test.Prop, "2");
  191. p.ResetValue (test);
  192. Assert.AreEqual (10, test.Prop, "3");
  193. }
  194. [Test]
  195. public void CanResetTest_protected ()
  196. {
  197. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
  198. CanReset_protected_test test = new CanReset_protected_test ();
  199. Assert.IsTrue (p.CanResetValue (test), "1");
  200. Assert.AreEqual (5, test.Prop, "2");
  201. p.ResetValue (test);
  202. Assert.AreEqual (10, test.Prop, "3");
  203. }
  204. [Test]
  205. public void CanResetTest_private ()
  206. {
  207. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
  208. CanReset_private_test test = new CanReset_private_test ();
  209. Assert.IsTrue (p.CanResetValue (test), "1");
  210. Assert.AreEqual (5, test.Prop, "2");
  211. p.ResetValue (test);
  212. Assert.AreEqual (10, test.Prop, "3");
  213. }
  214. [Test]
  215. public void CanResetTestNoSetterTest ()
  216. {
  217. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
  218. CanResetNoSetter_test test = new CanResetNoSetter_test ();
  219. #if NET_2_0
  220. Assert.IsFalse (p.CanResetValue (test), "1");
  221. #else
  222. Assert.IsTrue (p.CanResetValue (test), "1");
  223. #endif
  224. Assert.AreEqual (5, test.Prop, "2");
  225. p.ResetValue (test);
  226. Assert.AreEqual (10, test.Prop, "3");
  227. }
  228. [Test]
  229. public void NoSerializeOrResetPropTest ()
  230. {
  231. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
  232. NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
  233. Assert.IsFalse (p.CanResetValue (test), "1");
  234. Assert.IsFalse (p.ShouldSerializeValue (test), "2");
  235. }
  236. [Test]
  237. public void ShouldSerializeFalseEffectOnCanResetTest ()
  238. {
  239. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
  240. ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
  241. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  242. Assert.IsFalse (p.CanResetValue (test), "2");
  243. }
  244. [Test]
  245. public void ReadOnlyPropertyTest ()
  246. {
  247. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
  248. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  249. }
  250. [Test]
  251. public void ReadOnlyAttributeTest ()
  252. {
  253. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
  254. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  255. }
  256. [Test]
  257. public void ReadOnlyConflictingTest ()
  258. {
  259. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
  260. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  261. }
  262. }
  263. }