2007-05-11 21:15:35 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2013-04-25 13:11:59 +00:00
|
|
|
Copyright (C) 2002-2013 Nuclex Development Labs
|
2007-05-11 21:15:35 +00:00
|
|
|
|
|
|
|
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
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
#if UNITTEST
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
2011-03-08 20:36:43 +00:00
|
|
|
using NMock;
|
2007-04-18 20:20:03 +00:00
|
|
|
|
|
|
|
namespace Nuclex.Support.Collections {
|
|
|
|
|
|
|
|
/// <summary>Unit Test for the observable collection class</summary>
|
|
|
|
[TestFixture]
|
2012-02-29 16:27:43 +00:00
|
|
|
internal class ObservableCollectionTest {
|
2007-04-18 20:20:03 +00:00
|
|
|
|
2007-07-10 19:25:18 +00:00
|
|
|
#region interface IObservableCollectionSubscriber
|
|
|
|
|
2009-01-26 20:05:32 +00:00
|
|
|
/// <summary>Interface used to test the observable collection</summary>
|
2007-04-18 20:20:03 +00:00
|
|
|
public interface IObservableCollectionSubscriber {
|
|
|
|
|
|
|
|
/// <summary>Called when the collection is about to clear its contents</summary>
|
|
|
|
/// <param name="sender">Collection that is clearing its contents</param>
|
2009-01-26 20:05:32 +00:00
|
|
|
/// <param name="arguments">Not used</param>
|
|
|
|
void Clearing(object sender, EventArgs arguments);
|
2007-04-18 20:20:03 +00:00
|
|
|
|
2009-10-12 19:19:58 +00:00
|
|
|
/// <summary>Called when the collection has been cleared of its contents</summary>
|
|
|
|
/// <param name="sender">Collection that was cleared of its contents</param>
|
|
|
|
/// <param name="arguments">Not used</param>
|
|
|
|
void Cleared(object sender, EventArgs arguments);
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>Called when an item is added to the collection</summary>
|
|
|
|
/// <param name="sender">Collection to which an item is being added</param>
|
2009-01-26 20:05:32 +00:00
|
|
|
/// <param name="arguments">Contains the item that is being added</param>
|
|
|
|
void ItemAdded(object sender, ItemEventArgs<int> arguments);
|
2007-04-18 20:20:03 +00:00
|
|
|
|
|
|
|
/// <summary>Called when an item is removed from the collection</summary>
|
|
|
|
/// <param name="sender">Collection from which an item is being removed</param>
|
2009-01-26 20:05:32 +00:00
|
|
|
/// <param name="arguments">Contains the item that is being removed</param>
|
|
|
|
void ItemRemoved(object sender, ItemEventArgs<int> arguments);
|
2007-04-18 20:20:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-07-10 19:25:18 +00:00
|
|
|
#endregion // interface IObservableCollectionSubscriber
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>Initialization routine executed before each test is run</summary>
|
|
|
|
[SetUp]
|
|
|
|
public void Setup() {
|
2011-03-08 20:36:43 +00:00
|
|
|
this.mockery = new MockFactory();
|
2007-04-18 20:20:03 +00:00
|
|
|
|
2011-03-08 20:36:43 +00:00
|
|
|
this.mockedSubscriber = this.mockery.CreateMock<IObservableCollectionSubscriber>();
|
2007-04-18 20:20:03 +00:00
|
|
|
|
|
|
|
this.observedCollection = new ObservableCollection<int>();
|
2011-03-08 20:36:43 +00:00
|
|
|
this.observedCollection.Clearing += new EventHandler(
|
|
|
|
this.mockedSubscriber.MockObject.Clearing
|
|
|
|
);
|
|
|
|
this.observedCollection.Cleared += new EventHandler(
|
|
|
|
this.mockedSubscriber.MockObject.Cleared
|
|
|
|
);
|
|
|
|
this.observedCollection.ItemAdded += new EventHandler<ItemEventArgs<int>>(
|
|
|
|
this.mockedSubscriber.MockObject.ItemAdded
|
|
|
|
);
|
|
|
|
this.observedCollection.ItemRemoved += new EventHandler<ItemEventArgs<int>>(
|
|
|
|
this.mockedSubscriber.MockObject.ItemRemoved
|
|
|
|
);
|
2007-04-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Tests whether the Clearing event is fired</summary>
|
|
|
|
[Test]
|
|
|
|
public void TestClearingEvent() {
|
2011-03-08 20:36:43 +00:00
|
|
|
this.mockedSubscriber.Expects.One.Method(m => m.Clearing(null, null)).WithAnyArguments();
|
|
|
|
this.mockedSubscriber.Expects.One.Method(m => m.Cleared(null, null)).WithAnyArguments();
|
2007-04-18 20:20:03 +00:00
|
|
|
this.observedCollection.Clear();
|
|
|
|
|
2007-04-19 18:18:34 +00:00
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
2007-04-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Tests whether the ItemAdded event is fired</summary>
|
|
|
|
[Test]
|
|
|
|
public void TestItemAddedEvent() {
|
2011-03-08 20:36:43 +00:00
|
|
|
this.mockedSubscriber.Expects.One.Method(m => m.ItemAdded(null, null)).WithAnyArguments();
|
2007-04-18 20:20:03 +00:00
|
|
|
|
|
|
|
this.observedCollection.Add(123);
|
|
|
|
|
2007-04-19 18:18:34 +00:00
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
2007-04-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Tests whether the ItemRemoved event is fired</summary>
|
|
|
|
[Test]
|
|
|
|
public void TestItemRemovedEvent() {
|
2011-03-08 20:36:43 +00:00
|
|
|
this.mockedSubscriber.Expects.One.Method(m => m.ItemAdded(null, null)).WithAnyArguments();
|
2007-04-18 20:20:03 +00:00
|
|
|
|
2007-07-10 19:25:18 +00:00
|
|
|
this.observedCollection.Add(123);
|
|
|
|
|
2011-03-08 20:36:43 +00:00
|
|
|
this.mockedSubscriber.Expects.One.Method(m => m.ItemRemoved(null, null)).WithAnyArguments();
|
2007-04-18 20:20:03 +00:00
|
|
|
|
|
|
|
this.observedCollection.Remove(123);
|
|
|
|
|
2007-04-19 18:18:34 +00:00
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
2007-04-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
2011-08-24 11:33:22 +00:00
|
|
|
/// <summary>Tests whether a the list constructor is working</summary>
|
2008-11-27 18:56:08 +00:00
|
|
|
[Test]
|
|
|
|
public void TestListConstructor() {
|
|
|
|
int[] integers = new int[] { 12, 34, 56, 78 };
|
|
|
|
|
2011-08-24 11:33:22 +00:00
|
|
|
var testCollection = new ObservableCollection<int>(integers);
|
2009-10-12 19:19:58 +00:00
|
|
|
|
|
|
|
CollectionAssert.AreEqual(integers, testCollection);
|
2008-11-27 18:56:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>Mock object factory</summary>
|
2011-03-08 20:36:43 +00:00
|
|
|
private MockFactory mockery;
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>The mocked observable collection subscriber</summary>
|
2011-03-08 20:36:43 +00:00
|
|
|
private Mock<IObservableCollectionSubscriber> mockedSubscriber;
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>An observable collection to which a mock will be subscribed</summary>
|
|
|
|
private ObservableCollection<int> observedCollection;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Collections
|
|
|
|
|
|
|
|
#endif // UNITTEST
|