From f62cd4d6c153f9662611063adfb2e2e21786130b Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Thu, 19 Dec 2024 18:09:52 +0900 Subject: [PATCH 1/2] Add failing tests --- .../Bindables/BindableListTest.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/osu.Framework.Tests/Bindables/BindableListTest.cs b/osu.Framework.Tests/Bindables/BindableListTest.cs index 10d46d4649..a900b29b74 100644 --- a/osu.Framework.Tests/Bindables/BindableListTest.cs +++ b/osu.Framework.Tests/Bindables/BindableListTest.cs @@ -476,6 +476,17 @@ public void TestAddRangeBranchingBinds() Assert.That(b4.Count, Is.EqualTo(7)); } + [Test] + public void TestAddHashSetWithCallback() + { + var b1 = new BindableList(); + + b1.CollectionChanged += (_, _) => { }; + b1.AddRange(new HashSet([1, 2, 3, 4])); + + Assert.That(b1.Count, Is.EqualTo(4)); + } + #endregion #region .Move(item) @@ -1193,6 +1204,17 @@ public void TestReplaceRangeNotifiesBoundLists() Assert.That(triggeredArgs.OldStartingIndex, Is.EqualTo(0)); } + [Test] + public void TestReplaceRangeHashSetWithCallback() + { + var b1 = new BindableList([1]); + + b1.CollectionChanged += (_, _) => { }; + b1.ReplaceRange(0, 1, new HashSet([1, 2, 3, 4])); + + Assert.That(b1.Count, Is.EqualTo(4)); + } + #endregion #region .Clear() From 3ac37de16dfb06344956a177caadb97c83730f5b Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Thu, 19 Dec 2024 18:12:16 +0900 Subject: [PATCH 2/2] Fix BindableList methods crashing with non-IList collections --- osu.Framework/Bindables/BindableList.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/osu.Framework/Bindables/BindableList.cs b/osu.Framework/Bindables/BindableList.cs index 4f770fcef9..541a91232c 100644 --- a/osu.Framework/Bindables/BindableList.cs +++ b/osu.Framework/Bindables/BindableList.cs @@ -320,9 +320,9 @@ private int removeAll(Predicate match, HashSet> appliedInstan /// The count of items to be removed. /// The items to replace the removed items with. public void ReplaceRange(int index, int count, IEnumerable newItems) - => replaceRange(index, count, newItems as ICollection ?? newItems.ToArray(), new HashSet>()); + => replaceRange(index, count, newItems as IList ?? newItems.ToArray(), new HashSet>()); - private void replaceRange(int index, int count, ICollection newItems, HashSet> appliedInstances) + private void replaceRange(int index, int count, IList newItems, HashSet> appliedInstances) { if (checkAlreadyApplied(appliedInstances)) return; @@ -335,7 +335,7 @@ private void replaceRange(int index, int count, ICollection newItems, HashSet List removedItems = CollectionChanged == null ? null : collection.GetRange(index, count); collection.RemoveRange(index, count); - collection.InsertRange(index, newItems); + collection.InsertRange(index, (IEnumerable)newItems); if (bindings != null) { @@ -347,7 +347,7 @@ private void replaceRange(int index, int count, ICollection newItems, HashSet } } - CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, (IList)newItems, removedItems!, index)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, removedItems!, index)); } /// @@ -544,15 +544,15 @@ public virtual void UnbindFrom(IUnbindable them) /// The collection whose items should be added to this collection. /// Thrown if this collection is public void AddRange(IEnumerable items) - => addRange(items as ICollection ?? items.ToArray(), new HashSet>()); + => addRange(items as IList ?? items.ToArray(), new HashSet>()); - private void addRange(ICollection items, HashSet> appliedInstances) + private void addRange(IList items, HashSet> appliedInstances) { if (checkAlreadyApplied(appliedInstances)) return; ensureMutationAllowed(); - collection.AddRange(items); + collection.AddRange((IEnumerable)items); if (bindings != null) { @@ -560,7 +560,7 @@ private void addRange(ICollection items, HashSet> appliedInst b.addRange(items, appliedInstances); } - CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, collection.Count - items.Count)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items, collection.Count - items.Count)); } ///