PropertyDescriptorTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. public bool SerializeProp3 {
  165. get { return _serializeProp3; }
  166. set { _serializeProp3 = value; }
  167. }
  168. public bool SerializeProp4 {
  169. get { return _serializeProp4; }
  170. set { _serializeProp4 = value; }
  171. }
  172. public bool SerializeProp5 {
  173. get { return _serializeProp5; }
  174. set { _serializeProp5 = value; }
  175. }
  176. public bool SerializeProp6 {
  177. get { return _serializeProp6; }
  178. set { _serializeProp6 = value; }
  179. }
  180. public bool SerializeProp7 {
  181. get { return _serializeProp7; }
  182. set { _serializeProp7 = value; }
  183. }
  184. public bool ShouldSerializeProp3 ()
  185. {
  186. return _serializeProp3;
  187. }
  188. public bool ShouldSerializeProp4 ()
  189. {
  190. return _serializeProp4;
  191. }
  192. public bool ShouldSerializeProp5 ()
  193. {
  194. return _serializeProp5;
  195. }
  196. public bool ShouldSerializeProp6 ()
  197. {
  198. return _serializeProp6;
  199. }
  200. public bool ShouldSerializeProp7 ()
  201. {
  202. return _serializeProp7;
  203. }
  204. private string _prop1;
  205. private string _prop2;
  206. private string _prop3;
  207. private string _prop4;
  208. private string _prop5 = "good";
  209. private string _prop6 = "bad";
  210. private string _prop7;
  211. private bool _serializeProp3;
  212. private bool _serializeProp4;
  213. private bool _serializeProp5;
  214. private bool _serializeProp6;
  215. private bool _serializeProp7;
  216. }
  217. class NoSerializeOrResetProp_test
  218. {
  219. public int Prop {
  220. get { return 5; }
  221. }
  222. }
  223. class CanReset_public_test
  224. {
  225. int prop = 5;
  226. public int Prop {
  227. get { return prop; }
  228. set { prop = value; }
  229. }
  230. public void ResetProp()
  231. {
  232. prop = 10;
  233. }
  234. }
  235. class CanReset_protected_test
  236. {
  237. int prop = 5;
  238. public int Prop {
  239. get { return prop; }
  240. set { prop = value; }
  241. }
  242. protected void ResetProp()
  243. {
  244. prop = 10;
  245. }
  246. }
  247. class CanReset_private_test
  248. {
  249. int prop = 5;
  250. public int Prop {
  251. get { return prop; }
  252. set { prop = value; }
  253. }
  254. private void ResetProp()
  255. {
  256. prop = 10;
  257. }
  258. }
  259. class CanResetNoSetter_test
  260. {
  261. int prop = 5;
  262. public int Prop {
  263. get { return prop; }
  264. }
  265. private void ResetProp()
  266. {
  267. prop = 10;
  268. }
  269. }
  270. class DisplayName_test
  271. {
  272. #if NET_2_0
  273. [DisplayName ("An explicit displayname")]
  274. #endif
  275. public bool Explicit {
  276. get { return false; }
  277. }
  278. public bool Implicit {
  279. get { return false; }
  280. }
  281. }
  282. [Test]
  283. public void MissingTypeConverter ()
  284. {
  285. PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
  286. PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
  287. PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
  288. Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
  289. Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
  290. Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
  291. }
  292. [Test]
  293. public void ShouldSerializeTest_public ()
  294. {
  295. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
  296. ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
  297. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  298. }
  299. [Test]
  300. public void ShouldSerializeTest_protected ()
  301. {
  302. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  303. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  304. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  305. }
  306. [Test]
  307. public void ShouldSerializeTest_private ()
  308. {
  309. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
  310. ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
  311. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  312. }
  313. [Test]
  314. public void ShouldSerializeTest_No_Default ()
  315. {
  316. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
  317. ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
  318. Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
  319. test.Prop = "whatever";
  320. Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
  321. }
  322. [Test]
  323. public void ShouldSerializeTest_Null_Default ()
  324. {
  325. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
  326. ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
  327. Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
  328. test.SerializeProp = true;
  329. Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
  330. test.Prop = "whatever";
  331. Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
  332. test.SerializeProp = false;
  333. Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
  334. }
  335. [Test]
  336. public void ShouldSerializeTest_ReadOnly ()
  337. {
  338. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
  339. typeof (ShouldSerialize_ReadOnly));
  340. ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
  341. PropertyDescriptor prop1PD = properties ["Prop1"];
  342. PropertyDescriptor prop2PD = properties ["Prop2"];
  343. PropertyDescriptor prop3PD = properties ["Prop3"];
  344. PropertyDescriptor prop4PD = properties ["Prop4"];
  345. PropertyDescriptor prop5PD = properties ["Prop5"];
  346. PropertyDescriptor prop6PD = properties ["Prop6"];
  347. PropertyDescriptor prop7PD = properties ["Prop7"];
  348. Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
  349. Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
  350. Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
  351. Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
  352. Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
  353. Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
  354. Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
  355. test.Prop1 = "whatever";
  356. Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
  357. test.Prop2 = "whatever";
  358. Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
  359. test.Prop3 = "whatever";
  360. Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
  361. test.Prop4 = "whatever";
  362. Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
  363. test.Prop7 = "whatever";
  364. Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
  365. test.Prop1 = "ok";
  366. Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
  367. test.SerializeProp3 = true;
  368. Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
  369. test.SerializeProp4 = true;
  370. Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
  371. test.SerializeProp5 = true;
  372. Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
  373. test.SerializeProp6 = true;
  374. Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
  375. test.Prop7 = "good";
  376. Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
  377. test.SerializeProp7 = true;
  378. Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
  379. test.Prop7 = "good";
  380. Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
  381. }
  382. [Test]
  383. public void CanResetTest_public ()
  384. {
  385. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
  386. CanReset_public_test test = new CanReset_public_test ();
  387. Assert.IsTrue (p.CanResetValue (test), "1");
  388. Assert.AreEqual (5, test.Prop, "2");
  389. p.ResetValue (test);
  390. Assert.AreEqual (10, test.Prop, "3");
  391. }
  392. [Test]
  393. public void CanResetTest_protected ()
  394. {
  395. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
  396. CanReset_protected_test test = new CanReset_protected_test ();
  397. Assert.IsTrue (p.CanResetValue (test), "1");
  398. Assert.AreEqual (5, test.Prop, "2");
  399. p.ResetValue (test);
  400. Assert.AreEqual (10, test.Prop, "3");
  401. }
  402. [Test]
  403. public void CanResetTest_private ()
  404. {
  405. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
  406. CanReset_private_test test = new CanReset_private_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 CanResetTestNoSetterTest ()
  414. {
  415. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
  416. CanResetNoSetter_test test = new CanResetNoSetter_test ();
  417. #if NET_2_0
  418. Assert.IsFalse (p.CanResetValue (test), "1");
  419. #else
  420. Assert.IsTrue (p.CanResetValue (test), "1");
  421. #endif
  422. Assert.AreEqual (5, test.Prop, "2");
  423. p.ResetValue (test);
  424. Assert.AreEqual (10, test.Prop, "3");
  425. }
  426. [Test]
  427. public void NoSerializeOrResetPropTest ()
  428. {
  429. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
  430. NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
  431. Assert.IsFalse (p.CanResetValue (test), "1");
  432. Assert.IsFalse (p.ShouldSerializeValue (test), "2");
  433. }
  434. [Test]
  435. public void ShouldSerializeFalseEffectOnCanResetTest ()
  436. {
  437. PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
  438. ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
  439. Assert.IsFalse (p.ShouldSerializeValue (test), "1");
  440. Assert.IsFalse (p.CanResetValue (test), "2");
  441. }
  442. [Test]
  443. public void ReadOnlyPropertyTest ()
  444. {
  445. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
  446. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  447. }
  448. [Test]
  449. public void ReadOnlyAttributeTest ()
  450. {
  451. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
  452. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  453. }
  454. [Test]
  455. public void ReadOnlyConflictingTest ()
  456. {
  457. PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
  458. Assert.IsTrue (col["Prop"].IsReadOnly, "1");
  459. }
  460. [Test] // bug #80292
  461. public void DisplayNameTest ()
  462. {
  463. PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
  464. PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
  465. #if NET_2_0
  466. Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
  467. #else
  468. Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
  469. #endif
  470. Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
  471. }
  472. [Test]
  473. public void GetEditorTest ()
  474. {
  475. PropertyDescriptorCollection col;
  476. PropertyDescriptor pd;
  477. UITypeEditor ed;
  478. col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
  479. pd = col [0];
  480. ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
  481. Assert.IsNotNull (ed, "#01");
  482. Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
  483. }
  484. class GetEditor_test
  485. {
  486. [Editor (typeof (UIEditor), typeof (UITypeEditor))]
  487. public string Property {
  488. get { return "abc"; }
  489. set { }
  490. }
  491. }
  492. class UIEditor : UITypeEditor
  493. {
  494. }
  495. }
  496. }