|
| 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.ComponentModel.Design; |
| 6 | +using Moq; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Design.Tests; |
| 9 | + |
| 10 | +public class DataGridViewDesignerTests : IDisposable |
| 11 | +{ |
| 12 | + private readonly DataGridViewDesigner _designer; |
| 13 | + private readonly DataGridView _dataGridView; |
| 14 | + |
| 15 | + public DataGridViewDesignerTests() |
| 16 | + { |
| 17 | + _designer = new(); |
| 18 | + _dataGridView = new(); |
| 19 | + _designer.Initialize(_dataGridView); |
| 20 | + } |
| 21 | + |
| 22 | + public void Dispose() |
| 23 | + { |
| 24 | + _designer.Dispose(); |
| 25 | + _dataGridView.Dispose(); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void Control_ReturnsDataGridView() |
| 30 | + { |
| 31 | + _designer.Control.Should().NotBeNull(); |
| 32 | + _designer.Control.Should().BeOfType<DataGridView>(); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void Constructor_SetsAutoResizeHandlesToTrue() => |
| 37 | + _designer.AutoResizeHandles.Should().BeTrue(); |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void AssociatedComponents_ReturnsDataGridViewColumns() => |
| 41 | + _designer.AssociatedComponents.Should().BeSameAs(_dataGridView.Columns); |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void AutoSizeColumnsMode_Get_ReturnsExpected() |
| 45 | + { |
| 46 | + _dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; |
| 47 | + _designer.AutoSizeColumnsMode.Should().Be(DataGridViewAutoSizeColumnsMode.Fill); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void AutoSizeColumnsMode_Set_CallsComponentChangeService() |
| 52 | + { |
| 53 | + Mock<IComponentChangeService> mockChangeService = new(); |
| 54 | + Mock<ISite> site = new(); |
| 55 | + site.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(mockChangeService.Object); |
| 56 | + _dataGridView.Site = site.Object; |
| 57 | + |
| 58 | + _designer.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; |
| 59 | + |
| 60 | + mockChangeService.Verify( |
| 61 | + s => s.OnComponentChanging( |
| 62 | + It.IsAny<object>(), |
| 63 | + It.IsAny<PropertyDescriptor>() |
| 64 | + ), |
| 65 | + Times.Exactly(_dataGridView.Columns.Count) |
| 66 | + ); |
| 67 | + |
| 68 | + mockChangeService.Verify( |
| 69 | + s => s.OnComponentChanged( |
| 70 | + It.IsAny<object>(), |
| 71 | + It.IsAny<PropertyDescriptor>(), |
| 72 | + It.IsAny<object>(), |
| 73 | + It.IsAny<object>() |
| 74 | + ), |
| 75 | + Times.Exactly(_dataGridView.Columns.Count) |
| 76 | + ); |
| 77 | + |
| 78 | + _dataGridView.AutoSizeColumnsMode.Should().Be(DataGridViewAutoSizeColumnsMode.Fill); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void DataSource_Get_ReturnsExpected() |
| 83 | + { |
| 84 | + object dataSource = new(); |
| 85 | + _dataGridView.DataSource = dataSource; |
| 86 | + |
| 87 | + _designer.DataSource.Should().BeSameAs(dataSource); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void DataSource_Set_SetsExpected() |
| 92 | + { |
| 93 | + object dataSource = new(); |
| 94 | + _designer.DataSource = dataSource; |
| 95 | + |
| 96 | + _dataGridView.DataSource.Should().BeSameAs(dataSource); |
| 97 | + _dataGridView.AutoGenerateColumns.Should().BeFalse(); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void DataSource_Set_Null_SetsExpected() |
| 102 | + { |
| 103 | + _dataGridView.AutoGenerateColumns = false; |
| 104 | + _designer.DataSource = null; |
| 105 | + |
| 106 | + _dataGridView.DataSource.Should().BeNull(); |
| 107 | + _dataGridView.AutoGenerateColumns.Should().BeFalse(); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void Initialize_SetsAutoGenerateColumnsCorrectly() => |
| 112 | + _dataGridView.AutoGenerateColumns.Should().BeTrue(); |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void InitializeNewComponent_SetsColumnHeadersHeightSizeModeToAutoSize() |
| 116 | + { |
| 117 | + _designer.InitializeNewComponent(null); |
| 118 | + |
| 119 | + _dataGridView.ColumnHeadersHeightSizeMode.Should().Be(DataGridViewColumnHeadersHeightSizeMode.AutoSize); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void Verbs_ReturnsExpected() |
| 124 | + { |
| 125 | + DesignerVerbCollection verbs = _designer.Verbs; |
| 126 | + |
| 127 | + verbs.Should().NotBeNull(); |
| 128 | + verbs.Count.Should().Be(2); |
| 129 | + verbs[0]!.Text.Should().Be(SR.DataGridViewEditColumnsVerb); |
| 130 | + verbs[1]!.Text.Should().Be(SR.DataGridViewAddColumnVerb); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void ActionLists_CachesActionLists() |
| 135 | + { |
| 136 | + DesignerActionListCollection actionLists1 = _designer.ActionLists; |
| 137 | + DesignerActionListCollection actionLists2 = _designer.ActionLists; |
| 138 | + |
| 139 | + actionLists1.Should().BeSameAs(actionLists2); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void PreFilterProperties_ShadowsPropertiesCorrectly() |
| 144 | + { |
| 145 | + Dictionary<string, PropertyDescriptor> properties = new() |
| 146 | + { |
| 147 | + { "AutoSizeColumnsMode", TypeDescriptor.GetProperties(typeof(DataGridView))["AutoSizeColumnsMode"]!}, |
| 148 | + { "DataSource", TypeDescriptor.GetProperties(typeof(DataGridView))["DataSource"]!} |
| 149 | + }; |
| 150 | + |
| 151 | + _designer.TestAccessor().Dynamic.PreFilterProperties(properties); |
| 152 | + |
| 153 | + properties["AutoSizeColumnsMode"].ComponentType.Should().Be(typeof(DataGridViewDesigner)); |
| 154 | + properties["DataSource"].ComponentType.Should().Be(typeof(DataGridViewDesigner)); |
| 155 | + } |
| 156 | +} |
0 commit comments