public void WhenOperationWasCanceledCollectionShouldNotBeChanged() { SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); collection.Add(new Item { Id = -1 }); collection.CollectionChanging += (sender, args) => args.Cancel = true; var collectionTracker = new NotifiableCollectionTracker <Item>(collection, false); var item = new Item { Id = -2 }; collection.Add(item); collection.Remove(item); collection[0] = item; collection.Clear(); collection.Count.ShouldEqual(1); collection[0].ShouldNotEqual(item); collectionTracker.ChangedItems.SequenceEqual(collection).ShouldBeTrue(); collectionTracker.ChangingItems.SequenceEqual(collection).ShouldBeTrue(); }
public virtual void CollectionShouldTrackChangesCorrectBatchSize() { const int count = 10; SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); collection.BatchSize = 10; var collectionTracker = new NotifiableCollectionTracker <Item>(collection); using (collection.SuspendNotifications()) { var item = new Item(); var items = new[] { new Item(), new Item(), new Item() }; var items2 = new[] { new Item(), new Item(), new Item() }; for (int i = 0; i < count; i++) { collection.AddRange(items); collection.AddRange(items2); collection.RemoveRange(items); } for (int i = 0; i < collection.Count; i++) { collection[i] = item; } } ThreadManagerMock.InvokeOnUiThreadAsync(); collectionTracker.AssertEquals(); collection.Count.ShouldEqual(count * 3); }
public void WhenOperationWasCanceledCollectionShouldNotBeChanged() { SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); collection.Add(new Item { Id = new Guid("0C32E17E-020C-4E05-9B90-AE247B8BE703") }); collection.CollectionChanging += (sender, args) => args.Cancel = true; var collectionTracker = new NotifiableCollectionTracker <Item>(collection); var item = new Item { Id = new Guid("3C39C0C0-DFBA-4683-8473-0950085478E9") }; collection.Add(item); collection.Remove(item); collection[0] = item; collection.Clear(); collection.Count.ShouldEqual(1); collection[0].ShouldNotEqual(item); collectionTracker.ChangedItems.SequenceEqual(collection).ShouldBeTrue(); collectionTracker.ChangingItems.SequenceEqual(collection).ShouldBeTrue(); }
public void CollectionShouldRaiseEventsUsingThreadManager() { SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.AsynchronousOnUiThread, ThreadManagerMock); collection.Add(new Item()); ThreadManagerMock.InvokeOnUiThreadAsync(); var collectionTracker = new NotifiableCollectionTracker <Item>(collection); collectionTracker.AssertEquals(); }
public void CollectionShouldNotRaiseEventsUsingThreadManagerIfModeNone() { SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); collection.Add(new Item()); var collectionTracker = new NotifiableCollectionTracker <Item>(collection); collectionTracker.AssertEquals(); collection.Count.ShouldEqual(1); }
public void CollectionShouldTrackChangesCorrectInSourceCollection() { const int count = 10; var collection = new ObservableCollection <Item>(); var filterableCollection = new FilterableNotifiableCollection <Item>(collection, ThreadManagerMock) { Filter = _ => true }; var collectionTracker = new NotifiableCollectionTracker <Item>(filterableCollection); var item = new Item(); var items = new[] { new Item(), new Item(), new Item() }; var items2 = new[] { new Item { Hidden = true }, new Item(), new Item { Hidden = true } }; for (int i = 0; i < count; i++) { collection.AddRange(items); collection.SequenceEqual(filterableCollection).ShouldBeTrue(); collection.AddRange(items2); collection.SequenceEqual(filterableCollection).ShouldBeTrue(); collection.RemoveRange(items); collection.SequenceEqual(filterableCollection).ShouldBeTrue(); } for (int i = 0; i < collection.Count; i++) { collection[i] = item; collection.SequenceEqual(filterableCollection).ShouldBeTrue(); } ThreadManagerMock.InvokeOnUiThreadAsync(); collectionTracker.AssertChangedEquals(); collection.Count.ShouldEqual(count * items2.Length); collection.Clear(); ThreadManagerMock.InvokeOnUiThreadAsync(); collection.SequenceEqual(filterableCollection).ShouldBeTrue(); collection.Count.ShouldEqual(0); collectionTracker.AssertChangedEquals(); }
public void WhenNotificationSuspendedEventsShouldNotBeRaised() { SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); var collectionTracker = new NotifiableCollectionTracker <Item>(collection); using (collection.SuspendNotifications()) { for (int i = 0; i < 10; i++) { var item = new Item(); collection.Add(item); collection.Remove(item); } using (collection.SuspendNotifications()) { collection.Add(new Item()); } collectionTracker.ChangedItems.ShouldBeEmpty(); } ThreadManagerMock.InvokeOnUiThreadAsync(); collectionTracker.AssertEquals(); }
public void CollectionShouldTrackChangesCorrectWithFilter() { const int count = 10; var collection = (FilterableNotifiableCollection <Item>) CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); collection.Filter = item => !item.Hidden; var collectionTracker = new NotifiableCollectionTracker <Item>(collection); collection.BatchSize = int.MaxValue; using (collection.SuspendNotifications()) { var item = new Item(); var items = new[] { new Item(), new Item(), new Item() }; var items2 = new[] { new Item { Hidden = true }, new Item(), new Item { Hidden = true } }; for (int i = 0; i < count; i++) { collection.AddRange(items); collection.AddRange(items2); collection.RemoveRange(items); } for (int i = 0; i < collection.Count; i++) { collection[i] = item; } } collectionTracker.ChangingItems.OrderBy(item => item.Id) .SequenceEqual(collection.SourceCollection.OrderBy(item => item.Id)) .ShouldBeTrue(); collectionTracker.AssertChangedEquals(); collection.Count.ShouldEqual(count); }
public override void CollectionShouldTrackChangesCorrect() { const int count = 10; SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); var collectionTracker = new NotifiableCollectionTracker <Item>(collection); collection.BatchSize = int.MaxValue; var items = new[] { new Item(), new Item(), new Item() }; var items2 = new[] { new Item(), new Item(), new Item() }; using (collection.SuspendNotifications()) { for (int i = 0; i < count; i++) { collection.AddRange(items); collection.AddRange(items2); collection.RemoveRange(items); } } collectionTracker.AssertEquals(); collection.Count.ShouldEqual(count * 3); }
public void FilterShouldCorrectNotifyAboutChanges() { ThreadManagerMock.IsUiThread = true; var item = new Item(); var item2 = new Item(); var collection = (FilterableNotifiableCollection <Item>)CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock); var tracker = new NotifiableCollectionTracker <Item>(collection); collection.Add(item); collection.Contains(item).ShouldBeTrue(); collection.Clear(); tracker.AssertChangedEquals(); collection.Filter = item1 => false; collection.Add(item); collection.Contains(item).ShouldBeFalse(); tracker.AssertChangedEquals(); collection.Filter = null; collection.Contains(item).ShouldBeTrue(); tracker.AssertChangedEquals(); collection.Add(item); collection.Remove(item).ShouldBeTrue(); collection.Contains(item).ShouldBeTrue(); tracker.AssertChangedEquals(); collection.Filter = item1 => false; collection.Add(item); collection.Remove(item).ShouldBeFalse(); tracker.AssertChangedEquals(); collection.Filter = null; collection.Contains(item).ShouldBeTrue(); tracker.AssertChangedEquals(); collection.Clear(); collection.Add(item); collection.Add(item2); collection.Filter = item1 => item == item1; tracker.AssertChangedEquals(); collection[0] = item2; collection.Count.ShouldEqual(0); tracker.AssertChangedEquals(); collection.Filter = null; collection[0].ShouldEqual(item2); tracker.AssertChangedEquals(); collection.Clear(); collection.Add(item); collection.Add(item2); collection.Filter = item1 => item == item1; tracker.AssertChangedEquals(); collection.IsClearIgnoreFilter = true; collection.Clear(); collection.Filter = null; collection.Count.ShouldEqual(0); tracker.AssertChangedEquals(); collection.Add(item); collection.Add(item2); collection.Filter = item1 => item == item1; tracker.AssertChangedEquals(); collection.IsClearIgnoreFilter = false; collection.Clear(); collection.Filter = null; collection.Count.ShouldEqual(1); tracker.AssertChangedEquals(); }