Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,35 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul

public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (propertyValues == null)
{
throw new ArgumentNullException(nameof(propertyValues));
}

Padding original = (Padding)context.PropertyDescriptor.GetValue(context.Instance);
var original = context?.PropertyDescriptor?.GetValue(context.Instance);
try
{
int all = (int)propertyValues[nameof(Padding.All)];
if (original.All != all)
// When incrementally changing an existing Padding instance e.g. through a PropertyGrid
// the expected behavior is that a change of Padding.All will override the now outdated
// other properties of the original padding.
//
// If we are not incrementally changing an existing instance (i.e. have no original value)
// then we can just select the individual components passed in the full set of properties
// and the Padding constructor will determine whether they are all the same or not.

if (original is Padding originalPadding)
{
return new Padding(all);
}
else
{
return new Padding(
(int)propertyValues[nameof(Padding.Left)],
(int)propertyValues[nameof(Padding.Top)],
(int)propertyValues[nameof(Padding.Right)],
(int)propertyValues[nameof(Padding.Bottom)]
);
int all = (int)propertyValues[nameof(Padding.All)];
if (originalPadding.All != all)
return new Padding(all);
}

return new Padding(
(int)propertyValues[nameof(Padding.Left)],
(int)propertyValues[nameof(Padding.Top)],
(int)propertyValues[nameof(Padding.Right)],
(int)propertyValues[nameof(Padding.Bottom)]
);
}
catch (InvalidCastException invalidCast)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,21 @@ public void PaddingConverter_CreateInstance_ValidPropertyValuesAll_ReturnsExpect
}

[Fact]
public void PaddingConverter_CreateInstance_NullContext_ThrowsArgumentNullException()
public void PaddingConverter_CreateInstance_ValidPropertyValuesNullContext_ReturnsExpected()
{
var converter = new PaddingConverter();
Assert.Throws<ArgumentNullException>("context", () => converter.CreateInstance(null, new Dictionary<string, object>()));
Padding expected = new Padding(1, 2, 3, 4);
Padding padding = Assert.IsType<Padding>(converter.CreateInstance(
null, new Dictionary<string, object>
{
{nameof(Padding.All), expected.All},
{nameof(Padding.Left), expected.Left},
{nameof(Padding.Top), expected.Top},
{nameof(Padding.Right), expected.Right},
{nameof(Padding.Bottom), expected.Bottom}
})
);
Assert.Equal(expected, padding);
}

[Fact]
Expand Down Expand Up @@ -355,7 +366,7 @@ public void PaddingConverter_CreateInstance_InvalidPropertyValueType_ThrowsArgum
}

[Fact]
public void PaddingConverter_CreateInstance_InvalidInstanceType_ThrowsInvalidCastException()
public void PaddingConverter_CreateInstance_UnknownInstanceType_ReturnsExpected()
{
var converter = new PaddingConverter();
var mockContext = new Mock<ITypeDescriptorContext>(MockBehavior.Strict);
Expand All @@ -374,7 +385,10 @@ public void PaddingConverter_CreateInstance_InvalidInstanceType_ThrowsInvalidCas
{nameof(Padding.Right), 3},
{nameof(Padding.Bottom), 4},
};
Assert.Throws<InvalidCastException>(() => converter.CreateInstance(mockContext.Object, propertyValues));

Padding expected = new Padding(2, 2, 3, 4);
Padding padding = Assert.IsType<Padding>(converter.CreateInstance(mockContext.Object, propertyValues));
Assert.Equal(expected, padding);
}

[Fact]
Expand Down