Skip to content

Commit

Permalink
Add Code Coverage For ToolStrip (#11464)
Browse files Browse the repository at this point in the history
* Add Code Coverage For ToolStrip

* Handle FeedBacks

* Handle FeedBack

* Handle build error
  • Loading branch information
Zheng-Li01 committed Jun 4, 2024
1 parent 55690f4 commit 8a67b28
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Drawing;
using Windows.Win32.UI.Accessibility;
using static System.Windows.Forms.ToolStripDropDownButton;

Expand Down Expand Up @@ -92,4 +93,33 @@ public void ToolStripDropDownButtonAccessibleObject_FragmentNavigate_Child_Retur
Assert.Equal(expected, accessibleObject.FragmentNavigate(NavigateDirection.NavigateDirection_FirstChild));
Assert.Equal(expected, accessibleObject.FragmentNavigate(NavigateDirection.NavigateDirection_LastChild));
}

[WinFormsFact]
public void ToolStripDropDownButton_ConstructorParameters_ShouldInitializeCorrectly()
{
string text = "Test text";
using Bitmap image = new(10, 10);
bool wasClicked = false;
EventHandler onClick = (sender, e) => { wasClicked = true; };
string name = "Test name";
ToolStripItem[] dropDownItems = [new ToolStripMenuItem("Test item")];

ToolStripDropDownButton CreateToolStripDropDownButton(string text = null, Bitmap image = null, EventHandler onClick = null, string name = null, ToolStripItem[] dropDownItems = null)
{
ToolStripDropDownButton toolStripDropDownButton = new(text, image, dropDownItems);
toolStripDropDownButton.Click += onClick;
toolStripDropDownButton.Name = name;

return toolStripDropDownButton;
}

var toolStripDropDownButton = CreateToolStripDropDownButton(text, image, onClick, name, dropDownItems);
toolStripDropDownButton.PerformClick();

toolStripDropDownButton.Text.Should().Be(text);
toolStripDropDownButton.Image.Should().Be(image);
wasClicked.Should().BeTrue();
toolStripDropDownButton.Name.Should().Be(name);
toolStripDropDownButton.DropDownItems.Should().BeEquivalentTo(dropDownItems);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Windows.Forms.Tests;
public class ToolStripCustomIComparerTests
{
private readonly ToolStripCustomIComparer _comparer = new();

[WinFormsTheory]
[InlineData(null, null, 0)]
[InlineData(null, typeof(ToolStrip), -1)]
[InlineData(typeof(ToolStrip), null, 1)]
[InlineData(typeof(ToolStrip), typeof(ToolStrip), 0)]
[InlineData(typeof(ToolStrip), typeof(ToolStripDropDown), 1)]
[InlineData(typeof(ToolStripDropDown), typeof(ToolStrip), -1)]
public void ToolStripCustomIComparer_Compare_Tests(Type type1, Type type2, int expected)
{
object obj1 = type1 is null ? null : Activator.CreateInstance(type1);
object obj2 = type2 is null ? null : Activator.CreateInstance(type2);
int result = _comparer.Compare((ToolStrip)obj1, (ToolStrip)obj2);

result.Should().Be(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4930,6 +4930,128 @@ public void ToolStripDropDown_WorkingAreaConstrained_AssignmentTest(bool value)
Assert.Equal(value, control.WorkingAreaConstrained);
}

[WinFormsFact]
public void ToolStripDropDown_DockChanged_AddRemoveEvent_Invoke_Success()
{
using ToolStripDropDown control = new();

int callCount = 0;
EventHandler handler = (sender, e) =>
{
sender.Should().Be(control);
e.Should().Be(EventArgs.Empty);
callCount++;
};

control.DockChanged += handler;
control.Dock = DockStyle.Left;
callCount.Should().Be(1);
control.Dock.Should().Be(DockStyle.Left);

control.Dock = DockStyle.Right;
callCount.Should().Be(2);
control.Dock.Should().Be(DockStyle.Right);

control.DockChanged -= handler;
control.Dock = DockStyle.Top;
callCount.Should().Be(2);
control.Dock.Should().Be(DockStyle.Top);
}

[WinFormsTheory]
[InlineData(null)]
[InlineData(ToolStripDropDownCloseReason.AppClicked)]
[InlineData(ToolStripDropDownCloseReason.AppFocusChange)]
[InlineData(ToolStripDropDownCloseReason.CloseCalled)]
[InlineData(ToolStripDropDownCloseReason.ItemClicked)]
[InlineData(ToolStripDropDownCloseReason.Keyboard)]
public void ToolStripDropDown_Close_InvokeReason_Success(ToolStripDropDownCloseReason? reason)
{
using ToolStripDropDown control = new();
control.Visible = true;

if (reason.HasValue)
{
control.Close(reason.Value);
}
else
{
control.Close();
}

control.Visible.Should().BeFalse();
}

[WinFormsTheory]
[InlineData(10, 20, ToolStripDropDownDirection.AboveLeft)]
public void ToolStripDropDown_Show_ControlPointDirection(int x, int y, ToolStripDropDownDirection direction)
{
using Control control = new();
using ToolStripDropDown toolStripDropDown = new();

toolStripDropDown.Show(control, new Point(x, y), direction);

toolStripDropDown.Location.Should().Be(new Point(x + 6, y + 27));
}

[WinFormsTheory]
[InlineData(10, 20, ToolStripDropDownDirection.AboveLeft)]
public void ToolStripDropDown_Show_PositionDirection(int x, int y, ToolStripDropDownDirection direction)
{
using ToolStripDropDown toolStripDropDown = new();

toolStripDropDown.Show(new Point(x, y), direction);
Point expectedLocation = new(x, y);
var nonClientSize = SystemInformation.BorderSize;
expectedLocation.Offset(nonClientSize.Width-3, nonClientSize.Height -1);

if (direction is ToolStripDropDownDirection.AboveLeft or ToolStripDropDownDirection.AboveRight)
{
expectedLocation.Offset(0, -toolStripDropDown.Height);
}

toolStripDropDown.Location.Should().Be(expectedLocation);
}

[WinFormsTheory]
[InlineData(10, 20, true)]
[InlineData(10, 20, false)]
public void ToolStripDropDown_Show_ControlLocation(int x, int y, bool usePoint)
{
using Control control = new();
using ToolStripDropDown toolStripDropDown = new();

if (usePoint)
{
toolStripDropDown.Show(control, new Point(x, y));
}
else
{
toolStripDropDown.Show(control, x, y);
}

toolStripDropDown.Location.Should().Be(new Point(x + 8, y + 31));
}

[WinFormsTheory]
[InlineData(10, 20, true)]
[InlineData(10, 20, false)]
public void ToolStripDropDown_Show_ScreenLocation(int x, int y, bool usePoint)
{
using ToolStripDropDown toolStripDropDown = new();

if (usePoint)
{
toolStripDropDown.Show(new Point(x, y));
}
else
{
toolStripDropDown.Show(x, y);
}

toolStripDropDown.Location.Should().Be(new Point(x, y));
}

private class SubAxHost : AxHost
{
public SubAxHost(string clsid) : base(clsid)
Expand Down
Loading

0 comments on commit 8a67b28

Please sign in to comment.