Skip to content

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

Merged
merged 11 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -2,15 +2,16 @@
// 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;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Deferred;
using Microsoft.Toolkit.Uwp.UI.Automation.Peers;
using Microsoft.Toolkit.Uwp.UI.Helpers;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

Expand Down Expand Up @@ -486,6 +487,15 @@ protected void UpdateCurrentTextEdit(ITokenStringContainer edit)
Text = edit.Text; // Update our text property.
}

/// <summary>
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
/// </summary>
/// <returns>An automation peer for this <see cref="TokenizingTextBox"/>.</returns>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new TokenizingTextBoxAutomationPeer(this);
}

/// <summary>
/// Remove the specified token from the list.
/// </summary>
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 SelectAllTokensAndText method on the TokenizingTextBox but noted in the test, this will select an empty text item and append it to the collection.

{
[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;
}
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
<Compile Include="UI\Controls\Test_RadialGauge.cs" />
<Compile Include="UI\Controls\Test_TextToolbar_Localization.cs" />
<Compile Include="UI\Controls\Test_InfiniteCanvas_Regression.cs" />
<Compile Include="UI\Controls\Test_TokenizingTextBox_AutomationPeer.cs" />
<Compile Include="UI\Controls\Test_TokenizingTextBox_General.cs" />
<Compile Include="UI\Controls\Test_TokenizingTextBox_InterspersedCollection.cs" />
<Compile Include="UI\Controls\Test_ListDetailsView.cs" />
Expand Down