Skip to content
Draft
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 @@ -50,8 +50,34 @@ internal EditorServiceContext(ComponentDesigner designer, PropertyDescriptor? pr
UITypeEditor editor = descriptor.GetEditor<UITypeEditor>()!;
// Get value to edit
object? value = descriptor.GetValue(objectToChange);

// Edit value
object? newValue = editor.EditValue(context, context, value);
object? newValue;
try
{
newValue = editor.EditValue(context, context, value);
}
catch (Exception ex) when (!ex.IsCriticalException())
{
// Display the error to the user
IUIService? uiService = context.GetService<IUIService>();
if (uiService is not null)
{
uiService.ShowError(ex);
}
else
{
string message = ex.Message;
if (string.IsNullOrEmpty(message))
{
message = ex.ToString();
}

MessageBox.Show(message, null, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

return value;
}

if (newValue != value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,37 @@ public void EditValue_ShouldNotUpdatePropertyValue_WhenEditorReturnsSameValue()
_mockPropertyDescriptor.Verify(p => p.SetValue(It.IsAny<object>(), It.IsAny<object>()), Times.Never);
}

[Fact]
public void EditValue_ShouldShowError_WhenEditorThrowsException()
{
List<string> list = new() { "Test" };
ArgumentException expectedException = new("DataSource is set");

_mockPropertyDescriptor.Setup(p => p.GetValue(_component)).Returns(list);
_mockPropertyDescriptor.Setup(p => p.PropertyType).Returns(typeof(List<string>));
_mockPropertyDescriptor.Setup(p => p.Name).Returns("Items");
_mockPropertyDescriptor.Setup(p => p.Attributes).Returns(new AttributeCollection(null));

_mockEditor
.Setup(e => e.EditValue(It.IsAny<ITypeDescriptorContext>(), It.IsAny<IServiceProvider>(), list))
.Throws(expectedException);

_mockPropertyDescriptor
.Setup(p => p.GetEditor(typeof(UITypeEditor)))
.Returns(_mockEditor.Object);

_mockSite.Setup(s => s.GetService(typeof(IUIService))).Returns(_mockUIService.Object);
_component.Site = _mockSite.Object;

TypeDescriptor.AddProvider(new TypeDescriptionProviderMock(_mockPropertyDescriptor.Object), _component);

object? result = EditorServiceContext.EditValue(_designer, _component, "Items");

result.Should().BeSameAs(list);
_mockUIService.Verify(s => s.ShowError(expectedException), Times.Once);
_mockPropertyDescriptor.Verify(p => p.SetValue(It.IsAny<object>(), It.IsAny<object>()), Times.Never);
}

[Fact]
public void Container_ShouldReturnNull_WhenComponentSiteIsNull()
{
Expand Down