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 MaskedTextBox #11376

Merged
merged 5 commits into from
May 28, 2024
Merged
Changes from 2 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
155 changes: 154 additions & 1 deletion src/System.Windows.Forms/tests/UnitTests/MaskedTextBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Reflection;

namespace System.Windows.Forms.Tests;

public class MaskedTextBoxTests
public class MaskedTextBoxTests : IDisposable
Tanya-Solyanik marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly MaskedTextBox _maskedTextBox;

public MaskedTextBoxTests()
{
_maskedTextBox = new();
}

public void Dispose()
{
_maskedTextBox.Dispose();
}

[WinFormsFact]
public void MaskedTextBox_Constructor()
{
Expand All @@ -28,6 +42,16 @@ public void MaskedTextBox_Constructor()
mtb.ValidatingType.Should().BeNull();
mtb.TextAlign.Should().Be(HorizontalAlignment.Left);
mtb.FormatProvider.Should().Be(null);
mtb.ResetOnPrompt.Should().BeTrue();
MelonWang1 marked this conversation as resolved.
Show resolved Hide resolved
mtb.ValidatingType.Should().BeNull();
mtb.PromptChar.Should().Be('_');
mtb.GetLineFromCharIndex(2).Should().Be(0);
mtb.GetFirstCharIndexFromLine(100).Should().Be(0);
mtb.GetFirstCharIndexOfCurrentLine().Should().Be(0);
mtb.PasswordChar.Should().Be('\0');
mtb.RejectInputOnFirstFailure.Should().BeFalse();
mtb.Text.Should().Be(string.Empty);
mtb.UseSystemPasswordChar.Should().BeFalse();

mtb.IsHandleCreated.Should().BeFalse();
}
Expand Down Expand Up @@ -670,4 +694,133 @@ public void MaskedTextBox_TextAlignChangedEvent_AddRemove_Success()
control.TextAlign = HorizontalAlignment.Right;
callCount.Should().Be(1);
}

[WinFormsTheory]
[BoolData]
public void MaskedTextBox_BooleanProperty_GetSet_ReturnsExpected(bool value)
{
TestProperty(_maskedTextBox, nameof(MaskedTextBox.RejectInputOnFirstFailure), value);
TestProperty(_maskedTextBox, nameof(MaskedTextBox.ResetOnPrompt), value);
}

private void TestProperty(MaskedTextBox maskedTextBox, string propertyName, bool value)
{
PropertyInfo property = typeof(MaskedTextBox).GetProperty(propertyName);

property.SetValue(maskedTextBox, value);
property.GetValue(maskedTextBox).Should().Be(value);
maskedTextBox.IsHandleCreated.Should().BeFalse();

property.SetValue(maskedTextBox, !value);
property.GetValue(maskedTextBox).Should().Be(!value);
maskedTextBox.IsHandleCreated.Should().BeFalse();
}

[WinFormsFact]
public void MaskedTextBox_GetCharAndPosition_ReturnsExpected()
{
_maskedTextBox.Text = "Hello, World!";
_maskedTextBox.CreateControl();

// Test GetPositionFromCharIndex
Point position = _maskedTextBox.GetPositionFromCharIndex(4);
position.Should().NotBe(Point.Empty);

// Test GetCharFromPosition
char resultChar = _maskedTextBox.GetCharFromPosition(position);
resultChar.Should().Be('o');

// Test GetCharIndexFromPosition
int resultIndex = _maskedTextBox.GetCharIndexFromPosition(position);
resultIndex.Should().Be(4);
}

[WinFormsTheory]
[InlineData(typeof(int), "12345")]
[InlineData(typeof(string), "Hello")]
public void MaskedTextBox_ValidatingTypeAndValidateText_ReturnsExpected(Type validatingType, string text)
{
int callCount = 0;
TypeValidationEventHandler handler = (sender, e) =>
{
sender.Should().Be(_maskedTextBox);
e.Should().NotBeNull();
callCount++;
};

_maskedTextBox.ValidatingType = validatingType;
_maskedTextBox.ValidatingType.Should().Be(validatingType);
_maskedTextBox.IsHandleCreated.Should().BeFalse();

_maskedTextBox.Text = text;
_maskedTextBox.CreateControl();

_maskedTextBox.TypeValidationCompleted += handler;
object result = _maskedTextBox.ValidateText();
result.Should().NotBeNull();
callCount.Should().Be(1);

_maskedTextBox.TypeValidationCompleted -= handler;
_maskedTextBox.ValidateText();
callCount.Should().Be(1);
}

[WinFormsTheory]
[InlineData('A', 'B', true)]
[InlineData('1', '2', true)]
[InlineData('%', '&', true)]
[InlineData('A', 'B', false)]
[InlineData('1', '2', false)]
[InlineData('%', '&', false)]
public void MaskedTextBox_CharProperties_Set_GetReturnsExpected(char originalValue, char value, bool isPasswordChar)
{
if (isPasswordChar)
{
_maskedTextBox.PasswordChar = originalValue;
_maskedTextBox.PasswordChar.Should().Be(originalValue);

_maskedTextBox.PasswordChar = value;
_maskedTextBox.PasswordChar.Should().Be(value);
}
else
{
_maskedTextBox.PromptChar = originalValue;
_maskedTextBox.PromptChar.Should().Be(originalValue);

_maskedTextBox.PromptChar = value;
_maskedTextBox.PromptChar.Should().Be(value);
}
}

[WinFormsFact]
public void MaskedTextBox_PromptCharAndPasswordChar_SetInvalid_ThrowsArgumentException()
{
Action act = () => _maskedTextBox.PromptChar = '\0';
act.Should().Throw<ArgumentException>();

Action act2 = () => _maskedTextBox.PasswordChar = '\t';
act2.Should().Throw<ArgumentException>();
}

[WinFormsFact]
MelonWang1 marked this conversation as resolved.
Show resolved Hide resolved
public void MaskedTextBox_PasswordChar_SetSameAsPromptChar_ThrowsInvalidOperationException()
{
_maskedTextBox.PromptChar = 'A';
Action act = () => _maskedTextBox.PasswordChar = 'A';
act.Should().Throw<InvalidOperationException>();
}

[WinFormsFact]
public void MaskedTextBox_PasswordChar_SetWithUseSystemPasswordChar_UpdatesMaskedTextProvider()
{
_maskedTextBox.Mask = "00000";
_maskedTextBox.UseSystemPasswordChar = true;
_maskedTextBox.PasswordChar = 'A';

if (_maskedTextBox.MaskedTextProvider is not null)
{
_maskedTextBox.PasswordChar.Should().Be('●');
_maskedTextBox.MaskedTextProvider.PasswordChar.Should().Be('●');
}
}
}
Loading