-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Added TokenizingTextBox automation peer #3891
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
Changes from all commits
abb3d6b
5844ec0
4c40b33
9e1c078
e41c808
d54299e
299dc32
ebb294d
461d0f3
0f6b415
5b4bd23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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 System.Collections.Generic; | ||
using Microsoft.Toolkit.Uwp.UI.Controls; | ||
using Windows.UI.Xaml.Automation; | ||
using Windows.UI.Xaml.Automation.Peers; | ||
using Windows.UI.Xaml.Automation.Provider; | ||
using Windows.UI.Xaml.Controls; | ||
|
||
namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers | ||
{ | ||
/// <summary> | ||
/// Defines a framework element automation peer for the <see cref="TokenizingTextBox"/> control. | ||
/// </summary> | ||
public class TokenizingTextBoxAutomationPeer : ListViewBaseAutomationPeer, IValueProvider | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TokenizingTextBoxAutomationPeer"/> class. | ||
/// </summary> | ||
/// <param name="owner"> | ||
/// The <see cref="TokenizingTextBox" /> that is associated with this <see cref="T:Microsoft.Toolkit.Uwp.UI.Automation.Peers.TokenizingTextBoxAutomationPeer" />. | ||
/// </param> | ||
public TokenizingTextBoxAutomationPeer(TokenizingTextBox owner) | ||
: base(owner) | ||
{ | ||
} | ||
|
||
/// <summary>Gets a value indicating whether the value of a control is read-only.</summary> | ||
/// <returns>**true** if the value is read-only; **false** if it can be modified.</returns> | ||
public bool IsReadOnly => !this.OwningTokenizingTextBox.IsEnabled; | ||
|
||
/// <summary>Gets the value of the control.</summary> | ||
/// <returns>The value of the control.</returns> | ||
public string Value => this.OwningTokenizingTextBox.Text; | ||
|
||
private TokenizingTextBox OwningTokenizingTextBox | ||
{ | ||
get | ||
{ | ||
return Owner as TokenizingTextBox; | ||
} | ||
} | ||
|
||
/// <summary>Sets the value of a control.</summary> | ||
/// <param name="value">The value to set. The provider is responsible for converting the value to the appropriate data type.</param> | ||
/// <exception cref="T:Windows.UI.Xaml.Automation.ElementNotEnabledException">Thrown if the control is in a read-only state.</exception> | ||
public void SetValue(string value) | ||
{ | ||
if (IsReadOnly) | ||
{ | ||
throw new ElementNotEnabledException($"Could not set the value of the {nameof(TokenizingTextBox)} "); | ||
} | ||
|
||
this.OwningTokenizingTextBox.Text = value; | ||
} | ||
|
||
/// <summary> | ||
/// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType, | ||
/// differentiates the control represented by this AutomationPeer. | ||
/// </summary> | ||
/// <returns>The string that contains the name.</returns> | ||
protected override string GetClassNameCore() | ||
{ | ||
return Owner.GetType().Name; | ||
} | ||
|
||
/// <summary> | ||
/// Called by GetName. | ||
/// </summary> | ||
/// <returns> | ||
/// Returns the first of these that is not null or empty: | ||
/// - Value returned by the base implementation | ||
/// - Name of the owning TokenizingTextBox | ||
/// - TokenizingTextBox class name | ||
/// </returns> | ||
protected override string GetNameCore() | ||
{ | ||
string name = this.OwningTokenizingTextBox.Name; | ||
if (!string.IsNullOrWhiteSpace(name)) | ||
{ | ||
return name; | ||
} | ||
|
||
name = AutomationProperties.GetName(this.OwningTokenizingTextBox); | ||
return !string.IsNullOrWhiteSpace(name) ? name : base.GetNameCore(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the control pattern that is associated with the specified Windows.UI.Xaml.Automation.Peers.PatternInterface. | ||
/// </summary> | ||
/// <param name="patternInterface">A value from the Windows.UI.Xaml.Automation.Peers.PatternInterface enumeration.</param> | ||
/// <returns>The object that supports the specified pattern, or null if unsupported.</returns> | ||
protected override object GetPatternCore(PatternInterface patternInterface) | ||
{ | ||
return patternInterface switch | ||
{ | ||
PatternInterface.Value => this, | ||
_ => base.GetPatternCore(patternInterface) | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the collection of elements that are represented in the UI Automation tree as immediate | ||
/// child elements of the automation peer. | ||
/// </summary> | ||
/// <returns>The children elements.</returns> | ||
protected override IList<AutomationPeer> GetChildrenCore() | ||
{ | ||
TokenizingTextBox owner = this.OwningTokenizingTextBox; | ||
|
||
ItemCollection items = owner.Items; | ||
if (items.Count <= 0) | ||
{ | ||
return null; | ||
} | ||
|
||
List<AutomationPeer> peers = new List<AutomationPeer>(items.Count); | ||
for (int i = 0; i < items.Count; i++) | ||
{ | ||
if (owner.ContainerFromIndex(i) is TokenizingTextBoxItem element) | ||
{ | ||
peers.Add(FromElement(element) ?? CreatePeerForElement(element)); | ||
} | ||
} | ||
|
||
return peers; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// 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 System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Windows.UI.Xaml.Automation; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Windows.UI.Xaml.Automation.Peers; | ||
using Microsoft.Toolkit.Uwp; | ||
using Microsoft.Toolkit.Uwp.UI.Automation.Peers; | ||
using Microsoft.Toolkit.Uwp.UI.Controls; | ||
|
||
namespace UnitTests.UWP.UI.Controls | ||
{ | ||
[TestClass] | ||
[TestCategory("Test_TokenizingTextBox")] | ||
public class Test_TokenizingTextBox_AutomationPeer : VisualUITestBase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's worth adding a test ensuring that the actual list of tokens is also being presented to UIA. Currently, we wouldn't notice if this breaks and as such leaving out crucial information to users using UIA. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies for the huge delay looking into these comments. I've added the additional test for checking the children of the automation peer. To select the items, I used the |
||
{ | ||
[TestMethod] | ||
public async Task ShouldConfigureTokenizingTextBoxAutomationPeerAsync() | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
const string expectedAutomationName = "MyAutomationName"; | ||
const string expectedName = "MyName"; | ||
const string expectedValue = "Wor"; | ||
|
||
var items = new ObservableCollection<TokenizingTextBoxTestItem> { new() { Title = "Hello" }, new() { Title = "World" } }; | ||
|
||
var tokenizingTextBox = new TokenizingTextBox { ItemsSource = items }; | ||
|
||
await SetTestContentAsync(tokenizingTextBox); | ||
|
||
var tokenizingTextBoxAutomationPeer = | ||
FrameworkElementAutomationPeer.CreatePeerForElement(tokenizingTextBox) as TokenizingTextBoxAutomationPeer; | ||
|
||
Assert.IsNotNull(tokenizingTextBoxAutomationPeer, "Verify that the AutomationPeer is TokenizingTextBoxAutomationPeer."); | ||
|
||
// Asserts the automation peer name based on the Automation Property Name value. | ||
tokenizingTextBox.SetValue(AutomationProperties.NameProperty, expectedAutomationName); | ||
Assert.IsTrue(tokenizingTextBoxAutomationPeer.GetName().Contains(expectedAutomationName), "Verify that the UIA name contains the given AutomationProperties.Name of the TokenizingTextBox."); | ||
|
||
// Asserts the automation peer name based on the element Name property. | ||
tokenizingTextBox.Name = expectedName; | ||
Assert.IsTrue(tokenizingTextBoxAutomationPeer.GetName().Contains(expectedName), "Verify that the UIA name contains the given Name of the TokenizingTextBox."); | ||
|
||
tokenizingTextBoxAutomationPeer.SetValue(expectedValue); | ||
Assert.IsTrue(tokenizingTextBoxAutomationPeer.Value.Equals(expectedValue), "Verify that the Value contains the given Text of the TokenizingTextBox."); | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public async Task ShouldReturnTokensForTokenizingTextBoxAutomationPeerAsync() | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
var items = new ObservableCollection<TokenizingTextBoxTestItem> | ||
{ | ||
new() { Title = "Hello" }, new() { Title = "World" } | ||
}; | ||
|
||
var tokenizingTextBox = new TokenizingTextBox { ItemsSource = items }; | ||
|
||
await SetTestContentAsync(tokenizingTextBox); | ||
|
||
tokenizingTextBox | ||
.SelectAllTokensAndText(); // Will be 3 items due to the `AndText` that will select an empty text item. | ||
|
||
var tokenizingTextBoxAutomationPeer = | ||
FrameworkElementAutomationPeer.CreatePeerForElement(tokenizingTextBox) as | ||
TokenizingTextBoxAutomationPeer; | ||
|
||
Assert.IsNotNull( | ||
tokenizingTextBoxAutomationPeer, | ||
"Verify that the AutomationPeer is TokenizingTextBoxAutomationPeer."); | ||
|
||
var selectedItems = tokenizingTextBoxAutomationPeer | ||
.GetChildren() | ||
.Cast<ListViewItemAutomationPeer>() | ||
.Select(peer => peer.Owner as TokenizingTextBoxItem) | ||
.Select(item => item?.Content as TokenizingTextBoxTestItem) | ||
.ToList(); | ||
|
||
Assert.AreEqual(3, selectedItems.Count); | ||
Assert.AreEqual(items[0], selectedItems[0]); | ||
Assert.AreEqual(items[1], selectedItems[1]); | ||
Assert.IsNull(selectedItems[2]); // The 3rd item is the empty text item. | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public async Task ShouldThrowElementNotEnabledExceptionIfValueSetWhenDisabled() | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
const string expectedValue = "Wor"; | ||
|
||
var tokenizingTextBox = new TokenizingTextBox { IsEnabled = false }; | ||
|
||
await SetTestContentAsync(tokenizingTextBox); | ||
|
||
var tokenizingTextBoxAutomationPeer = | ||
FrameworkElementAutomationPeer.CreatePeerForElement(tokenizingTextBox) as TokenizingTextBoxAutomationPeer; | ||
|
||
Assert.ThrowsException<ElementNotEnabledException>(() => | ||
{ | ||
tokenizingTextBoxAutomationPeer.SetValue(expectedValue); | ||
}); | ||
}); | ||
} | ||
|
||
public class TokenizingTextBoxTestItem | ||
{ | ||
public string Title { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return Title; | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.