Skip to content

Commit 5fb62d5

Browse files
authored
Add unit tests for TableLayoutPanelDesigner (#13028)
Related #10773 ## Proposed changes - Add unit test TableLayoutPanelDesignerTests.cs for some public properties and method of the TableLayoutPanelDesigner.
1 parent e00df8a commit 5fb62d5

File tree

1 file changed

+314
-0
lines changed

1 file changed

+314
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
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 Moq;
8+
using Moq.Protected;
9+
10+
namespace System.Windows.Forms.Design.Tests;
11+
12+
public class TableLayoutPanelDesignerTests : IDisposable
13+
{
14+
private readonly TableLayoutPanelDesigner _designer;
15+
private readonly TableLayoutPanel _tableLayoutPanel;
16+
private readonly TableLayoutPanelDesigner.DesignerTableLayoutControlCollection _collection;
17+
18+
public TableLayoutPanelDesignerTests()
19+
{
20+
_designer = new();
21+
_tableLayoutPanel = new();
22+
_designer.Initialize(_tableLayoutPanel);
23+
_collection = new(_tableLayoutPanel);
24+
}
25+
26+
public void Dispose()
27+
{
28+
_designer.Dispose();
29+
_tableLayoutPanel.Dispose();
30+
}
31+
32+
[Fact]
33+
public void RowCount_Property_Tests()
34+
{
35+
_tableLayoutPanel.RowCount = 5;
36+
_designer.RowCount.Should().Be(5);
37+
38+
_designer.RowCount = 3;
39+
_tableLayoutPanel.RowCount.Should().Be(3);
40+
41+
Action rowCountAct = () => _designer.RowCount = 0;
42+
rowCountAct.Should().Throw<ArgumentException>();
43+
}
44+
45+
[Fact]
46+
public void ColumnCount_Property_Tests()
47+
{
48+
_tableLayoutPanel.ColumnCount = 5;
49+
_designer.ColumnCount.Should().Be(5);
50+
51+
_designer.ColumnCount = 3;
52+
_tableLayoutPanel.ColumnCount.Should().Be(3);
53+
54+
Action columnCountAct = () => _designer.ColumnCount = 0;
55+
columnCountAct.Should().Throw<ArgumentException>();
56+
57+
_designer.TestAccessor().Dynamic.Undoing = true;
58+
_designer.ColumnCount = 0;
59+
_tableLayoutPanel.ColumnCount.Should().Be(0);
60+
}
61+
62+
[Fact]
63+
public void Verbs_Property_Tests()
64+
{
65+
List<DesignerVerb> verbs = _designer.Verbs.Cast<DesignerVerb>().ToList();
66+
67+
verbs.Should().NotBeNull();
68+
verbs.Should().HaveCount(5);
69+
70+
verbs.Select(v => v.Text).Should()
71+
.Contain(SR.TableLayoutPanelDesignerAddColumn)
72+
.And.Contain(SR.TableLayoutPanelDesignerAddRow)
73+
.And.Contain(SR.TableLayoutPanelDesignerRemoveColumn)
74+
.And.Contain(SR.TableLayoutPanelDesignerRemoveRow)
75+
.And.Contain(SR.TableLayoutPanelDesignerEditRowAndCol);
76+
}
77+
78+
[Fact]
79+
public void Verbs_Property_CheckVerbStatus()
80+
{
81+
_tableLayoutPanel.ColumnCount = 1;
82+
_tableLayoutPanel.RowCount = 1;
83+
84+
_designer.TestAccessor().Dynamic.CheckVerbStatus();
85+
List<DesignerVerb> verbs = _designer.Verbs.Cast<DesignerVerb>().ToList();
86+
87+
verbs.Should().ContainSingle(v => v.Text == SR.TableLayoutPanelDesignerRemoveColumn && !v.Enabled);
88+
verbs.Should().ContainSingle(v => v.Text == SR.TableLayoutPanelDesignerRemoveRow && !v.Enabled);
89+
90+
_tableLayoutPanel.ColumnCount = 2;
91+
_tableLayoutPanel.RowCount = 2;
92+
93+
_designer.TestAccessor().Dynamic.CheckVerbStatus();
94+
verbs = _designer.Verbs.Cast<DesignerVerb>().ToList();
95+
96+
verbs.Should().ContainSingle(v => v.Text == SR.TableLayoutPanelDesignerRemoveColumn && v.Enabled);
97+
verbs.Should().ContainSingle(v => v.Text == SR.TableLayoutPanelDesignerRemoveRow && v.Enabled);
98+
}
99+
100+
[Fact]
101+
public void ActionLists_Should_Be_Initialized_Correctly()
102+
{
103+
_designer.TestAccessor().Dynamic._actionLists = null;
104+
_designer.TestAccessor().Dynamic.BuildActionLists();
105+
106+
DesignerActionListCollection actionLists = _designer.ActionLists;
107+
108+
actionLists.Should().NotBeNull();
109+
actionLists.Count.Should().BeGreaterThan(0);
110+
actionLists.Should().BeOfType<DesignerActionListCollection>();
111+
}
112+
113+
[Fact]
114+
public void Initialize_Should_Setup_Correctly()
115+
{
116+
Mock<IDesignerHost> hostMock = new();
117+
Mock<IComponentChangeService> compChangeServiceMock = new();
118+
119+
hostMock.Setup(h => h.GetService(typeof(IComponentChangeService)))
120+
.Returns(compChangeServiceMock.Object);
121+
hostMock.SetupAdd(h => h.TransactionClosing += It.IsAny<DesignerTransactionCloseEventHandler>());
122+
123+
Mock<IServiceProvider> serviceProviderMock = new();
124+
serviceProviderMock.Setup(sp => sp.GetService(typeof(IDesignerHost)))
125+
.Returns(hostMock.Object);
126+
127+
Mock<TableLayoutPanelDesigner> designerMock = new() { CallBase = true };
128+
designerMock.Protected()
129+
.Setup<object>("GetService", ItExpr.IsAny<Type>())
130+
.Returns((Type serviceType) =>
131+
{
132+
if (serviceType == typeof(IDesignerHost))
133+
return hostMock.Object;
134+
return null!;
135+
});
136+
137+
TableLayoutPanelDesigner designer = designerMock.Object;
138+
designer.Initialize(_tableLayoutPanel);
139+
140+
hostMock.VerifyAdd(h => h.TransactionClosing += It.IsAny<DesignerTransactionCloseEventHandler>(), Times.Once);
141+
compChangeServiceMock.VerifyAdd(c => c.ComponentChanging += It.IsAny<ComponentChangingEventHandler>(), Times.Once);
142+
compChangeServiceMock.VerifyAdd(c => c.ComponentChanged += It.IsAny<ComponentChangedEventHandler>(), Times.Once);
143+
144+
_tableLayoutPanel.ControlAdded += It.IsAny<ControlEventHandler>();
145+
_tableLayoutPanel.ControlRemoved += It.IsAny<ControlEventHandler>();
146+
147+
PropertyDescriptor rowStyleProp = (PropertyDescriptor)designer.TestAccessor().Dynamic._rowStyleProp;
148+
PropertyDescriptor colStyleProp = (PropertyDescriptor)designer.TestAccessor().Dynamic._colStyleProp;
149+
150+
rowStyleProp.Should().NotBeNull();
151+
colStyleProp.Should().NotBeNull();
152+
153+
InheritanceAttribute inheritanceAttribute = _designer.TestAccessor().Dynamic.InheritanceAttribute;
154+
155+
if (inheritanceAttribute == InheritanceAttribute.InheritedReadOnly)
156+
{
157+
foreach (Control control in _tableLayoutPanel.Controls)
158+
{
159+
TypeDescriptor.GetAttributes(control).Contains(InheritanceAttribute.InheritedReadOnly).Should().BeTrue();
160+
}
161+
}
162+
}
163+
164+
[Fact]
165+
public void InitializeNewComponent_Should_CreateEmptyTable()
166+
{
167+
Dictionary<string, object> defaultValues = new();
168+
169+
_designer.InitializeNewComponent(defaultValues);
170+
171+
_tableLayoutPanel.RowCount.Should().Be(2);
172+
_tableLayoutPanel.ColumnCount.Should().Be(2);
173+
_tableLayoutPanel.RowStyles.Count.Should().Be(2);
174+
_tableLayoutPanel.ColumnStyles.Count.Should().Be(2);
175+
}
176+
177+
[Fact]
178+
public void DesignerTableLayoutControlCollection_Constructor_Should_Initialize_RealCollection()
179+
{
180+
_collection.Should().NotBeNull();
181+
_collection.Count.Should().Be(0);
182+
_collection.Should().BeAssignableTo<IList>();
183+
}
184+
185+
[Fact]
186+
public void DesignerTableLayoutControlCollection_Count_Should_Return_RealCollection_Count()
187+
{
188+
using Button button = new();
189+
_tableLayoutPanel.Controls.Add(button);
190+
_collection.Count.Should().Be(1);
191+
}
192+
193+
[Fact]
194+
public void DesignerTableLayoutControlCollection_IsReadOnly_Should_Return_RealCollection_IsReadOnly()
195+
{
196+
_collection.IsReadOnly.Should().Be(_tableLayoutPanel.Controls.IsReadOnly);
197+
}
198+
199+
[Fact]
200+
public void DesignerTableLayoutControlCollection_Add_Should_Add_Control_To_RealCollection()
201+
{
202+
using Button button = new();
203+
_collection.Add(button);
204+
_tableLayoutPanel.Controls.Contains(button).Should().BeTrue();
205+
}
206+
207+
[Fact]
208+
public void DesignerTableLayoutControlCollection_AddRange_Should_Add_Controls_To_RealCollection()
209+
{
210+
using Button button1 = new();
211+
using Button button2 = new();
212+
213+
Control[] controls = [button1, button2];
214+
215+
_collection.AddRange(controls);
216+
217+
_tableLayoutPanel.Controls.Contains(button1).Should().BeTrue();
218+
_tableLayoutPanel.Controls.Contains(button2).Should().BeTrue();
219+
}
220+
221+
[Fact]
222+
public void DesignerTableLayoutControlCollection_CopyTo_Should_Copy_Controls_To_Array()
223+
{
224+
using Button button1 = new();
225+
using Button button2 = new();
226+
_collection.Add(button1);
227+
_collection.Add(button2);
228+
229+
Control[] array = new Control[2];
230+
_collection.CopyTo(array, 0);
231+
232+
array.Should().Contain(button1);
233+
array.Should().Contain(button2);
234+
}
235+
236+
[Fact]
237+
public void DesignerTableLayoutControlCollection_Equals_Tests()
238+
{
239+
TableLayoutPanelDesigner.DesignerTableLayoutControlCollection collection1 = new(_tableLayoutPanel);
240+
TableLayoutPanelDesigner.DesignerTableLayoutControlCollection collection2 = new(_tableLayoutPanel);
241+
TableLayoutPanel anotherTableLayoutPanel = new();
242+
TableLayoutPanelDesigner.DesignerTableLayoutControlCollection collection3 = new(anotherTableLayoutPanel);
243+
object nonCollectionObject = new();
244+
245+
collection1.Equals(collection1).Should().BeTrue();
246+
247+
collection1.Equals(collection2).Should().BeTrue();
248+
249+
collection1.Equals(collection3).Should().BeTrue();
250+
251+
collection1.Equals(null).Should().BeFalse();
252+
253+
collection1?.Equals(nonCollectionObject).Should().BeFalse();
254+
}
255+
256+
[Fact]
257+
public void DesignerTableLayoutControlCollection_GetEnumerator_Tests()
258+
{
259+
using Button button1 = new();
260+
using Button button2 = new();
261+
using Button button3 = new();
262+
263+
_collection.Add(button1);
264+
_collection.Add(button2);
265+
_collection.Add(button3);
266+
267+
IEnumerator enumerator = _collection.GetEnumerator();
268+
List<Control> controls = new();
269+
270+
while (enumerator.MoveNext())
271+
{
272+
controls.Add((Control)enumerator.Current);
273+
}
274+
275+
enumerator.Should().NotBeNull();
276+
277+
enumerator.Should().BeAssignableTo<IEnumerator>();
278+
279+
controls.Should().Contain(button1);
280+
controls.Should().Contain(button2);
281+
controls.Should().Contain(button3);
282+
}
283+
284+
[Fact]
285+
public void DesignerTableLayoutControlCollection_Add_Control_To_Specific_Cell()
286+
{
287+
using Button button = new();
288+
_collection.Add(button, 1, 1);
289+
TableLayoutPanelCellPosition position = _tableLayoutPanel.GetPositionFromControl(button);
290+
position.Column.Should().Be(1);
291+
position.Row.Should().Be(1);
292+
}
293+
294+
[Fact]
295+
public void DesignerTableLayoutControlCollection_GetChildIndex_Should_Return_Correct_Index()
296+
{
297+
using Button button1 = new();
298+
using Button button2 = new();
299+
_collection.Add(button1);
300+
_collection.Add(button2);
301+
_collection.GetChildIndex(button2).Should().Be(1);
302+
}
303+
304+
[Fact]
305+
public void DesignerTableLayoutControlCollection_SetChildIndex_Should_Update_Index()
306+
{
307+
using Button button1 = new();
308+
using Button button2 = new();
309+
_collection.Add(button1);
310+
_collection.Add(button2);
311+
_collection.SetChildIndex(button1, 1);
312+
_collection.GetChildIndex(button1).Should().Be(1);
313+
}
314+
}

0 commit comments

Comments
 (0)