From e6f9a59a8da116f47774964efd611b854b065cb9 Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Mon, 7 Apr 2025 08:49:22 +0000 Subject: [PATCH 1/4] Add code coverage for FlowLayoutPanelDesigner.cs file --- .../Design/FlowLayoutPanelDesignerTests.cs | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs new file mode 100644 index 00000000000..6c2e24c1231 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs @@ -0,0 +1,154 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections; +using System.ComponentModel; +using System.ComponentModel.Design; +using System.Drawing; +using Moq; + +namespace System.Windows.Forms.Design.Tests; + +public class FlowLayoutPanelDesignerTests : IDisposable +{ + private readonly Mock _serviceProviderMock; + private readonly Mock _designerHostMock; + private readonly Mock _selectionServiceMock; + private readonly FlowLayoutPanelDesigner _designer; + private readonly FlowLayoutPanel _flowLayoutPanel; + + public FlowLayoutPanelDesignerTests() + { + _serviceProviderMock = new Mock(); + _designerHostMock = new Mock(); + _selectionServiceMock = new Mock(); + + _serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); + _serviceProviderMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_selectionServiceMock.Object); + + _flowLayoutPanel = new() + { + Site = new Mock().Object + }; + + _designer = new FlowLayoutPanelDesigner(); + _designer.Initialize(_flowLayoutPanel); + } + + public void Dispose() + { + _flowLayoutPanel.Dispose(); + _designer.Dispose(); + } + + [Fact] + public void Initialize_ShouldSetInheritedReadOnlyAttribute_WhenFlowLayoutPanelIsInheritedReadOnly() + { + using Control childControl = new() { Site = new Mock().Object }; + _flowLayoutPanel.Controls.Add(childControl); + + TypeDescriptor.AddAttributes(childControl, InheritanceAttribute.InheritedReadOnly); + + _designer.Initialize(_flowLayoutPanel); + + TypeDescriptor.GetAttributes(childControl)[typeof(InheritanceAttribute)] + .Should().BeEquivalentTo(InheritanceAttribute.InheritedReadOnly); + } + + [Fact] + public void Initialize_ShouldBeNotInheritedAttribute_WhenFlowLayoutPanelIsNotInheritedReadOnly() + { + using Control childControl = new() { Site = new Mock().Object }; + _flowLayoutPanel.Controls.Add(childControl); + + _designer.Initialize(_flowLayoutPanel); + + TypeDescriptor.GetAttributes(childControl)[typeof(InheritanceAttribute)] + .Should().NotBeEquivalentTo(InheritanceAttribute.InheritedReadOnly); + } + + [Fact] + public void PreFilterProperties_ShouldModifyFlowDirectionProperty() + { + Hashtable properties = new() + { + { "FlowDirection", TypeDescriptor.CreateProperty(typeof(FlowLayoutPanel), "FlowDirection", typeof(FlowDirection)) } + }; + + _designer.TestAccessor().Dynamic.PreFilterProperties(properties); + + properties["FlowDirection"].Should().NotBeNull(); + properties["FlowDirection"].Should().BeAssignableTo(); + } + + [Fact] + public void AllowSetChildIndexOnDrop_ShouldReturnFalse() + { + _designer.AllowSetChildIndexOnDrop.Should().BeFalse(); + } + + [Fact] + public void AllowGenericDragBox_ShouldReturnFalse() + { + bool result = _designer.TestAccessor().Dynamic.AllowGenericDragBox; + + result.Should().BeFalse(); + } + + [Theory] + [InlineData(FlowDirection.LeftToRight, true)] + [InlineData(FlowDirection.RightToLeft, true)] + [InlineData(FlowDirection.TopDown, false)] + [InlineData(FlowDirection.BottomUp, false)] + public void HorizontalFlow_ShouldReturnCorrectValueBasedOnFlowDirection(FlowDirection flowDirection, bool expected) + { + _flowLayoutPanel.FlowDirection = flowDirection; + _designer.Initialize(_flowLayoutPanel); + + bool result = _designer.TestAccessor().Dynamic.HorizontalFlow; + + result.Should().Be(expected); + } + + [Theory] + [InlineData(FlowDirection.LeftToRight, FlowDirection.RightToLeft)] + [InlineData(FlowDirection.RightToLeft, FlowDirection.LeftToRight)] + [InlineData(FlowDirection.TopDown, FlowDirection.TopDown)] + [InlineData(FlowDirection.BottomUp, FlowDirection.BottomUp)] + public void RTLTranslateFlowDirection_ShouldTranslateCorrectly(FlowDirection input, FlowDirection expected) + { + _flowLayoutPanel.RightToLeft = RightToLeft.Yes; + _designer.Initialize(_flowLayoutPanel); + + FlowDirection result = _designer.TestAccessor().Dynamic.RTLTranslateFlowDirection(input); + + result.Should().Be(expected); + } + + [Theory] + [InlineData(RightToLeft.Yes, true)] + [InlineData(RightToLeft.No, false)] + public void IsRtl_ShouldReturnCorrectValueBasedOnRightToLeft(RightToLeft rightToLeft, bool expected) + { + _flowLayoutPanel.RightToLeft = rightToLeft; + _designer.Initialize(_flowLayoutPanel); + + bool result = _designer.TestAccessor().Dynamic.IsRtl; + + result.Should().Be(expected); + } + + [Fact] + public void GetMarginBounds_ShouldReturnCorrectRectangle() + { + using Control control = new() + { + Bounds = new Rectangle(10, 20, 30, 40), + Margin = new Padding(5, 6, 7, 8) + }; + + Rectangle result = _designer.TestAccessor().Dynamic.GetMarginBounds(control); + + result.Should().Be(new Rectangle(5, 14, 42, 54)); + } +} From 5fe78dad219cec519eea3e2d7c2399c4e7fcb7ee Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Mon, 7 Apr 2025 08:59:14 +0000 Subject: [PATCH 2/4] Update --- .../Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs index 6c2e24c1231..cd7dc0ddcec 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs @@ -19,9 +19,9 @@ public class FlowLayoutPanelDesignerTests : IDisposable public FlowLayoutPanelDesignerTests() { - _serviceProviderMock = new Mock(); - _designerHostMock = new Mock(); - _selectionServiceMock = new Mock(); + _serviceProviderMock = new(); + _designerHostMock = new(); + _selectionServiceMock = new(); _serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); _serviceProviderMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_selectionServiceMock.Object); @@ -31,7 +31,7 @@ public FlowLayoutPanelDesignerTests() Site = new Mock().Object }; - _designer = new FlowLayoutPanelDesigner(); + _designer = new(); _designer.Initialize(_flowLayoutPanel); } From 0e9772371f5dccf5a8b513decfb1cac7a8ba153e Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Wed, 9 Apr 2025 01:54:12 +0000 Subject: [PATCH 3/4] Add several protected override methods --- .../Design/FlowLayoutPanelDesignerTests.cs | 82 +++++++++++++++++-- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs index cd7dc0ddcec..3d054f4870f 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs @@ -26,11 +26,7 @@ public FlowLayoutPanelDesignerTests() _serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); _serviceProviderMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_selectionServiceMock.Object); - _flowLayoutPanel = new() - { - Site = new Mock().Object - }; - + _flowLayoutPanel = new() { Site = new Mock().Object }; _designer = new(); _designer.Initialize(_flowLayoutPanel); } @@ -82,10 +78,8 @@ public void PreFilterProperties_ShouldModifyFlowDirectionProperty() } [Fact] - public void AllowSetChildIndexOnDrop_ShouldReturnFalse() - { + public void AllowSetChildIndexOnDrop_ShouldReturnFalse() => _designer.AllowSetChildIndexOnDrop.Should().BeFalse(); - } [Fact] public void AllowGenericDragBox_ShouldReturnFalse() @@ -151,4 +145,76 @@ public void GetMarginBounds_ShouldReturnCorrectRectangle() result.Should().Be(new Rectangle(5, 14, 42, 54)); } + + [Fact] + public void OnDragEnter_ShouldInitializeDragState() + { + _designer.Initialize(_flowLayoutPanel); + DragEventArgs dragEventArgs = new( + new DataObject(), + 0, + 0, + 0, + DragDropEffects.Copy, + DragDropEffects.Copy); + + _designer.TestAccessor().Dynamic.OnDragEnter(dragEventArgs); + + int insertionIndex = _designer.TestAccessor().Dynamic._insertionIndex; + insertionIndex.Should().Be(-1); + + Point lastMouseLocation = _designer.TestAccessor().Dynamic._lastMouseLocation; + lastMouseLocation.Should().Be(Point.Empty); + } + + [Fact] + public void OnDragLeave_ShouldClearDragState() + { + _designer.Initialize(_flowLayoutPanel); + + _designer.TestAccessor().Dynamic.OnDragLeave(EventArgs.Empty); + + int insertionIndex = _designer.TestAccessor().Dynamic._insertionIndex; + insertionIndex.Should().Be(-1); + } + + [Fact] + public void OnDragOver_ShouldUpdateInsertionIndex() + { + _designer.Initialize(_flowLayoutPanel); + DragEventArgs dragEventArgs = new( + new DataObject(), + 0, + 10, + 10, + DragDropEffects.Copy, + DragDropEffects.Copy); + + _designer.TestAccessor().Dynamic.OnDragOver(dragEventArgs); + + int insertionIndex = _designer.TestAccessor().Dynamic._insertionIndex; + insertionIndex.Should().Be(-1); + } + + [Fact] + public void OnDragDrop_ShouldReorderControls() + { + using Control control1 = new(); + using Control control2 = new(); + _flowLayoutPanel.Controls.Add(control1); + _flowLayoutPanel.Controls.Add(control2); + _designer.Initialize(_flowLayoutPanel); + DragEventArgs dragEventArgs = new( + new DataObject(), + 0, + 0, + 0, + DragDropEffects.Move, + DragDropEffects.Move); + + _designer.TestAccessor().Dynamic.OnDragDrop(dragEventArgs); + + _flowLayoutPanel.Controls[0].Should().Be(control1); + _flowLayoutPanel.Controls[1].Should().Be(control2); + } } From b5485f8fe691971b6941540fd2dd2eb05f99485e Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Mon, 14 Apr 2025 03:06:31 +0000 Subject: [PATCH 4/4] update --- .../Design/FlowLayoutPanelDesignerTests.cs | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs index 3d054f4870f..e7c83d96d2a 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FlowLayoutPanelDesignerTests.cs @@ -33,8 +33,8 @@ public FlowLayoutPanelDesignerTests() public void Dispose() { - _flowLayoutPanel.Dispose(); _designer.Dispose(); + _flowLayoutPanel.Dispose(); } [Fact] @@ -45,10 +45,8 @@ public void Initialize_ShouldSetInheritedReadOnlyAttribute_WhenFlowLayoutPanelIs TypeDescriptor.AddAttributes(childControl, InheritanceAttribute.InheritedReadOnly); - _designer.Initialize(_flowLayoutPanel); - TypeDescriptor.GetAttributes(childControl)[typeof(InheritanceAttribute)] - .Should().BeEquivalentTo(InheritanceAttribute.InheritedReadOnly); + .Should().BeEquivalentTo(InheritanceAttribute.InheritedReadOnly); } [Fact] @@ -57,10 +55,8 @@ public void Initialize_ShouldBeNotInheritedAttribute_WhenFlowLayoutPanelIsNotInh using Control childControl = new() { Site = new Mock().Object }; _flowLayoutPanel.Controls.Add(childControl); - _designer.Initialize(_flowLayoutPanel); - TypeDescriptor.GetAttributes(childControl)[typeof(InheritanceAttribute)] - .Should().NotBeEquivalentTo(InheritanceAttribute.InheritedReadOnly); + .Should().NotBeEquivalentTo(InheritanceAttribute.InheritedReadOnly); } [Fact] @@ -68,13 +64,13 @@ public void PreFilterProperties_ShouldModifyFlowDirectionProperty() { Hashtable properties = new() { - { "FlowDirection", TypeDescriptor.CreateProperty(typeof(FlowLayoutPanel), "FlowDirection", typeof(FlowDirection)) } + { nameof(FlowLayoutPanel.FlowDirection), TypeDescriptor.CreateProperty(typeof(FlowLayoutPanel), nameof(FlowLayoutPanel.FlowDirection), typeof(FlowDirection)) } }; _designer.TestAccessor().Dynamic.PreFilterProperties(properties); - properties["FlowDirection"].Should().NotBeNull(); - properties["FlowDirection"].Should().BeAssignableTo(); + properties[nameof(FlowLayoutPanel.FlowDirection)].Should().NotBeNull(); + properties[nameof(FlowLayoutPanel.FlowDirection)].Should().BeAssignableTo(); } [Fact] @@ -97,7 +93,6 @@ public void AllowGenericDragBox_ShouldReturnFalse() public void HorizontalFlow_ShouldReturnCorrectValueBasedOnFlowDirection(FlowDirection flowDirection, bool expected) { _flowLayoutPanel.FlowDirection = flowDirection; - _designer.Initialize(_flowLayoutPanel); bool result = _designer.TestAccessor().Dynamic.HorizontalFlow; @@ -112,7 +107,6 @@ public void HorizontalFlow_ShouldReturnCorrectValueBasedOnFlowDirection(FlowDire public void RTLTranslateFlowDirection_ShouldTranslateCorrectly(FlowDirection input, FlowDirection expected) { _flowLayoutPanel.RightToLeft = RightToLeft.Yes; - _designer.Initialize(_flowLayoutPanel); FlowDirection result = _designer.TestAccessor().Dynamic.RTLTranslateFlowDirection(input); @@ -125,7 +119,6 @@ public void RTLTranslateFlowDirection_ShouldTranslateCorrectly(FlowDirection inp public void IsRtl_ShouldReturnCorrectValueBasedOnRightToLeft(RightToLeft rightToLeft, bool expected) { _flowLayoutPanel.RightToLeft = rightToLeft; - _designer.Initialize(_flowLayoutPanel); bool result = _designer.TestAccessor().Dynamic.IsRtl; @@ -149,7 +142,6 @@ public void GetMarginBounds_ShouldReturnCorrectRectangle() [Fact] public void OnDragEnter_ShouldInitializeDragState() { - _designer.Initialize(_flowLayoutPanel); DragEventArgs dragEventArgs = new( new DataObject(), 0, @@ -170,8 +162,6 @@ public void OnDragEnter_ShouldInitializeDragState() [Fact] public void OnDragLeave_ShouldClearDragState() { - _designer.Initialize(_flowLayoutPanel); - _designer.TestAccessor().Dynamic.OnDragLeave(EventArgs.Empty); int insertionIndex = _designer.TestAccessor().Dynamic._insertionIndex; @@ -181,7 +171,6 @@ public void OnDragLeave_ShouldClearDragState() [Fact] public void OnDragOver_ShouldUpdateInsertionIndex() { - _designer.Initialize(_flowLayoutPanel); DragEventArgs dragEventArgs = new( new DataObject(), 0, @@ -203,7 +192,6 @@ public void OnDragDrop_ShouldReorderControls() using Control control2 = new(); _flowLayoutPanel.Controls.Add(control1); _flowLayoutPanel.Controls.Add(control2); - _designer.Initialize(_flowLayoutPanel); DragEventArgs dragEventArgs = new( new DataObject(), 0,