diff --git a/components/Collections/tests/Test_AdvancedCollectionView.cs b/components/Collections/tests/Test_AdvancedCollectionView.cs index 07de59de..9456e6a9 100644 --- a/components/Collections/tests/Test_AdvancedCollectionView.cs +++ b/components/Collections/tests/Test_AdvancedCollectionView.cs @@ -80,6 +80,9 @@ public void Test_SourceNcc_CollectionChanged_Add() ObservableCollection col = new ObservableCollection(); 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()) { @@ -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 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")]