Skip to content
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

Add test coverage for TextBox #11021

Merged
merged 9 commits into from
Apr 19, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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;
using System.Windows.Forms.Metafiles;
using System.Windows.Forms.VisualStyles;

namespace System.Windows.Forms.Tests;

public class TextBoxRendererTests
{
public static TheoryData<TextBoxState> DrawTextBox_State_TheoryData() => new()
{
TextBoxState.Normal,
TextBoxState.Hot,
TextBoxState.Selected,
TextBoxState.Disabled
};

Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
[WinFormsTheory]
[MemberData(nameof(DrawTextBox_State_TheoryData))]
public void TextBoxRenderer_DrawTextBox(TextBoxState tBState)
{
using Form form = new();
using TextBox textbox = new();

form.Controls.Add(textbox);

form.Handle.Should().NotBe(IntPtr.Zero);
textbox.Handle.Should().NotBe(IntPtr.Zero);

using EmfScope emf = new();
DeviceContextState state = new(emf);
using Graphics graphics = Graphics.FromHdc((IntPtr)emf.HDC);

Rectangle bounds = textbox.Bounds;

TextBoxRenderer.DrawTextBox(graphics, bounds, tBState);

emf.Validate(
state,
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_EXTTEXTOUTW), 10),
tBState != TextBoxState.Disabled
? Validate.Polygon16(new(new(1, 1), new(bounds.Width - 2, bounds.Height - 2)))
: null
);
}

[WinFormsTheory]
[MemberData(nameof(DrawTextBox_State_TheoryData))]
public void TextBoxRenderer_DrawTextBox_OverloadWithTextAndFont(TextBoxState tBState)
{
using Form form = new();
using TextBox textbox = new();

Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
form.Controls.Add(textbox);

form.Handle.Should().NotBe(IntPtr.Zero);
textbox.Handle.Should().NotBe(IntPtr.Zero);

using EmfScope emf = new();
DeviceContextState state = new(emf);
using Graphics graphics = Graphics.FromHdc((IntPtr)emf.HDC);

Rectangle bounds = textbox.Bounds;

TextBoxRenderer.DrawTextBox(graphics, bounds, "text", SystemFonts.DefaultFont, tBState);

emf.Validate(
state,
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_EXTTEXTOUTW),
tBState != TextBoxState.Disabled ? 10 : 11),
tBState != TextBoxState.Disabled
? Validate.Polygon16(new(new(1, 1), new(bounds.Width - 2, bounds.Height - 2)))
: null,
Validate.TextOut(
"text",
bounds: new Rectangle(6, 3, 16, 12),
State.FontFace(SystemFonts.DefaultFont.Name)
)
);
}

[WinFormsTheory]
[MemberData(nameof(DrawTextBox_State_TheoryData))]
public void TextBoxRenderer_DrawTextBox_OverloadWithTextFontAndTextBounds(TextBoxState tBState)
{
using Form form = new();
using TextBox textbox = new();

Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
form.Controls.Add(textbox);

form.Handle.Should().NotBe(IntPtr.Zero);
textbox.Handle.Should().NotBe(IntPtr.Zero);

using EmfScope emf = new();
DeviceContextState state = new(emf);
using Graphics graphics = Graphics.FromHdc((IntPtr)emf.HDC);

Rectangle bounds = textbox.Bounds;
Rectangle textBounds = new(10, 20, 30, 40);

TextBoxRenderer.DrawTextBox(graphics, bounds, "text", SystemFonts.DefaultFont, textBounds, tBState);

emf.Validate(
state,
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_EXTTEXTOUTW),
tBState != TextBoxState.Disabled ? 10 : 11),
tBState != TextBoxState.Disabled
? Validate.Polygon16(new(new(1, 1), new(bounds.Width - 2, bounds.Height - 2)))
: null,
Validate.TextOut(
"text",
bounds: new Rectangle(13, 20, 16, 12),
State.FontFace(SystemFonts.DefaultFont.Name)
));
}
}
35 changes: 35 additions & 0 deletions src/System.Windows.Forms/tests/UnitTests/TextBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,41 @@ public void TextBox_ImeModeBase_GetWithHandle_ReturnsExpected()
Assert.Equal(0, createdCallCount);
}

[WinFormsTheory]
[InvalidEnumData<AutoCompleteMode>]
public void TextBox_AutoCompleteMode_SetInvalidValue_ThrowsInvalidEnumArgumentException(AutoCompleteMode value)
{
using SubTextBox control = new();

Action act = () => control.AutoCompleteMode = value;
act.Should().Throw<InvalidEnumArgumentException>().And.ParamName.Should().Be("value");
}

[WinFormsTheory]
[EnumData<AutoCompleteSource>]
public void TextBox_AutoCompleteSource_Set_GetReturnsExpected(AutoCompleteSource value)
{
using SubTextBox control = new()
{
AutoCompleteSource = value == AutoCompleteSource.ListItems ? AutoCompleteSource.None : value
};

if (value != AutoCompleteSource.ListItems)
{
control.AutoCompleteSource.Should().Be(value);
}
}

[WinFormsTheory]
[InvalidEnumData<AutoCompleteSource>]
public void TextBox_AutoCompleteSource_InvalidAutoCompleteSource_ThrowsInvalidEnumArgumentException(AutoCompleteSource source)
{
using SubTextBox control = new();

Action act = () => control.AutoCompleteSource = source;
act.Should().Throw<InvalidEnumArgumentException>().And.ParamName.Should().Be("value");
}

[WinFormsFact]
public void TextBox_PasswordChar_GetWithoutHandle_ReturnsExpected()
{
Expand Down