2 // System.ComponentModel.PropertyDescriptorCollection test cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (c) 2005 Novell, Inc. (http://www.ximian.com)
11 using System.Collections;
12 using System.ComponentModel;
13 using System.Globalization;
15 using NUnit.Framework;
17 namespace MonoTests.System.ComponentModel
20 public class PropertyDescriptorCollectionTests
25 PropertyDescriptorCollection descriptors = PropertyDescriptorCollection.Empty;
26 AssertReadOnly (descriptors, "Empty");
32 PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
33 new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
34 new MockPropertyDescriptor("b", 2)});
36 Assert.IsNotNull (descriptors.Find ("A", false), "#1");
37 Assert.IsNotNull (descriptors.Find ("b", false), "#2");
38 Assert.IsNull (descriptors.Find ("a", false), "#3");
39 Assert.IsNotNull (descriptors.Find ("a", true), "#4");
43 [ExpectedException (typeof(ArgumentNullException))]
44 public void Find_NullKey ()
46 PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
47 new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
48 new MockPropertyDescriptor("b", 2)});
49 descriptors.Find (null, false);
55 IList list = ((IList) new PropertyDescriptorCollection (null));
57 Assert.AreEqual (0, list.Count, "#1");
59 Assert.IsFalse (list.IsFixedSize, "#2");
61 Assert.IsTrue (list.IsFixedSize, "#2");
63 Assert.IsFalse (list.IsReadOnly, "#3");
64 Assert.IsFalse (list.IsSynchronized, "#4");
65 Assert.IsNull (list.SyncRoot, "#5");
69 public void IList_Add_Null ()
71 IList list = ((IList) new PropertyDescriptorCollection (null));
72 Assert.AreEqual (0, list.Count, "#1");
74 Assert.AreEqual (1, list.Count, "#2");
78 [ExpectedException (typeof (InvalidCastException))]
79 public void IList_Add_NoPropertyDescriptor ()
81 IList list = ((IList) new PropertyDescriptorCollection (null));
86 public void IDictionary ()
88 IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
90 Assert.AreEqual (0, dictionary.Count, "#1");
92 Assert.IsFalse (dictionary.IsFixedSize, "#2");
94 Assert.IsTrue (dictionary.IsFixedSize, "#2");
96 Assert.IsFalse (dictionary.IsReadOnly, "#3");
97 Assert.IsFalse (dictionary.IsSynchronized, "#4");
98 Assert.IsNull (dictionary.SyncRoot, "#5");
103 [ExpectedException (typeof(ArgumentException))]
104 public void IDictionary_Add_Null ()
106 IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
107 dictionary.Add ("whatever", null);
111 [ExpectedException (typeof (ArgumentException))]
112 public void IDictionary_Add_NoPropertyDescriptor ()
114 IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
115 dictionary.Add ("whatever", 5);
119 public void ReadOnly ()
121 PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null, true);
122 AssertReadOnly (descriptors, "ReadOnly");
126 private void AssertReadOnly (PropertyDescriptorCollection descriptors, string testCase)
128 MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor (
129 "Date", DateTime.Now);
132 descriptors.Add (mockPropertyDescr);
133 Assert.Fail (testCase + "#1");
134 } catch (NotSupportedException) {
135 // read-only collection cannot be modified
138 // ensure read-only check if performed before value is checked
140 descriptors.Add (null);
141 Assert.Fail (testCase + "#2");
142 } catch (NotSupportedException) {
143 // read-only collection cannot be modified
147 descriptors.Clear ();
148 Assert.Fail (testCase + "#3");
149 } catch (NotSupportedException) {
150 // read-only collection cannot be modified
154 descriptors.Insert (0, mockPropertyDescr);
155 Assert.Fail (testCase + "#4");
156 } catch (NotSupportedException) {
157 // read-only collection cannot be modified
160 // ensure read-only check if performed before value is checked
162 descriptors.Insert (0, null);
163 Assert.Fail (testCase + "#5");
164 } catch (NotSupportedException) {
165 // read-only collection cannot be modified
169 descriptors.Remove (mockPropertyDescr);
170 Assert.Fail (testCase + "#6");
171 } catch (NotSupportedException) {
172 // read-only collection cannot be modified
175 // ensure read-only check if performed before value is checked
177 descriptors.Remove (null);
178 Assert.Fail (testCase + "#7");
179 } catch (NotSupportedException) {
180 // read-only collection cannot be modified
184 descriptors.RemoveAt (0);
185 Assert.Fail (testCase + "#8");
186 } catch (NotSupportedException) {
187 // read-only collection cannot be modified
190 IList list = (IList) descriptors;
191 Assert.IsTrue (((IList) descriptors).IsReadOnly, testCase + "#9");
193 Assert.IsTrue (((IList) descriptors).IsFixedSize, testCase + "#10");
195 Assert.IsFalse (((IList) descriptors).IsFixedSize, testCase + "#10");
199 list.Add (mockPropertyDescr);
200 Assert.Fail (testCase + "#11");
201 } catch (NotSupportedException) {
202 // read-only collection cannot be modified
205 // ensure read-only check if performed before value is checked
208 Assert.Fail (testCase + "#12");
209 } catch (NotSupportedException) {
210 // read-only collection cannot be modified
215 Assert.Fail (testCase + "#13");
216 } catch (NotSupportedException) {
217 // read-only collection cannot be modified
221 list.Insert (0, mockPropertyDescr);
222 Assert.Fail (testCase + "#14");
223 } catch (NotSupportedException) {
224 // read-only collection cannot be modified
227 // ensure read-only check if performed before value is checked
229 list.Insert (0, null);
230 Assert.Fail (testCase + "#15");
231 } catch (NotSupportedException) {
232 // read-only collection cannot be modified
236 list.Remove (mockPropertyDescr);
237 Assert.Fail (testCase + "#16");
238 } catch (NotSupportedException) {
239 // read-only collection cannot be modified
242 // ensure read-only check if performed before value is checked
245 Assert.Fail (testCase + "#17");
246 } catch (NotSupportedException) {
247 // read-only collection cannot be modified
252 Assert.Fail (testCase + "#18");
253 } catch (NotSupportedException) {
254 // read-only collection cannot be modified
258 list[0] = mockPropertyDescr;
259 Assert.Fail (testCase + "#19");
260 } catch (NotSupportedException) {
261 // read-only collection cannot be modified
264 // ensure read-only check if performed before value is checked
267 Assert.Fail (testCase + "#20");
268 } catch (NotSupportedException) {
269 // read-only collection cannot be modified
272 IDictionary dictionary = (IDictionary) descriptors;
273 Assert.IsTrue (dictionary.IsReadOnly, testCase + "#21");
275 Assert.IsTrue (dictionary.IsFixedSize, testCase + "#22");
277 Assert.IsFalse (dictionary.IsFixedSize, testCase + "#22");
281 dictionary.Add ("test", mockPropertyDescr);
282 Assert.Fail (testCase + "#23");
283 } catch (NotSupportedException) {
284 // read-only collection cannot be modified
287 // value is checked before read-only check
289 dictionary.Add ("test", null);
290 Assert.Fail (testCase + "#24");
291 } catch (ArgumentException) {
292 // read-only collection cannot be modified
297 Assert.Fail (testCase + "#25");
298 } catch (NotSupportedException) {
299 // read-only collection cannot be modified
303 dictionary[0] = mockPropertyDescr;
304 Assert.Fail (testCase + "#26");
305 } catch (NotSupportedException) {
306 // read-only collection cannot be modified
309 // ensure read-only check if performed before value is checked
311 dictionary[0] = null;
312 Assert.Fail (testCase + "#27");
313 } catch (NotSupportedException) {
314 // read-only collection cannot be modified
318 private class MockPropertyDescriptor : PropertyDescriptor
320 private object _value;
322 public MockPropertyDescriptor (string name, object value) : base (name, null)
327 public override bool CanResetValue (object component)
332 public override object GetValue (object component)
337 public override void ResetValue (object component)
342 public override void SetValue (object component, object value)
347 public override bool ShouldSerializeValue (object component)
352 public override Type ComponentType {
354 if (_value != null) {
355 return _value.GetType ();
361 public override bool IsReadOnly {
367 public override Type PropertyType {
369 return ComponentType;