コード例 #1
0
        public void GetPropertyType()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.Equal(component.Property.GetType(), propertyDescriptor.PropertyType);
        }
コード例 #2
0
        public void ResetValueReturnsFalseWhenValueEqualsDefault()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.False(propertyDescriptor.CanResetValue(component));
        }
コード例 #3
0
ファイル: TypeDescriptorTests.cs プロジェクト: ESgarbi/corefx
        public void AddAttribute()
        {
            var component = new DescriptorTestComponent();
            var addedAttribute = new DescriptorTestAttribute("expected string");

            TypeDescriptor.AddAttributes(component.GetType(), addedAttribute);

            AttributeCollection attributes = TypeDescriptor.GetAttributes(component);
            Assert.True(attributes.Contains(addedAttribute));
        }
コード例 #4
0
        public void CopyConstructorAddsAttribute()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor oldPropertyDescriptor = properties.Find(nameof(component.Property), false);
            var newAttribute = new DescriptorTestAttribute();

            PropertyDescriptor newPropertyDescriptor = TypeDescriptor.CreateProperty(component.GetType(), oldPropertyDescriptor, newAttribute);

            Assert.True(newPropertyDescriptor.Attributes.Contains(newAttribute));
        }
コード例 #5
0
        public void CopyConstructorAddsAttribute()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor oldPropertyDescriptor = properties.Find(nameof(component.Property), false);
            var newAttribute = new DescriptorTestAttribute();

            PropertyDescriptor newPropertyDescriptor = TypeDescriptor.CreateProperty(component.GetType(), oldPropertyDescriptor, newAttribute);

            Assert.True(newPropertyDescriptor.Attributes.Contains(newAttribute));
        }
コード例 #6
0
ファイル: TypeDescriptorTests.cs プロジェクト: ESgarbi/corefx
        public void GetAssociationReturnsExpectedObject()
        {
            var primaryObject = new DescriptorTestComponent();
            var secondaryObject = new MockEventDescriptor();
            TypeDescriptor.CreateAssociation(primaryObject, secondaryObject);

            var associatedObject = TypeDescriptor.GetAssociation(secondaryObject.GetType(), primaryObject);

            Assert.IsType(secondaryObject.GetType(), associatedObject);
            Assert.Equal(secondaryObject, associatedObject);
        }
コード例 #7
0
        public void GetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = 37;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            var retrievedValue = propertyDescriptor.GetValue(component);

            Assert.Equal(component.Property, retrievedValue);
        }
コード例 #8
0
        public void AddAttribute()
        {
            var component      = new DescriptorTestComponent();
            var addedAttribute = new DescriptorTestAttribute("expected string");

            TypeDescriptor.AddAttributes(component.GetType(), addedAttribute);

            AttributeCollection attributes = TypeDescriptor.GetAttributes(component);

            Assert.True(attributes.Contains(addedAttribute));
        }
コード例 #9
0
        public void ResetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue - 1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            // this should set the property's value to that provided by the DefaultValueAttribute
            propertyDescriptor.ResetValue(component);

            Assert.Equal(DescriptorTestComponent.DefaultPropertyValue, component.Property);
        }
コード例 #10
0
        public void RemoveAddedEventHandler()
        {
            var             component       = new DescriptorTestComponent();
            Action          eventHandler    = () => Assert.True(false, "EventDescriptor failed to remove an event handler");
            EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            eventDescriptor.RemoveEventHandler(component, eventHandler);

            // component.Event should now have no handler => raising it should throw a NullReferenceException
            Assert.Throws <NullReferenceException>(() => component.RaiseEvent());
        }
コード例 #11
0
        public void GetAssociationReturnsExpectedObject()
        {
            var primaryObject   = new DescriptorTestComponent();
            var secondaryObject = new MockEventDescriptor();

            TypeDescriptor.CreateAssociation(primaryObject, secondaryObject);

            var associatedObject = TypeDescriptor.GetAssociation(secondaryObject.GetType(), primaryObject);

            Assert.IsType(secondaryObject.GetType(), associatedObject);
            Assert.Equal(secondaryObject, associatedObject);
        }
