|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.ComponentModel.Design; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace System.ComponentModel.Tests |
| 11 | +{ |
| 12 | + public class ReflectTypeDescriptionProviderTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public void GetAttributes_Skips_ComVisibleAttribute_And_GuidAttribute_And_InterfaceTypeAttribute() |
| 16 | + { |
| 17 | + AttributeCollection attributeCollection = TypeDescriptor.GetAttributes(typeof(TestClass1)); |
| 18 | + Assert.NotEmpty(attributeCollection); |
| 19 | + IEnumerable<Attribute> attributes = attributeCollection.Cast<Attribute>(); |
| 20 | + Attribute attribute = Assert.Single(attributes); |
| 21 | + Assert.IsType<DescriptionAttribute>(attribute); |
| 22 | + Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(ComVisibleAttribute)); |
| 23 | + Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(GuidAttribute)); |
| 24 | + Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(InterfaceTypeAttribute)); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void GetExtenderProviders_ReturnResultFromContainerComponents_WhenComponentSiteServiceNull() |
| 29 | + { |
| 30 | + using ComponentExtendedProvider testComponent = new ComponentExtendedProvider(); |
| 31 | + testComponent.Site = new TestSiteWithoutService(); |
| 32 | + testComponent.Disposed += (object obj, EventArgs args) => { }; |
| 33 | + PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(testComponent); |
| 34 | + PropertyDescriptor testPropDescriptor = propertyDescriptorCollection["TestProp"]; |
| 35 | + Assert.NotNull(testPropDescriptor); |
| 36 | + ExtenderProvidedPropertyAttribute extenderProvidedPropertyAttribute = testPropDescriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; |
| 37 | + Assert.NotNull(extenderProvidedPropertyAttribute); |
| 38 | + Assert.IsType<ComponentExtendedProvider>(extenderProvidedPropertyAttribute.Provider); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void GetExtenderProviders_ReturnResultFromComponentSiteService_WhenComponentSiteServiceNotNull() |
| 43 | + { |
| 44 | + using TestComponent testComponent = new TestComponent(); |
| 45 | + testComponent.Site = new TestSiteWithService(); |
| 46 | + testComponent.Disposed += (object obj, EventArgs args) => { }; |
| 47 | + PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(testComponent); |
| 48 | + PropertyDescriptor testPropDescriptor = propertyDescriptorCollection["TestProp"]; |
| 49 | + Assert.NotNull(testPropDescriptor); |
| 50 | + ExtenderProvidedPropertyAttribute extenderProvidedPropertyAttribute = testPropDescriptor.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; |
| 51 | + Assert.NotNull(extenderProvidedPropertyAttribute); |
| 52 | + Assert.IsType<TestExtenderProvider>(extenderProvidedPropertyAttribute.Provider); |
| 53 | + } |
| 54 | + |
| 55 | + [ComVisible(true), Guid("4a223ebb-fe95-4649-94e5-2e5cc8f5f4e9"), InterfaceType(1)] |
| 56 | + internal interface TestInterface |
| 57 | + { |
| 58 | + void Action(); |
| 59 | + } |
| 60 | + |
| 61 | + [Description] |
| 62 | + internal class TestClass1 : TestInterface |
| 63 | + { |
| 64 | + public void Action() { } |
| 65 | + } |
| 66 | + |
| 67 | + internal class TestComponent : IComponent |
| 68 | + { |
| 69 | + public ISite? Site { get; set; } |
| 70 | + |
| 71 | + public event EventHandler? Disposed; |
| 72 | + |
| 73 | + public void Dispose() |
| 74 | + { |
| 75 | + if (Disposed != null) |
| 76 | + { |
| 77 | + Disposed(this, new EventArgs()); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [ProvideProperty("TestProp", "System.Object")] |
| 83 | + internal class ComponentExtendedProvider : IComponent, IExtenderProvider |
| 84 | + { |
| 85 | + private int _testProp; |
| 86 | + |
| 87 | + public ISite? Site { get; set; } |
| 88 | + |
| 89 | + public event EventHandler? Disposed; |
| 90 | + |
| 91 | + public bool CanExtend(object extendee) => true; |
| 92 | + |
| 93 | + public int GetTestProp(object extendee) => _testProp; |
| 94 | + |
| 95 | + public void SetTestProp(object extendee, int value) |
| 96 | + { |
| 97 | + _testProp = value; |
| 98 | + } |
| 99 | + |
| 100 | + public void Dispose() |
| 101 | + { |
| 102 | + if (Disposed != null) |
| 103 | + { |
| 104 | + Disposed(this, new EventArgs()); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + internal class TestSiteWithoutService : ISite |
| 110 | + { |
| 111 | + private TestComponent _testComponent = new TestComponent(); |
| 112 | + private TestContainer _testContainer = new TestContainer(); |
| 113 | + |
| 114 | + public IComponent Component => _testComponent; |
| 115 | + |
| 116 | + public IContainer? Container => _testContainer; |
| 117 | + |
| 118 | + public bool DesignMode => true; |
| 119 | + |
| 120 | + public string? Name { get; set; } |
| 121 | + |
| 122 | + public object? GetService(Type serviceType) => null; |
| 123 | + } |
| 124 | + |
| 125 | + internal class TestSiteWithService : ISite |
| 126 | + { |
| 127 | + private TestComponent _testComponent = new TestComponent(); |
| 128 | + private TestContainer _testContainer = new TestContainer(); |
| 129 | + private TestExtenderListService _testExtenderListService = new TestExtenderListService(); |
| 130 | + |
| 131 | + public IComponent Component => _testComponent; |
| 132 | + |
| 133 | + public IContainer? Container => _testContainer; |
| 134 | + |
| 135 | + public bool DesignMode => true; |
| 136 | + |
| 137 | + public string? Name { get; set; } |
| 138 | + |
| 139 | + public object? GetService(Type serviceType) => _testExtenderListService; |
| 140 | + } |
| 141 | + |
| 142 | + internal class TestContainer : IContainer |
| 143 | + { |
| 144 | + private List<IComponent> _components = new List<IComponent> { new ComponentExtendedProvider() }; |
| 145 | + |
| 146 | + public ComponentCollection Components => new ComponentCollection(_components.ToArray()); |
| 147 | + |
| 148 | + public void Add(IComponent? component) => _components.Add(component); |
| 149 | + public void Add(IComponent? component, string? name) => _components.Add(component); |
| 150 | + public void Dispose() => _components.Clear(); |
| 151 | + public void Remove(IComponent? component) => _components.Remove(component); |
| 152 | + } |
| 153 | + |
| 154 | + [ProvideProperty("TestProp", "System.Object")] |
| 155 | + internal class TestExtenderProvider : IExtenderProvider |
| 156 | + { |
| 157 | + private int _testProp; |
| 158 | + |
| 159 | + public bool CanExtend(object extendee) => true; |
| 160 | + |
| 161 | + public int GetTestProp(object extendee) => _testProp; |
| 162 | + |
| 163 | + public void SetTestProp(object extendee, int value) |
| 164 | + { |
| 165 | + _testProp = value; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + internal class TestExtenderListService : IExtenderListService |
| 170 | + { |
| 171 | + private TestExtenderProvider _testExtenderProvider = new TestExtenderProvider(); |
| 172 | + |
| 173 | + public IExtenderProvider[] GetExtenderProviders() => new[] { _testExtenderProvider }; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments