Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code coverage for ListBoxAccessibleObject and ListBoxItemAccessib… #11724

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,105 @@ public void ListBoxItemAccessibleObject_GetPropertyValue_Pattern_ReturnsExpected
Assert.Equal(expected, !result.IsEmpty && (bool)result);
Assert.False(listBox.IsHandleCreated);
}

#nullable enable

[WinFormsFact]
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
public void ListBoxItemAccessibleObject_Bounds_BeforeAndAfterHandleCreation()
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
{
using Form form = new();
using ListBox listBox = new() { Parent = form };
listBox.Items.Add("Test Item");

var itemAccessibleObject = listBox.AccessibilityObject.GetChild(0).Should().BeOfType<ListBox.ListBoxItemAccessibleObject>().Which;

var boundsBeforeHandleCreation = itemAccessibleObject.Bounds;
boundsBeforeHandleCreation.Should().Be(Rectangle.Empty);

form.Show();

listBox.Height = 200;
listBox.Width = 100;

var boundsAfterHandleCreation = itemAccessibleObject.Bounds;
boundsAfterHandleCreation.Should().NotBe(Rectangle.Empty);
boundsAfterHandleCreation.Width.Should().BeLessOrEqualTo(listBox.Width);
boundsAfterHandleCreation.Height.Should().BeLessOrEqualTo(listBox.Height);
}


[WinFormsFact]
public void ListBoxItemAccessibleObject_DefaultAction_VariesByContext()
{
using Form formDoubleClick = new();
using ListBox listBoxDoubleClick = new() { Items = { "Item 1" }, Parent = formDoubleClick };
formDoubleClick.Show();

var itemAccessibleObjectDoubleClick = listBoxDoubleClick.AccessibilityObject.GetChild(0).Should().BeOfType<ListBox.ListBoxItemAccessibleObject>().Which;
itemAccessibleObjectDoubleClick.DefaultAction.Should().Be("Double Click");

using ListBox listBoxNullAction = new ListBox { Items = { "Item 2" } };
var itemAccessibleObjectNullAction = listBoxNullAction.AccessibilityObject.GetChild(0).Should().BeOfType<ListBox.ListBoxItemAccessibleObject>().Which;
itemAccessibleObjectNullAction.DefaultAction.Should().BeNull();
}

[WinFormsFact]
public void ListBoxItemAccessibleObject_VerifyProperties()
{
using ListBox listBox = new();
listBox.Items.Add("Item 1");

var accessibleObject = listBox.AccessibilityObject;
var itemAccessibleObject = accessibleObject.GetChild(0).Should().BeOfType<ListBox.ListBoxItemAccessibleObject>().Which;

itemAccessibleObject.Bounds.Should().Be(Rectangle.Empty);

listBox.CreateControl();
listBox.IsHandleCreated.Should().BeTrue();

itemAccessibleObject.Role.Should().Be(AccessibleRole.ListItem);
itemAccessibleObject.Name.Should().Be("Item 1");
itemAccessibleObject.DefaultAction.Should().NotBeNull();
}

[WinFormsFact]
public void TestDoDefaultAction_HandleCreatedAndNotCreated()
{
using Form form = new();
using ListBox listBox = new() { Parent = form };
listBox.Items.Add("Test Item");

var itemAccessibleObject = listBox.AccessibilityObject.GetChild(0).Should().BeOfType<ListBox.ListBoxItemAccessibleObject>().Which;
itemAccessibleObject.DoDefaultAction();

listBox.Focused.Should().BeFalse();

form.Show();

listBox.IsHandleCreated.Should().BeTrue();

itemAccessibleObject.DoDefaultAction();

listBox.Focused.Should().BeTrue();
}

[WinFormsTheory]
[InlineData(AccessibleSelection.AddSelection)]
[InlineData(AccessibleSelection.RemoveSelection)]
[InlineData(AccessibleSelection.TakeFocus)]
[InlineData((AccessibleSelection)int.MaxValue)]
public void Select_WithVariousFlags_ShouldNotThrow(AccessibleSelection flags)
{
using ListBox listBox = new();
ItemArray.Entry itemEntry = new ItemArray.Entry("Test Item");
ListBox.ListBoxAccessibleObject listBoxAccessibleObject = new ListBox.ListBoxAccessibleObject(listBox);
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
ListBox.ListBoxItemAccessibleObject listBoxItemAccessibleObject = new ListBox.ListBoxItemAccessibleObject(listBox, itemEntry, listBoxAccessibleObject);
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved

Action action = () => listBoxItemAccessibleObject.Select(flags);

action.Should().NotThrow();
}

#nullable disable
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Windows.Win32.System.Variant;
using Windows.Win32.UI.Accessibility;
using static System.Windows.Forms.ListBox;
using System.Drawing;

namespace System.Windows.Forms.Tests.AccessibleObjects;

Expand Down Expand Up @@ -246,4 +247,162 @@ private ListBoxAccessibleObject InitListBoxItemsAccessibleObjects(ListBox listBo

return accessibilityObject;
}

#nullable enable

