Skip to content

Commit

Permalink
WAF unit tests: reuse CollectionEventsTest for ReadOnlyObservableList
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Sep 24, 2023
1 parent 6896571 commit 00e2d7c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
Expand All @@ -10,22 +11,30 @@

namespace Test.Waf.Foundation
{
internal class CollectionEventsTestModel : Model
{
private string? name;

public string? Name { get => name; set => SetProperty(ref name, value); }
}


[TestClass]
public class ObservableListTest
{
[TestMethod]
public void PropertyChangedEventTest()
{
var list = new ObservableList<TestModel>(new[] { new TestModel { Name = "first" } });
var list = new ObservableList<CollectionEventsTestModel>(new[] { new CollectionEventsTestModel { Name = "first" } });

bool countChangedHandlerCalled = false;
list.PropertyChanged += PropertyChangedHandler;
list.Add(new TestModel { Name = "second" });
list.Add(new() { Name = "second" });
Assert.IsTrue(countChangedHandlerCalled);

countChangedHandlerCalled = false;
list.PropertyChanged -= PropertyChangedHandler;
list.Add(new TestModel { Name = "third" });
list.Add(new() { Name = "third" });
Assert.IsFalse(countChangedHandlerCalled);

void PropertyChangedHandler(object? sender, PropertyChangedEventArgs e)
Expand All @@ -36,48 +45,52 @@ void PropertyChangedHandler(object? sender, PropertyChangedEventArgs e)
}

[TestMethod]
public void CollectionChangingEventTest()
public void CollectionEventsTest()
{
var list = new ObservableList<CollectionEventsTestModel>();
CollectionEventsTestCore(list, list);
}

internal static void CollectionEventsTestCore(ObservableCollection<CollectionEventsTestModel> source, object observable)
{
var list = new ObservableList<TestModel>();

var collectionChangingArgs = new List<NotifyCollectionChangedEventArgs>();
var collectionChangedArgs = new List<NotifyCollectionChangedEventArgs>();

list.CollectionChanging += CollectionChangingHandler;
list.CollectionChanged += CollectionChangedHandler;
list.Add(new TestModel { Name = "first" });
((INotifyCollectionChanging)observable).CollectionChanging += CollectionChangingHandler;
((INotifyCollectionChanged)observable).CollectionChanged += CollectionChangedHandler;
source.Add(new() { Name = "first" });
AssertLastListEvent();

list.Insert(1, new TestModel { Name = "second" });
source.Insert(1, new() { Name = "second" });
AssertLastListEvent();

list.Insert(1, new TestModel { Name = "third" });
source.Insert(1, new() { Name = "third" });
AssertLastListEvent();

list.Move(2, 0);
source.Move(2, 0);
AssertLastListEvent();

list[1] = new TestModel { Name = "fourth" };
source[1] = new() { Name = "fourth" };
AssertLastListEvent();

list.Remove(list[0]);
source.Remove(source[0]);
AssertLastListEvent();

list.Clear();
source.Clear();
AssertLastListEvent();

Assert.AreEqual(7, collectionChangingArgs.Count);

void CollectionChangingHandler(object? sender, NotifyCollectionChangedEventArgs e)
{
Assert.AreEqual(collectionChangedArgs.Count, collectionChangingArgs.Count);
Assert.AreEqual(list, sender);
Assert.AreEqual(observable, sender);
collectionChangingArgs.Add(e);
}

void CollectionChangedHandler(object? sender, NotifyCollectionChangedEventArgs e)
{
Assert.AreEqual(list, sender);
Assert.AreEqual(observable, sender);
collectionChangedArgs.Add(e);
Assert.AreEqual(collectionChangedArgs.Count, collectionChangingArgs.Count);
}
Expand All @@ -95,13 +108,5 @@ static void AssertEqualEventArgs(NotifyCollectionChangedEventArgs expected, Noti
static IEnumerable<object> ToGeneric(IList? list) => list?.OfType<object>() ?? Array.Empty<object>();
}
}


private class TestModel : Model
{
private string? name;

public string? Name { get => name; set => SetProperty(ref name, value); }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ void PropertyChangedHandler(object? sender, PropertyChangedEventArgs e)
}
}

[TestMethod]
public void CollectionEventsTest()
{
var list = new ObservableList<CollectionEventsTestModel>();
var readOnlyList = new ReadOnlyObservableList<CollectionEventsTestModel>(list);
ObservableListTest.CollectionEventsTestCore(list, readOnlyList);
}

[TestMethod]
public void EmptyPropertyTest()
{
Expand Down

0 comments on commit 00e2d7c

Please sign in to comment.