diff --git a/Source/Collections/IObservableCollection.cs b/Source/Collections/IObservableCollection.cs index 295c165..99b934d 100644 --- a/Source/Collections/IObservableCollection.cs +++ b/Source/Collections/IObservableCollection.cs @@ -42,6 +42,9 @@ namespace Nuclex.Support.Collections { /// event EventHandler Clearing; + /// Raised when the collection has been cleared of its items + event EventHandler Cleared; + } } // namespace Nuclex.Support.Collections diff --git a/Source/Collections/ObservableCollection.Test.cs b/Source/Collections/ObservableCollection.Test.cs index cd539cd..a1e4337 100644 --- a/Source/Collections/ObservableCollection.Test.cs +++ b/Source/Collections/ObservableCollection.Test.cs @@ -42,6 +42,11 @@ namespace Nuclex.Support.Collections { /// Not used void Clearing(object sender, EventArgs arguments); + /// Called when the collection has been cleared of its contents + /// Collection that was cleared of its contents + /// Not used + void Cleared(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 @@ -64,7 +69,10 @@ namespace Nuclex.Support.Collections { this.mockedSubscriber = this.mockery.NewMock(); this.observedCollection = new ObservableCollection(); - this.observedCollection.Clearing += new EventHandler(this.mockedSubscriber.Clearing); + this.observedCollection.Clearing += + new EventHandler(this.mockedSubscriber.Clearing); + this.observedCollection.Cleared += + new EventHandler(this.mockedSubscriber.Cleared); this.observedCollection.ItemAdded += new EventHandler>( this.mockedSubscriber.ItemAdded @@ -78,9 +86,8 @@ namespace Nuclex.Support.Collections { /// Tests whether the Clearing event is fired [Test] public void TestClearingEvent() { - Expect.Once.On(this.mockedSubscriber). - Method("Clearing"); - + Expect.Once.On(this.mockedSubscriber).Method("Clearing").WithAnyArguments(); + Expect.Once.On(this.mockedSubscriber).Method("Cleared").WithAnyArguments(); this.observedCollection.Clear(); this.mockery.VerifyAllExpectationsHaveBeenMet(); @@ -89,9 +96,7 @@ namespace Nuclex.Support.Collections { /// Tests whether the ItemAdded event is fired [Test] public void TestItemAddedEvent() { - Expect.Once.On(this.mockedSubscriber). - Method("ItemAdded"). - WithAnyArguments(); + Expect.Once.On(this.mockedSubscriber).Method("ItemAdded").WithAnyArguments(); this.observedCollection.Add(123); @@ -101,15 +106,11 @@ namespace Nuclex.Support.Collections { /// Tests whether the ItemRemoved event is fired [Test] public void TestItemRemovedEvent() { - Expect.Once.On(this.mockedSubscriber). - Method("ItemAdded"). - WithAnyArguments(); + Expect.Once.On(this.mockedSubscriber).Method("ItemAdded").WithAnyArguments(); this.observedCollection.Add(123); - Expect.Once.On(this.mockedSubscriber). - Method("ItemRemoved"). - WithAnyArguments(); + Expect.Once.On(this.mockedSubscriber).Method("ItemRemoved").WithAnyArguments(); this.observedCollection.Remove(123); @@ -119,28 +120,20 @@ namespace Nuclex.Support.Collections { /// Tests whether items in the collection can be replaced [Test] public void TestItemReplacement() { - Expect.Exactly(3).On(this.mockedSubscriber). - Method("ItemAdded"). - WithAnyArguments(); + 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(); + 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) + 1, this.observedCollection.IndexOf(4) ); this.mockery.VerifyAllExpectationsHaveBeenMet(); @@ -152,11 +145,8 @@ namespace Nuclex.Support.Collections { int[] integers = new int[] { 12, 34, 56, 78 }; ObservableCollection testCollection = new ObservableCollection(integers); - - CollectionAssert.AreEqual( - integers, - testCollection - ); + + CollectionAssert.AreEqual(integers, testCollection); } /// Mock object factory diff --git a/Source/Collections/ObservableCollection.cs b/Source/Collections/ObservableCollection.cs index 73f629e..fff160f 100644 --- a/Source/Collections/ObservableCollection.cs +++ b/Source/Collections/ObservableCollection.cs @@ -39,6 +39,8 @@ namespace Nuclex.Support.Collections { /// to process the clearing of the entire collection as a special operation. /// public event EventHandler Clearing; + /// Raised when the collection has been cleared + public event EventHandler Cleared; /// /// Initializes a new instance of the ObservableCollection class that is empty. @@ -58,8 +60,8 @@ namespace Nuclex.Support.Collections { /// Removes all elements from the Collection protected override void ClearItems() { OnClearing(); - base.ClearItems(); + OnCleared(); } /// @@ -71,7 +73,6 @@ namespace Nuclex.Support.Collections { /// The zero-based index at which item should be inserted protected override void InsertItem(int index, ItemType item) { base.InsertItem(index, item); - OnAdded(item); } @@ -81,9 +82,7 @@ namespace Nuclex.Support.Collections { /// The zero-based index of the element to remove protected override void RemoveItem(int index) { ItemType item = base[index]; - base.RemoveItem(index); - OnRemoved(item); } @@ -95,9 +94,7 @@ namespace Nuclex.Support.Collections { /// The zero-based index of the element to replace protected override void SetItem(int index, ItemType item) { ItemType oldItem = base[index]; - base.SetItem(index, item); - OnRemoved(oldItem); OnAdded(item); } @@ -122,6 +119,12 @@ namespace Nuclex.Support.Collections { Clearing(this, EventArgs.Empty); } + /// Fires the 'Cleared' event + protected virtual void OnCleared() { + if(Cleared != null) + Cleared(this, EventArgs.Empty); + } + } } // namespace Nuclex.Support.Collections diff --git a/Source/Collections/ObservableDictionary.Test.cs b/Source/Collections/ObservableDictionary.Test.cs index befd74b..c8defc8 100644 --- a/Source/Collections/ObservableDictionary.Test.cs +++ b/Source/Collections/ObservableDictionary.Test.cs @@ -45,6 +45,11 @@ namespace Nuclex.Support.Collections { /// Not used void Clearing(object sender, EventArgs arguments); + /// Called when the dictionary has been clear of its contents + /// Dictionary that was cleared of its contents + /// Not used + void Cleared(object sender, EventArgs arguments); + /// Called when an item is added to the dictionary /// Dictionary to which an item is being added /// Contains the item that is being added @@ -72,7 +77,10 @@ namespace Nuclex.Support.Collections { this.observedDictionary.Add(3, "three"); this.observedDictionary.Add(42, "forty-two"); - this.observedDictionary.Clearing += new EventHandler(this.mockedSubscriber.Clearing); + this.observedDictionary.Clearing += + new EventHandler(this.mockedSubscriber.Clearing); + this.observedDictionary.Cleared += + new EventHandler(this.mockedSubscriber.Cleared); this.observedDictionary.ItemAdded += new EventHandler>>( this.mockedSubscriber.ItemAdded @@ -293,6 +301,7 @@ namespace Nuclex.Support.Collections { [Test] public void TestClearViaIDictionary() { Expect.Once.On(this.mockedSubscriber).Method("Clearing").WithAnyArguments(); + Expect.Once.On(this.mockedSubscriber).Method("Cleared").WithAnyArguments(); (this.observedDictionary as IDictionary).Clear(); this.mockery.VerifyAllExpectationsHaveBeenMet(); @@ -424,6 +433,7 @@ namespace Nuclex.Support.Collections { [Test] public void TestClearViaGenericICollection() { Expect.Once.On(this.mockedSubscriber).Method("Clearing").WithAnyArguments(); + Expect.Once.On(this.mockedSubscriber).Method("Cleared").WithAnyArguments(); (this.observedDictionary as ICollection>).Clear(); this.mockery.VerifyAllExpectationsHaveBeenMet(); diff --git a/Source/Collections/ObservableDictionary.cs b/Source/Collections/ObservableDictionary.cs index f6e96a6..f7811ae 100644 --- a/Source/Collections/ObservableDictionary.cs +++ b/Source/Collections/ObservableDictionary.cs @@ -32,7 +32,7 @@ namespace Nuclex.Support.Collections { [Serializable] public class ObservableDictionary : #if !NO_SERIALIZATION - ISerializable, + ISerializable, IDeserializationCallback, #endif IDictionary, @@ -76,6 +76,8 @@ namespace Nuclex.Support.Collections { public event EventHandler>> ItemRemoved; /// Raised when the dictionary is about to be cleared public event EventHandler Clearing; + /// Raised when the dictionary has been cleared + public event EventHandler Cleared; /// Initializes a new observable dictionary public ObservableDictionary() : this(new Dictionary()) { } @@ -220,6 +222,7 @@ namespace Nuclex.Support.Collections { public void Clear() { OnClearing(); this.typedDictionary.Clear(); + OnCleared(); } /// Fires the 'ItemAdded' event @@ -242,6 +245,12 @@ namespace Nuclex.Support.Collections { Clearing(this, EventArgs.Empty); } + /// Fires the 'Cleared' event + protected virtual void OnCleared() { + if(Cleared != null) + Cleared(this, EventArgs.Empty); + } + #region IEnumerable implementation /// Returns a new object enumerator for the Dictionary @@ -337,6 +346,7 @@ namespace Nuclex.Support.Collections { void ICollection>.Clear() { OnClearing(); this.typedDictionary.Clear(); + OnCleared(); } /// Removes all items from the Dictionary