Skip to content
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

Fix: AdvancedCollection inserts at wrong position when adding to end of source collection #309

Merged
merged 6 commits into from
Mar 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ private bool HandleItemAdded(int newStartingIndex, object newItem, int? viewInde
}
else if (newStartingIndex == _source.Count - 1)
{
newViewIndex = _view.Count - 1;
newViewIndex = _view.Count;
}
else if (viewIndex.HasValue)
{
Expand Down
41 changes: 41 additions & 0 deletions components/Collections/tests/Test_AdvancedCollectionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public void Test_SourceNcc_CollectionChanged_Add()
ObservableCollection<SampleClass> col = new ObservableCollection<SampleClass>();
AdvancedCollectionView acv = new AdvancedCollectionView(col, true);

Assert.AreEqual(0, col.Count);
Assert.AreEqual(0, acv.Count);

// Add all items to collection while DeferRefresh() is active:
using (acv.DeferRefresh())
{
Expand All @@ -89,11 +92,49 @@ public void Test_SourceNcc_CollectionChanged_Add()
}
}

Assert.AreNotEqual(0, col.Count);
Assert.AreNotEqual(0, acv.Count);

Assert.AreEqual(refList.Count, col.Count);
Assert.AreEqual(refList.Count, acv.Count);

// Make sure each item added to source is in the expected place in the view:
for (int i = 0; i < refList.Count; i++)
{
var sourceItem = col[i];
var collectionViewItem = (SampleClass)acv[i];

Assert.AreEqual(sourceItem.Val, collectionViewItem.Val);
}

// Check if subscribed to all items:
foreach (var item in refList)
{
Assert.IsTrue(item.GetPropertyChangedEventHandlerSubscriberLength() == 1);
}

// Add a filter and item to filter
var itemToFilter = new SampleClass(1000);
Func<object, bool> filter = x => ((SampleClass)x).Val == itemToFilter.Val;
acv.Filter = x => !filter(x);
col.Add(itemToFilter);

var filteredItemInAcv = (SampleClass?)acv.FirstOrDefault(filter);
Assert.IsNull(filteredItemInAcv, $"Filtered item unexpectedly included in collection view: {filteredItemInAcv?.Val}.");

col.Remove(itemToFilter);

// With filter set, create and add an item that isn't filtered
var itemToNotFilter = new SampleClass(itemToFilter.Val + 1);
col.Add(itemToNotFilter);

var addedItemInAcv = (SampleClass?)acv.FirstOrDefault(x => ((SampleClass)x).Val == itemToNotFilter.Val);

Assert.IsNotNull(addedItemInAcv, $"Unfiltered item unexpectedly filtered out of collection view: {addedItemInAcv?.Val}.");

// Ensure added (not filtered) item is added to the last position in view.
var indexOfNotFilteredItemInAcv = acv.IndexOf(addedItemInAcv);
Assert.AreEqual(acv.Count - 1, indexOfNotFilteredItemInAcv, "Unfiltered item added to source collection not last in view.");
}

[TestCategory("Helpers")]
Expand Down
Loading