From 3f96aead414975c369d58eec8fed2ddb906736cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Osman=20=C4=B0l=C3=A7ektu=C4=9F?= <57036326+osmanilcektu@users.noreply.github.com> Date: Mon, 14 Sep 2026 13:52:32 +0300 Subject: [PATCH 1/2] test: cover multi-item TokenizingTextBox collection changes --- ...est_TokenizingTextBox_CollectionChanges.cs | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 components/TokenizingTextBox/tests/Test_TokenizingTextBox_CollectionChanges.cs diff --git a/components/TokenizingTextBox/tests/Test_TokenizingTextBox_CollectionChanges.cs b/components/TokenizingTextBox/tests/Test_TokenizingTextBox_CollectionChanges.cs new file mode 100644 index 00000000..270ec45f --- /dev/null +++ b/components/TokenizingTextBox/tests/Test_TokenizingTextBox_CollectionChanges.cs @@ -0,0 +1,98 @@ +// 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.Collections.Specialized; +using System.ComponentModel; +using CommunityToolkit.Tests; +using CommunityToolkit.WinUI.Controls; + +namespace TokenizingTextBoxTests; + +[TestClass] +public class Test_TokenizingTextBox_CollectionChanges : VisualUITestBase +{ + [TestCategory("Test_TokenizingTextBox_CollectionChanges")] + [TestMethod] + public async Task Test_ItemsSourceRangeAdd() + { + await App.DispatcherQueue.EnqueueAsync(() => + { + var source = new RangeObservableCollection { 0, 3 }; + var tokenBox = new TokenizingTextBox { ItemsSource = source }; + + source.InsertRange(1, new object[] { 1, 2 }); + + Assert.AreEqual(4, source.Count); + Assert.AreEqual(5, tokenBox.Items.Count); + Assert.AreEqual(0, tokenBox.Items[0]); + Assert.AreEqual(1, tokenBox.Items[1]); + Assert.AreEqual(2, tokenBox.Items[2]); + Assert.AreEqual(3, tokenBox.Items[3]); + }); + } + + [TestCategory("Test_TokenizingTextBox_CollectionChanges")] + [TestMethod] + public async Task Test_ItemsSourceRangeRemove() + { + await App.DispatcherQueue.EnqueueAsync(() => + { + var source = new RangeObservableCollection { 0, 1, 2, 3 }; + var tokenBox = new TokenizingTextBox { ItemsSource = source }; + + source.RemoveRange(1, 2); + + Assert.AreEqual(2, source.Count); + Assert.AreEqual(3, tokenBox.Items.Count); + Assert.AreEqual(0, tokenBox.Items[0]); + Assert.AreEqual(3, tokenBox.Items[1]); + }); + } + + private sealed class RangeObservableCollection : ObservableCollection + { + public void InsertRange(int index, IReadOnlyList items) + { + if (items.Count == 0) + { + return; + } + + CheckReentrancy(); + + var insertedItems = new List(items); + for (var offset = 0; offset < insertedItems.Count; offset++) + { + Items.Insert(index + offset, insertedItems[offset]); + } + + OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); + OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, insertedItems, index)); + } + + public void RemoveRange(int index, int count) + { + if (count == 0) + { + return; + } + + CheckReentrancy(); + + var removedItems = new List(count); + for (var offset = 0; offset < count; offset++) + { + removedItems.Add(Items[index]); + Items.RemoveAt(index); + } + + OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); + OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItems, index)); + } + } +} From 0006c60709db6d3b9db82c52534c45da408e935a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Osman=20=C4=B0l=C3=A7ektu=C4=9F?= <57036326+osmanilcektu@users.noreply.github.com> Date: Mon, 14 Sep 2026 13:52:42 +0300 Subject: [PATCH 2/2] test: include TokenizingTextBox collection change tests --- .../TokenizingTextBox/tests/TokenizingTextBox.Tests.projitems | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/TokenizingTextBox/tests/TokenizingTextBox.Tests.projitems b/components/TokenizingTextBox/tests/TokenizingTextBox.Tests.projitems index efd6469b..66361f6b 100644 --- a/components/TokenizingTextBox/tests/TokenizingTextBox.Tests.projitems +++ b/components/TokenizingTextBox/tests/TokenizingTextBox.Tests.projitems @@ -1,4 +1,4 @@ - + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) @@ -11,5 +11,6 @@ + \ No newline at end of file