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)); }