Skip to content

Commit 5b4bd23

Browse files
committed
Added TokenizingTextBox automation peer test for returned child items
1 parent 0f6b415 commit 5b4bd23

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

UnitTests/UnitTests.UWP/UI/Controls/Test_TokenizingTextBox_AutomationPeer.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Collections.Generic;
56
using System.Collections.ObjectModel;
7+
using System.Linq;
68
using System.Threading.Tasks;
79
using Windows.UI.Xaml.Automation;
810
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -50,6 +52,45 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
5052
});
5153
}
5254

55+
[TestMethod]
56+
public async Task ShouldReturnTokensForTokenizingTextBoxAutomationPeerAsync()
57+
{
58+
await App.DispatcherQueue.EnqueueAsync(async () =>
59+
{
60+
var items = new ObservableCollection<TokenizingTextBoxTestItem>
61+
{
62+
new() { Title = "Hello" }, new() { Title = "World" }
63+
};
64+
65+
var tokenizingTextBox = new TokenizingTextBox { ItemsSource = items };
66+
67+
await SetTestContentAsync(tokenizingTextBox);
68+
69+
tokenizingTextBox
70+
.SelectAllTokensAndText(); // Will be 3 items due to the `AndText` that will select an empty text item.
71+
72+
var tokenizingTextBoxAutomationPeer =
73+
FrameworkElementAutomationPeer.CreatePeerForElement(tokenizingTextBox) as
74+
TokenizingTextBoxAutomationPeer;
75+
76+
Assert.IsNotNull(
77+
tokenizingTextBoxAutomationPeer,
78+
"Verify that the AutomationPeer is TokenizingTextBoxAutomationPeer.");
79+
80+
var selectedItems = tokenizingTextBoxAutomationPeer
81+
.GetChildren()
82+
.Cast<ListViewItemAutomationPeer>()
83+
.Select(peer => peer.Owner as TokenizingTextBoxItem)
84+
.Select(item => item?.Content as TokenizingTextBoxTestItem)
85+
.ToList();
86+
87+
Assert.AreEqual(3, selectedItems.Count);
88+
Assert.AreEqual(items[0], selectedItems[0]);
89+
Assert.AreEqual(items[1], selectedItems[1]);
90+
Assert.IsNull(selectedItems[2]); // The 3rd item is the empty text item.
91+
});
92+
}
93+
5394
[TestMethod]
5495
public async Task ShouldThrowElementNotEnabledExceptionIfValueSetWhenDisabled()
5596
{

0 commit comments

Comments
 (0)