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