From 46693f9ae7810dea2f98161d7f5f5157181cfe92 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Thu, 19 Nov 2026 21:11:11 -0800 Subject: [PATCH 1/3] Fix Segmented native selection automation peers Reuse the native ListViewBase selection provider and ListView item data peers. Add native UI regression tests for selection identity, provider actions, disabled controls, and collection changes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- components/Segmented/src/ReadMe.md | 4 + .../Segmented/src/Segmented/Segmented.cs | 4 + .../src/Segmented/SegmentedAutomationPeer.cs | 29 +++ .../Segmented/tests/Segmented.Tests.projitems | 1 + .../tests/Test_Segmented_AutomationPeer.cs | 190 ++++++++++++++++++ 5 files changed, 228 insertions(+) create mode 100644 components/Segmented/src/Segmented/SegmentedAutomationPeer.cs create mode 100644 components/Segmented/tests/Test_Segmented_AutomationPeer.cs diff --git a/components/Segmented/src/ReadMe.md b/components/Segmented/src/ReadMe.md index 326aad66..1839633f 100644 --- a/components/Segmented/src/ReadMe.md +++ b/components/Segmented/src/ReadMe.md @@ -9,6 +9,10 @@ This package contains the following controls in the `CommunityToolkit.WinUI.Cont - Segmented +On Windows, `Segmented` exposes the UI Automation Selection pattern and its items +expose the SelectionItem pattern through native list automation peers. Set +`AutomationProperties.Name` on the control to provide an accessible name. + ## Which Package is for me? If you're developing with _UWP/WinUI 2 or Uno.UI_ you should be using the `CommunityToolkit.Uwp.Controls.Segmented` package. diff --git a/components/Segmented/src/Segmented/Segmented.cs b/components/Segmented/src/Segmented/Segmented.cs index 6fbc6c30..71c71b8c 100644 --- a/components/Segmented/src/Segmented/Segmented.cs +++ b/components/Segmented/src/Segmented/Segmented.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using CommunityToolkit.WinUI.Automation.Peers; using Windows.System; namespace CommunityToolkit.WinUI.Controls; @@ -24,6 +25,9 @@ public Segmented() RegisterPropertyChangedCallback(SelectedIndexProperty, OnSelectedIndexChanged); } + /// + protected override AutomationPeer OnCreateAutomationPeer() => new SegmentedAutomationPeer(this); + /// protected override DependencyObject GetContainerForItemOverride() => new SegmentedItem(); diff --git a/components/Segmented/src/Segmented/SegmentedAutomationPeer.cs b/components/Segmented/src/Segmented/SegmentedAutomationPeer.cs new file mode 100644 index 00000000..22389c7f --- /dev/null +++ b/components/Segmented/src/Segmented/SegmentedAutomationPeer.cs @@ -0,0 +1,29 @@ +// 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 CommunityToolkit.WinUI.Controls; + +namespace CommunityToolkit.WinUI.Automation.Peers; + +/// +/// Defines an automation peer for the control. +/// +public partial class SegmentedAutomationPeer : ListViewBaseAutomationPeer +{ + /// + /// Initializes a new instance of the class. + /// + /// The associated with this automation peer. + public SegmentedAutomationPeer(Segmented owner) + : base(owner) + { + } + + /// + protected override string GetClassNameCore() => nameof(Segmented); + + /// + protected override ItemAutomationPeer OnCreateItemAutomationPeer(object item) + => new ListViewItemDataAutomationPeer(item, this); +} diff --git a/components/Segmented/tests/Segmented.Tests.projitems b/components/Segmented/tests/Segmented.Tests.projitems index 25f1b261..20cb0315 100644 --- a/components/Segmented/tests/Segmented.Tests.projitems +++ b/components/Segmented/tests/Segmented.Tests.projitems @@ -9,6 +9,7 @@ SegmentedTests + ExampleSegmentedTestPage.xaml diff --git a/components/Segmented/tests/Test_Segmented_AutomationPeer.cs b/components/Segmented/tests/Test_Segmented_AutomationPeer.cs new file mode 100644 index 00000000..104688b7 --- /dev/null +++ b/components/Segmented/tests/Test_Segmented_AutomationPeer.cs @@ -0,0 +1,190 @@ +// 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. + +#if !HAS_UNO +using CommunityToolkit.Tests; +using CommunityToolkit.Tooling.TestGen; +using CommunityToolkit.WinUI.Controls; + +#if WINAPPSDK +using Microsoft.UI.Xaml.Automation.Provider; +#else +using Windows.UI.Xaml.Automation.Provider; +#endif + +namespace SegmentedTests; + +[TestClass] +[TestCategory("Test_Segmented_AutomationPeer")] +public partial class Test_Segmented_AutomationPeer : VisualUITestBase +{ + [UIThreadTestMethod] + public Task StringItemsExposeSelection() => VerifySelectionAsync(false); + + [UIThreadTestMethod] + public Task ExplicitContainersExposeSelection() => VerifySelectionAsync(true); + + [UIThreadTestMethod] + public Task StringItemSelectionRespectsIsEnabled() => VerifySelectAsync(false); + + [UIThreadTestMethod] + public Task ExplicitContainerSelectionRespectsIsEnabled() => VerifySelectAsync(true); + + [UIThreadTestMethod] + public Task StringItemPeersTrackCollectionChanges() => VerifyCollectionChangesAsync(false); + + [UIThreadTestMethod] + public Task ExplicitContainerPeersTrackCollectionChanges() => VerifyCollectionChangesAsync(true); + + private async Task VerifySelectionAsync(bool explicitContainers) + { + var (control, items) = CreateControl(explicitContainers); + await LoadTestContentAsync(control); + + var peer = GetPeer(control); + Assert.AreEqual("View mode", peer.GetName()); + Assert.AreEqual(nameof(Segmented), peer.GetClassName()); + Assert.AreEqual(AutomationControlType.List, peer.GetAutomationControlType()); + + var selection = GetSelectionProvider(peer); + Assert.IsFalse(selection.CanSelectMultiple); + + var itemProviders = GetItemProviders(peer, "List", "Grid", "Details"); + for (var selectedIndex = 0; selectedIndex <= 1; selectedIndex++) + { + control.SelectedIndex = selectedIndex; + + Assert.AreSame(items[selectedIndex], control.SelectedItem); + AssertSelection(selection, itemProviders, selectedIndex); + } + } + + private async Task VerifySelectAsync(bool explicitContainers) + { + var (control, items) = CreateControl(explicitContainers); + await LoadTestContentAsync(control); + control.SelectedIndex = 0; + + var peer = GetPeer(control); + var selection = GetSelectionProvider(peer); + var itemProviders = GetItemProviders(peer, "List", "Grid", "Details"); + + itemProviders[1].Select(); + + Assert.AreEqual(1, control.SelectedIndex); + Assert.AreSame(items[1], control.SelectedItem); + AssertSelection(selection, itemProviders, 1); + + control.IsEnabled = false; + Assert.ThrowsException(() => itemProviders[0].Select()); + + Assert.AreEqual(1, control.SelectedIndex); + Assert.AreSame(items[1], control.SelectedItem); + AssertSelection(selection, itemProviders, 1); + } + + private async Task VerifyCollectionChangesAsync(bool explicitContainers) + { + var (control, items) = CreateControl(explicitContainers); + await LoadTestContentAsync(control); + control.SelectedIndex = 1; + + var peer = GetPeer(control); + var selection = GetSelectionProvider(peer); + var originalProviders = GetItemProviders(peer, "List", "Grid", "Details"); + var selectedItem = items[1]; + + items.Move(2, 0); + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + + Assert.AreEqual(2, control.SelectedIndex); + Assert.AreSame(selectedItem, control.SelectedItem); + AssertSelection(selection, GetItemProviders(peer, "Details", "List", "Grid"), 2); + AssertSelection(selection, originalProviders, 1); + + items.Remove(selectedItem); + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + + // A retained peer must still refer to List, not the item now at its old index. + originalProviders[0].Select(); + + Assert.AreEqual(1, control.SelectedIndex); + Assert.AreSame(items[1], control.SelectedItem); + AssertSelection(selection, GetItemProviders(peer, "Details", "List"), 1); + AssertSelection(selection, originalProviders, 0); + } + + private static (Segmented Control, ObservableCollection Items) CreateControl(bool explicitContainers) + { + var items = new ObservableCollection(); + foreach (var name in new[] { "List", "Grid", "Details" }) + { + if (explicitContainers) + { + var item = new SegmentedItem { Content = name }; + AutomationProperties.SetName(item, name); + items.Add(item); + } + else + { + items.Add(name); + } + } + + var control = new Segmented + { + Name = "ViewModeControl", + ItemsSource = items, + SelectionMode = ListViewSelectionMode.Single, + }; + AutomationProperties.SetName(control, "View mode"); + return (control, items); + } + + private static AutomationPeer GetPeer(Segmented control) + { + var peer = FrameworkElementAutomationPeer.CreatePeerForElement(control); + Assert.IsNotNull(peer); + return peer; + } + + private static ISelectionProvider GetSelectionProvider(AutomationPeer peer) + { + var selection = peer.GetPattern(PatternInterface.Selection) as ISelectionProvider; + Assert.IsNotNull(selection, "The root must expose the native Selection provider."); + return selection; + } + + private static ISelectionItemProvider[] GetItemProviders(AutomationPeer peer, params string[] expectedNames) + { + var children = peer.GetChildren(); + Assert.IsNotNull(children); + Assert.AreEqual(expectedNames.Length, children.Count); + + var providers = new ISelectionItemProvider[children.Count]; + for (var index = 0; index < children.Count; index++) + { + Assert.AreEqual(expectedNames[index], children[index].GetName()); + var provider = children[index].GetPattern(PatternInterface.SelectionItem) as ISelectionItemProvider; + Assert.IsNotNull(provider, $"{expectedNames[index]} must expose the native SelectionItem provider."); + Assert.IsNotNull(provider.SelectionContainer); + providers[index] = provider; + } + + return providers; + } + + private static void AssertSelection(ISelectionProvider selection, ISelectionItemProvider[] items, int selectedIndex) + { + var selectedItems = selection.GetSelection(); + Assert.IsNotNull(selectedItems); + Assert.AreEqual(1, selectedItems.Length); + + for (var index = 0; index < items.Length; index++) + { + Assert.AreEqual(index == selectedIndex, items[index].IsSelected, $"Selection identity at item {index}."); + } + } +} +#endif From 0796d14b53faffb22319875312bdeba2b24d2743 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Fri, 20 Nov 2026 19:53:57 -0800 Subject: [PATCH 2/3] Verify Segmented root selection provider identity Resolve the native provider returned by GetSelection and check the expected logical item in every selection assertion, including retained peers after collection changes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../tests/Test_Segmented_AutomationPeer.cs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/components/Segmented/tests/Test_Segmented_AutomationPeer.cs b/components/Segmented/tests/Test_Segmented_AutomationPeer.cs index 104688b7..c0c382c9 100644 --- a/components/Segmented/tests/Test_Segmented_AutomationPeer.cs +++ b/components/Segmented/tests/Test_Segmented_AutomationPeer.cs @@ -56,7 +56,7 @@ private async Task VerifySelectionAsync(bool explicitContainers) control.SelectedIndex = selectedIndex; Assert.AreSame(items[selectedIndex], control.SelectedItem); - AssertSelection(selection, itemProviders, selectedIndex); + AssertSelection(selection, itemProviders, selectedIndex, selectedIndex == 0 ? "List" : "Grid"); } } @@ -74,14 +74,14 @@ private async Task VerifySelectAsync(bool explicitContainers) Assert.AreEqual(1, control.SelectedIndex); Assert.AreSame(items[1], control.SelectedItem); - AssertSelection(selection, itemProviders, 1); + AssertSelection(selection, itemProviders, 1, "Grid"); control.IsEnabled = false; Assert.ThrowsException(() => itemProviders[0].Select()); Assert.AreEqual(1, control.SelectedIndex); Assert.AreSame(items[1], control.SelectedItem); - AssertSelection(selection, itemProviders, 1); + AssertSelection(selection, itemProviders, 1, "Grid"); } private async Task VerifyCollectionChangesAsync(bool explicitContainers) @@ -100,8 +100,8 @@ private async Task VerifyCollectionChangesAsync(bool explicitContainers) Assert.AreEqual(2, control.SelectedIndex); Assert.AreSame(selectedItem, control.SelectedItem); - AssertSelection(selection, GetItemProviders(peer, "Details", "List", "Grid"), 2); - AssertSelection(selection, originalProviders, 1); + AssertSelection(selection, GetItemProviders(peer, "Details", "List", "Grid"), 2, "Grid"); + AssertSelection(selection, originalProviders, 1, "Grid"); items.Remove(selectedItem); await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); @@ -111,8 +111,8 @@ private async Task VerifyCollectionChangesAsync(bool explicitContainers) Assert.AreEqual(1, control.SelectedIndex); Assert.AreSame(items[1], control.SelectedItem); - AssertSelection(selection, GetItemProviders(peer, "Details", "List"), 1); - AssertSelection(selection, originalProviders, 0); + AssertSelection(selection, GetItemProviders(peer, "Details", "List"), 1, "List"); + AssertSelection(selection, originalProviders, 0, "List"); } private static (Segmented Control, ObservableCollection Items) CreateControl(bool explicitContainers) @@ -175,16 +175,31 @@ private static ISelectionItemProvider[] GetItemProviders(AutomationPeer peer, pa return providers; } - private static void AssertSelection(ISelectionProvider selection, ISelectionItemProvider[] items, int selectedIndex) + private static void AssertSelection(ISelectionProvider selection, ISelectionItemProvider[] items, int selectedIndex, string expectedSelectedName) { var selectedItems = selection.GetSelection(); Assert.IsNotNull(selectedItems); Assert.AreEqual(1, selectedItems.Length); + var selectedPeer = new ProviderPeerAccess().Resolve(selectedItems[0]); + Assert.IsNotNull(selectedPeer); + Assert.AreEqual(expectedSelectedName, selectedPeer.GetName(), "The root must return the selected item's provider."); + for (var index = 0; index < items.Length; index++) { Assert.AreEqual(index == selectedIndex, items[index].IsSelected, $"Selection identity at item {index}."); } } + + // Exposes the protected conversion on a detached element, without replacing any control or item peer. + private sealed partial class ProviderPeerAccess : FrameworkElementAutomationPeer + { + public ProviderPeerAccess() + : base(new Grid()) + { + } + + public AutomationPeer Resolve(IRawElementProviderSimple provider) => PeerFromProvider(provider); + } } #endif From c8380aa5f6971c98e75c97d89dffcc504c4641f5 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Fri, 20 Nov 2026 20:00:40 -0800 Subject: [PATCH 3/3] Align Segmented tests with native UIA contracts Assert UIA_E_ELEMENTNOTENABLED for disabled selection and re-enable before querying selection providers. Reorder unselected items with remove/insert instead of a Move reset, and retain identity assertions for surviving peers after removal. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../tests/Test_Segmented_AutomationPeer.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/components/Segmented/tests/Test_Segmented_AutomationPeer.cs b/components/Segmented/tests/Test_Segmented_AutomationPeer.cs index c0c382c9..8d8e998a 100644 --- a/components/Segmented/tests/Test_Segmented_AutomationPeer.cs +++ b/components/Segmented/tests/Test_Segmented_AutomationPeer.cs @@ -6,6 +6,7 @@ using CommunityToolkit.Tests; using CommunityToolkit.Tooling.TestGen; using CommunityToolkit.WinUI.Controls; +using System.Runtime.InteropServices; #if WINAPPSDK using Microsoft.UI.Xaml.Automation.Provider; @@ -77,10 +78,14 @@ private async Task VerifySelectAsync(bool explicitContainers) AssertSelection(selection, itemProviders, 1, "Grid"); control.IsEnabled = false; - Assert.ThrowsException(() => itemProviders[0].Select()); + var exception = Assert.ThrowsException(() => itemProviders[0].Select()); + Assert.AreEqual(unchecked((int)0x80040200), exception.HResult, "Expected UIA_E_ELEMENTNOTENABLED."); Assert.AreEqual(1, control.SelectedIndex); Assert.AreSame(items[1], control.SelectedItem); + + // Native item peers also reject IsSelected while disabled. + control.IsEnabled = true; AssertSelection(selection, itemProviders, 1, "Grid"); } @@ -95,7 +100,10 @@ private async Task VerifyCollectionChangesAsync(bool explicitContainers) var originalProviders = GetItemProviders(peer, "List", "Grid", "Details"); var selectedItem = items[1]; - items.Move(2, 0); + // Native Move notifications can reset selection; remove/insert preserves the selected logical item. + var movedItem = items[2]; + items.RemoveAt(2); + items.Insert(0, movedItem); await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); Assert.AreEqual(2, control.SelectedIndex); @@ -112,7 +120,7 @@ private async Task VerifyCollectionChangesAsync(bool explicitContainers) Assert.AreEqual(1, control.SelectedIndex); Assert.AreSame(items[1], control.SelectedItem); AssertSelection(selection, GetItemProviders(peer, "Details", "List"), 1, "List"); - AssertSelection(selection, originalProviders, 0, "List"); + AssertSelection(selection, new[] { originalProviders[0], originalProviders[2] }, 0, "List"); } private static (Segmented Control, ObservableCollection Items) CreateControl(bool explicitContainers)