Repository navigation
Fix Segmented selection automation providers #892
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
Open
azchohfi
wants to merge
3
commits into
CommunityToolkit:main
Choose a base branch
from
azchohfi:azchohfi-fix-segmented-automation-peers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
components/Segmented/src/Segmented/SegmentedAutomationPeer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
|
||
| /// <summary> | ||
| /// Defines an automation peer for the <see cref="Segmented"/> control. | ||
| /// </summary> | ||
| public partial class SegmentedAutomationPeer : ListViewBaseAutomationPeer | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="SegmentedAutomationPeer"/> class. | ||
| /// </summary> | ||
| /// <param name="owner">The <see cref="Segmented"/> associated with this automation peer.</param> | ||
| public SegmentedAutomationPeer(Segmented owner) | ||
| : base(owner) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override string GetClassNameCore() => nameof(Segmented); | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override ItemAutomationPeer OnCreateItemAutomationPeer(object item) | ||
| => new ListViewItemDataAutomationPeer(item, this); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
components/Segmented/tests/Test_Segmented_AutomationPeer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| // 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; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| #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, selectedIndex == 0 ? "List" : "Grid"); | ||
| } | ||
| } | ||
|
|
||
| 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, "Grid"); | ||
|
|
||
| control.IsEnabled = false; | ||
| var exception = Assert.ThrowsException<COMException>(() => 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"); | ||
| } | ||
|
|
||
| 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]; | ||
|
|
||
| // 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); | ||
| Assert.AreSame(selectedItem, control.SelectedItem); | ||
| AssertSelection(selection, GetItemProviders(peer, "Details", "List", "Grid"), 2, "Grid"); | ||
| AssertSelection(selection, originalProviders, 1, "Grid"); | ||
|
|
||
| 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, "List"); | ||
| AssertSelection(selection, new[] { originalProviders[0], originalProviders[2] }, 0, "List"); | ||
| } | ||
|
|
||
| private static (Segmented Control, ObservableCollection<object> Items) CreateControl(bool explicitContainers) | ||
| { | ||
| var items = new ObservableCollection<object>(); | ||
| 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, 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.