Skip to content

Commit a3d902d

Browse files
authored
Add unit test for ListView.CheckedIndexCollection file (#13682)
Related #13442 ## Proposed changes - Add unit test file: ListView.CheckedIndexCollectionTests.cs for ListView.CheckedIndexCollection.cs file
1 parent 32489a2 commit a3d902d

File tree

1 file changed

+271
-3
lines changed

1 file changed

+271
-3
lines changed
Lines changed: 271 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,283 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
4+
using System.Collections;
55

66
namespace System.Windows.Forms.Tests;
77

8-
public class ListView_CheckedIndexCollectionTests
8+
public class ListView_CheckedIndexCollectionTests : IDisposable
99
{
10+
private readonly ListView _listView;
11+
private readonly ListView.CheckedIndexCollection _collection;
12+
13+
public ListView_CheckedIndexCollectionTests()
14+
{
15+
_listView = new ListView();
16+
_collection = new ListView.CheckedIndexCollection(_listView);
17+
}
18+
19+
public void Dispose() => _listView.Dispose();
20+
1021
[WinFormsFact]
1122
public void CheckedIndexCollection_Ctor_OwnerIsNull_ThrowsArgumentNullException()
1223
{
13-
Assert.Throws<ArgumentNullException>("owner", () => { new ListView.CheckedIndexCollection(null); });
24+
Assert.Throws<ArgumentNullException>("owner", () => new ListView.CheckedIndexCollection(null!));
25+
}
26+
27+
[WinFormsFact]
28+
public void Count_ReturnsZero_WhenCheckBoxesIsFalse()
29+
{
30+
_listView.CheckBoxes = false;
31+
_listView.Items.Add(new ListViewItem { Checked = true });
32+
33+
_collection.Count.Should().Be(0);
34+
}
35+
36+
[WinFormsFact]
37+
public void Count_ReturnsCheckedItemCount_WhenCheckBoxesIsTrue()
38+
{
39+
_listView.CheckBoxes = true;
40+
_listView.Items.AddRange([
41+
new ListViewItem { Checked = true },
42+
new ListViewItem { Checked = false },
43+
new ListViewItem { Checked = true }
44+
]);
45+
46+
_collection.Count.Should().Be(2);
47+
}
48+
49+
[WinFormsFact]
50+
public void Indexer_ReturnsCorrectIndex_ForCheckedItems()
51+
{
52+
_listView.CheckBoxes = true;
53+
_listView.Items.AddRange([
54+
new ListViewItem { Checked = false },
55+
new ListViewItem { Checked = true },
56+
new ListViewItem { Checked = true }
57+
]);
58+
59+
_collection[0].Should().Be(1);
60+
_collection[1].Should().Be(2);
61+
}
62+
63+
[WinFormsFact]
64+
public void Indexer_ThrowsArgumentOutOfRangeException_ForInvalidIndex()
65+
{
66+
_listView.CheckBoxes = true;
67+
_listView.Items.Add(new ListViewItem { Checked = true });
68+
69+
Action act = () => _ = _collection[1];
70+
71+
act.Should().Throw<ArgumentOutOfRangeException>();
72+
}
73+
74+
[WinFormsTheory]
75+
[InlineData(1, true)]
76+
[InlineData(0, false)]
77+
public void Contains_ReturnsExpected(int index, bool expected)
78+
{
79+
_listView.CheckBoxes = true;
80+
_listView.Items.AddRange([
81+
new ListViewItem { Checked = false },
82+
new ListViewItem { Checked = true }
83+
]);
84+
85+
_collection.Contains(index).Should().Be(expected);
86+
}
87+
88+
[WinFormsFact]
89+
public void IndexOf_ReturnsIndexInCheckedCollection_IfChecked()
90+
{
91+
_listView.CheckBoxes = true;
92+
_listView.Items.AddRange([
93+
new ListViewItem { Checked = false },
94+
new ListViewItem { Checked = true },
95+
new ListViewItem { Checked = true }
96+
]);
97+
98+
_collection.IndexOf(1).Should().Be(0);
99+
_collection.IndexOf(2).Should().Be(1);
100+
}
101+
102+
[WinFormsFact]
103+
public void IndexOf_ReturnsMinusOne_IfNotChecked()
104+
{
105+
_listView.CheckBoxes = true;
106+
_listView.Items.AddRange([
107+
new ListViewItem { Checked = false },
108+
new ListViewItem { Checked = true }
109+
]);
110+
111+
_collection.IndexOf(0).Should().Be(-1);
112+
}
113+
114+
[WinFormsFact]
115+
public void IsReadOnly_IsTrue()
116+
{
117+
_collection.IsReadOnly.Should().BeTrue();
118+
}
119+
120+
[WinFormsFact]
121+
public void GetEnumerator_EnumeratesCheckedIndices()
122+
{
123+
_listView.CheckBoxes = true;
124+
_listView.Items.AddRange([
125+
new ListViewItem { Checked = true },
126+
new ListViewItem { Checked = false },
127+
new ListViewItem { Checked = true }
128+
]);
129+
130+
var indices = _collection.Cast<int>().ToArray();
131+
132+
indices.Should().Equal(0, 2);
133+
}
134+
135+
[WinFormsFact]
136+
public void CopyTo_CopiesCheckedIndices()
137+
{
138+
_listView.CheckBoxes = true;
139+
_listView.Items.AddRange([
140+
new ListViewItem { Checked = true },
141+
new ListViewItem { Checked = false },
142+
new ListViewItem { Checked = true }
143+
]);
144+
int[] array = new int[2];
145+
146+
((ICollection)_collection).CopyTo(array, 0);
147+
148+
array.Should().Equal(0, 2);
149+
}
150+
151+
[WinFormsTheory]
152+
[InlineData("Add")]
153+
[InlineData("Clear")]
154+
[InlineData("Insert")]
155+
[InlineData("Remove")]
156+
[InlineData("RemoveAt")]
157+
public void IList_ModificationMethods_ThrowNotSupportedException(string method)
158+
{
159+
IList ilist = _collection;
160+
161+
Action act = method switch
162+
{
163+
"Add" => () => ilist.Add(0),
164+
"Clear" => ilist.Clear,
165+
"Insert" => () => ilist.Insert(0, 0),
166+
"Remove" => () => ilist.Remove(0),
167+
"RemoveAt" => () => ilist.RemoveAt(0),
168+
_ => throw new ArgumentOutOfRangeException(nameof(method))
169+
};
170+
171+
act.Should().Throw<NotSupportedException>();
172+
}
173+
174+
[WinFormsFact]
175+
public void IList_Indexer_Get_ReturnsCheckedIndex()
176+
{
177+
_listView.CheckBoxes = true;
178+
_listView.Items.AddRange([
179+
new ListViewItem { Checked = false },
180+
new ListViewItem { Checked = true }
181+
]);
182+
183+
IList ilist = _collection;
184+
185+
ilist[0].Should().Be(1);
186+
}
187+
188+
[WinFormsFact]
189+
public void IList_Indexer_Set_ThrowsNotSupportedException()
190+
{
191+
IList ilist = _collection;
192+
Action act = () => ilist[0] = 1;
193+
194+
act.Should().Throw<NotSupportedException>();
195+
}
196+
197+
[WinFormsFact]
198+
public void ICollection_SyncRoot_ReturnsSelf()
199+
{
200+
ListView.CheckedIndexCollection syncRoot = (ListView.CheckedIndexCollection)((ICollection)_collection).SyncRoot;
201+
syncRoot.Should().BeSameAs(_collection);
202+
}
203+
204+
[WinFormsFact]
205+
public void ICollection_IsSynchronized_IsFalse()
206+
{
207+
bool isSynchronized = ((ICollection)_collection).IsSynchronized;
208+
isSynchronized.Should().BeFalse();
209+
}
210+
211+
[WinFormsFact]
212+
public void IList_IsFixedSize_IsTrue()
213+
{
214+
bool isFixedSize = ((IList)_collection).IsFixedSize;
215+
isFixedSize.Should().BeTrue();
216+
}
217+
218+
[WinFormsTheory]
219+
[InlineData(null, false)]
220+
[InlineData("string", false)]
221+
[InlineData(1.5, false)]
222+
[InlineData(true, false)]
223+
public void IList_Contains_ReturnsFalse_ForNonIntOrNonCheckedIndex(object? value, bool expected)
224+
{
225+
_listView.CheckBoxes = true;
226+
_listView.Items.AddRange([
227+
new ListViewItem { Checked = false },
228+
new ListViewItem { Checked = true }
229+
]);
230+
231+
IList ilist = _collection;
232+
233+
ilist.Contains(value).Should().Be(expected);
234+
}
235+
236+
[WinFormsTheory]
237+
[InlineData(null, -1)]
238+
[InlineData("string", -1)]
239+
[InlineData(1.5, -1)]
240+
[InlineData(true, -1)]
241+
public void IList_IndexOf_ReturnsMinusOne_ForNonIntValues(object? value, int expected)
242+
{
243+
_listView.CheckBoxes = true;
244+
_listView.Items.AddRange([
245+
new ListViewItem { Checked = false },
246+
new ListViewItem { Checked = true }
247+
]);
248+
249+
IList ilist = _collection;
250+
251+
ilist.IndexOf(value).Should().Be(expected);
252+
}
253+
254+
[WinFormsFact]
255+
public void IList_IndexOf_ReturnsIndexInCheckedCollection_IfChecked()
256+
{
257+
_listView.CheckBoxes = true;
258+
_listView.Items.AddRange([
259+
new ListViewItem { Checked = false },
260+
new ListViewItem { Checked = true },
261+
new ListViewItem { Checked = true }
262+
]);
263+
264+
IList ilist = _collection;
265+
266+
ilist.IndexOf(1).Should().Be(0);
267+
ilist.IndexOf(2).Should().Be(1);
268+
}
269+
270+
[WinFormsFact]
271+
public void IList_IndexOf_ReturnsMinusOne_IfIntIndexIsNotChecked()
272+
{
273+
_listView.CheckBoxes = true;
274+
_listView.Items.AddRange([
275+
new ListViewItem { Checked = false },
276+
new ListViewItem { Checked = true }
277+
]);
278+
279+
IList ilist = _collection;
280+
281+
ilist.IndexOf(0).Should().Be(-1);
14282
}
15283
}

0 commit comments

Comments
 (0)