#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
#if UNITTEST
using NUnit.Framework;
using NMock2;
namespace Nuclex.Support.Collections {
/// Unit Test for the observable collection class
[TestFixture]
public class ObservableCollectionTest {
#region interface IObservableCollectionSubscriber
/// Interface used to test the observable collection
public interface IObservableCollectionSubscriber {
/// Called when the collection is about to clear its contents
/// Collection that is clearing its contents
/// Not used
void Clearing(object sender, EventArgs arguments);
/// Called when an item is added to the collection
/// Collection to which an item is being added
/// Contains the item that is being added
void ItemAdded(object sender, ItemEventArgs arguments);
/// Called when an item is removed from the collection
/// Collection from which an item is being removed
/// Contains the item that is being removed
void ItemRemoved(object sender, ItemEventArgs arguments);
}
#endregion // interface IObservableCollectionSubscriber
/// Initialization routine executed before each test is run
[SetUp]
public void Setup() {
this.mockery = new Mockery();
this.mockedSubscriber = this.mockery.NewMock();
this.observedCollection = new ObservableCollection();
this.observedCollection.Clearing += new EventHandler(this.mockedSubscriber.Clearing);
this.observedCollection.ItemAdded +=
new EventHandler>(
this.mockedSubscriber.ItemAdded
);
this.observedCollection.ItemRemoved +=
new EventHandler>(
this.mockedSubscriber.ItemRemoved
);
}
/// Tests whether the Clearing event is fired
[Test]
public void TestClearingEvent() {
Expect.Once.On(this.mockedSubscriber).
Method("Clearing");
this.observedCollection.Clear();
this.mockery.VerifyAllExpectationsHaveBeenMet();
}
/// Tests whether the ItemAdded event is fired
[Test]
public void TestItemAddedEvent() {
Expect.Once.On(this.mockedSubscriber).
Method("ItemAdded").
WithAnyArguments();
this.observedCollection.Add(123);
this.mockery.VerifyAllExpectationsHaveBeenMet();
}
/// Tests whether the ItemRemoved event is fired
[Test]
public void TestItemRemovedEvent() {
Expect.Once.On(this.mockedSubscriber).
Method("ItemAdded").
WithAnyArguments();
this.observedCollection.Add(123);
Expect.Once.On(this.mockedSubscriber).
Method("ItemRemoved").
WithAnyArguments();
this.observedCollection.Remove(123);
this.mockery.VerifyAllExpectationsHaveBeenMet();
}
/// Tests whether items in the collection can be replaced
[Test]
public void TestItemReplacement() {
Expect.Exactly(3).On(this.mockedSubscriber).
Method("ItemAdded").
WithAnyArguments();
this.observedCollection.Add(1);
this.observedCollection.Add(2);
this.observedCollection.Add(3);
Expect.Once.On(this.mockedSubscriber).
Method("ItemRemoved").
WithAnyArguments();
Expect.Once.On(this.mockedSubscriber).
Method("ItemAdded").
WithAnyArguments();
// Replace the middle item with something else
this.observedCollection[1] = 4;
Assert.AreEqual(
1,
this.observedCollection.IndexOf(4)
);
this.mockery.VerifyAllExpectationsHaveBeenMet();
}
/// Tests whether the ItemRemoved event is fired
[Test]
public void TestListConstructor() {
int[] integers = new int[] { 12, 34, 56, 78 };
ObservableCollection testCollection = new ObservableCollection(integers);
CollectionAssert.AreEqual(
integers,
testCollection
);
}
/// Mock object factory
private Mockery mockery;
/// The mocked observable collection subscriber
private IObservableCollectionSubscriber mockedSubscriber;
/// An observable collection to which a mock will be subscribed
private ObservableCollection observedCollection;
}
} // namespace Nuclex.Support.Collections
#endif // UNITTEST