Small fices to BindableCollection, and finish its unit test suite

This commit is contained in:
Antony Male 2014-03-11 13:34:42 +00:00
parent 228147b634
commit 877a9b87a4
2 changed files with 129 additions and 38 deletions

View File

@ -26,10 +26,7 @@ namespace Stylet
protected void NotifyOfPropertyChange([CallerMemberName] string propertyName = "")
{
if (this.isNotifying)
Execute.OnUIThread(() =>
{
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
});
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
@ -46,50 +43,43 @@ namespace Stylet
public virtual void AddRange(IEnumerable<T> items)
{
Execute.OnUIThread(() =>
var previousNotificationSetting = this.isNotifying;
this.isNotifying = false;
var index = Count;
foreach (var item in items)
{
var previousNotificationSetting = this.isNotifying;
this.isNotifying = false;
var index = Count;
foreach (var item in items)
{
this.InsertItem(index, item);
index++;
}
this.isNotifying = previousNotificationSetting;
this.Refresh();
});
this.InsertItem(index, item);
index++;
}
this.isNotifying = previousNotificationSetting;
this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items.ToList()));
}
public virtual void RemoveRange(IEnumerable<T> items)
{
Execute.OnUIThread(() =>
var previousNotificationSetting = this.isNotifying;
this.isNotifying = false;
foreach (var item in items)
{
var previousNotificationSetting = this.isNotifying;
this.isNotifying = false;
foreach (var item in items)
var index = IndexOf(item);
if (index >= 0)
{
var index = IndexOf(item);
if (index >= 0)
{
this.RemoveItem(index);
}
this.RemoveItem(index);
}
this.isNotifying = previousNotificationSetting;
this.Refresh();
});
}
this.isNotifying = previousNotificationSetting;
this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items.ToList()));
}
public void Refresh()
{
Execute.OnUIThread(() =>
{
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
}

View File

@ -2,6 +2,7 @@
using Stylet;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
@ -13,8 +14,34 @@ namespace StyletUnitTests
[TestFixture]
public class BindableCollectionTests
{
private class Element { }
[Test]
public void AddrangeFiresPropertyChanged()
public void AddRangeAddsElements()
{
var itemsToAdd = new[] { new Element(), new Element() };
var existingItems = new[] { new Element() };
var collection = new BindableCollection<Element>(existingItems);
collection.AddRange(itemsToAdd);
Assert.AreEqual(existingItems.Concat(itemsToAdd), collection);
}
[Test]
public void RemoveRangeRemovesElements()
{
var itemsToRemove = new[] { new Element(), new Element() };
var existingItems = new[] { new Element() };
var collection = new BindableCollection<Element>(itemsToRemove.Concat(existingItems));
collection.RemoveRange(itemsToRemove);
Assert.AreEqual(existingItems, collection);
}
[Test]
public void AddRangeFiresPropertyChanged()
{
var collection = new BindableCollection<Element>(new[] { new Element(), new Element() });
@ -26,6 +53,80 @@ namespace StyletUnitTests
Assert.That(changedProperties, Is.EquivalentTo(new[] { "Count", "Item[]" }));
}
private class Element { }
[Test]
public void AddRangeFiresCollectionChanged()
{
var collection = new BindableCollection<Element>(new[] { new Element(), new Element() });
var changedEvents = new List<NotifyCollectionChangedEventArgs>();
collection.CollectionChanged += (o, e) => changedEvents.Add(e);
var elementsToAdd = new[] { new Element(), new Element() };
collection.AddRange(elementsToAdd);
Assert.AreEqual(1, changedEvents.Count);
var changedEvent = changedEvents[0];
Assert.AreEqual(NotifyCollectionChangedAction.Add, changedEvent.Action);
Assert.AreEqual(elementsToAdd, changedEvent.NewItems);
}
[Test]
public void RemoveRangeFiresPropertyChanged()
{
var itemsToRemove = new[] { new Element(), new Element() };
var collection = new BindableCollection<Element>(new[] { new Element() }.Concat(itemsToRemove));
var changedProperties = new List<string>();
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => changedProperties.Add(e.PropertyName);
collection.RemoveRange(itemsToRemove);
Assert.That(changedProperties, Is.EquivalentTo(new[] { "Count", "Item[]" }));
}
[Test]
public void RemoveRangeFiresCollectionChanged()
{
var itemsToRemove = new[] { new Element(), new Element() };
var collection = new BindableCollection<Element>(new[] { new Element() }.Concat(itemsToRemove));
var changedEvents = new List<NotifyCollectionChangedEventArgs>();
collection.CollectionChanged += (o, e) => changedEvents.Add(e);
collection.AddRange(itemsToRemove);
Assert.AreEqual(1, changedEvents.Count);
var changedEvent = changedEvents[0];
Assert.AreEqual(NotifyCollectionChangedAction.Add, changedEvent.Action);
Assert.AreEqual(itemsToRemove, changedEvent.NewItems);
}
[Test]
public void RefreshFiresPropertyChanged()
{
var collection = new BindableCollection<Element>(new[] { new Element() });
var changedProperties = new List<string>();
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => changedProperties.Add(e.PropertyName);
collection.Refresh();
Assert.That(changedProperties, Is.EquivalentTo(new[] { "Count", "Item[]" }));
}
[Test]
public void RefreshFiresCollectionChanged()
{
var collection = new BindableCollection<Element>(new[] { new Element() });
var changedEvents = new List<NotifyCollectionChangedEventArgs>();
collection.CollectionChanged += (o, e) => changedEvents.Add(e);
collection.Refresh();
Assert.AreEqual(1, changedEvents.Count);
var changedEvent = changedEvents[0];
Assert.AreEqual(NotifyCollectionChangedAction.Reset, changedEvent.Action);
}
}
}