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 unit tests for MaskedTextBoxDesigner #12779

Merged
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
@@ -1,43 +1,138 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using Moq;

namespace System.Windows.Forms.Design.Tests;

public sealed class MaskedTextBoxDesignerTests
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved
public sealed class MaskedTextBoxDesignerTests : IDisposable
{
[Fact]
public void SnapLines_WithDefaultMaskedTextBox_ShouldReturnExpectedCount()
private readonly MaskedTextBox _maskedTextBox = new();
private readonly MaskedTextBoxDesigner _maskedTextBoxDesigner = new();

public MaskedTextBoxDesignerTests()
{
using MaskedTextBoxDesigner maskedTextBoxDesigner = new();
using MaskedTextBox maskedTextBox = new();
maskedTextBoxDesigner.Initialize(maskedTextBox);
_maskedTextBoxDesigner.Initialize(_maskedTextBox);
}

maskedTextBoxDesigner.SnapLines.Count.Should().Be(9);
public void Dispose()
{
_maskedTextBox.Dispose();
_maskedTextBoxDesigner.Dispose();
}

[Fact]
public void SnapLines_WithDefaultMaskedTextBox_ShouldReturnExpectedCount() => _maskedTextBoxDesigner.SnapLines.Count.Should().Be(9);

[Fact]
public void SelectionRules_WithDefaultMaskedTextBox_ShouldReturnExpectedValue()
{
using MaskedTextBoxDesigner maskedTextBoxDesigner = new();
using MaskedTextBox maskedTextBox = new();
maskedTextBoxDesigner.Initialize(maskedTextBox);

SelectionRules selectionRules;
using (new NoAssertContext())
{
selectionRules = maskedTextBoxDesigner.SelectionRules;
selectionRules = _maskedTextBoxDesigner.SelectionRules;
}

selectionRules.Should().Be(SelectionRules.LeftSizeable | SelectionRules.RightSizeable | SelectionRules.Moveable | SelectionRules.Visible);
}

[Fact]
public void Verbs_WithDefaultMaskedTextBox_ShouldReturnExpectedCount()
public void Verbs_WithDefaultMaskedTextBox_ShouldReturnExpectedCount() => _maskedTextBoxDesigner.Verbs.Count.Should().Be(1);

[Fact]
public void Verbs_WithDefaultMaskedTextBox_ShouldContainSetMaskVerb()
{
using MaskedTextBoxDesigner maskedTextBoxDesigner = new();
using MaskedTextBox maskedTextBox = new();
maskedTextBoxDesigner.Initialize(maskedTextBox);
DesignerVerbCollection verbs = _maskedTextBoxDesigner.Verbs;

verbs.Should().NotBeNull();
verbs.Count.Should().BeGreaterThan(0);
DesignerVerb? firstVerb = verbs.Count > 0 ? verbs[0] : null;
firstVerb.Should().NotBeNull();
firstVerb!.Text.Should().Be(SR.MaskedTextBoxDesignerVerbsSetMaskDesc);
}

[Fact]
public void GetDesignMaskedTextBox_WithNullInput_ShouldReturnDefaultMaskedTextBox()
{
using MaskedTextBox designMaskedTextBox = MaskedTextBoxDesigner.GetDesignMaskedTextBox(null!);
designMaskedTextBox.Should().NotBeNull();
designMaskedTextBox.Mask.Should().BeEmpty();
designMaskedTextBox.Text.Should().BeEmpty();
}

[Fact]
public void GetDesignMaskedTextBox_WithMaskedTextBox_ShouldCloneProperties()
{
using MaskedTextBox maskedTextBox = new()
{
Text = "123",
ValidatingType = typeof(int),
BeepOnError = true,
InsertKeyMode = InsertKeyMode.Overwrite,
RejectInputOnFirstFailure = true,
CutCopyMaskFormat = MaskFormat.IncludePrompt,
Culture = new CultureInfo("en-US")
};

using MaskedTextBox designMaskedTextBox = MaskedTextBoxDesigner.GetDesignMaskedTextBox(maskedTextBox);
designMaskedTextBox.Should().NotBeNull();
designMaskedTextBox.Text.Should().Be("123");
designMaskedTextBox.ValidatingType.Should().Be(typeof(int));
designMaskedTextBox.BeepOnError.Should().BeTrue();
designMaskedTextBox.InsertKeyMode.Should().Be(InsertKeyMode.Overwrite);
designMaskedTextBox.RejectInputOnFirstFailure.Should().BeTrue();
designMaskedTextBox.CutCopyMaskFormat.Should().Be(MaskFormat.IncludePrompt);
designMaskedTextBox.Culture.Should().Be(new CultureInfo("en-US"));
}

[Fact]
public void ActionLists_WithDefaultMaskedTextBox_ShouldReturnExpectedCount()
{
IServiceContainer serviceContainer = new Mock<IServiceContainer>().Object;
ITypeDiscoveryService typeDiscoveryService = new Mock<ITypeDiscoveryService>().Object;
IUIService uiService = new Mock<IUIService>().Object;
ISite mockSite = new Mock<ISite>().Object;

Mock<IServiceContainer> mockServiceContainer = new();
mockServiceContainer.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
mockServiceContainer.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);

Mock<ISite> mockMockSite = new();
mockMockSite.Setup(s => s.GetService(typeof(IServiceContainer))).Returns(mockServiceContainer.Object);
mockMockSite.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
mockMockSite.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);

_maskedTextBoxDesigner.Component.Site = mockMockSite.Object;

DesignerActionListCollection actionLists = _maskedTextBoxDesigner.ActionLists;
actionLists.Count.Should().Be(1);
}

[Fact]
public void ActionLists_WithDefaultMaskedTextBox_ShouldReturnExpectedType()
{
IServiceContainer serviceContainer = new Mock<IServiceContainer>().Object;
ITypeDiscoveryService typeDiscoveryService = new Mock<ITypeDiscoveryService>().Object;
IUIService uiService = new Mock<IUIService>().Object;
ISite mockSite = new Mock<ISite>().Object;

Mock<IServiceContainer> mockServiceContainer = new();
mockServiceContainer.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
mockServiceContainer.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);

Mock<ISite> mockMockSite = new();
mockMockSite.Setup(s => s.GetService(typeof(IServiceContainer))).Returns(mockServiceContainer.Object);
mockMockSite.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
mockMockSite.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);

_maskedTextBoxDesigner.Component.Site = mockMockSite.Object;

maskedTextBoxDesigner.Verbs.Count.Should().Be(1);
DesignerActionListCollection actionLists = _maskedTextBoxDesigner.ActionLists;
actionLists[0].Should().BeOfType<MaskedTextBoxDesignerActionList>();
}
}