2014-02-06 05:23:28 -08:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Stylet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-03-11 06:34:42 -07:00
|
|
|
|
using System.Collections.Specialized;
|
2014-02-06 05:23:28 -08:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace StyletUnitTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class BindableCollectionTests
|
|
|
|
|
{
|
2014-03-11 06:34:42 -07:00
|
|
|
|
private class Element { }
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
private class TestDispatcher : IDispatcher
|
|
|
|
|
{
|
|
|
|
|
public Action PostAction;
|
|
|
|
|
public void Post(Action action)
|
|
|
|
|
{
|
|
|
|
|
this.PostAction = action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Action SendAction;
|
|
|
|
|
public void Send(Action action)
|
|
|
|
|
{
|
|
|
|
|
this.SendAction = action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsCurrent
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IDispatcher dispatcher;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
|
|
|
|
this.dispatcher = Execute.Dispatcher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
public void TearDown()
|
|
|
|
|
{
|
|
|
|
|
Execute.Dispatcher = this.dispatcher;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-11 06:34:42 -07:00
|
|
|
|
[Test]
|
2015-01-05 08:52:47 -08:00
|
|
|
|
public void AddRangeUsesDispatcherToAddElements()
|
2014-03-11 06:34:42 -07:00
|
|
|
|
{
|
|
|
|
|
var itemsToAdd = new[] { new Element(), new Element() };
|
|
|
|
|
var existingItems = new[] { new Element() };
|
|
|
|
|
var collection = new BindableCollection<Element>(existingItems);
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
2014-03-11 06:34:42 -07:00
|
|
|
|
collection.AddRange(itemsToAdd);
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.That(collection, Is.EquivalentTo(existingItems));
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
|
|
|
|
|
2014-03-11 06:34:42 -07:00
|
|
|
|
Assert.AreEqual(existingItems.Concat(itemsToAdd), collection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2015-01-05 08:52:47 -08:00
|
|
|
|
public void RemoveRangeUsesDispatcherToRemoveElements()
|
2014-03-11 06:34:42 -07:00
|
|
|
|
{
|
|
|
|
|
var itemsToRemove = new[] { new Element(), new Element() };
|
|
|
|
|
var existingItems = new[] { new Element() };
|
|
|
|
|
var collection = new BindableCollection<Element>(itemsToRemove.Concat(existingItems));
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
2014-03-11 06:34:42 -07:00
|
|
|
|
collection.RemoveRange(itemsToRemove);
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.That(collection, Is.EquivalentTo(itemsToRemove.Concat(existingItems)));
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
|
|
|
|
|
2014-03-11 06:34:42 -07:00
|
|
|
|
Assert.AreEqual(existingItems, collection);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 05:23:28 -08:00
|
|
|
|
[Test]
|
2014-03-11 06:34:42 -07:00
|
|
|
|
public void AddRangeFiresPropertyChanged()
|
2014-02-06 05:23:28 -08:00
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>(new[] { new Element(), new Element() });
|
|
|
|
|
|
|
|
|
|
var changedProperties = new List<string>();
|
|
|
|
|
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => changedProperties.Add(e.PropertyName);
|
|
|
|
|
|
|
|
|
|
collection.AddRange(new[] { new Element(), new Element() });
|
|
|
|
|
|
|
|
|
|
Assert.That(changedProperties, Is.EquivalentTo(new[] { "Count", "Item[]" }));
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-11 06:34:42 -07:00
|
|
|
|
[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];
|
2014-04-23 09:32:57 -07:00
|
|
|
|
Assert.AreEqual(NotifyCollectionChangedAction.Reset, changedEvent.Action);
|
2014-03-11 06:34:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
collection.RemoveRange(itemsToRemove);
|
2014-03-11 06:34:42 -07:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, changedEvents.Count);
|
|
|
|
|
var changedEvent = changedEvents[0];
|
2014-04-23 09:32:57 -07:00
|
|
|
|
Assert.AreEqual(NotifyCollectionChangedAction.Reset, changedEvent.Action);
|
2014-03-11 06:34:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
}
|
2014-05-06 05:11:51 -07:00
|
|
|
|
|
2015-01-04 04:37:33 -08:00
|
|
|
|
[Test]
|
2015-01-05 08:52:47 -08:00
|
|
|
|
public void RefreshUsesDispatcherToFireEvents()
|
2015-01-04 04:37:33 -08:00
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>();
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
bool propertyChangedRaised = false;
|
|
|
|
|
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => propertyChangedRaised = true;
|
|
|
|
|
bool collectionChangedRaised = false;
|
|
|
|
|
collection.CollectionChanged += (o, e) => collectionChangedRaised = true;
|
|
|
|
|
|
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
|
|
|
|
collection.Refresh();
|
|
|
|
|
|
|
|
|
|
Assert.False(propertyChangedRaised);
|
|
|
|
|
Assert.False(collectionChangedRaised);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.True(propertyChangedRaised);
|
|
|
|
|
Assert.True(collectionChangedRaised);
|
2015-01-04 04:37:33 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2015-01-05 08:52:47 -08:00
|
|
|
|
public void InsertItemUsesDispatcherToInsertItem()
|
2015-01-04 04:37:33 -08:00
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>();
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
|
|
|
|
collection.Add(new Element());
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(0, collection.Count);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
dispatcher.SendAction();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, collection.Count);
|
2015-01-04 04:37:33 -08:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 05:11:51 -07:00
|
|
|
|
[Test]
|
2015-01-05 08:52:47 -08:00
|
|
|
|
public void InsertItemUsesDispatcherToRaiseEvents()
|
2014-05-06 05:11:51 -07:00
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>();
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
bool propertyChangedRaised = false;
|
|
|
|
|
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => propertyChangedRaised = true;
|
|
|
|
|
bool collectionChangedRaised = false;
|
|
|
|
|
collection.CollectionChanged += (o, e) => collectionChangedRaised = true;
|
2014-05-06 05:11:51 -07:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
2014-05-06 05:11:51 -07:00
|
|
|
|
|
|
|
|
|
collection.Add(new Element());
|
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.False(propertyChangedRaised);
|
|
|
|
|
Assert.False(collectionChangedRaised);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
2014-05-06 05:11:51 -07:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
dispatcher.SendAction();
|
2014-05-06 05:11:51 -07:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.True(propertyChangedRaised);
|
|
|
|
|
Assert.True(collectionChangedRaised);
|
2015-01-04 04:37:33 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2015-01-05 08:52:47 -08:00
|
|
|
|
public void SetItemUsesDispatcherToSetItems()
|
2015-01-04 04:37:33 -08:00
|
|
|
|
{
|
2015-01-05 08:52:47 -08:00
|
|
|
|
var initialElement = new Element();
|
|
|
|
|
var collection = new BindableCollection<Element>() { initialElement };
|
|
|
|
|
var element = new Element();
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
collection[0] = element;
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.AreEqual(initialElement, collection[0]);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
dispatcher.SendAction();
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.AreEqual(element, collection[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void SetItemUsesDispatcherToRaiseEvents()
|
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>() { new Element() };
|
|
|
|
|
|
|
|
|
|
bool propertyChangedRaised = false;
|
|
|
|
|
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => propertyChangedRaised = true;
|
|
|
|
|
bool collectionChangedRaised = false;
|
|
|
|
|
collection.CollectionChanged += (o, e) => collectionChangedRaised = true;
|
|
|
|
|
|
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
|
|
|
|
collection[0] = new Element();
|
|
|
|
|
|
|
|
|
|
Assert.False(propertyChangedRaised);
|
|
|
|
|
Assert.False(collectionChangedRaised);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
|
|
|
|
|
|
|
|
|
Assert.True(propertyChangedRaised);
|
|
|
|
|
Assert.True(collectionChangedRaised);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RemoveItemUsesDispatcherToRemoveItems()
|
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>() { new Element() };
|
|
|
|
|
|
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
|
|
|
|
collection.RemoveAt(0);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, collection.Count);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(0, collection.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RemoveItemUsesDispatcherToRaiseEvents()
|
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>() { new Element() };
|
|
|
|
|
|
|
|
|
|
bool propertyChangedRaised = false;
|
|
|
|
|
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => propertyChangedRaised = true;
|
|
|
|
|
bool collectionChangedRaised = false;
|
|
|
|
|
collection.CollectionChanged += (o, e) => collectionChangedRaised = true;
|
|
|
|
|
|
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
|
|
|
|
collection.RemoveAt(0);
|
|
|
|
|
|
|
|
|
|
Assert.False(propertyChangedRaised);
|
|
|
|
|
Assert.False(collectionChangedRaised);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
|
|
|
|
|
|
|
|
|
Assert.True(propertyChangedRaised);
|
|
|
|
|
Assert.True(collectionChangedRaised);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ClearItemsUsesDispatcherToClearItems()
|
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>() { new Element() };
|
|
|
|
|
|
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
|
|
|
|
collection.Clear();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, collection.Count);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(0, collection.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ClearItemsUsesDispatcherToRaiseEvents()
|
|
|
|
|
{
|
|
|
|
|
var collection = new BindableCollection<Element>() { new Element() };
|
|
|
|
|
|
|
|
|
|
bool propertyChangedRaised = false;
|
|
|
|
|
((INotifyPropertyChanged)collection).PropertyChanged += (o, e) => propertyChangedRaised = true;
|
|
|
|
|
bool collectionChangedRaised = false;
|
|
|
|
|
collection.CollectionChanged += (o, e) => collectionChangedRaised = true;
|
|
|
|
|
|
|
|
|
|
var dispatcher = new TestDispatcher();
|
|
|
|
|
Execute.Dispatcher = dispatcher;
|
|
|
|
|
|
|
|
|
|
collection.Clear();
|
|
|
|
|
|
|
|
|
|
Assert.False(propertyChangedRaised);
|
|
|
|
|
Assert.False(collectionChangedRaised);
|
|
|
|
|
Assert.NotNull(dispatcher.SendAction);
|
|
|
|
|
|
|
|
|
|
dispatcher.SendAction();
|
2015-01-04 04:37:33 -08:00
|
|
|
|
|
2015-01-05 08:52:47 -08:00
|
|
|
|
Assert.True(propertyChangedRaised);
|
|
|
|
|
Assert.True(collectionChangedRaised);
|
2014-05-06 05:11:51 -07:00
|
|
|
|
}
|
2014-02-06 05:23:28 -08:00
|
|
|
|
}
|
|
|
|
|
}
|