From a1e2cb8bbe7ebe60c335754d9d2ab2597d05326a Mon Sep 17 00:00:00 2001 From: "John Qiao (Beyondsoft Corporation)" Date: Wed, 25 Dec 2024 02:29:28 -0800 Subject: [PATCH 1/2] Fix issue 12661 and add unit test --- .../Forms/Controls/TabControl/TabControl.cs | 2 +- .../System/Windows/Forms/TabControlTests.cs | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TabControl/TabControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TabControl/TabControl.cs index 618ce40bb14..aaa7afd5d85 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TabControl/TabControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TabControl/TabControl.cs @@ -1957,7 +1957,7 @@ private bool WmSelChange() if (IsAccessibilityObjectCreated && SelectedTab?.ParentInternal is TabControl) { SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_SelectionItem_ElementSelectedEventId); - BeginInvoke((MethodInvoker)(() => SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId))); + BeginInvoke((MethodInvoker)(() => SelectedTab?.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId))); } } else diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs index 8e0ac90df96..c95fd5cb808 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs @@ -5703,6 +5703,30 @@ public void TabControl_Invokes_SetToolTip_IfExternalToolTipIsSet() Assert.Equal(text, actual); } + [WinFormsFact] + public void TabControl_WmSelChange_SelectedTabIsNull_DoesNotThrowException() + { + using Form form = new(); + using TabControl control = new(); + using TabPage page1 = new("text1"); + using TabPage page2 = new("text2"); + control.TabPages.Add(page1); + control.TabPages.Add(page2); + _ = control.AccessibilityObject; + + form.Controls.Add(control); + form.Show(); + + control.SelectedIndex = 1; + control.TabPages.Clear(); + + Action act = () => control.TestAccessor().Dynamic.WmSelChange(); + Application.DoEvents(); + Thread.Sleep(1000); + + act.Should().NotThrow(); + } + private class SubTabPage : TabPage { } From 43f7faaee0c9942ff78786d2b9875592d018ea16 Mon Sep 17 00:00:00 2001 From: "John Qiao (Beyondsoft Corporation)" Date: Thu, 26 Dec 2024 02:06:31 -0800 Subject: [PATCH 2/2] Updated the unit test code lines to resolve the conversations from Tanya --- .../System/Windows/Forms/TabControlTests.cs | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs index c95fd5cb808..a1f42f48171 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs @@ -5709,26 +5709,67 @@ public void TabControl_WmSelChange_SelectedTabIsNull_DoesNotThrowException() using Form form = new(); using TabControl control = new(); using TabPage page1 = new("text1"); - using TabPage page2 = new("text2"); control.TabPages.Add(page1); - control.TabPages.Add(page2); _ = control.AccessibilityObject; form.Controls.Add(control); form.Show(); + control.SelectedIndex = 0; + + Action act = () => control.TestAccessor().Dynamic.WmSelChange(); + act.Should().NotThrow(); - control.SelectedIndex = 1; control.TabPages.Clear(); - Action act = () => control.TestAccessor().Dynamic.WmSelChange(); + var exception = Record.Exception(() => + { + Application.DoEvents(); + Thread.Sleep(100); + }); + + exception.Should().BeNull(); + } + + [WinFormsFact] + public void TabControl_WmSelChange_AccessibilityObjectNotCreated_NoAutomationEventRaised() + { + using TabControl tabControl = new(); + using SubTabPage tabPage = new(); + tabControl.SelectedTab = tabPage; + tabControl.SelectedIndex = 1; + + var automationEventRaised = false; + tabPage.TabAccessibilityObject = new TabAccessibilityObject + { + RaiseAutomationEventAction = () => automationEventRaised = true + }; + + tabControl.TestAccessor().Dynamic.WmSelChange(); + Application.DoEvents(); - Thread.Sleep(1000); + Thread.Sleep(100); - act.Should().NotThrow(); + automationEventRaised.Should().BeFalse(); + } + + public class TabAccessibilityObject + { + public Action RaiseAutomationEventAction { get; set; } + public void RaiseAutomationEvent(UIA_EVENT_ID eventId) + { + RaiseAutomationEventAction?.Invoke(); + } + } + + public enum UIA_EVENT_ID + { + UIA_SelectionItem_ElementSelectedEventId, + UIA_AutomationFocusChangedEventId } private class SubTabPage : TabPage { + public TabAccessibilityObject TabAccessibilityObject { get; set; } } private class NullTextTabPage : TabPage