-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WAF WeakEvent add support for CollectionChanging and CollectionItemCh…
…anged
- Loading branch information
Showing
5 changed files
with
222 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/System.Waf/System.Waf/System.Waf.Core.Test/Foundation/WeakEventCollectionChangingTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Specialized; | ||
using System.Waf.Foundation; | ||
using System.Waf.UnitTesting; | ||
|
||
namespace Test.Waf.Foundation | ||
{ | ||
[TestClass] | ||
public class WeakEventCollectionChangingTest : WeakEventTestBase<WeakEventCollectionChangingTest.Manager, WeakEventCollectionChangingTest.Publisher, WeakEventCollectionChangingTest.Subscriber> | ||
{ | ||
[TestMethod] | ||
public void WeakEventAddArgumentException() | ||
{ | ||
AssertHelper.ExpectedException<ArgumentNullException>(() => WeakEvent.CollectionChanging.Add(null!, (s, h) => { })); | ||
AssertHelper.ExpectedException<ArgumentNullException>(() => WeakEvent.CollectionChanging.Add(new Publisher(), null!)); | ||
} | ||
|
||
public class Manager : IManager<Publisher, Subscriber> | ||
{ | ||
public IWeakEventProxy? Proxy { get; set; } | ||
|
||
public void Add(Publisher publisher, Subscriber subscriber) => Proxy = WeakEvent.CollectionChanging.Add(publisher, subscriber.Handler); | ||
} | ||
|
||
public class Publisher : IPublisher, INotifyCollectionChanging | ||
{ | ||
private static readonly NotifyCollectionChangedEventArgs args = new(NotifyCollectionChangedAction.Reset); | ||
private NotifyCollectionChangedEventHandler? collectionChanging; | ||
|
||
public int EventHandlerCount { get; private set; } | ||
|
||
public event NotifyCollectionChangedEventHandler? CollectionChanging | ||
{ | ||
add | ||
{ | ||
collectionChanging += value; | ||
EventHandlerCount++; | ||
} | ||
remove | ||
{ | ||
collectionChanging -= value; | ||
EventHandlerCount--; | ||
} | ||
} | ||
|
||
public void RaiseEvent() => collectionChanging?.Invoke(this, args); | ||
} | ||
|
||
public class Subscriber : ISubscriber | ||
{ | ||
public int HandlerCallCount { get; set; } | ||
|
||
public void Handler(object? sender, NotifyCollectionChangedEventArgs e) => HandlerCallCount++; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...stem.Waf/System.Waf/System.Waf.Core.Test/Foundation/WeakEventCollectionItemChangedTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.ComponentModel; | ||
using System.Waf.Foundation; | ||
using System.Waf.UnitTesting; | ||
|
||
namespace Test.Waf.Foundation | ||
{ | ||
[TestClass] | ||
public class WeakEventCollectionItemChangedTest : WeakEventTestBase<WeakEventCollectionItemChangedTest.Manager, WeakEventCollectionItemChangedTest.Publisher, WeakEventCollectionItemChangedTest.Subscriber> | ||
{ | ||
[TestMethod] | ||
public void WeakEventAddArgumentException() | ||
{ | ||
AssertHelper.ExpectedException<ArgumentNullException>(() => WeakEvent.CollectionItemChanged.Add(null!, (s, h) => { })); | ||
AssertHelper.ExpectedException<ArgumentNullException>(() => WeakEvent.CollectionItemChanged.Add(new Publisher(), null!)); | ||
} | ||
|
||
public class Manager : IManager<Publisher, Subscriber> | ||
{ | ||
public IWeakEventProxy? Proxy { get; set; } | ||
|
||
public void Add(Publisher publisher, Subscriber subscriber) => Proxy = WeakEvent.CollectionItemChanged.Add(publisher, subscriber.Handler); | ||
} | ||
|
||
public class Publisher : IPublisher, INotifyCollectionItemChanged | ||
{ | ||
private static readonly PropertyChangedEventArgs args = new("Test"); | ||
private PropertyChangedEventHandler? collectionItemChanged; | ||
|
||
public int EventHandlerCount { get; private set; } | ||
|
||
public event PropertyChangedEventHandler? CollectionItemChanged | ||
{ | ||
add | ||
{ | ||
collectionItemChanged += value; | ||
EventHandlerCount++; | ||
} | ||
remove | ||
{ | ||
collectionItemChanged -= value; | ||
EventHandlerCount--; | ||
} | ||
} | ||
|
||
public void RaiseEvent() => collectionItemChanged?.Invoke(this, args); | ||
} | ||
|
||
public class Subscriber : ISubscriber | ||
{ | ||
public int HandlerCallCount { get; set; } | ||
|
||
public void Handler(object? sender, PropertyChangedEventArgs e) => HandlerCallCount++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters