|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Drawing.Design; |
| 6 | +using Moq; |
| 7 | + |
| 8 | +#nullable enable |
| 9 | + |
| 10 | +namespace System.Windows.Forms.Design.Tests; |
| 11 | + |
| 12 | +public class MaskedTextBoxTextEditorTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void EditValue_ReturnsOriginalValue_WhenContextInstanceIsNull() |
| 16 | + { |
| 17 | + Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict); |
| 18 | + mockContext |
| 19 | + .Setup(c => c.Instance) |
| 20 | + .Returns(null); |
| 21 | + |
| 22 | + Mock<IWindowsFormsEditorService> mockEditorService = new(MockBehavior.Strict); |
| 23 | + mockEditorService |
| 24 | + .Setup(s => s.ShowDialog(It.IsAny<Form>())) |
| 25 | + .Returns(DialogResult.OK); |
| 26 | + |
| 27 | + Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict); |
| 28 | + mockServiceProvider |
| 29 | + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) |
| 30 | + .Returns(mockEditorService.Object); |
| 31 | + |
| 32 | + MaskedTextBoxTextEditor editor = new(); |
| 33 | + object? value = "TestValue"; |
| 34 | + |
| 35 | + value.Should().BeSameAs(editor.EditValue(mockContext.Object, mockServiceProvider.Object, value)); |
| 36 | + } |
| 37 | + |
| 38 | + [WinFormsFact] |
| 39 | + public void EditValue_ReturnsOriginalValue_WhenEditorServiceIsNotAvailable() |
| 40 | + { |
| 41 | + Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict); |
| 42 | + mockContext |
| 43 | + .Setup(c => c.Instance) |
| 44 | + .Returns(new object()); |
| 45 | + |
| 46 | + IWindowsFormsEditorService? editorService = null; |
| 47 | + |
| 48 | + Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict); |
| 49 | + mockServiceProvider |
| 50 | + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) |
| 51 | + .Returns(editorService); |
| 52 | + |
| 53 | + MaskedTextBoxTextEditor editor = new(); |
| 54 | + object? value = "TestValue"; |
| 55 | + |
| 56 | + value.Should().BeSameAs(editor.EditValue(mockContext.Object, mockServiceProvider.Object, value)); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void EditValue_ReturnsOriginalValue_WhenContextInstanceIsNotMaskedTextBox() |
| 61 | + { |
| 62 | + // Set context.Instance to new object. |
| 63 | + Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict); |
| 64 | + mockContext |
| 65 | + .Setup(c => c.Instance) |
| 66 | + .Returns(new object()); |
| 67 | + |
| 68 | + Mock<IWindowsFormsEditorService> mockEditorService = new(MockBehavior.Strict); |
| 69 | + mockEditorService |
| 70 | + .Setup(s => s.DropDownControl(It.IsAny<MaskedTextBoxTextEditorDropDown>())); |
| 71 | + |
| 72 | + Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict); |
| 73 | + mockServiceProvider |
| 74 | + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) |
| 75 | + .Returns(mockEditorService.Object); |
| 76 | + |
| 77 | + MaskedTextBoxTextEditor editor = new(); |
| 78 | + string originalValue = "TestValue"; |
| 79 | + object? actualValue = editor.EditValue(mockContext.Object, mockServiceProvider.Object, originalValue); |
| 80 | + |
| 81 | + // When Context.Instance is not MaskedTextBox, its Text property should be the original value, |
| 82 | + // and the return value of EditValue is also the original value. |
| 83 | + mockContext.Object.Instance.Should().NotBeOfType<MaskedTextBox>(); |
| 84 | + MaskedTextBox? maskedText = mockContext.Object.Instance as MaskedTextBox; |
| 85 | + |
| 86 | + maskedText?.Text.Should().Be(originalValue); |
| 87 | + actualValue.Should().Be(originalValue); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void EditValue_ReturnsDropDownValue_WhenContextInstanceIsMaskedTextBox_AndDropDownValueIsNotNull() |
| 92 | + { |
| 93 | + MaskedTextBox CustomMaskedTextBox = new() |
| 94 | + { |
| 95 | + Text = "MaskText" |
| 96 | + }; |
| 97 | + |
| 98 | + // Set context.Instance to a new MaskedTextBox with Text. |
| 99 | + Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict); |
| 100 | + mockContext |
| 101 | + .Setup(c => c.Instance) |
| 102 | + .Returns(CustomMaskedTextBox); |
| 103 | + |
| 104 | + Mock<IWindowsFormsEditorService> mockEditorService = new(MockBehavior.Strict); |
| 105 | + mockEditorService |
| 106 | + .Setup(s => s.DropDownControl(It.IsAny<MaskedTextBoxTextEditorDropDown>())); |
| 107 | + |
| 108 | + Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict); |
| 109 | + mockServiceProvider |
| 110 | + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) |
| 111 | + .Returns(mockEditorService.Object); |
| 112 | + |
| 113 | + MaskedTextBoxTextEditor editor = new(); |
| 114 | + string originalValue = "TestValue"; |
| 115 | + object? actualValue = editor.EditValue(mockContext.Object, mockServiceProvider.Object, originalValue); |
| 116 | + |
| 117 | + // When Context.Instance is MaskedTextBox, its own Text property of control itself is not changed, |
| 118 | + // and the return value of EditValue is equal to the control's own Text value. |
| 119 | + Assert.True(mockContext.Object.Instance is MaskedTextBox); |
| 120 | + MaskedTextBox? maskedTextBox = mockContext.Object.Instance as MaskedTextBox; |
| 121 | + |
| 122 | + maskedTextBox?.Text.Should().NotBe(originalValue); |
| 123 | + actualValue.Should().Be(CustomMaskedTextBox.Text); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void EditValue_ReturnsOriginalValue_WhenContextInstanceIsMaskedTextBox_AndDropDownValueIsNull() |
| 128 | + { |
| 129 | + MaskedTextBox CustomMaskedTextBox = new(); |
| 130 | + |
| 131 | + // Set context.Instance to a new MaskedTextBox without Text value. |
| 132 | + Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict); |
| 133 | + mockContext |
| 134 | + .Setup(c => c.Instance) |
| 135 | + .Returns(CustomMaskedTextBox); |
| 136 | + |
| 137 | + Mock<IWindowsFormsEditorService> mockEditorService = new(MockBehavior.Strict); |
| 138 | + mockEditorService |
| 139 | + .Setup(s => s.DropDownControl(It.IsAny<MaskedTextBoxTextEditorDropDown>())); |
| 140 | + |
| 141 | + Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict); |
| 142 | + mockServiceProvider |
| 143 | + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) |
| 144 | + .Returns(mockEditorService.Object); |
| 145 | + |
| 146 | + MaskedTextBoxTextEditor editor = new(); |
| 147 | + string originalValue = "TestValue"; |
| 148 | + object? actualValue = editor.EditValue(mockContext.Object, mockServiceProvider.Object, originalValue); |
| 149 | + |
| 150 | + // When Context.Instance is MaskedTextBox, its own Text property of control itself will not change, |
| 151 | + // and the return value of EditValue is also original value. |
| 152 | + Assert.True(mockContext.Object.Instance is MaskedTextBox); |
| 153 | + MaskedTextBox? maskedTextBox = mockContext.Object.Instance as MaskedTextBox; |
| 154 | + |
| 155 | + maskedTextBox?.Text.Should().NotBe(originalValue); |
| 156 | + actualValue.Should().BeSameAs(CustomMaskedTextBox.Text); |
| 157 | + } |
| 158 | + |
| 159 | + [Theory] |
| 160 | + [InlineData([new object[] { }])] |
| 161 | + [InlineData(null)] |
| 162 | + public void GetEditStyle_ReturnExpected(object? host) |
| 163 | + { |
| 164 | + Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict); |
| 165 | + mockContext |
| 166 | + .Setup(c => c.Instance) |
| 167 | + .Returns(host); |
| 168 | + |
| 169 | + MaskedTextBoxTextEditor editor = new(); |
| 170 | + UITypeEditorEditStyle result = editor.GetEditStyle(mockContext.Object); |
| 171 | + |
| 172 | + if (host is null) |
| 173 | + { |
| 174 | + // Returns base.GetEditStyle when Context.Instance is null. |
| 175 | + result.Should().Be(editor.GetEditStyle(mockContext.Object)); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + // Returns DropDown when Context.Instance is not null. |
| 180 | + result.Should().Be(UITypeEditorEditStyle.DropDown); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + [Theory] |
| 185 | + [InlineData([new object[] { }])] |
| 186 | + [InlineData(null)] |
| 187 | + public void GetPaintValueSupported_ReturnExpected(object? host) |
| 188 | + { |
| 189 | + Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict); |
| 190 | + mockContext |
| 191 | + .Setup(c => c.Instance) |
| 192 | + .Returns(host); |
| 193 | + |
| 194 | + MaskedTextBoxTextEditor editor = new(); |
| 195 | + bool result = editor.GetPaintValueSupported(mockContext.Object); |
| 196 | + |
| 197 | + if (host is null) |
| 198 | + { |
| 199 | + // Returns base.GetPaintValueSupported when Context.Instance is null. |
| 200 | + result.Should().Be(editor.GetPaintValueSupported(mockContext.Object)); |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + // Returns false when Context.Instance is not null. |
| 205 | + result.Should().BeFalse(); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + [Fact] |
| 210 | + public void IsDropDownResizable_ReturnsFalse() |
| 211 | + { |
| 212 | + MaskedTextBoxTextEditor editor = new(); |
| 213 | + editor.IsDropDownResizable.Should().BeFalse(); |
| 214 | + } |
| 215 | +} |
0 commit comments