-
Notifications
You must be signed in to change notification settings - Fork 1k
Add unit tests for ParentControlDesigner #13173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Tanya-Solyanik
merged 4 commits into
dotnet:main
from
MelonWang1:Add_unit_tests_for_ParentControlDesigner
Apr 8, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
234 changes: 234 additions & 0 deletions
234
...ws.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ParentControlDesignerTests.cs
This file contains hidden or 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,234 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
using System.ComponentModel.Design; | ||
using System.Drawing; | ||
using System.Drawing.Design; | ||
using System.Reflection; | ||
using System.Windows.Forms.Design.Behavior; | ||
using Moq; | ||
|
||
namespace System.Windows.Forms.Design.Tests; | ||
public class ParentControlDesignerTests : IDisposable | ||
{ | ||
private readonly ParentControlDesigner _designer; | ||
private readonly Control _control; | ||
|
||
public ParentControlDesignerTests() | ||
{ | ||
_designer = new(); | ||
_control = new(); | ||
_designer.Initialize(_control); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_control.Dispose(); | ||
_designer.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void SnapLines_ReturnsExpectedSnapLines() | ||
{ | ||
IList<SnapLine> snapLines = _designer.SnapLines.Cast<SnapLine>().ToList(); | ||
|
||
snapLines.Should().NotBeNull(); | ||
snapLines.Should().BeOfType<List<SnapLine>>(); | ||
((List<SnapLine>)snapLines).Should().NotBeEmpty(); | ||
((List<SnapLine>)snapLines).Should().OnlyHaveUniqueItems(); | ||
|
||
((List<SnapLine>)snapLines).Should() | ||
.Contain(sl => sl.SnapLineType == SnapLineType.Top) | ||
.And.Contain(sl => sl.SnapLineType == SnapLineType.Bottom) | ||
.And.Contain(sl => sl.SnapLineType == SnapLineType.Left) | ||
.And.Contain(sl => sl.SnapLineType == SnapLineType.Right); | ||
} | ||
|
||
[Fact] | ||
public void CanParent_ControlDesigner_ReturnsExpected() | ||
{ | ||
Mock<ControlDesigner> mockControlDesigner = new(); | ||
Mock<Control> mockControl = new(); | ||
mockControlDesigner.Setup(cd => cd.Control).Returns(mockControl.Object); | ||
|
||
_designer.CanParent(mockControlDesigner.Object).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void CanParent_Control_ReturnsExpected() | ||
{ | ||
using TestControl testControl = new() { ContainsControl = false }; | ||
|
||
_designer.CanParent(testControl).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void GetGlyphs_SelectionTypeSelected_AddsContainerSelectorGlyph() | ||
{ | ||
Mock<IServiceProvider> mockServiceProvider = new(); | ||
Mock<ISite> mockSite = new(); | ||
Mock<IDesignerHost> mockDesignerHost = new(); | ||
Mock<IComponentChangeService> mockChangeService = new(); | ||
_control.Site = mockSite.Object; | ||
mockSite.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(mockDesignerHost.Object); | ||
mockSite.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(mockChangeService.Object); | ||
|
||
Mock<DesignerFrame> mockDesignerFrame = new(mockSite.Object) { CallBase = true }; | ||
BehaviorService behaviorService = new(mockServiceProvider.Object, mockDesignerFrame.Object); | ||
mockServiceProvider.Setup(sp => sp.GetService(typeof(BehaviorService))).Returns(() => behaviorService); | ||
mockSite.Setup(s => s.GetService(typeof(BehaviorService))).Returns(behaviorService); | ||
|
||
_designer.TestAccessor().Dynamic._behaviorService = behaviorService; | ||
_designer.TestAccessor().Dynamic._host = mockDesignerHost.Object; | ||
_designer.TestAccessor().Dynamic._changeService = mockChangeService.Object; | ||
|
||
GlyphSelectionType selectionType = GlyphSelectionType.Selected; | ||
|
||
GlyphCollection glyphs = _designer.GetGlyphs(selectionType); | ||
|
||
glyphs.Should().BeOfType<GlyphCollection>(); | ||
glyphs.OfType<ContainerSelectorGlyph>().Count().Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void InitializeNewComponent_WithNullDefaultValues_DoesNotThrow() | ||
{ | ||
_designer.Invoking(d => d.InitializeNewComponent(null)).Should().NotThrow(); | ||
} | ||
|
||
[WinFormsFact] | ||
public void InitializeNewComponent_ShouldReparentControls_WhenAllowControlLassoIsTrue() | ||
{ | ||
Mock<IDesignerHost> mockDesignerHost = new(); | ||
Mock<ISite> mockSite = new(); | ||
Mock<ISelectionService> mockSelectionService = new(); | ||
Mock<IComponentChangeService> mockChangeService = new(); | ||
|
||
using TestControl testComponent = new(); | ||
using TestControl parentComponent = new(); | ||
|
||
mockSite.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(mockDesignerHost.Object); | ||
mockSite.Setup(s => s.GetService(typeof(ISelectionService))).Returns(mockSelectionService.Object); | ||
mockSite.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(mockChangeService.Object); | ||
testComponent.Site = mockSite.Object; | ||
parentComponent.Site = mockSite.Object; | ||
|
||
mockDesignerHost.Setup(h => h.GetDesigner(parentComponent)).Returns(_designer); | ||
|
||
_designer.Initialize(testComponent); | ||
_designer.TestAccessor().Dynamic._changeService = mockChangeService.Object; | ||
|
||
Dictionary<string, object> defaultValues = new() | ||
{ | ||
{ "Size", new Size(100, 100) }, | ||
{ "Location", new Point(10, 10) }, | ||
{ "Parent", parentComponent } | ||
}; | ||
|
||
DisableDragDrop(testComponent); | ||
DisableDragDrop(parentComponent); | ||
|
||
_designer.InitializeNewComponent(defaultValues); | ||
|
||
mockDesignerHost.Verify(h => h.GetDesigner(parentComponent), Times.AtMost(2)); | ||
} | ||
|
||
public class TestControl : Control | ||
{ | ||
public bool ContainsControl { get; set; } | ||
|
||
public new bool Contains(Control ctl) | ||
{ | ||
return ContainsControl; | ||
} | ||
|
||
public new ControlCollection Controls => base.Controls; | ||
} | ||
|
||
private void DisableDragDrop(Control control) | ||
{ | ||
if (control is null) | ||
return; | ||
|
||
control.AllowDrop = false; | ||
foreach (Control child in control.Controls) | ||
{ | ||
DisableDragDrop(child); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void AllowSetChildIndexOnDrop_ReturnsTrue() | ||
{ | ||
_designer.AllowSetChildIndexOnDrop.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void CanAddComponent_ReturnsTrue() | ||
{ | ||
Mock<IComponent> mockComponent = new(); | ||
_designer.CanAddComponent(mockComponent.Object).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void EnableDragRect_ReturnsTrue() | ||
{ | ||
bool enableDragRect = _designer.TestAccessor().Dynamic.EnableDragRect; | ||
enableDragRect.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void GridSize_DefaultValue_ReturnsExpected() | ||
{ | ||
Size gridSize = _designer.TestAccessor().Dynamic.GridSize; | ||
gridSize.Should().Be(new Size(8, 8)); | ||
} | ||
|
||
[Fact] | ||
public void GridSize_SetValue_UpdatesGridSize() | ||
{ | ||
Size newSize = new Size(10, 10); | ||
_designer.TestAccessor().Dynamic.GridSize = newSize; | ||
Size gridSize = _designer.TestAccessor().Dynamic.GridSize; | ||
gridSize.Should().Be(newSize); | ||
} | ||
|
||
[Fact] | ||
public void GridSize_SetValue_InvalidSize_ThrowsArgumentException() | ||
{ | ||
Action action = () => _designer.TestAccessor().Dynamic.GridSize = new Size(1, 1); | ||
action.Should().Throw<TargetInvocationException>() | ||
.WithInnerException<ArgumentException>() | ||
.WithMessage("*'GridSize'*"); | ||
|
||
action = () => _designer.TestAccessor().Dynamic.GridSize = new Size(201, 201); | ||
action.Should().Throw<TargetInvocationException>() | ||
.WithInnerException<ArgumentException>() | ||
.WithMessage("*'GridSize'*"); | ||
} | ||
|
||
[Fact] | ||
public void MouseDragTool_DefaultValue_ReturnsNull() | ||
{ | ||
ToolboxItem mouseDragTool = _designer.TestAccessor().Dynamic.MouseDragTool; | ||
mouseDragTool.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void MouseDragTool_SetValue_ReturnsExpected() | ||
{ | ||
ToolboxItem toolboxItem = new(typeof(Button)); | ||
_designer.TestAccessor().Dynamic._mouseDragTool = toolboxItem; | ||
ToolboxItem mouseDragTool = _designer.TestAccessor().Dynamic.MouseDragTool; | ||
mouseDragTool.Should().Be(toolboxItem); | ||
} | ||
|
||
[Fact] | ||
public void GetParentForComponent_ReturnsControl() | ||
{ | ||
Mock<IComponent> mockComponent = new(); | ||
Control parentControl = _designer.TestAccessor().Dynamic.GetParentForComponent(mockComponent.Object); | ||
parentControl.Should().Be(_control); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.