Skip to content

Commit 82fc52e

Browse files
authored
Add unit test for ToolStripControlHost.ToolStripHostedControlAccessibleObject.cs file (#13794)
Add more test for uncovered internal override methods of ToolStripControlHost.ToolStripHostedControlAccessibleObject.cs file
1 parent 8a44096 commit 82fc52e

File tree

1 file changed

+131
-6
lines changed

1 file changed

+131
-6
lines changed

src/test/unit/System.Windows.Forms/System/Windows/Forms/AccessibleObjects/ToolStripControlHost.ToolStripHostedControlAccessibleObjectTests.cs

Lines changed: 131 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,32 @@
44
#nullable disable
55

66
using System.Drawing;
7+
using Windows.Win32.System.Variant;
78
using Windows.Win32.UI.Accessibility;
89

910
namespace System.Windows.Forms.Tests.AccessibleObjects;
1011

11-
public class ToolStripControlHost_ToolStripHostedControlAccessibleObjectTests
12+
public class ToolStripControlHost_ToolStripHostedControlAccessibleObjectTests : IDisposable
1213
{
14+
private readonly ToolStrip _toolStrip;
15+
private readonly TextBox _textBox;
16+
private readonly ToolStripControlHost _host;
17+
18+
public ToolStripControlHost_ToolStripHostedControlAccessibleObjectTests()
19+
{
20+
_toolStrip = new ToolStrip();
21+
_textBox = new TextBox();
22+
_host = new ToolStripControlHost(_textBox);
23+
_toolStrip.Items.Add(_host);
24+
}
25+
26+
public void Dispose()
27+
{
28+
_host.Dispose();
29+
_textBox.Dispose();
30+
_toolStrip.Dispose();
31+
}
32+
1333
public static IEnumerable<object[]> ToolStripItemAccessibleObject_TestData()
1434
{
1535
return ReflectionHelper.GetPublicNotAbstractClasses<ToolStripControlHost>().Select(type => new object[] { type });
@@ -19,14 +39,119 @@ public static IEnumerable<object[]> ToolStripItemAccessibleObject_TestData()
1939
[MemberData(nameof(ToolStripItemAccessibleObject_TestData))]
2040
public void ToolStripHostedControlAccessibleObject_GetPropertyValue_IsOffscreenPropertyId_ReturnExpected(Type type)
2141
{
22-
using ToolStrip toolStrip = new();
23-
toolStrip.CreateControl();
42+
_toolStrip.CreateControl();
2443
using ToolStripControlHost item = ReflectionHelper.InvokePublicConstructor<ToolStripControlHost>(type);
2544
item.Size = new Size(0, 0);
26-
toolStrip.Items.Add(item);
45+
_toolStrip.Items.Add(item);
46+
47+
GetIsOffscreenPropertyValue(item.AccessibilityObject).Should().BeTrue();
48+
GetIsOffscreenPropertyValue(item.Control.AccessibilityObject).Should().BeTrue();
49+
}
50+
51+
[WinFormsFact]
52+
public void ToolStripHostedControlAccessibleObject_FragmentRoot_Returns_Owner_AccessibilityObject_When_Valid()
53+
{
54+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
55+
new(_textBox, _host);
56+
IRawElementProviderFragmentRoot.Interface fragmentRoot = accessibleObject.FragmentRoot;
57+
58+
fragmentRoot.Should().BeSameAs(_toolStrip.AccessibilityObject);
59+
}
60+
61+
[WinFormsFact]
62+
public void ToolStripHostedControlAccessibleObject_FragmentRoot_FallsBack_ToBase_When_Null()
63+
{
64+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
65+
new(_textBox, null);
66+
IRawElementProviderFragmentRoot.Interface fragmentRoot = accessibleObject.FragmentRoot;
67+
68+
fragmentRoot.Should().Be(_textBox.AccessibilityObject.FragmentRoot);
69+
}
70+
71+
[WinFormsFact]
72+
public void ToolStripHostedControlAccessibleObject_FragmentNavigate_FallsBack_ToBase_When_Null()
73+
{
74+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
75+
new(_textBox, null);
76+
77+
// Use a valid direction that is not handled by the switch, e.g. NavigateDirection.NavigateDirection_FirstChild
78+
NavigateDirection direction = NavigateDirection.NavigateDirection_FirstChild;
79+
IRawElementProviderFragment.Interface expected = _textBox.AccessibilityObject.FragmentNavigate(direction);
80+
IRawElementProviderFragment.Interface actual = accessibleObject.FragmentNavigate(direction);
81+
82+
actual.Should().Be(expected);
83+
}
84+
85+
[WinFormsFact]
86+
public void ToolStripHostedControlAccessibleObject_ReturnsVariant_ForIsOffscreenPropertyId()
87+
{
88+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
89+
new(_textBox, null);
90+
VARIANT value = accessibleObject.GetPropertyValue(UIA_PROPERTY_ID.UIA_IsOffscreenPropertyId);
91+
92+
value.Type.Should().Be(VARENUM.VT_BOOL);
93+
((bool)value).Should().BeTrue();
94+
}
95+
96+
[WinFormsFact]
97+
public void ToolStripHostedControlAccessibleObject_GetPropertyValue_UnknownPropertyId_FallsBack_ToBase()
98+
{
99+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
100+
new(_textBox, null);
101+
VARIANT value = accessibleObject.GetPropertyValue((UIA_PROPERTY_ID)99999);
102+
103+
value.Should().Be(_textBox.AccessibilityObject.GetPropertyValue((UIA_PROPERTY_ID)99999));
104+
}
105+
106+
[WinFormsFact]
107+
public void ToolStripHostedControlAccessibleObject_FragmentNavigate_Delegates_To_Host_For_Parent()
108+
{
109+
// Ensure the host is fully initialized and accessible
110+
_toolStrip.CreateControl();
111+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
112+
new(_textBox, _host);
113+
IRawElementProviderFragment.Interface expected =
114+
_host.AccessibilityObject.FragmentNavigate(NavigateDirection.NavigateDirection_Parent);
115+
IRawElementProviderFragment.Interface actual =
116+
accessibleObject.FragmentNavigate(NavigateDirection.NavigateDirection_Parent);
117+
118+
actual.Should().Be(expected);
119+
}
120+
121+
[WinFormsFact]
122+
public void ToolStripHostedControlAccessibleObject_FragmentNavigate_FallsBack_ToBase_For_OtherDirections()
123+
{
124+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
125+
new(_textBox, null);
126+
127+
// Use a direction not handled by the switch, e.g. FirstChild
128+
IRawElementProviderFragment.Interface expected =
129+
_textBox.AccessibilityObject.FragmentNavigate(NavigateDirection.NavigateDirection_FirstChild);
130+
IRawElementProviderFragment.Interface actual =
131+
accessibleObject.FragmentNavigate(NavigateDirection.NavigateDirection_FirstChild);
132+
133+
actual.Should().Be(expected);
134+
}
135+
136+
[WinFormsFact]
137+
public void ToolStripHostedControlAccessibleObject_IsPatternSupported_ValuePatternId_Returns_True()
138+
{
139+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
140+
new(_textBox, null);
141+
bool result = accessibleObject.IsPatternSupported(UIA_PATTERN_ID.UIA_ValuePatternId);
142+
143+
result.Should().BeTrue();
144+
}
145+
146+
[WinFormsFact]
147+
public void ToolStripHostedControlAccessibleObject_IsPatternSupported_UnknownPatternId_FallsBack_ToBase()
148+
{
149+
ToolStripControlHost.ToolStripHostedControlAccessibleObject accessibleObject =
150+
new(_textBox, null);
151+
bool expected = _textBox.AccessibilityObject.IsPatternSupported((UIA_PATTERN_ID)99999);
152+
bool actual = accessibleObject.IsPatternSupported((UIA_PATTERN_ID)99999);
27153

28-
Assert.True(GetIsOffscreenPropertyValue(item.AccessibilityObject));
29-
Assert.True(GetIsOffscreenPropertyValue(item.Control.AccessibilityObject));
154+
actual.Should().Be(expected);
30155
}
31156

32157
private bool GetIsOffscreenPropertyValue(AccessibleObject accessibleObject) =>

0 commit comments

Comments
 (0)