* Makefile.am: Build `docs` after `runtime`, so that it can depend
[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 NUnit.Framework;
18
19 namespace MonoTests.System.ComponentModel
20 {
21         interface IFoo
22         {
23                 event EventHandler Fired;
24                 event EventHandler Closed;
25
26                 bool HasFired {
27                         get;
28                 }
29         }
30
31         interface IBar : IFoo
32         {
33                 event EventHandler Destroyed;
34
35                 bool IsDestroyed {
36                         get;
37                 }
38         }
39
40         class MyDesigner: IDesigner
41         {
42                 public MyDesigner()
43                 {
44                 }
45
46                 public IComponent Component {get{return null; }}
47
48                 public DesignerVerbCollection Verbs {get{return null; }}
49
50                 public void DoDefaultAction () { }
51
52                 public void Initialize (IComponent component) { }
53
54                 public void Dispose () { }
55         }
56
57         class MyOtherDesigner: IDesigner
58         {
59                 public MyOtherDesigner()
60                 {
61                 }
62
63                 public IComponent Component {get {return null; } }
64                 public DesignerVerbCollection Verbs { get {return null; } }
65                 public void DoDefaultAction () { }
66                 public void Initialize (IComponent component) { }
67                 public void Dispose () { }
68         }
69         
70         class MySite: ISite
71         { 
72                 public IComponent Component { get {  return null; } }
73
74                 public IContainer Container { get {  return null; } }
75
76                 public bool DesignMode { get {  return true; } }
77
78                 public string Name { get { return "TestName"; } set { } }
79
80                 public object GetService (Type t)
81                 {
82                         if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
83                         return null;
84                 }
85         }
86         
87         class MyFilter: ITypeDescriptorFilterService
88         {
89                 public bool FilterAttributes (IComponent component,IDictionary attributes)
90                 {
91                         Attribute ea = new DefaultEventAttribute ("AnEvent");
92                         attributes [ea.TypeId] = ea;
93                         ea = new DefaultPropertyAttribute ("TestProperty");
94                         attributes [ea.TypeId] = ea;
95                         ea = new EditorAttribute ();
96                         attributes [ea.TypeId] = ea;
97                         return true;
98                 }
99                 
100                 public bool FilterEvents (IComponent component, IDictionary events)
101                 {
102                         events.Remove ("AnEvent");
103                         return true;
104                 }
105                 
106                 public bool FilterProperties (IComponent component, IDictionary properties)
107                 {
108                         properties.Remove ("TestProperty");
109                         return true;
110                 }
111         }
112
113         class AnotherSite: ISite
114         { 
115                 public IComponent Component { get {  return null; } }
116
117                 public IContainer Container { get {  return null; } }
118
119                 public bool DesignMode { get {  return true; } }
120
121                 public string Name { get { return "TestName"; } set { } }
122
123                 public object GetService (Type t)
124                 {
125                         if (t == typeof(ITypeDescriptorFilterService)) {
126                                 return new AnotherFilter ();
127                         }
128                         return null;
129                 }
130         }
131
132         class NoFilterSite : ISite
133         {
134                 public NoFilterSite () : this (null)
135                 {
136                 }
137
138                 public NoFilterSite (IContainer container)
139                 {
140                         _container = container;
141                 }
142
143                 public IComponent Component {
144                         get { return null; }
145                 }
146
147                 public IContainer Container {
148                         get { return _container; }
149                 }
150
151                 public bool DesignMode { get { return true; } }
152
153                 public string Name { get { return "TestName"; } set { } }
154
155                 public object GetService (Type t)
156                 {
157                         return null;
158                 }
159
160                 public IContainer _container;
161         }
162
163         class MyContainer : IContainer
164         {
165                 public MyContainer ()
166                 {
167                         _components = new ComponentCollection (new IComponent [0]);
168                 }
169
170                 public ComponentCollection Components {
171                         get { return _components; }
172                 }
173
174                 public void Add (IComponent component)
175                 {
176                 }
177
178                 public void Add (IComponent component, string name)
179                 {
180                 }
181
182                 public void Dispose ()
183                 {
184                 }
185
186                 public void Remove (IComponent component)
187                 {
188                 }
189
190                 private ComponentCollection _components;
191         }
192
193         class AnotherFilter: ITypeDescriptorFilterService
194         {
195                 public bool FilterAttributes (IComponent component,IDictionary attributes) {
196                         Attribute ea = new DefaultEventAttribute ("AnEvent");
197                         attributes [ea.TypeId] = ea;
198                         ea = new DefaultPropertyAttribute ("TestProperty");
199                         attributes [ea.TypeId] = ea;
200                         ea = new EditorAttribute ();
201                         attributes [ea.TypeId] = ea;
202                         return true;
203                 }
204
205                 public bool FilterEvents (IComponent component, IDictionary events) {
206                         return true;
207                 }
208
209                 public bool FilterProperties (IComponent component, IDictionary properties) {
210                         return true;
211                 }
212         }
213
214         [DescriptionAttribute ("my test component")]
215         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
216         public class MyComponent: Component
217         {
218                 string prop;
219                 
220                 [DescriptionAttribute ("test")]
221                 public event EventHandler AnEvent;
222                 
223                 public event EventHandler AnotherEvent;
224                 
225                 public MyComponent  ()
226                 {
227                 }
228                 
229                 public MyComponent (ISite site)
230                 {
231                         Site = site;
232                 }
233
234                 public MyComponent (IContainer container)
235                 {
236                         container.Add (this);
237                 }
238
239                 [DescriptionAttribute ("test")]
240                 public virtual string TestProperty
241                 {
242                         get { return prop; }
243                         set { prop = value; }
244                 }
245                 
246                 public string AnotherProperty
247                 {
248                         get { return prop; }
249                         set { prop = value; }
250                 }
251
252                 [Browsable (false)]
253                 public string YetAnotherProperty
254                 {
255                         get { return null; }
256                 }
257
258                 public string Name {
259                         get { return null; }
260                 }
261
262                 public string Address {
263                         get { return null; }
264                 }
265
266                 public string Country {
267                         get { return null; }
268                 }
269
270                 private string HairColor {
271                         get { return null; }
272                 }
273
274                 protected int Weight {
275                         get { return 5; }
276                 }
277
278                 internal int Height {
279                         get { return 0; }
280                 }
281
282                 public string WriteOnlyProperty {
283                         set { prop = value; }
284                 }
285         }
286
287         [DescriptionAttribute ("my test derived component")]
288         [DesignerAttribute (typeof(MyOtherDesigner))]
289         public class MyDerivedComponent: MyComponent
290         {
291                 string prop;
292                 
293                 public MyDerivedComponent  ()
294                 {
295                 }
296                 
297                 public MyDerivedComponent (ISite site) : base (site)
298                 {
299                 }
300                 
301                 [DescriptionAttribute ("test derived")]
302                 public override string TestProperty
303                 {
304                         get { return prop; }
305                         set { prop = value; }
306                 }
307
308
309                 [DescriptionAttribute ("test derived")]
310                 public new string AnotherProperty
311                 {
312                         get { return base.AnotherProperty; }
313                         set { base.AnotherProperty = value; }
314                 }
315
316                 public new object YetAnotherProperty
317                 {
318                         get { return null; }
319                 }
320         }
321         
322
323         [DefaultProperty("AnotherProperty")]
324         [DefaultEvent("AnotherEvent")]
325         [DescriptionAttribute ("my test component")]
326         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
327         public class AnotherComponent: Component {
328                 string prop;
329                 
330                 [DescriptionAttribute ("test")]
331                 public event EventHandler AnEvent;
332                 
333                 public event EventHandler AnotherEvent;
334                 
335                 public AnotherComponent () {
336                 }
337                 
338                 public AnotherComponent (ISite site) {
339                         Site = site;
340                 }
341                 
342                 [DescriptionAttribute ("test")]
343                 public string TestProperty {
344                         get { return prop; }
345                         set { prop = value; }
346                 }
347                 
348                 public string AnotherProperty {
349                         get { return prop; }
350                         set { prop = value; }
351                 }
352         }
353
354         [Browsable (false)]
355         public interface ITestInterface
356         {
357                 void TestFunction ();
358         }
359
360         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
361         public class TestClass
362         {
363                 public TestClass()
364                 {}
365                         
366                 void TestFunction ()
367                 {}
368         }
369
370         [DescriptionAttribute ("bla")]
371         public class TestDerivedClass : TestClass, ITestInterface
372         {
373                 public void TestFunction ()
374                 {}
375         }
376
377         public struct TestStruct
378         {
379                 public int TestVal;
380         }
381
382         public class TestCustomTypeDescriptor : ICustomTypeDescriptor
383         {
384                 public string methods_called = "";
385
386                 public void ResetMethodsCalled ()
387                 {
388                         methods_called = "";
389                 }
390
391                 public TypeConverter GetConverter()
392                 {
393                         return new StringConverter();
394                 }
395
396                 public EventDescriptorCollection GetEvents(Attribute[] attributes)
397                 {
398                         methods_called += "1";
399                         return null;
400                 }
401
402                 public EventDescriptorCollection GetEvents()
403                 {
404                         methods_called += "2";
405                         return null;
406                 }
407
408                 public string GetComponentName()
409                 {
410                         return "MyComponentnName";
411                 }
412
413                 public object GetPropertyOwner(PropertyDescriptor pd)
414                 {
415                         return this;
416                 }
417
418                 public AttributeCollection GetAttributes()
419                 {
420                         methods_called += "3";
421                         return null;
422                 }
423
424                 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
425                 {
426                         methods_called += "4";
427                         return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
428                 }
429
430                 public PropertyDescriptorCollection GetProperties()
431                 {
432                         methods_called += "5";
433                         return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
434                 }
435
436                 public object GetEditor(Type editorBaseType)
437                 {
438                         return null;
439                 }
440
441                 public PropertyDescriptor GetDefaultProperty()
442                 {
443                         methods_called += "6";
444                         return null;
445                 }
446
447                 public EventDescriptor GetDefaultEvent()
448                 {
449                         methods_called += "7";
450                         return null;
451                 }
452
453                 public string GetClassName()
454                 {
455                         return this.GetType().Name;
456                 }
457         }
458
459         [TestFixture]
460         public class TypeDescriptorTests
461         {
462                 MyComponent com = new MyComponent ();
463                 MyComponent sitedcom = new MyComponent (new MySite ());
464                 MyComponent nfscom = new MyComponent (new NoFilterSite (new MyContainer ()));
465                 AnotherComponent anothercom = new AnotherComponent ();
466                 
467                 [Test]
468                 public void TestICustomTypeDescriptor ()
469                 {
470                         TestCustomTypeDescriptor test = new TestCustomTypeDescriptor ();
471
472                         PropertyDescriptorCollection props;
473                         PropertyDescriptor prop;
474                         EventDescriptorCollection events;
475
476                         test.ResetMethodsCalled ();
477                         props = TypeDescriptor.GetProperties (test);
478                         Assert.AreEqual ("5", test.methods_called, "#1");
479
480                         test.ResetMethodsCalled ();
481                         props = TypeDescriptor.GetProperties (test, new Attribute[0]);
482                         Assert.AreEqual ("4", test.methods_called, "#2");
483
484                         test.ResetMethodsCalled ();
485                         props = TypeDescriptor.GetProperties (test, new Attribute[0], false);
486                         Assert.AreEqual ("4", test.methods_called, "#3");
487
488                         test.ResetMethodsCalled ();
489                         props = TypeDescriptor.GetProperties (test, false);
490                         Assert.AreEqual ("5", test.methods_called, "#4");
491
492                         test.ResetMethodsCalled ();
493                         prop = TypeDescriptor.GetDefaultProperty (test);
494                         Assert.AreEqual ("6", test.methods_called, "#5");
495
496                         test.ResetMethodsCalled ();
497                         events = TypeDescriptor.GetEvents (test);
498                         Assert.AreEqual ("2", test.methods_called, "#6");
499
500                         test.ResetMethodsCalled ();
501                         events = TypeDescriptor.GetEvents (test, new Attribute[0]);
502                         Assert.AreEqual ("1", test.methods_called, "#7");
503
504                         test.ResetMethodsCalled ();
505                         events = TypeDescriptor.GetEvents (test, false);
506                         Assert.AreEqual ("2", test.methods_called, "#8");
507                 }
508
509                 [Test]
510                 public void TestCreateDesigner ()
511                 {
512                         IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
513                         Assert.IsTrue (des is MyDesigner, "#1");
514                         
515                         des = TypeDescriptor.CreateDesigner (com, typeof(string));
516                         Assert.IsNull (des, "#2");
517                 }
518                 
519                 [Test]
520                 public void TestCreateEvent ()
521                 {
522                         EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
523                         Assert.AreEqual (typeof (MyComponent), ed.ComponentType, "#1");
524                         Assert.AreEqual (typeof (EventHandler), ed.EventType, "#2");
525                         Assert.IsTrue (ed.IsMulticast, "#3");
526                         Assert.AreEqual ("AnEvent", ed.Name, "#4");
527                 }
528                 
529                 [Test]
530                 public void TestCreateProperty ()
531                 {
532                         PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
533                         Assert.AreEqual (typeof (MyComponent), pd.ComponentType, "#1");
534                         Assert.AreEqual ("TestProperty", pd.Name, "#2");
535                         Assert.AreEqual (typeof (string), pd.PropertyType, "#3");
536                         Assert.IsFalse (pd.IsReadOnly, "#4");
537                         
538                         pd.SetValue (com, "hi");
539                         Assert.AreEqual ("hi", pd.GetValue (com), "#5");
540                 }
541                 
542                 [Test]
543                 public void TestGetAttributes ()
544                 {
545                         AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
546                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#A1");
547                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#A2");
548                         Assert.IsNull (col [typeof (EditorAttribute)], "#A3");
549                         
550                         col = TypeDescriptor.GetAttributes (com);
551                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#B1");
552                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#B2");
553                         Assert.IsNull (col [typeof (EditorAttribute)], "#B3");
554                         
555                         col = TypeDescriptor.GetAttributes (sitedcom);
556                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#C1");
557                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#C2");
558                         Assert.IsNotNull (col [typeof (EditorAttribute)], "#C3");
559
560                         col = TypeDescriptor.GetAttributes (nfscom);
561                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#D1");
562                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#D2");
563                         Assert.IsNull (col [typeof (EditorAttribute)], "#D3");
564
565                         col = TypeDescriptor.GetAttributes (typeof (MyDerivedComponent));
566                         Assert.IsNotNull (col [typeof (DesignerAttribute)], "#E1");
567                         Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#E2");
568                         DesignerAttribute attribute = col[typeof(DesignerAttribute)] as DesignerAttribute;
569                         Assert.IsNotNull (attribute, "#E3");
570                         // there are multiple DesignerAttribute present and their order in the collection isn't deterministic
571                         bool found = false;
572                         for (int i = 0; i < col.Count; i++) {
573                                 attribute = (col [i] as DesignerAttribute);
574                                 if (attribute != null) {
575                                         found = typeof(MyOtherDesigner).AssemblyQualifiedName == attribute.DesignerTypeName;
576                                         if (found)
577                                                 break;
578                                 }
579                         }
580                         Assert.IsTrue (found, "#E4");
581
582                         // Shows that attributes are retrieved from the current type, the base types 
583                         // and the implemented by the type interfaces.
584                         Assert.AreEqual (3, TypeDescriptor.GetAttributes (typeof (TestDerivedClass)).Count, "#F1");
585                 }
586                 
587                 [Test]
588                 public void TestGetClassName ()
589                 {
590                         Assert.AreEqual (typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
591                 }
592                 
593                 [Test]
594                 public void TestGetComponentName ()
595                 {
596 #if NET_2_0
597                         // in MS.NET 2.0, GetComponentName no longer returns
598                         // the type name if there's no custom typedescriptor
599                         // and no site
600                         Assert.IsNull (TypeDescriptor.GetComponentName (com), "#1");
601                         Assert.IsNull (TypeDescriptor.GetComponentName (com, false), "#2");
602                         Assert.IsNull (TypeDescriptor.GetComponentName (new Exception ()), "#3");
603                         Assert.IsNull (TypeDescriptor.GetComponentName (new Exception (), false), "#4");
604                         Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception)), "#4");
605                         Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception), false), "#6");
606 #else
607                         Assert.AreEqual ("MyComponent", TypeDescriptor.GetComponentName (com), "#1");
608                         Assert.AreEqual ("MyComponent", TypeDescriptor.GetComponentName (com, false), "#2");
609                         Assert.AreEqual ("Exception", TypeDescriptor.GetComponentName (new Exception ()), "#3");
610                         Assert.AreEqual ("Exception", TypeDescriptor.GetComponentName (new Exception (), false), "#4");
611                         Assert.IsNotNull (TypeDescriptor.GetComponentName (typeof (Exception)), "#5");
612                         Assert.IsNotNull (TypeDescriptor.GetComponentName (typeof (Exception), false), "#6");
613 #endif
614                         Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#7");
615                         Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#8");
616                 }
617                 
618                 [Test]
619                 public void TestGetConverter ()
620                 {
621                         Assert.AreEqual (typeof (BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType (), "#1");
622                         Assert.AreEqual (typeof (ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType (), "#2");
623                         Assert.AreEqual (typeof (SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType (), "#3");
624                         Assert.AreEqual (typeof (StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType (), "#4");
625                         Assert.AreEqual (typeof (CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType (), "#5");
626                         Assert.AreEqual (typeof (Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType (), "#6");
627                         Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType (), "#7");
628                         Assert.AreEqual (typeof (Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType (), "#8");
629                         Assert.AreEqual (typeof (UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType (), "#9");
630                         Assert.AreEqual (typeof (UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType (), "#10");
631                         Assert.AreEqual (typeof (UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType (), "#11");
632                         Assert.AreEqual (typeof (SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType (), "#12");
633                         Assert.AreEqual (typeof (DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType (), "#13");
634                         Assert.AreEqual (typeof (DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType (), "#14");
635                         Assert.AreEqual (typeof (ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType (), "#15");
636                         Assert.AreEqual (typeof (CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType (), "#16");
637                         Assert.AreEqual (typeof (DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType (), "#17");
638                         Assert.AreEqual (typeof (GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType (), "#18");
639                         Assert.AreEqual (typeof (TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType (), "#19");
640                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType (), "#20");
641
642                         // Tests from bug #71444
643                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType (), "#21");
644                         Assert.AreEqual (typeof (ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType (), "#22");
645                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType (), "#23");
646                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType (), "#24");
647
648                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType (), "#25");
649                         Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType (), "#26");
650                         Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType (), "#27");
651
652 #if NET_2_0
653                         // Test from bug #76686
654                         Assert.AreEqual  (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType (), "#28");
655 #endif
656                         Assert.IsTrue (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29");
657                         Assert.IsTrue (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30");
658                 }
659                 
660                 [Test]
661                 public void TestGetDefaultEvent ()
662                 {
663                         EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
664                         Assert.IsNull ( des, "#A");
665                         
666                         des = TypeDescriptor.GetDefaultEvent (com);
667                         Assert.IsNull (des, "#B");
668
669                         des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
670                         Assert.IsNotNull (des, "#C1");
671                         Assert.AreEqual ("AnotherEvent", des.Name, "#C2");
672
673                         des = TypeDescriptor.GetDefaultEvent (anothercom);
674                         Assert.IsNotNull (des, "#D1");
675                         Assert.AreEqual ("AnotherEvent", des.Name, "#D2");
676
677                         des = TypeDescriptor.GetDefaultEvent (sitedcom);
678 #if NET_2_0
679                         Assert.IsNull (des, "#E1");
680 #else
681                         Assert.IsNotNull (des, "#E1");
682                         Assert.AreEqual ("AnotherEvent", des.Name, "#E2");
683 #endif
684
685                         des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
686                         Assert.IsNotNull (des, "#F1");
687                         Assert.AreEqual ("AnEvent", des.Name, "#F2");
688
689                         des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
690                         Assert.IsNotNull (des, "#G1");
691                         Assert.AreEqual ("AnEvent", des.Name, "#G2");
692                 }
693                 
694                 [Test]
695                 public void TestGetDefaultProperty ()
696                 {
697                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
698                         Assert.IsNull (des, "#A");
699                         
700                         des = TypeDescriptor.GetDefaultProperty (com);
701                         Assert.IsNull (des, "#B");
702
703                         des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
704                         Assert.IsNotNull (des, "#C1");
705                         Assert.AreEqual ("AnotherProperty", des.Name, "#C2");
706
707                         des = TypeDescriptor.GetDefaultProperty (anothercom);
708                         Assert.IsNotNull (des, "#D1");
709                         Assert.AreEqual ("AnotherProperty", des.Name, "#D2");
710                 }
711                 
712                 [Test]
713 #if ONLY_1_1
714                 // throws NullReferenceException on MS.NET 1.x due to bug
715                 // which is fixed in MS.NET 2.0
716                 [NUnit.Framework.Category("NotDotNet")]
717 #endif
718                 public void TestGetDefaultProperty2 ()
719                 {
720                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
721                         Assert.IsNull (des, "#A");
722
723                         des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
724                         Assert.IsNotNull (des, "#B1");
725                         Assert.AreEqual ("TestProperty", des.Name, "#B2");
726
727                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
728                         Assert.IsNotNull (des, "#C1");
729                         Assert.AreEqual ("TestProperty", des.Name, "#C2");
730
731                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
732                         Assert.IsNull (des, "#D");
733                 }
734
735                 [Test]
736                 public void TestGetEvents ()
737                 {
738                         EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
739                         Assert.AreEqual (3, col.Count, "#A1");
740                         Assert.IsNotNull (col.Find ("AnEvent", true), "#A2");
741                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#A3");
742                         Assert.IsNotNull (col.Find ("Disposed", true), "#A4");
743                         
744                         col = TypeDescriptor.GetEvents (com);
745                         Assert.AreEqual (3, col.Count, "#B1");
746                         Assert.IsNotNull (col.Find ("AnEvent", true), "#B2");
747                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#B3");
748                         Assert.IsNotNull (col.Find ("Disposed", true), "#B4");
749                         
750                         col = TypeDescriptor.GetEvents (sitedcom);
751                         Assert.AreEqual (2, col.Count, "#C1");
752                         Assert.IsNotNull (col.Find ("AnotherEvent", true), "#C2");
753                         Assert.IsNotNull (col.Find ("Disposed", true), "#C3");
754
755                         col = TypeDescriptor.GetEvents (nfscom);
756                         Assert.AreEqual (3, col.Count, "#D1");
757                         Assert.IsNotNull (col.Find ("AnEvent", true), "#D2");
758                         Assert.IsNotNull ( col.Find ("AnotherEvent", true), "#D3");
759                         Assert.IsNotNull (col.Find ("Disposed", true), "#D4");
760
761                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
762                         
763                         col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
764                         Assert.AreEqual (1, col.Count, "#E1");
765                         Assert.IsNotNull (col.Find ("AnEvent", true), "#E2");
766                         
767                         col = TypeDescriptor.GetEvents (com, filter);
768                         Assert.AreEqual (1, col.Count, "#F1");
769                         Assert.IsNotNull (col.Find ("AnEvent", true), "#F2");
770                         
771                         col = TypeDescriptor.GetEvents (sitedcom, filter);
772                         Assert.AreEqual (0, col.Count, "#G");
773
774                         col = TypeDescriptor.GetEvents (nfscom, filter);
775                         Assert.AreEqual (1, col.Count, "#H1");
776                         Assert.IsNotNull (col.Find ("AnEvent", true), "#H2");
777
778                         col = TypeDescriptor.GetEvents (typeof (IFoo));
779                         Assert.AreEqual (2, col.Count, "#I1");
780                         Assert.IsNotNull (col.Find ("Fired", true), "#I2");
781                         Assert.IsNotNull (col.Find ("Fired", false), "#I3");
782                         Assert.IsNotNull (col.Find ("fired", true), "#I4");
783                         Assert.IsNull (col.Find ("fired", false), "#I5");
784                         Assert.IsNotNull (col.Find ("Closed", true), "#I6");
785
786                         col = TypeDescriptor.GetEvents (typeof (IBar));
787                         Assert.AreEqual (1, col.Count, "#J1");
788                         Assert.IsNull (col.Find ("Fired", true), "#J2");
789                         Assert.IsNull (col.Find ("Closed", true), "#J3");
790                         Assert.IsNotNull (col.Find ("Destroyed", true), "#J4");
791                 }
792                 
793                 [Test]
794                 public void TestGetProperties ()
795                 {
796                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
797                         Assert.IsNotNull (col.Find ("TestProperty", true), "#A1");
798                         Assert.IsNotNull ( col.Find ("AnotherProperty", true), "#A2");
799                         
800                         col = TypeDescriptor.GetProperties (com);
801                         Assert.IsNotNull (col.Find ("TestProperty", true), "#B1");
802                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#B2");
803
804                         col = TypeDescriptor.GetProperties (nfscom);
805                         Assert.IsNotNull (col.Find ("TestProperty", true), "#C1");
806                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#C2");
807
808                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
809                         
810                         col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
811                         Assert.IsNotNull (col.Find ("TestProperty", true), "#D1");
812                         Assert.IsNull (col.Find ("AnotherProperty", true), "#D2");
813                         
814                         col = TypeDescriptor.GetProperties (com, filter);
815                         Assert.IsNotNull (col.Find ("TestProperty", true), "#E1");
816                         Assert.IsNull (col.Find ("AnotherProperty", true), "#E2");
817
818                         col = TypeDescriptor.GetProperties (nfscom, filter);
819                         Assert.IsNotNull (col.Find ("TestProperty", true), "#F1");
820                         Assert.IsNull (col.Find ("AnotherProperty", true), "#F2");
821
822
823                         // GetProperties should return only the last type's implementation of a
824                         // property with a matching name in the base types. E.g in the case where 
825                         // the "new" keyword is used.
826                         //
827                         PropertyDescriptorCollection derivedCol = TypeDescriptor.GetProperties (typeof(MyDerivedComponent));
828                         Assert.IsNotNull (derivedCol["AnotherProperty"].Attributes[typeof (DescriptionAttribute)], "#G1");
829                         int anotherPropsFound = 0;
830                         int yetAnotherPropsFound = 0;
831                         foreach (PropertyDescriptor props in derivedCol) {
832                                 if (props.Name == "AnotherProperty")
833                                         anotherPropsFound++;
834                                 else if (props.Name == "YetAnotherProperty")
835                                         yetAnotherPropsFound++;
836                         }
837
838                         // GetProperties does not return the base type property in the case 
839                         // where both the "new" keyword is used and also the Property type is different 
840                         // (Type.GetProperties does return both properties)
841                         //
842                         Assert.AreEqual (1, anotherPropsFound, "#G2");
843                         Assert.IsNotNull (derivedCol["AnotherProperty"].Attributes[typeof (DescriptionAttribute)], "#G3");
844                         Assert.AreEqual (1, yetAnotherPropsFound, "#G4");
845
846                         // Verify that we return the derived "new" property when 
847                         // filters are applied that match the parent property
848                         // 
849                         PropertyDescriptorCollection filteredCol = TypeDescriptor.GetProperties (typeof(MyDerivedComponent), 
850                                                                                                  new Attribute[] { BrowsableAttribute.Yes });
851                         Assert.IsNotNull (filteredCol["YetAnotherProperty"], "#G5");
852
853                         // GetProperties does not return write-only properties (ones without a getter)
854                         // 
855                         Assert.IsNull (col.Find ("WriteOnlyProperty", true), "#H1");
856
857                         col = TypeDescriptor.GetProperties (typeof (IFoo));
858                         Assert.AreEqual (1, col.Count, "#I1");
859                         Assert.IsNotNull (col.Find ("HasFired", true), "#I1");
860                         Assert.IsNotNull (col.Find ("HasFired", false), "#I2");
861                         Assert.IsNotNull (col.Find ("hasFired", true), "#I3");
862                         Assert.IsNull (col.Find ("hasFired", false), "#I4");
863
864                         col = TypeDescriptor.GetProperties (typeof (IBar));
865                         Assert.AreEqual (1, col.Count, "#J1");
866                         Assert.IsNull (col.Find ("HasFired", true), "#J2");
867                         Assert.IsNotNull (col.Find ("IsDestroyed", true), "#J3");
868                         Assert.IsNotNull (col.Find ("IsDestroyed", false), "#J4");
869                         Assert.IsNotNull (col.Find ("isDestroyed", true), "#J5");
870                         Assert.IsNull (col.Find ("isDestroyed", false), "#J6");
871                 }
872
873                 [Test]
874 #if ONLY_1_1
875                 // throws NullReferenceException on MS.NET 1.x due to bug
876                 // which is fixed in MS.NET 2.0
877                 [NUnit.Framework.Category("NotDotNet")]
878 #endif
879                 public void TestGetProperties2 ()
880                 {
881                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
882                         Assert.IsNull (col.Find ("TestProperty", true), "#A1");
883                         Assert.IsNotNull (col.Find ("AnotherProperty", true), "#A2");
884
885                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
886                         col = TypeDescriptor.GetProperties (sitedcom, filter);
887                         Assert.IsNull (col.Find ("TestProperty", true), "#B1");
888                         Assert.IsNull (col.Find ("AnotherProperty", true), "#B2");
889                 }
890
891                 [Test]
892 #if ONLY_1_1
893                 [NUnit.Framework.Category ("NotDotNet")] // .NET 1.x (or csc 1.x) does not retain the original order
894 #endif
895                 public void GetProperties_Order ()
896                 {
897                         MyComponent com = new MyComponent (new MyContainer ());
898
899                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (com);
900                         Assert.AreEqual (8, col.Count, "#1");
901                         Assert.AreEqual ("TestProperty", col [0].Name, "#2");
902                         Assert.AreEqual ("AnotherProperty", col [1].Name, "#3");
903                         Assert.AreEqual ("YetAnotherProperty", col [2].Name, "#4");
904                         Assert.AreEqual ("Name", col [3].Name, "#5");
905                         Assert.AreEqual ("Address", col [4].Name, "#6");
906                         Assert.AreEqual ("Country", col [5].Name, "#7");
907                         Assert.AreEqual ("Site", col [6].Name, "#8");
908                         Assert.AreEqual ("Container", col [7].Name, "#9");
909                 }
910
911                 [TypeConverter (typeof (TestConverter))]
912                 class TestConverterClass {
913                 }
914
915                 class TestConverter : TypeConverter {
916                         public Type Type;
917
918                         public TestConverter (Type type)
919                         {
920                                 this.Type = type;
921                         }
922                 }
923
924                 [Test]
925                 public void TestConverterCtorWithArgument ()
926                 {
927                         TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass));
928                         Assert.IsNotNull (t.GetType (), "#A1");
929                         Assert.AreEqual (typeof (TestConverter), t.GetType (), "#A2");
930                         TestConverter converter = (TestConverter) t;
931                         Assert.AreEqual (typeof (TestConverterClass), converter.Type, "#B");
932                 }
933
934                 [Test]
935                 public void GetPropertiesIgnoreIndexers ()
936                 {
937                         PropertyDescriptorCollection pc =
938                                 TypeDescriptor.GetProperties (typeof (string));
939                         // There are two string properties: Length and Chars.
940                         // Chars is an indexer.
941                         //
942                         // Future version of CLI might contain some additional
943                         // properties. In that case simply increase the
944                         // number. (Also, it is fine to just remove #2.)
945                         Assert.AreEqual (1, pc.Count, "#1");
946                         Assert.AreEqual ("Length", pc [0].Name, "#2");
947                 }
948         }
949 }