-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for LinkAreaEditor (#12718)
Related #10773 Proposed changes Add unit test LinkAreaEditorTests.cs for public properties and method of the LinkAreaEditor. Enable nullability in LinkAreaEditorTests.cs.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...m.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/LinkAreaEditorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using Moq; | ||
using System.ComponentModel; | ||
using static System.Windows.Forms.Design.LinkAreaEditor; | ||
|
||
namespace System.Windows.Forms.Design.Tests; | ||
|
||
public sealed class LinkAreaEditorTests | ||
{ | ||
[Fact] | ||
public void EditValue_ShouldBehaveCorrectly_BasedOnEditorServiceAvailability() | ||
{ | ||
LinkAreaEditor editor = new(); | ||
Mock<ITypeDescriptorContext> context = new(); | ||
Mock<IServiceProvider> provider = new(); | ||
Mock<IWindowsFormsEditorService> editorService = new(); | ||
LinkArea value = new(0, 5); | ||
using Label instance = new() { Text = "Initial Text" }; | ||
var property = TypeDescriptor.GetProperties(instance)["Text"]; | ||
|
||
provider.Setup(p => p.GetService(typeof(IWindowsFormsEditorService))).Returns((object?)null); | ||
editor.EditValue(context.Object, provider.Object, value).Should().Be(value); | ||
|
||
provider.Setup(p => p.GetService(typeof(IWindowsFormsEditorService))).Returns(editorService.Object); | ||
editorService.Setup(es => es.ShowDialog(It.IsAny<Form>())).Returns(DialogResult.OK); | ||
var result = editor.EditValue(context.Object, provider.Object, value); | ||
result.Should().BeOfType<LinkArea>(); | ||
if (result is LinkArea linkAreaResult) | ||
{ | ||
linkAreaResult.Length.Should().Be(value.Length); | ||
} | ||
|
||
property?.SetValue(instance, "Updated Text"); | ||
context.Setup(c => c.Instance).Returns(instance); | ||
editor.EditValue(context.Object, provider.Object, value).Should().NotBeNull(); | ||
property?.GetValue(instance).Should().Be("Updated Text"); | ||
|
||
editorService.Setup(es => es.ShowDialog(It.IsAny<Form>())).Callback<Form>(form => | ||
{ | ||
if (form is LinkAreaUI linkAreaUI) | ||
{ | ||
linkAreaUI.SampleText = "Updated Text2"; | ||
} | ||
}).Returns(DialogResult.OK); | ||
editor.EditValue(context.Object, provider.Object, value).Should().NotBeNull(); | ||
property?.GetValue(instance).Should().Be("Updated Text2"); | ||
} | ||
} |