-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makes mock initialization reusable by extracting it to TestControlDes…
…igner.Mocks
- Loading branch information
Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)
committed
Nov 13, 2024
1 parent
7c30dc9
commit 8808365
Showing
3 changed files
with
140 additions
and
121 deletions.
There are no files selected for viewing
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
116 changes: 116 additions & 0 deletions
116
src/System.Windows.Forms.Design/tests/UnitTests/TestControlDesigner.Mocks.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,116 @@ | ||
// 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 System.ComponentModel; | ||
using System.ComponentModel.Design; | ||
using Moq; | ||
|
||
namespace System.Windows.Forms.Design.Tests; | ||
|
||
internal partial class TestControlDesigner | ||
{ | ||
internal readonly Control _control = new(); | ||
internal readonly Mock<IDesignerHost> _mockDesignerHost = new(); | ||
internal readonly Mock<ISite> _mockSite = new(); | ||
|
||
public TestControlDesigner(bool isInitialized = true) | ||
{ | ||
_mockDesignerHost | ||
.Setup(h => h.RootComponent) | ||
.Returns(_control); | ||
_mockDesignerHost | ||
.Setup(s => s.GetDesigner(It.IsAny<Control>())) | ||
.Returns(this); | ||
Mock<IComponentChangeService> mockComponentChangeService = new(); | ||
_mockDesignerHost | ||
.Setup(s => s.GetService(typeof(IComponentChangeService))) | ||
.Returns(mockComponentChangeService.Object); | ||
_mockSite = CreateMockSiteWithDesignerHost(_mockDesignerHost.Object); | ||
_control.Site = _mockSite.Object; | ||
|
||
if (isInitialized) | ||
{ | ||
Initialize(_control); | ||
} | ||
} | ||
|
||
public new void Dispose() | ||
{ | ||
_control.Dispose(); | ||
} | ||
|
||
public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost) | ||
{ | ||
Mock<ISite> mockSite = new(); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(IDesignerHost))) | ||
.Returns(designerHost); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(IInheritanceService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(IDictionaryService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(IExtenderListService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(ITypeDescriptorFilterService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(AmbientProperties))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(DesignerActionService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(IComponentChangeService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(ToolStripKeyboardHandlingService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(ISupportInSituService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(INestedContainer))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(ToolStripMenuItem))) | ||
.Returns((object?)null); | ||
|
||
Mock<IServiceProvider> mockServiceProvider = new(); | ||
|
||
mockSite | ||
.Setup(s => s.GetService(typeof(IServiceProvider))) | ||
.Returns(mockServiceProvider.Object); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(ToolStripAdornerWindowService))) | ||
.Returns((object?)null); | ||
mockSite | ||
.Setup(s => s.GetService(typeof(DesignerOptionService))) | ||
.Returns(mockServiceProvider.Object); | ||
|
||
Mock<ISelectionService> mockSelectionService = new(); | ||
|
||
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((object?)null); | ||
|
||
return mockSite; | ||
} | ||
} |
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