public void GetComponentType() { var component = new DescriptorTestComponent(); var properties = TypeDescriptor.GetProperties(component.GetType()); PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false); Assert.Equal(component.GetType(), propertyDescriptor.ComponentType); }
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)); }
public void GetValueDoesNotHandleExceptions() { var component = new DescriptorTestComponent(); var properties = TypeDescriptor.GetProperties(component.GetType()); PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.PropertyWhichThrows), false); Assert.Throws <TargetInvocationException>(() => propertyDescriptor.GetValue(component)); }
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)); }
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)); }
public void ShouldSerializeValueHandlesEnumWithBackingTypeDefaultValue() { var component = new DescriptorTestComponent(); component.EnumProperty = DescriptorTestEnum.Value1; var properties = TypeDescriptor.GetProperties(component.GetType()); PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.EnumProperty), false); Assert.True(propertyDescriptor.ShouldSerializeValue(component)); }
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)); }
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); }
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()); }
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()); }
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); }
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); }
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); }
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); }
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)); }