Skip to content

Commit a821c1c

Browse files
authored
Add code coverage for ButtonFlatAdapter (#13743)
* Add code coverage for ButtonFlatAdapter
1 parent 6e61bc0 commit a821c1c

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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.Windows.Forms.ButtonInternal;
6+
7+
namespace System.Windows.Forms.Tests;
8+
9+
public class ButtonFlatAdapterTests : IDisposable
10+
{
11+
public TestButtonBase buttonBase { get; }
12+
internal ButtonFlatAdapter buttonFlatAdapter { get; }
13+
14+
public ButtonFlatAdapterTests()
15+
{
16+
buttonBase = new TestButtonBase();
17+
buttonFlatAdapter = new(buttonBase);
18+
}
19+
20+
public void Dispose() => buttonBase.Dispose();
21+
22+
public class TestButtonBase : ButtonBase
23+
{
24+
public Rectangle ClientRectangleValue { get; set; } = new(0, 0, 100, 30);
25+
26+
internal override Rectangle OverChangeRectangle => ClientRectangleValue;
27+
28+
internal override ButtonBaseAdapter CreateStandardAdapter() => new ButtonFlatAdapter(this);
29+
30+
internal override StringFormat CreateStringFormat() => new();
31+
32+
internal override TextFormatFlags CreateTextFormatFlags() => TextFormatFlags.Default;
33+
}
34+
35+
public static TheoryData<CheckState> PaintStates => new()
36+
{
37+
CheckState.Unchecked,
38+
CheckState.Checked,
39+
CheckState.Indeterminate
40+
};
41+
42+
[WinFormsTheory]
43+
[MemberData(nameof(PaintStates))]
44+
public void PaintUp_DoesNotThrow(CheckState state)
45+
{
46+
using Bitmap bitmap = new(100, 30);
47+
using Graphics graphics = Graphics.FromImage(bitmap);
48+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
49+
50+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintUp(e, state));
51+
exception.Should().BeNull();
52+
}
53+
54+
[WinFormsTheory]
55+
[MemberData(nameof(PaintStates))]
56+
public void PaintDown_DoesNotThrow(CheckState state)
57+
{
58+
using Bitmap bitmap = new(100, 30);
59+
using Graphics graphics = Graphics.FromImage(bitmap);
60+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
61+
62+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintDown(e, state));
63+
exception.Should().BeNull();
64+
}
65+
66+
[WinFormsTheory]
67+
[MemberData(nameof(PaintStates))]
68+
public void PaintOver_DoesNotThrow(CheckState state)
69+
{
70+
using Bitmap bitmap = new(100, 30);
71+
using Graphics graphics = Graphics.FromImage(bitmap);
72+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
73+
74+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintOver(e, state));
75+
exception.Should().BeNull();
76+
}
77+
78+
[WinFormsTheory]
79+
[MemberData(nameof(PaintStates))]
80+
public void PaintUp_WithCustomBorder_DoesNotThrow(CheckState state)
81+
{
82+
using TestButtonBase button = new();
83+
button.FlatAppearance.BorderSize = 2;
84+
button.FlatAppearance.BorderColor = Color.Red;
85+
ButtonFlatAdapter buttonFlatAdapter = new(button);
86+
87+
using Bitmap bitmap = new(100, 30);
88+
using Graphics graphics = Graphics.FromImage(bitmap);
89+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
90+
91+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintUp(e, state));
92+
exception.Should().BeNull();
93+
}
94+
95+
[WinFormsTheory]
96+
[MemberData(nameof(PaintStates))]
97+
public void PaintDown_WithMouseDownBackColor_DoesNotThrow(CheckState state)
98+
{
99+
using TestButtonBase button = new();
100+
button.FlatAppearance.MouseDownBackColor = Color.Blue;
101+
ButtonFlatAdapter buttonFlatAdapter = new(button);
102+
103+
using Bitmap bitmap = new(100, 30);
104+
using Graphics graphics = Graphics.FromImage(bitmap);
105+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
106+
107+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintDown(e, state));
108+
exception.Should().BeNull();
109+
}
110+
111+
[WinFormsTheory]
112+
[MemberData(nameof(PaintStates))]
113+
public void PaintOver_WithMouseOverBackColor_DoesNotThrow(CheckState state)
114+
{
115+
using TestButtonBase button = new();
116+
button.FlatAppearance.MouseOverBackColor = Color.Green;
117+
ButtonFlatAdapter buttonFlatAdapter = new(button);
118+
119+
using Bitmap bitmap = new(100, 30);
120+
using Graphics graphics = Graphics.FromImage(bitmap);
121+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
122+
123+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintOver(e, state));
124+
exception.Should().BeNull();
125+
}
126+
127+
[WinFormsTheory]
128+
[MemberData(nameof(PaintStates))]
129+
public void PaintUp_WithCheckedBackColor_DoesNotThrow(CheckState state)
130+
{
131+
using TestButtonBase button = new();
132+
button.FlatAppearance.CheckedBackColor = Color.Yellow;
133+
ButtonFlatAdapter buttonFlatAdapter = new(button);
134+
135+
using Bitmap bitmap = new(100, 30);
136+
using Graphics graphics = Graphics.FromImage(bitmap);
137+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
138+
139+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintUp(e, state));
140+
exception.Should().BeNull();
141+
}
142+
143+
[WinFormsTheory]
144+
[BoolData]
145+
public void PaintUp_WithIsDefault_DoesNotThrow(bool isDefault)
146+
{
147+
using TestButtonBase button = new();
148+
button.IsDefault = isDefault;
149+
ButtonFlatAdapter buttonFlatAdapter = new(button);
150+
151+
using Bitmap bitmap = new(100, 30);
152+
using Graphics graphics = Graphics.FromImage(bitmap);
153+
using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30));
154+
155+
Exception? exception = Record.Exception(() => buttonFlatAdapter.PaintUp(e, CheckState.Unchecked));
156+
exception.Should().BeNull();
157+
}
158+
159+
[Fact]
160+
public void PaintFlatLayout_SetsProperties_Correctly_WhenUpIsFalse()
161+
{
162+
Rectangle clientRectangle = new(0, 0, 50, 20);
163+
Padding padding = new(1, 2, 3, 4);
164+
using Font font = new("Segoe UI", 9);
165+
string text = "CombinedTest";
166+
bool enabled = true;
167+
ContentAlignment textAlign = ContentAlignment.TopLeft;
168+
RightToLeft rightToLeft = RightToLeft.No;
169+
170+
ButtonBaseAdapter.LayoutOptions layoutOptionsTrue = ButtonFlatAdapter.PaintFlatLayout(
171+
up: false,
172+
check: true,
173+
borderSize: 2,
174+
clientRectangle,
175+
padding,
176+
isDefault: false,
177+
font,
178+
text,
179+
enabled,
180+
textAlign,
181+
rightToLeft);
182+
183+
ButtonBaseAdapter.LayoutOptions layoutOptionsFalse = ButtonFlatAdapter.PaintFlatLayout(
184+
up: false,
185+
check: false,
186+
borderSize: 2,
187+
clientRectangle,
188+
padding,
189+
isDefault: false,
190+
font,
191+
text,
192+
enabled,
193+
textAlign,
194+
rightToLeft);
195+
196+
layoutOptionsTrue.PaddingSize.Should().Be(1);
197+
layoutOptionsFalse.PaddingSize.Should().Be(2);
198+
199+
layoutOptionsTrue.FocusOddEvenFixup.Should().BeFalse();
200+
layoutOptionsFalse.FocusOddEvenFixup.Should().BeFalse();
201+
202+
layoutOptionsTrue.BorderSize.Should().Be(3);
203+
layoutOptionsFalse.BorderSize.Should().Be(2);
204+
}
205+
206+
[Theory]
207+
[BoolData]
208+
public void PaintFlatLayout_SetsTextOffset_Correctly(bool up)
209+
{
210+
Rectangle clientRectangle = new(0, 0, 50, 20);
211+
Padding padding = new(1, 2, 3, 4);
212+
using Font font = new("Segoe UI", 9);
213+
string text = "TextOffsetTest";
214+
bool enabled = true;
215+
ContentAlignment textAlign = ContentAlignment.TopLeft;
216+
RightToLeft rightToLeft = RightToLeft.No;
217+
218+
ButtonBaseAdapter.LayoutOptions layoutOptions = ButtonFlatAdapter.PaintFlatLayout(
219+
up,
220+
check: false,
221+
borderSize: 2,
222+
clientRectangle,
223+
padding,
224+
isDefault: false,
225+
font,
226+
text,
227+
enabled,
228+
textAlign,
229+
rightToLeft);
230+
231+
layoutOptions.TextOffset.Should().Be(!up);
232+
}
233+
}

0 commit comments

Comments
 (0)