diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/Mocks/MockSite.cs b/src/System.Windows.Forms.Design/tests/UnitTests/Mocks/MockSite.cs index 5c852b3e1e5..cb6e723bcb5 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/Mocks/MockSite.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/Mocks/MockSite.cs @@ -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 @@ -11,9 +11,9 @@ namespace System.Windows.Forms.Design.Tests.Mocks { public class MockSite { - public static Mock CreateMockSiteWithDesignerHost(object designerHost) + public static Mock CreateMockSiteWithDesignerHost(object designerHost, MockBehavior mockBehavior = MockBehavior.Strict) { - Mock mockSite = new(MockBehavior.Strict); + Mock mockSite = new(mockBehavior); mockSite .Setup(s => s.GetService(typeof(IDesignerHost))) .Returns(designerHost); @@ -51,7 +51,7 @@ public static Mock CreateMockSiteWithDesignerHost(object designerHost) .Setup(s => s.GetService(typeof(ToolStripMenuItem))) .Returns(null); - Mock mockServiceProvider = new(MockBehavior.Strict); + Mock mockServiceProvider = new(mockBehavior); mockSite .Setup(s => s.GetService(typeof(IServiceProvider))) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ChangeToolStripParentVerbTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ChangeToolStripParentVerbTests.cs new file mode 100644 index 00000000000..6714318701b --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ChangeToolStripParentVerbTests.cs @@ -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 _designerHostMock = new(); + private readonly Mock _serviceProviderMock = new(); + private readonly Mock _mockSelectionService = new(); + private Mock? _siteMock; + private readonly Mock _componentChangeServiceMock = new() ; + private readonly Mock _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().Object); + _designerHostMock.Setup(h => h.RootComponent).Returns(_toolStrip); + _designerHostMock.Setup(h => h.RootComponent).Returns(_toolStrip); + _designerHostMock.Setup(h => h.CreateTransaction(It.IsAny())).Returns(_mockTransaction.Object); + _designerHostMock.Setup(h => h.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object); + _designerHostMock.Setup(h => h.AddService(typeof(ToolStripKeyboardHandlingService), It.IsAny())); + _designerHostMock.Setup(h => h.AddService(typeof(ISupportInSituService), It.IsAny())); + _designerHostMock.Setup(h => h.AddService(typeof(DesignerActionService), It.IsAny())); + _designerHostMock.Setup(h => h.GetDesigner(_toolStrip)).Returns(_parentControlDesigner); + _designerHostMock.Setup(h => h.AddService(typeof(DesignerActionUIService), It.IsAny())); + + _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(); + } + + [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(); + } + + [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); + } +}