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,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;
using System.Windows.Forms.VisualStyles;

namespace System.Windows.Forms.Tests;

public class TextBoxRendererTests
{
[WinFormsTheory]
[InlineData(TextBoxState.Normal)]
[InlineData(TextBoxState.Hot)]
[InlineData(TextBoxState.Selected)]
[InlineData(TextBoxState.Disabled)]
public void TextBoxRenderer_DrawTextBox(TextBoxState state)
{
using Bitmap bitmap = new(100, 100);
using Graphics graphics = Graphics.FromImage(bitmap);
Rectangle bounds = new Rectangle(10, 20, 30, 40);
Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved

TextBoxRenderer.DrawTextBox(graphics, bounds, state);
Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
}

Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
[WinFormsTheory]
[InlineData(TextBoxState.Normal)]
[InlineData(TextBoxState.Hot)]
[InlineData(TextBoxState.Selected)]
[InlineData(TextBoxState.Disabled)]
public void TextBoxRenderer_DrawTextBox_OverloadWithTextAndFont(TextBoxState state)
{
using Bitmap bitmap = new(100, 100);
using Graphics graphics = Graphics.FromImage(bitmap);
Rectangle bounds = new Rectangle(10, 20, 30, 40);

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

[WinFormsTheory]
[InlineData(TextBoxState.Normal)]
[InlineData(TextBoxState.Hot)]
[InlineData(TextBoxState.Selected)]
[InlineData(TextBoxState.Disabled)]
Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
public void TextBoxRenderer_DrawTextBox_OverloadWithTextFontAndTextBounds(TextBoxState state)
{
using Bitmap bitmap = new(100, 100);
using Graphics graphics = Graphics.FromImage(bitmap);
Rectangle bounds = new Rectangle(10, 20, 30, 40);
Rectangle textBounds = new Rectangle(10, 20, 30, 40);

TextBoxRenderer.DrawTextBox(graphics, bounds, "text", SystemFonts.DefaultFont, textBounds, state);
Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
}

public static IEnumerable<object[]> DrawTextBox_FlagsAndState_TestData()
{
yield return new object[] { TextBoxState.Normal, TextFormatFlags.Default };
yield return new object[] { TextBoxState.Normal, TextFormatFlags.TextBoxControl };
yield return new object[] { TextBoxState.Normal, TextFormatFlags.PreserveGraphicsTranslateTransform };

yield return new object[] { TextBoxState.Hot, TextFormatFlags.Default };
yield return new object[] { TextBoxState.Hot, TextFormatFlags.TextBoxControl };
yield return new object[] { TextBoxState.Hot, TextFormatFlags.PreserveGraphicsTranslateTransform };

yield return new object[] { TextBoxState.Disabled, TextFormatFlags.Default };
yield return new object[] { TextBoxState.Disabled, TextFormatFlags.TextBoxControl };
yield return new object[] { TextBoxState.Disabled, TextFormatFlags.PreserveGraphicsTranslateTransform };

yield return new object[] { TextBoxState.Selected, TextFormatFlags.Default };
yield return new object[] { TextBoxState.Selected, TextFormatFlags.TextBoxControl };
yield return new object[] { TextBoxState.Selected, TextFormatFlags.PreserveGraphicsTranslateTransform };
}

[WinFormsTheory]
[MemberData(nameof(DrawTextBox_FlagsAndState_TestData))]
public void TextBoxRenderer_DrawTextBox_OverloadWithTextFontAndFlags(TextFormatFlags flag, TextBoxState state)
{
using Bitmap bitmap = new(100, 100);
using Graphics graphics = Graphics.FromImage(bitmap);
Rectangle bounds = new Rectangle(10, 20, 30, 40);

TextBoxRenderer.DrawTextBox(graphics, bounds, "text", SystemFonts.DefaultFont, flag, state);
}

[WinFormsTheory]
[MemberData(nameof(DrawTextBox_FlagsAndState_TestData))]
public void TextBoxRenderer_DrawTextBox_OverloadWithTextFontTextboundsAndFlags(TextFormatFlags flag, TextBoxState state)
{
using Bitmap bitmap = new(100, 100);
using Graphics graphics = Graphics.FromImage(bitmap);
Rectangle bounds = new Rectangle(10, 20, 30, 40);
Rectangle textBounds = new Rectangle(10, 20, 30, 40);

TextBoxRenderer.DrawTextBox(graphics, bounds, "text", SystemFonts.DefaultFont, textBounds, flag, state);
}
}
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)
Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
{
control.AutoCompleteSource.Should().Be(value);
}
Eudora-Li01 marked this conversation as resolved.
Show resolved Hide resolved
}

[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