Skip to content

Adds tests to ExpandAllGridItems and CollapseAllGridItems#14601

Open
ricardobossan wants to merge 1 commit into
dotnet:mainfrom
ricardobossan:Issue_12055_Add_Code_Coverage_For_Collapse_Expand_All_Grid_Items
Open

Adds tests to ExpandAllGridItems and CollapseAllGridItems#14601
ricardobossan wants to merge 1 commit into
dotnet:mainfrom
ricardobossan:Issue_12055_Add_Code_Coverage_For_Collapse_Expand_All_Grid_Items

Conversation

@ricardobossan
Copy link
Copy Markdown
Member

@ricardobossan ricardobossan commented Jun 2, 2026

Related to #12055

Root Cause

  • Not a bug. PropertyGrid.ExpandAllGridItems and PropertyGrid.CollapseAllGridItems had no unit test coverage, contributing to the PropertyGrid's overall coverage gap (~49.91%).

Proposed changes

  • Add two unit tests to PropertyGridTests.cs:
    • PropertyGrid_ExpandAllGridItems_ExpandsNestedGridItems
    • PropertyGrid_CollapseAllGridItems_CollapsesNestedGridItems
  • Both reuse the existing MyClass fixture (it already exposes an [ExpandableObjectConverter] nested property), set it as SelectedObject, then assert the nested entry's Expanded state flips as expected.
  • Add a small FindExpandableProperty test helper that walks to the root grid item and returns the first expandable property entry.

Customer Impact

  • None

Regression?

  • No

Risk

  • Minimal

Test methodology

  • Unit tests

Test environment(s)

  • 11.0.100-preview.5.26227.104
Microsoft Reviewers: Open in CodeFlow

- Not a bug. `PropertyGrid.ExpandAllGridItems` and `PropertyGrid.CollapseAllGridItems` had no unit test coverage, contributing to the PropertyGrid's overall coverage gap (~49.91%).

- Add two unit tests to `PropertyGridTests.cs`:
  - `PropertyGrid_ExpandAllGridItems_ExpandsNestedGridItems`
  - `PropertyGrid_CollapseAllGridItems_CollapsesNestedGridItems`
- Both reuse the existing `MyClass` fixture (it already exposes an `[ExpandableObjectConverter]` nested property), set it as `SelectedObject`, then assert the nested entry's `Expanded` state flips as expected.
- Add a small `FindExpandableProperty` test helper that walks to the root grid item and returns the first expandable property entry.

- None

- No

- Minimal

- Unit tests

- 11.0.100-preview.5.26227.104
@SimonZhao888
Copy link
Copy Markdown
Member

Please fix the failed test cases first.

using PropertyGrid propertyGrid = new();
propertyGrid.SelectedObject = new MyClass();

GridItem nested = FindExpandableProperty(propertyGrid.SelectedGridItem);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't assert the initial state before calling ExpandAllGridItems(). If nested.Expanded is already true by default (e.g., due to a future change in PropertyGrid initialization or the MyClass fixture), the test would pass vacuously without verifying that ExpandAllGridItems actually did anything.

Consider adding:

nested.Expanded.Should().BeFalse(); // Precondition: not yet expanded
propertyGrid.ExpandAllGridItems();
nested.Expanded.Should().BeTrue();

The collapse test already has this pattern (it explicitly expands first, then asserts the collapse flips the state), which makes it a stronger test.

{
return item;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inner Find method returns null when no expandable item is found, but the outer FindExpandableProperty propagates that null through its non-nullable GridItem return type. While #nullable disable suppresses the warning, callers (the two tests above) rely on the .Should().NotBeNull() assertion as a guard.

This is fine today since MyClass has exactly one expandable property, but if MyClass is ever simplified or the [ExpandableObjectConverter] attribute is removed, the failure would manifest as a NullReferenceException on nested.Expanded rather than a clear "could not find expandable property" message.

Minor suggestion: consider throwing a descriptive exception (or using Assert.Fail) here instead of returning null, so a broken test fixture surfaces immediately with a clear message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants