public void Unsubscribe() { string sentMessage = null; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, "SimpleTest", (sender, args) => sentMessage = args); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(this, "SimpleTest"); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.Null(sentMessage); }
public void NoArgUnsubscribe() { bool sentMessage = false; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(this, "SimpleTest", (sender) => sentMessage = true); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, "SimpleTest"); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.False(sentMessage); }
public void NoArgSingleSubscriber() { bool sentMessage = false; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(this, "SimpleTest", sender => sentMessage = true); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.True(sentMessage); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, "SimpleTest"); }
public void Filter() { string sentMessage = null; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, "SimpleTest", callback: (sender, args) => sentMessage = args, source: this); SafeMessagingCenter.Send(new SafeMessagingCenterTests(), "SimpleTest", "My Message"); Assert.Null(sentMessage); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.Equal("My Message", sentMessage); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(this, "SimpleTest"); }
public void MultipleSubscribersOfTheSameClass() { var sub1 = new object(); var sub2 = new object(); string args2 = null; const string message = "message"; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub1, message, (sender, args) => { }); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub2, message, (sender, args) => args2 = args); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(sub1, message); SafeMessagingCenter.Send(this, message, "Testing"); Assert.Equal("Testing", args2); //unsubscribing sub1 should not unsubscribe sub2 }
public void NoArgFilter() { bool sentMessage = false; SafeMessagingCenter.Subscribe(this, "SimpleTest", (sender) => sentMessage = true, source: this); SafeMessagingCenter.Send(new SafeMessagingCenterTests(), "SimpleTest"); Assert.False(sentMessage); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.True(sentMessage); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, "SimpleTest"); }
public void MultiSubscriber() { var sub1 = new object(); var sub2 = new object(); string sentMessage1 = null; string sentMessage2 = null; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub1, "SimpleTest", (sender, args) => sentMessage1 = args); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub2, "SimpleTest", (sender, args) => sentMessage2 = args); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.Equal("My Message", sentMessage1); Assert.Equal("My Message", sentMessage2); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(sub1, "SimpleTest"); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(sub2, "SimpleTest"); }
public void NoArgMultiSubscriber() { var sub1 = new object(); var sub2 = new object(); bool sentMessage1 = false; bool sentMessage2 = false; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(sub1, "SimpleTest", (sender) => sentMessage1 = true); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(sub2, "SimpleTest", (sender) => sentMessage2 = true); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.True(sentMessage1); Assert.True(sentMessage2); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(sub1, "SimpleTest"); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(sub2, "SimpleTest"); }
public void ThrowOnNullArgs() { Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(null, "Foo", (sender, args) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, null, (sender, args) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, "Foo", null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(null, "Foo", (sender) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(this, null, (sender) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe(this, "Foo", (Action <SafeMessagingCenterTests>)null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests, string>(null, "Foo", "Bar")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests, string>(this, null, "Bar")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests>(null, "Foo")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests>(this, null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(null, "Foo")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(null, "Foo")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(this, null)); }
public void UnsubscribeInCallback() { int messageCount = 0; var subscriber1 = new object(); var subscriber2 = new object(); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(subscriber1, "SimpleTest", (sender) => { messageCount++; SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(subscriber2, "SimpleTest"); }); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(subscriber2, "SimpleTest", (sender) => { messageCount++; SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(subscriber1, "SimpleTest"); }); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.Equal(1, messageCount); }