diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/PropertyGridTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/PropertyGridTests.cs index 4c1a0b890fb..9c215f07f3f 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/PropertyGridTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/PropertyGridTests.cs @@ -2387,6 +2387,106 @@ public void PropertyGrid_PropertyTabCollection_AddAndRemoveTabType_Success() Assert.Single(grid.PropertyTabs); } + [WinFormsFact] + public void PropertyGrid_AddTab_NewTabType_AddsTabToCollection() + { + using PropertyGrid control = new(); + int initialCount = control.PropertyTabs.Count; + + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Static); + + // AddTab does not unconditionally append; it picks an insertion index + // based on tab kind and alphabetical order. Insertion order is verified + // separately by PropertyGrid_AddTab_OrdersCustomTabsAlphabetically. + Assert.Equal(initialCount + 1, control.PropertyTabs.Count); + Assert.Contains(control.PropertyTabs.Cast(), t => t is TestPropertyTab); + } + + [WinFormsFact] + public void PropertyGrid_AddTab_ExistingTabType_DoesNotAddDuplicate() + { + using PropertyGrid control = new(); + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Static); + int countAfterFirst = control.PropertyTabs.Count; + + // Adding the same tab type a second time should be a no-op for tab insertion. + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Static); + + Assert.Equal(countAfterFirst, control.PropertyTabs.Count); + Assert.Single(control.PropertyTabs.Cast(), t => t is TestPropertyTab); + } + + [WinFormsFact] + public void PropertyGrid_AddTab_ExistingTabType_RetainsOriginalScopeAndComponents() + { + using PropertyGrid control = new(); + using Button originalComponent = new(); + + // First call: attach a component with PropertyTabScope.Component. + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Component, originalComponent, setupToolbar: false); + int countAfterFirst = control.PropertyTabs.Count; + + // Second call uses a different scope and no component. This must not + // replace the existing tab or wipe out the previously-attached component, + // and the originally recorded scope must be preserved. + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Static, @object: null, setupToolbar: false); + + Assert.Equal(countAfterFirst, control.PropertyTabs.Count); + + PropertyTab tab = control.PropertyTabs.Cast().Single(t => t is TestPropertyTab); + Assert.NotNull(tab.Components); + Assert.Single(tab.Components); + Assert.Same(originalComponent, tab.Components[0]); + + // Verify the original scope is preserved (TabInfo.Scope is read via + // reflection because TabInfo is a private nested record). + Collections.IList tabs = control.TestAccessor.Dynamic._tabs; + object matchingTabInfo = null; + foreach (object tabInfo in tabs) + { + PropertyTab candidate = (PropertyTab)tabInfo.GetType().GetProperty("Tab").GetValue(tabInfo); + if (candidate is TestPropertyTab) + { + matchingTabInfo = tabInfo; + break; + } + } + + Assert.NotNull(matchingTabInfo); + PropertyTabScope actualScope = (PropertyTabScope)matchingTabInfo.GetType().GetProperty("Scope").GetValue(matchingTabInfo); + Assert.Equal(PropertyTabScope.Component, actualScope); + } + + [WinFormsFact] + public void PropertyGrid_AddTab_WithComponent_AddsComponentToTab() + { + using PropertyGrid control = new(); + using Button component = new(); + + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Component, component, setupToolbar: false); + + PropertyTab tab = control.PropertyTabs.Cast().Single(t => t is TestPropertyTab); + Assert.NotNull(tab.Components); + Assert.Single(tab.Components); + Assert.Same(component, tab.Components[0]); + } + + [WinFormsFact] + public void PropertyGrid_AddTab_SameTypeMultipleComponents_AccumulatesComponents() + { + using PropertyGrid control = new(); + using Button first = new(); + using Button second = new(); + + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Component, first, setupToolbar: false); + control.AddTab(typeof(TestPropertyTab), PropertyTabScope.Component, second, setupToolbar: false); + + PropertyTab tab = control.PropertyTabs.Cast().Single(t => t is TestPropertyTab); + Assert.Equal(2, tab.Components.Length); + Assert.Same(first, tab.Components[0]); + Assert.Same(second, tab.Components[1]); + } + private class TestPropertyTab : PropertyTab { public override string TabName => "TestTabName";