2006-11-07 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System / Test / System.ComponentModel / TypeDescriptorTests.cs
1 //
2 // System.ComponentModel.TypeDescriptorTests test cases
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (c) 2004 Novell, Inc. (http://www.ximian.com)
8 //
9 using NUnit.Framework;
10 using System;
11 using System.Collections;
12 using System.ComponentModel;
13 using System.ComponentModel.Design;
14 using System.Globalization;
15
16 namespace MonoTests.System.ComponentModel
17 {
18         class MyDesigner: IDesigner
19         {
20                 public MyDesigner()
21                 {
22                 }
23
24                 public IComponent Component {get{return null; }}
25
26                 public DesignerVerbCollection Verbs {get{return null; }}
27
28                 public void DoDefaultAction () { }
29
30                 public void Initialize (IComponent component) { }
31
32                 public void Dispose () { }
33         }
34
35         class MyOtherDesigner: IDesigner
36         {
37                 public MyOtherDesigner()
38                 {
39                 }
40
41                 public IComponent Component {get {return null; } }
42                 public DesignerVerbCollection Verbs { get {return null; } }
43                 public void DoDefaultAction () { }
44                 public void Initialize (IComponent component) { }
45                 public void Dispose () { }
46         }
47         
48         class MySite: ISite
49         { 
50                 public IComponent Component { get {  return null; } }
51
52                 public IContainer Container { get {  return null; } }
53
54                 public bool DesignMode { get {  return true; } }
55
56                 public string Name { get { return "TestName"; } set { } }       
57
58                 public object GetService (Type t)
59                 {
60                         if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
61                         return null;
62                 }
63         }
64         
65         class MyFilter: ITypeDescriptorFilterService
66         {
67                 public bool FilterAttributes (IComponent component,IDictionary attributes)
68                 {
69                         Attribute ea = new DefaultEventAttribute ("AnEvent");
70                         attributes [ea.TypeId] = ea;
71                         ea = new DefaultPropertyAttribute ("TestProperty");
72                         attributes [ea.TypeId] = ea;
73                         ea = new EditorAttribute ();
74                         attributes [ea.TypeId] = ea;
75                         return true;
76                 }
77                 
78                 public bool FilterEvents (IComponent component, IDictionary events)
79                 {
80                         events.Remove ("AnEvent");
81                         return true;
82                 }
83                 
84                 public bool FilterProperties (IComponent component, IDictionary properties)
85                 {
86                         properties.Remove ("TestProperty");
87                         return true;
88                 }
89         }
90
91         class AnotherSite: ISite
92         { 
93                 public IComponent Component { get {  return null; } }
94
95                 public IContainer Container { get {  return null; } }
96
97                 public bool DesignMode { get {  return true; } }
98
99                 public string Name { get { return "TestName"; } set { } }
100
101                 public object GetService (Type t)
102                 {
103                         if (t == typeof(ITypeDescriptorFilterService)) {
104                                 return new AnotherFilter ();
105                         }
106                         return null;
107                 }
108         }
109
110         class AnotherFilter: ITypeDescriptorFilterService
111         {
112                 public bool FilterAttributes (IComponent component,IDictionary attributes) {
113                         Attribute ea = new DefaultEventAttribute ("AnEvent");
114                         attributes [ea.TypeId] = ea;
115                         ea = new DefaultPropertyAttribute ("TestProperty");
116                         attributes [ea.TypeId] = ea;
117                         ea = new EditorAttribute ();
118                         attributes [ea.TypeId] = ea;
119                         return true;
120                 }
121
122                 public bool FilterEvents (IComponent component, IDictionary events) {
123                         return true;
124                 }
125
126                 public bool FilterProperties (IComponent component, IDictionary properties) {
127                         return true;
128                 }
129         }
130
131         [DescriptionAttribute ("my test component")]
132         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
133         public class MyComponent: Component
134         {
135                 string prop;
136                 
137                 [DescriptionAttribute ("test")]
138                 public event EventHandler AnEvent;
139                 
140                 public event EventHandler AnotherEvent;
141                 
142                 public MyComponent  ()
143                 {
144                 }
145                 
146                 public MyComponent (ISite site)
147                 {
148                         Site = site;
149                 }
150                 
151                 [DescriptionAttribute ("test")]
152                 public virtual string TestProperty
153                 {
154                         get { return prop; }
155                         set { prop = value; }
156                 }
157                 
158                 public string AnotherProperty
159                 {
160                         get { return prop; }
161                         set { prop = value; }
162                 }
163         }
164
165         [DescriptionAttribute ("my test derived component")]
166         [DesignerAttribute (typeof(MyOtherDesigner))]
167         public class MyDerivedComponent: MyComponent
168         {
169                 string prop;
170                 
171                 public MyDerivedComponent  ()
172                 {
173                 }
174                 
175                 public MyDerivedComponent (ISite site) : base (site)
176                 {
177                 }
178                 
179                 [DescriptionAttribute ("test derived")]
180                 public override string TestProperty
181                 {
182                         get { return prop; }
183                         set { prop = value; }
184                 }
185         }
186         
187
188         [DefaultProperty("AnotherProperty")]
189         [DefaultEvent("AnotherEvent")]
190         [DescriptionAttribute ("my test component")]
191         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
192         public class AnotherComponent: Component {
193                 string prop;
194                 
195                 [DescriptionAttribute ("test")]
196                 public event EventHandler AnEvent;
197                 
198                 public event EventHandler AnotherEvent;
199                 
200                 public AnotherComponent () {
201                 }
202                 
203                 public AnotherComponent (ISite site) {
204                         Site = site;
205                 }
206                 
207                 [DescriptionAttribute ("test")]
208                 public string TestProperty {
209                         get { return prop; }
210                         set { prop = value; }
211                 }
212                 
213                 public string AnotherProperty {
214                         get { return prop; }
215                         set { prop = value; }
216                 }
217         }
218
219         public interface ITestInterface
220         {
221                 void TestFunction ();
222         }
223         
224         public class TestClass
225         {
226                 public TestClass()
227                 {}
228                         
229                 void TestFunction ()
230                 {}
231         }
232         
233         public struct TestStruct
234         {
235                 public int TestVal;
236         }
237         
238         [TestFixture]
239         public class TypeDescriptorTests: Assertion
240         {
241                 MyComponent com = new MyComponent ();
242                 MyComponent sitedcom = new MyComponent (new MySite ());
243                 AnotherComponent anothercom = new AnotherComponent ();
244                 
245                 [Test]
246                 public void TestCreateDesigner ()
247                 {
248                         IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
249                         Assert ("t1", des is MyDesigner);
250                         
251                         des = TypeDescriptor.CreateDesigner (com, typeof(string));
252                         AssertNull ("t2", des);
253                 }
254                 
255                 [Test]
256                 public void TestCreateEvent ()
257                 {
258                         EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
259                         AssertEquals ("t1", typeof(MyComponent), ed.ComponentType);
260                         AssertEquals ("t2", typeof(EventHandler), ed.EventType);
261                         AssertEquals ("t3", true, ed.IsMulticast);
262                         AssertEquals ("t4", "AnEvent", ed.Name);
263                 }
264                 
265                 [Test]
266                 public void TestCreateProperty ()
267                 {
268                         PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
269                         AssertEquals ("t1", typeof(MyComponent), pd.ComponentType);
270                         AssertEquals ("t2", "TestProperty", pd.Name);
271                         AssertEquals ("t3", typeof(string), pd.PropertyType);
272                         AssertEquals ("t4", false, pd.IsReadOnly);
273                         
274                         pd.SetValue (com, "hi");
275                         AssertEquals ("t5", "hi", pd.GetValue(com));
276                 }
277                 
278                 [Test]
279                 public void TestGetAttributes ()
280                 {
281                         AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
282                         Assert ("t2", col[typeof(DescriptionAttribute)] != null);
283                         Assert ("t3", col[typeof(DesignerAttribute)] != null);
284                         Assert ("t4", col[typeof(EditorAttribute)] == null);
285                         
286                         col = TypeDescriptor.GetAttributes (com);
287                         Assert ("t6", col[typeof(DescriptionAttribute)] != null);
288                         Assert ("t7", col[typeof(DesignerAttribute)] != null);
289                         Assert ("t8", col[typeof(EditorAttribute)] == null);
290                         
291                         col = TypeDescriptor.GetAttributes (sitedcom);
292                         Assert ("t10", col[typeof(DescriptionAttribute)] != null);
293                         Assert ("t11", col[typeof(DesignerAttribute)] != null);
294                         Assert ("t12", col[typeof(EditorAttribute)] != null);
295
296                         col = TypeDescriptor.GetAttributes (typeof (MyDerivedComponent));
297                         Assert ("t13", col[typeof(DesignerAttribute)] != null);
298                         Assert ("t14", col[typeof(DescriptionAttribute)] != null);
299                         DesignerAttribute attribute = col[typeof(DesignerAttribute)] as DesignerAttribute;
300                         Assert ("t15", attribute != null);
301                         AssertEquals ("t16", attribute.DesignerTypeName, typeof(MyOtherDesigner).AssemblyQualifiedName);
302                 }
303                 
304                 [Test]
305                 public void TestGetClassName ()
306                 {
307                         AssertEquals ("t1", typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
308                 }
309                 
310                 [Test]
311                 public void TestGetComponentName ()
312                 {
313 #if !NET_2_0
314                         AssertEquals ("t1", "MyComponent", TypeDescriptor.GetComponentName (com));
315                         AssertEquals ("t2", "MyComponent", TypeDescriptor.GetComponentName (com, false));
316                         AssertEquals ("t3", "Exception", TypeDescriptor.GetComponentName (new Exception ()));
317                         AssertEquals ("t4", "Exception", TypeDescriptor.GetComponentName (new Exception (), false));
318                         AssertNotNull ("t5", TypeDescriptor.GetComponentName (typeof (Exception)));
319                         AssertNotNull ("t6", TypeDescriptor.GetComponentName (typeof (Exception), false));
320 #else
321                         // in MS.NET 2.0, GetComponentName no longer returns
322                         // the type name if there's no custom typedescriptor
323                         // and no site
324                         AssertNull ("t1", TypeDescriptor.GetComponentName (com));
325                         AssertNull ("t2", TypeDescriptor.GetComponentName (com, false));
326                         AssertNull ("t3", TypeDescriptor.GetComponentName (new Exception ()));
327                         AssertNull ("t4", TypeDescriptor.GetComponentName (new Exception (), false));
328                         AssertNull ("t5", TypeDescriptor.GetComponentName (typeof (Exception)));
329                         AssertNull ("t6", TypeDescriptor.GetComponentName (typeof (Exception), false));
330 #endif
331                         AssertEquals ("t7", "TestName", TypeDescriptor.GetComponentName (sitedcom));
332                         AssertEquals ("t8", "TestName", TypeDescriptor.GetComponentName (sitedcom));
333                 }
334                 
335                 [Test]
336                 public void TestGetConverter ()
337                 {
338                         AssertEquals (typeof(BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType());
339                         AssertEquals (typeof(ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType());
340                         AssertEquals (typeof(SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType());
341                         AssertEquals (typeof(StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType());
342                         AssertEquals (typeof(CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType());
343                         AssertEquals (typeof(Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType());
344                         AssertEquals (typeof(Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType());
345                         AssertEquals (typeof(Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType());
346                         AssertEquals (typeof(UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType());
347                         AssertEquals (typeof(UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType());
348                         AssertEquals (typeof(UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType());
349                         AssertEquals (typeof(SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType());
350                         AssertEquals (typeof(DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType());
351                         AssertEquals (typeof(DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType());
352                         AssertEquals (typeof(ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType());
353                         AssertEquals (typeof(CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType());
354                         AssertEquals (typeof(DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType());
355                         AssertEquals (typeof(GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType());
356                         AssertEquals (typeof(TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType());
357                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType());
358
359                         // Tests from bug #71444
360                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType());
361                         AssertEquals (typeof(ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType());
362                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType());
363                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType());
364                         
365                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType());
366                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType());
367                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType());
368
369 #if NET_2_0
370                         // Test from bug #76686
371                         AssertEquals (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType ());
372 #endif
373                 }
374                 
375                 [Test]
376                 public void TestGetDefaultEvent ()
377                 {
378                         EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
379                         AssertNull ("t1", des);
380                         
381                         des = TypeDescriptor.GetDefaultEvent (com);
382                         AssertNull ("t2", des);
383
384                         des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
385                         AssertNotNull ("t3", des);
386                         AssertEquals ("t4", "AnotherEvent", des.Name);
387
388                         des = TypeDescriptor.GetDefaultEvent (anothercom);
389                         AssertNotNull ("t5", des);
390                         AssertEquals ("t6", "AnotherEvent", des.Name);
391
392                         des = TypeDescriptor.GetDefaultEvent (sitedcom);
393 #if NET_2_0
394                         AssertNull ("t7", des);
395 #else
396                         AssertNotNull ("t7/1", des);
397                         AssertEquals ("t7/2", "AnotherEvent", des.Name);
398 #endif
399
400                         des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
401                         AssertNotNull ("t8", des);
402                         AssertEquals ("t9", "AnEvent", des.Name);
403
404                         des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
405                         AssertNotNull ("t10", des);
406                         AssertEquals ("t11", "AnEvent", des.Name);
407                 }
408                 
409                 [Test]
410                 public void TestGetDefaultProperty ()
411                 {
412                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
413                         AssertNull ("t1", des);
414                         
415                         des = TypeDescriptor.GetDefaultProperty (com);
416                         AssertNull ("t2", des);
417
418                         des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
419                         AssertNotNull ("t1", des);
420                         AssertEquals ("t2", "AnotherProperty", des.Name);
421
422                         des = TypeDescriptor.GetDefaultProperty (anothercom);
423                         AssertNotNull ("t1", des);
424                         AssertEquals ("t2", "AnotherProperty", des.Name);
425                 }
426                 
427                 [Test]
428 #if !NET_2_0
429                 // throws NullReferenceException on MS.NET 1.x due to bug
430                 // which is fixed in MS.NET 2.0
431                 [NUnit.Framework.Category("NotDotNet")]
432 #endif
433                 public void TestGetDefaultProperty2 ()
434                 {
435                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
436                         AssertNull ("t1", des);
437
438                         des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
439                         AssertNotNull ("t2", des);
440                         AssertEquals ("t3", "TestProperty", des.Name);
441
442                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
443                         AssertNotNull ("t4", des);
444                         AssertEquals ("t5", "TestProperty", des.Name);
445
446                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
447                         AssertNull ("t6", des);
448                 }
449
450                 [Test]
451                 public void TestGetEvents ()
452                 {
453                         EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
454                                 
455                         AssertEquals ("t1.1", 3, col.Count);
456                         Assert ("t1.2", col.Find ("AnEvent", true) != null);
457                         Assert ("t1.3", col.Find ("AnotherEvent", true) != null);
458                         Assert ("t1.4", col.Find ("Disposed", true) != null);
459                         
460                         col = TypeDescriptor.GetEvents (com);
461                         AssertEquals ("t2.1", 3, col.Count);
462                         Assert ("t2.2", col.Find ("AnEvent", true) != null);
463                         Assert ("t2.3", col.Find ("AnotherEvent", true) != null);
464                         Assert ("t2.4", col.Find ("Disposed", true) != null);
465                         
466                         col = TypeDescriptor.GetEvents (sitedcom);
467                         AssertEquals ("t3.1", 2, col.Count);
468                         Assert ("t3.2", col.Find ("AnotherEvent", true) != null);
469                         Assert ("t3.3", col.Find ("Disposed", true) != null);
470                         
471                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
472                         
473                         col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
474                         AssertEquals ("t4.1", 1, col.Count);
475                         Assert ("t4.2", col.Find ("AnEvent", true) != null);
476                         
477                         col = TypeDescriptor.GetEvents (com, filter);
478                         AssertEquals ("t5.1", 1, col.Count);
479                         Assert ("t5.2", col.Find ("AnEvent", true) != null);
480                         
481                         col = TypeDescriptor.GetEvents (sitedcom, filter);
482                         AssertEquals ("t6", 0, col.Count);
483                 }
484                 
485                 [Test]
486                 public void TestGetProperties ()
487                 {
488                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
489                         Assert ("t1.1", col.Find ("TestProperty", true) != null);
490                         Assert ("t1.2", col.Find ("AnotherProperty", true) != null);
491                         
492                         col = TypeDescriptor.GetProperties (com);
493                         Assert ("t2.1", col.Find ("TestProperty", true) != null);
494                         Assert ("t2.2", col.Find ("AnotherProperty", true) != null);
495                         
496                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
497                         
498                         col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
499                         Assert ("t4.1", col.Find ("TestProperty", true) != null);
500                         Assert ("t4.2", col.Find ("AnotherProperty", true) == null);
501                         
502                         col = TypeDescriptor.GetProperties (com, filter);
503                         Assert ("t5.1", col.Find ("TestProperty", true) != null);
504                         Assert ("t5.2", col.Find ("AnotherProperty", true) == null);
505                         
506                 }
507
508                 [Test]
509 #if !NET_2_0
510                 // throws NullReferenceException on MS.NET 1.x due to bug
511                 // which is fixed in MS.NET 2.0
512                 [NUnit.Framework.Category("NotDotNet")]
513 #endif
514                 public void TestGetProperties2 ()
515                 {
516                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
517                         Assert ("t3.1", col.Find ("TestProperty", true) == null);
518                         Assert ("t3.2", col.Find ("AnotherProperty", true) != null);
519
520                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
521                         col = TypeDescriptor.GetProperties (sitedcom, filter);
522                         Assert ("t6.1", col.Find ("TestProperty", true) == null);
523                         Assert ("t6.2", col.Find ("AnotherProperty", true) == null);
524                 }
525
526                 [TypeConverter (typeof (TestConverter))]
527                 class TestConverterClass {
528                 }
529
530                 class TestConverter : TypeConverter {
531                         public Type Type;
532
533                         public TestConverter (Type type)
534                         {
535                                 this.Type = type;
536                         }
537                 }
538
539                 [Test]
540                 public void TestConverterCtorWithArgument ()
541                 {
542                         TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass));
543                         Assert ("#01", null != t.GetType ());
544                         AssertEquals ("#02", typeof (TestConverter), t.GetType ());
545                         TestConverter converter = (TestConverter) t;
546                         AssertEquals ("#03", typeof (TestConverterClass), converter.Type);
547                 }
548
549                 [Test]
550                 public void GetPropertiesIgnoreIndexers ()
551                 {
552                         PropertyDescriptorCollection pc =
553                                 TypeDescriptor.GetProperties (typeof (string));
554                         // There are two string properties: Length and Chars.
555                         // Chars is an indexer.
556                         //
557                         // Future version of CLI might contain some additional
558                         // properties. In that case simply increase the
559                         // number. (Also, it is fine to just remove #2.)
560                         AssertEquals ("#1", 1, pc.Count);
561                         AssertEquals ("#2", "Length", pc [0].Name);
562                 }
563         }
564 }
565