From 341cc253a46e707101c91a5bb4242453434e6a9e Mon Sep 17 00:00:00 2001 From: v-melonwang Date: Thu, 21 Nov 2024 09:12:47 +0000 Subject: [PATCH 1/2] Add unit tests for ToolStripActionList --- .../Forms/Design/ToolStripActionListTests.cs | 177 ++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs new file mode 100644 index 00000000000..4b4a636f0c5 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs @@ -0,0 +1,177 @@ +// 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 Moq; +using System.ComponentModel.Design; +using System.ComponentModel; + +namespace System.Windows.Forms.Design.Tests; + +public sealed class ToolStripActionListTests : IDisposable +{ + private readonly Mock _mockDesignerHost; + private readonly Mock _mockSelectionService; + private readonly Mock _mockComponentChangeService; + private readonly ToolStrip _toolStrip; + private readonly ToolStripDesigner _designer; + private readonly ToolStripActionList _actionList; + + public ToolStripActionListTests() + { + _mockDesignerHost = new(); + _mockSelectionService = new(); + _mockComponentChangeService = new(); + _toolStrip = new(); + InitializeMocks(); + + _designer = new(); + _designer.Initialize(_toolStrip); + _actionList = new(_designer); + + void InitializeMocks() + { + Mock mockSite = new(); + mockSite.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_mockDesignerHost.Object); + mockSite.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_mockSelectionService.Object); + mockSite.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(_mockComponentChangeService.Object); + + _toolStrip.Site = mockSite.Object; + + _mockDesignerHost.Setup(d => d.GetService(typeof(IComponentChangeService))).Returns(_mockComponentChangeService.Object); + } + } + + public void Dispose() + { + _toolStrip.Dispose(); + _designer.Dispose(); + } + + [Fact] + public void Constructor_InitializesFields() + { + _actionList.Should().NotBeNull(); + _actionList.Should().BeOfType(); + _actionList.Component.Should().Be(_toolStrip); + } + + [Theory] + [BoolData] + public void AutoShow_GetSet_ReturnsExpected(bool value) + { + _actionList.AutoShow.Should().BeFalse(); + + _actionList.AutoShow = value; + _actionList.AutoShow.Should().Be(value); + } + + [Theory] + [EnumData] + public void Dock_GetSet_ReturnsExpected(DockStyle dockStyle) + { + _actionList.Dock.Should().Be(DockStyle.Top); + + _actionList.Dock = dockStyle; + _actionList.Dock.Should().Be(dockStyle); + } + + [Theory] + [EnumData] + public void RenderMode_GetSet_ReturnsExpected(ToolStripRenderMode renderMode) + { + if (renderMode == ToolStripRenderMode.Custom) + { + _actionList.Invoking(a => a.RenderMode = renderMode) + .Should().Throw(); + } + else + { + _actionList.RenderMode = renderMode; + _actionList.RenderMode.Should().Be(renderMode); + } + } + + [Theory] + [EnumData] + public void GripStyle_GetSet_ReturnsExpected(ToolStripGripStyle gripStyle) + { + _actionList.GripStyle.Should().Be(ToolStripGripStyle.Visible); + + _actionList.GripStyle = gripStyle; + _actionList.GripStyle.Should().Be(gripStyle); + } + + [Fact] + public void GetSortedActionItems_ShouldIncludeExpectedItems_WhenConditionsAreMet() + { + var items = _actionList.GetSortedActionItems(); + var itemList = items.Cast().ToList(); + + itemList.Should().ContainSingle(i => i is DesignerActionMethodItem && ((DesignerActionMethodItem)i).MemberName == "InvokeEmbedVerb"); + itemList.Should().ContainSingle(i => i is DesignerActionMethodItem && ((DesignerActionMethodItem)i).MemberName == "InvokeInsertStandardItemsVerb"); + itemList.Should().ContainSingle(i => i is DesignerActionPropertyItem && ((DesignerActionPropertyItem)i).MemberName == "RenderMode"); + itemList.Should().ContainSingle(i => i is DesignerActionPropertyItem && ((DesignerActionPropertyItem)i).MemberName == "Dock"); + itemList.Should().ContainSingle(i => i is DesignerActionPropertyItem && ((DesignerActionPropertyItem)i).MemberName == "GripStyle"); + } + + [Fact] + public void GetSortedActionItems_ShouldNotIncludeEmbedVerb_WhenIsReadOnly() + { + TypeDescriptor.AddAttributes(_toolStrip, new InheritanceAttribute(InheritanceLevel.InheritedReadOnly)); + TypeDescriptor.Refresh(_toolStrip); + + var items = _actionList.GetSortedActionItems(); + var itemList = items.Cast().ToList(); + + itemList.Should().NotContain(i => + i is DesignerActionMethodItem && + ((DesignerActionMethodItem)i).MemberName == "InvokeEmbedVerb"); + } + + [Fact] + public void GetSortedActionItems_ShouldIncludeRenderModeAndInsertStandardItemsVerb_WhenCanAddItems() + { + var items = _actionList.GetSortedActionItems(); + var itemList = items.Cast().ToList(); + + itemList.Should().ContainSingle(i => + i is DesignerActionMethodItem && + ((DesignerActionMethodItem)i).MemberName == "InvokeInsertStandardItemsVerb"); + itemList.Should().ContainSingle(i => + i is DesignerActionPropertyItem && + ((DesignerActionPropertyItem)i).MemberName == "RenderMode"); + } + + [Fact] + public void GetSortedActionItems_ShouldIncludeDock_WhenParentIsNotToolStripPanel() + { + _toolStrip.Parent = new Form(); + + var items = _actionList.GetSortedActionItems(); + var itemList = items.Cast().ToList(); + + itemList.Should().ContainSingle(i => + i is DesignerActionPropertyItem && + ((DesignerActionPropertyItem)i).MemberName == "Dock"); + } + + [Fact] + public void GetSortedActionItems_ShouldNotIncludeGripStyle_WhenToolStripIsStatusStrip() + { + var statusStrip = new StatusStrip + { + Site = _toolStrip.Site + }; + _designer.Initialize(statusStrip); + _actionList.TestAccessor().Dynamic._toolStrip = statusStrip; + + var items = _actionList.GetSortedActionItems(); + var itemList = items.Cast().ToList(); + + itemList.Should().NotContain(i => + i is DesignerActionPropertyItem && + ((DesignerActionPropertyItem)i).MemberName == "GripStyle"); + } +} From ed7b62e575f1c570449c58c1156f03bc7a81ae92 Mon Sep 17 00:00:00 2001 From: v-melonwang Date: Fri, 22 Nov 2024 07:51:42 +0000 Subject: [PATCH 2/2] declare variables with specific types --- .../Forms/Design/ToolStripActionListTests.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs index 4b4a636f0c5..fe51fb607e5 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripActionListTests.cs @@ -106,8 +106,8 @@ public void GripStyle_GetSet_ReturnsExpected(ToolStripGripStyle gripStyle) [Fact] public void GetSortedActionItems_ShouldIncludeExpectedItems_WhenConditionsAreMet() { - var items = _actionList.GetSortedActionItems(); - var itemList = items.Cast().ToList(); + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); + List itemList = items.Cast().ToList(); itemList.Should().ContainSingle(i => i is DesignerActionMethodItem && ((DesignerActionMethodItem)i).MemberName == "InvokeEmbedVerb"); itemList.Should().ContainSingle(i => i is DesignerActionMethodItem && ((DesignerActionMethodItem)i).MemberName == "InvokeInsertStandardItemsVerb"); @@ -122,8 +122,8 @@ public void GetSortedActionItems_ShouldNotIncludeEmbedVerb_WhenIsReadOnly() TypeDescriptor.AddAttributes(_toolStrip, new InheritanceAttribute(InheritanceLevel.InheritedReadOnly)); TypeDescriptor.Refresh(_toolStrip); - var items = _actionList.GetSortedActionItems(); - var itemList = items.Cast().ToList(); + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); + List itemList = items.Cast().ToList(); itemList.Should().NotContain(i => i is DesignerActionMethodItem && @@ -133,8 +133,8 @@ i is DesignerActionMethodItem && [Fact] public void GetSortedActionItems_ShouldIncludeRenderModeAndInsertStandardItemsVerb_WhenCanAddItems() { - var items = _actionList.GetSortedActionItems(); - var itemList = items.Cast().ToList(); + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); + List itemList = items.Cast().ToList(); itemList.Should().ContainSingle(i => i is DesignerActionMethodItem && @@ -149,8 +149,8 @@ public void GetSortedActionItems_ShouldIncludeDock_WhenParentIsNotToolStripPanel { _toolStrip.Parent = new Form(); - var items = _actionList.GetSortedActionItems(); - var itemList = items.Cast().ToList(); + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); + List itemList = items.Cast().ToList(); itemList.Should().ContainSingle(i => i is DesignerActionPropertyItem && @@ -167,8 +167,8 @@ public void GetSortedActionItems_ShouldNotIncludeGripStyle_WhenToolStripIsStatus _designer.Initialize(statusStrip); _actionList.TestAccessor().Dynamic._toolStrip = statusStrip; - var items = _actionList.GetSortedActionItems(); - var itemList = items.Cast().ToList(); + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); + List itemList = items.Cast().ToList(); itemList.Should().NotContain(i => i is DesignerActionPropertyItem &&