Skip to content

Commit

Permalink
Add unit tests for keeping AdvancedCollectionView in sync with source
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlodotexe committed Mar 1, 2024
1 parent 4758f6f commit 7b8f241
Showing 1 changed file with 41 additions and 0 deletions.
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

0 comments on commit 7b8f241

Please sign in to comment.