コード例 #12
0
        public void RemoveAddedEventHandler()
        {
            var component = new DescriptorTestComponent();
            Action eventHandler = () => Assert.True(false, "EventDescriptor failed to remove an event handler");
            EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            eventDescriptor.RemoveEventHandler(component, eventHandler);

            // component.Event should now have no handler => raising it should throw a NullReferenceException
            Assert.Throws(typeof(NullReferenceException), () => component.RaiseEvent());
        }
コード例 #13
0
        public void RaiseAddedEventHandler()
        {
            var component = new DescriptorTestComponent();
            var eventHandlerWasCalled = false;
            Action eventHandler = () => eventHandlerWasCalled = true;
            var eventInfo = typeof(DescriptorTestComponent).GetTypeInfo().GetEvent(nameof(DescriptorTestComponent.ActionEvent));
            EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            component.RaiseEvent();

            Assert.True(eventHandlerWasCalled);
        }
コード例 #14
0
        public void RaiseAddedEventHandler()
        {
            var             component             = new DescriptorTestComponent();
            var             eventHandlerWasCalled = false;
            Action          eventHandler          = () => eventHandlerWasCalled = true;
            var             eventInfo             = typeof(DescriptorTestComponent).GetEvent(nameof(DescriptorTestComponent.ActionEvent));
            EventDescriptor eventDescriptor       = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            component.RaiseEvent();

            Assert.True(eventHandlerWasCalled);
        }
コード例 #15
0
        public void RaiseAddedValueChangedHandler()
        {
            var component  = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);
            var          handlerWasCalled         = false;
            EventHandler valueChangedHandler      = (_, __) => handlerWasCalled = true;

            propertyDescriptor.AddValueChanged(component, valueChangedHandler);
            propertyDescriptor.SetValue(component, int.MaxValue);

            Assert.True(handlerWasCalled);
        }
コード例 #16
0
        public void RaiseAddedValueChangedHandler()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);
            var handlerWasCalled = false;
            EventHandler valueChangedHandler = (_, __) => handlerWasCalled = true;

            propertyDescriptor.AddValueChanged(component, valueChangedHandler);
            propertyDescriptor.SetValue(component, int.MaxValue);

            Assert.True(handlerWasCalled);
        }
コード例 #17
0
        public void GetAssociationReturnsDesigner()
        {
            var designer     = new MockDesigner();
            var designerHost = new MockDesignerHost();
            var component    = new DescriptorTestComponent();

            designerHost.AddDesigner(component, designer);
            component.AddService(typeof(IDesignerHost), designerHost);

            object associatedObject = TypeDescriptor.GetAssociation(designer.GetType(), component);

            Assert.IsType <MockDesigner>(associatedObject);
            Assert.Same(designer, associatedObject);
        }
コード例 #18
0
        public void PropertyDescriptorCanBeCreatedThatAccessesNonPublicProperty()
        {
            var component = new DescriptorTestComponent();

            // Create a new property that "exposes" a protected property on our component.
            PropertyDescriptor property = TypeDescriptor.CreateProperty(
                componentType: typeof(DescriptorTestComponent),
                name: DescriptorTestComponent.ProtectedStringPropertyName,
                type: typeof(string));

            const string PropertyValue = "Test";

            property.SetValue(component, PropertyValue);
            Assert.Equal(PropertyValue, property.GetValue(component));
            Assert.Equal(PropertyValue, component.ProtectedStringPropertyValue);
        }
コード例 #19
0
ファイル: TypeDescriptorTests.cs プロジェクト: ESgarbi/corefx
        public void AddAndRemoveProvider()
        {
            var provider = new InvocationRecordingTypeDescriptionProvider();
            var component = new DescriptorTestComponent();
            TypeDescriptor.AddProvider(provider, component);

            var retrievedProvider = TypeDescriptor.GetProvider(component);
            retrievedProvider.GetCache(component);

            Assert.True(provider.ReceivedCall);

            provider.Reset();
            TypeDescriptor.RemoveProvider(provider, component);
            retrievedProvider = TypeDescriptor.GetProvider(component);
            retrievedProvider.GetCache(component);

            Assert.False(provider.ReceivedCall);
        }
