Skip to content

Commit

Permalink
Add unit tests for MaskedTextBoxDesigner (dotnet#12779)
Browse files Browse the repository at this point in the history
* Add unit tests for MaskedTextBoxDesigner
Related dotnet#10773
-

Co-authored-by: v-norazhou <v-norazhou@win1122h21226>
  • Loading branch information
Nora-Zhou01 and v-norazhou authored Jan 24, 2025
1 parent df75fe5 commit 5fc66aa
Showing 1 changed file with 112 additions and 17 deletions.
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
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>();
}
}

0 comments on commit 5fc66aa

Please sign in to comment.