Skip to content

Commit 5fc66aa

Browse files
Nora-Zhou01v-norazhou
and
v-norazhou
authored
Add unit tests for MaskedTextBoxDesigner (#12779)
* Add unit tests for MaskedTextBoxDesigner Related #10773 - Co-authored-by: v-norazhou <v-norazhou@win1122h21226>
1 parent df75fe5 commit 5fc66aa

File tree

1 file changed

+112
-17
lines changed

1 file changed

+112
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,138 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#nullable enable
5+
6+
using System.ComponentModel;
7+
using System.ComponentModel.Design;
8+
using System.Globalization;
9+
using Moq;
10+
411
namespace System.Windows.Forms.Design.Tests;
512

6-
public sealed class MaskedTextBoxDesignerTests
13+
public sealed class MaskedTextBoxDesignerTests : IDisposable
714
{
8-
[Fact]
9-
public void SnapLines_WithDefaultMaskedTextBox_ShouldReturnExpectedCount()
15+
private readonly MaskedTextBox _maskedTextBox = new();
16+
private readonly MaskedTextBoxDesigner _maskedTextBoxDesigner = new();
17+
18+
public MaskedTextBoxDesignerTests()
1019
{
11-
using MaskedTextBoxDesigner maskedTextBoxDesigner = new();
12-
using MaskedTextBox maskedTextBox = new();
13-
maskedTextBoxDesigner.Initialize(maskedTextBox);
20+
_maskedTextBoxDesigner.Initialize(_maskedTextBox);
21+
}
1422

15-
maskedTextBoxDesigner.SnapLines.Count.Should().Be(9);
23+
public void Dispose()
24+
{
25+
_maskedTextBox.Dispose();
26+
_maskedTextBoxDesigner.Dispose();
1627
}
1728

29+
[Fact]
30+
public void SnapLines_WithDefaultMaskedTextBox_ShouldReturnExpectedCount() => _maskedTextBoxDesigner.SnapLines.Count.Should().Be(9);
31+
1832
[Fact]
1933
public void SelectionRules_WithDefaultMaskedTextBox_ShouldReturnExpectedValue()
2034
{
21-
using MaskedTextBoxDesigner maskedTextBoxDesigner = new();
22-
using MaskedTextBox maskedTextBox = new();
23-
maskedTextBoxDesigner.Initialize(maskedTextBox);
24-
2535
SelectionRules selectionRules;
2636
using (new NoAssertContext())
2737
{
28-
selectionRules = maskedTextBoxDesigner.SelectionRules;
38+
selectionRules = _maskedTextBoxDesigner.SelectionRules;
2939
}
3040

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

3444
[Fact]
35-
public void Verbs_WithDefaultMaskedTextBox_ShouldReturnExpectedCount()
45+
public void Verbs_WithDefaultMaskedTextBox_ShouldReturnExpectedCount() => _maskedTextBoxDesigner.Verbs.Count.Should().Be(1);
46+
47+
[Fact]
48+
public void Verbs_WithDefaultMaskedTextBox_ShouldContainSetMaskVerb()
3649
{
37-
using MaskedTextBoxDesigner maskedTextBoxDesigner = new();
38-
using MaskedTextBox maskedTextBox = new();
39-
maskedTextBoxDesigner.Initialize(maskedTextBox);
50+
DesignerVerbCollection verbs = _maskedTextBoxDesigner.Verbs;
51+
52+
verbs.Should().NotBeNull();
53+
verbs.Count.Should().BeGreaterThan(0);
54+
DesignerVerb? firstVerb = verbs.Count > 0 ? verbs[0] : null;
55+
firstVerb.Should().NotBeNull();
56+
firstVerb!.Text.Should().Be(SR.MaskedTextBoxDesignerVerbsSetMaskDesc);
57+
}
58+
59+
[Fact]
60+
public void GetDesignMaskedTextBox_WithNullInput_ShouldReturnDefaultMaskedTextBox()
61+
{
62+
using MaskedTextBox designMaskedTextBox = MaskedTextBoxDesigner.GetDesignMaskedTextBox(null!);
63+
designMaskedTextBox.Should().NotBeNull();
64+
designMaskedTextBox.Mask.Should().BeEmpty();
65+
designMaskedTextBox.Text.Should().BeEmpty();
66+
}
67+
68+
[Fact]
69+
public void GetDesignMaskedTextBox_WithMaskedTextBox_ShouldCloneProperties()
70+
{
71+
using MaskedTextBox maskedTextBox = new()
72+
{
73+
Text = "123",
74+
ValidatingType = typeof(int),
75+
BeepOnError = true,
76+
InsertKeyMode = InsertKeyMode.Overwrite,
77+
RejectInputOnFirstFailure = true,
78+
CutCopyMaskFormat = MaskFormat.IncludePrompt,
79+
Culture = new CultureInfo("en-US")
80+
};
81+
82+
using MaskedTextBox designMaskedTextBox = MaskedTextBoxDesigner.GetDesignMaskedTextBox(maskedTextBox);
83+
designMaskedTextBox.Should().NotBeNull();
84+
designMaskedTextBox.Text.Should().Be("123");
85+
designMaskedTextBox.ValidatingType.Should().Be(typeof(int));
86+
designMaskedTextBox.BeepOnError.Should().BeTrue();
87+
designMaskedTextBox.InsertKeyMode.Should().Be(InsertKeyMode.Overwrite);
88+
designMaskedTextBox.RejectInputOnFirstFailure.Should().BeTrue();
89+
designMaskedTextBox.CutCopyMaskFormat.Should().Be(MaskFormat.IncludePrompt);
90+
designMaskedTextBox.Culture.Should().Be(new CultureInfo("en-US"));
91+
}
92+
93+
[Fact]
94+
public void ActionLists_WithDefaultMaskedTextBox_ShouldReturnExpectedCount()
95+
{
96+
IServiceContainer serviceContainer = new Mock<IServiceContainer>().Object;
97+
ITypeDiscoveryService typeDiscoveryService = new Mock<ITypeDiscoveryService>().Object;
98+
IUIService uiService = new Mock<IUIService>().Object;
99+
ISite mockSite = new Mock<ISite>().Object;
100+
101+
Mock<IServiceContainer> mockServiceContainer = new();
102+
mockServiceContainer.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
103+
mockServiceContainer.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);
104+
105+
Mock<ISite> mockMockSite = new();
106+
mockMockSite.Setup(s => s.GetService(typeof(IServiceContainer))).Returns(mockServiceContainer.Object);
107+
mockMockSite.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
108+
mockMockSite.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);
109+
110+
_maskedTextBoxDesigner.Component.Site = mockMockSite.Object;
111+
112+
DesignerActionListCollection actionLists = _maskedTextBoxDesigner.ActionLists;
113+
actionLists.Count.Should().Be(1);
114+
}
115+
116+
[Fact]
117+
public void ActionLists_WithDefaultMaskedTextBox_ShouldReturnExpectedType()
118+
{
119+
IServiceContainer serviceContainer = new Mock<IServiceContainer>().Object;
120+
ITypeDiscoveryService typeDiscoveryService = new Mock<ITypeDiscoveryService>().Object;
121+
IUIService uiService = new Mock<IUIService>().Object;
122+
ISite mockSite = new Mock<ISite>().Object;
123+
124+
Mock<IServiceContainer> mockServiceContainer = new();
125+
mockServiceContainer.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
126+
mockServiceContainer.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);
127+
128+
Mock<ISite> mockMockSite = new();
129+
mockMockSite.Setup(s => s.GetService(typeof(IServiceContainer))).Returns(mockServiceContainer.Object);
130+
mockMockSite.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService);
131+
mockMockSite.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService);
132+
133+
_maskedTextBoxDesigner.Component.Site = mockMockSite.Object;
40134

41-
maskedTextBoxDesigner.Verbs.Count.Should().Be(1);
135+
DesignerActionListCollection actionLists = _maskedTextBoxDesigner.ActionLists;
136+
actionLists[0].Should().BeOfType<MaskedTextBoxDesignerActionList>();
42137
}
43138
}

0 commit comments

Comments
 (0)