Skip to content

Commit 96e2d7b

Browse files
committed
Add unit tests for StatusCommandUI
1 parent 8691d0e commit 96e2d7b

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.ComponentModel;
5+
using System.ComponentModel.Design;
6+
using System.Drawing;
7+
using Moq;
8+
9+
namespace System.Windows.Forms.Design.Tests;
10+
11+
public class StatusCommandUITests
12+
{
13+
private readonly Mock<IServiceProvider> _mockServiceProvider;
14+
private readonly Mock<IMenuCommandService> _mockMenuCommandService;
15+
private readonly Mock<MenuCommand> _mockMenuCommand;
16+
private readonly StatusCommandUI _statusCommandUI;
17+
18+
public StatusCommandUITests()
19+
{
20+
_mockServiceProvider = new();
21+
_mockMenuCommandService = new();
22+
_mockMenuCommand = new(null!, null!);
23+
24+
_mockServiceProvider
25+
.Setup(sp => sp.GetService(typeof(IMenuCommandService)))
26+
.Returns(_mockMenuCommandService.Object);
27+
28+
_mockMenuCommandService
29+
.Setup(mcs => mcs.FindCommand(MenuCommands.SetStatusRectangle))
30+
.Returns(_mockMenuCommand.Object);
31+
32+
_statusCommandUI = new(_mockServiceProvider.Object);
33+
}
34+
35+
[Fact]
36+
public void SetStatusInformation_WithNullComponent_ShouldNotInvokeCommand()
37+
{
38+
_statusCommandUI.SetStatusInformation(null);
39+
_mockMenuCommand.Verify(mc => mc.Invoke(It.IsAny<Rectangle>()), Times.Never);
40+
}
41+
42+
[Fact]
43+
public void SetStatusInformation_WithControlComponent_ShouldInvokeWithOriginalBounds()
44+
{
45+
using Control control = new() { Bounds = new(10, 20, 30, 40) };
46+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(control), new(10, 20, 30, 40));
47+
}
48+
49+
[Fact]
50+
public void SetStatusInformation_WithCustomComponentHavingBounds_ShouldInvokeWithOriginalBounds()
51+
{
52+
using CustomComponent component = new() { Bounds = new(10, 20, 30, 40) };
53+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(component), new(10, 20, 30, 40));
54+
}
55+
56+
[Fact]
57+
public void SetStatusInformation_WithComponentWithoutBounds_ShouldInvokeWithEmptyRectangle()
58+
{
59+
using Component component = new();
60+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(component), Rectangle.Empty);
61+
}
62+
63+
[Fact]
64+
public void SetStatusInformation_WithRectangle_ShouldInvokeWithSameRectangle()
65+
{
66+
Rectangle bounds = new(10, 20, 30, 40);
67+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(bounds), bounds);
68+
}
69+
70+
[Fact]
71+
public void SetStatusInformation_WithEmptyRectangle_ShouldInvokeWithEmptyRectangle()
72+
{
73+
Rectangle bounds = Rectangle.Empty;
74+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(bounds), bounds);
75+
}
76+
77+
[Fact]
78+
public void SetStatusInformation_WithControlAndLocation_ShouldInvokeWithUpdatedBounds()
79+
{
80+
using Control control = new() { Bounds = new(10, 20, 30, 40) };
81+
Point location = new(50, 60);
82+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(control, location), new(50, 60, 30, 40));
83+
}
84+
85+
[Fact]
86+
public void SetStatusInformation_WithComponentHavingBoundsAndLocation_ShouldInvokeWithUpdatedBounds()
87+
{
88+
using CustomComponent component = new() { Bounds = new(10, 20, 30, 40) };
89+
Point location = new(50, 60);
90+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(component, location), new(50, 60, 30, 40));
91+
}
92+
93+
[Fact]
94+
public void SetStatusInformation_WithComponentWithoutBoundsAndLocation_ShouldInvokeWithEmptyRectangle()
95+
{
96+
using Component component = new();
97+
Point location = new(50, 60);
98+
_statusCommandUI.SetStatusInformation(component, location);
99+
_mockMenuCommand.Verify(cmd => cmd.Invoke(new Rectangle(50, 60, 0, 0)), Times.Once);
100+
}
101+
102+
[Fact]
103+
public void SetStatusInformation_WithLocationAsEmpty_ShouldNotChangeBounds()
104+
{
105+
using Control control = new() { Bounds = new(10, 20, 30, 40) };
106+
Point location = Point.Empty;
107+
TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(control, location), new(10, 20, 30, 40));
108+
}
109+
110+
[Fact]
111+
public void SetStatusInformation_NullComponentWithLocation_ShouldNotInvokeCommand()
112+
{
113+
_statusCommandUI.SetStatusInformation(null, new Point(10, 10));
114+
_mockMenuCommand.Verify(cmd => cmd.Invoke(It.IsAny<Rectangle>()), Times.Never);
115+
}
116+
117+
private void TestSetStatusInformation(Action action, Rectangle expectedRectangle)
118+
{
119+
action.Invoke();
120+
VerifyInvoke(expectedRectangle);
121+
}
122+
123+
private void VerifyInvoke(Rectangle expectedRectangle)
124+
{
125+
_mockMenuCommand.Verify(mc => mc.Invoke(It.Is<Rectangle>(r =>
126+
r.X == expectedRectangle.X &&
127+
r.Y == expectedRectangle.Y &&
128+
r.Width == expectedRectangle.Width &&
129+
r.Height == expectedRectangle.Height)), Times.Once);
130+
}
131+
132+
private class CustomComponent : Component
133+
{
134+
public Rectangle Bounds { get; set; }
135+
}
136+
}

0 commit comments

Comments
 (0)