Skip to content

Commit 70724a7

Browse files
author
Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)
committed
Add unit tests for FlatButton, RadioButton, CheckBox, RadioButtonRenderer and CheckBoxRenderer
1 parent d152b3c commit 70724a7

File tree

6 files changed

+596
-49
lines changed

6 files changed

+596
-49
lines changed

src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ComboBoxDesignerTests.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ButtonBaseTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9244,6 +9244,52 @@ public void ButtonBase_WndProc_InvokeSetStateWithHandle_Success(FlatStyle flatSt
92449244
Assert.Equal(0, createdCallCount);
92459245
}
92469246

9247+
[WinFormsTheory]
9248+
[InlineData(-1)]
9249+
[InlineData(0)]
9250+
[InlineData(1)]
9251+
public unsafe void Button_Flat_ValidBorder(int borderSize)
9252+
{
9253+
using SubButton button = new()
9254+
{
9255+
FlatStyle = FlatStyle.Flat,
9256+
};
9257+
9258+
Assert.Throws<NotSupportedException>(() => button.FlatAppearance.BorderColor = Color.Transparent);
9259+
9260+
if (borderSize < 0)
9261+
{
9262+
Assert.Throws<ArgumentOutOfRangeException>(() => button.FlatAppearance.BorderSize = borderSize);
9263+
}
9264+
else
9265+
{
9266+
button.FlatAppearance.BorderSize = borderSize;
9267+
Assert.Equal(borderSize, button.FlatAppearance.BorderSize);
9268+
}
9269+
}
9270+
9271+
[WinFormsTheory]
9272+
[InlineData(255, 0, 0)]
9273+
[InlineData(0, 255, 0)]
9274+
[InlineData(0, 0, 255)]
9275+
public unsafe void Button_Flat_ProperColor(int red, int green, int blue)
9276+
{
9277+
Color expectedColor = Color.FromArgb(red, green, blue);
9278+
9279+
using SubButton button = new()
9280+
{
9281+
FlatStyle = FlatStyle.Flat,
9282+
BackColor = expectedColor
9283+
};
9284+
9285+
button.FlatAppearance.CheckedBackColor = expectedColor;
9286+
button.FlatAppearance.BorderColor = expectedColor;
9287+
9288+
Assert.Equal(expectedColor, button.BackColor);
9289+
Assert.Equal(expectedColor, button.FlatAppearance.BorderColor);
9290+
Assert.Equal(expectedColor, button.FlatAppearance.CheckedBackColor);
9291+
}
9292+
92479293
private class SubButton : Button
92489294
{
92499295
public new bool GetStyle(ControlStyles flag) => base.GetStyle(flag);
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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.Metafiles;
6+
using System.Windows.Forms.VisualStyles;
7+
8+
namespace System.Windows.Forms.Tests;
9+
10+
public class CheckBoxRenderingTests
11+
{
12+
[WinFormsTheory]
13+
[InlineData(CheckBoxState.CheckedNormal)]
14+
[InlineData(CheckBoxState.MixedNormal)]
15+
public void CheckBoxRenderer_DrawCheckBox(CheckBoxState state)
16+
{
17+
using Bitmap bitmap = new(100, 100);
18+
using Graphics graphics = Graphics.FromImage(bitmap);
19+
Point rectangle = new Point(10, 20);
20+
21+
CheckBoxRenderer.DrawCheckBox(graphics, rectangle, state);
22+
}
23+
24+
[WinFormsTheory]
25+
[InlineData(CheckBoxState.CheckedNormal)]
26+
[InlineData(CheckBoxState.MixedNormal)]
27+
public void CheckBoxRenderer_DrawCheckBox_OverloadWithSizeAndText(CheckBoxState state)
28+
{
29+
using Bitmap bitmap = new(100, 100);
30+
using Graphics graphics = Graphics.FromImage(bitmap);
31+
Point point = new Point(10, 20);
32+
Rectangle bounds = new Rectangle(10, 20, 30, 40);
33+
34+
CheckBoxRenderer.DrawCheckBox(graphics, point, bounds, "Text", SystemFonts.DefaultFont, false, state);
35+
}
36+
37+
[WinFormsTheory]
38+
[InlineData(TextFormatFlags.Default, CheckBoxState.CheckedNormal)]
39+
[InlineData(TextFormatFlags.Default, CheckBoxState.MixedNormal)]
40+
[InlineData(TextFormatFlags.GlyphOverhangPadding, CheckBoxState.MixedHot)]
41+
[InlineData(TextFormatFlags.PreserveGraphicsTranslateTransform, CheckBoxState.CheckedPressed)]
42+
[InlineData(TextFormatFlags.TextBoxControl, CheckBoxState.UncheckedNormal)]
43+
public void CheckBoxRenderer_DrawCheckBox_OverloadWithTextFormat(TextFormatFlags textFormat, CheckBoxState state)
44+
{
45+
using Bitmap bitmap = new(100, 100);
46+
using Graphics graphics = Graphics.FromImage(bitmap);
47+
Point point = new Point(10, 20);
48+
Rectangle bounds = new Rectangle(10, 20, 30, 40);
49+
50+
CheckBoxRenderer.DrawCheckBox(graphics, point, bounds, "Text", SystemFonts.DefaultFont, textFormat, false, state);
51+
}
52+
53+
[WinFormsFact]
54+
public unsafe void CaptureButton()
55+
{
56+
using CheckBox checkBox = new();
57+
using EmfScope emf = new();
58+
checkBox.PrintToMetafile(emf);
59+
60+
List<ENHANCED_METAFILE_RECORD_TYPE> types = [];
61+
List<string> details = [];
62+
emf.Enumerate((ref EmfRecord record) =>
63+
{
64+
types.Add(record.Type);
65+
details.Add(record.ToString());
66+
return true;
67+
});
68+
}
69+
70+
[WinFormsFact]
71+
public unsafe void Button_VisualStyles_off_Default_LineDrawing()
72+
{
73+
if (Application.RenderWithVisualStyles)
74+
{
75+
return;
76+
}
77+
78+
using CheckBox checkBox = new();
79+
using EmfScope emf = new();
80+
DeviceContextState state = new(emf);
81+
Rectangle bounds = checkBox.Bounds;
82+
83+
checkBox.PrintToMetafile(emf);
84+
85+
emf.Validate(
86+
state,
87+
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_BITBLT), 1),
88+
Validate.LineTo(
89+
new(bounds.Right - 1, 0), new(0, 0),
90+
State.PenColor(SystemColors.ControlLightLight)),
91+
Validate.LineTo(
92+
new(0, 0), new(0, bounds.Bottom - 1),
93+
State.PenColor(SystemColors.ControlLightLight)),
94+
Validate.LineTo(
95+
new(0, bounds.Bottom - 1), new(bounds.Right - 1, bounds.Bottom - 1),
96+
State.PenColor(SystemColors.ControlDarkDark)),
97+
Validate.LineTo(
98+
new(bounds.Right - 1, bounds.Bottom - 1), new(bounds.Right - 1, -1),
99+
State.PenColor(SystemColors.ControlDarkDark)),
100+
Validate.LineTo(
101+
new(bounds.Right - 2, 1), new(1, 1),
102+
State.PenColor(SystemColors.Control)),
103+
Validate.LineTo(
104+
new(1, 1), new(1, bounds.Bottom - 2),
105+
State.PenColor(SystemColors.Control)),
106+
Validate.LineTo(
107+
new(1, bounds.Bottom - 2), new(bounds.Right - 2, bounds.Bottom - 2),
108+
State.PenColor(SystemColors.ControlDark)),
109+
Validate.LineTo(
110+
new(bounds.Right - 2, bounds.Bottom - 2), new(bounds.Right - 2, 0),
111+
State.PenColor(SystemColors.ControlDark)));
112+
}
113+
114+
[WinFormsFact]
115+
public unsafe void Button_VisualStyles_off_Default_WithText_LineDrawing()
116+
{
117+
if (Application.RenderWithVisualStyles)
118+
{
119+
return;
120+
}
121+
122+
using CheckBox checkBox = new() { Text = "Hello" };
123+
using EmfScope emf = new();
124+
DeviceContextState state = new(emf);
125+
Rectangle bounds = checkBox.Bounds;
126+
127+
checkBox.PrintToMetafile(emf);
128+
129+
emf.Validate(
130+
state,
131+
Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_BITBLT),
132+
Validate.TextOut("Hello"),
133+
Validate.LineTo(
134+
new(bounds.Right - 1, 0), new(0, 0),
135+
State.PenColor(SystemColors.ControlLightLight)),
136+
Validate.LineTo(
137+
new(0, 0), new(0, bounds.Bottom - 1),
138+
State.PenColor(SystemColors.ControlLightLight)),
139+
Validate.LineTo(
140+
new(0, bounds.Bottom - 1), new(bounds.Right - 1, bounds.Bottom - 1),
141+
State.PenColor(SystemColors.ControlDarkDark)),
142+
Validate.LineTo(
143+
new(bounds.Right - 1, bounds.Bottom - 1), new(bounds.Right - 1, -1),
144+
State.PenColor(SystemColors.ControlDarkDark)),
145+
Validate.LineTo(
146+
new(bounds.Right - 2, 1), new(1, 1),
147+
State.PenColor(SystemColors.Control)),
148+
Validate.LineTo(
149+
new(1, 1), new(1, bounds.Bottom - 2),
150+
State.PenColor(SystemColors.Control)),
151+
Validate.LineTo(
152+
new(1, bounds.Bottom - 2), new(bounds.Right - 2, bounds.Bottom - 2),
153+
State.PenColor(SystemColors.ControlDark)),
154+
Validate.LineTo(
155+
new(bounds.Right - 2, bounds.Bottom - 2), new(bounds.Right - 2, 0),
156+
State.PenColor(SystemColors.ControlDark)));
157+
}
158+
159+
[WinFormsFact]
160+
public unsafe void CaptureButtonOnForm()
161+
{
162+
using Form form = new();
163+
using CheckBox checkBox = new();
164+
form.Controls.Add(checkBox);
165+
166+
using EmfScope emf = new();
167+
form.PrintToMetafile(emf);
168+
169+
string details = emf.RecordsToString();
170+
}
171+
}

0 commit comments

Comments
 (0)