|
| 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.Design; |
| 5 | +using System.Drawing; |
| 6 | +using Moq; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Design.Tests; |
| 9 | + |
| 10 | +public class DataGridViewColumnTypePickerTests : IDisposable |
| 11 | +{ |
| 12 | + private readonly DataGridViewColumnTypePicker _picker; |
| 13 | + |
| 14 | + public DataGridViewColumnTypePickerTests() |
| 15 | + { |
| 16 | + _picker = new(); |
| 17 | + } |
| 18 | + |
| 19 | + public void Dispose() => _picker.Dispose(); |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void Constructor_ShouldInitializeListBoxCorrectly() |
| 23 | + { |
| 24 | + _picker.Controls.Count.Should().Be(1); |
| 25 | + _picker.Controls[0].Should().BeOfType<ListBox>().Which.Should().Match<ListBox>(listBox => |
| 26 | + listBox.Dock == DockStyle.Fill |
| 27 | + && listBox.Sorted |
| 28 | + && listBox.HorizontalScrollbar); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void Constructor_ShouldSetBackColorAndActiveControl() |
| 33 | + { |
| 34 | + _picker.BackColor.Should().Be(SystemColors.Control); |
| 35 | + _picker.ActiveControl.Should().Be(_picker.Controls[0]); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void SelectedType_ShouldReturnNull_WhenNotSet() => |
| 40 | + _picker.SelectedType.Should().BeNull(); |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Start_ShouldPopulateListBoxItems() |
| 44 | + { |
| 45 | + Mock<IWindowsFormsEditorService> editorServiceMock = new(); |
| 46 | + Mock<ITypeDiscoveryService> discoveryServiceMock = new(); |
| 47 | + List<Type> types = [typeof(DataGridViewTextBoxColumn), typeof(DataGridViewButtonColumn)]; |
| 48 | + discoveryServiceMock.Setup(ds => ds.GetTypes(It.IsAny<Type>(), It.IsAny<bool>())).Returns(types); |
| 49 | + |
| 50 | + _picker.Start(editorServiceMock.Object, discoveryServiceMock.Object, typeof(DataGridViewTextBoxColumn)); |
| 51 | + |
| 52 | + var listBox = _picker.TestAccessor().Dynamic._typesListBox; |
| 53 | + object count = listBox.Items.Count; |
| 54 | + count.Should().Be(2); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void SetBoundsCore_ShouldSetMinimumWidthAndHeight() |
| 59 | + { |
| 60 | + var accessor = _picker.TestAccessor().Dynamic; |
| 61 | + accessor.SetBoundsCore(0, 0, 50, 50, BoundsSpecified.All); |
| 62 | + |
| 63 | + _picker.Width.Should().BeGreaterOrEqualTo(100); |
| 64 | + _picker.Height.Should().BeGreaterOrEqualTo(90); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void typesListBox_SelectedIndexChanged_ShouldSetSelectedType() |
| 69 | + { |
| 70 | + Mock<IWindowsFormsEditorService> editorServiceMock = new(); |
| 71 | + Mock<ITypeDiscoveryService> discoveryServiceMock = new(); |
| 72 | + List<Type> types = [typeof(DataGridViewTextBoxColumn), typeof(DataGridViewButtonColumn)]; |
| 73 | + discoveryServiceMock.Setup(ds => ds.GetTypes(It.IsAny<Type>(), It.IsAny<bool>())).Returns(types); |
| 74 | + |
| 75 | + _picker.Start(editorServiceMock.Object, discoveryServiceMock.Object, typeof(DataGridViewTextBoxColumn)); |
| 76 | + var listBox = _picker.TestAccessor().Dynamic._typesListBox; |
| 77 | + listBox.SelectedIndex = 1; |
| 78 | + |
| 79 | + _picker.TestAccessor().Dynamic.typesListBox_SelectedIndexChanged(listBox, EventArgs.Empty); |
| 80 | + |
| 81 | + _picker.SelectedType.Should().Be(typeof(DataGridViewTextBoxColumn)); |
| 82 | + } |
| 83 | +} |
0 commit comments