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); }
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 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 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 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); }
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 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 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 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 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 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 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); }
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); }
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); }
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); }
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); }
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); }
public void GetEvents() { var component = new DescriptorTestComponent(); EventDescriptorCollection events = TypeDescriptor.GetEvents(component); Assert.Equal(2, events.Count); }
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 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)); }