|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +#nullable enable |
| 5 | + |
| 6 | +using System.ComponentModel; |
| 7 | +using System.ComponentModel.Design; |
| 8 | +using System.Globalization; |
| 9 | +using Moq; |
| 10 | + |
4 | 11 | namespace System.Windows.Forms.Design.Tests;
|
5 | 12 |
|
6 |
| -public sealed class MaskedTextBoxDesignerTests |
| 13 | +public sealed class MaskedTextBoxDesignerTests : IDisposable |
7 | 14 | {
|
8 |
| - [Fact] |
9 |
| - public void SnapLines_WithDefaultMaskedTextBox_ShouldReturnExpectedCount() |
| 15 | + private readonly MaskedTextBox _maskedTextBox = new(); |
| 16 | + private readonly MaskedTextBoxDesigner _maskedTextBoxDesigner = new(); |
| 17 | + |
| 18 | + public MaskedTextBoxDesignerTests() |
10 | 19 | {
|
11 |
| - using MaskedTextBoxDesigner maskedTextBoxDesigner = new(); |
12 |
| - using MaskedTextBox maskedTextBox = new(); |
13 |
| - maskedTextBoxDesigner.Initialize(maskedTextBox); |
| 20 | + _maskedTextBoxDesigner.Initialize(_maskedTextBox); |
| 21 | + } |
14 | 22 |
|
15 |
| - maskedTextBoxDesigner.SnapLines.Count.Should().Be(9); |
| 23 | + public void Dispose() |
| 24 | + { |
| 25 | + _maskedTextBox.Dispose(); |
| 26 | + _maskedTextBoxDesigner.Dispose(); |
16 | 27 | }
|
17 | 28 |
|
| 29 | + [Fact] |
| 30 | + public void SnapLines_WithDefaultMaskedTextBox_ShouldReturnExpectedCount() => _maskedTextBoxDesigner.SnapLines.Count.Should().Be(9); |
| 31 | + |
18 | 32 | [Fact]
|
19 | 33 | public void SelectionRules_WithDefaultMaskedTextBox_ShouldReturnExpectedValue()
|
20 | 34 | {
|
21 |
| - using MaskedTextBoxDesigner maskedTextBoxDesigner = new(); |
22 |
| - using MaskedTextBox maskedTextBox = new(); |
23 |
| - maskedTextBoxDesigner.Initialize(maskedTextBox); |
24 |
| - |
25 | 35 | SelectionRules selectionRules;
|
26 | 36 | using (new NoAssertContext())
|
27 | 37 | {
|
28 |
| - selectionRules = maskedTextBoxDesigner.SelectionRules; |
| 38 | + selectionRules = _maskedTextBoxDesigner.SelectionRules; |
29 | 39 | }
|
30 | 40 |
|
31 | 41 | selectionRules.Should().Be(SelectionRules.LeftSizeable | SelectionRules.RightSizeable | SelectionRules.Moveable | SelectionRules.Visible);
|
32 | 42 | }
|
33 | 43 |
|
34 | 44 | [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() |
36 | 49 | {
|
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; |
40 | 134 |
|
41 |
| - maskedTextBoxDesigner.Verbs.Count.Should().Be(1); |
| 135 | + DesignerActionListCollection actionLists = _maskedTextBoxDesigner.ActionLists; |
| 136 | + actionLists[0].Should().BeOfType<MaskedTextBoxDesignerActionList>(); |
42 | 137 | }
|
43 | 138 | }
|
0 commit comments