Skip to content

Commit

Permalink
Adding UIA support for WebBrowser (#7314)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkazennov authored Jun 28, 2022
1 parent 569f459 commit 250c0e1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/System.Windows.Forms/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
~override System.Windows.Forms.WebBrowser.CreateAccessibilityInstance() -> System.Windows.Forms.AccessibleObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 WebBrowser
{
internal class WebBrowserAccessibleObject : ControlAccessibleObject
{
public WebBrowserAccessibleObject(WebBrowser owner) : base(owner)
{ }

internal override object? GetPropertyValue(UiaCore.UIA propertyID)
=> propertyID switch
{
UiaCore.UIA.AutomationIdPropertyId
=> Owner.Name,
UiaCore.UIA.HasKeyboardFocusPropertyId
=> Owner.Focused,
_ => base.GetPropertyValue(propertyID)
};
}
}
}
18 changes: 11 additions & 7 deletions src/System.Windows.Forms/src/System/Windows/Forms/WebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public bool ScriptErrorsSuppressed
}
}

internal override bool SupportsUiaProviders => true;

/// <summary>
/// Specifies whether the browser control Shortcuts are enabled.
/// Maps to IDocHostUIHandler:TranslateAccelerator event.
Expand Down Expand Up @@ -1122,13 +1124,7 @@ protected override void DetachInterfaces()
axIWebBrowser2 = null;
}

/// <summary>
/// Returns a WebBrowserSite object.
/// </summary>
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
return new WebBrowserSite(this);
}
protected override AccessibleObject CreateAccessibilityInstance() => new WebBrowserAccessibleObject(this);

/// <summary>
/// Attaches to the DWebBrowserEvents2 connection point.
Expand Down Expand Up @@ -1159,6 +1155,14 @@ protected override void DetachSink()
}
}

/// <summary>
/// Returns a WebBrowserSite object.
/// </summary>
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
return new WebBrowserSite(this);
}

/// <summary>
/// Raises the <see cref="CanGoBackChanged"/> event.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Control_ControlAccessibleObjectTests : IClassFixture<ThreadExceptio
// otherwise an error can be thrown.
private static Type[] s_controlsIgnoringTextChangesForTests = new Type[]
{
typeof(DateTimePicker),
typeof(DateTimePicker), typeof(WebBrowser)
};

[WinFormsFact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 System.Windows.Forms.WebBrowser;
using static Interop.UiaCore;

namespace System.Windows.Forms.Tests
{
public class WebBrowser_WebBrowserAccessibleObjectTests : IClassFixture<ThreadExceptionFixture>
{
[WinFormsFact]
public void WebBrowserAccessibleObject_Ctor_Default()
{
using WebBrowser webBrowser = new();
WebBrowserAccessibleObject accessibleObject = (WebBrowserAccessibleObject)webBrowser.AccessibilityObject;

Assert.Equal(webBrowser, accessibleObject.Owner);
Assert.False(webBrowser.IsHandleCreated);
}

[WinFormsTheory]
[InlineData((int)UIA.NamePropertyId, "TestName")]
[InlineData((int)UIA.AutomationIdPropertyId, "ToolStripContainer1")]
public void WebBrowserAccessibleObject_GetPropertyValue_Invoke_ReturnsExpected(int propertyID, object expected)
{
using WebBrowser webBrowser = new WebBrowser
{
Name = expected.ToString(),
AccessibleName = expected.ToString()
};

WebBrowserAccessibleObject accessibleObject = (WebBrowserAccessibleObject)webBrowser.AccessibilityObject;
object value = accessibleObject.GetPropertyValue((UIA)propertyID);

Assert.Equal(expected, value);
Assert.False(webBrowser.IsHandleCreated);
}

[WinFormsFact]
public void WebBrowserAccessibleObject_GetPropertyValue_HasKeyboardFocus_ReturnsFalse_IfControlHasNoFocus()
{
using WebBrowser webBrowser = new();

WebBrowserAccessibleObject accessibleObject = (WebBrowserAccessibleObject)webBrowser.AccessibilityObject;
bool value = (bool)accessibleObject.GetPropertyValue(UIA.HasKeyboardFocusPropertyId);

Assert.False(value);
Assert.False(webBrowser.IsHandleCreated);
}
}
}

0 comments on commit 250c0e1

Please sign in to comment.