2 // System.ComponentModel.TypeDescriptorTests test cases
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (c) 2004 Novell, Inc. (http://www.ximian.com)
11 using System.Collections;
12 using System.ComponentModel;
13 using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
14 using System.ComponentModel.Design;
15 using System.Globalization;
17 using NUnit.Framework;
19 namespace MonoTests.System.ComponentModel
23 event EventHandler Fired;
24 event EventHandler Closed;
33 event EventHandler Destroyed;
40 class MyDesigner: IDesigner
46 public IComponent Component {get{return null; }}
48 public DesignerVerbCollection Verbs {get{return null; }}
50 public void DoDefaultAction () { }
52 public void Initialize (IComponent component) { }
54 public void Dispose () { }
57 class MyOtherDesigner: IDesigner
59 public MyOtherDesigner()
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 () { }
72 public IComponent Component { get { return null; } }
74 public IContainer Container { get { return null; } }
76 public bool DesignMode { get { return true; } }
78 public string Name { get { return "TestName"; } set { } }
80 public object GetService (Type t)
82 if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
87 class MyFilter: ITypeDescriptorFilterService
89 public bool FilterAttributes (IComponent component,IDictionary attributes)
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;
100 public bool FilterEvents (IComponent component, IDictionary events)
102 events.Remove ("AnEvent");
106 public bool FilterProperties (IComponent component, IDictionary properties)
108 properties.Remove ("TestProperty");
113 class AnotherSite: ISite
115 public IComponent Component { get { return null; } }
117 public IContainer Container { get { return null; } }
119 public bool DesignMode { get { return true; } }
121 public string Name { get { return "TestName"; } set { } }
123 public object GetService (Type t)
125 if (t == typeof(ITypeDescriptorFilterService)) {
126 return new AnotherFilter ();
132 class NoFilterSite : ISite
134 public NoFilterSite () : this (null)
138 public NoFilterSite (IContainer container)
140 _container = container;
143 public IComponent Component {
147 public IContainer Container {
148 get { return _container; }
151 public bool DesignMode { get { return true; } }
153 public string Name { get { return "TestName"; } set { } }
155 public object GetService (Type t)
160 public IContainer _container;
163 class MyContainer : IContainer
165 public MyContainer ()
167 _components = new ComponentCollection (new IComponent [0]);
170 public ComponentCollection Components {
171 get { return _components; }
174 public void Add (IComponent component)
178 public void Add (IComponent component, string name)
182 public void Dispose ()
186 public void Remove (IComponent component)
190 private ComponentCollection _components;
193 class AnotherFilter: ITypeDescriptorFilterService
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;
205 public bool FilterEvents (IComponent component, IDictionary events) {
209 public bool FilterProperties (IComponent component, IDictionary properties) {
214 [DescriptionAttribute ("my test component")]
215 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
216 public class MyComponent: Component
220 [DescriptionAttribute ("test")]
221 public event EventHandler AnEvent;
223 public event EventHandler AnotherEvent;
225 public MyComponent ()
229 public MyComponent (ISite site)
234 public MyComponent (IContainer container)
236 container.Add (this);
239 [DescriptionAttribute ("test")]
240 public virtual string TestProperty
243 set { prop = value; }
246 public string AnotherProperty
249 set { prop = value; }
253 public string YetAnotherProperty
262 public string Address {
266 public string Country {
270 private string HairColor {
274 protected int Weight {
278 internal int Height {
282 public string WriteOnlyProperty {
283 set { prop = value; }
287 [DescriptionAttribute ("my test derived component")]
288 [DesignerAttribute (typeof(MyOtherDesigner))]
289 public class MyDerivedComponent: MyComponent
293 public MyDerivedComponent ()
297 public MyDerivedComponent (ISite site) : base (site)
301 [DescriptionAttribute ("test derived")]
302 public override string TestProperty
305 set { prop = value; }
309 [DescriptionAttribute ("test derived")]
310 public new string AnotherProperty
312 get { return base.AnotherProperty; }
313 set { base.AnotherProperty = value; }
316 public new object YetAnotherProperty
323 [DefaultProperty("AnotherProperty")]
324 [DefaultEvent("AnotherEvent")]
325 [DescriptionAttribute ("my test component")]
326 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
327 public class AnotherComponent: Component {
330 [DescriptionAttribute ("test")]
331 public event EventHandler AnEvent;
333 public event EventHandler AnotherEvent;
335 public AnotherComponent () {
338 public AnotherComponent (ISite site) {
342 [DescriptionAttribute ("test")]
343 public string TestProperty {
345 set { prop = value; }
348 public string AnotherProperty {
350 set { prop = value; }
355 public interface ITestInterface
357 void TestFunction ();
360 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
361 public class TestClass
370 [DescriptionAttribute ("bla")]
371 public class TestDerivedClass : TestClass, ITestInterface
373 public void TestFunction ()
377 public struct TestStruct
382 public class TestCustomTypeDescriptor : ICustomTypeDescriptor
384 public string methods_called = "";
386 public void ResetMethodsCalled ()
391 public TypeConverter GetConverter()
393 return new StringConverter();
396 public EventDescriptorCollection GetEvents(Attribute[] attributes)
398 methods_called += "1";
402 public EventDescriptorCollection GetEvents()
404 methods_called += "2";
408 public string GetComponentName()
410 return "MyComponentnName";
413 public object GetPropertyOwner(PropertyDescriptor pd)
418 public AttributeCollection GetAttributes()
420 methods_called += "3";
424 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
426 methods_called += "4";
427 return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
430 public PropertyDescriptorCollection GetProperties()
432 methods_called += "5";
433 return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
436 public object GetEditor(Type editorBaseType)
441 public PropertyDescriptor GetDefaultProperty()
443 methods_called += "6";
447 public EventDescriptor GetDefaultEvent()
449 methods_called += "7";
453 public string GetClassName()
455 return this.GetType().Name;
460 public class TypeDescriptorTests
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 ();
468 public void TestICustomTypeDescriptor ()
470 TestCustomTypeDescriptor test = new TestCustomTypeDescriptor ();
472 PropertyDescriptorCollection props;
473 PropertyDescriptor prop;
474 EventDescriptorCollection events;
476 test.ResetMethodsCalled ();
477 props = TypeDescriptor.GetProperties (test);
478 Assert.AreEqual ("5", test.methods_called, "#1");
480 test.ResetMethodsCalled ();
481 props = TypeDescriptor.GetProperties (test, new Attribute[0]);
482 Assert.AreEqual ("4", test.methods_called, "#2");
484 test.ResetMethodsCalled ();
485 props = TypeDescriptor.GetProperties (test, new Attribute[0], false);
486 Assert.AreEqual ("4", test.methods_called, "#3");
488 test.ResetMethodsCalled ();
489 props = TypeDescriptor.GetProperties (test, false);
490 Assert.AreEqual ("5", test.methods_called, "#4");
492 test.ResetMethodsCalled ();
493 prop = TypeDescriptor.GetDefaultProperty (test);
494 Assert.AreEqual ("6", test.methods_called, "#5");
496 test.ResetMethodsCalled ();
497 events = TypeDescriptor.GetEvents (test);
498 Assert.AreEqual ("2", test.methods_called, "#6");
500 test.ResetMethodsCalled ();
501 events = TypeDescriptor.GetEvents (test, new Attribute[0]);
502 Assert.AreEqual ("1", test.methods_called, "#7");
504 test.ResetMethodsCalled ();
505 events = TypeDescriptor.GetEvents (test, false);
506 Assert.AreEqual ("2", test.methods_called, "#8");
510 public void TestCreateDesigner ()
512 IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
513 Assert.IsTrue (des is MyDesigner, "#1");
515 des = TypeDescriptor.CreateDesigner (com, typeof(string));
516 Assert.IsNull (des, "#2");
520 public void TestCreateEvent ()
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");
530 public void TestCreateProperty ()
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");
538 pd.SetValue (com, "hi");
539 Assert.AreEqual ("hi", pd.GetValue (com), "#5");
543 public void TestGetAttributes ()
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");
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");
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");
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");
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
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;
580 Assert.IsTrue (found, "#E4");
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");
588 public void TestGetClassName ()
590 Assert.AreEqual (typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
594 public void TestGetComponentName ()
597 // in MS.NET 2.0, GetComponentName no longer returns
598 // the type name if there's no custom typedescriptor
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");
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");
614 Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#7");
615 Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#8");
619 public void TestGetConverter ()
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");
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");
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");
653 // Test from bug #76686
654 Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType (), "#28");
656 Assert.IsTrue (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29");
657 Assert.IsTrue (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30");
661 public void TestGetDefaultEvent ()
663 EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
664 Assert.IsNull ( des, "#A");
666 des = TypeDescriptor.GetDefaultEvent (com);
667 Assert.IsNull (des, "#B");
669 des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
670 Assert.IsNotNull (des, "#C1");
671 Assert.AreEqual ("AnotherEvent", des.Name, "#C2");
673 des = TypeDescriptor.GetDefaultEvent (anothercom);
674 Assert.IsNotNull (des, "#D1");
675 Assert.AreEqual ("AnotherEvent", des.Name, "#D2");
677 des = TypeDescriptor.GetDefaultEvent (sitedcom);
679 Assert.IsNull (des, "#E1");
681 Assert.IsNotNull (des, "#E1");
682 Assert.AreEqual ("AnotherEvent", des.Name, "#E2");
685 des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
686 Assert.IsNotNull (des, "#F1");
687 Assert.AreEqual ("AnEvent", des.Name, "#F2");
689 des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
690 Assert.IsNotNull (des, "#G1");
691 Assert.AreEqual ("AnEvent", des.Name, "#G2");
695 public void TestGetDefaultProperty ()
697 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
698 Assert.IsNull (des, "#A");
700 des = TypeDescriptor.GetDefaultProperty (com);
701 Assert.IsNull (des, "#B");
703 des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
704 Assert.IsNotNull (des, "#C1");
705 Assert.AreEqual ("AnotherProperty", des.Name, "#C2");
707 des = TypeDescriptor.GetDefaultProperty (anothercom);
708 Assert.IsNotNull (des, "#D1");
709 Assert.AreEqual ("AnotherProperty", des.Name, "#D2");
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")]
718 public void TestGetDefaultProperty2 ()
720 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
721 Assert.IsNull (des, "#A");
723 des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
724 Assert.IsNotNull (des, "#B1");
725 Assert.AreEqual ("TestProperty", des.Name, "#B2");
727 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
728 Assert.IsNotNull (des, "#C1");
729 Assert.AreEqual ("TestProperty", des.Name, "#C2");
731 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
732 Assert.IsNull (des, "#D");
736 public void TestGetEvents ()
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");
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");
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");
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");
761 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
763 col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
764 Assert.AreEqual (1, col.Count, "#E1");
765 Assert.IsNotNull (col.Find ("AnEvent", true), "#E2");
767 col = TypeDescriptor.GetEvents (com, filter);
768 Assert.AreEqual (1, col.Count, "#F1");
769 Assert.IsNotNull (col.Find ("AnEvent", true), "#F2");
771 col = TypeDescriptor.GetEvents (sitedcom, filter);
772 Assert.AreEqual (0, col.Count, "#G");
774 col = TypeDescriptor.GetEvents (nfscom, filter);
775 Assert.AreEqual (1, col.Count, "#H1");
776 Assert.IsNotNull (col.Find ("AnEvent", true), "#H2");
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");
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");
794 public void TestGetProperties ()
796 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
797 Assert.IsNotNull (col.Find ("TestProperty", true), "#A1");
798 Assert.IsNotNull ( col.Find ("AnotherProperty", true), "#A2");
800 col = TypeDescriptor.GetProperties (com);
801 Assert.IsNotNull (col.Find ("TestProperty", true), "#B1");
802 Assert.IsNotNull (col.Find ("AnotherProperty", true), "#B2");
804 col = TypeDescriptor.GetProperties (nfscom);
805 Assert.IsNotNull (col.Find ("TestProperty", true), "#C1");
806 Assert.IsNotNull (col.Find ("AnotherProperty", true), "#C2");
808 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
810 col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
811 Assert.IsNotNull (col.Find ("TestProperty", true), "#D1");
812 Assert.IsNull (col.Find ("AnotherProperty", true), "#D2");
814 col = TypeDescriptor.GetProperties (com, filter);
815 Assert.IsNotNull (col.Find ("TestProperty", true), "#E1");
816 Assert.IsNull (col.Find ("AnotherProperty", true), "#E2");
818 col = TypeDescriptor.GetProperties (nfscom, filter);
819 Assert.IsNotNull (col.Find ("TestProperty", true), "#F1");
820 Assert.IsNull (col.Find ("AnotherProperty", true), "#F2");
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.
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")
834 else if (props.Name == "YetAnotherProperty")
835 yetAnotherPropsFound++;
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)
842 Assert.AreEqual (1, anotherPropsFound, "#G2");
843 Assert.IsNotNull (derivedCol["AnotherProperty"].Attributes[typeof (DescriptionAttribute)], "#G3");
844 Assert.AreEqual (1, yetAnotherPropsFound, "#G4");
846 // Verify that we return the derived "new" property when
847 // filters are applied that match the parent property
849 PropertyDescriptorCollection filteredCol = TypeDescriptor.GetProperties (typeof(MyDerivedComponent),
850 new Attribute[] { BrowsableAttribute.Yes });
851 Assert.IsNotNull (filteredCol["YetAnotherProperty"], "#G5");
853 // GetProperties does not return write-only properties (ones without a getter)
855 Assert.IsNull (col.Find ("WriteOnlyProperty", true), "#H1");
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");
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");
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")]
879 public void TestGetProperties2 ()
881 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
882 Assert.IsNull (col.Find ("TestProperty", true), "#A1");
883 Assert.IsNotNull (col.Find ("AnotherProperty", true), "#A2");
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");
893 [NUnit.Framework.Category ("NotDotNet")] // .NET 1.x (or csc 1.x) does not retain the original order
895 public void GetProperties_Order ()
897 MyComponent com = new MyComponent (new MyContainer ());
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");
911 [TypeConverter (typeof (TestConverter))]
912 class TestConverterClass {
915 class TestConverter : TypeConverter {
918 public TestConverter (Type type)
925 public void TestConverterCtorWithArgument ()
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");
935 public void GetPropertiesIgnoreIndexers ()
937 PropertyDescriptorCollection pc =
938 TypeDescriptor.GetProperties (typeof (string));
939 // There are two string properties: Length and Chars.
940 // Chars is an indexer.
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");