PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System/Test/System.ComponentModel/TypeDescriptorTests.cs

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