public void DeadInstanceTest() { var target = new MessageMediator(); var tc = new TestCounter(); WeakReference wr = new WeakReference(tc); target.Register(tc); tc = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); // If it's still alive, might be debug if (wr.IsAlive) { Assert.Inconclusive("Target still alive - debug mode?"); tc = (TestCounter)wr.Target; if (tc != null) { target.Unregister(wr.Target); return; } } bool hadTarget = target.SendMessage("test2", "Test2"); Assert.IsFalse(hadTarget, "Mediator located dead reference - debug mode?"); }
public void ReturnsFalseForUnregisteredHandler() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); Assert.IsFalse(mediator.IsRegistered <string>(recipient, recipient.OnMessage)); Assert.IsFalse(mediator.Unregister <string>(recipient, recipient.OnMessage)); }
public void TestBadRegister() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); Assert.ThrowsException <ArgumentException>(() => target.RegisterHandler <int>("test", tc.MessageHandler5)); target.Unregister(tc); }
public void UnregistersRegisteredHandlerWithTag() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage, "myTag"); Assert.IsTrue(mediator.IsRegistered <string>(recipient, recipient.OnMessage, "myTag")); Assert.IsTrue(mediator.Unregister <string>(recipient, recipient.OnMessage, "myTag")); Assert.IsFalse(mediator.IsRegistered <string>(recipient, recipient.OnMessage, "myTag")); }
public void RegisterInstanceTest() { var target = new MessageMediator(); TestCounter counter = new TestCounter(); target.Register(counter); bool hadTarget = target.SendMessage("test2", "Test2"); Assert.IsTrue(hadTarget, "Mediator did not return success"); Assert.AreEqual(1, counter.Count, "Method did not receive message"); target.Unregister(counter); Assert.IsFalse(target.SendMessage("test2", "Test2")); }
public void TestInterfaceUsingDirectKey() { var target = new MessageMediator(); var tc = new TestImpl(); target.Register(tc); bool hadTarget = target.SendMessage("test", new DataEvent("Test")); Assert.IsTrue(hadTarget, "Mediator did not find target"); Assert.AreEqual(2, tc.Count); target.Unregister(tc); Assert.IsFalse(target.SendMessage("test", new DataEvent("Test"))); }
public void SendMessage() { var a = new ReceiverA(); var b = new ReceiverB(); var m = new MessageMediator(); m.Register <Message>(a, a.OnMessageReceived); m.Register <Message>(b, b.OnMessageReceived); m.Unregister <Message>(b, b.OnMessageReceived); // this actually unregisters a's handler, not b's handler. m.SendMessage(new Message { Text = "hello" }); Assert.That(a.Received, Is.EqualTo("hello")); }
public void TestInterfaceUsingConcreteClass() { var target = new MessageMediator(); var tc = new TestImpl(); target.Register(tc); // Should invoke two methods - interface AND direct bool hadTarget = target.SendMessage(new DataEvent("Test")); Assert.IsTrue(hadTarget, "Mediator did not find target"); Assert.AreEqual(5, tc.Count); target.Unregister(tc); Assert.IsFalse(target.SendMessage(new DataEvent("Test"))); }
public void TypeTest() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); bool hadTarget = target.SendMessage("Test2"); Assert.IsTrue(hadTarget, "Mediator did not return success"); hadTarget = target.SendMessage("test3", "Test2"); Assert.IsTrue(hadTarget, "Mediator did not return success"); Assert.AreEqual(2, tc.Count); target.Unregister(tc); Assert.IsFalse(target.SendMessage("test3", "Test2")); }
public void TestIntParam2() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); target.RegisterHandler <int>("test6", tc.MessageHandler6); bool hadTarget = target.SendMessage(5); Assert.IsFalse(hadTarget, "Mediator found unexpected target"); hadTarget = target.SendMessage("test6", 5); Assert.IsTrue(hadTarget, "Mediator did not find target"); Assert.AreEqual(5, tc.Count); target.UnregisterHandler <int>("test6", tc.MessageHandler6); target.Unregister(tc); Assert.IsFalse(target.SendMessage("test6", 5)); }
public void TestJustInterfaces() { var target = new MessageMediator(); var tc = new TestImplJustInterfaces(); target.Register(tc); bool hadTarget = target.SendMessage(new DataEvent("Test")); Assert.IsTrue(hadTarget, "Mediator did not find target"); hadTarget = target.SendMessage(new DataEventUpper("Test")); Assert.IsTrue(hadTarget, "Mediator did not find target"); hadTarget = target.SendMessage(new DataEventLower("Test")); Assert.IsTrue(hadTarget, "Mediator did not find target"); target.Unregister(tc); Assert.IsFalse(target.SendMessage(new DataEvent("Test"))); Assert.IsFalse(target.SendMessage(new DataEventUpper("Test"))); Assert.IsFalse(target.SendMessage(new DataEventLower("Test"))); }
public void ReturnsFalseForUnregisteredHandler() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.OnMessage)); Assert.IsFalse(mediator.Unregister<string>(recipient, recipient.OnMessage)); }
public void UnregistersRegisteredHandlerWithTag() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage, "myTag"); Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage, "myTag")); Assert.IsTrue(mediator.Unregister<string>(recipient, recipient.OnMessage, "myTag")); Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.OnMessage, "myTag")); }