コード例 #20
0
        public void AddAndRemoveProvider()
        {
            var provider  = new InvocationRecordingTypeDescriptionProvider();
            var component = new DescriptorTestComponent();

            TypeDescriptor.AddProvider(provider, component);

            var retrievedProvider = TypeDescriptor.GetProvider(component);

            retrievedProvider.GetCache(component);

            Assert.True(provider.ReceivedCall);

            provider.Reset();
            TypeDescriptor.RemoveProvider(provider, component);
            retrievedProvider = TypeDescriptor.GetProvider(component);
            retrievedProvider.GetCache(component);

            Assert.False(provider.ReceivedCall);
        }
コード例 #21
0
        public void RemoveSingleAssociation()
        {
            var primaryObject          = new DescriptorTestComponent();
            var firstAssociatedObject  = new MockEventDescriptor();
            var secondAssociatedObject = new MockPropertyDescriptor();

            TypeDescriptor.CreateAssociation(primaryObject, firstAssociatedObject);
            TypeDescriptor.CreateAssociation(primaryObject, secondAssociatedObject);

            TypeDescriptor.RemoveAssociation(primaryObject, firstAssociatedObject);

            // the second association should remain
            var secondAssociation = TypeDescriptor.GetAssociation(secondAssociatedObject.GetType(), primaryObject);

            Assert.Equal(secondAssociatedObject, secondAssociation);

            // the first association should not
            var firstAssociation = TypeDescriptor.GetAssociation(firstAssociatedObject.GetType(), primaryObject);

            Assert.NotEqual(firstAssociatedObject, firstAssociation);
        }
コード例 #22
0
        public void RemoveAssociationsRemovesAllAssociations()
        {
            var primaryObject          = new DescriptorTestComponent();
            var firstAssociatedObject  = new MockEventDescriptor();
            var secondAssociatedObject = new MockPropertyDescriptor();

            TypeDescriptor.CreateAssociation(primaryObject, firstAssociatedObject);
            TypeDescriptor.CreateAssociation(primaryObject, secondAssociatedObject);

            TypeDescriptor.RemoveAssociations(primaryObject);

            // GetAssociation never returns null. The default implementation returns the
            // primary object when an association doesn't exist. This isn't documented,
            // however, so here we only verify that the formerly associated objects aren't returned.
            var firstAssociation = TypeDescriptor.GetAssociation(firstAssociatedObject.GetType(), primaryObject);

            Assert.NotEqual(firstAssociatedObject, firstAssociation);
            var secondAssociation = TypeDescriptor.GetAssociation(secondAssociatedObject.GetType(), primaryObject);

            Assert.NotEqual(secondAssociatedObject, secondAssociation);
        }
コード例 #23
0
        public void PropertyDescriptorCanBeCreatedThatAccessesNonPublicPropertyOnAssociatedDesigner()
        {
            var designer     = new MockDesigner();
            var designerHost = new MockDesignerHost();
            var component    = new DescriptorTestComponent();

            designerHost.AddDesigner(component, designer);
            component.AddService(typeof(IDesignerHost), designerHost);

            PropertyDescriptorCollection properties     = TypeDescriptor.GetProperties(component);
            PropertyDescriptor           stringProperty = properties[nameof(DescriptorTestComponent.StringProperty)];

            Assert.NotNull(stringProperty);

            // Create new property that "wraps" stringProperty and redirects to the designer.
            // Note that a ReadOnlyAttribute is added to ensure that we can set it
            PropertyDescriptor newStringProperty = TypeDescriptor.CreateProperty(
                componentType: typeof(MockDesigner),
                oldPropertyDescriptor: stringProperty,
                attributes: new[] { ReadOnlyAttribute.No });

            // The property descriptor should be redirected to reflect over the designer.
            const string PropertyValue = "Test";

            Assert.False(designer.StringPropertyHasBeenSet);
            newStringProperty.SetValue(component, PropertyValue);
            Assert.Empty(component.StringProperty);
            Assert.Equal(PropertyValue, newStringProperty.GetValue(component));
            Assert.True(designer.StringPropertyHasBeenSet);

            Assert.False(designer.ShouldSerializeStringPropertyCalled);
            Assert.True(newStringProperty.ShouldSerializeValue(component));
            Assert.True(designer.ShouldSerializeStringPropertyCalled);

            Assert.False(designer.ResetStringPropertyCalled);
            newStringProperty.ResetValue(component);
            Assert.True(designer.ResetStringPropertyCalled);
        }
