Skip to content

Commit d5b62a7

Browse files
authored
Add code coverage for DataGridViewComboBoxCell (#13654)
* Add code coverage for DataGridViewComboBoxCell * Address FeedBacks
1 parent a3d902d commit d5b62a7

File tree

1 file changed

+358
-0
lines changed

1 file changed

+358
-0
lines changed
Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
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.Drawing;
6+
7+
namespace System.Windows.Forms.Tests;
8+
9+
public class DataGridViewComboBoxCellTests : IDisposable
10+
{
11+
private readonly DataGridViewComboBoxCell _dataGridViewComboBoxCell;
12+
13+
public void Dispose() => _dataGridViewComboBoxCell.Dispose();
14+
15+
public DataGridViewComboBoxCellTests() =>
16+
_dataGridViewComboBoxCell = new();
17+
18+
[Fact]
19+
public void AutoComplete_DefaultValue_IsTrue() =>
20+
_dataGridViewComboBoxCell.AutoComplete.Should().BeTrue();
21+
22+
[Fact]
23+
public void AutoComplete_SetFalse_ChangesValue()
24+
{
25+
_dataGridViewComboBoxCell.AutoComplete = false;
26+
_dataGridViewComboBoxCell.AutoComplete.Should().BeFalse();
27+
}
28+
29+
[WinFormsFact]
30+
public void DataSource_DefaultValue_IsNull() =>
31+
_dataGridViewComboBoxCell.DataSource.Should().BeNull();
32+
33+
[WinFormsFact]
34+
public void DataSource_SetToIList_StoresReference()
35+
{
36+
IList<string> list = ["a", "b"];
37+
_dataGridViewComboBoxCell.DataSource = list;
38+
_dataGridViewComboBoxCell.DataSource.Should().BeSameAs(list);
39+
}
40+
41+
[WinFormsFact]
42+
public void DataSource_SetToIListSource_StoresReference()
43+
{
44+
using BindingSource bindingSource = new();
45+
_dataGridViewComboBoxCell.DataSource = bindingSource;
46+
47+
_dataGridViewComboBoxCell.DataSource.Should().BeSameAs(bindingSource);
48+
}
49+
50+
[WinFormsFact]
51+
public void DataSource_SetToInvalidType_Throws()
52+
{
53+
Exception? invalidDataSourceException = Record.Exception(() => _dataGridViewComboBoxCell.DataSource = 123);
54+
55+
invalidDataSourceException.Should().BeOfType<ArgumentException>();
56+
}
57+
58+
[WinFormsFact]
59+
public void DataSource_SetToNull_ClearsDisplayAndValueMember()
60+
{
61+
_dataGridViewComboBoxCell.DataSource = new List<string> { "a" };
62+
_dataGridViewComboBoxCell.DisplayMember = "Name";
63+
_dataGridViewComboBoxCell.ValueMember = "Id";
64+
_dataGridViewComboBoxCell.DataSource = null;
65+
66+
_dataGridViewComboBoxCell.DataSource.Should().BeNull();
67+
_dataGridViewComboBoxCell.DisplayMember.Should().BeEmpty();
68+
_dataGridViewComboBoxCell.ValueMember.Should().BeEmpty();
69+
}
70+
71+
[WinFormsFact]
72+
public void DisplayStyleForCurrentCellOnly_DefaultValue_IsFalse() =>
73+
_dataGridViewComboBoxCell.DisplayStyleForCurrentCellOnly.Should().BeFalse();
74+
75+
[Fact]
76+
public void DropDownWidth_DefaultValue_IsOne() =>
77+
_dataGridViewComboBoxCell.DropDownWidth.Should().Be(1);
78+
79+
[Theory]
80+
[InlineData(5)]
81+
[InlineData(100)]
82+
public void DropDownWidth_SetValidValue_ChangesValue(int width)
83+
{
84+
_dataGridViewComboBoxCell.DropDownWidth = width;
85+
_dataGridViewComboBoxCell.DropDownWidth.Should().Be(width);
86+
}
87+
88+
[Theory]
89+
[InlineData(0)]
90+
[InlineData(-5)]
91+
public void DropDownWidth_SetInvalidValue_Throws(int width)
92+
{
93+
Exception? dropDownWidthOutOfRangeException = Record.Exception(() => _dataGridViewComboBoxCell.DropDownWidth = width);
94+
95+
dropDownWidthOutOfRangeException.Should().BeOfType<ArgumentOutOfRangeException>();
96+
}
97+
98+
[Fact]
99+
public void FlatStyle_DefaultValue_IsStandard() =>
100+
_dataGridViewComboBoxCell.FlatStyle.Should().Be(FlatStyle.Standard);
101+
102+
[WinFormsTheory]
103+
[InlineData(FlatStyle.Flat)]
104+
[InlineData(FlatStyle.Popup)]
105+
[InlineData(FlatStyle.System)]
106+
[InlineData(FlatStyle.Standard)]
107+
public void FlatStyle_SetValidValue_ChangesValue(FlatStyle style)
108+
{
109+
_dataGridViewComboBoxCell.FlatStyle = style;
110+
_dataGridViewComboBoxCell.FlatStyle.Should().Be(style);
111+
}
112+
113+
[Fact]
114+
public void MaxDropDownItems_DefaultValue_IsEight() =>
115+
_dataGridViewComboBoxCell.MaxDropDownItems.Should().Be(DataGridViewComboBoxCell.DefaultMaxDropDownItems);
116+
117+
[Theory]
118+
[InlineData(1)]
119+
[InlineData(50)]
120+
[InlineData(100)]
121+
public void MaxDropDownItems_SetValidValue_ChangesValue(int value)
122+
{
123+
_dataGridViewComboBoxCell.MaxDropDownItems = value;
124+
_dataGridViewComboBoxCell.MaxDropDownItems.Should().Be(value);
125+
}
126+
127+
[Theory]
128+
[InlineData(0)]
129+
[InlineData(-1)]
130+
[InlineData(101)]
131+
[InlineData(200)]
132+
public void MaxDropDownItems_SetInvalidValue_Throws(int value)
133+
{
134+
Exception? maxDropDownItemsOutOfRangeException = Record.Exception(() => _dataGridViewComboBoxCell.MaxDropDownItems = value);
135+
136+
maxDropDownItemsOutOfRangeException.Should().BeOfType<ArgumentOutOfRangeException>();
137+
}
138+
139+
[Fact]
140+
public void Sorted_DefaultValue_IsFalse() =>
141+
_dataGridViewComboBoxCell.Sorted.Should().BeFalse();
142+
143+
[Fact]
144+
public void Sorted_SetTrue_WithoutDataSource_SetsFlag()
145+
{
146+
_dataGridViewComboBoxCell.Sorted = true;
147+
_dataGridViewComboBoxCell.Sorted.Should().BeTrue();
148+
}
149+
150+
[WinFormsFact]
151+
public void Sorted_SetTrue_WithDataSource_Throws()
152+
{
153+
_dataGridViewComboBoxCell.DataSource = new List<string> { "a", "b" };
154+
155+
Exception? sortedWithDataSourceException = Record.Exception(() => _dataGridViewComboBoxCell.Sorted = true);
156+
157+
sortedWithDataSourceException.Should().BeOfType<ArgumentException>();
158+
}
159+
160+
[Fact]
161+
public void ValueMember_DefaultValue_IsEmpty() =>
162+
_dataGridViewComboBoxCell.ValueMember.Should().BeEmpty();
163+
164+
[WinFormsTheory]
165+
[InlineData("Id")]
166+
[InlineData("Name")]
167+
[InlineData("")]
168+
public void ValueMember_SetValue_ChangesValue(string value)
169+
{
170+
_dataGridViewComboBoxCell.ValueMember = value;
171+
_dataGridViewComboBoxCell.ValueMember.Should().Be(value);
172+
}
173+
174+
[Fact]
175+
public void ValueType_DefaultValue_IsObject() =>
176+
_dataGridViewComboBoxCell.ValueType.Should().Be(typeof(object));
177+
178+
private class StubPropertyDescriptor : PropertyDescriptor
179+
{
180+
private readonly Type _propertyType;
181+
182+
public StubPropertyDescriptor(string name, Type propertyType)
183+
: base(name, null)
184+
{
185+
_propertyType = propertyType;
186+
}
187+
188+
public override Type PropertyType => _propertyType;
189+
public override void SetValue(object? component, object? value) { }
190+
public override object? GetValue(object? component) => null!;
191+
public override bool CanResetValue(object? component) => false;
192+
public override void ResetValue(object? component) { }
193+
public override bool IsReadOnly => true;
194+
public override Type ComponentType => typeof(object);
195+
public override bool ShouldSerializeValue(object? component) => false;
196+
}
197+
198+
[Fact]
199+
public void ValueType_WithDisplayMemberProperty_ReturnsPropertyType()
200+
{
201+
StubPropertyDescriptor prop = new("Name", typeof(string));
202+
203+
_dataGridViewComboBoxCell.TestAccessor().Dynamic.DisplayMemberProperty = prop;
204+
_dataGridViewComboBoxCell.TestAccessor().Dynamic.ValueMemberProperty = null;
205+
206+
_dataGridViewComboBoxCell.ValueType.Should().Be(typeof(string));
207+
}
208+
209+
[Fact]
210+
public void GetContentBounds_ReturnsEmpty_WhenDataGridViewIsNull()
211+
{
212+
using Bitmap bmp = new(10, 10);
213+
using Graphics g = Graphics.FromImage(bmp);
214+
DataGridViewCellStyle style = new();
215+
216+
Rectangle result = _dataGridViewComboBoxCell.TestAccessor().Dynamic.GetContentBounds(g, style, 0);
217+
218+
result.Should().Be(Rectangle.Empty);
219+
}
220+
221+
[Fact]
222+
public void GetContentBounds_ReturnsEmpty_WhenRowIndexIsNegative()
223+
{
224+
using DataGridView dataGridView = new();
225+
using DataGridViewComboBoxColumn column = new() { CellTemplate = _dataGridViewComboBoxCell };
226+
dataGridView.Columns.Add(column);
227+
using Bitmap bmp = new(10, 10);
228+
using Graphics g = Graphics.FromImage(bmp);
229+
DataGridViewCellStyle style = new();
230+
231+
Rectangle result = _dataGridViewComboBoxCell.TestAccessor().Dynamic.GetContentBounds(g, style, -1);
232+
233+
result.Should().Be(Rectangle.Empty);
234+
}
235+
236+
[Fact]
237+
public void GetErrorIconBounds_ReturnsEmpty_WhenDataGridViewIsNull()
238+
{
239+
using Bitmap bmp = new(10, 10);
240+
using Graphics g = Graphics.FromImage(bmp);
241+
DataGridViewCellStyle style = new();
242+
243+
Rectangle result = _dataGridViewComboBoxCell.TestAccessor().Dynamic.GetErrorIconBounds(g, style, 0);
244+
245+
result.Should().Be(Rectangle.Empty);
246+
}
247+
248+
[Fact]
249+
public void GetErrorIconBounds_UsingTestAccessor_ReturnsEmpty_WhenRowIndexIsNegative()
250+
{
251+
using DataGridView dataGridView = new();
252+
using DataGridViewComboBoxColumn column = new() { CellTemplate = _dataGridViewComboBoxCell };
253+
dataGridView.Columns.Add(column);
254+
using Bitmap bmp = new(10, 10);
255+
using Graphics g = Graphics.FromImage(bmp);
256+
DataGridViewCellStyle style = new();
257+
258+
Rectangle result = _dataGridViewComboBoxCell.TestAccessor().Dynamic.GetErrorIconBounds(g, style, -1);
259+
260+
result.Should().Be(Rectangle.Empty);
261+
}
262+
263+
[Fact]
264+
public void GetPreferredSize_ReturnsMinusOne_WhenDataGridViewIsNull()
265+
{
266+
using Bitmap bmp = new(10, 10);
267+
using Graphics g = Graphics.FromImage(bmp);
268+
DataGridViewCellStyle style = new();
269+
270+
Size result = _dataGridViewComboBoxCell.TestAccessor().Dynamic.GetPreferredSize(
271+
g,
272+
style,
273+
0,
274+
new Size(100, 20));
275+
276+
result.Should().Be(new Size(-1, -1));
277+
}
278+
279+
[Theory]
280+
[InlineData(Keys.A, false, false, false, true)]
281+
[InlineData(Keys.F4, false, false, false, true)]
282+
[InlineData(Keys.Space, false, false, false, true)]
283+
[InlineData(Keys.Space, true, false, false, false)]
284+
[InlineData(Keys.Down, false, true, false, true)]
285+
[InlineData(Keys.Up, false, true, false, true)]
286+
[InlineData(Keys.Down, false, false, false, false)]
287+
[InlineData(Keys.F1, false, false, false, false)]
288+
[InlineData(Keys.F24, false, false, false, false)]
289+
[InlineData(Keys.A, false, false, true, false)]
290+
public void KeyEntersEditMode_ReturnsExpected(Keys key, bool shift, bool alt, bool control, bool expected)
291+
{
292+
Keys keyData = key;
293+
if (shift)
294+
{
295+
keyData |= Keys.Shift;
296+
}
297+
298+
if (alt)
299+
{
300+
keyData |= Keys.Alt;
301+
}
302+
303+
if (control)
304+
{
305+
keyData |= Keys.Control;
306+
}
307+
308+
KeyEventArgs e = new(keyData);
309+
bool result = _dataGridViewComboBoxCell.KeyEntersEditMode(e);
310+
result.Should().Be(expected);
311+
}
312+
313+
[Fact]
314+
public void ParseFormattedValue_UsesValueTypeConverter_WhenValueTypeConverterIsProvided()
315+
{
316+
using DataGridView dataGridView = new();
317+
using DataGridViewComboBoxColumn column = new() { CellTemplate = _dataGridViewComboBoxCell };
318+
dataGridView.Columns.Add(column);
319+
DataGridViewCellStyle style = new();
320+
321+
object? result = _dataGridViewComboBoxCell.ParseFormattedValue(
322+
"test",
323+
style,
324+
null,
325+
TypeDescriptor.GetConverter(typeof(string)));
326+
327+
result.Should().Be("test");
328+
}
329+
330+
[Fact]
331+
public void ParseFormattedValue_UsesValueMemberPropertyConverter_WhenValueTypeConverterIsNull()
332+
{
333+
using DataGridView dataGridView = new();
334+
using DataGridViewComboBoxColumn column = new() { CellTemplate = _dataGridViewComboBoxCell };
335+
dataGridView.Columns.Add(column);
336+
DataGridViewCellStyle style = new();
337+
338+
StubPropertyDescriptor prop = new("Id", typeof(string));
339+
_dataGridViewComboBoxCell.TestAccessor().Dynamic.ValueMemberProperty = prop;
340+
341+
object? result = _dataGridViewComboBoxCell.ParseFormattedValue("abc", style, null, null);
342+
343+
result.Should().Be("abc");
344+
}
345+
346+
[Fact]
347+
public void ParseFormattedValue_UsesParseFormattedValueInternal_WithValueType_WhenNoDataManagerOrMembers()
348+
{
349+
using DataGridView dataGridView = new();
350+
using DataGridViewComboBoxColumn column = new() { CellTemplate = _dataGridViewComboBoxCell };
351+
dataGridView.Columns.Add(column);
352+
DataGridViewCellStyle style = new();
353+
354+
object? result = _dataGridViewComboBoxCell.ParseFormattedValue("test", style, null, null);
355+
356+
result.Should().Be("test");
357+
}
358+
}

0 commit comments

Comments
 (0)