Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for ToolStripActionList #12525

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<IDesignerHost> _mockDesignerHost;
private readonly Mock<ISelectionService> _mockSelectionService;
private readonly Mock<IComponentChangeService> _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<ISite> 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<ToolStripActionList>();
_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<DockStyle>]
public void Dock_GetSet_ReturnsExpected(DockStyle dockStyle)
{
_actionList.Dock.Should().Be(DockStyle.Top);

_actionList.Dock = dockStyle;
_actionList.Dock.Should().Be(dockStyle);
}

[Theory]
[EnumData<ToolStripRenderMode>]
public void RenderMode_GetSet_ReturnsExpected(ToolStripRenderMode renderMode)
{
if (renderMode == ToolStripRenderMode.Custom)
{
_actionList.Invoking(a => a.RenderMode = renderMode)
.Should().Throw<NotSupportedException>();
}
else
{
_actionList.RenderMode = renderMode;
_actionList.RenderMode.Should().Be(renderMode);
}
}

[Theory]
[EnumData<ToolStripGripStyle>]
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();
MelonWang1 marked this conversation as resolved.
Show resolved Hide resolved
var itemList = items.Cast<DesignerActionItem>().ToList();

itemList.Should().ContainSingle(i => i is DesignerActionMethodItem && ((DesignerActionMethodItem)i).MemberName == "InvokeEmbedVerb");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be simplified

Suggested change
itemList.Should().ContainSingle(i => i is DesignerActionMethodItem && ((DesignerActionMethodItem)i).MemberName == "InvokeEmbedVerb");
itemList.Should().ContainSingle(i => i is DesignerActionMethodItem action && (action.MemberName == "InvokeEmbedVerb");

same can be applied in other places in this change

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<DesignerActionItem>().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<DesignerActionItem>().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<DesignerActionItem>().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<DesignerActionItem>().ToList();

itemList.Should().NotContain(i =>
i is DesignerActionPropertyItem &&
((DesignerActionPropertyItem)i).MemberName == "GripStyle");
}
}