[io-layer] add URLs for some ximian bug numbers in sockets.cs
[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
10 using System;
11 using System.Collections;
12 using System.ComponentModel;
13 using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
14 using System.ComponentModel.Design;
15 using System.Globalization;
16
17 using System.Collections.Generic;
18
19 using NUnit.Framework;
20 using System.Reflection;
21
22 namespace MonoTests.System.ComponentModel
23 {
24         interface IFoo
25         {
26                 event EventHandler Fired;
27                 event EventHandler Closed;
28
29                 bool HasFired {
30                         get;
31                 }
32         }
33
34         interface IBar : IFoo
35         {
36                 event EventHandler Destroyed;
37
38                 bool IsDestroyed {
39                         get;
40                 }
41         }
42
43         class MyDesigner: IDesigner
44         {
45                 public MyDesigner()
46                 {
47                 }
48
49                 public IComponent Component {get{return null; }}
50
51                 public DesignerVerbCollection Verbs {get{return null; }}
52
53                 public void DoDefaultAction () { }
54
55                 public void Initialize (IComponent component) { }
56
57                 public void Dispose () { }
58         }
59
60         class MyOtherDesigner: IDesigner
61         {
62                 public MyOtherDesigner()
63                 {
64                 }
65
66                 public IComponent Component {get {return null; } }
67                 public DesignerVerbCollection Verbs { get {return null; } }
68                 public void DoDefaultAction () { }
69                 public void Initialize (IComponent component) { }
70                 public void Dispose () { }
71         }
72         
73         class MySite: ISite
74         { 
75                 public IComponent Component { get {  return null; } }
76
77                 public IContainer Container { get {  return null; } }
78
79                 public bool DesignMode { get {  return true; } }
80
81                 public string Name { get { return "TestName"; } set { } }
82
83                 public object GetService (Type t)
84                 {
85                         if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
86                         return null;
87                 }
88         }
89         
90         class MyFilter: ITypeDescriptorFilterService
91         {
92                 public bool FilterAttributes (IComponent component,IDictionary attributes)
93                 {
94                         Attribute ea = new DefaultEventAttribute ("AnEvent");
95                         attributes [ea.TypeId] = ea;
96                         ea = new DefaultPropertyAttribute ("TestProperty");
97                         attributes [ea.TypeId] = ea;
98                         ea = new EditorAttribute ();
99                         attributes [ea.TypeId] = ea;
100                         return true;
101                 }
102                 
103                 public bool FilterEvents (IComponent component, IDictionary events)
104                 {
105                         events.Remove ("AnEvent");
106                         return true;
107                 }
108                 
109                 public bool FilterProperties (IComponent component, IDictionary properties)
110                 {
111                         properties.Remove ("TestProperty");
112                         return true;
113                 }
114         }
115
116         class AnotherSite: ISite
117         { 
118                 public IComponent Component { get {  return null; } }
119
120                 public IContainer Container { get {  return null; } }
121
122                 public bool DesignMode { get {  return true; } }
123
124                 public string Name { get { return "TestName"; } set { } }
125
126                 public object GetService (Type t)
127                 {
128                         if (t == typeof(ITypeDescriptorFilterService)) {
129                                 return new AnotherFilter ();
130                         }
131                         return null;
132                 }
133         }
134
135         class NoFilterSite : ISite
136         {
137                 public NoFilterSite () : this (null)
138                 {
139                 }
140
141                 public NoFilterSite (IContainer container)
142                 {
143                         _container = container;
144                 }
145
146                 public IComponent Component {
147                         get { return null; }
148                 }
149
150                 public IContainer Container {
151                         get { return _container; }
152                 }
153
154                 public bool DesignMode { get { return true; } }
155
156                 public string Name { get { return "TestName"; } set { } }
157
158                 public object GetService (Type t)
159                 {
160                         return null;
161                 }
162
163                 public IContainer _container;
164         }
165
166         class MyContainer : IContainer
167         {
168                 public MyContainer ()
169                 {
170                         _components = new ComponentCollection (new IComponent [0]);
171                 }
172
173                 public ComponentCollection Components {
174                         get { return _components; }
175                 }
176
177                 public void Add (IComponent component)
178                 {
179                 }
180
181                 public void Add (IComponent component, string name)
182                 {
183                 }
184
185                 public void Dispose ()
186                 {
187                 }
188
189                 public void Remove (IComponent component)
190                 {
191                 }
192
193                 private ComponentCollection _components;
194         }
195
196         class AnotherFilter: ITypeDescriptorFilterService
197         {
198                 public bool FilterAttributes (IComponent component,IDictionary attributes) {
199                         Attribute ea = new DefaultEventAttribute ("AnEvent");
200                         attributes [ea.TypeId] = ea;
201                         ea = new DefaultPropertyAttribute ("TestProperty");
202                         attributes [ea.TypeId] = ea;
203                         ea = new EditorAttribute ();
204                         attributes [ea.TypeId] = ea;
205                         return true;
206                 }
207
208                 public bool FilterEvents (IComponent component, IDictionary events) {
209                         return true;
210                 }
211
212                 public bool FilterProperties (IComponent component, IDictionary properties) {
213                         return true;
214                 }
215         }
216
217         [DescriptionAttribute ("my test component")]
218         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
219         public class MyComponent: Component
220         {
221                 string prop;
222                 
223                 [DescriptionAttribute ("test")]
224                 public event EventHandler AnEvent;
225                 
226                 public event EventHandler AnotherEvent;
227                 
228                 public MyComponent  ()
229                 {
230                 }
231                 
232                 public MyComponent (ISite site)
233                 {
234                         Site = site;
235                 }
236
237                 public MyComponent (IContainer container)
238                 {
239                         container.Add (this);
240                 }
241
242                 [DescriptionAttribute ("test")]
243                 public virtual string TestProperty
244                 {
245                         get { return prop; }
246                         set { prop = value; }
247                 }
248                 
249                 public string AnotherProperty
250                 {
251                         get { return prop; }
252                         set { prop = value; }
253                 }
254
255                 [Browsable (false)]
256                 public string YetAnotherProperty
257                 {
258                         get { return null; }
259                 }
260
261                 public string Name {
262                         get { return null; }
263                 }
264
265                 public string Address {
266                         get { return null; }
267                 }
268
269                 public string Country {
270                         get { return null; }
271                 }
272
273                 private string HairColor {
274                         get { return null; }
275                 }
276
277                 protected int Weight {
278                         get { return 5; }
279                 }
280
281                 internal int Height {
282                         get { return 0; }
283                 }
284
285                 public string WriteOnlyProperty {
286                         set { prop = value; }
287                 }
288         }
289
290         [DescriptionAttribute ("my test derived component")]
291         [DesignerAttribute (typeof(MyOtherDesigner))]
292         public class MyDerivedComponent: MyComponent
293         {
294                 string prop;
295                 
296                 public MyDerivedComponent  ()
297                 {
298                 }
299                 
300                 public MyDerivedComponent (ISite site) : base (site)
301                 {
302                 }
303                 
304                 [DescriptionAttribute ("test derived")]
305                 public override string TestProperty
306                 {
307                         get { return prop; }
308                         set { prop = value; }
309                 }
310
311
312                 [DescriptionAttribute ("test derived")]
313                 public new string AnotherProperty
314                 {
315                         get { return base.AnotherProperty; }
316                         set { base.AnotherProperty = value; }
317                 }
318
319                 public new object YetAnotherProperty
320                 {
321                         get { return null; }
322                 }
323         }
324         
325
326         [DefaultProperty("AnotherProperty")]
327         [DefaultEvent("AnotherEvent")]
328         [DescriptionAttribute ("my test component")]
329         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
330         public class AnotherComponent: Component {
331                 string prop;
332                 
333                 [DescriptionAttribute ("test")]
334                 public event EventHandler AnEvent;
335                 
336                 public event EventHandler AnotherEvent;
337                 
338                 public AnotherComponent () {
339                 }
340                 
341                 public AnotherComponent (ISite site) {
342                         Site = site;
343                 }
344                 
345                 [DescriptionAttribute ("test")]
346                 public string TestProperty {
347                         get { return prop; }
348                         set { prop = value; }
349                 }
350                 
351                 public string AnotherProperty {
352                         get { return prop; }
353                         set { prop = value; }
354                 }
355         }
356
357         [Browsable (false)]
358         public interface ITestInterface
359         {
360                 void TestFunction ();
361         }
362
363         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
364         public class TestClass
365         {
366                 public TestClass()
367                 {}
368
369                 void TestFunction ()
370                 {}
371         }
372
373         [DescriptionAttribute ("bla")]
374         public class TestDerivedClass : TestClass, ITestInterface
375         {
376                 public void TestFunction ()
377                 {}
378         }
379
380         public struct TestStruct
381         {
382                 public int TestVal;
383         }
384
385         public class TestCustomTypeDescriptor : ICustomTypeDescriptor
386         {
387                 public string methods_called = "";
388
389                 public void ResetMethodsCalled ()
390                 {
391                         methods_called = "";
392                 }
393
394                 public TypeConverter GetConverter()
395                 {
396                         return new StringConverter ();
397                 }
398
399                 public EventDescriptorCollection GetEvents(Attribute[] attributes)
400                 {
401                         methods_called += "1";
402                         return null;
403                 }
404
405                 public EventDescriptorCollection GetEvents()
406                 {
407                         methods_called += "2";
408                         return null;
409                 }
410
411                 public string GetComponentName()
412                 {
413                         return "MyComponentnName";
414                 }
415
416                 public object GetPropertyOwner(PropertyDescriptor pd)
417                 {
418                         return this;
419                 }
420
421                 public AttributeCollection GetAttributes()
422                 {
423                         methods_called += "3";
424                         return null;
425                 }
426
427                 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
428                 {
429                         methods_called += "4";
430                         return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
431                 }
432
433                 public PropertyDescriptorCollection GetProperties()
434                 {
435                         methods_called += "5";
436                         return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
437                 }
438
439                 public object GetEditor(Type editorBaseType)
440                 {
441                         return null;
442                 }
443
444                 public PropertyDescriptor GetDefaultProperty()
445                 {
446                         methods_called += "6";
447                         return null;
448                 }
449
450                 public EventDescriptor GetDefaultEvent()
451                 {
452                         methods_called += "7";
453                         return null;
454                 }
455
456                 public string GetClassName()
457                 {
458                         return this.GetType ().Name;
459                 }
460         }
461
462         class MyCustomTypeDescriptor : CustomTypeDescriptor
463         {
464                 public MyTypeDescriptionProvider Provider { get; private set; }
465
466                 public MyCustomTypeDescriptor (MyTypeDescriptionProvider provider)
467                 {
468                         Provider = provider;
469                 }
470
471                 public override string GetClassName ()
472                 {
473                         return Provider.Id;
474                 }
475         }
476
477         class MyTypeDescriptionProvider : TypeDescriptionProvider
478         {
479                 public string Id { get; private set; }
480                 public bool CreateInstanceCalled { get; private set; }
481
482                 public MyTypeDescriptionProvider ()
483                         : this (null)
484                 {
485                 }
486
487                 public MyTypeDescriptionProvider (string id)
488                 {
489                         Id = id;
490                 }
491
492                 public override ICustomTypeDescriptor GetTypeDescriptor (Type objectType, object instance)
493                 {
494                         return new MyCustomTypeDescriptor (this);
495                 }
496
497                 public override object CreateInstance (IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
498                 {
499                         CreateInstanceCalled = true;
500                         return base.CreateInstance (provider, objectType, argTypes, args);
501                 }
502         }
503
504         [TestFixture]
505         public class TypeDescriptorTests
506         {
507                 MyComponent com = new MyComponent ();
508                 MyComponent sitedcom = new MyComponent (new MySite ());
509                 MyComponent nfscom = new MyComponent (new NoFilterSite (new MyContainer ()));
510                 AnotherComponent anothercom = new AnotherComponent ();
511                 
512                 [Test]
513                 [ExpectedException (typeof (ArgumentNullException))]
514                 public void TestAddAttributes_Type_Attributes_1 ()
515                 {
516                         TypeDescriptionProvider provider = TypeDescriptor.AddAttributes ((Type) null, null);
517                 }
518
519                 [Test]
520                 [ExpectedException (typeof (ArgumentNullException))]
521                 public void TestAddAttributes_Type_Attributes_2 ()
522                 {
523                         TypeDescriptionProvider provider = TypeDescriptor.AddAttributes (typeof (string), null);
524                 }
525
526                 [Test]
527                 public void TestAddAttributes_Type_Attributes_3 ()
528                 {
529                         Attribute[] new_attributes = new Attribute[] {
530                                 new ReadOnlyAttribute (true),
531                                 new BindableAttribute (true)
532                         };
533
534                         TypeDescriptionProvider provider = null;
535                         ICustomTypeDescriptor descriptor;
536                         AttributeCollection attributes;
537
538                         try {
539                                 provider = TypeDescriptor.AddAttributes (typeof (string), new Attribute[] { });
540                                 Assert.IsNotNull (provider, "#A1");
541
542                                 descriptor = provider.GetTypeDescriptor (typeof (string));
543                                 Assert.IsNotNull (descriptor, "#A1-1");
544
545                                 attributes = descriptor.GetAttributes ();
546                                 Assert.IsNotNull (attributes, "#A1-2");
547                         } finally {
548                                 if (provider != null)
549                                         TypeDescriptor.RemoveProvider (provider, typeof (string));
550                         }
551
552                         provider = null;
553                         try {
554                                 provider = TypeDescriptor.AddAttributes (typeof (string), new_attributes);
555                                 Assert.IsNotNull (provider, "#B1");
556
557                                 descriptor = provider.GetTypeDescriptor (typeof (string));
558                                 Assert.IsNotNull (descriptor, "#B1-1");
559
560                                 attributes = descriptor.GetAttributes ();
561                                 Assert.IsNotNull (attributes, "#B1-2");
562                                 Assert.AreNotEqual (0, attributes.Count, "#B1-3");
563                                 Assert.IsTrue (attributes.Contains (new_attributes));
564                         } finally {
565                                 if (provider != null)
566                                         TypeDescriptor.RemoveProvider (provider, typeof (string));
567                         }
568                 }
569
570                 [Test]
571                 [ExpectedException (typeof (ArgumentNullException))]
572                 public void TestAddAttributes_Instance_Attributes_1 ()
573                 {
574                         TypeDescriptionProvider provider = TypeDescriptor.AddAttributes ((object) null, null);
575                 }
576
577                 [Test]
578                 [ExpectedException (typeof (ArgumentNullException))]
579                 public void TestAddAttributes_Instance_Attributes_2 ()
580                 {
581                         string s = "test";
582                         TypeDescriptionProvider provider = TypeDescriptor.AddAttributes (s, null);
583                 }
584
585                 [Test]
586                 public void TestAddAttributes_Instance_Attributes_3 ()
587                 {
588                         Attribute[] new_attributes = new Attribute[] {
589                                 new ReadOnlyAttribute (true),
590                                 new BindableAttribute (true)
591                         };
592
593                         TypeDescriptionProvider provider = null;
594                         ICustomTypeDescriptor descriptor;
595                         AttributeCollection attributes;
596                         string s = "test";
597
598                         try {
599                                 provider = TypeDescriptor.AddAttributes (s, new Attribute[] { });
600                                 Assert.IsNotNull (provider, "#A1");
601
602                                 descriptor = provider.GetTypeDescriptor (s);
603                                 Assert.IsNotNull (descriptor, "#A1-1");
604
605                                 attributes = descriptor.GetAttributes ();
606                                 Assert.IsNotNull (attributes, "#A1-2");
607                         } finally {
608                                 if (provider != null)
609                                         TypeDescriptor.RemoveProvider (provider, s);
610                         }
611
612                         provider = null;
613                         try {
614                                 provider = TypeDescriptor.AddAttributes (s, new_attributes);
615                                 Assert.IsNotNull (provider, "#B1");
616
617                                 descriptor = provider.GetTypeDescriptor (s);
618                                 Assert.IsNotNull (descriptor, "#B1-1");
619
620                                 attributes = descriptor.GetAttributes ();
621                                 Assert.IsNotNull (attributes, "#B1-2");
622                                 Assert.AreNotEqual (0, attributes.Count, "#B1-3");
623                                 Assert.IsTrue (attributes.Contains (new_attributes));
624                         } finally {
625                                 if (provider != null)
626                                         TypeDescriptor.RemoveProvider (provider, s);
627                         }
628                 }
629
630                 [Test]
631                 [ExpectedException (typeof (ArgumentNullException))]
632                 public void TestAddProvider_Provider_Instance_1 ()
633                 {
634                         TypeDescriptor.AddProvider (null, (object)null);
635                 }
636
637                 [Test]
638                 [ExpectedException (typeof (ArgumentNullException))]
639                 public void TestAddProvider_Provider_Instance_2 ()
640                 {
641                         var provider = new MyTypeDescriptionProvider ();
642                         TypeDescriptor.AddProvider (provider, (object) null);
643                 }
644
645                 [Test]
646                 public void TestAddProvider_Provider_Instance_3 ()
647                 {
648                         var instance = new MyComponent ();
649                         var providers = new MyTypeDescriptionProvider[] {
650                                 new MyTypeDescriptionProvider ("One"),
651                                 new MyTypeDescriptionProvider ("Two"),
652                                 new MyTypeDescriptionProvider ("Three"),
653                                 new MyTypeDescriptionProvider ("Four")
654                         };
655
656                         try {
657                                 TypeDescriptionProvider provider;
658                                 ICustomTypeDescriptor descriptor;
659
660                                 TypeDescriptor.AddProvider (providers[0], instance);
661                                 provider = TypeDescriptor.GetProvider (instance);
662                                 Assert.IsNotNull (provider, "#A1");
663                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
664                                 Assert.IsNotNull (descriptor, "#A1-1");
665                                 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2");
666                                 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3");
667
668                                 descriptor.GetProperties ();
669
670                                 TypeDescriptor.AddProvider (providers[1], instance);
671                                 provider = TypeDescriptor.GetProvider (instance);
672                                 Assert.IsNotNull (provider, "#B1");
673                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
674                                 Assert.IsNotNull (descriptor, "#B1-1");
675                                 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2");
676
677                                 // Providers are stored in a stack according to docs, but it's in reality
678                                 // a FIFO linked list
679                                 TypeDescriptor.AddProvider (providers[2], instance);
680                                 TypeDescriptor.AddProvider (providers[3], instance);
681                                 provider = TypeDescriptor.GetProvider (instance);
682                                 Assert.IsNotNull (provider, "#C1");
683                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
684                                 Assert.IsNotNull (descriptor, "#C1-1");
685                                 Assert.AreEqual ("Four", descriptor.GetClassName (), "#C1-2");
686
687                                 TypeDescriptor.RemoveProvider (providers[2], instance);
688                                 provider = TypeDescriptor.GetProvider (instance);
689                                 Assert.IsNotNull (provider, "#D1");
690                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
691                                 Assert.IsNotNull (descriptor, "#D1-1");
692                                 Assert.AreEqual ("Four", descriptor.GetClassName (), "#D1-2");
693
694                                 TypeDescriptor.RemoveProvider (providers[3], instance);
695                                 provider = TypeDescriptor.GetProvider (instance);
696                                 Assert.IsNotNull (provider, "#E1");
697                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
698                                 Assert.IsNotNull (descriptor, "#E1-1");
699                                 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2");
700
701                         } finally {
702                                 TypeDescriptor.RemoveProvider (providers[0], instance);
703                                 TypeDescriptor.RemoveProvider (providers[1], instance);
704                                 TypeDescriptor.RemoveProvider (providers[2], instance);
705                                 TypeDescriptor.RemoveProvider (providers[3], instance);
706                         }
707                 }
708
709                 [Test]
710                 public void TestAddProvider_Provider_Instance_4 ()
711                 {
712                         var instance = new MyComponent ();
713                         var providers = new MyTypeDescriptionProvider[] {
714                                 new MyTypeDescriptionProvider ("One"),
715                                 new MyTypeDescriptionProvider ("Two"),
716                                 new MyTypeDescriptionProvider ("Three"),
717                                 new MyTypeDescriptionProvider ("Four")
718                         };
719
720                         try {
721                                 TypeDescriptionProvider provider;
722                                 ICustomTypeDescriptor descriptor;
723
724                                 TypeDescriptor.AddProvider (providers[0], instance);
725                                 provider = TypeDescriptor.GetProvider (instance);
726                                 Assert.IsNotNull (provider, "#A1");
727                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
728                                 Assert.IsNotNull (descriptor, "#A1-1");
729                                 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2");
730                                 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3");
731
732                                 descriptor.GetProperties ();
733
734                                 TypeDescriptor.AddProvider (providers[1], instance);
735                                 provider = TypeDescriptor.GetProvider (instance);
736                                 Assert.IsNotNull (provider, "#B1");
737                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
738                                 Assert.IsNotNull (descriptor, "#B1-1");
739                                 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2");
740
741                                 // Providers are stored in a stack according to docs, but it's in reality
742                                 // a FIFO linked list
743                                 TypeDescriptor.AddProvider (providers[0], instance);
744                                 TypeDescriptor.AddProvider (providers[0], instance);
745                                 provider = TypeDescriptor.GetProvider (instance);
746                                 Assert.IsNotNull (provider, "#C1");
747                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
748                                 Assert.IsNotNull (descriptor, "#C1-1");
749                                 Assert.AreEqual ("One", descriptor.GetClassName (), "#C1-2");
750
751                                 TypeDescriptor.RemoveProvider (providers[0], instance);
752                                 provider = TypeDescriptor.GetProvider (instance);
753                                 Assert.IsNotNull (provider, "#D1");
754                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
755                                 Assert.IsNotNull (descriptor, "#D1-1");
756                                 Assert.AreEqual ("One", descriptor.GetClassName (), "#D1-2");
757
758                                 TypeDescriptor.RemoveProvider (providers[0], instance);
759                                 provider = TypeDescriptor.GetProvider (instance);
760                                 Assert.IsNotNull (provider, "#E1");
761                                 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
762                                 Assert.IsNotNull (descriptor, "#E1-1");
763                                 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2");
764
765                         } finally {
766                                 TypeDescriptor.RemoveProvider (providers[0], instance);
767                                 TypeDescriptor.RemoveProvider (providers[1], instance);
768                                 TypeDescriptor.RemoveProvider (providers[2], instance);
769                                 TypeDescriptor.RemoveProvider (providers[3], instance);
770                         }
771                 }
772
773                 [Test]
774                 [ExpectedException (typeof (ArgumentNullException))]
775                 public void TestAddProvider_Provider_Type_1 ()
776                 {
777                         TypeDescriptor.AddProvider (null, (Type) null);
778                 }
779
780                 [Test]
781                 [ExpectedException (typeof (ArgumentNullException))]
782                 public void TestAddProvider_Provider_Type_2 ()
783                 {
784                         var provider = new MyTypeDescriptionProvider ();
785                         TypeDescriptor.AddProvider (provider, (Type) null);
786                 }
787
788                 [Test]
789                 public void TestAddProvider_Provider_Type_3 ()
790                 {
791                         var type = typeof (MyComponent);
792                         var providers = new MyTypeDescriptionProvider[] {
793                                 new MyTypeDescriptionProvider ("One"),
794                                 new MyTypeDescriptionProvider ("Two"),
795                                 new MyTypeDescriptionProvider ("Three"),
796                                 new MyTypeDescriptionProvider ("Four")
797                         };
798
799                         try {
800                                 TypeDescriptionProvider provider;
801                                 ICustomTypeDescriptor descriptor;
802
803                                 TypeDescriptor.AddProvider (providers[0], type);
804                                 provider = TypeDescriptor.GetProvider (type);
805                                 Assert.IsNotNull (provider, "#A1");
806                                 descriptor = provider.GetTypeDescriptor (type);
807                                 Assert.IsNotNull (descriptor, "#A1-1");
808                                 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2");
809                                 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3");
810
811                                 TypeDescriptor.AddProvider (providers[1], type);
812                                 provider = TypeDescriptor.GetProvider (type);
813                                 Assert.IsNotNull (provider, "#B1");
814                                 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
815                                 Assert.IsNotNull (descriptor, "#B1-1");
816                                 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2");
817
818                                 // Providers are stored in a stack according to docs, but it's in reality
819                                 // a FIFO linked list
820                                 TypeDescriptor.AddProvider (providers[2], type);
821                                 TypeDescriptor.AddProvider (providers[3], type);
822                                 provider = TypeDescriptor.GetProvider (type);
823                                 Assert.IsNotNull (provider, "#C1");
824                                 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
825                                 Assert.IsNotNull (descriptor, "#C1-1");
826                                 Assert.AreEqual ("Four", descriptor.GetClassName (), "#C1-2");
827
828                                 TypeDescriptor.RemoveProvider (providers[2], type);
829                                 provider = TypeDescriptor.GetProvider (type);
830                                 Assert.IsNotNull (provider, "#D1");
831                                 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
832                                 Assert.IsNotNull (descriptor, "#D1-1");
833                                 Assert.AreEqual ("Four", descriptor.GetClassName (), "#D1-2");
834
835                                 TypeDescriptor.RemoveProvider (providers[3], type);
836                                 provider = TypeDescriptor.GetProvider (type);
837                                 Assert.IsNotNull (provider, "#E1");
838                                 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
839                                 Assert.IsNotNull (descriptor, "#E1-1");
840                                 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2");
841
842                         } finally {
843                                 TypeDescriptor.RemoveProvider (providers[0], type);
844                                 TypeDescriptor.RemoveProvider (providers[1], type);
845                                 TypeDescriptor.RemoveProvider (providers[2], type);
846                                 TypeDescriptor.RemoveProvider (providers[3], type);
847                         }
848                 }
849
850                 [Test]
851                 [ExpectedException (typeof (ArgumentNullException))]
852                 public void TestGetProvider_Type_1 ()
853                 {
854                         TypeDescriptor.GetProvider ((Type)null);
855                 }
856
857                 [Test]
858                 public void TestGetProvider_Type_2 ()
859                 {
860                         TypeDescriptionProvider provider = TypeDescriptor.GetProvider (typeof (string));
861                         Assert.IsNotNull (provider, "#A1");
862                         provider = new MyTypeDescriptionProvider ("One");
863
864                         try {
865                                 TypeDescriptor.AddProvider (provider, typeof (string));
866                                 ICustomTypeDescriptor descriptor = provider.GetTypeDescriptor (typeof (string));
867                                 Assert.IsNotNull (descriptor, "#B1");
868                                 Assert.AreEqual ("One", descriptor.GetClassName (), "#B1-1");
869                         } finally {
870                                 TypeDescriptor.RemoveProvider (provider, typeof (string));
871                         }
872                 }
873
874                 [Test]
875                 [ExpectedException (typeof (ArgumentNullException))]
876                 public void TestGetProvider_Instance_1 ()
877                 {
878                         TypeDescriptor.GetProvider ((object) null);
879                 }
880
881                 [Test]
882                 public void TestGetProvider_Instance_2 ()
883                 {
884                         var instance = new MyComponent ();
885                         TypeDescriptionProvider provider = TypeDescriptor.GetProvider (instance);
886                         Assert.IsNotNull (provider, "#A1");
887                         provider = new MyTypeDescriptionProvider ("One");
888
889                         try {
890                                 TypeDescriptor.AddProvider (provider, instance);
891                                 ICustomTypeDescriptor descriptor = provider.GetTypeDescriptor (instance);
892                                 Assert.IsNotNull (descriptor, "#B1");
893                                 Assert.AreEqual ("One", descriptor.GetClassName (), "#B1-1");
894                         } finally {
895                                 TypeDescriptor.RemoveProvider (provider, instance);
896                         }
897                 }
898
899                 [Test]
900                 [ExpectedException (typeof (ArgumentNullException))]
901                 public void TestRemoveProvider_Provider_Type_1 ()
902                 {
903                         TypeDescriptor.RemoveProvider (null, (Type)null);
904                 }
905
906                 [Test]
907                 [ExpectedException (typeof (ArgumentNullException))]
908                 public void TestRemoveProvider_Provider_Type_2 ()
909                 {
910                         var provider = new MyTypeDescriptionProvider ();
911                         TypeDescriptor.RemoveProvider (provider, null);
912                 }
913
914                 [Test]
915                 public void TestRemoveProvider_Provider_Type_3 ()
916                 {
917                         var provider = new MyTypeDescriptionProvider ();
918                         bool refreshedCalled = false;
919                         bool refreshedCorrectComponentChanged = false;
920                         bool refreshedCorrectTypeChanged = false;
921
922                         RefreshEventHandler handler = (RefreshEventArgs args) => {
923                                 refreshedCalled = true;
924                                 refreshedCorrectComponentChanged = args.ComponentChanged == null;
925                                 refreshedCorrectTypeChanged = args.TypeChanged == typeof (string);
926                         };
927
928                         try {
929                                 TypeDescriptor.Refreshed += handler;
930
931                                 TypeDescriptor.RemoveProvider (provider, typeof (string));
932                                 Assert.AreEqual (true, refreshedCalled, "#A1");
933                                 Assert.AreEqual (true, refreshedCorrectComponentChanged, "#A2");
934                                 Assert.AreEqual (true, refreshedCorrectTypeChanged, "#A3");
935                         } finally {
936                                 TypeDescriptor.Refreshed -= handler;
937                         }
938                 }
939
940                 [Test]
941                 [ExpectedException (typeof (ArgumentNullException))]
942                 public void TestRemoveProvider_Provider_Instance_1 ()
943                 {
944                         TypeDescriptor.RemoveProvider (null, (object)null);
945                 }
946
947                 [Test]
948                 [ExpectedException (typeof (ArgumentNullException))]
949                 public void TestRemoveProvider_Provider_Instance_2 ()
950                 {
951                         var provider = new MyTypeDescriptionProvider ();
952                         TypeDescriptor.RemoveProvider (provider, (object)null);
953                 }
954
955                 [Test]
956                 public void TestRemoveProvider_Provider_Instance_3 ()
957                 {
958                         var instance = new MyComponent ();
959                         var provider = new MyTypeDescriptionProvider ();
960                         bool refreshedCalled = false;
961                         bool refreshedCorrectComponentChanged = false;
962                         bool refreshedCorrectTypeChanged = false;
963
964                         RefreshEventHandler handler = (RefreshEventArgs args) => {
965                                 refreshedCalled = true;
966                                 refreshedCorrectComponentChanged = args.ComponentChanged == instance;
967                                 refreshedCorrectTypeChanged = args.TypeChanged == typeof (MyComponent);
968                         };
969
970                         try {
971                                 TypeDescriptor.Refreshed += handler;
972
973                                 TypeDescriptor.RemoveProvider (provider, instance);
974                                 Assert.AreEqual (true, refreshedCalled, "#A1");
975                                 Assert.AreEqual (true, refreshedCorrectComponentChanged, "#A2");
976                                 Assert.AreEqual (true, refreshedCorrectTypeChanged, "#A3");
977                         } finally {
978                                 TypeDescriptor.Refreshed -= handler;
979                         }
980                 }
981
982                 [Test]
983                 [ExpectedException (typeof (ArgumentNullException))]
984                 public void TestGetReflectionType_Type_1 ()
985                 {
986                         TypeDescriptor.GetReflectionType ((Type) null);
987                 }
988
989                 [Test]
990                 public void TestGetReflectionType_Type_2 ()
991                 {
992                         Type type = TypeDescriptor.GetReflectionType (typeof (string));
993                         Assert.IsNotNull (type, "#A1");
994                         Assert.AreEqual (typeof (string), type, "#A1-1");
995
996                         type = TypeDescriptor.GetReflectionType (typeof (MyComponent));
997                         Assert.IsNotNull (type, "#B1");
998                         Assert.AreEqual (typeof (MyComponent), type, "#B1-1");
999
1000                         type = TypeDescriptor.GetReflectionType (typeof (List<string>));
1001                         Assert.IsNotNull (type, "#C1");
1002                         Assert.AreEqual (typeof (List <string>), type, "#C1-1");
1003
1004                         type = TypeDescriptor.GetReflectionType (typeof (IList<>));
1005                         Assert.IsNotNull (type, "#D1");
1006                         Assert.AreEqual (typeof (IList<>), type, "#D1-1");
1007
1008                         type = TypeDescriptor.GetReflectionType (typeof (IDictionary<,>));
1009                         Assert.IsNotNull (type, "#E1");
1010                         Assert.AreEqual (typeof (IDictionary<,>), type, "#E1-1");
1011                 }
1012
1013                 [Test]
1014                 [ExpectedException (typeof (ArgumentNullException))]
1015                 public void TestGetReflectionType_Instance_1 ()
1016                 {
1017                         TypeDescriptor.GetReflectionType ((object) null);
1018                 }
1019
1020                 [Test]
1021                 public void TestGetReflectionType_Instance_2 ()
1022                 {
1023                         string s = "string";
1024                         Type type = TypeDescriptor.GetReflectionType (s);
1025                         Assert.IsNotNull (type, "#A1");
1026                         Assert.AreEqual (typeof (string), type, "#A1-1");
1027
1028                         var mc = new MyComponent ();
1029                         type = TypeDescriptor.GetReflectionType (mc);
1030                         Assert.IsNotNull (type, "#B1");
1031                         Assert.AreEqual (typeof (MyComponent), type, "#B1-1");
1032
1033                         var l = new List<string> ();
1034                         type = TypeDescriptor.GetReflectionType (l);
1035                         Assert.IsNotNull (type, "#C1");
1036                         Assert.AreEqual (typeof (List<string>), type, "#C1-1");
1037
1038                         IList il = new List<string> ();
1039                         type = TypeDescriptor.GetReflectionType (il);
1040                         Assert.IsNotNull (type, "#D1");
1041                         Assert.AreEqual (typeof (List<string>), type, "#D1-1");
1042
1043                         IDictionary id = new Dictionary<string, object> ();
1044                         type = TypeDescriptor.GetReflectionType (id);
1045                         Assert.IsNotNull (type, "#E1");
1046                         Assert.AreEqual (typeof (Dictionary<string,object>), type, "#E1-1");
1047
1048                         object o = 1;
1049                         type = TypeDescriptor.GetReflectionType (o);
1050                         Assert.IsNotNull (type, "#F1");
1051                         Assert.AreEqual (typeof (int), type, "#F1-1");
1052                 }
1053
1054                 [Test]
1055                 public void TestICustomTypeDescriptor ()
1056                 {
1057                         TestCustomTypeDescriptor test = new TestCustomTypeDescriptor ();
1058
1059                         PropertyDescriptorCollection props;
1060                         PropertyDescriptor prop;
1061                         EventDescriptorCollection events;
1062
1063                         test.ResetMethodsCalled ();
1064                         props = TypeDescriptor.GetProperties (test);
1065                         Assert.AreEqual ("5", test.methods_called, "#1");
1066
1067                         test.ResetMethodsCalled ();
1068                         props = TypeDescriptor.GetProperties (test, new Attribute[0]);
1069                         Assert.AreEqual ("4", test.methods_called, "#2");
1070
1071                         test.ResetMethodsCalled ();
1072                         props = TypeDescriptor.GetProperties (test, new Attribute[0], false);
1073                         Assert.AreEqual ("4", test.methods_called, "#3");
1074
1075                         test.ResetMethodsCalled ();
1076                         props = TypeDescriptor.GetProperties (test, false);
1077                         Assert.AreEqual ("5", test.methods_called, "#4");
1078
1079                         test.ResetMethodsCalled ();
1080                         prop = TypeDescriptor.GetDefaultProperty (test);
1081                         Assert.AreEqual ("6", test.methods_called, "#5");
1082
1083                         test.ResetMethodsCalled ();
1084                         events = TypeDescriptor.GetEvents (test);
1085                         Assert.AreEqual ("2", test.methods_called, "#6");
1086
1087                         test.ResetMethodsCalled ();
1088                         events = TypeDescriptor.GetEvents (test, new Attribute[0]);
1089                         Assert.AreEqual ("1", test.methods_called, "#7");
1090
1091                         test.ResetMethodsCalled ();
1092                         events = TypeDescriptor.GetEvents (test, false);
1093                         Assert.AreEqual ("2", test.methods_called, "#8");
1094                 }
1095
1096                 [Test]
1097                 public void TestCreateDesigner ()
1098                 {
1099                         IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
1100                         Assert.IsTrue (des is MyDesigner, "#1");
1101                         
1102                         des = TypeDescriptor.CreateDesigner (com, typeof(string));
1103                         Assert.IsNull (des, "#2");
1104                 }
1105                 
1106                 [Test]
1107                 public void TestCreateEvent ()
1108                 {
1109                         EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
1110                         Assert.AreEqual (typeof (MyComponent), ed.ComponentType, "#1");
1111                         Assert.AreEqual (typeof (EventHandler), ed.EventType, "#2");
1112                         Assert.IsTrue (ed.IsMulticast, "#3");
1113                         Assert.AreEqual ("AnEvent", ed.Name, "#4");
1114                 }
1115                 
1116                 [Test]
1117                 public void TestCreateProperty ()
1118                 {
1119                         PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
1120                         Assert.AreEqual (typeof (MyComponent), pd.ComponentType, "#1");
1121                         Assert.AreEqual ("TestProperty", pd.Name, "#2");
1122                         Assert.AreEqual (typeof (string), pd.PropertyType, "#3");
1123                         Assert.IsFalse (pd.IsReadOnly, "#4");
1124                         
1125                         pd.SetValue (com, "hi");
1126                         Assert.AreEqual ("hi", pd.GetValue (com), "#5");
1127                 }
1128                 
1129                 [Test]
1130                 public void TestGetAttributes ()
1131                 {
1132                         AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
1133                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#A1");
1134                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#A2");
1135                         Assert.IsNull (col [typeof (EditorAttribute)], "#A3");
1136                         
1137                         col = TypeDescriptor.GetAttributes (com);
1138                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#B1");
1139                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#B2");
1140                         Assert.IsNull (col [typeof (EditorAttribute)], "#B3");
1141                         
1142                         col = TypeDescriptor.GetAttributes (sitedcom);
1143                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#C1");
1144                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#C2");
1145                         Assert.IsNotNull (col [typeof (EditorAttribute)], "#C3");
1146
1147                         col = TypeDescriptor.GetAttributes (nfscom);
1148                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#D1");
1149                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#D2");
1150                         Assert.IsNull (col [typeof (EditorAttribute)], "#D3");
1151
1152                         col = TypeDescriptor.GetAttributes (typeof (MyDerivedComponent));
1153                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#E1");
1154                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#E2");
1155                         DesignerAttribute attribute = col[typeof(DesignerAttribute)] as DesignerAttribute;
1156                         Assert.IsNotNull (attribute, "#E3");
1157                         // there are multiple DesignerAttribute present and their order in the collection isn't deterministic
1158                         bool found = false;
1159                         for (int i = 0; i < col.Count; i++) {
1160                                 attribute = (col [i] as DesignerAttribute);
1161                                 if (attribute != null) {
1162                                         found = typeof(MyOtherDesigner).AssemblyQualifiedName == attribute.DesignerTypeName;
1163                                         if (found)
1164                                                 break;
1165                                 }
1166                         }
1167                         Assert.IsTrue (found, "#E4");
1168
1169                         // Shows that attributes are retrieved from the current type, the base types 
1170                         // and the implemented by the type interfaces.
1171                         Assert.AreEqual (3, TypeDescriptor.GetAttributes (typeof (TestDerivedClass)).Count, "#F1");
1172                 }
1173                 
1174                 [Test]
1175                 public void TestGetClassName ()
1176                 {
1177                         Assert.AreEqual (typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
1178                 }
1179                 
1180                 [Test]
1181                 public void TestGetComponentName ()
1182                 {
1183                         // in MS.NET 2.0, GetComponentName no longer returns
1184                         // the type name if there's no custom typedescriptor
1185                         // and no site
1186                         Assert.IsNull (TypeDescriptor.GetComponentName (com), "#1");
1187                         Assert.IsNull (TypeDescriptor.GetComponentName (com, false), "#2");
1188                         Assert.IsNull (TypeDescriptor.GetComponentName (new Exception ()), "#3");
1189                         Assert.IsNull (TypeDescriptor.GetComponentName (new Exception (), false), "#4");
1190                         Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception)), "#4");
1191                         Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception), false), "#6");
1192                         Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#7");
1193                         Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#8");
1194                 }
1195
1196                 [Test]
1197                 [ExpectedException (typeof (ArgumentNullException))]
1198                 public void TestGetConverterNullParam ()
1199                 {
1200                         TypeDescriptor.GetConverter (null);
1201                 }
1202                 
1203                 [Test]
1204                 public void TestGetConverter ()
1205                 {
1206                         Assert.AreEqual (typeof (BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType (), "#1");
1207                         Assert.AreEqual (typeof (ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType (), "#2");
1208                         Assert.AreEqual (typeof (SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType (), "#3");
1209                         Assert.AreEqual (typeof (StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType (), "#4");
1210                         Assert.AreEqual (typeof (CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType (), "#5");
1211                         Assert.AreEqual (typeof (Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType (), "#6");
1212                         Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType (), "#7");
1213                         Assert.AreEqual (typeof (Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType (), "#8");
1214                         Assert.AreEqual (typeof (UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType (), "#9");
1215                         Assert.AreEqual (typeof (UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType (), "#10");
1216                         Assert.AreEqual (typeof (UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType (), "#11");
1217                         Assert.AreEqual (typeof (SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType (), "#12");
1218                         Assert.AreEqual (typeof (DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType (), "#13");
1219                         Assert.AreEqual (typeof (DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType (), "#14");
1220                         Assert.AreEqual (typeof (ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType (), "#15");
1221                         Assert.AreEqual (typeof (CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType (), "#16");
1222                         Assert.AreEqual (typeof (DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType (), "#17");
1223                         Assert.AreEqual (typeof (GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType (), "#18");
1224                         Assert.AreEqual (typeof (TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType (), "#19");
1225                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType (), "#20");
1226
1227                         // Tests from bug #71444
1228                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType (), "#21");
1229                         Assert.AreEqual (typeof (ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType (), "#22");
1230                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType (), "#23");
1231                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType (), "#24");
1232
1233                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType (), "#25");
1234                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType (), "#26");
1235                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType (), "#27");
1236
1237                         // Test from bug #76686
1238                         Assert.AreEqual  (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType (), "#28");
1239 #if MOBILE
1240                         Assert.IsFalse (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29");
1241                         Assert.IsFalse (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30");
1242 #else
1243                         Assert.IsTrue (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29");
1244                         Assert.IsTrue (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30");
1245 #endif
1246                         Assert.AreEqual (typeof (NullableConverter), TypeDescriptor.GetConverter (typeof (int?)).GetType (), "#31");
1247                 }
1248                 
1249                 [Test]
1250                 public void TestGetDefaultEvent ()
1251                 {
1252                         EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
1253                         Assert.IsNull ( des, "#A");
1254                         
1255                         des = TypeDescriptor.GetDefaultEvent (com);
1256                         Assert.IsNull (des, "#B");
1257
1258                         des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
1259                         Assert.IsNotNull (des, "#C1");
1260                         Assert.AreEqual ("AnotherEvent", des.Name, "#C2");
1261
1262                         des = TypeDescriptor.GetDefaultEvent (anothercom);
1263                         Assert.IsNotNull (des, "#D1");
1264                         Assert.AreEqual ("AnotherEvent", des.Name, "#D2");
1265
1266                         des = TypeDescriptor.GetDefaultEvent (sitedcom);
1267                         Assert.IsNull (des, "#E1");
1268
1269                         des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
1270                         Assert.IsNotNull (des, "#F1");
1271                         Assert.AreEqual ("AnEvent", des.Name, "#F2");
1272
1273                         des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
1274                         Assert.IsNotNull (des, "#G1");
1275                         Assert.AreEqual ("AnEvent", des.Name, "#G2");
1276                 }
1277                 
1278                 [Test]
1279                 public void TestGetDefaultProperty ()
1280                 {
1281                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
1282                         Assert.IsNull (des, "#A");
1283                         
1284                         des = TypeDescriptor.GetDefaultProperty (com);
1285                         Assert.IsNull (des, "#B");
1286
1287                         des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
1288                         Assert.IsNotNull (des, "#C1");
1289                         Assert.AreEqual ("AnotherProperty", des.Name, "#C2");
1290
1291                         des = TypeDescriptor.GetDefaultProperty (anothercom);
1292                         Assert.IsNotNull (des, "#D1");
1293                         Assert.AreEqual ("AnotherProperty", des.Name, "#D2");
1294                 }
1295                 
1296                 [Test]
1297                 public void TestGetDefaultProperty2 ()
1298                 {
1299                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
1300                         Assert.IsNull (des, "#A");
1301
1302                         des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
1303                         Assert.IsNotNull (des, "#B1");
1304                         Assert.AreEqual ("TestProperty", des.Name, "#B2");
1305
1306                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
1307                         Assert.IsNotNull (des, "#C1");
1308                         Assert.AreEqual ("TestProperty", des.Name, "#C2");
1309
1310                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
1311                         Assert.IsNull (des, "#D");
1312                 }
1313
1314                 [Test]
1315                 public void TestGetEvents ()
1316                 {
1317                         EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
1318                         Assert.AreEqual (3, col.Count, "#A1");
1319                         Assert.IsNotNull (col.Find ("AnEvent", true), "#A2");
1320                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#A3");
1321                         Assert.IsNotNull (col.Find ("Disposed", true), "#A4");
1322                         
1323                         col = TypeDescriptor.GetEvents (com);
1324                         Assert.AreEqual (3, col.Count, "#B1");
1325                         Assert.IsNotNull (col.Find ("AnEvent", true), "#B2");
1326                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#B3");
1327                         Assert.IsNotNull (col.Find ("Disposed", true), "#B4");
1328                         
1329                         col = TypeDescriptor.GetEvents (sitedcom);
1330                         Assert.AreEqual (2, col.Count, "#C1");
1331                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#C2");
1332                         Assert.IsNotNull (col.Find ("Disposed", true), "#C3");
1333
1334                         col = TypeDescriptor.GetEvents (nfscom);
1335                         Assert.AreEqual (3, col.Count, "#D1");
1336                         Assert.IsNotNull (col.Find ("AnEvent", true), "#D2");
1337                         Assert.IsNotNull ( col.Find ("AnotherEvent", true), "#D3");
1338                         Assert.IsNotNull (col.Find ("Disposed", true), "#D4");
1339
1340                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
1341                         
1342                         col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
1343                         Assert.AreEqual (1, col.Count, "#E1");
1344                         Assert.IsNotNull (col.Find ("AnEvent", true), "#E2");
1345                         
1346                         col = TypeDescriptor.GetEvents (com, filter);
1347                         Assert.AreEqual (1, col.Count, "#F1");
1348                         Assert.IsNotNull (col.Find ("AnEvent", true), "#F2");
1349                         
1350                         col = TypeDescriptor.GetEvents (sitedcom, filter);
1351                         Assert.AreEqual (0, col.Count, "#G");
1352
1353                         col = TypeDescriptor.GetEvents (nfscom, filter);
1354                         Assert.AreEqual (1, col.Count, "#H1");
1355                         Assert.IsNotNull (col.Find ("AnEvent", true), "#H2");
1356
1357                         col = TypeDescriptor.GetEvents (typeof (IFoo));
1358                         Assert.AreEqual (2, col.Count, "#I1");
1359                         Assert.IsNotNull (col.Find ("Fired", true), "#I2");
1360                         Assert.IsNotNull (col.Find ("Fired", false), "#I3");
1361                         Assert.IsNotNull (col.Find ("fired", true), "#I4");
1362                         Assert.IsNull (col.Find ("fired", false), "#I5");
1363                         Assert.IsNotNull (col.Find ("Closed", true), "#I6");
1364
1365                         col = TypeDescriptor.GetEvents (typeof (IBar));
1366                         Assert.AreEqual (1, col.Count, "#J1");
1367                         Assert.IsNull (col.Find ("Fired", true), "#J2");
1368                         Assert.IsNull (col.Find ("Closed", true), "#J3");
1369                         Assert.IsNotNull (col.Find ("Destroyed", true), "#J4");
1370                 }
1371                 
1372                 [Test]
1373                 public void TestGetProperties ()
1374                 {
1375                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
1376                         Assert.IsNotNull (col.Find ("TestProperty", true), "#A1");
1377                         Assert.IsNotNull ( col.Find ("AnotherProperty", true), "#A2");
1378                         
1379                         col = TypeDescriptor.GetProperties (com);
1380                         Assert.IsNotNull (col.Find ("TestProperty", true), "#B1");
1381                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#B2");
1382
1383                         col = TypeDescriptor.GetProperties (nfscom);
1384                         Assert.IsNotNull (col.Find ("TestProperty", true), "#C1");
1385                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#C2");
1386
1387                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
1388                         
1389                         col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
1390                         Assert.IsNotNull (col.Find ("TestProperty", true), "#D1");
1391                         Assert.IsNull (col.Find ("AnotherProperty", true), "#D2");
1392                         
1393                         col = TypeDescriptor.GetProperties (com, filter);
1394                         Assert.IsNotNull (col.Find ("TestProperty", true), "#E1");
1395                         Assert.IsNull (col.Find ("AnotherProperty", true), "#E2");
1396
1397                         col = TypeDescriptor.GetProperties (nfscom, filter);
1398                         Assert.IsNotNull (col.Find ("TestProperty", true), "#F1");
1399                         Assert.IsNull (col.Find ("AnotherProperty", true), "#F2");
1400
1401
1402                         // GetProperties should return only the last type's implementation of a
1403                         // property with a matching name in the base types. E.g in the case where 
1404                         // the "new" keyword is used.
1405                         //
1406                         PropertyDescriptorCollection derivedCol = TypeDescriptor.GetProperties (typeof(MyDerivedComponent));
1407                         Assert.IsNotNull (derivedCol["AnotherProperty"].Attributes[typeof (DescriptionAttribute)], "#G1");
1408                         int anotherPropsFound = 0;
1409                         int yetAnotherPropsFound = 0;
1410                         foreach (PropertyDescriptor props in derivedCol) {
1411                                 if (props.Name == "AnotherProperty")
1412                                         anotherPropsFound++;
1413                                 else if (props.Name == "YetAnotherProperty")
1414                                         yetAnotherPropsFound++;
1415                         }
1416
1417                         // GetProperties does not return the base type property in the case 
1418                         // where both the "new" keyword is used and also the Property type is different 
1419                         // (Type.GetProperties does return both properties)
1420                         //
1421                         Assert.AreEqual (1, anotherPropsFound, "#G2");
1422                         Assert.IsNotNull (derivedCol["AnotherProperty"].Attributes[typeof (DescriptionAttribute)], "#G3");
1423                         Assert.AreEqual (1, yetAnotherPropsFound, "#G4");
1424
1425                         // Verify that we return the derived "new" property when 
1426                         // filters are applied that match the parent property
1427                         // 
1428                         PropertyDescriptorCollection filteredCol = TypeDescriptor.GetProperties (typeof(MyDerivedComponent), 
1429                                                                                                  new Attribute[] { BrowsableAttribute.Yes });
1430                         Assert.IsNotNull (filteredCol["YetAnotherProperty"], "#G5");
1431
1432                         // GetProperties does not return write-only properties (ones without a getter)
1433                         // 
1434                         Assert.IsNull (col.Find ("WriteOnlyProperty", true), "#H1");
1435
1436                         col = TypeDescriptor.GetProperties (typeof (IFoo));
1437                         Assert.AreEqual (1, col.Count, "#I1");
1438                         Assert.IsNotNull (col.Find ("HasFired", true), "#I1");
1439                         Assert.IsNotNull (col.Find ("HasFired", false), "#I2");
1440                         Assert.IsNotNull (col.Find ("hasFired", true), "#I3");
1441                         Assert.IsNull (col.Find ("hasFired", false), "#I4");
1442
1443                         col = TypeDescriptor.GetProperties (typeof (IBar));
1444                         Assert.AreEqual (1, col.Count, "#J1");
1445                         Assert.IsNull (col.Find ("HasFired", true), "#J2");
1446                         Assert.IsNotNull (col.Find ("IsDestroyed", true), "#J3");
1447                         Assert.IsNotNull (col.Find ("IsDestroyed", false), "#J4");
1448                         Assert.IsNotNull (col.Find ("isDestroyed", true), "#J5");
1449                         Assert.IsNull (col.Find ("isDestroyed", false), "#J6");
1450                 }
1451
1452                 [Test]
1453                 public void TestGetProperties2 ()
1454                 {
1455                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
1456                         Assert.IsNull (col.Find ("TestProperty", true), "#A1");
1457                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#A2");
1458
1459                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
1460                         col = TypeDescriptor.GetProperties (sitedcom, filter);
1461                         Assert.IsNull (col.Find ("TestProperty", true), "#B1");
1462                         Assert.IsNull (col.Find ("AnotherProperty", true), "#B2");
1463                 }
1464
1465                 [Test]
1466                 public void GetProperties_Order ()
1467                 {
1468                         MyComponent com = new MyComponent (new MyContainer ());
1469
1470                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (com);
1471                         Assert.AreEqual (8, col.Count, "#1");
1472                         Assert.AreEqual ("TestProperty", col [0].Name, "#2");
1473                         Assert.AreEqual ("AnotherProperty", col [1].Name, "#3");
1474                         Assert.AreEqual ("YetAnotherProperty", col [2].Name, "#4");
1475                         Assert.AreEqual ("Name", col [3].Name, "#5");
1476                         Assert.AreEqual ("Address", col [4].Name, "#6");
1477                         Assert.AreEqual ("Country", col [5].Name, "#7");
1478                         Assert.AreEqual ("Site", col [6].Name, "#8");
1479                         Assert.AreEqual ("Container", col [7].Name, "#9");
1480                 }
1481
1482                 [TypeConverter (typeof (TestConverter))]
1483                 class TestConverterClass {
1484                 }
1485
1486                 class TestConverter : TypeConverter {
1487                         public Type Type;
1488
1489                         public TestConverter (Type type)
1490                         {
1491                                 this.Type = type;
1492                         }
1493                 }
1494
1495                 [Test]
1496                 public void TestConverterCtorWithArgument ()
1497                 {
1498                         TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass));
1499                         Assert.IsNotNull (t.GetType (), "#A1");
1500                         Assert.AreEqual (typeof (TestConverter), t.GetType (), "#A2");
1501                         TestConverter converter = (TestConverter) t;
1502                         Assert.AreEqual (typeof (TestConverterClass), converter.Type, "#B");
1503                 }
1504
1505                 [Test]
1506                 public void GetPropertiesIgnoreIndexers ()
1507                 {
1508                         PropertyDescriptorCollection pc =
1509                                 TypeDescriptor.GetProperties (typeof (string));
1510                         // There are two string properties: Length and Chars.
1511                         // Chars is an indexer.
1512                         //
1513                         // Future version of CLI might contain some additional
1514                         // properties. In that case simply increase the
1515                         // number. (Also, it is fine to just remove #2.)
1516                         Assert.AreEqual (1, pc.Count, "#1");
1517                         Assert.AreEqual ("Length", pc [0].Name, "#2");
1518                 }
1519
1520 #if NET_4_0
1521                 [Test]
1522                 public void InterfaceType ()
1523                 {
1524                         Type interface_type = TypeDescriptor.InterfaceType;
1525                         Assert.AreEqual ("TypeDescriptorInterface", interface_type.Name, "#A0");
1526                         Assert.IsTrue (interface_type.IsClass, "#A1");
1527                         Assert.IsTrue (interface_type.IsSealed, "#A2");
1528                         Assert.AreEqual (typeof (object), interface_type.BaseType, "#A3");
1529                         Assert.IsFalse (interface_type.IsInterface, "#A4");
1530                         Assert.IsFalse (interface_type.IsPublic, "#A5");
1531                 }
1532 #endif
1533         }
1534 }