ce887c7ea82524f97e6ecc1c225807e1db6487fb
[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 NoFilterSite : ISite
111         {
112                 public NoFilterSite () : this (null)
113                 {
114                 }
115
116                 public NoFilterSite (IContainer container)
117                 {
118                         _container = container;
119                 }
120
121                 public IComponent Component {
122                         get { return null; }
123                 }
124
125                 public IContainer Container {
126                         get { return _container; }
127                 }
128
129                 public bool DesignMode { get { return true; } }
130
131                 public string Name { get { return "TestName"; } set { } }
132
133                 public object GetService (Type t)
134                 {
135                         return null;
136                 }
137
138                 public IContainer _container;
139         }
140
141         class MyContainer : IContainer
142         {
143                 public MyContainer ()
144                 {
145                         _components = new ComponentCollection (new IComponent [0]);
146                 }
147
148                 public ComponentCollection Components {
149                         get { return _components; }
150                 }
151
152                 public void Add (IComponent component)
153                 {
154                 }
155
156                 public void Add (IComponent component, string name)
157                 {
158                 }
159
160                 public void Dispose ()
161                 {
162                 }
163
164                 public void Remove (IComponent component)
165                 {
166                 }
167
168                 private ComponentCollection _components;
169         }
170
171         class AnotherFilter: ITypeDescriptorFilterService
172         {
173                 public bool FilterAttributes (IComponent component,IDictionary attributes) {
174                         Attribute ea = new DefaultEventAttribute ("AnEvent");
175                         attributes [ea.TypeId] = ea;
176                         ea = new DefaultPropertyAttribute ("TestProperty");
177                         attributes [ea.TypeId] = ea;
178                         ea = new EditorAttribute ();
179                         attributes [ea.TypeId] = ea;
180                         return true;
181                 }
182
183                 public bool FilterEvents (IComponent component, IDictionary events) {
184                         return true;
185                 }
186
187                 public bool FilterProperties (IComponent component, IDictionary properties) {
188                         return true;
189                 }
190         }
191
192         [DescriptionAttribute ("my test component")]
193         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
194         public class MyComponent: Component
195         {
196                 string prop;
197                 
198                 [DescriptionAttribute ("test")]
199                 public event EventHandler AnEvent;
200                 
201                 public event EventHandler AnotherEvent;
202                 
203                 public MyComponent  ()
204                 {
205                 }
206                 
207                 public MyComponent (ISite site)
208                 {
209                         Site = site;
210                 }
211
212                 public MyComponent (IContainer container)
213                 {
214                         container.Add (this);
215                 }
216
217                 [DescriptionAttribute ("test")]
218                 public virtual string TestProperty
219                 {
220                         get { return prop; }
221                         set { prop = value; }
222                 }
223                 
224                 public string AnotherProperty
225                 {
226                         get { return prop; }
227                         set { prop = value; }
228                 }
229         }
230
231         [DescriptionAttribute ("my test derived component")]
232         [DesignerAttribute (typeof(MyOtherDesigner))]
233         public class MyDerivedComponent: MyComponent
234         {
235                 string prop;
236                 
237                 public MyDerivedComponent  ()
238                 {
239                 }
240                 
241                 public MyDerivedComponent (ISite site) : base (site)
242                 {
243                 }
244                 
245                 [DescriptionAttribute ("test derived")]
246                 public override string TestProperty
247                 {
248                         get { return prop; }
249                         set { prop = value; }
250                 }
251         }
252         
253
254         [DefaultProperty("AnotherProperty")]
255         [DefaultEvent("AnotherEvent")]
256         [DescriptionAttribute ("my test component")]
257         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
258         public class AnotherComponent: Component {
259                 string prop;
260                 
261                 [DescriptionAttribute ("test")]
262                 public event EventHandler AnEvent;
263                 
264                 public event EventHandler AnotherEvent;
265                 
266                 public AnotherComponent () {
267                 }
268                 
269                 public AnotherComponent (ISite site) {
270                         Site = site;
271                 }
272                 
273                 [DescriptionAttribute ("test")]
274                 public string TestProperty {
275                         get { return prop; }
276                         set { prop = value; }
277                 }
278                 
279                 public string AnotherProperty {
280                         get { return prop; }
281                         set { prop = value; }
282                 }
283         }
284
285         public interface ITestInterface
286         {
287                 void TestFunction ();
288         }
289         
290         public class TestClass
291         {
292                 public TestClass()
293                 {}
294                         
295                 void TestFunction ()
296                 {}
297         }
298         
299         public struct TestStruct
300         {
301                 public int TestVal;
302         }
303
304         public class TestCustomTypeDescriptor : ICustomTypeDescriptor
305         {
306                 public string methods_called = "";
307
308                 public void ResetMethodsCalled ()
309                 {
310                         methods_called = "";
311                 }
312
313                 public TypeConverter GetConverter()
314                 {
315                         return new StringConverter();
316                 }
317
318                 public EventDescriptorCollection GetEvents(Attribute[] attributes)
319                 {
320                         methods_called += "1";
321                         return null;
322                 }
323
324                 public EventDescriptorCollection GetEvents()
325                 {
326                         methods_called += "2";
327                         return null;
328                 }
329
330                 public string GetComponentName()
331                 {
332                         return "MyComponentnName";
333                 }
334
335                 public object GetPropertyOwner(PropertyDescriptor pd)
336                 {
337                         return this;
338                 }
339
340                 public AttributeCollection GetAttributes()
341                 {
342                         methods_called += "3";
343                         return null;
344                 }
345
346                 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
347                 {
348                         methods_called += "4";
349                         return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
350                 }
351
352                 public PropertyDescriptorCollection GetProperties()
353                 {
354                         methods_called += "5";
355                         return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
356                 }
357
358                 public object GetEditor(Type editorBaseType)
359                 {
360                         return null;
361                 }
362
363                 public PropertyDescriptor GetDefaultProperty()
364                 {
365                         methods_called += "6";
366                         return null;
367                 }
368
369                 public EventDescriptor GetDefaultEvent()
370                 {
371                         methods_called += "7";
372                         return null;
373                 }
374
375                 public string GetClassName()
376                 {
377                         return this.GetType().Name;
378                 }
379         }
380
381         [TestFixture]
382         public class TypeDescriptorTests
383         {
384                 MyComponent com = new MyComponent ();
385                 MyComponent sitedcom = new MyComponent (new MySite ());
386                 MyComponent nfscom = new MyComponent (new NoFilterSite (new MyContainer ()));
387                 AnotherComponent anothercom = new AnotherComponent ();
388                 
389                 [Test]
390                 public void TestICustomTypeDescriptor ()
391                 {
392                         TestCustomTypeDescriptor test = new TestCustomTypeDescriptor ();
393
394                         PropertyDescriptorCollection props;
395                         PropertyDescriptor prop;
396                         EventDescriptorCollection events;
397
398                         test.ResetMethodsCalled ();
399                         props = TypeDescriptor.GetProperties (test);
400                         Assert.AreEqual ("5", test.methods_called, "#1");
401
402                         test.ResetMethodsCalled ();
403                         props = TypeDescriptor.GetProperties (test, new Attribute[0]);
404                         Assert.AreEqual ("4", test.methods_called, "#2");
405
406                         test.ResetMethodsCalled ();
407                         props = TypeDescriptor.GetProperties (test, new Attribute[0], false);
408                         Assert.AreEqual ("4", test.methods_called, "#3");
409
410                         test.ResetMethodsCalled ();
411                         props = TypeDescriptor.GetProperties (test, false);
412                         Assert.AreEqual ("5", test.methods_called, "#4");
413
414                         test.ResetMethodsCalled ();
415                         prop = TypeDescriptor.GetDefaultProperty (test);
416                         Assert.AreEqual ("6", test.methods_called, "#5");
417
418                         test.ResetMethodsCalled ();
419                         events = TypeDescriptor.GetEvents (test);
420                         Assert.AreEqual ("2", test.methods_called, "#6");
421
422                         test.ResetMethodsCalled ();
423                         events = TypeDescriptor.GetEvents (test, new Attribute[0]);
424                         Assert.AreEqual ("1", test.methods_called, "#7");
425
426                         test.ResetMethodsCalled ();
427                         events = TypeDescriptor.GetEvents (test, false);
428                         Assert.AreEqual ("2", test.methods_called, "#8");
429                 }
430
431                 [Test]
432                 public void TestCreateDesigner ()
433                 {
434                         IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
435                         Assert.IsTrue (des is MyDesigner, "#1");
436                         
437                         des = TypeDescriptor.CreateDesigner (com, typeof(string));
438                         Assert.IsNull (des, "#2");
439                 }
440                 
441                 [Test]
442                 public void TestCreateEvent ()
443                 {
444                         EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
445                         Assert.AreEqual (typeof (MyComponent), ed.ComponentType, "#1");
446                         Assert.AreEqual (typeof (EventHandler), ed.EventType, "#2");
447                         Assert.IsTrue (ed.IsMulticast, "#3");
448                         Assert.AreEqual ("AnEvent", ed.Name, "#4");
449                 }
450                 
451                 [Test]
452                 public void TestCreateProperty ()
453                 {
454                         PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
455                         Assert.AreEqual (typeof (MyComponent), pd.ComponentType, "#1");
456                         Assert.AreEqual ("TestProperty", pd.Name, "#2");
457                         Assert.AreEqual (typeof (string), pd.PropertyType, "#3");
458                         Assert.IsFalse (pd.IsReadOnly, "#4");
459                         
460                         pd.SetValue (com, "hi");
461                         Assert.AreEqual ("hi", pd.GetValue (com), "#5");
462                 }
463                 
464                 [Test]
465                 public void TestGetAttributes ()
466                 {
467                         AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
468                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#A1");
469                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#A2");
470                         Assert.IsNull (col [typeof (EditorAttribute)], "#A3");
471                         
472                         col = TypeDescriptor.GetAttributes (com);
473                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#B1");
474                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#B2");
475                         Assert.IsNull (col [typeof (EditorAttribute)], "#B3");
476                         
477                         col = TypeDescriptor.GetAttributes (sitedcom);
478                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#C1");
479                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#C2");
480                         Assert.IsNotNull (col [typeof (EditorAttribute)], "#C3");
481
482                         col = TypeDescriptor.GetAttributes (nfscom);
483                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#D1");
484                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#D2");
485                         Assert.IsNull (col [typeof (EditorAttribute)], "#D3");
486
487                         col = TypeDescriptor.GetAttributes (typeof (MyDerivedComponent));
488                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#E1");
489                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#E2");
490                         DesignerAttribute attribute = col[typeof(DesignerAttribute)] as DesignerAttribute;
491                         Assert.IsNotNull (attribute, "#E3");
492                         // there are multiple DesignerAttribute present and their order in the collection isn't deterministic
493                         bool found = false;
494                         for (int i = 0; i < col.Count; i++) {
495                                 attribute = (col [i] as DesignerAttribute);
496                                 if (attribute != null) {
497                                         found = typeof(MyOtherDesigner).AssemblyQualifiedName == attribute.DesignerTypeName;
498                                         if (found)
499                                                 break;
500                                 }
501                         }
502                         Assert.IsTrue (found, "#E4");
503                 }
504                 
505                 [Test]
506                 public void TestGetClassName ()
507                 {
508                         Assert.AreEqual (typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
509                 }
510                 
511                 [Test]
512                 public void TestGetComponentName ()
513                 {
514 #if NET_2_0
515                         // in MS.NET 2.0, GetComponentName no longer returns
516                         // the type name if there's no custom typedescriptor
517                         // and no site
518                         Assert.IsNull (TypeDescriptor.GetComponentName (com), "#1");
519                         Assert.IsNull (TypeDescriptor.GetComponentName (com, false), "#2");
520                         Assert.IsNull (TypeDescriptor.GetComponentName (new Exception ()), "#3");
521                         Assert.IsNull (TypeDescriptor.GetComponentName (new Exception (), false), "#4");
522                         Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception)), "#4");
523                         Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception), false), "#6");
524 #else
525                         Assert.AreEqual ("MyComponent", TypeDescriptor.GetComponentName (com), "#1");
526                         Assert.AreEqual ("MyComponent", TypeDescriptor.GetComponentName (com, false), "#2");
527                         Assert.AreEqual ("Exception", TypeDescriptor.GetComponentName (new Exception ()), "#3");
528                         Assert.AreEqual ("Exception", TypeDescriptor.GetComponentName (new Exception (), false), "#4");
529                         Assert.IsNotNull (TypeDescriptor.GetComponentName (typeof (Exception)), "#5");
530                         Assert.IsNotNull (TypeDescriptor.GetComponentName (typeof (Exception), false), "#6");
531 #endif
532                         Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#7");
533                         Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#8");
534                 }
535                 
536                 [Test]
537                 public void TestGetConverter ()
538                 {
539                         Assert.AreEqual (typeof (BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType (), "#1");
540                         Assert.AreEqual (typeof (ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType (), "#2");
541                         Assert.AreEqual (typeof (SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType (), "#3");
542                         Assert.AreEqual (typeof (StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType (), "#4");
543                         Assert.AreEqual (typeof (CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType (), "#5");
544                         Assert.AreEqual (typeof (Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType (), "#6");
545                         Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType (), "#7");
546                         Assert.AreEqual (typeof (Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType (), "#8");
547                         Assert.AreEqual (typeof (UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType (), "#9");
548                         Assert.AreEqual (typeof (UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType (), "#10");
549                         Assert.AreEqual (typeof (UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType (), "#11");
550                         Assert.AreEqual (typeof (SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType (), "#12");
551                         Assert.AreEqual (typeof (DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType (), "#13");
552                         Assert.AreEqual (typeof (DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType (), "#14");
553                         Assert.AreEqual (typeof (ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType (), "#15");
554                         Assert.AreEqual (typeof (CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType (), "#16");
555                         Assert.AreEqual (typeof (DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType (), "#17");
556                         Assert.AreEqual (typeof (GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType (), "#18");
557                         Assert.AreEqual (typeof (TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType (), "#19");
558                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType (), "#20");
559
560                         // Tests from bug #71444
561                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType (), "#21");
562                         Assert.AreEqual (typeof (ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType (), "#22");
563                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType (), "#23");
564                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType (), "#24");
565
566                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType (), "#25");
567                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType (), "#26");
568                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType (), "#27");
569
570 #if NET_2_0
571                         // Test from bug #76686
572                         Assert.AreEqual  (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType (), "#28");
573 #endif
574                 }
575                 
576                 [Test]
577                 public void TestGetDefaultEvent ()
578                 {
579                         EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
580                         Assert.IsNull ( des, "#A");
581                         
582                         des = TypeDescriptor.GetDefaultEvent (com);
583                         Assert.IsNull (des, "#B");
584
585                         des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
586                         Assert.IsNotNull (des, "#C1");
587                         Assert.AreEqual ("AnotherEvent", des.Name, "#C2");
588
589                         des = TypeDescriptor.GetDefaultEvent (anothercom);
590                         Assert.IsNotNull (des, "#D1");
591                         Assert.AreEqual ("AnotherEvent", des.Name, "#D2");
592
593                         des = TypeDescriptor.GetDefaultEvent (sitedcom);
594 #if NET_2_0
595                         Assert.IsNull (des, "#E1");
596 #else
597                         Assert.IsNotNull (des, "#E1");
598                         Assert.AreEqual ("AnotherEvent", des.Name, "#E2");
599 #endif
600
601                         des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
602                         Assert.IsNotNull (des, "#F1");
603                         Assert.AreEqual ("AnEvent", des.Name, "#F2");
604
605                         des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
606                         Assert.IsNotNull (des, "#G1");
607                         Assert.AreEqual ("AnEvent", des.Name, "#G2");
608                 }
609                 
610                 [Test]
611                 public void TestGetDefaultProperty ()
612                 {
613                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
614                         Assert.IsNull (des, "#A");
615                         
616                         des = TypeDescriptor.GetDefaultProperty (com);
617                         Assert.IsNull (des, "#B");
618
619                         des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
620                         Assert.IsNotNull (des, "#C1");
621                         Assert.AreEqual ("AnotherProperty", des.Name, "#C2");
622
623                         des = TypeDescriptor.GetDefaultProperty (anothercom);
624                         Assert.IsNotNull (des, "#D1");
625                         Assert.AreEqual ("AnotherProperty", des.Name, "#D2");
626                 }
627                 
628                 [Test]
629 #if ONLY_1_1
630                 // throws NullReferenceException on MS.NET 1.x due to bug
631                 // which is fixed in MS.NET 2.0
632                 [NUnit.Framework.Category("NotDotNet")]
633 #endif
634                 public void TestGetDefaultProperty2 ()
635                 {
636                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
637                         Assert.IsNull (des, "#A");
638
639                         des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
640                         Assert.IsNotNull (des, "#B1");
641                         Assert.AreEqual ("TestProperty", des.Name, "#B2");
642
643                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
644                         Assert.IsNotNull (des, "#C1");
645                         Assert.AreEqual ("TestProperty", des.Name, "#C2");
646
647                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
648                         Assert.IsNull (des, "#D");
649                 }
650
651                 [Test]
652                 public void TestGetEvents ()
653                 {
654                         EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
655                         Assert.AreEqual (3, col.Count, "#A1");
656                         Assert.IsNotNull (col.Find ("AnEvent", true), "#A2");
657                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#A3");
658                         Assert.IsNotNull (col.Find ("Disposed", true), "#A4");
659                         
660                         col = TypeDescriptor.GetEvents (com);
661                         Assert.AreEqual (3, col.Count, "#B1");
662                         Assert.IsNotNull (col.Find ("AnEvent", true), "#B2");
663                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#B3");
664                         Assert.IsNotNull (col.Find ("Disposed", true), "#B4");
665                         
666                         col = TypeDescriptor.GetEvents (sitedcom);
667                         Assert.AreEqual (2, col.Count, "#C1");
668                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#C2");
669                         Assert.IsNotNull (col.Find ("Disposed", true), "#C3");
670
671                         col = TypeDescriptor.GetEvents (nfscom);
672                         Assert.AreEqual (3, col.Count, "#D1");
673                         Assert.IsNotNull (col.Find ("AnEvent", true), "#D2");
674                         Assert.IsNotNull ( col.Find ("AnotherEvent", true), "#D3");
675                         Assert.IsNotNull (col.Find ("Disposed", true), "#D4");
676
677                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
678                         
679                         col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
680                         Assert.AreEqual (1, col.Count, "#E1");
681                         Assert.IsNotNull (col.Find ("AnEvent", true), "#E2");
682                         
683                         col = TypeDescriptor.GetEvents (com, filter);
684                         Assert.AreEqual (1, col.Count, "#F1");
685                         Assert.IsNotNull (col.Find ("AnEvent", true), "#F2");
686                         
687                         col = TypeDescriptor.GetEvents (sitedcom, filter);
688                         Assert.AreEqual (0, col.Count, "#G");
689
690                         col = TypeDescriptor.GetEvents (nfscom, filter);
691                         Assert.AreEqual (1, col.Count, "#H1");
692                         Assert.IsNotNull (col.Find ("AnEvent", true), "#H2");
693                 }
694                 
695                 [Test]
696                 public void TestGetProperties ()
697                 {
698                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
699                         Assert.IsNotNull (col.Find ("TestProperty", true), "#A1");
700                         Assert.IsNotNull ( col.Find ("AnotherProperty", true), "#A2");
701                         
702                         col = TypeDescriptor.GetProperties (com);
703                         Assert.IsNotNull (col.Find ("TestProperty", true), "#B1");
704                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#B2");
705
706                         col = TypeDescriptor.GetProperties (nfscom);
707                         Assert.IsNotNull (col.Find ("TestProperty", true), "#C1");
708                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#C2");
709
710                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
711                         
712                         col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
713                         Assert.IsNotNull (col.Find ("TestProperty", true), "#D1");
714                         Assert.IsNull (col.Find ("AnotherProperty", true), "#D2");
715                         
716                         col = TypeDescriptor.GetProperties (com, filter);
717                         Assert.IsNotNull (col.Find ("TestProperty", true), "#E1");
718                         Assert.IsNull (col.Find ("AnotherProperty", true), "#E2");
719
720                         col = TypeDescriptor.GetProperties (nfscom, filter);
721                         Assert.IsNotNull (col.Find ("TestProperty", true), "#F1");
722                         Assert.IsNull (col.Find ("AnotherProperty", true), "#F2");
723                 }
724
725                 [Test]
726 #if ONLY_1_1
727                 // throws NullReferenceException on MS.NET 1.x due to bug
728                 // which is fixed in MS.NET 2.0
729                 [NUnit.Framework.Category("NotDotNet")]
730 #endif
731                 public void TestGetProperties2 ()
732                 {
733                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
734                         Assert.IsNull (col.Find ("TestProperty", true), "#A1");
735                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#A2");
736
737                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
738                         col = TypeDescriptor.GetProperties (sitedcom, filter);
739                         Assert.IsNull (col.Find ("TestProperty", true), "#B1");
740                         Assert.IsNull (col.Find ("AnotherProperty", true), "#B2");
741                 }
742
743                 [TypeConverter (typeof (TestConverter))]
744                 class TestConverterClass {
745                 }
746
747                 class TestConverter : TypeConverter {
748                         public Type Type;
749
750                         public TestConverter (Type type)
751                         {
752                                 this.Type = type;
753                         }
754                 }
755
756                 [Test]
757                 public void TestConverterCtorWithArgument ()
758                 {
759                         TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass));
760                         Assert.IsNotNull (t.GetType (), "#A1");
761                         Assert.AreEqual (typeof (TestConverter), t.GetType (), "#A2");
762                         TestConverter converter = (TestConverter) t;
763                         Assert.AreEqual (typeof (TestConverterClass), converter.Type, "#B");
764                 }
765
766                 [Test]
767                 public void GetPropertiesIgnoreIndexers ()
768                 {
769                         PropertyDescriptorCollection pc =
770                                 TypeDescriptor.GetProperties (typeof (string));
771                         // There are two string properties: Length and Chars.
772                         // Chars is an indexer.
773                         //
774                         // Future version of CLI might contain some additional
775                         // properties. In that case simply increase the
776                         // number. (Also, it is fine to just remove #2.)
777                         Assert.AreEqual (1, pc.Count, "#1");
778                         Assert.AreEqual ("Length", pc [0].Name, "#2");
779                 }
780         }
781 }