PropertyDescriptorCollectionTests.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. //
  2. // System.ComponentModel.PropertyDescriptorCollection test cases
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (c) 2005 Novell, Inc. (http://www.ximian.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 PropertyDescriptorCollectionTests
  18. {
  19. [Test]
  20. #if TARGET_JVM
  21. [Ignore ("TD BUG ID: 7229")]
  22. #endif
  23. public void Empty ()
  24. {
  25. PropertyDescriptorCollection descriptors = PropertyDescriptorCollection.Empty;
  26. AssertReadOnly (descriptors, "Empty");
  27. }
  28. [Test]
  29. public void Find ()
  30. {
  31. PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
  32. new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
  33. new MockPropertyDescriptor("b", 2)});
  34. Assert.IsNotNull (descriptors.Find ("A", false), "#1");
  35. Assert.IsNotNull (descriptors.Find ("b", false), "#2");
  36. Assert.IsNull (descriptors.Find ("a", false), "#3");
  37. Assert.IsNotNull (descriptors.Find ("a", true), "#4");
  38. }
  39. [Test]
  40. [ExpectedException (typeof(ArgumentNullException))]
  41. public void Find_NullKey ()
  42. {
  43. PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
  44. new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
  45. new MockPropertyDescriptor("b", 2)});
  46. descriptors.Find (null, false);
  47. }
  48. [Test]
  49. public void IList ()
  50. {
  51. IList list = ((IList) new PropertyDescriptorCollection (null));
  52. Assert.AreEqual (0, list.Count, "#1");
  53. #if NET_2_0
  54. Assert.IsFalse (list.IsFixedSize, "#2");
  55. #else
  56. Assert.IsTrue (list.IsFixedSize, "#2");
  57. #endif
  58. Assert.IsFalse (list.IsReadOnly, "#3");
  59. Assert.IsFalse (list.IsSynchronized, "#4");
  60. Assert.IsNull (list.SyncRoot, "#5");
  61. }
  62. [Test]
  63. public void IList_Add_Null ()
  64. {
  65. IList list = ((IList) new PropertyDescriptorCollection (null));
  66. Assert.AreEqual (0, list.Count, "#1");
  67. list.Add (null);
  68. Assert.AreEqual (1, list.Count, "#2");
  69. }
  70. [Test]
  71. [ExpectedException (typeof (InvalidCastException))]
  72. public void IList_Add_NoPropertyDescriptor ()
  73. {
  74. IList list = ((IList) new PropertyDescriptorCollection (null));
  75. list.Add (5);
  76. }
  77. [Test]
  78. public void IDictionary ()
  79. {
  80. IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
  81. Assert.AreEqual (0, dictionary.Count, "#1");
  82. #if NET_2_0
  83. Assert.IsFalse (dictionary.IsFixedSize, "#2");
  84. #else
  85. Assert.IsTrue (dictionary.IsFixedSize, "#2");
  86. #endif
  87. Assert.IsFalse (dictionary.IsReadOnly, "#3");
  88. Assert.IsFalse (dictionary.IsSynchronized, "#4");
  89. Assert.IsNull (dictionary.SyncRoot, "#5");
  90. }
  91. [Test]
  92. [ExpectedException (typeof(ArgumentException))]
  93. public void IDictionary_Add_Null ()
  94. {
  95. IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
  96. dictionary.Add ("whatever", null);
  97. }
  98. [Test]
  99. [ExpectedException (typeof (ArgumentException))]
  100. public void IDictionary_Add_NoPropertyDescriptor ()
  101. {
  102. IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
  103. dictionary.Add ("whatever", 5);
  104. }
  105. #if NET_2_0
  106. public void ReadOnly ()
  107. {
  108. PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null, true);
  109. AssertReadOnly (descriptors, "ReadOnly");
  110. }
  111. #endif
  112. private void AssertReadOnly (PropertyDescriptorCollection descriptors, string testCase)
  113. {
  114. MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor (
  115. "Date", DateTime.Now);
  116. try {
  117. descriptors.Add (mockPropertyDescr);
  118. Assert.Fail (testCase + "#1");
  119. } catch (NotSupportedException) {
  120. // read-only collection cannot be modified
  121. }
  122. // ensure read-only check if performed before value is checked
  123. try {
  124. descriptors.Add (null);
  125. Assert.Fail (testCase + "#2");
  126. } catch (NotSupportedException) {
  127. // read-only collection cannot be modified
  128. }
  129. try {
  130. descriptors.Clear ();
  131. Assert.Fail (testCase + "#3");
  132. } catch (NotSupportedException) {
  133. // read-only collection cannot be modified
  134. }
  135. try {
  136. descriptors.Insert (0, mockPropertyDescr);
  137. Assert.Fail (testCase + "#4");
  138. } catch (NotSupportedException) {
  139. // read-only collection cannot be modified
  140. }
  141. // ensure read-only check if performed before value is checked
  142. try {
  143. descriptors.Insert (0, null);
  144. Assert.Fail (testCase + "#5");
  145. } catch (NotSupportedException) {
  146. // read-only collection cannot be modified
  147. }
  148. try {
  149. descriptors.Remove (mockPropertyDescr);
  150. Assert.Fail (testCase + "#6");
  151. } catch (NotSupportedException) {
  152. // read-only collection cannot be modified
  153. }
  154. // ensure read-only check if performed before value is checked
  155. try {
  156. descriptors.Remove (null);
  157. Assert.Fail (testCase + "#7");
  158. } catch (NotSupportedException) {
  159. // read-only collection cannot be modified
  160. }
  161. try {
  162. descriptors.RemoveAt (0);
  163. Assert.Fail (testCase + "#8");
  164. } catch (NotSupportedException) {
  165. // read-only collection cannot be modified
  166. }
  167. IList list = (IList) descriptors;
  168. Assert.IsTrue (((IList) descriptors).IsReadOnly, testCase + "#9");
  169. #if NET_2_0
  170. Assert.IsTrue (((IList) descriptors).IsFixedSize, testCase + "#10");
  171. #else
  172. Assert.IsFalse (((IList) descriptors).IsFixedSize, testCase + "#10");
  173. #endif
  174. try {
  175. list.Add (mockPropertyDescr);
  176. Assert.Fail (testCase + "#11");
  177. } catch (NotSupportedException) {
  178. // read-only collection cannot be modified
  179. }
  180. // ensure read-only check if performed before value is checked
  181. try {
  182. list.Add (null);
  183. Assert.Fail (testCase + "#12");
  184. } catch (NotSupportedException) {
  185. // read-only collection cannot be modified
  186. }
  187. try {
  188. list.Clear ();
  189. Assert.Fail (testCase + "#13");
  190. } catch (NotSupportedException) {
  191. // read-only collection cannot be modified
  192. }
  193. try {
  194. list.Insert (0, mockPropertyDescr);
  195. Assert.Fail (testCase + "#14");
  196. } catch (NotSupportedException) {
  197. // read-only collection cannot be modified
  198. }
  199. // ensure read-only check if performed before value is checked
  200. try {
  201. list.Insert (0, null);
  202. Assert.Fail (testCase + "#15");
  203. } catch (NotSupportedException) {
  204. // read-only collection cannot be modified
  205. }
  206. try {
  207. list.Remove (mockPropertyDescr);
  208. Assert.Fail (testCase + "#16");
  209. } catch (NotSupportedException) {
  210. // read-only collection cannot be modified
  211. }
  212. // ensure read-only check if performed before value is checked
  213. try {
  214. list.Remove (null);
  215. Assert.Fail (testCase + "#17");
  216. } catch (NotSupportedException) {
  217. // read-only collection cannot be modified
  218. }
  219. try {
  220. list.RemoveAt (0);
  221. Assert.Fail (testCase + "#18");
  222. } catch (NotSupportedException) {
  223. // read-only collection cannot be modified
  224. }
  225. try {
  226. list[0] = mockPropertyDescr;
  227. Assert.Fail (testCase + "#19");
  228. } catch (NotSupportedException) {
  229. // read-only collection cannot be modified
  230. }
  231. // ensure read-only check if performed before value is checked
  232. try {
  233. list[0] = null;
  234. Assert.Fail (testCase + "#20");
  235. } catch (NotSupportedException) {
  236. // read-only collection cannot be modified
  237. }
  238. IDictionary dictionary = (IDictionary) descriptors;
  239. Assert.IsTrue (dictionary.IsReadOnly, testCase + "#21");
  240. #if NET_2_0
  241. Assert.IsTrue (dictionary.IsFixedSize, testCase + "#22");
  242. #else
  243. Assert.IsFalse (dictionary.IsFixedSize, testCase + "#22");
  244. #endif
  245. try {
  246. dictionary.Add ("test", mockPropertyDescr);
  247. Assert.Fail (testCase + "#23");
  248. } catch (NotSupportedException) {
  249. // read-only collection cannot be modified
  250. }
  251. // value is checked before read-only check
  252. try {
  253. dictionary.Add ("test", null);
  254. Assert.Fail (testCase + "#24");
  255. } catch (ArgumentException) {
  256. // read-only collection cannot be modified
  257. }
  258. try {
  259. dictionary.Clear ();
  260. Assert.Fail (testCase + "#25");
  261. } catch (NotSupportedException) {
  262. // read-only collection cannot be modified
  263. }
  264. try {
  265. dictionary[0] = mockPropertyDescr;
  266. Assert.Fail (testCase + "#26");
  267. } catch (NotSupportedException) {
  268. // read-only collection cannot be modified
  269. }
  270. // ensure read-only check if performed before value is checked
  271. try {
  272. dictionary[0] = null;
  273. Assert.Fail (testCase + "#27");
  274. } catch (NotSupportedException) {
  275. // read-only collection cannot be modified
  276. }
  277. }
  278. private class MockPropertyDescriptor : PropertyDescriptor
  279. {
  280. private object _value;
  281. public MockPropertyDescriptor (string name, object value) : base (name, null)
  282. {
  283. _value = value;
  284. }
  285. public override bool CanResetValue (object component)
  286. {
  287. return true;
  288. }
  289. public override object GetValue (object component)
  290. {
  291. return _value;
  292. }
  293. public override void ResetValue (object component)
  294. {
  295. _value = null;
  296. }
  297. public override void SetValue (object component, object value)
  298. {
  299. _value = value;
  300. }
  301. public override bool ShouldSerializeValue (object component)
  302. {
  303. return false;
  304. }
  305. public override Type ComponentType {
  306. get {
  307. if (_value != null) {
  308. return _value.GetType ();
  309. }
  310. return null;
  311. }
  312. }
  313. public override bool IsReadOnly {
  314. get {
  315. return false;
  316. }
  317. }
  318. public override Type PropertyType {
  319. get {
  320. return ComponentType;
  321. }
  322. }
  323. }
  324. }
  325. }