Skip to content

Commit f577533

Browse files
authored
Add code coverage for MultiPropertyDescriptorGridEntry (#13713)
* Add code coverage for MultiPropertyDescriptorGridEntry * Address FeedBacks * Address FeedBacks * Address FeedBacks * Address FeedBacks
1 parent 2be1d3f commit f577533

File tree

1 file changed

+272
-0
lines changed

1 file changed

+272
-0
lines changed
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
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.Windows.Forms.PropertyGridInternal;
6+
using static System.Windows.Forms.PropertyGridInternal.GridEntry;
7+
8+
namespace System.Windows.Forms.Tests;
9+
10+
public class MultiPropertyDescriptorGridEntryTests
11+
{
12+
private sealed class DummyComponent : Component { }
13+
14+
private sealed class TestGridEntry : GridEntry
15+
{
16+
public TestGridEntry(PropertyGrid ownerGrid)
17+
: base(ownerGrid, null)
18+
{
19+
}
20+
}
21+
22+
private static MultiPropertyDescriptorGridEntry CreateEntryWithObjects(object[] objects, PropertyDescriptor[] descriptors)
23+
{
24+
using PropertyGrid ownerGrid = new();
25+
TestGridEntry parent = new(ownerGrid);
26+
27+
return new MultiPropertyDescriptorGridEntry(ownerGrid, parent, objects, descriptors, false);
28+
}
29+
30+
[Fact]
31+
public void Container_AllObjectsAreIComponentWithSameContainer_ReturnsContainer()
32+
{
33+
using Container container = new();
34+
using DummyComponent dummyComponent1 = new();
35+
using DummyComponent dummyComponent2 = new();
36+
container.Add(dummyComponent1);
37+
container.Add(dummyComponent2);
38+
39+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]];
40+
object[] objects = [dummyComponent1, dummyComponent2];
41+
42+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
43+
44+
IContainer? result = multiPropertyDescriptorGridEntry.Container;
45+
46+
result.Should().BeSameAs(container);
47+
}
48+
49+
[Fact]
50+
public void Container_ObjectsAreIComponentWithDifferentContainers_ReturnsNull()
51+
{
52+
using Container container1 = new();
53+
using Container container2 = new();
54+
using DummyComponent dummyComponent1 = new();
55+
using DummyComponent dummyComponent2 = new();
56+
container1.Add(dummyComponent1);
57+
container2.Add(dummyComponent2);
58+
59+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]];
60+
object[] objects = [dummyComponent1, dummyComponent2];
61+
62+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
63+
64+
IContainer? result = multiPropertyDescriptorGridEntry.Container;
65+
66+
result.Should().BeNull();
67+
}
68+
69+
[Fact]
70+
public void Container_ObjectsAreNotIComponent_ReturnsNull()
71+
{
72+
object[] objects = [new object(), new object()];
73+
using DummyComponent dummyComponent = new();
74+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent)[0], TypeDescriptor.GetProperties(dummyComponent)[0]];
75+
76+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
77+
78+
IContainer? result = multiPropertyDescriptorGridEntry.Container;
79+
80+
result.Should().BeNull();
81+
}
82+
83+
[Fact]
84+
public void Container_ObjectsAreIComponent_SomeWithoutSite_ReturnsNull()
85+
{
86+
using DummyComponent dummyComponent1 = new();
87+
using DummyComponent dummyComponent2 = new();
88+
using Container container = new();
89+
container.Add(dummyComponent1);
90+
91+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]];
92+
object[] objects = [dummyComponent1, dummyComponent2];
93+
94+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
95+
96+
IContainer? result = multiPropertyDescriptorGridEntry.Container;
97+
98+
result.Should().BeNull();
99+
}
100+
101+
[Fact]
102+
public void Container_EmptyObjects_ReturnsNull()
103+
{
104+
object[] objects = [];
105+
using DummyComponent dummyComponent = new();
106+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent)[0]];
107+
108+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
109+
110+
IContainer? result = multiPropertyDescriptorGridEntry.Container;
111+
112+
result.Should().BeNull();
113+
}
114+
115+
[Fact]
116+
public void Expandable_WhenFlagExpandableAndDescriptionExist_ReturnsTrue()
117+
{
118+
using Button button = new();
119+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(button)[0]];
120+
object[] objects = [button];
121+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
122+
123+
multiPropertyDescriptorGridEntry.TestAccessor().Dynamic.SetFlag(Flags.Expandable, true);
124+
125+
string? description = propertyDescriptors[0].Description;
126+
description.Should().NotBeNull();
127+
128+
bool result = multiPropertyDescriptorGridEntry.Expandable;
129+
130+
result.Should().BeTrue();
131+
}
132+
133+
[Fact]
134+
public void Expandable_ExpandableFailedFlag_ReturnsFalse()
135+
{
136+
using Button button = new();
137+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(button)[0]];
138+
object[] objects = [button];
139+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
140+
141+
multiPropertyDescriptorGridEntry.TestAccessor().Dynamic.SetFlag(Flags.ExpandableFailed, true);
142+
143+
bool result = multiPropertyDescriptorGridEntry.Expandable;
144+
145+
result.Should().BeFalse();
146+
}
147+
148+
[Fact]
149+
public void GetComponents_ReturnsCopyOfObjects()
150+
{
151+
using DummyComponent dummyComponent1 = new();
152+
using DummyComponent dummyComponent2 = new();
153+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]];
154+
object[] objects = [dummyComponent1, dummyComponent2];
155+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
156+
157+
IComponent[] result = multiPropertyDescriptorGridEntry.GetComponents();
158+
159+
result.Should().HaveCount(2);
160+
result[0].Should().BeSameAs(dummyComponent1);
161+
result[1].Should().BeSameAs(dummyComponent2);
162+
}
163+
164+
[Fact]
165+
public void GetPropertyTextValue_ValueIsNullAndMergedDescriptorReturnsNull_ReturnsEmptyString()
166+
{
167+
using DummyComponent dummyComponent1 = new();
168+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]];
169+
object[] objects = [dummyComponent1];
170+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
171+
172+
string result = multiPropertyDescriptorGridEntry.GetPropertyTextValue(null);
173+
174+
result.Should().Be("(none)");
175+
}
176+
177+
[Fact]
178+
public void SendNotification_GridEntry_CreatesTransactionAndCommits()
179+
{
180+
using DummyComponent dummyComponent1 = new();
181+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]];
182+
object[] objects = [dummyComponent1];
183+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
184+
185+
using PropertyGrid propertyGrid = new();
186+
GridEntry entryParam = new TestGridEntry(propertyGrid);
187+
188+
bool result = multiPropertyDescriptorGridEntry.SendNotification(entryParam, Notify.Reset);
189+
190+
result.Should().BeFalse();
191+
}
192+
193+
[Fact]
194+
public void NotifyParentsOfChanges_NotifiesParentProperties()
195+
{
196+
using DummyComponent dummyComponent1 = new();
197+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]];
198+
object[] objects = [dummyComponent1];
199+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
200+
201+
Action action = () => multiPropertyDescriptorGridEntry
202+
.TestAccessor()
203+
.Dynamic
204+
.NotifyParentsOfChanges(multiPropertyDescriptorGridEntry);
205+
206+
action.Should().NotThrow();
207+
}
208+
209+
[Fact]
210+
public void SendNotification_GridEntry_HandlesResetAndDoubleClick()
211+
{
212+
using DummyComponent dummyComponent1 = new();
213+
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]];
214+
object[] objects = [dummyComponent1];
215+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
216+
217+
using PropertyGrid propertyGrid = new();
218+
TestGridEntry gridEntry = new(propertyGrid);
219+
220+
bool resultReset = multiPropertyDescriptorGridEntry.SendNotification(gridEntry, Notify.Reset);
221+
bool resultDoubleClick = multiPropertyDescriptorGridEntry.SendNotification(gridEntry, Notify.DoubleClick);
222+
223+
resultReset.Should().BeFalse();
224+
resultDoubleClick.Should().BeFalse();
225+
}
226+
227+
[Fact]
228+
public void OwnersEqual_ComparesOwnersCorrectly()
229+
{
230+
object o1 = new();
231+
object o2 = new();
232+
233+
var accessor = typeof(MultiPropertyDescriptorGridEntry).TestAccessor();
234+
bool result1 = accessor.Dynamic.OwnersEqual(o1, o1);
235+
bool result2 = accessor.Dynamic.OwnersEqual(new[] { o1, o2 }, new[] { o1, o2 });
236+
bool result3 = accessor.Dynamic.OwnersEqual(new[] { o1 }, new[] { o2 });
237+
238+
result1.Should().BeTrue();
239+
result2.Should().BeTrue();
240+
result3.Should().BeFalse();
241+
}
242+
243+
[Fact]
244+
public void OnComponentChanging_CallsChangeServiceForEachObject()
245+
{
246+
using DummyComponent dummyComponent1 = new();
247+
using DummyComponent dummyComponent2 = new();
248+
PropertyDescriptor[] descriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]];
249+
object[] objects = [dummyComponent1, dummyComponent2];
250+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, descriptors);
251+
252+
bool result = multiPropertyDescriptorGridEntry.OnComponentChanging();
253+
254+
result.Should().BeTrue();
255+
}
256+
257+
[Fact]
258+
public void OnComponentChanged_DoesNothing_WhenServiceIsNull()
259+
{
260+
using DummyComponent dummyComponent1 = new();
261+
using DummyComponent dummyComponent2 = new();
262+
PropertyDescriptor[] propertyDescriptors =
263+
[
264+
TypeDescriptor.GetProperties(dummyComponent1)[0],
265+
TypeDescriptor.GetProperties(dummyComponent2)[0]
266+
];
267+
object[] objects = [dummyComponent1, dummyComponent2];
268+
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors);
269+
270+
multiPropertyDescriptorGridEntry.Invoking(e => e.OnComponentChanged()).Should().NotThrow();
271+
}
272+
}

0 commit comments

Comments
 (0)