PropertyDescriptorCollectionTests.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. [Test]
  106. public void CultureInsensitiveFindTest ()
  107. {
  108. Assert.AreEqual(0, string.Compare ("\u0061\u030a", "\u00e5", true), "#1");
  109. PropertyDescriptorCollection col =
  110. new PropertyDescriptorCollection (
  111. new PropertyDescriptor [] {
  112. new MockPropertyDescriptor ("hehe_\u0061\u030a", null),
  113. new MockPropertyDescriptor ("heh_\u00e5", null) });
  114. Assert.IsNull (col.Find ("heh_\u0061\u030a", false), "#2");
  115. Assert.IsNull (col.Find ("hehe_\u00e5", false), "#3");
  116. }
  117. #if NET_2_0
  118. public void ReadOnly ()
  119. {
  120. PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null, true);
  121. AssertReadOnly (descriptors, "ReadOnly");
  122. }
  123. #endif
  124. private void AssertReadOnly (PropertyDescriptorCollection descriptors, string testCase)
  125. {
  126. MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor (
  127. "Date", DateTime.Now);
  128. try {
  129. descriptors.Add (mockPropertyDescr);
  130. Assert.Fail (testCase + "#1");
  131. } catch (NotSupportedException) {
  132. // read-only collection cannot be modified
  133. }
  134. // ensure read-only check if performed before value is checked
  135. try {
  136. descriptors.Add (null);
  137. Assert.Fail (testCase + "#2");
  138. } catch (NotSupportedException) {
  139. // read-only collection cannot be modified
  140. }
  141. try {
  142. descriptors.Clear ();
  143. Assert.Fail (testCase + "#3");
  144. } catch (NotSupportedException) {
  145. // read-only collection cannot be modified
  146. }
  147. try {
  148. descriptors.Insert (0, mockPropertyDescr);
  149. Assert.Fail (testCase + "#4");
  150. } catch (NotSupportedException) {
  151. // read-only collection cannot be modified
  152. }
  153. // ensure read-only check if performed before value is checked
  154. try {
  155. descriptors.Insert (0, null);
  156. Assert.Fail (testCase + "#5");
  157. } catch (NotSupportedException) {
  158. // read-only collection cannot be modified
  159. }
  160. try {
  161. descriptors.Remove (mockPropertyDescr);
  162. Assert.Fail (testCase + "#6");
  163. } catch (NotSupportedException) {
  164. // read-only collection cannot be modified
  165. }
  166. // ensure read-only check if performed before value is checked
  167. try {
  168. descriptors.Remove (null);
  169. Assert.Fail (testCase + "#7");
  170. } catch (NotSupportedException) {
  171. // read-only collection cannot be modified
  172. }
  173. try {
  174. descriptors.RemoveAt (0);
  175. Assert.Fail (testCase + "#8");
  176. } catch (NotSupportedException) {
  177. // read-only collection cannot be modified
  178. }
  179. IList list = (IList) descriptors;
  180. Assert.IsTrue (((IList) descriptors).IsReadOnly, testCase + "#9");
  181. #if NET_2_0
  182. Assert.IsTrue (((IList) descriptors).IsFixedSize, testCase + "#10");
  183. #else
  184. Assert.IsFalse (((IList) descriptors).IsFixedSize, testCase + "#10");
  185. #endif
  186. try {
  187. list.Add (mockPropertyDescr);
  188. Assert.Fail (testCase + "#11");
  189. } catch (NotSupportedException) {
  190. // read-only collection cannot be modified
  191. }
  192. // ensure read-only check if performed before value is checked
  193. try {
  194. list.Add (null);
  195. Assert.Fail (testCase + "#12");
  196. } catch (NotSupportedException) {
  197. // read-only collection cannot be modified
  198. }
  199. try {
  200. list.Clear ();
  201. Assert.Fail (testCase + "#13");
  202. } catch (NotSupportedException) {
  203. // read-only collection cannot be modified
  204. }
  205. try {
  206. list.Insert (0, mockPropertyDescr);
  207. Assert.Fail (testCase + "#14");
  208. } catch (NotSupportedException) {
  209. // read-only collection cannot be modified
  210. }
  211. // ensure read-only check if performed before value is checked
  212. try {
  213. list.Insert (0, null);
  214. Assert.Fail (testCase + "#15");
  215. } catch (NotSupportedException) {
  216. // read-only collection cannot be modified
  217. }
  218. try {
  219. list.Remove (mockPropertyDescr);
  220. Assert.Fail (testCase + "#16");
  221. } catch (NotSupportedException) {
  222. // read-only collection cannot be modified
  223. }
  224. // ensure read-only check if performed before value is checked
  225. try {
  226. list.Remove (null);
  227. Assert.Fail (testCase + "#17");
  228. } catch (NotSupportedException) {
  229. // read-only collection cannot be modified
  230. }
  231. try {
  232. list.RemoveAt (0);
  233. Assert.Fail (testCase + "#18");
  234. } catch (NotSupportedException) {
  235. // read-only collection cannot be modified
  236. }
  237. try {
  238. list[0] = mockPropertyDescr;
  239. Assert.Fail (testCase + "#19");
  240. } catch (NotSupportedException) {
  241. // read-only collection cannot be modified
  242. }
  243. // ensure read-only check if performed before value is checked
  244. try {
  245. list[0] = null;
  246. Assert.Fail (testCase + "#20");
  247. } catch (NotSupportedException) {
  248. // read-only collection cannot be modified
  249. }
  250. IDictionary dictionary = (IDictionary) descriptors;
  251. Assert.IsTrue (dictionary.IsReadOnly, testCase + "#21");
  252. #if NET_2_0
  253. Assert.IsTrue (dictionary.IsFixedSize, testCase + "#22");
  254. #else
  255. Assert.IsFalse (dictionary.IsFixedSize, testCase + "#22");
  256. #endif
  257. try {
  258. dictionary.Add ("test", mockPropertyDescr);
  259. Assert.Fail (testCase + "#23");
  260. } catch (NotSupportedException) {
  261. // read-only collection cannot be modified
  262. }
  263. // value is checked before read-only check
  264. try {
  265. dictionary.Add ("test", null);
  266. Assert.Fail (testCase + "#24");
  267. } catch (ArgumentException) {
  268. // read-only collection cannot be modified
  269. }
  270. try {
  271. dictionary.Clear ();
  272. Assert.Fail (testCase + "#25");
  273. } catch (NotSupportedException) {
  274. // read-only collection cannot be modified
  275. }
  276. try {
  277. dictionary[0] = mockPropertyDescr;
  278. Assert.Fail (testCase + "#26");
  279. } catch (NotSupportedException) {
  280. // read-only collection cannot be modified
  281. }
  282. // ensure read-only check if performed before value is checked
  283. try {
  284. dictionary[0] = null;
  285. Assert.Fail (testCase + "#27");
  286. } catch (NotSupportedException) {
  287. // read-only collection cannot be modified
  288. }
  289. }
  290. private class MockPropertyDescriptor : PropertyDescriptor
  291. {
  292. private object _value;
  293. public MockPropertyDescriptor (string name, object value) : base (name, null)
  294. {
  295. _value = value;
  296. }
  297. public override bool CanResetValue (object component)
  298. {
  299. return true;
  300. }
  301. public override object GetValue (object component)
  302. {
  303. return _value;
  304. }
  305. public override void ResetValue (object component)
  306. {
  307. _value = null;
  308. }
  309. public override void SetValue (object component, object value)
  310. {
  311. _value = value;
  312. }
  313. public override bool ShouldSerializeValue (object component)
  314. {
  315. return false;
  316. }
  317. public override Type ComponentType {
  318. get {
  319. if (_value != null) {
  320. return _value.GetType ();
  321. }
  322. return null;
  323. }
  324. }
  325. public override bool IsReadOnly {
  326. get {
  327. return false;
  328. }
  329. }
  330. public override Type PropertyType {
  331. get {
  332. return ComponentType;
  333. }
  334. }
  335. }
  336. }
  337. }