Skip to content

Add code coverage for buttons #10693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using System.Text;
using System.Windows.Forms.Metafiles;

namespace System.Windows.Forms.misc.Tests;

public class EmfValidateHelper
{
/// <summary>
/// Helps immensely with debugging metafile tests.
/// </summary>
/// <param name="emf">The EmfScope instance to extract log information from.</param>
/// <param name="methodName">The name of the calling method. Use <see cref="MethodBase.GetCurrentMethod()"/> and property <see cref="MemberInfo.Name"/>.</param>
/// <param name="parameters">Optional parameters to include in the log information.</param>
internal void LogEmfValidateToFile(EmfScope emf, string methodName, params object[] parameters)
{
ArgumentNullException.ThrowIfNull(emf, nameof(emf));

string timestamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm");

StringBuilder sb = new();
sb.Append($"# {timestamp}. Parameters: ");
sb.AppendJoin(", ", parameters);
sb.AppendLine("\r\n\r\n```c");
sb.AppendLine($"{emf.RecordsToString()}```\r\n");

File.AppendAllText(@$"c:\temp\{methodName}.md", sb.ToString());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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;

namespace System.Windows.Forms.Tests;

public abstract class AbstractButtonBaseTests
{
protected abstract ButtonBase CreateButton();

protected void ButtonBase_FlatStyle_ValidFlatButtonBorder(int borderSize)
{
using var control = CreateButton();
control.FlatStyle = FlatStyle.Flat;

control.Invoking(y => y.FlatAppearance.BorderColor = Color.Transparent)
.Should().Throw<NotSupportedException>();

if (borderSize < 0)
{
control.Invoking(y => y.FlatAppearance.BorderSize = borderSize)
.Should().Throw<ArgumentOutOfRangeException>();
}
else
{
control.FlatAppearance.BorderSize = borderSize;
control.FlatAppearance.BorderSize.Should().Be(borderSize);
}
}

protected void ButtonBase_FlatStyle_ProperFlatButtonColor(int red, int green, int blue)
{
Color expectedColor = Color.FromArgb(red, green, blue);

using var control = CreateButton();
control.FlatStyle = FlatStyle.Flat;
control.BackColor = expectedColor;

control.FlatAppearance.CheckedBackColor = expectedColor;
control.FlatAppearance.BorderColor = expectedColor;

control.BackColor.Should().Be(expectedColor);
control.FlatAppearance.BorderColor.Should().Be(expectedColor);
control.FlatAppearance.CheckedBackColor.Should().Be(expectedColor);
}

protected virtual void ButtonBase_OverChangeRectangle_Get(Appearance appearance, FlatStyle flatStyle)
{
using dynamic control = CreateButton();

if (control is null)
{
return;
}

control.Appearance = appearance;
control.FlatStyle = flatStyle;

Rectangle overChangeRectangle;

// ButtonBase.Adapter prohibits this
if (appearance == Appearance.Normal
&& (flatStyle != FlatStyle.Standard
&& flatStyle != FlatStyle.Popup
&& flatStyle != FlatStyle.Flat))
{
// Compiler requires casting lambda expression to delegate or expression
// before using it as a dynamically dispatched operation
Action act = () => overChangeRectangle = control.OverChangeRectangle;
act.Should().Throw<Exception>();

return;
}

overChangeRectangle = control.OverChangeRectangle;

if (control.FlatStyle == FlatStyle.Standard)
{
overChangeRectangle.Should().Be(new Rectangle(-1, -1, 1, 1));
}

if (control.Appearance == Appearance.Button)
{
if (control.FlatStyle != FlatStyle.Standard)
{
overChangeRectangle.Should().Be(control.ClientRectangle);
}
}
else if (control.FlatStyle != FlatStyle.Standard)
{
overChangeRectangle.Should().Be(control.Adapter.CommonLayout().Layout().CheckBounds);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace System.Windows.Forms.Tests;

public class ButtonBaseTests
public class ButtonBaseTests : AbstractButtonBaseTests
{
[WinFormsFact]
public void ButtonBase_Ctor_Default()
Expand Down Expand Up @@ -9335,7 +9335,7 @@ private class SubButtonBase : ButtonBase
public new bool GetStyle(ControlStyles flag) => base.GetStyle(flag);

public new bool GetTopLevel() => base.GetTopLevel();

public new void OnClick(EventArgs e) => base.OnClick(e);

public new void OnEnabledChanged(EventArgs e) => base.OnEnabledChanged(e);
Expand Down Expand Up @@ -9376,4 +9376,6 @@ private class SubButtonBase : ButtonBase

public new void WndProc(ref Message m) => base.WndProc(ref m);
}

protected override ButtonBase CreateButton() => new Button();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace System.Windows.Forms.Tests;

public class ButtonRenderingTests
public class ButtonRenderingTests : AbstractButtonBaseTests
{
[WinFormsFact]
public unsafe void CaptureButton()
{
using Button button = new();
using Button button = (Button)CreateButton();
using EmfScope emf = new();
button.PrintToMetafile(emf);

Expand All @@ -33,7 +33,7 @@ public unsafe void Button_VisualStyles_off_Default_LineDrawing()
return;
}

using Button button = new();
using Button button = (Button)CreateButton();
using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = button.Bounds;
Expand Down Expand Up @@ -77,7 +77,7 @@ public unsafe void Button_VisualStyles_on_Default_LineDrawing()
return;
}

using Button button = new();
using Button button = (Button)CreateButton();
using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = button.Bounds;
Expand Down Expand Up @@ -117,7 +117,8 @@ public unsafe void Button_VisualStyles_off_Default_WithText_LineDrawing()
return;
}

using Button button = new() { Text = "Hello" };
using Button button = (Button)CreateButton();
button.Text = "Hello";
using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = button.Bounds;
Expand Down Expand Up @@ -162,7 +163,8 @@ public unsafe void Button_VisualStyles_on_Default_WithText_LineDrawing()
return;
}

using Button button = new() { Text = "Hello" };
using Button button = (Button)CreateButton();
button.Text = "Hello";
using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = button.Bounds;
Expand Down Expand Up @@ -210,7 +212,7 @@ public unsafe void Button_VisualStyles_on_Default_WithText_LineDrawing()
public unsafe void CaptureButtonOnForm()
{
using Form form = new();
using Button button = new();
using Button button = (Button)CreateButton();
form.Controls.Add(button);

using EmfScope emf = new();
Expand All @@ -222,11 +224,9 @@ public unsafe void CaptureButtonOnForm()
[WinFormsFact]
public unsafe void Button_FlatStyle_WithText_Rectangle()
{
using Button button = new()
{
Text = "Flat Style",
FlatStyle = FlatStyle.Flat,
};
using Button button = (Button)CreateButton();
button.Text = "Flat Style";
button.FlatStyle = FlatStyle.Flat;

using EmfScope emf = new();
DeviceContextState state = new(emf);
Expand All @@ -245,4 +245,6 @@ public unsafe void Button_FlatStyle_WithText_Rectangle()
State.BrushStyle(BRUSH_STYLE.BS_NULL), // Regressed in https://github.com/dotnet/winforms/pull/3667
State.Rop2(R2_MODE.R2_COPYPEN)));
}

protected override ButtonBase CreateButton() => new Button();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace System.Windows.Forms.Tests;

public class ButtonTests
public class ButtonTests : AbstractButtonBaseTests
{
[WinFormsFact]
public void Button_Ctor_Default()
Expand Down Expand Up @@ -3578,6 +3578,17 @@ public static IEnumerable<object[]> WndProc_ReflectCommandWithoutHandle_TestData
yield return new object[] { FlatStyle.System, PARAM.FromLowHigh(123, 456), (IntPtr)250, 0 };
}

[WinFormsFact]
public void ButtonBase_Click_RaisesEvent()
{
using var button = (SubButton)CreateButton();
bool clickEventRaised = false;
button.Click += (sender, e) => clickEventRaised = true;
button.PerformClick();

clickEventRaised.Should().BeTrue();
}

[WinFormsTheory]
[MemberData(nameof(WndProc_ReflectCommandWithoutHandle_TestData))]
public void Button_WndProc_InvokeReflectCommandWithoutHandle_Success(FlatStyle flatStyle, IntPtr wParam, IntPtr expectedResult, int expectedCallCount)
Expand Down Expand Up @@ -3649,6 +3660,20 @@ public void Button_WndProc_InvokeReflectCommandWithHandle_Success(FlatStyle flat
Assert.Equal(0, createdCallCount);
}

[WinFormsTheory]
[InlineData(-1)]
[InlineData(0)]
[InlineData(1)]
public void Button_Flat_ValidBorder(int borderSize) => base.ButtonBase_FlatStyle_ValidFlatButtonBorder(borderSize);

[WinFormsTheory]
[InlineData(255, 0, 0)]
[InlineData(0, 255, 0)]
[InlineData(0, 0, 255)]
public void Button_Flat_ProperColor(int red, int green, int blue) => base.ButtonBase_FlatStyle_ProperFlatButtonColor(red, green, blue);

protected override ButtonBase CreateButton() => new SubButton();

private class SubButton : Button
{
public new bool CanEnableIme => base.CanEnableIme;
Expand Down
Loading