Skip to content

Commit d9dc002

Browse files
Issue 10773 Adds code coverage for public members of ChangeToolStripParentVerb. (#13445)
* Adds code coverage for public members of `ChangeToolStripParentVerb`.
1 parent 4bb99e4 commit d9dc002

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-3
lines changed

src/System.Windows.Forms.Design/tests/UnitTests/Mocks/MockSite.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace System.Windows.Forms.Design.Tests.Mocks
1111
{
1212
public class MockSite
1313
{
14-
public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
14+
public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost, MockBehavior mockBehavior = MockBehavior.Strict)
1515
{
16-
Mock<ISite> mockSite = new(MockBehavior.Strict);
16+
Mock<ISite> mockSite = new(mockBehavior);
1717
mockSite
1818
.Setup(s => s.GetService(typeof(IDesignerHost)))
1919
.Returns(designerHost);
@@ -51,7 +51,7 @@ public static Mock<ISite> CreateMockSiteWithDesignerHost(object designerHost)
5151
.Setup(s => s.GetService(typeof(ToolStripMenuItem)))
5252
.Returns(null);
5353

54-
Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict);
54+
Mock<IServiceProvider> mockServiceProvider = new(mockBehavior);
5555

5656
mockSite
5757
.Setup(s => s.GetService(typeof(IServiceProvider)))
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.ComponentModel;
5+
using System.ComponentModel.Design;
6+
using System.Windows.Forms.Design.Behavior;
7+
using System.Windows.Forms.Design.Tests.Mocks;
8+
using Moq;
9+
10+
namespace System.Windows.Forms.Design.Tests;
11+
12+
public class ChangeToolStripParentVerbTests : IDisposable
13+
{
14+
private DesignerActionService? _designerActionService;
15+
private DesignerActionUIService? _designerActionUIService;
16+
private BehaviorService? _behaviorService;
17+
private readonly Mock<IDesignerHost> _designerHostMock = new();
18+
private readonly Mock<IServiceProvider> _serviceProviderMock = new();
19+
private readonly Mock<ISelectionService> _mockSelectionService = new();
20+
private Mock<ISite>? _siteMock;
21+
private readonly Mock<IComponentChangeService> _componentChangeServiceMock = new() ;
22+
private readonly Mock<DesignerTransaction> _mockTransaction = new(MockBehavior.Loose);
23+
private readonly ParentControlDesigner _parentControlDesigner = new();
24+
private readonly ToolStripDesigner _designer = new();
25+
private ToolStrip _toolStrip = new();
26+
27+
public void Dispose()
28+
{
29+
_toolStrip?.Dispose();
30+
_parentControlDesigner.Dispose();
31+
_designerActionService?.Dispose();
32+
_designerActionUIService?.Dispose();
33+
_behaviorService!.Dispose();
34+
_designer.Dispose();
35+
}
36+
37+
private ToolStrip MockMinimalControl()
38+
{
39+
_mockSelectionService.Setup(s => s.GetComponentSelected(_toolStrip)).Returns(true);
40+
_siteMock = MockSite.CreateMockSiteWithDesignerHost(_designerHostMock.Object, MockBehavior.Loose);
41+
_siteMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_mockSelectionService.Object);
42+
_siteMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object);
43+
_siteMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object);
44+
_behaviorService = new(_serviceProviderMock.Object, new DesignerFrame(_siteMock.Object));
45+
_siteMock.Setup(s => s.GetService(typeof(BehaviorService))).Returns(_behaviorService);
46+
_siteMock.Setup(s => s.GetService(typeof(ToolStripAdornerWindowService))).Returns(null!);
47+
_designerActionService = new(_siteMock.Object);
48+
_siteMock.Setup(s => s.GetService(typeof(DesignerActionService))).Returns(_designerActionService);
49+
50+
_siteMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object);
51+
_designerActionUIService = new(_siteMock.Object);
52+
_siteMock.Setup(s => s.GetService(typeof(DesignerActionUIService))).Returns(_designerActionUIService);
53+
54+
_serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object);
55+
_serviceProviderMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(new Mock<IComponentChangeService>().Object);
56+
_designerHostMock.Setup(h => h.RootComponent).Returns(_toolStrip);
57+
_designerHostMock.Setup(h => h.RootComponent).Returns(_toolStrip);
58+
_designerHostMock.Setup(h => h.CreateTransaction(It.IsAny<string>())).Returns(_mockTransaction.Object);
59+
_designerHostMock.Setup(h => h.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object);
60+
_designerHostMock.Setup(h => h.AddService(typeof(ToolStripKeyboardHandlingService), It.IsAny<object>()));
61+
_designerHostMock.Setup(h => h.AddService(typeof(ISupportInSituService), It.IsAny<object>()));
62+
_designerHostMock.Setup(h => h.AddService(typeof(DesignerActionService), It.IsAny<object>()));
63+
_designerHostMock.Setup(h => h.GetDesigner(_toolStrip)).Returns(_parentControlDesigner);
64+
_designerHostMock.Setup(h => h.AddService(typeof(DesignerActionUIService), It.IsAny<object>()));
65+
66+
_toolStrip.Site = _siteMock.Object;
67+
return _toolStrip;
68+
}
69+
70+
[Fact]
71+
public void ChangeParent_WithNullParent_ChangesParentToToolStripPanel()
72+
{
73+
_toolStrip = MockMinimalControl();
74+
Control? oldParent = _toolStrip.Parent;
75+
_parentControlDesigner.Initialize(_toolStrip);
76+
_designer.Initialize(_toolStrip);
77+
ChangeToolStripParentVerb changeToolStripParentVerb = new(_designer);
78+
79+
changeToolStripParentVerb.ChangeParent();
80+
81+
_toolStrip.Parent.Should().NotBe(oldParent);
82+
_toolStrip.Parent.Should().BeOfType<ToolStripPanel>();
83+
}
84+
85+
[Fact]
86+
public void ChangeParent_WithParent_ChangesParentToToolStripContentPanel()
87+
{
88+
_toolStrip = MockMinimalControl();
89+
_toolStrip.Parent = new ToolStripPanel();
90+
Control oldParent = _toolStrip.Parent;
91+
_parentControlDesigner.Initialize(_toolStrip);
92+
_designer.Initialize(_toolStrip);
93+
ChangeToolStripParentVerb changeToolStripParentVerb = new(_designer);
94+
95+
changeToolStripParentVerb.ChangeParent();
96+
_toolStrip.Parent.Should().NotBe(oldParent);
97+
_toolStrip.Parent.Should().BeOfType<ToolStripContentPanel>();
98+
}
99+
100+
[Fact]
101+
public void ChangeParent_WhenDesignerActionUIServiceIsNull_DoesNotChangeParent()
102+
{
103+
_toolStrip = MockMinimalControl();
104+
_toolStrip.Parent = new ToolStripPanel();
105+
_parentControlDesigner.Initialize(_toolStrip);
106+
_designer.Initialize(_toolStrip);
107+
108+
_siteMock!.Setup(s => s.GetService(typeof(DesignerActionUIService))).Returns(null!);
109+
110+
Control oldParent = _toolStrip.Parent;
111+
var changeToolStripParentVerb = new ChangeToolStripParentVerb(_designer);
112+
113+
changeToolStripParentVerb.ChangeParent();
114+
115+
_toolStrip.Parent.Should().Be(oldParent);
116+
}
117+
}

0 commit comments

Comments
 (0)