コード例 #24
0
ファイル: TypeDescriptorTests.cs プロジェクト: ESgarbi/corefx
        public void GetEvents()
        {
            var component = new DescriptorTestComponent();

            EventDescriptorCollection events = TypeDescriptor.GetEvents(component);

            Assert.Equal(2, events.Count);
        }
コード例 #25
0
        public void ResetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue - 1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            // this should set the property's value to that provided by the DefaultValueAttribute
            propertyDescriptor.ResetValue(component);

            Assert.Equal(DescriptorTestComponent.DefaultPropertyValue, component.Property);
        }
コード例 #26
0
        public void ShouldSerializeValueReturnsTrueWhenValueIsNotDefault()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue - 1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.True(propertyDescriptor.ShouldSerializeValue(component));
        }
コード例 #27
0
ファイル: TypeDescriptorTests.cs プロジェクト: ESgarbi/corefx
        public void RemoveSingleAssociation()
        {
            var primaryObject = new DescriptorTestComponent();
            var firstAssociatedObject = new MockEventDescriptor();
            var secondAssociatedObject = new MockPropertyDescriptor();
            TypeDescriptor.CreateAssociation(primaryObject, firstAssociatedObject);
            TypeDescriptor.CreateAssociation(primaryObject, secondAssociatedObject);

            TypeDescriptor.RemoveAssociation(primaryObject, firstAssociatedObject);

            // the second association should remain
            var secondAssociation = TypeDescriptor.GetAssociation(secondAssociatedObject.GetType(), primaryObject);
            Assert.Equal(secondAssociatedObject, secondAssociation);

            // the first association should not
            var firstAssociation = TypeDescriptor.GetAssociation(firstAssociatedObject.GetType(), primaryObject);
            Assert.NotEqual(firstAssociatedObject, firstAssociation);
        }
コード例 #28
0
        public void GetValueDoesNotHandleExceptions()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.PropertyWhichThrows), false);

            Assert.ThrowsAny<Exception>(() => propertyDescriptor.GetValue(component));
        }
コード例 #29
0
        public void GetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = 37;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            var retrievedValue = propertyDescriptor.GetValue(component);

            Assert.Equal(component.Property, retrievedValue);
        }
コード例 #30
0
ファイル: TypeDescriptorTests.cs プロジェクト: ESgarbi/corefx
        public void RemoveAssociationsRemovesAllAssociations()
        {
            var primaryObject = new DescriptorTestComponent();
            var firstAssociatedObject = new MockEventDescriptor();
            var secondAssociatedObject = new MockPropertyDescriptor();
            TypeDescriptor.CreateAssociation(primaryObject, firstAssociatedObject);
            TypeDescriptor.CreateAssociation(primaryObject, secondAssociatedObject);

            TypeDescriptor.RemoveAssociations(primaryObject);

            // GetAssociation never returns null. The default implementation returns the
            // primary object when an association doesn't exist. This isn't documented,
            // however, so here we only verify that the formerly associated objects aren't returned.
            var firstAssociation = TypeDescriptor.GetAssociation(firstAssociatedObject.GetType(), primaryObject);
            Assert.NotEqual(firstAssociatedObject, firstAssociation);
            var secondAssociation = TypeDescriptor.GetAssociation(secondAssociatedObject.GetType(), primaryObject);
            Assert.NotEqual(secondAssociatedObject, secondAssociation);
        }