public void DisposeTest() { // Calling dispose twice must not throw an exception. var originalCollection = new ObservableCollection <MyModel>(); bool factoryCalled = false; var synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => { factoryCalled = true; return(new MyDataModel(m)); }); originalCollection.Add(new MyModel()); Assert.IsTrue(factoryCalled); synchronizingCollection.Dispose(); synchronizingCollection.Dispose(); factoryCalled = false; originalCollection.Add(new MyModel()); Assert.IsFalse(factoryCalled); // Check that no memory leak occurs var weakSynchronizingCollection = WeakTest(originalCollection); GC.Collect(); Assert.IsFalse(weakSynchronizingCollection.IsAlive);
public void PropertyChangedTest() { var originalCollection = new ObservableCollection <MyModel>(); using var synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m)); // Check that the PropertyChanged event for Count is raised. bool handlerCalled = false; PropertyChangedEventHandler handler = (sender, e) => { Assert.AreEqual(synchronizingCollection, sender); if (e.PropertyName == nameof(SynchronizingCollectionCore <MyDataModel, MyModel> .Count)) { handlerCalled = true; } }; synchronizingCollection.PropertyChanged += handler; originalCollection.Add(new MyModel()); synchronizingCollection.PropertyChanged -= handler; Assert.IsTrue(handlerCalled); // Check that after unwiring the event the handler is not called anymore. handlerCalled = false; originalCollection.Add(new MyModel()); Assert.IsFalse(handlerCalled); }
public void DisposeTest() { // Calling dispose twice must not throw an exception. var originalCollection = new ObservableCollection <MyModel>(); bool factoryCalled = false; var synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => { factoryCalled = true; return(new MyDataModel(m)); }); originalCollection.Add(new MyModel()); Assert.IsTrue(factoryCalled); synchronizingCollection.Dispose(); synchronizingCollection.Dispose(); factoryCalled = false; originalCollection.Add(new MyModel()); Assert.IsFalse(factoryCalled); // Check that no memory leak occurs synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m)); var weakSynchronizingCollection = new WeakReference(synchronizingCollection); originalCollection.Add(new MyModel()); Assert.IsTrue(weakSynchronizingCollection.IsAlive); synchronizingCollection.Dispose(); synchronizingCollection = null; GC.Collect(); Assert.IsNotNull(originalCollection); #if !NETCOREAPP2_1 || !DEBUG Assert.IsFalse(weakSynchronizingCollection.IsAlive); #endif }
static WeakReference WeakTest(ObservableCollection <MyModel> originalCollection) { var synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m)); var weakSynchronizingCollection = new WeakReference(synchronizingCollection); originalCollection.Add(new MyModel()); Assert.IsTrue(weakSynchronizingCollection.IsAlive); synchronizingCollection.Dispose(); return(weakSynchronizingCollection); }
public void DisposeTest() { // Calling dispose twice must not throw an exception. var originalCollection = new ObservableCollection <MyModel>(); var synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m)); synchronizingCollection.Dispose(); synchronizingCollection.Dispose(); synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m)); WeakReference weakSynchronizingCollection = new WeakReference(synchronizingCollection); originalCollection.Add(new MyModel()); Assert.IsTrue(weakSynchronizingCollection.IsAlive); synchronizingCollection.Dispose(); synchronizingCollection = null; GC.Collect(); Assert.IsNotNull(originalCollection); Assert.IsFalse(weakSynchronizingCollection.IsAlive); }
public void ObservableCollectionTest() { var originalCollection = new ObservableCollection <MyModel>() { new MyModel(), new MyModel(), new MyModel() }; using var synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m)); AssertHelper.SequenceEqual(originalCollection, synchronizingCollection.Select(dm => dm.Model)); // Check add operation with collection changed event. bool handlerCalled = false; NotifyCollectionChangedEventHandler handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action); Assert.AreEqual(3, e.NewStartingIndex); Assert.AreEqual(originalCollection.Last(), e.NewItems.Cast <MyDataModel>().Single().Model); }; synchronizingCollection.CollectionChanged += handler; originalCollection.Add(new MyModel()); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); // Check insert at index 0 operation with collection changed event. handlerCalled = false; handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action); Assert.AreEqual(0, e.NewStartingIndex); Assert.AreEqual(originalCollection.First(), e.NewItems.Cast <MyDataModel>().Single().Model); }; synchronizingCollection.CollectionChanged += handler; originalCollection.Insert(0, new MyModel()); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); // Compare the collections AssertHelper.SequenceEqual(originalCollection, synchronizingCollection.Select(dm => dm.Model)); // Check remove operation with collection changed event. MyModel itemToRemove = originalCollection[2]; handlerCalled = false; handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Remove, e.Action); Assert.AreEqual(2, e.OldStartingIndex); Assert.AreEqual(itemToRemove, e.OldItems.Cast <MyDataModel>().Single().Model); }; synchronizingCollection.CollectionChanged += handler; originalCollection.Remove(itemToRemove); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); // Check replace operation with collection changed event. MyModel itemToReplace = originalCollection[1]; handlerCalled = false; handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Replace, e.Action); Assert.AreEqual(1, e.NewStartingIndex); Assert.AreEqual(1, e.OldStartingIndex); Assert.AreEqual(originalCollection[1], e.NewItems.Cast <MyDataModel>().Single().Model); Assert.AreEqual(itemToReplace, e.OldItems.Cast <MyDataModel>().Single().Model); }; synchronizingCollection.CollectionChanged += handler; originalCollection[1] = new MyModel(); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); // Check move operation with collection changed event. handlerCalled = false; handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Move, e.Action); Assert.AreEqual(0, e.OldStartingIndex); Assert.AreEqual(2, e.NewStartingIndex); }; synchronizingCollection.CollectionChanged += handler; originalCollection.Move(0, 2); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); // Check clear operation with collection changed event. handlerCalled = false; handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Reset, e.Action); }; synchronizingCollection.CollectionChanged += handler; originalCollection.Clear(); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); Assert.IsFalse(synchronizingCollection.Any()); }
public void CustomCollectionTest() { var originalCollection = new CustomCollection <MyModel>(true) { new MyModel(), new MyModel(), new MyModel() }; using var synchronizingCollection = new SynchronizingCollectionCore <MyDataModel, MyModel>( originalCollection, m => new MyDataModel(m)); AssertHelper.SequenceEqual(originalCollection, synchronizingCollection.Select(dm => dm.Model)); // Check add operation with collection changed event. bool handlerCalled = false; NotifyCollectionChangedEventHandler handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action); Assert.AreEqual(3, e.NewStartingIndex); Assert.AreEqual(originalCollection.Last(), e.NewItems.Cast <MyDataModel>().Single().Model); }; synchronizingCollection.CollectionChanged += handler; originalCollection.Add(new MyModel()); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); // Compare the collections AssertHelper.SequenceEqual(originalCollection, synchronizingCollection.Select(dm => dm.Model)); // Check remove operation with collection changed event. MyModel itemToRemove = originalCollection[2]; handlerCalled = false; handler = (sender, e) => { handlerCalled = true; Assert.AreEqual(synchronizingCollection, sender); Assert.AreEqual(NotifyCollectionChangedAction.Remove, e.Action); Assert.AreEqual(2, e.OldStartingIndex); Assert.AreEqual(itemToRemove, e.OldItems.Cast <MyDataModel>().Single().Model); }; synchronizingCollection.CollectionChanged += handler; originalCollection.Remove(itemToRemove); synchronizingCollection.CollectionChanged -= handler; Assert.IsTrue(handlerCalled); // Check replace operation with collection changed event. MyModel itemToReplace = originalCollection[1]; int handlerCalledCount = 0; handler = (sender, e) => { Assert.AreEqual(synchronizingCollection, sender); if (handlerCalledCount == 0) { Assert.AreEqual(NotifyCollectionChangedAction.Remove, e.Action); Assert.AreEqual(itemToReplace, e.OldItems.Cast <MyDataModel>().Single().Model); } else { Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action); Assert.AreEqual(originalCollection[1], e.NewItems.Cast <MyDataModel>().Single().Model); } handlerCalledCount++; }; synchronizingCollection.CollectionChanged += handler; originalCollection[1] = new MyModel(); synchronizingCollection.CollectionChanged -= handler; Assert.AreEqual(2, handlerCalledCount); // Check reset operation with collection changed event. var newItems = new List <MyModel>() { new MyModel(), new MyModel() }; handlerCalledCount = 0; handler = (sender, e) => { Assert.AreEqual(synchronizingCollection, sender); if (handlerCalledCount == 0) { Assert.AreEqual(NotifyCollectionChangedAction.Reset, e.Action); } else { Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action); } handlerCalledCount++; }; synchronizingCollection.CollectionChanged += handler; originalCollection.Reset(newItems); synchronizingCollection.CollectionChanged -= handler; Assert.AreEqual(3, handlerCalledCount); AssertHelper.SequenceEqual(newItems, synchronizingCollection.Select(dm => dm.Model)); }