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