|
| 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.Collections; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.ComponentModel.Design; |
| 7 | +using System.Drawing.Design; |
| 8 | +using Moq; |
| 9 | + |
| 10 | +namespace System.Windows.Forms.Design.Tests; |
| 11 | + |
| 12 | +public class DataGridViewColumnTypeEditorTests |
| 13 | +{ |
| 14 | + private readonly DataGridViewColumnTypeEditor _editor; |
| 15 | + |
| 16 | + public DataGridViewColumnTypeEditorTests() |
| 17 | + { |
| 18 | + _editor = new(); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void Constructor_ShouldInitializeCorrectly() |
| 23 | + { |
| 24 | + _editor.Should().NotBeNull(); |
| 25 | + _editor.IsDropDownResizable.Should().BeTrue(); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void IsDropDownResizable_ShouldReturnTrue() => |
| 30 | + _editor.IsDropDownResizable.Should().BeTrue(); |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void GetEditStyle_ShouldReturnDropDown() => |
| 34 | + _editor.GetEditStyle(null).Should().Be(UITypeEditorEditStyle.DropDown); |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void EditValue_ShouldReturnOriginalValue_WhenNoServiceProvider() => |
| 38 | + _editor.EditValue(null, null!, typeof(DataGridViewTextBoxColumn)).Should().Be(typeof(DataGridViewTextBoxColumn)); |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void EditValue_ShouldReturnOriginalValue_WhenNoEditorService() |
| 42 | + { |
| 43 | + Mock<IServiceProvider> serviceProviderMock = new(); |
| 44 | + serviceProviderMock.Setup(sp => sp.GetService(typeof(IWindowsFormsEditorService))).Returns(null!); |
| 45 | + |
| 46 | + _editor.EditValue(null, serviceProviderMock.Object, typeof(DataGridViewTextBoxColumn)).Should().Be(typeof(DataGridViewTextBoxColumn)); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void EditValue_ShouldReturnOriginalValue_WhenNoContextInstance() |
| 51 | + { |
| 52 | + Mock<IServiceProvider> serviceProviderMock = new(); |
| 53 | + Mock<IWindowsFormsEditorService> editorServiceMock = new(); |
| 54 | + serviceProviderMock.Setup(sp => sp.GetService(typeof(IWindowsFormsEditorService))).Returns(editorServiceMock.Object); |
| 55 | + |
| 56 | + _editor.EditValue(null, serviceProviderMock.Object, typeof(DataGridViewTextBoxColumn)).Should().Be(typeof(DataGridViewTextBoxColumn)); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void EditValue_ShouldReturnOriginalValue_WhenSelectedTypeIsNull() |
| 61 | + { |
| 62 | + Mock<IServiceProvider> mockProvider = new(); |
| 63 | + Mock<IWindowsFormsEditorService> mockEditorService = new(); |
| 64 | + Mock<ITypeDescriptorContext> mockContext = new(); |
| 65 | + Mock<IDesignerHost> mockDesignerHost = new(); |
| 66 | + Mock<ITypeDiscoveryService> mockDiscoveryService = new(); |
| 67 | + |
| 68 | + mockProvider.Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) |
| 69 | + .Returns(mockEditorService.Object); |
| 70 | + mockProvider.Setup(p => p.GetService(typeof(IDesignerHost))) |
| 71 | + .Returns(mockDesignerHost.Object); |
| 72 | + mockDesignerHost.Setup(d => d.GetService(typeof(ITypeDiscoveryService))) |
| 73 | + .Returns(mockDiscoveryService.Object); |
| 74 | + |
| 75 | + ArrayList mockTypeCollection = [typeof(DataGridViewTextBoxColumn), typeof(DataGridViewCheckBoxColumn)]; |
| 76 | + mockDiscoveryService.Setup(d => d.GetTypes(typeof(DataGridViewColumn), false)) |
| 77 | + .Returns(mockTypeCollection); |
| 78 | + |
| 79 | + DataGridViewTextBoxColumn column = new(); |
| 80 | + SubListBoxItem listBoxItem = new(column); |
| 81 | + mockContext.Setup(c => c.Instance).Returns(listBoxItem); |
| 82 | + |
| 83 | + object? result = _editor.EditValue(mockContext.Object, mockProvider.Object, null); |
| 84 | + |
| 85 | + mockEditorService.Verify(s => s.DropDownControl(It.IsAny<Control>()), Times.Once); |
| 86 | + result.Should().Be(null); |
| 87 | + } |
| 88 | + |
| 89 | + private class SubListBoxItem : DataGridViewColumnCollectionDialog.ListBoxItem |
| 90 | + { |
| 91 | + public SubListBoxItem(DataGridViewColumn column) : base(column, null!, null!) { } |
| 92 | + } |
| 93 | +} |
0 commit comments