PropertyDescriptorTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 System.Drawing.Design;
  14. using System.ComponentModel.Design;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.ComponentModel
  17. {
  18. [TestFixture]
  19. public class PropertyDescriptorTests
  20. {
  21. class MissingConverterType_test
  22. {
  23. public class NestedClass { }
  24. [TypeConverter ("missing-type-name")]
  25. public NestedClass Prop {
  26. get { return null; }
  27. }
  28. [TypeConverter ("missing-type-name")]
  29. public int IntProp {
  30. get { return 5; }
  31. }
  32. [TypeConverter ("missing-type-name")]
  33. public string StringProp {
  34. get { return ""; }
  35. }
  36. }
  37. class ReadOnlyProperty_test
  38. {
  39. public int Prop {
  40. get { return 5; }
  41. }
  42. }
  43. class ReadOnlyAttribute_test
  44. {
  45. [ReadOnly (true)]
  46. public int Prop {
  47. get { return 5; }
  48. set { }
  49. }
  50. }
  51. class ConflictingReadOnly_test
  52. {
  53. [ReadOnly (false)]
  54. public int Prop {
  55. get { return 5; }
  56. }
  57. }
  58. class ShouldSerialize_public_test
  59. {
  60. public int Prop {
  61. get { return 5; }
  62. }
  63. public bool ShouldSerializeProp()
  64. {
  65. return false;
  66. }
  67. }
  68. class ShouldSerialize_protected_test
  69. {
  70. public int Prop {
  71. get { return 5; }
  72. }
  73. protected bool ShouldSerializeProp()
  74. {
  75. return false;
  76. }
  77. }
  78. class ShouldSerialize_private_test
  79. {
  80. public int Prop {
  81. get { return 5; }
  82. }
  83. private bool ShouldSerializeProp()
  84. {
  85. return false;
  86. }
  87. }
  88. class ShouldSerializeFalseEffectOnCanReset_test
  89. {
  90. public int Prop {
  91. get { return 5; }
  92. set { }
  93. }
  94. public bool ShouldSerializeProp()
  95. {
  96. return false;
  97. }
  98. public void ResetProp()
  99. {
  100. }
  101. }
  102. class ShouldSerialize_Null_Default
  103. {
  104. [DefaultValue (null)]
  105. public string Prop {
  106. get { return _prop; }
  107. set { _prop = value; }
  108. }
  109. public bool SerializeProp {
  110. get { return _serializeProp; }
  111. set { _serializeProp = value; }
  112. }
  113. public bool ShouldSerializeProp ()
  114. {
  115. return _serializeProp;
  116. }
  117. private string _prop;
  118. private bool _serializeProp;
  119. }
  120. class ShouldSerialize_No_Default
  121. {
  122. public string Prop {
  123. get { return _prop; }
  124. set { _prop = value; }
  125. }
  126. private string _prop;
  127. }
  128. class ShouldSerialize_ReadOnly
  129. {
  130. [ReadOnly (true)]
  131. [DefaultValue ("ok")]
  132. public string Prop1 {
  133. get { return _prop1; }
  134. set { _prop1 = value; }
  135. }
  136. [ReadOnly (false)]
  137. public string Prop2 {
  138. get { return _prop2; }
  139. set { _prop2 = value; }
  140. }
  141. [ReadOnly (true)]
  142. public string Prop3 {
  143. get { return _prop3; }
  144. set { _prop3 = value; }
  145. }
  146. [ReadOnly (false)]
  147. public string Prop4 {
  148. get { return _prop4; }
  149. set { _prop4 = value; }
  150. }
  151. public string Prop5 {
  152. get { return _prop5; }
  153. }
  154. [DefaultValue ("bad")]
  155. public string Prop6 {
  156. get { return _prop6; }
  157. }
  158. [ReadOnly (true)]
  159. [DefaultValue ("good")]
  160. public string Prop7 {
  161. get { return _prop7; }
  162. set { _prop7 = value; }
  163. }
  164. [ReadOnly (true)]
  165. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  166. public string Prop8 {
  167. get { return null; }
  168. }
  169. [ReadOnly (true)]
  170. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  171. public string Prop9 {
  172. get { return null; }
  173. }
  174. public bool SerializeProp3 {
  175. get { return _serializeProp3; }
  176. set { _serializeProp3 = value; }
  177. }
  178. public bool SerializeProp4 {
  179. get { return _serializeProp4; }
  180. set { _serializeProp4 = value; }
  181. }
  182. public bool SerializeProp5 {
  183. get { return _serializeProp5; }
  184. set { _serializeProp5 = value; }
  185. }
  186. public bool SerializeProp6 {
  187. get { return _serializeProp6; }
  188. set { _serializeProp6 = value; }
  189. }
  190. public bool SerializeProp7 {
  191. get { return _serializeProp7; }
  192. set { _serializeProp7 = value; }
  193. }
  194. public bool ShouldSerializeProp3 ()
  195. {
  196. return _serializeProp3;
  197. }
  198. public bool ShouldSerializeProp4 ()
  199. {
  200. return _serializeProp4;
  201. }
  202. public bool ShouldSerializeProp5 ()
  203. {
  204. return _serializeProp5;
  205. }
  206. public bool ShouldSerializeProp6 ()
  207. {
  208. return _serializeProp6;
  209. }
  210. public bool ShouldSerializeProp7 ()
  211. {
  212. return _serializeProp7;
  213. }
  214. public bool ShouldSerializeProp8 ()
  215. {
  216. return false;
  217. }
  218. private string _prop1;
  219. private string _prop2;
  220. private string _prop3;
  221. private string _prop4;
  222. private string _prop5 = "good";
  223. private string _prop6 = "bad";
  224. private string _prop7;
  225. private bool _serializeProp3;
  226. private bool _serializeProp4;
  227. private bool _serializeProp5;
  228. private bool _serializeProp6;
  229. private bool _serializeProp7;
  230. }
  231. class NoSerializeOrResetProp_test
  232. {
  233. public int Prop {
  234. get { return 5; }
  235. }
  236. }
  237. class CanReset_public_test
  238. {
  239. int prop = 5;
  240. public int Prop {
  241. get { return prop; }
  242. set { prop = value; }
  243. }
  244. public void ResetProp()
  245. {
  246. prop = 10;
  247. }
  248. }
  249. class CanReset_protected_test
  250. {
  251. int prop = 5;
  252. public int Prop {
  253. get { return prop; }
  254. set { prop = value; }
  255. }
  256. protected void ResetProp()
  257. {
  258. prop = 10;
  259. }
  260. }
  261. class CanReset_private_test
  262. {
  263. int prop = 5;
  264. public int Prop {
  265. get { return prop; }
  266. set { prop = value; }
  267. }
  268. private void ResetProp()
  269. {
  270. prop = 10;
  271. }
  272. }
  273. class CanResetNoSetter_test
  274. {
  275. int prop = 5;
  276. public int Prop {
  277. get { return prop; }
  278. }
  279. private void ResetProp()
  280. {
  281. prop = 10;
  282. }
  283. }
  284. class DisplayName_test
  285. {
  286. #if NET_2_0
  287. [DisplayName ("An explicit displayname")]
  288. #endif
  289. public bool Explicit {
  290. get { return false; }
  291. }
  292. public bool Implicit {
  293. get { return false; }
  294. }
  295. }
  296. [Test]
  297. public void MissingTypeConverter ()
  298. {
  299. PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
  300. PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
  301. PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
  302. Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
  303. Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
  304. Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
  305. }
  306. [Test]
  307. public void ShouldSerializeTest_public ()
  308. {
  309. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
  310. ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
  311. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  312. }
  313. [Test]
  314. public void ShouldSerializeTest_protected ()
  315. {
  316. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  317. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  318. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  319. }
  320. [Test]
  321. public void ShouldSerializeTest_private ()
  322. {
  323. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  324. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  325. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  326. }
  327. [Test]
  328. public void ShouldSerializeTest_No_Default ()
  329. {
  330. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
  331. ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
  332. Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
  333. test.Prop = "whatever";
  334. Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
  335. }
  336. [Test]
  337. public void ShouldSerializeTest_Null_Default ()
  338. {
  339. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
  340. ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
  341. Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
  342. test.SerializeProp = true;
  343. Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
  344. test.Prop = "whatever";
  345. Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
  346. test.SerializeProp = false;
  347. Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
  348. }
  349. [Test]
  350. public void ShouldSerializeTest_ReadOnly ()
  351. {
  352. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
  353. typeof (ShouldSerialize_ReadOnly));
  354. ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
  355. PropertyDescriptor prop1PD = properties ["Prop1"];
  356. PropertyDescriptor prop2PD = properties ["Prop2"];
  357. PropertyDescriptor prop3PD = properties ["Prop3"];
  358. PropertyDescriptor prop4PD = properties ["Prop4"];
  359. PropertyDescriptor prop5PD = properties ["Prop5"];
  360. PropertyDescriptor prop6PD = properties ["Prop6"];
  361. PropertyDescriptor prop7PD = properties ["Prop7"];
  362. PropertyDescriptor prop8PD = properties ["Prop8"];
  363. PropertyDescriptor prop9PD = properties ["Prop9"];
  364. Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
  365. Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
  366. Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
  367. Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
  368. Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
  369. Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
  370. Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
  371. test.Prop1 = "whatever";
  372. Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
  373. test.Prop2 = "whatever";
  374. Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
  375. test.Prop3 = "whatever";
  376. Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
  377. test.Prop4 = "whatever";
  378. Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
  379. test.Prop7 = "whatever";
  380. Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
  381. test.Prop1 = "ok";
  382. Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
  383. test.SerializeProp3 = true;
  384. Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
  385. test.SerializeProp4 = true;
  386. Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
  387. test.SerializeProp5 = true;
  388. Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
  389. test.SerializeProp6 = true;
  390. Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
  391. test.Prop7 = "good";
  392. Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
  393. test.SerializeProp7 = true;
  394. Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
  395. test.Prop7 = "good";
  396. Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
  397. // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
  398. Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
  399. // has DesignerSerializationVisibility.Content, no ShouldSerialize
  400. Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
  401. }
  402. [Test]
  403. public void CanResetTest_public ()
  404. {
  405. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
  406. CanReset_public_test test = new CanReset_public_test ();
  407. Assert.IsTrue (p.CanResetValue (test), "1");
  408. Assert.AreEqual (5, test.Prop, "2");
  409. p.ResetValue (test);
  410. Assert.AreEqual (10, test.Prop, "3");
  411. }
  412. [Test]
  413. public void CanResetTest_protected ()
  414. {
  415. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
  416. CanReset_protected_test test = new CanReset_protected_test ();
  417. Assert.IsTrue (p.CanResetValue (test), "1");
  418. Assert.AreEqual (5, test.Prop, "2");
  419. p.ResetValue (test);
  420. Assert.AreEqual (10, test.Prop, "3");
  421. }
  422. [Test]
  423. public void CanResetTest_private ()
  424. {
  425. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
  426. CanReset_private_test test = new CanReset_private_test ();
  427. Assert.IsTrue (p.CanResetValue (test), "1");
  428. Assert.AreEqual (5, test.Prop, "2");
  429. p.ResetValue (test);
  430. Assert.AreEqual (10, test.Prop, "3");
  431. }
  432. [Test]
  433. public void CanResetTestNoSetterTest ()
  434. {
  435. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
  436. CanResetNoSetter_test test = new CanResetNoSetter_test ();
  437. #if NET_2_0
  438. Assert.IsFalse (p.CanResetValue (test), "1");
  439. #else
  440. Assert.IsTrue (p.CanResetValue (test), "1");
  441. #endif
  442. Assert.AreEqual (5, test.Prop, "2");
  443. p.ResetValue (test);
  444. Assert.AreEqual (10, test.Prop, "3");
  445. }
  446. [Test]
  447. public void NoSerializeOrResetPropTest ()
  448. {
  449. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
  450. NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
  451. Assert.IsFalse (p.CanResetValue (test), "1");
  452. Assert.IsFalse (p.ShouldSerializeValue (test), "2");
  453. }
  454. [Test]
  455. public void ShouldSerializeFalseEffectOnCanResetTest ()
  456. {
  457. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
  458. ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
  459. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  460. Assert.IsFalse (p.CanResetValue (test), "2");
  461. }
  462. [Test]
  463. public void ReadOnlyPropertyTest ()
  464. {
  465. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
  466. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  467. }
  468. [Test]
  469. public void ReadOnlyAttributeTest ()
  470. {
  471. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
  472. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  473. }
  474. [Test]
  475. public void ReadOnlyConflictingTest ()
  476. {
  477. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
  478. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  479. }
  480. [Test] // bug #80292
  481. public void DisplayNameTest ()
  482. {
  483. PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
  484. PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
  485. #if NET_2_0
  486. Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
  487. #else
  488. Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
  489. #endif
  490. Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
  491. }
  492. [Test]
  493. public void GetEditorTest ()
  494. {
  495. PropertyDescriptorCollection col;
  496. PropertyDescriptor pd;
  497. UITypeEditor ed;
  498. col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
  499. pd = col [0];
  500. ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
  501. Assert.IsNotNull (ed, "#01");
  502. Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
  503. }
  504. class GetEditor_test
  505. {
  506. [Editor (typeof (UIEditor), typeof (UITypeEditor))]
  507. public string Property {
  508. get { return "abc"; }
  509. set { }
  510. }
  511. }
  512. class UIEditor : UITypeEditor
  513. {
  514. }
  515. }
  516. }