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 using System.Linq; 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 { MyDesigner()45 public MyDesigner() 46 { 47 } 48 49 public IComponent Component {get{return null; }} 50 51 public DesignerVerbCollection Verbs {get{return null; }} 52 DoDefaultAction()53 public void DoDefaultAction () { } 54 Initialize(IComponent component)55 public void Initialize (IComponent component) { } 56 Dispose()57 public void Dispose () { } 58 } 59 60 class MyOtherDesigner: IDesigner 61 { MyOtherDesigner()62 public MyOtherDesigner() 63 { 64 } 65 66 public IComponent Component {get {return null; } } 67 public DesignerVerbCollection Verbs { get {return null; } } DoDefaultAction()68 public void DoDefaultAction () { } Initialize(IComponent component)69 public void Initialize (IComponent component) { } Dispose()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 GetService(Type t)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 { FilterAttributes(IComponent component,IDictionary attributes)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 FilterEvents(IComponent component, IDictionary events)103 public bool FilterEvents (IComponent component, IDictionary events) 104 { 105 events.Remove ("AnEvent"); 106 return true; 107 } 108 FilterProperties(IComponent component, IDictionary properties)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 GetService(Type t)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 { NoFilterSite()137 public NoFilterSite () : this (null) 138 { 139 } 140 NoFilterSite(IContainer container)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 GetService(Type t)158 public object GetService (Type t) 159 { 160 return null; 161 } 162 163 public IContainer _container; 164 } 165 166 class MyContainer : IContainer 167 { MyContainer()168 public MyContainer () 169 { 170 _components = new ComponentCollection (new IComponent [0]); 171 } 172 173 public ComponentCollection Components { 174 get { return _components; } 175 } 176 Add(IComponent component)177 public void Add (IComponent component) 178 { 179 } 180 Add(IComponent component, string name)181 public void Add (IComponent component, string name) 182 { 183 } 184 Dispose()185 public void Dispose () 186 { 187 } 188 Remove(IComponent component)189 public void Remove (IComponent component) 190 { 191 } 192 193 private ComponentCollection _components; 194 } 195 196 class AnotherFilter: ITypeDescriptorFilterService 197 { FilterAttributes(IComponent component,IDictionary attributes)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 FilterEvents(IComponent component, IDictionary events)208 public bool FilterEvents (IComponent component, IDictionary events) { 209 return true; 210 } 211 FilterProperties(IComponent component, IDictionary properties)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 MyComponent()228 public MyComponent () 229 { 230 // A workaround for aggressive linker 231 Disposed -= null; 232 } 233 MyComponent(ISite site)234 public MyComponent (ISite site) 235 { 236 Site = site; 237 } 238 MyComponent(IContainer container)239 public MyComponent (IContainer container) 240 { 241 container.Add (this); 242 } 243 244 [DescriptionAttribute ("test")] 245 public virtual string TestProperty 246 { 247 get { return prop; } 248 set { prop = value; } 249 } 250 251 public string AnotherProperty 252 { 253 get { return prop; } 254 set { prop = value; } 255 } 256 257 [Browsable (false)] 258 public string YetAnotherProperty 259 { 260 get { return null; } 261 } 262 263 public string Name { 264 get { return null; } 265 } 266 267 public string Address { 268 get { return null; } 269 } 270 271 public string Country { 272 get { return null; } 273 } 274 275 private string HairColor { 276 get { return null; } 277 } 278 279 protected int Weight { 280 get { return 5; } 281 } 282 283 internal int Height { 284 get { return 0; } 285 } 286 287 public string WriteOnlyProperty { 288 set { prop = value; } 289 } 290 } 291 292 [DescriptionAttribute ("my test derived component")] 293 [DesignerAttribute (typeof(MyOtherDesigner))] 294 public class MyDerivedComponent: MyComponent 295 { 296 string prop; 297 MyDerivedComponent()298 public MyDerivedComponent () 299 { 300 } 301 MyDerivedComponent(ISite site)302 public MyDerivedComponent (ISite site) : base (site) 303 { 304 } 305 306 [DescriptionAttribute ("test derived")] 307 public override string TestProperty 308 { 309 get { return prop; } 310 set { prop = value; } 311 } 312 313 314 [DescriptionAttribute ("test derived")] 315 public new string AnotherProperty 316 { 317 get { return base.AnotherProperty; } 318 set { base.AnotherProperty = value; } 319 } 320 321 public new object YetAnotherProperty 322 { 323 get { return null; } 324 } 325 } 326 327 328 [DefaultProperty("AnotherProperty")] 329 [DefaultEvent("AnotherEvent")] 330 [DescriptionAttribute ("my test component")] 331 [DesignerAttribute (typeof(MyDesigner), typeof(int))] 332 public class AnotherComponent: Component { 333 string prop; 334 335 [DescriptionAttribute ("test")] 336 public event EventHandler AnEvent; 337 338 public event EventHandler AnotherEvent; 339 AnotherComponent()340 public AnotherComponent () { 341 } 342 AnotherComponent(ISite site)343 public AnotherComponent (ISite site) { 344 Site = site; 345 } 346 347 [DescriptionAttribute ("test")] 348 public string TestProperty { 349 get { return prop; } 350 set { prop = value; } 351 } 352 353 public string AnotherProperty { 354 get { return prop; } 355 set { prop = value; } 356 } 357 } 358 359 [Browsable (false)] 360 public interface ITestInterface 361 { TestFunction()362 void TestFunction (); 363 } 364 365 [DesignerAttribute (typeof(MyDesigner), typeof(int))] 366 public class TestClass 367 { TestClass()368 public TestClass() 369 {} 370 TestFunction()371 void TestFunction () 372 {} 373 } 374 375 [DescriptionAttribute ("bla")] 376 public class TestDerivedClass : TestClass, ITestInterface 377 { TestFunction()378 public void TestFunction () 379 {} 380 } 381 382 public struct TestStruct 383 { 384 public int TestVal; 385 } 386 387 public class TestCustomTypeDescriptor : ICustomTypeDescriptor 388 { 389 public string methods_called = ""; 390 ResetMethodsCalled()391 public void ResetMethodsCalled () 392 { 393 methods_called = ""; 394 } 395 GetConverter()396 public TypeConverter GetConverter() 397 { 398 return new StringConverter (); 399 } 400 GetEvents(Attribute[] attributes)401 public EventDescriptorCollection GetEvents(Attribute[] attributes) 402 { 403 methods_called += "1"; 404 return null; 405 } 406 GetEvents()407 public EventDescriptorCollection GetEvents() 408 { 409 methods_called += "2"; 410 return null; 411 } 412 GetComponentName()413 public string GetComponentName() 414 { 415 return "MyComponentnName"; 416 } 417 GetPropertyOwner(PropertyDescriptor pd)418 public object GetPropertyOwner(PropertyDescriptor pd) 419 { 420 return this; 421 } 422 GetAttributes()423 public AttributeCollection GetAttributes() 424 { 425 methods_called += "3"; 426 return null; 427 } 428 GetProperties(Attribute[] attributes)429 public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 430 { 431 methods_called += "4"; 432 return new PropertyDescriptorCollection(new PropertyDescriptor[0]); 433 } 434 GetProperties()435 public PropertyDescriptorCollection GetProperties() 436 { 437 methods_called += "5"; 438 return new PropertyDescriptorCollection(new PropertyDescriptor[0]); 439 } 440 GetEditor(Type editorBaseType)441 public object GetEditor(Type editorBaseType) 442 { 443 return null; 444 } 445 GetDefaultProperty()446 public PropertyDescriptor GetDefaultProperty() 447 { 448 methods_called += "6"; 449 return null; 450 } 451 GetDefaultEvent()452 public EventDescriptor GetDefaultEvent() 453 { 454 methods_called += "7"; 455 return null; 456 } 457 GetClassName()458 public string GetClassName() 459 { 460 return this.GetType ().Name; 461 } 462 } 463 464 class MyCustomTypeDescriptor : CustomTypeDescriptor 465 { 466 public MyTypeDescriptionProvider Provider { get; private set; } 467 MyCustomTypeDescriptor(MyTypeDescriptionProvider provider)468 public MyCustomTypeDescriptor (MyTypeDescriptionProvider provider) 469 { 470 Provider = provider; 471 } 472 GetClassName()473 public override string GetClassName () 474 { 475 return Provider.Id; 476 } 477 } 478 479 class MyTypeDescriptionProvider : TypeDescriptionProvider 480 { 481 public string Id { get; private set; } 482 public bool CreateInstanceCalled { get; private set; } 483 MyTypeDescriptionProvider()484 public MyTypeDescriptionProvider () 485 : this (null) 486 { 487 } 488 MyTypeDescriptionProvider(string id)489 public MyTypeDescriptionProvider (string id) 490 { 491 Id = id; 492 } 493 GetTypeDescriptor(Type objectType, object instance)494 public override ICustomTypeDescriptor GetTypeDescriptor (Type objectType, object instance) 495 { 496 return new MyCustomTypeDescriptor (this); 497 } 498 CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)499 public override object CreateInstance (IServiceProvider provider, Type objectType, Type[] argTypes, object[] args) 500 { 501 CreateInstanceCalled = true; 502 return base.CreateInstance (provider, objectType, argTypes, args); 503 } 504 } 505 506 [TestFixture] 507 public class TypeDescriptorTests 508 { 509 MyComponent com = new MyComponent (); 510 MyComponent sitedcom = new MyComponent (new MySite ()); 511 MyComponent nfscom = new MyComponent (new NoFilterSite (new MyContainer ())); 512 AnotherComponent anothercom = new AnotherComponent (); 513 514 [Test] 515 [ExpectedException (typeof (ArgumentNullException))] TestAddAttributes_Type_Attributes_1()516 public void TestAddAttributes_Type_Attributes_1 () 517 { 518 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes ((Type) null, null); 519 } 520 521 [Test] 522 [ExpectedException (typeof (ArgumentNullException))] TestAddAttributes_Type_Attributes_2()523 public void TestAddAttributes_Type_Attributes_2 () 524 { 525 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes (typeof (string), null); 526 } 527 528 [Test] TestAddAttributes_Type_Attributes_3()529 public void TestAddAttributes_Type_Attributes_3 () 530 { 531 Attribute[] new_attributes = new Attribute[] { 532 new ReadOnlyAttribute (true), 533 new BindableAttribute (true) 534 }; 535 536 TypeDescriptionProvider provider = null; 537 ICustomTypeDescriptor descriptor; 538 AttributeCollection attributes; 539 540 try { 541 provider = TypeDescriptor.AddAttributes (typeof (string), new Attribute[] { }); 542 Assert.IsNotNull (provider, "#A1"); 543 544 descriptor = provider.GetTypeDescriptor (typeof (string)); 545 Assert.IsNotNull (descriptor, "#A1-1"); 546 547 attributes = descriptor.GetAttributes (); 548 Assert.IsNotNull (attributes, "#A1-2"); 549 } finally { 550 if (provider != null) 551 TypeDescriptor.RemoveProvider (provider, typeof (string)); 552 } 553 554 provider = null; 555 try { 556 provider = TypeDescriptor.AddAttributes (typeof (string), new_attributes); 557 Assert.IsNotNull (provider, "#B1"); 558 559 descriptor = provider.GetTypeDescriptor (typeof (string)); 560 Assert.IsNotNull (descriptor, "#B1-1"); 561 562 attributes = descriptor.GetAttributes (); 563 Assert.IsNotNull (attributes, "#B1-2"); 564 Assert.AreNotEqual (0, attributes.Count, "#B1-3"); 565 Assert.IsTrue (attributes.Contains (new_attributes)); 566 } finally { 567 if (provider != null) 568 TypeDescriptor.RemoveProvider (provider, typeof (string)); 569 } 570 } 571 572 [Test] 573 [ExpectedException (typeof (ArgumentNullException))] TestAddAttributes_Instance_Attributes_1()574 public void TestAddAttributes_Instance_Attributes_1 () 575 { 576 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes ((object) null, null); 577 } 578 579 [Test] 580 [ExpectedException (typeof (ArgumentNullException))] TestAddAttributes_Instance_Attributes_2()581 public void TestAddAttributes_Instance_Attributes_2 () 582 { 583 string s = "test"; 584 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes (s, null); 585 } 586 587 [Test] TestAddAttributes_Instance_Attributes_3()588 public void TestAddAttributes_Instance_Attributes_3 () 589 { 590 Attribute[] new_attributes = new Attribute[] { 591 new ReadOnlyAttribute (true), 592 new BindableAttribute (true) 593 }; 594 595 TypeDescriptionProvider provider = null; 596 ICustomTypeDescriptor descriptor; 597 AttributeCollection attributes; 598 string s = "test"; 599 600 try { 601 provider = TypeDescriptor.AddAttributes (s, new Attribute[] { }); 602 Assert.IsNotNull (provider, "#A1"); 603 604 descriptor = provider.GetTypeDescriptor (s); 605 Assert.IsNotNull (descriptor, "#A1-1"); 606 607 attributes = descriptor.GetAttributes (); 608 Assert.IsNotNull (attributes, "#A1-2"); 609 } finally { 610 if (provider != null) 611 TypeDescriptor.RemoveProvider (provider, s); 612 } 613 614 provider = null; 615 try { 616 provider = TypeDescriptor.AddAttributes (s, new_attributes); 617 Assert.IsNotNull (provider, "#B1"); 618 619 descriptor = provider.GetTypeDescriptor (s); 620 Assert.IsNotNull (descriptor, "#B1-1"); 621 622 attributes = descriptor.GetAttributes (); 623 Assert.IsNotNull (attributes, "#B1-2"); 624 Assert.AreNotEqual (0, attributes.Count, "#B1-3"); 625 Assert.IsTrue (attributes.Contains (new_attributes)); 626 } finally { 627 if (provider != null) 628 TypeDescriptor.RemoveProvider (provider, s); 629 } 630 } 631 632 [Test] 633 [ExpectedException (typeof (ArgumentNullException))] TestAddProvider_Provider_Instance_1()634 public void TestAddProvider_Provider_Instance_1 () 635 { 636 TypeDescriptor.AddProvider (null, (object)null); 637 } 638 639 [Test] 640 [ExpectedException (typeof (ArgumentNullException))] TestAddProvider_Provider_Instance_2()641 public void TestAddProvider_Provider_Instance_2 () 642 { 643 var provider = new MyTypeDescriptionProvider (); 644 TypeDescriptor.AddProvider (provider, (object) null); 645 } 646 647 [Test] TestAddProvider_Provider_Instance_3()648 public void TestAddProvider_Provider_Instance_3 () 649 { 650 var instance = new MyComponent (); 651 var providers = new MyTypeDescriptionProvider[] { 652 new MyTypeDescriptionProvider ("One"), 653 new MyTypeDescriptionProvider ("Two"), 654 new MyTypeDescriptionProvider ("Three"), 655 new MyTypeDescriptionProvider ("Four") 656 }; 657 658 try { 659 TypeDescriptionProvider provider; 660 ICustomTypeDescriptor descriptor; 661 662 TypeDescriptor.AddProvider (providers[0], instance); 663 provider = TypeDescriptor.GetProvider (instance); 664 Assert.IsNotNull (provider, "#A1"); 665 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 666 Assert.IsNotNull (descriptor, "#A1-1"); 667 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2"); 668 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3"); 669 670 descriptor.GetProperties (); 671 672 TypeDescriptor.AddProvider (providers[1], instance); 673 provider = TypeDescriptor.GetProvider (instance); 674 Assert.IsNotNull (provider, "#B1"); 675 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 676 Assert.IsNotNull (descriptor, "#B1-1"); 677 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2"); 678 679 // Providers are stored in a stack according to docs, but it's in reality 680 // a FIFO linked list 681 TypeDescriptor.AddProvider (providers[2], instance); 682 TypeDescriptor.AddProvider (providers[3], instance); 683 provider = TypeDescriptor.GetProvider (instance); 684 Assert.IsNotNull (provider, "#C1"); 685 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 686 Assert.IsNotNull (descriptor, "#C1-1"); 687 Assert.AreEqual ("Four", descriptor.GetClassName (), "#C1-2"); 688 689 TypeDescriptor.RemoveProvider (providers[2], instance); 690 provider = TypeDescriptor.GetProvider (instance); 691 Assert.IsNotNull (provider, "#D1"); 692 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 693 Assert.IsNotNull (descriptor, "#D1-1"); 694 Assert.AreEqual ("Four", descriptor.GetClassName (), "#D1-2"); 695 696 TypeDescriptor.RemoveProvider (providers[3], instance); 697 provider = TypeDescriptor.GetProvider (instance); 698 Assert.IsNotNull (provider, "#E1"); 699 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 700 Assert.IsNotNull (descriptor, "#E1-1"); 701 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2"); 702 703 } finally { 704 TypeDescriptor.RemoveProvider (providers[0], instance); 705 TypeDescriptor.RemoveProvider (providers[1], instance); 706 TypeDescriptor.RemoveProvider (providers[2], instance); 707 TypeDescriptor.RemoveProvider (providers[3], instance); 708 } 709 } 710 711 [Test] TestAddProvider_Provider_Instance_4()712 public void TestAddProvider_Provider_Instance_4 () 713 { 714 var instance = new MyComponent (); 715 var providers = new MyTypeDescriptionProvider[] { 716 new MyTypeDescriptionProvider ("One"), 717 new MyTypeDescriptionProvider ("Two"), 718 new MyTypeDescriptionProvider ("Three"), 719 new MyTypeDescriptionProvider ("Four") 720 }; 721 722 try { 723 TypeDescriptionProvider provider; 724 ICustomTypeDescriptor descriptor; 725 726 TypeDescriptor.AddProvider (providers[0], instance); 727 provider = TypeDescriptor.GetProvider (instance); 728 Assert.IsNotNull (provider, "#A1"); 729 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 730 Assert.IsNotNull (descriptor, "#A1-1"); 731 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2"); 732 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3"); 733 734 descriptor.GetProperties (); 735 736 TypeDescriptor.AddProvider (providers[1], instance); 737 provider = TypeDescriptor.GetProvider (instance); 738 Assert.IsNotNull (provider, "#B1"); 739 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 740 Assert.IsNotNull (descriptor, "#B1-1"); 741 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2"); 742 743 // Providers are stored in a stack according to docs, but it's in reality 744 // a FIFO linked list 745 TypeDescriptor.AddProvider (providers[0], instance); 746 TypeDescriptor.AddProvider (providers[0], instance); 747 provider = TypeDescriptor.GetProvider (instance); 748 Assert.IsNotNull (provider, "#C1"); 749 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 750 Assert.IsNotNull (descriptor, "#C1-1"); 751 Assert.AreEqual ("One", descriptor.GetClassName (), "#C1-2"); 752 753 TypeDescriptor.RemoveProvider (providers[0], instance); 754 provider = TypeDescriptor.GetProvider (instance); 755 Assert.IsNotNull (provider, "#D1"); 756 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 757 Assert.IsNotNull (descriptor, "#D1-1"); 758 Assert.AreEqual ("One", descriptor.GetClassName (), "#D1-2"); 759 760 TypeDescriptor.RemoveProvider (providers[0], instance); 761 provider = TypeDescriptor.GetProvider (instance); 762 Assert.IsNotNull (provider, "#E1"); 763 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance); 764 Assert.IsNotNull (descriptor, "#E1-1"); 765 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2"); 766 767 } finally { 768 TypeDescriptor.RemoveProvider (providers[0], instance); 769 TypeDescriptor.RemoveProvider (providers[1], instance); 770 TypeDescriptor.RemoveProvider (providers[2], instance); 771 TypeDescriptor.RemoveProvider (providers[3], instance); 772 } 773 } 774 775 [Test] 776 [ExpectedException (typeof (ArgumentNullException))] TestAddProvider_Provider_Type_1()777 public void TestAddProvider_Provider_Type_1 () 778 { 779 TypeDescriptor.AddProvider (null, (Type) null); 780 } 781 782 [Test] 783 [ExpectedException (typeof (ArgumentNullException))] TestAddProvider_Provider_Type_2()784 public void TestAddProvider_Provider_Type_2 () 785 { 786 var provider = new MyTypeDescriptionProvider (); 787 TypeDescriptor.AddProvider (provider, (Type) null); 788 } 789 790 [Test] TestAddProvider_Provider_Type_3()791 public void TestAddProvider_Provider_Type_3 () 792 { 793 var type = typeof (MyComponent); 794 var providers = new MyTypeDescriptionProvider[] { 795 new MyTypeDescriptionProvider ("One"), 796 new MyTypeDescriptionProvider ("Two"), 797 new MyTypeDescriptionProvider ("Three"), 798 new MyTypeDescriptionProvider ("Four") 799 }; 800 801 try { 802 TypeDescriptionProvider provider; 803 ICustomTypeDescriptor descriptor; 804 805 TypeDescriptor.AddProvider (providers[0], type); 806 provider = TypeDescriptor.GetProvider (type); 807 Assert.IsNotNull (provider, "#A1"); 808 descriptor = provider.GetTypeDescriptor (type); 809 Assert.IsNotNull (descriptor, "#A1-1"); 810 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2"); 811 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3"); 812 813 TypeDescriptor.AddProvider (providers[1], type); 814 provider = TypeDescriptor.GetProvider (type); 815 Assert.IsNotNull (provider, "#B1"); 816 descriptor = provider.GetTypeDescriptor (type.GetType (), type); 817 Assert.IsNotNull (descriptor, "#B1-1"); 818 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2"); 819 820 // Providers are stored in a stack according to docs, but it's in reality 821 // a FIFO linked list 822 TypeDescriptor.AddProvider (providers[2], type); 823 TypeDescriptor.AddProvider (providers[3], type); 824 provider = TypeDescriptor.GetProvider (type); 825 Assert.IsNotNull (provider, "#C1"); 826 descriptor = provider.GetTypeDescriptor (type.GetType (), type); 827 Assert.IsNotNull (descriptor, "#C1-1"); 828 Assert.AreEqual ("Four", descriptor.GetClassName (), "#C1-2"); 829 830 TypeDescriptor.RemoveProvider (providers[2], type); 831 provider = TypeDescriptor.GetProvider (type); 832 Assert.IsNotNull (provider, "#D1"); 833 descriptor = provider.GetTypeDescriptor (type.GetType (), type); 834 Assert.IsNotNull (descriptor, "#D1-1"); 835 Assert.AreEqual ("Four", descriptor.GetClassName (), "#D1-2"); 836 837 TypeDescriptor.RemoveProvider (providers[3], type); 838 provider = TypeDescriptor.GetProvider (type); 839 Assert.IsNotNull (provider, "#E1"); 840 descriptor = provider.GetTypeDescriptor (type.GetType (), type); 841 Assert.IsNotNull (descriptor, "#E1-1"); 842 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2"); 843 844 } finally { 845 TypeDescriptor.RemoveProvider (providers[0], type); 846 TypeDescriptor.RemoveProvider (providers[1], type); 847 TypeDescriptor.RemoveProvider (providers[2], type); 848 TypeDescriptor.RemoveProvider (providers[3], type); 849 } 850 } 851 852 [Test] 853 [ExpectedException (typeof (ArgumentNullException))] TestGetProvider_Type_1()854 public void TestGetProvider_Type_1 () 855 { 856 TypeDescriptor.GetProvider ((Type)null); 857 } 858 859 [Test] TestGetProvider_Type_2()860 public void TestGetProvider_Type_2 () 861 { 862 TypeDescriptionProvider provider = TypeDescriptor.GetProvider (typeof (string)); 863 Assert.IsNotNull (provider, "#A1"); 864 provider = new MyTypeDescriptionProvider ("One"); 865 866 try { 867 TypeDescriptor.AddProvider (provider, typeof (string)); 868 ICustomTypeDescriptor descriptor = provider.GetTypeDescriptor (typeof (string)); 869 Assert.IsNotNull (descriptor, "#B1"); 870 Assert.AreEqual ("One", descriptor.GetClassName (), "#B1-1"); 871 } finally { 872 TypeDescriptor.RemoveProvider (provider, typeof (string)); 873 } 874 } 875 876 [Test] 877 [ExpectedException (typeof (ArgumentNullException))] TestGetProvider_Instance_1()878 public void TestGetProvider_Instance_1 () 879 { 880 TypeDescriptor.GetProvider ((object) null); 881 } 882 883 [Test] TestGetProvider_Instance_2()884 public void TestGetProvider_Instance_2 () 885 { 886 var instance = new MyComponent (); 887 TypeDescriptionProvider provider = TypeDescriptor.GetProvider (instance); 888 Assert.IsNotNull (provider, "#A1"); 889 provider = new MyTypeDescriptionProvider ("One"); 890 891 try { 892 TypeDescriptor.AddProvider (provider, instance); 893 ICustomTypeDescriptor descriptor = provider.GetTypeDescriptor (instance); 894 Assert.IsNotNull (descriptor, "#B1"); 895 Assert.AreEqual ("One", descriptor.GetClassName (), "#B1-1"); 896 } finally { 897 TypeDescriptor.RemoveProvider (provider, instance); 898 } 899 } 900 901 [Test] 902 [ExpectedException (typeof (ArgumentNullException))] TestRemoveProvider_Provider_Type_1()903 public void TestRemoveProvider_Provider_Type_1 () 904 { 905 TypeDescriptor.RemoveProvider (null, (Type)null); 906 } 907 908 [Test] 909 [ExpectedException (typeof (ArgumentNullException))] TestRemoveProvider_Provider_Type_2()910 public void TestRemoveProvider_Provider_Type_2 () 911 { 912 var provider = new MyTypeDescriptionProvider (); 913 TypeDescriptor.RemoveProvider (provider, null); 914 } 915 916 [Test] TestRemoveProvider_Provider_Type_3()917 public void TestRemoveProvider_Provider_Type_3 () 918 { 919 var provider = new MyTypeDescriptionProvider (); 920 bool refreshedCalled = false; 921 bool refreshedCorrectComponentChanged = false; 922 bool refreshedCorrectTypeChanged = false; 923 924 RefreshEventHandler handler = (RefreshEventArgs args) => { 925 refreshedCalled = true; 926 refreshedCorrectComponentChanged = args.ComponentChanged == null; 927 refreshedCorrectTypeChanged = args.TypeChanged == typeof (string); 928 }; 929 930 try { 931 TypeDescriptor.Refreshed += handler; 932 933 TypeDescriptor.RemoveProvider (provider, typeof (string)); 934 Assert.AreEqual (true, refreshedCalled, "#A1"); 935 Assert.AreEqual (true, refreshedCorrectComponentChanged, "#A2"); 936 Assert.AreEqual (true, refreshedCorrectTypeChanged, "#A3"); 937 } finally { 938 TypeDescriptor.Refreshed -= handler; 939 } 940 } 941 942 [Test] 943 [ExpectedException (typeof (ArgumentNullException))] TestRemoveProvider_Provider_Instance_1()944 public void TestRemoveProvider_Provider_Instance_1 () 945 { 946 TypeDescriptor.RemoveProvider (null, (object)null); 947 } 948 949 [Test] 950 [ExpectedException (typeof (ArgumentNullException))] TestRemoveProvider_Provider_Instance_2()951 public void TestRemoveProvider_Provider_Instance_2 () 952 { 953 var provider = new MyTypeDescriptionProvider (); 954 TypeDescriptor.RemoveProvider (provider, (object)null); 955 } 956 957 [Test] TestRemoveProvider_Provider_Instance_3()958 public void TestRemoveProvider_Provider_Instance_3 () 959 { 960 var instance = new MyComponent (); 961 var provider = new MyTypeDescriptionProvider (); 962 bool refreshedCalled = false; 963 bool refreshedCorrectComponentChanged = false; 964 bool refreshedCorrectTypeChanged = false; 965 966 RefreshEventHandler handler = (RefreshEventArgs args) => { 967 refreshedCalled = true; 968 refreshedCorrectComponentChanged = args.ComponentChanged == instance; 969 refreshedCorrectTypeChanged = args.TypeChanged == typeof (MyComponent); 970 }; 971 972 try { 973 TypeDescriptor.Refreshed += handler; 974 975 TypeDescriptor.RemoveProvider (provider, instance); 976 Assert.AreEqual (true, refreshedCalled, "#A1"); 977 Assert.AreEqual (true, refreshedCorrectComponentChanged, "#A2"); 978 Assert.AreEqual (true, refreshedCorrectTypeChanged, "#A3"); 979 } finally { 980 TypeDescriptor.Refreshed -= handler; 981 } 982 } 983 984 [Test] 985 [ExpectedException (typeof (ArgumentNullException))] TestGetReflectionType_Type_1()986 public void TestGetReflectionType_Type_1 () 987 { 988 TypeDescriptor.GetReflectionType ((Type) null); 989 } 990 991 [Test] TestGetReflectionType_Type_2()992 public void TestGetReflectionType_Type_2 () 993 { 994 Type type = TypeDescriptor.GetReflectionType (typeof (string)); 995 Assert.IsNotNull (type, "#A1"); 996 Assert.AreEqual (typeof (string), type, "#A1-1"); 997 998 type = TypeDescriptor.GetReflectionType (typeof (MyComponent)); 999 Assert.IsNotNull (type, "#B1"); 1000 Assert.AreEqual (typeof (MyComponent), type, "#B1-1"); 1001 1002 type = TypeDescriptor.GetReflectionType (typeof (List<string>)); 1003 Assert.IsNotNull (type, "#C1"); 1004 Assert.AreEqual (typeof (List <string>), type, "#C1-1"); 1005 1006 type = TypeDescriptor.GetReflectionType (typeof (IList<>)); 1007 Assert.IsNotNull (type, "#D1"); 1008 Assert.AreEqual (typeof (IList<>), type, "#D1-1"); 1009 1010 type = TypeDescriptor.GetReflectionType (typeof (IDictionary<,>)); 1011 Assert.IsNotNull (type, "#E1"); 1012 Assert.AreEqual (typeof (IDictionary<,>), type, "#E1-1"); 1013 } 1014 1015 [Test] 1016 [ExpectedException (typeof (ArgumentNullException))] TestGetReflectionType_Instance_1()1017 public void TestGetReflectionType_Instance_1 () 1018 { 1019 TypeDescriptor.GetReflectionType ((object) null); 1020 } 1021 1022 [Test] TestGetReflectionType_Instance_2()1023 public void TestGetReflectionType_Instance_2 () 1024 { 1025 string s = "string"; 1026 Type type = TypeDescriptor.GetReflectionType (s); 1027 Assert.IsNotNull (type, "#A1"); 1028 Assert.AreEqual (typeof (string), type, "#A1-1"); 1029 1030 var mc = new MyComponent (); 1031 type = TypeDescriptor.GetReflectionType (mc); 1032 Assert.IsNotNull (type, "#B1"); 1033 Assert.AreEqual (typeof (MyComponent), type, "#B1-1"); 1034 1035 var l = new List<string> (); 1036 type = TypeDescriptor.GetReflectionType (l); 1037 Assert.IsNotNull (type, "#C1"); 1038 Assert.AreEqual (typeof (List<string>), type, "#C1-1"); 1039 1040 IList il = new List<string> (); 1041 type = TypeDescriptor.GetReflectionType (il); 1042 Assert.IsNotNull (type, "#D1"); 1043 Assert.AreEqual (typeof (List<string>), type, "#D1-1"); 1044 1045 IDictionary id = new Dictionary<string, object> (); 1046 type = TypeDescriptor.GetReflectionType (id); 1047 Assert.IsNotNull (type, "#E1"); 1048 Assert.AreEqual (typeof (Dictionary<string,object>), type, "#E1-1"); 1049 1050 object o = 1; 1051 type = TypeDescriptor.GetReflectionType (o); 1052 Assert.IsNotNull (type, "#F1"); 1053 Assert.AreEqual (typeof (int), type, "#F1-1"); 1054 } 1055 1056 [Test] TestICustomTypeDescriptor()1057 public void TestICustomTypeDescriptor () 1058 { 1059 TestCustomTypeDescriptor test = new TestCustomTypeDescriptor (); 1060 1061 PropertyDescriptorCollection props; 1062 PropertyDescriptor prop; 1063 EventDescriptorCollection events; 1064 1065 test.ResetMethodsCalled (); 1066 props = TypeDescriptor.GetProperties (test); 1067 Assert.AreEqual ("5", test.methods_called, "#1"); 1068 1069 test.ResetMethodsCalled (); 1070 props = TypeDescriptor.GetProperties (test, new Attribute[0]); 1071 Assert.AreEqual ("4", test.methods_called, "#2"); 1072 1073 test.ResetMethodsCalled (); 1074 props = TypeDescriptor.GetProperties (test, new Attribute[0], false); 1075 Assert.AreEqual ("4", test.methods_called, "#3"); 1076 1077 test.ResetMethodsCalled (); 1078 props = TypeDescriptor.GetProperties (test, false); 1079 Assert.AreEqual ("5", test.methods_called, "#4"); 1080 1081 test.ResetMethodsCalled (); 1082 prop = TypeDescriptor.GetDefaultProperty (test); 1083 Assert.AreEqual ("63", test.methods_called, "#5"); 1084 1085 test.ResetMethodsCalled (); 1086 events = TypeDescriptor.GetEvents (test); 1087 Assert.AreEqual ("1", test.methods_called, "#6"); 1088 1089 test.ResetMethodsCalled (); 1090 events = TypeDescriptor.GetEvents (test, new Attribute[0]); 1091 Assert.AreEqual ("1", test.methods_called, "#7"); 1092 1093 test.ResetMethodsCalled (); 1094 events = TypeDescriptor.GetEvents (test, false); 1095 Assert.AreEqual ("1", test.methods_called, "#8"); 1096 } 1097 1098 [Test] TestCreateDesigner()1099 public void TestCreateDesigner () 1100 { 1101 IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int)); 1102 Assert.IsTrue (des is MyDesigner, "#1"); 1103 1104 des = TypeDescriptor.CreateDesigner (com, typeof(string)); 1105 Assert.IsNull (des, "#2"); 1106 } 1107 1108 [Test] TestCreateEvent()1109 public void TestCreateEvent () 1110 { 1111 EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null); 1112 Assert.AreEqual (typeof (MyComponent), ed.ComponentType, "#1"); 1113 Assert.AreEqual (typeof (EventHandler), ed.EventType, "#2"); 1114 Assert.IsTrue (ed.IsMulticast, "#3"); 1115 Assert.AreEqual ("AnEvent", ed.Name, "#4"); 1116 } 1117 1118 [Test] TestCreateProperty()1119 public void TestCreateProperty () 1120 { 1121 PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null); 1122 Assert.AreEqual (typeof (MyComponent), pd.ComponentType, "#1"); 1123 Assert.AreEqual ("TestProperty", pd.Name, "#2"); 1124 Assert.AreEqual (typeof (string), pd.PropertyType, "#3"); 1125 Assert.IsFalse (pd.IsReadOnly, "#4"); 1126 1127 pd.SetValue (com, "hi"); 1128 Assert.AreEqual ("hi", pd.GetValue (com), "#5"); 1129 } 1130 1131 [Test] TestGetAttributes()1132 public void TestGetAttributes () 1133 { 1134 AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent)); 1135 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#A1"); 1136 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#A2"); 1137 Assert.IsNull (col [typeof (EditorAttribute)], "#A3"); 1138 1139 col = TypeDescriptor.GetAttributes (com); 1140 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#B1"); 1141 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#B2"); 1142 Assert.IsNull (col [typeof (EditorAttribute)], "#B3"); 1143 1144 col = TypeDescriptor.GetAttributes (sitedcom); 1145 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#C1"); 1146 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#C2"); 1147 Assert.IsNotNull (col [typeof (EditorAttribute)], "#C3"); 1148 1149 col = TypeDescriptor.GetAttributes (nfscom); 1150 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#D1"); 1151 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#D2"); 1152 Assert.IsNull (col [typeof (EditorAttribute)], "#D3"); 1153 1154 col = TypeDescriptor.GetAttributes (typeof (MyDerivedComponent)); 1155 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#E1"); 1156 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#E2"); 1157 DesignerAttribute attribute = col[typeof(DesignerAttribute)] as DesignerAttribute; 1158 Assert.IsNotNull (attribute, "#E3"); 1159 // there are multiple DesignerAttribute present and their order in the collection isn't deterministic 1160 bool found = false; 1161 for (int i = 0; i < col.Count; i++) { 1162 attribute = (col [i] as DesignerAttribute); 1163 if (attribute != null) { 1164 found = typeof(MyOtherDesigner).AssemblyQualifiedName == attribute.DesignerTypeName; 1165 if (found) 1166 break; 1167 } 1168 } 1169 Assert.IsTrue (found, "#E4"); 1170 1171 // Shows that attributes are retrieved from the current type, the base types 1172 // and the implemented by the type interfaces. 1173 Assert.AreEqual (3, TypeDescriptor.GetAttributes (typeof (TestDerivedClass)).Count, "#F1"); 1174 } 1175 1176 [Test] TestGetClassName()1177 public void TestGetClassName () 1178 { 1179 Assert.AreEqual (typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com)); 1180 } 1181 1182 [Test] TestGetComponentName()1183 public void TestGetComponentName () 1184 { 1185 // in MS.NET 2.0, GetComponentName no longer returns 1186 // the type name if there's no custom typedescriptor 1187 // and no site 1188 Assert.IsNull (TypeDescriptor.GetComponentName (com), "#1"); 1189 Assert.IsNull (TypeDescriptor.GetComponentName (com, false), "#2"); 1190 Assert.IsNull (TypeDescriptor.GetComponentName (new Exception ()), "#3"); 1191 Assert.IsNull (TypeDescriptor.GetComponentName (new Exception (), false), "#4"); 1192 Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception)), "#4"); 1193 Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception), false), "#6"); 1194 Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#7"); 1195 Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#8"); 1196 } 1197 1198 [Test] 1199 [ExpectedException (typeof (ArgumentNullException))] TestGetConverterNullParam()1200 public void TestGetConverterNullParam () 1201 { 1202 TypeDescriptor.GetConverter (null); 1203 } 1204 1205 [Test] TestGetConverter()1206 public void TestGetConverter () 1207 { 1208 Assert.AreEqual (typeof (BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType (), "#1"); 1209 Assert.AreEqual (typeof (ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType (), "#2"); 1210 Assert.AreEqual (typeof (SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType (), "#3"); 1211 Assert.AreEqual (typeof (StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType (), "#4"); 1212 Assert.AreEqual (typeof (CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType (), "#5"); 1213 Assert.AreEqual (typeof (Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType (), "#6"); 1214 Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType (), "#7"); 1215 Assert.AreEqual (typeof (Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType (), "#8"); 1216 Assert.AreEqual (typeof (UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType (), "#9"); 1217 Assert.AreEqual (typeof (UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType (), "#10"); 1218 Assert.AreEqual (typeof (UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType (), "#11"); 1219 Assert.AreEqual (typeof (SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType (), "#12"); 1220 Assert.AreEqual (typeof (DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType (), "#13"); 1221 Assert.AreEqual (typeof (DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType (), "#14"); 1222 Assert.AreEqual (typeof (ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType (), "#15"); 1223 Assert.AreEqual (typeof (CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType (), "#16"); 1224 Assert.AreEqual (typeof (DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType (), "#17"); 1225 Assert.AreEqual (typeof (GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType (), "#18"); 1226 Assert.AreEqual (typeof (TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType (), "#19"); 1227 Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType (), "#20"); 1228 1229 // Tests from bug #71444 1230 Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType (), "#21"); 1231 Assert.AreEqual (typeof (ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType (), "#22"); 1232 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType (), "#23"); 1233 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType (), "#24"); 1234 1235 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType (), "#25"); 1236 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType (), "#26"); 1237 Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType (), "#27"); 1238 1239 // Test from bug #76686 1240 Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType (), "#28"); 1241 new ComponentConverter (null); // Needed for MT linker 1242 Assert.IsTrue (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29"); 1243 Assert.IsTrue (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30"); 1244 Assert.AreEqual (typeof (NullableConverter), TypeDescriptor.GetConverter (typeof (int?)).GetType (), "#31"); 1245 new DateTimeOffsetConverter (); // Needed for MT linker 1246 Assert.AreEqual (typeof (DateTimeOffsetConverter), TypeDescriptor.GetConverter (typeof (DateTimeOffset)).GetType (), "#32"); 1247 } 1248 1249 [Test] TestGetDefaultEvent()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] TestGetDefaultProperty()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] TestGetDefaultProperty2()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] TestGetEvents()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] TestGetProperties()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] TestGetProperties2()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] GetProperties_Order()1466 public void GetProperties_Order () 1467 { 1468 #if MOBILE 1469 // Component.Container will be be linked out (when using Link SDK) if unused 1470 Assert.Null (new Component ().Container, "pre-test"); 1471 #endif 1472 MyComponent com = new MyComponent (new MyContainer ()); 1473 1474 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (com); 1475 Assert.AreEqual (8, col.Count, "#1"); 1476 Assert.AreEqual ("TestProperty", col [0].Name, "#2"); 1477 Assert.AreEqual ("AnotherProperty", col [1].Name, "#3"); 1478 Assert.AreEqual ("YetAnotherProperty", col [2].Name, "#4"); 1479 Assert.AreEqual ("Name", col [3].Name, "#5"); 1480 Assert.AreEqual ("Address", col [4].Name, "#6"); 1481 Assert.AreEqual ("Country", col [5].Name, "#7"); 1482 Assert.AreEqual ("Site", col [6].Name, "#8"); 1483 Assert.AreEqual ("Container", col [7].Name, "#9"); 1484 } 1485 1486 [TypeConverter (typeof (TestConverter))] 1487 class TestConverterClass { 1488 } 1489 1490 class TestConverter : TypeConverter { 1491 public Type Type; 1492 TestConverter(Type type)1493 public TestConverter (Type type) 1494 { 1495 this.Type = type; 1496 } 1497 } 1498 1499 [Test] TestConverterCtorWithArgument()1500 public void TestConverterCtorWithArgument () 1501 { 1502 TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass)); 1503 Assert.IsNotNull (t.GetType (), "#A1"); 1504 Assert.AreEqual (typeof (TestConverter), t.GetType (), "#A2"); 1505 TestConverter converter = (TestConverter) t; 1506 Assert.AreEqual (typeof (TestConverterClass), converter.Type, "#B"); 1507 } 1508 1509 [Test] GetPropertiesIgnoreIndexers()1510 public void GetPropertiesIgnoreIndexers () 1511 { 1512 PropertyDescriptorCollection pc = 1513 TypeDescriptor.GetProperties (typeof (string)); 1514 // There are two string properties: Length and Chars. 1515 // Chars is an indexer. 1516 // 1517 // Future version of CLI might contain some additional 1518 // properties. In that case simply increase the 1519 // number. (Also, it is fine to just remove #2.) 1520 Assert.AreEqual (1, pc.Count, "#1"); 1521 Assert.AreEqual ("Length", pc [0].Name, "#2"); 1522 } 1523 1524 [Test] InterfaceType()1525 public void InterfaceType () 1526 { 1527 Type interface_type = TypeDescriptor.InterfaceType; 1528 Assert.AreEqual ("TypeDescriptorInterface", interface_type.Name, "#A0"); 1529 Assert.IsTrue (interface_type.IsClass, "#A1"); 1530 Assert.IsTrue (interface_type.IsSealed, "#A2"); 1531 Assert.AreEqual (typeof (object), interface_type.BaseType, "#A3"); 1532 Assert.IsFalse (interface_type.IsInterface, "#A4"); 1533 Assert.IsFalse (interface_type.IsPublic, "#A5"); 1534 } 1535 1536 [Test] DynamicAttributesShouldBeReturnedForType()1537 public void DynamicAttributesShouldBeReturnedForType() 1538 { 1539 var testType = typeof(AttrTestClass); 1540 TypeDescriptor.AddAttributes(testType, new ProviderTestAttribute()); 1541 var registeredAttributeTypes = TypeDescriptor.GetAttributes(testType).Cast<Attribute>().Select(attr => attr.GetType()).ToArray(); 1542 Assert.AreEqual(2, registeredAttributeTypes.Length); 1543 Assert.IsTrue (registeredAttributeTypes.Contains (typeof(ProviderTestAttribute)), "Contains-1"); 1544 Assert.IsTrue (registeredAttributeTypes.Contains (typeof(SerializableAttribute)), "Contains-2"); 1545 } 1546 1547 [Test] DynamicAttributesShouldBeReturnedForInstance()1548 public void DynamicAttributesShouldBeReturnedForInstance() 1549 { 1550 var testObj = new AttrTestClass(); 1551 TypeDescriptor.AddAttributes(testObj, new ProviderTestAttribute()); 1552 var registeredAttributeTypes = TypeDescriptor.GetAttributes(testObj).Cast<Attribute>().Select(attr => attr.GetType()).ToArray(); 1553 Assert.AreEqual(2, registeredAttributeTypes.Length); 1554 Assert.IsTrue (registeredAttributeTypes.Contains (typeof(ProviderTestAttribute)), "Contains-1"); 1555 Assert.IsTrue (registeredAttributeTypes.Contains (typeof(SerializableAttribute)), "Contains-2"); 1556 } 1557 1558 [Test] CustomTypeDescriptorsShouldBeUsedForType()1559 public void CustomTypeDescriptorsShouldBeUsedForType() 1560 { 1561 var testType = typeof(CustomDescriptorTestClass); 1562 TypeDescriptor.AddProvider(new CustomDescriptionTestProvider(), testType); 1563 1564 var registeredAttributeTypes = TypeDescriptor.GetAttributes(testType).Cast<Attribute>().Select(attr => attr.GetType()).ToArray(); 1565 Assert.AreEqual(1, registeredAttributeTypes.Length); 1566 Assert.IsTrue (registeredAttributeTypes.Contains (typeof(ProviderTestAttribute)), "Contains-1"); 1567 1568 var registeredPropertyDescriptorTypes = TypeDescriptor.GetProperties(testType).Cast<PropertyDescriptor>().Select(prop => prop.GetType()).ToArray(); 1569 Assert.AreEqual(1, registeredPropertyDescriptorTypes.Length); 1570 Assert.IsTrue (registeredPropertyDescriptorTypes.Contains (typeof(ProviderTestPropertyDescriptor)), "Contains-2"); 1571 } 1572 1573 [Test] CustomTypeDescriptorsShouldBeUsedForInstance()1574 public void CustomTypeDescriptorsShouldBeUsedForInstance() 1575 { 1576 var testObj = new CustomDescriptorTestClass(); 1577 TypeDescriptor.AddProvider(new CustomDescriptionTestProvider(), testObj); 1578 1579 var registeredAttributeTypes = TypeDescriptor.GetAttributes(testObj).Cast<Attribute>().Select(attr => attr.GetType()).ToArray(); 1580 Assert.AreEqual(1, registeredAttributeTypes.Length); 1581 Assert.IsTrue (registeredAttributeTypes.Contains (typeof(ProviderTestAttribute)), "Contains-1"); 1582 1583 var registeredPropertyDescriptorTypes = TypeDescriptor.GetProperties(testObj).Cast<PropertyDescriptor>().Select(prop => prop.GetType()).ToArray(); 1584 Assert.AreEqual(1, registeredPropertyDescriptorTypes.Length); 1585 Assert.IsTrue (registeredPropertyDescriptorTypes.Contains (typeof(ProviderTestPropertyDescriptor)), "Contains-2"); 1586 } 1587 1588 private class CustomDescriptionTestProvider : TypeDescriptionProvider 1589 { GetTypeDescriptor(Type objectType, object instance)1590 public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) 1591 { 1592 return new CustomTestProvider(); 1593 } 1594 } 1595 1596 private class CustomTestProvider : CustomTypeDescriptor 1597 { CustomTestProvider()1598 public CustomTestProvider() 1599 { 1600 } 1601 GetAttributes()1602 public override AttributeCollection GetAttributes() 1603 { 1604 return new AttributeCollection(new ProviderTestAttribute()); 1605 } 1606 GetProperties()1607 public override PropertyDescriptorCollection GetProperties() 1608 { 1609 return new PropertyDescriptorCollection(new PropertyDescriptor[] { new ProviderTestPropertyDescriptor() }); 1610 } 1611 } 1612 1613 private class ProviderTestAttribute : Attribute 1614 { 1615 } 1616 1617 private class ProviderTestPropertyDescriptor : PropertyDescriptor 1618 { ProviderTestPropertyDescriptor()1619 public ProviderTestPropertyDescriptor() 1620 : base("test", new Attribute[0]) 1621 { 1622 } 1623 CanResetValue(object component)1624 public override bool CanResetValue(object component) 1625 { 1626 return false; 1627 } 1628 GetValue(object component)1629 public override object GetValue(object component) 1630 { 1631 return null; 1632 } 1633 ResetValue(object component)1634 public override void ResetValue(object component) 1635 { 1636 } 1637 SetValue(object component, object value)1638 public override void SetValue(object component, object value) 1639 { 1640 } 1641 ShouldSerializeValue(object component)1642 public override bool ShouldSerializeValue(object component) 1643 { 1644 return false; 1645 } 1646 1647 public override Type ComponentType 1648 { 1649 get 1650 { 1651 return typeof(object); 1652 } 1653 } 1654 1655 public override bool IsReadOnly 1656 { 1657 get 1658 { 1659 return false; 1660 } 1661 } 1662 1663 public override Type PropertyType 1664 { 1665 get 1666 { 1667 return typeof(object); 1668 } 1669 } 1670 } 1671 1672 [Serializable] 1673 private class AttrTestClass 1674 { 1675 } 1676 1677 private class CustomDescriptorTestClass 1678 { 1679 } 1680 } 1681 } 1682