* PropertyDescriptorCollection.cs: optimized Find method, removed culture sensitive...
[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                 [Test]
122                 public void CultureInsensitiveFindTest ()
123                 {
124                         Assert.AreEqual(0, string.Compare ("\u0061\u030a", "\u00e5", true), "#1");
125
126                         PropertyDescriptorCollection col =
127                                 new PropertyDescriptorCollection (
128                                 new PropertyDescriptor [] {
129                                         new MockPropertyDescriptor ("hehe_\u0061\u030a", null),
130                                         new MockPropertyDescriptor ("heh_\u00e5", null) });
131
132                         Assert.IsNull (col.Find ("heh_\u0061\u030a", false), "#2");
133                         Assert.IsNull (col.Find ("hehe_\u00e5", false), "#3");
134
135                 }
136 #if NET_2_0
137                 public void ReadOnly ()
138                 {
139                         PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null, true);
140                         AssertReadOnly (descriptors, "ReadOnly");
141                 }
142 #endif
143
144                 private void AssertReadOnly (PropertyDescriptorCollection descriptors, string testCase)
145                 {
146                         MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor (
147                                 "Date", DateTime.Now);
148
149                         try {
150                                 descriptors.Add (mockPropertyDescr);
151                                 Assert.Fail (testCase + "#1");
152                         } catch (NotSupportedException) {
153                                 // read-only collection cannot be modified
154                         }
155
156                         // ensure read-only check if performed before value is checked
157                         try {
158                                 descriptors.Add (null);
159                                 Assert.Fail (testCase + "#2");
160                         } catch (NotSupportedException) {
161                                 // read-only collection cannot be modified
162                         }
163
164                         try {
165                                 descriptors.Clear ();
166                                 Assert.Fail (testCase + "#3");
167                         } catch (NotSupportedException) {
168                                 // read-only collection cannot be modified
169                         }
170
171                         try {
172                                 descriptors.Insert (0, mockPropertyDescr);
173                                 Assert.Fail (testCase + "#4");
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.Insert (0, null);
181                                 Assert.Fail (testCase + "#5");
182                         } catch (NotSupportedException) {
183                                 // read-only collection cannot be modified
184                         }
185
186                         try {
187                                 descriptors.Remove (mockPropertyDescr);
188                                 Assert.Fail (testCase + "#6");
189                         } catch (NotSupportedException) {
190                                 // read-only collection cannot be modified
191                         }
192
193                         // ensure read-only check if performed before value is checked
194                         try {
195                                 descriptors.Remove (null);
196                                 Assert.Fail (testCase + "#7");
197                         } catch (NotSupportedException) {
198                                 // read-only collection cannot be modified
199                         }
200
201                         try {
202                                 descriptors.RemoveAt (0);
203                                 Assert.Fail (testCase + "#8");
204                         } catch (NotSupportedException) {
205                                 // read-only collection cannot be modified
206                         }
207
208                         IList list = (IList) descriptors;
209                         Assert.IsTrue (((IList) descriptors).IsReadOnly, testCase + "#9");
210 #if NET_2_0
211                         Assert.IsTrue (((IList) descriptors).IsFixedSize, testCase + "#10");
212 #else
213                         Assert.IsFalse (((IList) descriptors).IsFixedSize, testCase + "#10");
214 #endif
215
216                         try {
217                                 list.Add (mockPropertyDescr);
218                                 Assert.Fail (testCase + "#11");
219                         } catch (NotSupportedException) {
220                                 // read-only collection cannot be modified
221                         }
222
223                         // ensure read-only check if performed before value is checked
224                         try {
225                                 list.Add (null);
226                                 Assert.Fail (testCase + "#12");
227                         } catch (NotSupportedException) {
228                                 // read-only collection cannot be modified
229                         }
230
231                         try {
232                                 list.Clear ();
233                                 Assert.Fail (testCase + "#13");
234                         } catch (NotSupportedException) {
235                                 // read-only collection cannot be modified
236                         }
237
238                         try {
239                                 list.Insert (0, mockPropertyDescr);
240                                 Assert.Fail (testCase + "#14");
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.Insert (0, null);
248                                 Assert.Fail (testCase + "#15");
249                         } catch (NotSupportedException) {
250                                 // read-only collection cannot be modified
251                         }
252
253                         try {
254                                 list.Remove (mockPropertyDescr);
255                                 Assert.Fail (testCase + "#16");
256                         } catch (NotSupportedException) {
257                                 // read-only collection cannot be modified
258                         }
259
260                         // ensure read-only check if performed before value is checked
261                         try {
262                                 list.Remove (null);
263                                 Assert.Fail (testCase + "#17");
264                         } catch (NotSupportedException) {
265                                 // read-only collection cannot be modified
266                         }
267
268                         try {
269                                 list.RemoveAt (0);
270                                 Assert.Fail (testCase + "#18");
271                         } catch (NotSupportedException) {
272                                 // read-only collection cannot be modified
273                         }
274
275                         try {
276                                 list[0] = mockPropertyDescr;
277                                 Assert.Fail (testCase + "#19");
278                         } catch (NotSupportedException) {
279                                 // read-only collection cannot be modified
280                         }
281
282                         // ensure read-only check if performed before value is checked
283                         try {
284                                 list[0] = null;
285                                 Assert.Fail (testCase + "#20");
286                         } catch (NotSupportedException) {
287                                 // read-only collection cannot be modified
288                         }
289
290                         IDictionary dictionary = (IDictionary) descriptors;
291                         Assert.IsTrue (dictionary.IsReadOnly, testCase + "#21");
292 #if NET_2_0
293                         Assert.IsTrue (dictionary.IsFixedSize, testCase + "#22");
294 #else
295                         Assert.IsFalse (dictionary.IsFixedSize, testCase + "#22");
296 #endif
297
298                         try {
299                                 dictionary.Add ("test", mockPropertyDescr);
300                                 Assert.Fail (testCase + "#23");
301                         } catch (NotSupportedException) {
302                                 // read-only collection cannot be modified
303                         }
304
305                         // value is checked before read-only check
306                         try {
307                                 dictionary.Add ("test", null);
308                                 Assert.Fail (testCase + "#24");
309                         } catch (ArgumentException) {
310                                 // read-only collection cannot be modified
311                         }
312
313                         try {
314                                 dictionary.Clear ();
315                                 Assert.Fail (testCase + "#25");
316                         } catch (NotSupportedException) {
317                                 // read-only collection cannot be modified
318                         }
319
320                         try {
321                                 dictionary[0] = mockPropertyDescr;
322                                 Assert.Fail (testCase + "#26");
323                         } catch (NotSupportedException) {
324                                 // read-only collection cannot be modified
325                         }
326
327                         // ensure read-only check if performed before value is checked
328                         try {
329                                 dictionary[0] = null;
330                                 Assert.Fail (testCase + "#27");
331                         } catch (NotSupportedException) {
332                                 // read-only collection cannot be modified
333                         }
334                 }
335
336                 private class MockPropertyDescriptor : PropertyDescriptor
337                 {
338                         private object _value;
339
340                         public MockPropertyDescriptor (string name, object value) : base (name, null)
341                         {
342                                 _value = value;
343                         }
344
345                         public override bool CanResetValue (object component)
346                         {
347                                 return true;
348                         }
349
350                         public override object GetValue (object component)
351                         {
352                                 return _value;
353                         }
354
355                         public override void ResetValue (object component)
356                         {
357                                 _value = null;
358                         }
359
360                         public override void SetValue (object component, object value)
361                         {
362                                 _value = value;
363                         }
364
365                         public override bool ShouldSerializeValue (object component)
366                         {
367                                 return false;
368                         }
369
370                         public override Type ComponentType {
371                                 get {
372                                         if (_value != null) {
373                                                 return _value.GetType ();
374                                         }
375                                         return null;
376                                 }
377                         }
378
379                         public override bool IsReadOnly {
380                                 get {
381                                         return false;
382                                 }
383                         }
384
385                         public override Type PropertyType {
386                                 get {
387                                         return ComponentType;
388                                 }
389                         }
390                 }
391         }
392 }
393