public void SubscribesToMessagesWithTagsCorrectly()
            {
                var messageMediator = new MessageMediator();
                var recipient = new MessageRecipient();

                Assert.AreEqual(0, recipient.MessagesReceivedViaMessageMediatorWithoutTag);
                Assert.AreEqual(0, recipient.MessagesReceivedViaMessageMediatorWithTag);

                recipient.SubscribeViaMessageMediatorHelper(messageMediator);

                messageMediator.SendMessage("test", "tag");
                messageMediator.SendMessage("test 2", "tag");

                Assert.AreEqual(0, recipient.MessagesReceivedViaMessageMediatorWithoutTag);
                Assert.AreEqual(2, recipient.MessagesReceivedViaMessageMediatorWithTag);              
            }
            public void CleanUpKeepsNonGarbageCollectedHandlersRegistered()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage);

                mediator.CleanUp();

                Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage));
            }
            public void UnregistersAllMethodsOfRecipient()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage);
                mediator.Register<string>(recipient, recipient.AnotherOnMessage);
                mediator.Register<string>(recipient, recipient.YetAnotherOnMessage);

                Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage));
                Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.AnotherOnMessage));
                Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.YetAnotherOnMessage));

                mediator.UnregisterRecipient(recipient);

                Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.OnMessage));
                Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.AnotherOnMessage));
                Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.YetAnotherOnMessage));
            }
            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"));
            }
            public void ReturnsFalseForHandlersClearedByGarbageCollector()
            {
                var mediator = new MessageMediator();
                var sender = new MessageSender();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage);

                recipient = null;
                GC.Collect();

                Assert.IsFalse(sender.SendMessage(mediator, "test"));
                Assert.AreEqual(0, mediator.GetRegisteredHandlers<string>().Count, "SendMessage should auto cleanup");
            }
            public void ReturnsTrueForRegisteredHandlersWithTag()
            {
                var mediator = new MessageMediator();
                var sender = new MessageSender();
                var recipient = new MessageRecipient();

                // Double registration with separate tags is possible
                mediator.Register<string>(recipient, recipient.OnMessage, "myTag");
                mediator.Register<string>(recipient, recipient.OnMessage, "anotherTag");

                Assert.IsTrue(sender.SendMessage(mediator, "test", "myTag"));
                Assert.AreEqual(1, recipient.MessagesReceived);
            }
            public void ReturnsTrueForRegisteredHandlers()
            {
                var mediator = new MessageMediator();
                var sender = new MessageSender();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage);

                Assert.IsTrue(sender.SendMessage(mediator, "test"));
                Assert.AreEqual(1, recipient.MessagesReceived);
            }
            public void ReturnsFalseForUnregisteredHandlersWithTag()
            {
                var mediator = new MessageMediator();
                var sender = new MessageSender();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage);

                Assert.IsFalse(sender.SendMessage(mediator, "test", "myTag"));
            }
            public void ReturnsFalsForDoubleRegistration()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                Assert.IsTrue(mediator.Register<string>(recipient, recipient.OnMessage));
                Assert.IsFalse(mediator.Register<string>(recipient, recipient.OnMessage));
            }
            public void RegistersHandler()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.OnMessage));

                mediator.Register<string>(recipient, recipient.OnMessage);

                Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage));
            }
            public void ReturnsTrueWhenRegisteredWithAndWithoutTag()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage, "myTag");
                mediator.Register<string>(recipient, recipient.AnotherOnMessage);

                Assert.IsTrue(mediator.IsMessageRegistered(typeof(string)));
            }
            public void ReturnsFalseWhenRegisteredWithTagButNotProvided()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage, "myTag");

                Assert.IsFalse(mediator.IsMessageRegistered(typeof(string)));
            }
            public void ReturnsTrueWhenRegistered()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage);

                Assert.IsTrue(mediator.IsMessageRegistered(typeof (string)));
            }
            public void CleanUpClearsGarbageCollectedHandlers()
            {
                var mediator = new MessageMediator();
                var recipient = new MessageRecipient();

                mediator.Register<string>(recipient, recipient.OnMessage);

                recipient = null;
                GC.Collect();

                mediator.CleanUp();

                Assert.AreEqual(0, mediator.GetRegisteredHandlers<string>().Count);
            }