Skip to content

Commit 1922204

Browse files
authored
Add code coverage for DesignerUtils (#13341)
* Add code coverage for DesignerUtils * Address the FeedBacks
1 parent fb328e6 commit 1922204

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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.Drawing;
5+
using System.Drawing.Imaging;
6+
7+
namespace System.Windows.Forms.Design.Tests;
8+
9+
public class DesignerUtilsTests :IDisposable
10+
{
11+
private readonly Bitmap _bitmap;
12+
private readonly Graphics _graphics;
13+
private readonly Rectangle _bounds;
14+
15+
public DesignerUtilsTests()
16+
{
17+
_bitmap = new(20, 20);
18+
_graphics = Graphics.FromImage(_bitmap);
19+
_bounds = new(5, 5, 20, 20);
20+
}
21+
22+
public void Dispose()
23+
{
24+
_graphics.Dispose();
25+
_bitmap.Dispose();
26+
}
27+
28+
[WinFormsFact]
29+
public void BoxImage_ShouldReturnNonNullImage() =>
30+
DesignerUtils.BoxImage.Should().BeOfType<Bitmap>();
31+
32+
[WinFormsFact]
33+
public void BoxImage_ShouldHaveExpectedDimensions()
34+
{
35+
DesignerUtils.BoxImage.Width.Should().Be(DesignerUtils.s_boxImageSize);
36+
DesignerUtils.BoxImage.Height.Should().Be(DesignerUtils.s_boxImageSize);
37+
}
38+
39+
[WinFormsFact]
40+
public void BoxImage_ShouldHaveExpectedPixelFormat() =>
41+
((Bitmap)DesignerUtils.BoxImage).PixelFormat.Should().Be(PixelFormat.Format32bppPArgb);
42+
43+
[WinFormsFact]
44+
public void BoxImage_ShouldBeCached()
45+
{
46+
Image firstCall = DesignerUtils.BoxImage;
47+
Image secondCall = DesignerUtils.BoxImage;
48+
firstCall.Should().BeSameAs(secondCall);
49+
}
50+
51+
[Fact]
52+
public void HoverBrush_ShouldBeSolidBrush() =>
53+
DesignerUtils.HoverBrush.Should().BeOfType<SolidBrush>();
54+
55+
[Fact]
56+
public void HoverBrush_ShouldHaveExpectedColor() =>
57+
((SolidBrush)DesignerUtils.HoverBrush).Color.Should().Be(Color.FromArgb(50, SystemColors.Highlight));
58+
59+
[Fact]
60+
public void MinDragSize_ShouldReturnNonEmptySize()
61+
{
62+
Size minDragSize = DesignerUtils.MinDragSize;
63+
minDragSize.Should().NotBe(Size.Empty);
64+
}
65+
66+
[Fact]
67+
public void MinDragSize_ShouldBeCached()
68+
{
69+
Size firstCall = DesignerUtils.MinDragSize;
70+
Size secondCall = DesignerUtils.MinDragSize;
71+
firstCall.Should().Be(secondCall);
72+
}
73+
74+
[WinFormsTheory]
75+
[MemberData(nameof(ResizeBorderTestData))]
76+
public void DrawResizeBorder_ShouldUseCorrectBrushBasedOnBackColor(Color backColor, Color expectedColor)
77+
{
78+
using Region region = new(new Rectangle(0, 0, _bounds.Width, _bounds.Height));
79+
DesignerUtils.DrawResizeBorder(_graphics, region, backColor);
80+
Color pixelColor = _bitmap.GetPixel(_bounds.Width / 2, _bounds.Height / 2);
81+
pixelColor.ToArgb().Should().Be(expectedColor.ToArgb());
82+
}
83+
84+
public static TheoryData<Color, Color> ResizeBorderTestData => new()
85+
{
86+
{ Color.White, SystemColors.ControlDarkDark },
87+
{ Color.Black, SystemColors.ControlLight }
88+
};
89+
90+
[WinFormsFact]
91+
public void DrawGrabHandle_ShouldNotThrow_WhenCalledWithValidParameters()
92+
{
93+
Exception exception = Record.Exception(() => DesignerUtils.DrawGrabHandle(_graphics, _bounds, isPrimary: true));
94+
exception.Should().BeNull();
95+
}
96+
97+
[WinFormsTheory]
98+
[BoolData]
99+
public void DrawGrabHandle_ShouldDrawHandle_BasedOnSelectionType(bool isPrimary)
100+
{
101+
DesignerUtils.DrawGrabHandle(_graphics, _bounds, isPrimary);
102+
103+
Color pixelColor = _bitmap.GetPixel(6, 6);
104+
pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb());
105+
}
106+
107+
[WinFormsTheory]
108+
[BoolData]
109+
public void DrawLockedHandle_ShouldDrawHandle_BasedOnSelectionType(bool isPrimary)
110+
{
111+
DesignerUtils.DrawLockedHandle(_graphics, _bounds, isPrimary);
112+
113+
Color pixelColor = _bitmap.GetPixel(6, 6);
114+
pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb());
115+
}
116+
117+
[WinFormsFact]
118+
public void DrawLockedHandle_ShouldDrawUpperRect_WhenCalledWithPrimarySelection()
119+
{
120+
DesignerUtils.DrawLockedHandle(_graphics, _bounds, isPrimary: true);
121+
Color pixelColor = _bitmap.GetPixel(_bounds.Left + 1, _bounds.Top + 1);
122+
pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb());
123+
}
124+
125+
[WinFormsFact]
126+
public void DrawLockedHandle_ShouldDrawLowerRect_WhenCalledWithNonPrimarySelection()
127+
{
128+
DesignerUtils.DrawLockedHandle(_graphics, _bounds, isPrimary: false);
129+
Color pixelColor = _bitmap.GetPixel(_bounds.Left + 1, _bounds.Top + DesignerUtils.s_lockedHandleLowerOffset + 1);
130+
pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb());
131+
}
132+
133+
[WinFormsFact]
134+
public void DrawSelectionBorder_ShouldNotThrow_WhenCalledWithValidParameters()
135+
{
136+
Exception exception = Record.Exception(() => DesignerUtils.DrawSelectionBorder(_graphics, _bounds));
137+
exception.Should().BeNull();
138+
}
139+
140+
[WinFormsFact]
141+
public void DrawSelectionBorder_ShouldFillRectangle_WhenCalled()
142+
{
143+
DesignerUtils.DrawSelectionBorder(_graphics, _bounds);
144+
Color pixelColor = _bitmap.GetPixel(6, 6);
145+
pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb());
146+
}
147+
}

0 commit comments

Comments
 (0)