Skip to content

Commit

Permalink
Add AssociatedComponents test of ToolStripDropDownDesigner ToolStripD…
Browse files Browse the repository at this point in the history
…ropDownItemDesigner ToolStripMenuItemDesigner (#9575)

* Add AssociatedComponents test of ToolStripDropDownDesigner ToolStripDropDownItemDesigner ToolStripMenuItemDesigner
  • Loading branch information
Epica3055 committed Aug 3, 2023
1 parent 1d7467b commit 977ad72
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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.Tests.Mocks;
using Moq;
using Windows.Win32;

Expand Down Expand Up @@ -214,40 +214,6 @@ public void ControlDesigner_AssociatedComponents_NullSite_Test()
Assert.Empty(controlDesigner.AssociatedComponents);
}

internal static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
{
Mock<ISite> mockSite = new(MockBehavior.Strict);
mockSite
.Setup(s => s.GetService(typeof(IDesignerHost)))
.Returns(designerHost);
mockSite
.Setup(s => s.GetService(typeof(IComponentChangeService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IInheritanceService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IDictionaryService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IExtenderListService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(ITypeDescriptorFilterService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(AmbientProperties)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(Control)))
.Returns(null);
mockSite
.SetupGet(s => s.Container)
.Returns((IContainer)null);

return mockSite;
}

[WinFormsFact]
public void ControlDesigner_AssociatedComponentsTest()
{
Expand All @@ -261,7 +227,7 @@ public void ControlDesigner_AssociatedComponentsTest()
mockDesignerHost
.Setup(s => s.GetDesigner(It.IsAny<Control>()))
.Returns(() => null);
var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
control.Site = mockSite.Object;

controlDesigner.Initialize(control);
Expand Down
67 changes: 67 additions & 0 deletions src/System.Windows.Forms.Design/tests/UnitTests/Mocks/MockSite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.ComponentModel.Design;
using System.ComponentModel;
using Moq;

namespace System.Windows.Forms.Design.Tests.Mocks
{
public class MockSite
{
public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
{
Mock<ISite> mockSite = new(MockBehavior.Strict);
mockSite
.Setup(s => s.GetService(typeof(IDesignerHost)))
.Returns(designerHost);
mockSite
.Setup(s => s.GetService(typeof(IInheritanceService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IDictionaryService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IExtenderListService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(ITypeDescriptorFilterService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(AmbientProperties)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(DesignerActionService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IComponentChangeService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(ToolStripKeyboardHandlingService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(ISupportInSituService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(INestedContainer)))
.Returns(null);

Mock<ISelectionService> mockSelectionService = new(MockBehavior.Strict);

mockSite
.Setup(s => s.GetService(typeof(ISelectionService)))
.Returns(mockSelectionService.Object);
mockSite
.Setup(s => s.Container)
.Returns((IContainer)null);
mockSite
.Setup(s => s.Name)
.Returns("Site");
mockSite
.Setup(s => s.DesignMode)
.Returns(true);
mockSite
.Setup(s => s.GetService(typeof(UndoEngine)))
.Returns(null);

return mockSite;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design;
using System.ComponentModel;
using Moq;
using System.Windows.Forms.Design.Tests.Mocks;

namespace System.Windows.Forms.Design.Tests;

public class SplitContainerDesignerTests
{
private static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
{
Mock<ISite> mockSite = new(MockBehavior.Strict);
mockSite
.Setup(s => s.GetService(typeof(IDesignerHost)))
.Returns(designerHost);
mockSite
.Setup(s => s.GetService(typeof(IInheritanceService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IDictionaryService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IExtenderListService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(ITypeDescriptorFilterService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(AmbientProperties)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(DesignerActionService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(INestedContainer)))
.Returns(null);
Mock<ISelectionService> mockSelectionService = new(MockBehavior.Strict);
mockSite
.Setup(s => s.GetService(typeof(ISelectionService)))
.Returns(mockSelectionService.Object);
Mock<IComponentChangeService> mockComponentChangeService = new(MockBehavior.Strict);
mockSite
.Setup(s => s.GetService(typeof(IComponentChangeService)))
.Returns(mockComponentChangeService.Object);
mockSite
.SetupGet(s => s.Container)
.Returns((IContainer)null);

return mockSite;
}

[WinFormsFact]
public void SplitContainerDesigner_AssociatedComponentsTest()
{
Expand All @@ -69,7 +27,7 @@ public void SplitContainerDesigner_AssociatedComponentsTest()
.Setup(s => s.GetService(typeof(IComponentChangeService)))
.Returns(mockComponentChangeService.Object);

var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
splitContainer.Site = mockSite.Object;

splitContainerDesigner.Initialize(splitContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Moq;
using Moq.Protected;
using System.Windows.Forms.TestUtilities;
using System.Windows.Forms.Design.Tests.Mocks;

namespace System.ComponentModel.Design.Tests;

Expand Down Expand Up @@ -55,7 +56,7 @@ public void ComponentDesigner_GetService_ReturnsExpected()
mockDesignerHost
.Setup(h => h.RootComponent)
.Returns(component);
var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
component.Site = mockSite.Object;
designer.Initialize(component);

Expand All @@ -82,7 +83,7 @@ public void ComponentDesigner_PostFilterProperties_Success()
mockDesignerHost
.Setup(h => h.RootComponent)
.Returns(component);
var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
component.Site = mockSite.Object;
designer.Initialize(component);

Expand All @@ -91,34 +92,6 @@ public void ComponentDesigner_PostFilterProperties_Success()
Assert.NotSame(descriptor, result);
}

private static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
{
Mock<ISite> mockSite = new(MockBehavior.Strict);
mockSite
.Setup(s => s.GetService(typeof(IDesignerHost)))
.Returns(designerHost);
mockSite
.Setup(s => s.GetService(typeof(IComponentChangeService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IInheritanceService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IDictionaryService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(IExtenderListService)))
.Returns(null);
mockSite
.Setup(s => s.GetService(typeof(ITypeDescriptorFilterService)))
.Returns(null);
mockSite
.SetupGet(s => s.Container)
.Returns((IContainer)null);

return mockSite;
}

[Fact]
public void ComponentDesigner_Children_GetWithValidHostValidResult_ReturnsExpected()
{
Expand All @@ -144,7 +117,7 @@ public void ComponentDesigner_Children_GetWithValidHostValidResult_ReturnsExpect

using Component component = new()
{
Site = CreateMockSiteWithDesignerHost(mockDesignerHost.Object).Object
Site = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object).Object
};

designer.Initialize(component);
Expand Down Expand Up @@ -182,7 +155,7 @@ public void ComponentDesigner_Children_GetWithValidHostInvalidResult_ReturnsExpe

using Component component = new()
{
Site = CreateMockSiteWithDesignerHost(mockDesignerHost.Object).Object
Site = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object).Object
};

designer.Initialize(component);
Expand Down Expand Up @@ -214,7 +187,7 @@ public void ComponentDesigner_Children_GetWithInvalidDesignerHost_ReturnsEmpty(I

using Component component = new()
{
Site = CreateMockSiteWithDesignerHost(host).Object
Site = MockSite.CreateMockSiteWithDesignerHost(host).Object
};

designer.Initialize(component);
Expand Down Expand Up @@ -336,7 +309,7 @@ public void ComponentDesigner_ParentComponent_GetWithValidService_ReturnsExpecte
mockDesignerHost
.Setup(h => h.RootComponent)
.Returns(rootComponent);
var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);

using Component component = new()
{
Expand Down Expand Up @@ -366,7 +339,7 @@ public void ComponentDesigner_ParentComponent_GetWithValidServiceRootComponentEq
mockDesignerHost
.Setup(h => h.RootComponent)
.Returns(component);
var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
component.Site = mockSite.Object;
designer.Initialize(component);
mockSite.Verify(s => s.GetService(typeof(IDesignerHost)), Times.Once());
Expand All @@ -393,7 +366,7 @@ public static IEnumerable<object[]> ParentComponent_InvalidService_TestData()
public void ComponentDesigner_ParentComponent_GetWithInvalidService_ReturnsNull(object host)
{
using SubComponentDesigner designer = new();
var mockSite = CreateMockSiteWithDesignerHost(host);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(host);

using Component component = new()
{
Expand Down Expand Up @@ -425,7 +398,7 @@ public void ComponentDesigner_ITreeDesignerParent_GetWithValidService_ReturnsExp
.Setup(h => h.GetDesigner(rootComponent))
.Returns(designer)
.Verifiable();
var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);

using Component component = new()
{
Expand Down Expand Up @@ -459,7 +432,7 @@ public void ComponentDesigner_ITreeDesignerParent_GetWithValidServiceRootCompone
mockDesignerHost
.Setup(h => h.RootComponent)
.Returns(component);
var mockSite = CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(mockDesignerHost.Object);
component.Site = mockSite.Object;
designer.Initialize(component);
mockSite.Verify(s => s.GetService(typeof(IDesignerHost)), Times.Once());
Expand All @@ -481,7 +454,7 @@ public void ComponentDesigner_ITreeDesignerParent_GetWithInvalidServiceFirstCall
{
using SubComponentDesigner designer = new();
ITreeDesigner treeDesigner = designer;
var mockSite = CreateMockSiteWithDesignerHost(host);
var mockSite = MockSite.CreateMockSiteWithDesignerHost(host);

using Component component = new()
{
Expand Down
Loading

0 comments on commit 977ad72

Please sign in to comment.