Skip to content

Commit c7acf1b

Browse files
authored
Add unit test for PropertyGrid.PropertyTabCollection file (#13758)
Add more test for uncovered public methods in PropertyGrid.PropertyTabCollectionTests.cs
1 parent 4d27bef commit c7acf1b

File tree

1 file changed

+107
-17
lines changed

1 file changed

+107
-17
lines changed

src/test/unit/System.Windows.Forms/System/Windows/Forms/PropertyGrid.PropertyTabCollectionTests.cs

Lines changed: 107 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@
33

44
#nullable disable
55

6+
using System.Collections;
67
using System.ComponentModel;
78
using System.Drawing;
89
using System.Windows.Forms.Design;
910

1011
namespace System.Windows.Forms.Tests;
1112

12-
public class PropertyTabCollectionTests
13+
public class PropertyTabCollectionTests : IDisposable
1314
{
15+
private readonly PropertyGrid _propertyGrid;
16+
private readonly PropertyGrid.PropertyTabCollection _propertyTabCollection;
17+
18+
public PropertyTabCollectionTests()
19+
{
20+
_propertyGrid = new PropertyGrid();
21+
_propertyTabCollection = new PropertyGrid.PropertyTabCollection(_propertyGrid);
22+
}
23+
24+
public void Dispose()
25+
{
26+
_propertyGrid.Dispose();
27+
}
28+
1429
[WinFormsTheory]
1530
[InlineData(typeof(PropertyGrid))]
1631
public void Count_ReturnsCorrectCount(Type ownerType)
1732
{
18-
var owner = Activator.CreateInstance(ownerType) as PropertyGrid;
33+
PropertyGrid owner = Activator.CreateInstance(ownerType) as PropertyGrid;
1934
TestPropertyTabCollection propertyTabCollection = new(owner);
2035
propertyTabCollection.Count.Should().Be(1); // PropertyGrid initially contains one PropertiesTab
2136
}
@@ -25,11 +40,11 @@ public void Count_ReturnsCorrectCount(Type ownerType)
2540
[InlineData(typeof(PropertyGrid), 1, typeof(TestPropertyTab))]
2641
public void Indexer_ReturnsCorrectTab(Type ownerType, int index, Type expectedTabType)
2742
{
28-
var owner = Activator.CreateInstance(ownerType) as PropertyGrid;
43+
PropertyGrid owner = Activator.CreateInstance(ownerType) as PropertyGrid;
2944
TestPropertyTabCollection propertyTabCollection = new(owner);
3045
propertyTabCollection.AddTabType(typeof(TestPropertyTab));
3146

32-
var tab = propertyTabCollection[index];
47+
PropertyTab tab = propertyTabCollection[index];
3348
tab.Should().BeOfType(expectedTabType);
3449
}
3550

@@ -38,7 +53,7 @@ public void Indexer_ReturnsCorrectTab(Type ownerType, int index, Type expectedTa
3853
[InlineData(typeof(PropertyGrid), typeof(TestPropertyTab), 2, true)]
3954
public void AddTabType_WithDifferentInputs(Type ownerType, Type tabType, int expectedCount, bool addTwice)
4055
{
41-
var owner = Activator.CreateInstance(ownerType) as PropertyGrid;
56+
PropertyGrid owner = Activator.CreateInstance(ownerType) as PropertyGrid;
4257
TestPropertyTabCollection propertyTabCollection = new(owner);
4358
int initialCount = propertyTabCollection.Count;
4459

@@ -55,6 +70,92 @@ public void AddTabType_WithDifferentInputs(Type ownerType, Type tabType, int exp
5570
}
5671
}
5772

73+
[WinFormsFact]
74+
public void AddTabType_AddsTab()
75+
{
76+
int initialCount = _propertyTabCollection.Count;
77+
_propertyTabCollection.AddTabType(typeof(TestPropertyTab));
78+
79+
_propertyTabCollection.Count.Should().Be(initialCount + 1);
80+
_propertyTabCollection[initialCount].Should().BeOfType<TestPropertyTab>();
81+
}
82+
83+
[WinFormsFact]
84+
public void AddTabType_WithScope_AddsTab()
85+
{
86+
int initialCount = _propertyTabCollection.Count;
87+
_propertyTabCollection.AddTabType(typeof(TestPropertyTab), PropertyTabScope.Component);
88+
89+
_propertyTabCollection.Count.Should().Be(initialCount + 1);
90+
_propertyTabCollection[initialCount].Should().BeOfType<TestPropertyTab>();
91+
}
92+
93+
[WinFormsFact]
94+
public void RemoveTabType_RemovesTab()
95+
{
96+
_propertyTabCollection.AddTabType(typeof(TestPropertyTab));
97+
int countAfterAdd = _propertyTabCollection.Count;
98+
99+
_propertyTabCollection.RemoveTabType(typeof(TestPropertyTab));
100+
101+
_propertyTabCollection.Count.Should().Be(countAfterAdd - 1);
102+
_propertyTabCollection.Cast<PropertyTab>().Should().NotContain(tab => tab is TestPropertyTab);
103+
}
104+
105+
[WinFormsFact]
106+
public void Clear_RemovesTabsOfGivenScope()
107+
{
108+
_propertyTabCollection.AddTabType(typeof(TestPropertyTab), PropertyTabScope.Component);
109+
int countAfterAdd = _propertyTabCollection.Count;
110+
111+
_propertyTabCollection.Clear(PropertyTabScope.Component);
112+
113+
_propertyTabCollection.Count.Should().BeLessThan(countAfterAdd);
114+
}
115+
116+
[WinFormsFact]
117+
public void CopyTo_CopiesTabsToArray()
118+
{
119+
_propertyTabCollection.AddTabType(typeof(TestPropertyTab));
120+
PropertyTab[] array = new PropertyTab[_propertyTabCollection.Count];
121+
122+
((ICollection)_propertyTabCollection).CopyTo(array, 0);
123+
124+
array.Should().ContainItemsAssignableTo<PropertyTab>();
125+
array.Should().Contain(tab => tab is TestPropertyTab);
126+
}
127+
128+
[WinFormsFact]
129+
public void GetEnumerator_EnumeratesTabs()
130+
{
131+
_propertyTabCollection.AddTabType(typeof(TestPropertyTab));
132+
133+
int count = 0;
134+
foreach (PropertyTab tab in _propertyTabCollection)
135+
{
136+
tab.Should().NotBeNull();
137+
count++;
138+
}
139+
140+
count.Should().Be(_propertyTabCollection.Count);
141+
}
142+
143+
[WinFormsFact]
144+
public void SyncRoot_ReturnsSelf()
145+
{
146+
object syncRoot = ((ICollection)_propertyTabCollection).SyncRoot;
147+
148+
syncRoot.Should().BeSameAs(_propertyTabCollection);
149+
}
150+
151+
[WinFormsFact]
152+
public void IsSynchronized_ReturnsFalse()
153+
{
154+
bool isSynchronized = ((ICollection)_propertyTabCollection).IsSynchronized;
155+
156+
isSynchronized.Should().BeFalse();
157+
}
158+
58159
public class TestPropertyTabCollection : PropertyGrid.PropertyTabCollection
59160
{
60161
public TestPropertyTabCollection(PropertyGrid ownerPropertyGrid) : base(ownerPropertyGrid)
@@ -72,18 +173,7 @@ public class TestPropertyTab : PropertyTab, IDisposable
72173

73174
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attributes) => TypeDescriptor.GetProperties(component, attributes);
74175

75-
public override Bitmap Bitmap
76-
{
77-
get
78-
{
79-
if (_bitmap is null)
80-
{
81-
_bitmap = new Bitmap(1, 1);
82-
}
83-
84-
return _bitmap;
85-
}
86-
}
176+
public override Bitmap Bitmap => _bitmap ??= new(1, 1);
87177

88178
public override void Dispose()
89179
{

0 commit comments

Comments
 (0)