PropertyDescriptorCollectionTests.cs 9.2 KB

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