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 CultureInsensitiveFindTest ()
124 Assert.AreEqual(0, string.Compare ("\u0061\u030a", "\u00e5", true), "#1");
126 PropertyDescriptorCollection col =
127 new PropertyDescriptorCollection (
128 new PropertyDescriptor [] {
129 new MockPropertyDescriptor ("hehe_\u0061\u030a", null),
130 new MockPropertyDescriptor ("heh_\u00e5", null) });
132 Assert.IsNull (col.Find ("heh_\u0061\u030a", false), "#2");
133 Assert.IsNull (col.Find ("hehe_\u00e5", false), "#3");
137 public void ReadOnly ()
139 PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null, true);
140 AssertReadOnly (descriptors, "ReadOnly");
144 private void AssertReadOnly (PropertyDescriptorCollection descriptors, string testCase)
146 MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor (
147 "Date", DateTime.Now);
150 descriptors.Add (mockPropertyDescr);
151 Assert.Fail (testCase + "#1");
152 } catch (NotSupportedException) {
153 // read-only collection cannot be modified
156 // ensure read-only check if performed before value is checked
158 descriptors.Add (null);
159 Assert.Fail (testCase + "#2");
160 } catch (NotSupportedException) {
161 // read-only collection cannot be modified
165 descriptors.Clear ();
166 Assert.Fail (testCase + "#3");
167 } catch (NotSupportedException) {
168 // read-only collection cannot be modified
172 descriptors.Insert (0, mockPropertyDescr);
173 Assert.Fail (testCase + "#4");
174 } catch (NotSupportedException) {
175 // read-only collection cannot be modified
178 // ensure read-only check if performed before value is checked
180 descriptors.Insert (0, null);
181 Assert.Fail (testCase + "#5");
182 } catch (NotSupportedException) {
183 // read-only collection cannot be modified
187 descriptors.Remove (mockPropertyDescr);
188 Assert.Fail (testCase + "#6");
189 } catch (NotSupportedException) {
190 // read-only collection cannot be modified
193 // ensure read-only check if performed before value is checked
195 descriptors.Remove (null);
196 Assert.Fail (testCase + "#7");
197 } catch (NotSupportedException) {
198 // read-only collection cannot be modified
202 descriptors.RemoveAt (0);
203 Assert.Fail (testCase + "#8");
204 } catch (NotSupportedException) {
205 // read-only collection cannot be modified
208 IList list = (IList) descriptors;
209 Assert.IsTrue (((IList) descriptors).IsReadOnly, testCase + "#9");
211 Assert.IsTrue (((IList) descriptors).IsFixedSize, testCase + "#10");
213 Assert.IsFalse (((IList) descriptors).IsFixedSize, testCase + "#10");
217 list.Add (mockPropertyDescr);
218 Assert.Fail (testCase + "#11");
219 } catch (NotSupportedException) {
220 // read-only collection cannot be modified
223 // ensure read-only check if performed before value is checked
226 Assert.Fail (testCase + "#12");
227 } catch (NotSupportedException) {
228 // read-only collection cannot be modified
233 Assert.Fail (testCase + "#13");
234 } catch (NotSupportedException) {
235 // read-only collection cannot be modified
239 list.Insert (0, mockPropertyDescr);
240 Assert.Fail (testCase + "#14");
241 } catch (NotSupportedException) {
242 // read-only collection cannot be modified
245 // ensure read-only check if performed before value is checked
247 list.Insert (0, null);
248 Assert.Fail (testCase + "#15");
249 } catch (NotSupportedException) {
250 // read-only collection cannot be modified
254 list.Remove (mockPropertyDescr);
255 Assert.Fail (testCase + "#16");
256 } catch (NotSupportedException) {
257 // read-only collection cannot be modified
260 // ensure read-only check if performed before value is checked
263 Assert.Fail (testCase + "#17");
264 } catch (NotSupportedException) {
265 // read-only collection cannot be modified
270 Assert.Fail (testCase + "#18");
271 } catch (NotSupportedException) {
272 // read-only collection cannot be modified
276 list[0] = mockPropertyDescr;
277 Assert.Fail (testCase + "#19");
278 } catch (NotSupportedException) {
279 // read-only collection cannot be modified
282 // ensure read-only check if performed before value is checked
285 Assert.Fail (testCase + "#20");
286 } catch (NotSupportedException) {
287 // read-only collection cannot be modified
290 IDictionary dictionary = (IDictionary) descriptors;
291 Assert.IsTrue (dictionary.IsReadOnly, testCase + "#21");
293 Assert.IsTrue (dictionary.IsFixedSize, testCase + "#22");
295 Assert.IsFalse (dictionary.IsFixedSize, testCase + "#22");
299 dictionary.Add ("test", mockPropertyDescr);
300 Assert.Fail (testCase + "#23");
301 } catch (NotSupportedException) {
302 // read-only collection cannot be modified
305 // value is checked before read-only check
307 dictionary.Add ("test", null);
308 Assert.Fail (testCase + "#24");
309 } catch (ArgumentException) {
310 // read-only collection cannot be modified
315 Assert.Fail (testCase + "#25");
316 } catch (NotSupportedException) {
317 // read-only collection cannot be modified
321 dictionary[0] = mockPropertyDescr;
322 Assert.Fail (testCase + "#26");
323 } catch (NotSupportedException) {
324 // read-only collection cannot be modified
327 // ensure read-only check if performed before value is checked
329 dictionary[0] = null;
330 Assert.Fail (testCase + "#27");
331 } catch (NotSupportedException) {
332 // read-only collection cannot be modified
336 private class MockPropertyDescriptor : PropertyDescriptor
338 private object _value;
340 public MockPropertyDescriptor (string name, object value) : base (name, null)
345 public override bool CanResetValue (object component)
350 public override object GetValue (object component)
355 public override void ResetValue (object component)
360 public override void SetValue (object component, object value)
365 public override bool ShouldSerializeValue (object component)
370 public override Type ComponentType {
372 if (_value != null) {
373 return _value.GetType ();
379 public override bool IsReadOnly {
385 public override Type PropertyType {
387 return ComponentType;