diff --git a/src/System.Windows.Forms/src/PublicAPI.Unshipped.txt b/src/System.Windows.Forms/src/PublicAPI.Unshipped.txt index e69de29bb2d..1eadb8c1fd8 100644 --- a/src/System.Windows.Forms/src/PublicAPI.Unshipped.txt +++ b/src/System.Windows.Forms/src/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +~override System.Windows.Forms.ToolStripContainer.CreateAccessibilityInstance() -> System.Windows.Forms.AccessibleObject \ No newline at end of file diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripContainer.ToolStripContainerAccessibleObject.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripContainer.ToolStripContainerAccessibleObject.cs new file mode 100644 index 00000000000..41616ae12b1 --- /dev/null +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripContainer.ToolStripContainerAccessibleObject.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using static Interop; + +namespace System.Windows.Forms +{ + public partial class ToolStripContainer + { + internal class ToolStripContainerAccessibleObject : ControlAccessibleObject + { + public ToolStripContainerAccessibleObject(ToolStripContainer owner) : base(owner) + { + } + + internal override object? GetPropertyValue(UiaCore.UIA propertyID) + => propertyID switch + { + UiaCore.UIA.AutomationIdPropertyId + => Owner.Name, + UiaCore.UIA.HasKeyboardFocusPropertyId + => Owner.Focused, + UiaCore.UIA.IsKeyboardFocusablePropertyId + => (State & AccessibleStates.Focusable) == AccessibleStates.Focusable, + _ => base.GetPropertyValue(propertyID) + }; + } + } +} diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripContainer.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripContainer.cs index 8ce231b3500..739c7b8c81f 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripContainer.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripContainer.cs @@ -309,6 +309,8 @@ public bool RightToolStripPanelVisible } } + internal override bool SupportsUiaProviders => true; + [SRCategory(nameof(SR.CatAppearance))] [SRDescription(nameof(SR.ToolStripContainerTopToolStripPanelDescr))] [Localizable(false)] @@ -347,6 +349,9 @@ public bool TopToolStripPanelVisible get => base.Controls; } + protected override AccessibleObject CreateAccessibilityInstance() + => new ToolStripContainerAccessibleObject(this); + [EditorBrowsable(EditorBrowsableState.Advanced)] protected override ControlCollection CreateControlsInstance() { diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripContainer.ToolStripContainerAccessibleObjectTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripContainer.ToolStripContainerAccessibleObjectTests.cs new file mode 100644 index 00000000000..4dc898859c6 --- /dev/null +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripContainer.ToolStripContainerAccessibleObjectTests.cs @@ -0,0 +1,66 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; +using static Interop.UiaCore; + +namespace System.Windows.Forms.Tests +{ + public class ToolStripContainer_ToolStripContainerAccessibleObjectTests : IClassFixture + { + [WinFormsFact] + public void ToolStripContainerAccessibleObject_Ctor_Default() + { + using ToolStripContainer toolStripContainer = new(); + ToolStripContainer.ToolStripContainerAccessibleObject accessibleObject = new(toolStripContainer); + + Assert.Equal(toolStripContainer, accessibleObject.Owner); + Assert.False(toolStripContainer.IsHandleCreated); + } + + [WinFormsTheory] + [InlineData((int)UIA.NamePropertyId, "TestName")] + [InlineData((int)UIA.AutomationIdPropertyId, "ToolStripContainer1")] + public void ToolStripContainerAccessibleObject_GetPropertyValue_Invoke_ReturnsExpected(int propertyID, object expected) + { + using var control = new ToolStripContainer + { + Name = "ToolStripContainer1", + AccessibleName = "TestName" + }; + + var accessibleObject = new ToolStripContainer.ToolStripContainerAccessibleObject(control); + object value = accessibleObject.GetPropertyValue((UIA)propertyID); + + Assert.Equal(expected, value); + Assert.False(control.IsHandleCreated); + } + + [WinFormsFact] + public void ToolStripContainerAccessibleObject_GetPropertyValue_HasKeyboardFocus_ReturnsTrue_IfControlHasFocus() + { + using var control = new ToolStripContainer(); + + var accessibleObject = new ToolStripContainer.ToolStripContainerAccessibleObject(control); + Assert.False(control.IsHandleCreated); + control.FocusActiveControlInternal(); + bool value = (bool)accessibleObject.GetPropertyValue(UIA.HasKeyboardFocusPropertyId); + + Assert.True(value); + Assert.True(control.IsHandleCreated); + } + + [WinFormsFact] + public void ToolStripContainerAccessibleObject_GetPropertyValue_HasKeyboardFocus_ReturnsFalse_IfControlHasNoFocus() + { + using var control = new ToolStripContainer(); + + var accessibleObject = new ToolStripContainer.ToolStripContainerAccessibleObject(control); + bool value = (bool)accessibleObject.GetPropertyValue(UIA.HasKeyboardFocusPropertyId); + + Assert.False(value); + Assert.False(control.IsHandleCreated); + } + } +}