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
24 [Ignore ("TD BUG ID: 7229")]
28 PropertyDescriptorCollection descriptors = PropertyDescriptorCollection.Empty;
29 AssertReadOnly (descriptors, "Empty");
35 PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
36 new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
37 new MockPropertyDescriptor("b", 2)});
39 Assert.IsNotNull (descriptors.Find ("A", false), "#1");
40 Assert.IsNotNull (descriptors.Find ("b", false), "#2");
41 Assert.IsNull (descriptors.Find ("a", false), "#3");
42 Assert.IsNotNull (descriptors.Find ("a", true), "#4");
46 [ExpectedException (typeof(ArgumentNullException))]
47 public void Find_NullKey ()
49 PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
50 new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
51 new MockPropertyDescriptor("b", 2)});
52 descriptors.Find (null, false);
58 IList list = ((IList) new PropertyDescriptorCollection (null));
60 Assert.AreEqual (0, list.Count, "#1");
62 Assert.IsFalse (list.IsFixedSize, "#2");
64 Assert.IsTrue (list.IsFixedSize, "#2");
66 Assert.IsFalse (list.IsReadOnly, "#3");
67 Assert.IsFalse (list.IsSynchronized, "#4");
68 Assert.IsNull (list.SyncRoot, "#5");
72 public void IList_Add_Null ()
74 IList list = ((IList) new PropertyDescriptorCollection (null));
75 Assert.AreEqual (0, list.Count, "#1");
77 Assert.AreEqual (1, list.Count, "#2");
81 [ExpectedException (typeof (InvalidCastException))]
82 public void IList_Add_NoPropertyDescriptor ()
84 IList list = ((IList) new PropertyDescriptorCollection (null));
89 public void IDictionary ()
91 IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
93 Assert.AreEqual (0, dictionary.Count, "#1");
95 Assert.IsFalse (dictionary.IsFixedSize, "#2");
97 Assert.IsTrue (dictionary.IsFixedSize, "#2");
99 Assert.IsFalse (dictionary.IsReadOnly, "#3");
100 Assert.IsFalse (dictionary.IsSynchronized, "#4");
101 Assert.IsNull (dictionary.SyncRoot, "#5");
106 [ExpectedException (typeof(ArgumentException))]
107 public void IDictionary_Add_Null ()
109 IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
110 dictionary.Add ("whatever", null);
114 [ExpectedException (typeof (ArgumentException))]
115 public void IDictionary_Add_NoPropertyDescriptor ()
117 IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
118 dictionary.Add ("whatever", 5);
122 public void ReadOnly ()
124 PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null, true);
125 AssertReadOnly (descriptors, "ReadOnly");
129 private void AssertReadOnly (PropertyDescriptorCollection descriptors, string testCase)
131 MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor (
132 "Date", DateTime.Now);
135 descriptors.Add (mockPropertyDescr);
136 Assert.Fail (testCase + "#1");
137 } catch (NotSupportedException) {
138 // read-only collection cannot be modified
141 // ensure read-only check if performed before value is checked
143 descriptors.Add (null);
144 Assert.Fail (testCase + "#2");
145 } catch (NotSupportedException) {
146 // read-only collection cannot be modified
150 descriptors.Clear ();
151 Assert.Fail (testCase + "#3");
152 } catch (NotSupportedException) {
153 // read-only collection cannot be modified
157 descriptors.Insert (0, mockPropertyDescr);
158 Assert.Fail (testCase + "#4");
159 } catch (NotSupportedException) {
160 // read-only collection cannot be modified
163 // ensure read-only check if performed before value is checked
165 descriptors.Insert (0, null);
166 Assert.Fail (testCase + "#5");
167 } catch (NotSupportedException) {
168 // read-only collection cannot be modified
172 descriptors.Remove (mockPropertyDescr);
173 Assert.Fail (testCase + "#6");
174 } catch (NotSupportedException) {
175 // read-only collection cannot be modified
178 // ensure read-only check if performed before value is checked
180 descriptors.Remove (null);
181 Assert.Fail (testCase + "#7");
182 } catch (NotSupportedException) {
183 // read-only collection cannot be modified
187 descriptors.RemoveAt (0);
188 Assert.Fail (testCase + "#8");
189 } catch (NotSupportedException) {
190 // read-only collection cannot be modified
193 IList list = (IList) descriptors;
194 Assert.IsTrue (((IList) descriptors).IsReadOnly, testCase + "#9");
196 Assert.IsTrue (((IList) descriptors).IsFixedSize, testCase + "#10");
198 Assert.IsFalse (((IList) descriptors).IsFixedSize, testCase + "#10");
202 list.Add (mockPropertyDescr);
203 Assert.Fail (testCase + "#11");
204 } catch (NotSupportedException) {
205 // read-only collection cannot be modified
208 // ensure read-only check if performed before value is checked
211 Assert.Fail (testCase + "#12");
212 } catch (NotSupportedException) {
213 // read-only collection cannot be modified
218 Assert.Fail (testCase + "#13");
219 } catch (NotSupportedException) {
220 // read-only collection cannot be modified
224 list.Insert (0, mockPropertyDescr);
225 Assert.Fail (testCase + "#14");
226 } catch (NotSupportedException) {
227 // read-only collection cannot be modified
230 // ensure read-only check if performed before value is checked
232 list.Insert (0, null);
233 Assert.Fail (testCase + "#15");
234 } catch (NotSupportedException) {
235 // read-only collection cannot be modified
239 list.Remove (mockPropertyDescr);
240 Assert.Fail (testCase + "#16");
241 } catch (NotSupportedException) {
242 // read-only collection cannot be modified
245 // ensure read-only check if performed before value is checked
248 Assert.Fail (testCase + "#17");
249 } catch (NotSupportedException) {
250 // read-only collection cannot be modified
255 Assert.Fail (testCase + "#18");
256 } catch (NotSupportedException) {
257 // read-only collection cannot be modified
261 list[0] = mockPropertyDescr;
262 Assert.Fail (testCase + "#19");
263 } catch (NotSupportedException) {
264 // read-only collection cannot be modified
267 // ensure read-only check if performed before value is checked
270 Assert.Fail (testCase + "#20");
271 } catch (NotSupportedException) {
272 // read-only collection cannot be modified
275 IDictionary dictionary = (IDictionary) descriptors;
276 Assert.IsTrue (dictionary.IsReadOnly, testCase + "#21");
278 Assert.IsTrue (dictionary.IsFixedSize, testCase + "#22");
280 Assert.IsFalse (dictionary.IsFixedSize, testCase + "#22");
284 dictionary.Add ("test", mockPropertyDescr);
285 Assert.Fail (testCase + "#23");
286 } catch (NotSupportedException) {
287 // read-only collection cannot be modified
290 // value is checked before read-only check
292 dictionary.Add ("test", null);
293 Assert.Fail (testCase + "#24");
294 } catch (ArgumentException) {
295 // read-only collection cannot be modified
300 Assert.Fail (testCase + "#25");
301 } catch (NotSupportedException) {
302 // read-only collection cannot be modified
306 dictionary[0] = mockPropertyDescr;
307 Assert.Fail (testCase + "#26");
308 } catch (NotSupportedException) {
309 // read-only collection cannot be modified
312 // ensure read-only check if performed before value is checked
314 dictionary[0] = null;
315 Assert.Fail (testCase + "#27");
316 } catch (NotSupportedException) {
317 // read-only collection cannot be modified
321 private class MockPropertyDescriptor : PropertyDescriptor
323 private object _value;
325 public MockPropertyDescriptor (string name, object value) : base (name, null)
330 public override bool CanResetValue (object component)
335 public override object GetValue (object component)
340 public override void ResetValue (object component)
345 public override void SetValue (object component, object value)
350 public override bool ShouldSerializeValue (object component)
355 public override Type ComponentType {
357 if (_value != null) {
358 return _value.GetType ();
364 public override bool IsReadOnly {
370 public override Type PropertyType {
372 return ComponentType;