PropertyDescriptorTests.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. public 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. class DisplayName_test
  154. {
  155. #if NET_2_0
  156. [DisplayName ("An explicit displayname")]
  157. #endif
  158. public bool Explicit {
  159. get { return false; }
  160. }
  161. public bool Implicit {
  162. get { return false; }
  163. }
  164. }
  165. [Test]
  166. public void MissingTypeConverter ()
  167. {
  168. PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
  169. PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
  170. PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
  171. Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
  172. Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
  173. Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
  174. }
  175. [Test]
  176. public void ShouldSerializeTest_public ()
  177. {
  178. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
  179. ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
  180. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  181. }
  182. [Test]
  183. public void ShouldSerializeTest_protected ()
  184. {
  185. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  186. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  187. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  188. }
  189. [Test]
  190. public void ShouldSerializeTest_private ()
  191. {
  192. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  193. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  194. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  195. }
  196. [Test]
  197. public void CanResetTest_public ()
  198. {
  199. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
  200. CanReset_public_test test = new CanReset_public_test ();
  201. Assert.IsTrue (p.CanResetValue (test), "1");
  202. Assert.AreEqual (5, test.Prop, "2");
  203. p.ResetValue (test);
  204. Assert.AreEqual (10, test.Prop, "3");
  205. }
  206. [Test]
  207. public void CanResetTest_protected ()
  208. {
  209. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
  210. CanReset_protected_test test = new CanReset_protected_test ();
  211. Assert.IsTrue (p.CanResetValue (test), "1");
  212. Assert.AreEqual (5, test.Prop, "2");
  213. p.ResetValue (test);
  214. Assert.AreEqual (10, test.Prop, "3");
  215. }
  216. [Test]
  217. public void CanResetTest_private ()
  218. {
  219. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
  220. CanReset_private_test test = new CanReset_private_test ();
  221. Assert.IsTrue (p.CanResetValue (test), "1");
  222. Assert.AreEqual (5, test.Prop, "2");
  223. p.ResetValue (test);
  224. Assert.AreEqual (10, test.Prop, "3");
  225. }
  226. [Test]
  227. public void CanResetTestNoSetterTest ()
  228. {
  229. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
  230. CanResetNoSetter_test test = new CanResetNoSetter_test ();
  231. #if NET_2_0
  232. Assert.IsFalse (p.CanResetValue (test), "1");
  233. #else
  234. Assert.IsTrue (p.CanResetValue (test), "1");
  235. #endif
  236. Assert.AreEqual (5, test.Prop, "2");
  237. p.ResetValue (test);
  238. Assert.AreEqual (10, test.Prop, "3");
  239. }
  240. [Test]
  241. public void NoSerializeOrResetPropTest ()
  242. {
  243. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
  244. NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
  245. Assert.IsFalse (p.CanResetValue (test), "1");
  246. Assert.IsFalse (p.ShouldSerializeValue (test), "2");
  247. }
  248. [Test]
  249. public void ShouldSerializeFalseEffectOnCanResetTest ()
  250. {
  251. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
  252. ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
  253. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  254. Assert.IsFalse (p.CanResetValue (test), "2");
  255. }
  256. [Test]
  257. public void ReadOnlyPropertyTest ()
  258. {
  259. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
  260. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  261. }
  262. [Test]
  263. public void ReadOnlyAttributeTest ()
  264. {
  265. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
  266. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  267. }
  268. [Test]
  269. public void ReadOnlyConflictingTest ()
  270. {
  271. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
  272. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  273. }
  274. [Test] // bug #80292
  275. public void DisplayNameTest ()
  276. {
  277. PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
  278. PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
  279. #if NET_2_0
  280. Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
  281. #else
  282. Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
  283. #endif
  284. Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
  285. }
  286. }
  287. }