Ejemplo n.º 1
0
        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?");
        }
Ejemplo n.º 2
0
            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));
            }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
            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"));
            }
Ejemplo n.º 5
0
        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"));
        }
Ejemplo n.º 6
0
        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")));
        }
Ejemplo n.º 7
0
        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"));
        }
Ejemplo n.º 8
0
        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")));
        }
Ejemplo n.º 9
0
        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"));
        }
Ejemplo n.º 10
0
        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));
        }
Ejemplo n.º 11
0
        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")));
        }
Ejemplo n.º 12
0
            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));
            }
Ejemplo n.º 13
0
            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"));
            }