Skip to content

Issue 10773 Adds code coverage for public members of ChangeToolStripParentVerb. #13445

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable
Expand All @@ -11,9 +11,9 @@ namespace System.Windows.Forms.Design.Tests.Mocks
{
public class MockSite
{
public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost, MockBehavior mockBehavior = MockBehavior.Strict)
{
Mock<ISite> mockSite = new(MockBehavior.Strict);
Mock<ISite> mockSite = new(mockBehavior);
mockSite
.Setup(s => s.GetService(typeof(IDesignerHost)))
.Returns(designerHost);
Expand Down Expand Up @@ -51,7 +51,7 @@ public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
.Setup(s => s.GetService(typeof(ToolStripMenuItem)))
.Returns(null);

Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict);
Mock<IServiceProvider> mockServiceProvider = new(mockBehavior);

mockSite
.Setup(s => s.GetService(typeof(IServiceProvider)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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.Windows.Forms.Design.Behavior;
using System.Windows.Forms.Design.Tests.Mocks;
using Moq;

namespace System.Windows.Forms.Design.Tests;

public class ChangeToolStripParentVerbTests : IDisposable
{
private DesignerActionService? _designerActionService;
private DesignerActionUIService? _designerActionUIService;
private BehaviorService? _behaviorService;
private readonly Mock<IDesignerHost> _designerHostMock = new();
private readonly Mock<IServiceProvider> _serviceProviderMock = new();
private readonly Mock<ISelectionService> _mockSelectionService = new();
private Mock<ISite>? _siteMock;
private readonly Mock<IComponentChangeService> _componentChangeServiceMock = new() ;
private readonly Mock<DesignerTransaction> _mockTransaction = new(MockBehavior.Loose);
private readonly ParentControlDesigner _parentControlDesigner = new();
private readonly ToolStripDesigner _designer = new();
private ToolStrip _toolStrip = new();

public void Dispose()
{
_toolStrip?.Dispose();
_parentControlDesigner.Dispose();
_designerActionService?.Dispose();
_designerActionUIService?.Dispose();
_behaviorService.Dispose();
_designer.Dispose();
}

private ToolStrip MockMinimalControl()
{
_mockSelectionService.Setup(s => s.GetComponentSelected(_toolStrip)).Returns(true);
_siteMock = MockSite.CreateMockSiteWithDesignerHost(_designerHostMock.Object, MockBehavior.Loose);
_siteMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_mockSelectionService.Object);
_siteMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object);
_siteMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object);
_behaviorService = new (_serviceProviderMock.Object, new DesignerFrame(_siteMock.Object));
_siteMock.Setup(s => s.GetService(typeof(BehaviorService))).Returns(_behaviorService);
_siteMock.Setup(s => s.GetService(typeof(ToolStripAdornerWindowService))).Returns(null!);
_designerActionService = new (_siteMock.Object);
_siteMock.Setup(s => s.GetService(typeof(DesignerActionService))).Returns(_designerActionService);

_siteMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object);
_designerActionUIService = new(_siteMock.Object);
_siteMock.Setup(s => s.GetService(typeof(DesignerActionUIService))).Returns(_designerActionUIService);

_serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object);
_serviceProviderMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(new Mock<IComponentChangeService>().Object);
_designerHostMock.Setup(h => h.RootComponent).Returns(_toolStrip);
_designerHostMock.Setup(h => h.RootComponent).Returns(_toolStrip);
_designerHostMock.Setup(h => h.CreateTransaction(It.IsAny<string>())).Returns(_mockTransaction.Object);
_designerHostMock.Setup(h => h.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object);
_designerHostMock.Setup(h => h.AddService(typeof(ToolStripKeyboardHandlingService), It.IsAny<object>()));
_designerHostMock.Setup(h => h.AddService(typeof(ISupportInSituService), It.IsAny<object>()));
_designerHostMock.Setup(h => h.AddService(typeof(DesignerActionService), It.IsAny<object>()));
_designerHostMock.Setup(h => h.GetDesigner(_toolStrip)).Returns(_parentControlDesigner);
_designerHostMock.Setup(h => h.AddService(typeof(DesignerActionUIService), It.IsAny<object>()));

_toolStrip.Site = _siteMock.Object;
return _toolStrip;
}

[Fact]
public void ChangeParent_WithNullParent_ChangesParentToToolStripPanel()
{
_toolStrip = MockMinimalControl();
Control? oldParent = _toolStrip.Parent;
_parentControlDesigner.Initialize(_toolStrip);
_designer.Initialize(_toolStrip);
ChangeToolStripParentVerb changeToolStripParentVerb = new(_designer);

changeToolStripParentVerb.ChangeParent();

_toolStrip.Parent.Should().NotBe(oldParent);
_toolStrip.Parent.Should().BeOfType<ToolStripPanel>();
}

[Fact]
public void ChangeParent_WithParent_ChangesParentToToolStripContentPanel()
{
_toolStrip = MockMinimalControl();
_toolStrip.Parent = new ToolStripPanel();
Control oldParent = _toolStrip.Parent;
_parentControlDesigner.Initialize(_toolStrip);
_designer.Initialize(_toolStrip);
ChangeToolStripParentVerb changeToolStripParentVerb = new(_designer);

changeToolStripParentVerb.ChangeParent();
_toolStrip.Parent.Should().NotBe(oldParent);
_toolStrip.Parent.Should().BeOfType<ToolStripContentPanel>();
}

[Fact]
public void ChangeParent_WhenDesignerActionUIServiceIsNull_DoesNotChangeParent()
{
_toolStrip = MockMinimalControl();
_toolStrip.Parent = new ToolStripPanel();
_parentControlDesigner.Initialize(_toolStrip);
_designer.Initialize(_toolStrip);

_siteMock.Setup(s => s.GetService(typeof(DesignerActionUIService))).Returns(null);

Control oldParent = _toolStrip.Parent;
var changeToolStripParentVerb = new ChangeToolStripParentVerb(_designer);

changeToolStripParentVerb.ChangeParent();

_toolStrip.Parent.Should().Be(oldParent);
}
}
Loading