|
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace CodeProject.VisualStudio.QualityTools.UnitTestFramework
{
public sealed class CollectionChangedWatcher
{
private INotifyCollectionChanged watchedObject;
private List<NotifyCollectionChangedEventArgs> events = new List<NotifyCollectionChangedEventArgs>();
public CollectionChangedWatcher(INotifyCollectionChanged watchedObject)
{
this.watchedObject = watchedObject;
this.watchedObject.CollectionChanged += watchedObject_CollectionChanged;
}
public void Reset()
{
events.Clear();
}
public IEnumerator<NotifyCollectionChangedEventArgs> Find(NotifyCollectionChangedAction action)
{
foreach (NotifyCollectionChangedEventArgs e in events)
{
if (e.Action.Equals(action))
{
yield return e;
}
}
}
public IEnumerator<NotifyCollectionChangedEventArgs> Find(NotifyCollectionChangedAction action, ICollection items)
{
foreach (NotifyCollectionChangedEventArgs e in events)
{
if (e.Action.Equals(action))
{
ICollection eventItems = null;
switch (action)
{
case NotifyCollectionChangedAction.Add:
eventItems = e.NewItems;
break;
case NotifyCollectionChangedAction.Remove:
eventItems = e.OldItems;
break;
default:
continue;
}
if (items.Count != eventItems.Count)
continue;
int actualOccurrences;
int expectedOccurrences;
object mismatchedObject;
if (Specify.FindMismatchedElement(eventItems, items, out actualOccurrences,
out expectedOccurrences, out mismatchedObject))
{
continue;
}
yield return e;
}
}
}
public IEnumerator<NotifyCollectionChangedEventArgs> Find(
NotifyCollectionChangedAction action, ICollection newItems, ICollection oldItems)
{
foreach (NotifyCollectionChangedEventArgs e in events)
{
if (e.Action.Equals(action))
{
if (newItems.Count != e.NewItems.Count || oldItems.Count != e.OldItems.Count)
continue;
int actualOccurrences;
int expectedOccurrences;
object mismatchedObject;
if (Specify.FindMismatchedElement(e.NewItems, newItems, out actualOccurrences,
out expectedOccurrences, out mismatchedObject))
{
continue;
}
if (Specify.FindMismatchedElement(e.OldItems, oldItems, out actualOccurrences,
out expectedOccurrences, out mismatchedObject))
{
continue;
}
yield return e;
}
}
}
private void watchedObject_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
events.Add(e);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Windows developer with 10+ years experience working in the banking industry.