Skip to content

Commit

Permalink
Issue 3421
Browse files Browse the repository at this point in the history
UIAProvider support for WebBrowser control and UnitTests
  • Loading branch information
dkazennov committed Jun 16, 2022
1 parent 6e448a3 commit 0f28ee9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 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,
UiaCore.UIA.IsKeyboardFocusablePropertyId
=> (State & AccessibleStates.Focusable) == AccessibleStates.Focusable,
_ => base.GetPropertyValue(propertyID)
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public WebBrowser() : base("8856f961-340a-11d0-a96b-00c04fd705a2")
// Public properties:
//

internal override bool SupportsUiaProviders => true;

/// <summary>
/// Specifies whether the WebBrowser control may navigate to another page once
/// it has been loaded. NOTE: it will always be able to navigate before being loaded.
Expand Down Expand Up @@ -1130,6 +1132,8 @@ protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
return new WebBrowserSite(this);
}

protected override AccessibleObject CreateAccessibilityInstance() => new WebBrowserAccessibleObject(this);

/// <summary>
/// Attaches to the DWebBrowserEvents2 connection point.
/// </summary>
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 0f28ee9

Please sign in to comment.