diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxDesignerTests.cs index 9e0b26a8987..8925716b5f1 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxDesignerTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxDesignerTests.cs @@ -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().Object; + ITypeDiscoveryService typeDiscoveryService = new Mock().Object; + IUIService uiService = new Mock().Object; + ISite mockSite = new Mock().Object; + + Mock mockServiceContainer = new(); + mockServiceContainer.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService); + mockServiceContainer.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService); + + Mock 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().Object; + ITypeDiscoveryService typeDiscoveryService = new Mock().Object; + IUIService uiService = new Mock().Object; + ISite mockSite = new Mock().Object; + + Mock mockServiceContainer = new(); + mockServiceContainer.Setup(s => s.GetService(typeof(ITypeDiscoveryService))).Returns(typeDiscoveryService); + mockServiceContainer.Setup(s => s.GetService(typeof(IUIService))).Returns(uiService); + + Mock 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(); } }