From ab588d3b989dd8b0c7a4b5ce9f66d5a1b220ee2f Mon Sep 17 00:00:00 2001 From: Leaf Shi <132890443+LeafShi1@users.noreply.github.com> Date: Wed, 24 Jul 2024 05:33:52 +0800 Subject: [PATCH] Add tests for MaskedTextBoxTextEditor (#11737) Add tests for MaskedTextBoxTextEditor.cs --- .../Design/MaskedTextBoxTextEditorTests.cs | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorTests.cs diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorTests.cs new file mode 100644 index 00000000000..1708dd11667 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorTests.cs @@ -0,0 +1,215 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; +using System.Drawing.Design; +using Moq; + +#nullable enable + +namespace System.Windows.Forms.Design.Tests; + +public class MaskedTextBoxTextEditorTests +{ + [Fact] + public void EditValue_ReturnsOriginalValue_WhenContextInstanceIsNull() + { + Mock mockContext = new(MockBehavior.Strict); + mockContext + .Setup(c => c.Instance) + .Returns(null); + + Mock mockEditorService = new(MockBehavior.Strict); + mockEditorService + .Setup(s => s.ShowDialog(It.IsAny
())) + .Returns(DialogResult.OK); + + Mock mockServiceProvider = new(MockBehavior.Strict); + mockServiceProvider + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) + .Returns(mockEditorService.Object); + + MaskedTextBoxTextEditor editor = new(); + object? value = "TestValue"; + + value.Should().BeSameAs(editor.EditValue(mockContext.Object, mockServiceProvider.Object, value)); + } + + [WinFormsFact] + public void EditValue_ReturnsOriginalValue_WhenEditorServiceIsNotAvailable() + { + Mock mockContext = new(MockBehavior.Strict); + mockContext + .Setup(c => c.Instance) + .Returns(new object()); + + IWindowsFormsEditorService? editorService = null; + + Mock mockServiceProvider = new(MockBehavior.Strict); + mockServiceProvider + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) + .Returns(editorService); + + MaskedTextBoxTextEditor editor = new(); + object? value = "TestValue"; + + value.Should().BeSameAs(editor.EditValue(mockContext.Object, mockServiceProvider.Object, value)); + } + + [Fact] + public void EditValue_ReturnsOriginalValue_WhenContextInstanceIsNotMaskedTextBox() + { + // Set context.Instance to new object. + Mock mockContext = new(MockBehavior.Strict); + mockContext + .Setup(c => c.Instance) + .Returns(new object()); + + Mock mockEditorService = new(MockBehavior.Strict); + mockEditorService + .Setup(s => s.DropDownControl(It.IsAny())); + + Mock mockServiceProvider = new(MockBehavior.Strict); + mockServiceProvider + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) + .Returns(mockEditorService.Object); + + MaskedTextBoxTextEditor editor = new(); + string originalValue = "TestValue"; + object? actualValue = editor.EditValue(mockContext.Object, mockServiceProvider.Object, originalValue); + + // When Context.Instance is not MaskedTextBox, its Text property should be the original value, + // and the return value of EditValue is also the original value. + mockContext.Object.Instance.Should().NotBeOfType(); + MaskedTextBox? maskedText = mockContext.Object.Instance as MaskedTextBox; + + maskedText?.Text.Should().Be(originalValue); + actualValue.Should().Be(originalValue); + } + + [Fact] + public void EditValue_ReturnsDropDownValue_WhenContextInstanceIsMaskedTextBox_AndDropDownValueIsNotNull() + { + MaskedTextBox CustomMaskedTextBox = new() + { + Text = "MaskText" + }; + + // Set context.Instance to a new MaskedTextBox with Text. + Mock mockContext = new(MockBehavior.Strict); + mockContext + .Setup(c => c.Instance) + .Returns(CustomMaskedTextBox); + + Mock mockEditorService = new(MockBehavior.Strict); + mockEditorService + .Setup(s => s.DropDownControl(It.IsAny())); + + Mock mockServiceProvider = new(MockBehavior.Strict); + mockServiceProvider + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) + .Returns(mockEditorService.Object); + + MaskedTextBoxTextEditor editor = new(); + string originalValue = "TestValue"; + object? actualValue = editor.EditValue(mockContext.Object, mockServiceProvider.Object, originalValue); + + // When Context.Instance is MaskedTextBox, its own Text property of control itself is not changed, + // and the return value of EditValue is equal to the control's own Text value. + Assert.True(mockContext.Object.Instance is MaskedTextBox); + MaskedTextBox? maskedTextBox = mockContext.Object.Instance as MaskedTextBox; + + maskedTextBox?.Text.Should().NotBe(originalValue); + actualValue.Should().Be(CustomMaskedTextBox.Text); + } + + [Fact] + public void EditValue_ReturnsOriginalValue_WhenContextInstanceIsMaskedTextBox_AndDropDownValueIsNull() + { + MaskedTextBox CustomMaskedTextBox = new(); + + // Set context.Instance to a new MaskedTextBox without Text value. + Mock mockContext = new(MockBehavior.Strict); + mockContext + .Setup(c => c.Instance) + .Returns(CustomMaskedTextBox); + + Mock mockEditorService = new(MockBehavior.Strict); + mockEditorService + .Setup(s => s.DropDownControl(It.IsAny())); + + Mock mockServiceProvider = new(MockBehavior.Strict); + mockServiceProvider + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) + .Returns(mockEditorService.Object); + + MaskedTextBoxTextEditor editor = new(); + string originalValue = "TestValue"; + object? actualValue = editor.EditValue(mockContext.Object, mockServiceProvider.Object, originalValue); + + // When Context.Instance is MaskedTextBox, its own Text property of control itself will not change, + // and the return value of EditValue is also original value. + Assert.True(mockContext.Object.Instance is MaskedTextBox); + MaskedTextBox? maskedTextBox = mockContext.Object.Instance as MaskedTextBox; + + maskedTextBox?.Text.Should().NotBe(originalValue); + actualValue.Should().BeSameAs(CustomMaskedTextBox.Text); + } + + [Theory] + [InlineData([new object[] { }])] + [InlineData(null)] + public void GetEditStyle_ReturnExpected(object? host) + { + Mock mockContext = new(MockBehavior.Strict); + mockContext + .Setup(c => c.Instance) + .Returns(host); + + MaskedTextBoxTextEditor editor = new(); + UITypeEditorEditStyle result = editor.GetEditStyle(mockContext.Object); + + if (host is null) + { + // Returns base.GetEditStyle when Context.Instance is null. + result.Should().Be(editor.GetEditStyle(mockContext.Object)); + } + else + { + // Returns DropDown when Context.Instance is not null. + result.Should().Be(UITypeEditorEditStyle.DropDown); + } + } + + [Theory] + [InlineData([new object[] { }])] + [InlineData(null)] + public void GetPaintValueSupported_ReturnExpected(object? host) + { + Mock mockContext = new(MockBehavior.Strict); + mockContext + .Setup(c => c.Instance) + .Returns(host); + + MaskedTextBoxTextEditor editor = new(); + bool result = editor.GetPaintValueSupported(mockContext.Object); + + if (host is null) + { + // Returns base.GetPaintValueSupported when Context.Instance is null. + result.Should().Be(editor.GetPaintValueSupported(mockContext.Object)); + } + else + { + // Returns false when Context.Instance is not null. + result.Should().BeFalse(); + } + } + + [Fact] + public void IsDropDownResizable_ReturnsFalse() + { + MaskedTextBoxTextEditor editor = new(); + editor.IsDropDownResizable.Should().BeFalse(); + } +}