-
Notifications
You must be signed in to change notification settings - Fork 1k
Add code coverage for MultiPropertyDescriptorGridEntry #13713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ricardobossan
merged 5 commits into
dotnet:main
from
Zheng-Li01:Add_code_coverage_for_MultiPropertyDescriptorGridEntry
Jul 29, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
266 changes: 266 additions & 0 deletions
266
...t/unit/System.Windows.Forms/System/Windows/Forms/MultiPropertyDescriptorGridEntryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
using System.Windows.Forms.PropertyGridInternal; | ||
using static System.Windows.Forms.PropertyGridInternal.GridEntry; | ||
|
||
namespace System.Windows.Forms.Tests; | ||
|
||
public class MultiPropertyDescriptorGridEntryTests | ||
{ | ||
private sealed class DummyComponent : Component { } | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private sealed class TestGridEntry : GridEntry | ||
{ | ||
public TestGridEntry(PropertyGrid ownerGrid) | ||
: base(ownerGrid, null) | ||
{ | ||
} | ||
} | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static MultiPropertyDescriptorGridEntry CreateEntryWithObjects(object[] objects, PropertyDescriptor[] descriptors) | ||
{ | ||
PropertyGrid ownerGrid = new(); | ||
TestGridEntry parent = new(ownerGrid); | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bool hide = false; | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new MultiPropertyDescriptorGridEntry(ownerGrid, parent, objects, descriptors, hide); | ||
} | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[Fact] | ||
public void Container_AllObjectsAreIComponentWithSameContainer_ReturnsContainer() | ||
{ | ||
Container container = new(); | ||
DummyComponent dummyComponent1 = new(); | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DummyComponent dummyComponent2 = new(); | ||
container.Add(dummyComponent1); | ||
container.Add(dummyComponent2); | ||
|
||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
|
||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
IContainer? result = multiPropertyDescriptorGridEntry.Container; | ||
|
||
result.Should().BeSameAs(container); | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[Fact] | ||
public void Container_ObjectsAreIComponentWithDifferentContainers_ReturnsNull() | ||
{ | ||
Container container1 = new(); | ||
Container container2 = new(); | ||
DummyComponent dummyComponent1 = new(); | ||
DummyComponent dummyComponent2 = new(); | ||
container1.Add(dummyComponent1); | ||
container2.Add(dummyComponent2); | ||
|
||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
|
||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
IContainer? result = multiPropertyDescriptorGridEntry.Container; | ||
|
||
result.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Container_ObjectsAreNotIComponent_ReturnsNull() | ||
{ | ||
object[] objects = [new object(), new object()]; | ||
DummyComponent dummyComponent = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent)[0], TypeDescriptor.GetProperties(dummyComponent)[0]]; | ||
|
||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
IContainer? result = multiPropertyDescriptorGridEntry.Container; | ||
|
||
result.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Container_ObjectsAreIComponent_SomeWithoutSite_ReturnsNull() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
DummyComponent dummyComponent2 = new(); | ||
Container container = new(); | ||
container.Add(dummyComponent1); | ||
|
||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
|
||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
IContainer? result = multiPropertyDescriptorGridEntry.Container; | ||
|
||
result.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Container_EmptyObjects_ReturnsNull() | ||
{ | ||
object[] objects = []; | ||
DummyComponent dummyComponent = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent)[0]]; | ||
|
||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
IContainer? result = multiPropertyDescriptorGridEntry.Container; | ||
|
||
result.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Expandable_FlagExpandableAndHasChildren_ReturnsTrue() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
DummyComponent dummyComponent2 = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
multiPropertyDescriptorGridEntry.TestAccessor().Dynamic.SetFlag(Flags.Expandable, true); | ||
|
||
PropertyGrid ownerGrid = new(); | ||
multiPropertyDescriptorGridEntry.TestAccessor().Dynamic.ChildCollection.Add(new TestGridEntry(ownerGrid)); | ||
|
||
bool result = multiPropertyDescriptorGridEntry.Expandable; | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void Expandable_ExpandableFailedFlag_ReturnsFalse() | ||
{ | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DummyComponent dummyComponent1 = new(); | ||
DummyComponent dummyComponent2 = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
multiPropertyDescriptorGridEntry.TestAccessor().Dynamic.SetFlag(Flags.ExpandableFailed, true); | ||
|
||
bool result = multiPropertyDescriptorGridEntry.Expandable; | ||
|
||
result.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void GetComponents_ReturnsCopyOfObjects() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
DummyComponent dummyComponent2 = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
IComponent[] result = multiPropertyDescriptorGridEntry.GetComponents(); | ||
|
||
result.Should().HaveCount(2); | ||
result[0].Should().BeSameAs(dummyComponent1); | ||
result[1].Should().BeSameAs(dummyComponent2); | ||
} | ||
|
||
[Fact] | ||
public void GetPropertyTextValue_ValueIsNullAndMergedDescriptorReturnsNull_ReturnsEmptyString() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]]; | ||
object[] objects = [dummyComponent1]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
string result = multiPropertyDescriptorGridEntry.GetPropertyTextValue(null); | ||
|
||
result.Should().Be("(none)"); | ||
} | ||
|
||
[Fact] | ||
public void SendNotification_GridEntry_CreatesTransactionAndCommits() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]]; | ||
object[] objects = [dummyComponent1]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
GridEntry entryParam = new TestGridEntry(new PropertyGrid()); | ||
|
||
bool result = multiPropertyDescriptorGridEntry.SendNotification(entryParam, Notify.Reset); | ||
|
||
result.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void NotifyParentsOfChanges_NotifiesParentProperties() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]]; | ||
object[] objects = [dummyComponent1]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
multiPropertyDescriptorGridEntry.TestAccessor().Dynamic.NotifyParentsOfChanges(multiPropertyDescriptorGridEntry); | ||
} | ||
|
||
[Fact] | ||
public void SendNotification_GridEntry_HandlesResetAndDoubleClick() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
PropertyDescriptor[] propertyDescriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0]]; | ||
object[] objects = [dummyComponent1]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
TestGridEntry gridEntry = new(new PropertyGrid()); | ||
Zheng-Li01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bool resultReset = multiPropertyDescriptorGridEntry.SendNotification(gridEntry, Notify.Reset); | ||
bool resultDoubleClick = multiPropertyDescriptorGridEntry.SendNotification(gridEntry, Notify.DoubleClick); | ||
|
||
resultReset.Should().BeFalse(); | ||
resultDoubleClick.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void OwnersEqual_ComparesOwnersCorrectly() | ||
{ | ||
object o1 = new(); | ||
object o2 = new(); | ||
|
||
var accessor = typeof(MultiPropertyDescriptorGridEntry).TestAccessor(); | ||
bool result1 = accessor.Dynamic.OwnersEqual(o1, o1); | ||
bool result2 = accessor.Dynamic.OwnersEqual(new[] { o1, o2 }, new[] { o1, o2 }); | ||
bool result3 = accessor.Dynamic.OwnersEqual(new[] { o1 }, new[] { o2 }); | ||
|
||
result1.Should().BeTrue(); | ||
result2.Should().BeTrue(); | ||
result3.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void OnComponentChanging_CallsChangeServiceForEachObject() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
DummyComponent dummyComponent2 = new(); | ||
PropertyDescriptor[] descriptors = [TypeDescriptor.GetProperties(dummyComponent1)[0], TypeDescriptor.GetProperties(dummyComponent2)[0]]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, descriptors); | ||
|
||
bool result = multiPropertyDescriptorGridEntry.OnComponentChanging(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void OnComponentChanged_DoesNothing_WhenServiceIsNull() | ||
{ | ||
DummyComponent dummyComponent1 = new(); | ||
DummyComponent dummyComponent2 = new(); | ||
PropertyDescriptor[] propertyDescriptors = | ||
[ | ||
TypeDescriptor.GetProperties(dummyComponent1)[0], | ||
TypeDescriptor.GetProperties(dummyComponent2)[0] | ||
]; | ||
object[] objects = [dummyComponent1, dummyComponent2]; | ||
MultiPropertyDescriptorGridEntry multiPropertyDescriptorGridEntry = CreateEntryWithObjects(objects, propertyDescriptors); | ||
|
||
multiPropertyDescriptorGridEntry.Invoking(e => e.OnComponentChanged()).Should().NotThrow(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.