[WinFormsFact]
public void ListBoxAccessibleObject_State_ShouldBeExpected()
{
using Form form = new();
using ListBox listBox = new();
using TextBox textBox = new();
form.Controls.Add(listBox);
form.Controls.Add(textBox);

form.Show();
listBox.CreateControl();

textBox.Focus();
listBox.Focused.Should().BeFalse();
listBox.AccessibilityObject.State.Should().Be(AccessibleStates.Focusable);

listBox.Focus();
listBox.Focused.Should().BeTrue();
listBox.AccessibilityObject.State.Should().Be(AccessibleStates.Focused | AccessibleStates.Focusable);
}

[WinFormsFact]
public void ListBoxAccessibleObject_ShouldCorrectlyHandleChildrenAndSelection()
{
using ListBox listBox = new();
listBox.Items.AddRange(new[] { "Item 1", "Item 2" });
var accessibleObject = listBox.AccessibilityObject;

accessibleObject.GetSelected().Should().BeNull();

listBox.SelectedIndex = 0;
accessibleObject.GetSelected().Should().Be(accessibleObject.GetChild(0));

listBox.SelectedIndex = 1;
accessibleObject.GetSelected().Should().Be(accessibleObject.GetChild(1));

listBox.ClearSelected();
accessibleObject.GetSelected().Should().BeNull();
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved

listBox.IsHandleCreated.Should().BeFalse();
}

[WinFormsTheory]
[InlineData(0, 0)]
[InlineData(2, 2)]
public void GetChildCount_ReturnsExpected(int numberOfItemsToAdd, int expectedCount)
{
using ListBox listBox = new();
for (int i = 0; i < numberOfItemsToAdd; i++)
{
listBox.Items.Add($"Item {i + 1}");
}

var accessibleObject = listBox.AccessibilityObject;

accessibleObject.GetChildCount().Should().Be(expectedCount);
}

[WinFormsFact]
public void TestGetFocused_ReturnsExpected()
{
using ListBox listBox = new();
listBox.Items.AddRange(new[] { "Item 1", "Item 2" });
listBox.CreateControl();
listBox.SelectedIndex = 1; // Focus the second item
listBox.Focus();

var accessibleObject = listBox.AccessibilityObject;
var focusedObject = accessibleObject.GetFocused();

focusedObject.Should().BeEquivalentTo(accessibleObject.GetChild(1));
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved

listBox.IsHandleCreated.Should().BeTrue();
}

[WinFormsTheory]
[InlineData(-1, null)]
[InlineData(1, 1)]
[InlineData(-1, null, true)]
[InlineData(0, 0, false, true)]
public void TestGetSelected_VariousScenarios(int selectedIndex, int? expectedIndex, bool clearSelection = false, bool multipleSelection = false)
{
using ListBox listBox = new() { SelectionMode = multipleSelection ? SelectionMode.MultiExtended : SelectionMode.One };
listBox.Items.AddRange(new[] { "Item 1", "Item 2", "Item 3" });
if (selectedIndex >= 0)
{
listBox.SelectedIndices.Add(selectedIndex);
if (multipleSelection)
{
listBox.SelectedIndices.Add(2);
}
}

if (clearSelection)
{
listBox.ClearSelected();
}

var accessibleObject = listBox.AccessibilityObject;
var selectedObject = accessibleObject.GetSelected();

if (expectedIndex.HasValue)
{
selectedObject.Should().BeEquivalentTo(accessibleObject.GetChild(expectedIndex.Value));
}
else
{
selectedObject.Should().BeNull();
}
}

[WinFormsFact]
public void TestHitTest_ListBoxNotCreated_ReturnsNull()
{
using Form form = new();
using ListBox listBox = new() { Parent = form, Items = { "Item 1", "Item 2" } };
Point testPoint = new Point(10, 10);

var result = listBox.AccessibilityObject.HitTest(testPoint.X, testPoint.Y);

result.Should().BeNull();
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
listBox.IsHandleCreated.Should().BeFalse();
}

[WinFormsFact]
public void TestHitTest_PointInsideListBoxBoundsButOutsideItems_ReturnsSelf()
{
using Form form = new();
using ListBox listBox = new() { Parent = form, Items = { "Item 1", "Item 2" } };
listBox.CreateControl();
form.Show();
Syareel-Sukeri marked this conversation as resolved.
Show resolved Hide resolved
Point testPoint = listBox.PointToScreen(new Point(0, listBox.ClientRectangle.Height - 1));

var result = listBox.AccessibilityObject.HitTest(testPoint.X, testPoint.Y);

result.Should().Be(listBox.AccessibilityObject);
}

[WinFormsFact]
public void TestHitTest_PointInsideChildBounds_ReturnsChild()
{
using Form form = new();
using ListBox listBox = new() { Parent = form, Items = { "Item 1", "Item 2" } };
listBox.CreateControl();
form.Show();
listBox.SelectedIndex = 0;
var itemBounds = listBox.GetItemRectangle(0);
Point testPoint = listBox.PointToScreen(new Point(itemBounds.Left + 1, itemBounds.Top + 1));

var result = listBox.AccessibilityObject.HitTest(testPoint.X, testPoint.Y);

result.Should().Be(listBox.AccessibilityObject.GetChild(0));
}

#nullable disable
}
Loading