Skip to content

Commit 2be1d3f

Browse files
authored
Add unit test for ListViewItemStateImageIndexConverter file (#13716)
* Add unit test for ListViewItemStateImageIndexConverter file * handle feedback
1 parent 21f0dbc commit 2be1d3f

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

src/test/unit/System.Windows.Forms/System/Windows/Forms/ListViewItemStateImageIndexConverterTests.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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
5-
4+
using System.ComponentModel;
5+
using System.Drawing;
66
using static System.ComponentModel.TypeConverter;
77

88
namespace System.Windows.Forms.Tests;
@@ -25,4 +25,63 @@ public void ListViewItemStateImageIndexConverter_GetStandardValues_Null_Context_
2525

2626
Assert.Empty(result);
2727
}
28+
29+
[Fact]
30+
public void GetStandardValues_ContextWithoutListView_ReturnsEmpty()
31+
{
32+
object instance = new();
33+
ITypeDescriptorContext context = new TypeDescriptorContextStub(instance);
34+
ListViewItemStateImageIndexConverter converter = new();
35+
36+
StandardValuesCollection result = converter.GetStandardValues(context);
37+
38+
result.Cast<object>().Should().BeEmpty();
39+
}
40+
41+
[Fact]
42+
public void GetStandardValues_WithListViewWithoutStateImageList_ReturnsEmpty()
43+
{
44+
using ListView listView = new();
45+
ListViewItem item = listView.Items.Add("test");
46+
47+
ITypeDescriptorContext context = new TypeDescriptorContextStub(item);
48+
ListViewItemStateImageIndexConverter converter = new();
49+
50+
StandardValuesCollection result = converter.GetStandardValues(context);
51+
52+
result.Cast<object>().Should().BeEmpty();
53+
}
54+
55+
[WinFormsFact]
56+
public void GetStandardValues_WithStateImageList_ReturnsCorrectIndexes()
57+
{
58+
using ListView listView = new();
59+
using ImageList imageList = new();
60+
using Bitmap bmp1 = new(16, 16);
61+
using Bitmap bmp2 = new(16, 16);
62+
imageList.Images.Add(bmp1);
63+
imageList.Images.Add(bmp2);
64+
listView.StateImageList = imageList;
65+
66+
ListViewItem item = listView.Items.Add("test");
67+
68+
ITypeDescriptorContext context = new TypeDescriptorContextStub(item);
69+
ListViewItemStateImageIndexConverter converter = new();
70+
71+
StandardValuesCollection result = converter.GetStandardValues(context);
72+
73+
result.Cast<object>().Should().Equal(0, 1);
74+
}
75+
76+
// Minimal stub for ITypeDescriptorContext
77+
private sealed class TypeDescriptorContextStub : ITypeDescriptorContext
78+
{
79+
public TypeDescriptorContextStub(object instance) => Instance = instance;
80+
public object Instance { get; }
81+
public IContainer? Container => null;
82+
public object? GetService(Type serviceType) => null;
83+
public void OnComponentChanged() { }
84+
public bool OnComponentChanging() => false;
85+
public PropertyDescriptor? PropertyDescriptor => null;
86+
}
2887
}

0 commit comments

Comments
 (0)